]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
gtkbin: Complete more of a base implementation for GtkBin subclasses
[~andy/gtk] / gtk / gtkbin.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 /**
26  * SECTION:gtkbin
27  * @Short_description: A container with just one child
28  * @Title: GtkBin
29  *
30  * The #GtkBin widget is a container with just one child.
31  * It is not very useful itself, but it is useful for deriving subclasses,
32  * since it provides common code needed for handling a single child widget.
33  *
34  * Many GTK+ widgets are subclasses of #GtkBin, including #GtkWindow,
35  * #GtkButton, #GtkFrame, #GtkHandleBox or #GtkScrolledWindow.
36  */
37
38 #include "config.h"
39 #include "gtkbin.h"
40 #include "gtksizerequest.h"
41 #include "gtkintl.h"
42
43
44 struct _GtkBinPrivate
45 {
46   GtkWidget *child;
47 };
48
49 static void gtk_bin_add         (GtkContainer   *container,
50                                  GtkWidget      *widget);
51 static void gtk_bin_remove      (GtkContainer   *container,
52                                  GtkWidget      *widget);
53 static void gtk_bin_forall      (GtkContainer   *container,
54                                  gboolean       include_internals,
55                                  GtkCallback     callback,
56                                  gpointer        callback_data);
57 static GType gtk_bin_child_type (GtkContainer   *container);
58
59 static void               gtk_bin_get_preferred_width             (GtkWidget           *widget,
60                                                                    gint                *minimum_width,
61                                                                    gint                *natural_width);
62 static void               gtk_bin_get_preferred_height            (GtkWidget           *widget,
63                                                                    gint                *minimum_height,
64                                                                    gint                *natural_height);
65 static void               gtk_bin_get_preferred_width_for_height  (GtkWidget           *widget,
66                                                                    gint                 height,
67                                                                    gint                *minimum_width,
68                                                                    gint                *natural_width);
69 static void               gtk_bin_get_preferred_height_for_width  (GtkWidget           *widget,
70                                                                    gint                 width,
71                                                                    gint                *minimum_height,
72                                                                    gint                *natural_height);
73 static void               gtk_bin_size_allocate                   (GtkWidget           *widget,
74                                                                    GtkAllocation       *allocation);
75
76 G_DEFINE_ABSTRACT_TYPE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER)
77
78 static void
79 gtk_bin_class_init (GtkBinClass *class)
80 {
81   GtkWidgetClass *widget_class = (GtkWidgetClass*) class;
82   GtkContainerClass *container_class = (GtkContainerClass*) class;
83
84   widget_class->get_preferred_width = gtk_bin_get_preferred_width;
85   widget_class->get_preferred_height = gtk_bin_get_preferred_height;
86   widget_class->get_preferred_width_for_height = gtk_bin_get_preferred_width_for_height;
87   widget_class->get_preferred_height_for_width = gtk_bin_get_preferred_height_for_width;
88   widget_class->size_allocate = gtk_bin_size_allocate;
89
90   container_class->add = gtk_bin_add;
91   container_class->remove = gtk_bin_remove;
92   container_class->forall = gtk_bin_forall;
93   container_class->child_type = gtk_bin_child_type;
94
95   g_type_class_add_private (class, sizeof (GtkBinPrivate));
96 }
97
98 static void
99 gtk_bin_init (GtkBin *bin)
100 {
101   GtkBinPrivate *priv;
102
103   bin->priv = G_TYPE_INSTANCE_GET_PRIVATE (bin,
104                                            GTK_TYPE_BIN,
105                                            GtkBinPrivate);
106   priv = bin->priv;
107
108   gtk_widget_set_has_window (GTK_WIDGET (bin), FALSE);
109
110   priv->child = NULL;
111 }
112
113
114 static GType
115 gtk_bin_child_type (GtkContainer *container)
116 {
117   GtkBinPrivate *priv = GTK_BIN (container)->priv;
118
119   if (!priv->child)
120     return GTK_TYPE_WIDGET;
121   else
122     return G_TYPE_NONE;
123 }
124
125 static void
126 gtk_bin_add (GtkContainer *container,
127              GtkWidget    *child)
128 {
129   GtkBin *bin = GTK_BIN (container);
130   GtkBinPrivate *priv = bin->priv;
131
132   if (priv->child != NULL)
133     {
134       g_warning ("Attempting to add a widget with type %s to a %s, "
135                  "but as a GtkBin subclass a %s can only contain one widget at a time; "
136                  "it already contains a widget of type %s",
137                  g_type_name (G_OBJECT_TYPE (child)),
138                  g_type_name (G_OBJECT_TYPE (bin)),
139                  g_type_name (G_OBJECT_TYPE (bin)),
140                  g_type_name (G_OBJECT_TYPE (priv->child)));
141       return;
142     }
143
144   gtk_widget_set_parent (child, GTK_WIDGET (bin));
145   priv->child = child;
146 }
147
148 static void
149 gtk_bin_remove (GtkContainer *container,
150                 GtkWidget    *child)
151 {
152   GtkBin *bin = GTK_BIN (container);
153   GtkBinPrivate *priv = bin->priv;
154   gboolean widget_was_visible;
155
156   g_return_if_fail (priv->child == child);
157
158   widget_was_visible = gtk_widget_get_visible (child);
159   
160   gtk_widget_unparent (child);
161   priv->child = NULL;
162   
163   /* queue resize regardless of gtk_widget_get_visible (container),
164    * since that's what is needed by toplevels, which derive from GtkBin.
165    */
166   if (widget_was_visible)
167     gtk_widget_queue_resize (GTK_WIDGET (container));
168 }
169
170 static void
171 gtk_bin_forall (GtkContainer *container,
172                 gboolean      include_internals,
173                 GtkCallback   callback,
174                 gpointer      callback_data)
175 {
176   GtkBin *bin = GTK_BIN (container);
177   GtkBinPrivate *priv = bin->priv;
178
179   if (priv->child)
180     (* callback) (priv->child, callback_data);
181 }
182
183
184 /* GtkBin widgets define the padding and borders independantly so
185  * we cannot provide a generic get_size() for the same reason
186  * we never implemented size_request() here.
187  *
188  * But for cases where the GtkBin class's padding is constant and
189  * does not vary based on allocation (most cases), we can at least 
190  * deduce a common code path for the get_width_for_height()/get_height_for_width()
191  * cases by using the delta of the base size requsts.
192  */
193 static void
194 get_child_padding_delta (GtkBin *bin,
195                          gint   *delta_h,
196                          gint   *delta_v)
197 {
198   GtkBinPrivate *priv = bin->priv;
199   gint hmin, vmin, hnat, vnat, child_hmin, child_vmin;
200
201   /* we can't use gtk_widget_get_preferred_width() wrapper 
202    * because we want our "original" request, not any external
203    * adjustments from set_size_request() or whatever.  we have
204    * to ask for natural also because NULL isn't allowed for the
205    * direct vfuncs
206    */
207   GTK_WIDGET_GET_CLASS (bin)->get_preferred_width (GTK_WIDGET (bin), &hmin, &hnat);
208   GTK_WIDGET_GET_CLASS (bin)->adjust_size_request (GTK_WIDGET (bin), 
209                                                    GTK_ORIENTATION_HORIZONTAL, &hmin, &hnat);
210
211   GTK_WIDGET_GET_CLASS (bin)->get_preferred_height (GTK_WIDGET (bin), &vmin, &vnat);
212   GTK_WIDGET_GET_CLASS (bin)->adjust_size_request (GTK_WIDGET (bin), 
213                                                    GTK_ORIENTATION_VERTICAL, &vmin, &vnat);
214
215   gtk_widget_get_preferred_width (priv->child, &child_hmin, NULL);
216   gtk_widget_get_preferred_height (priv->child, &child_vmin, NULL);
217
218   *delta_h = hmin - child_hmin;
219   *delta_v = vmin - child_vmin;
220 }
221
222 static void
223 gtk_bin_get_preferred_width (GtkWidget *widget,
224                              gint      *minimum_width,
225                              gint      *natural_width)
226 {
227   GtkBin *bin = GTK_BIN (widget);
228   GtkBinPrivate *priv = bin->priv;
229
230   if (priv->child)
231     {
232       gint child_min, child_nat;
233       gtk_widget_get_preferred_width (priv->child,
234                                       &child_min, &child_nat);
235       *minimum_width = child_min;
236       *natural_width = child_nat;
237     }
238 }
239
240 static void
241 gtk_bin_get_preferred_height (GtkWidget *widget,
242                               gint      *minimum_height,
243                               gint      *natural_height)
244 {
245   GtkBin *bin = GTK_BIN (widget);
246   GtkBinPrivate *priv = bin->priv;
247
248   if (priv->child)
249     {
250       gint child_min, child_nat;
251       gtk_widget_get_preferred_height (priv->child,
252                                        &child_min, &child_nat);
253       *minimum_height = child_min;
254       *natural_height = child_nat;
255     }
256 }
257
258 static void 
259 gtk_bin_get_preferred_width_for_height (GtkWidget *widget,
260                                         gint       height,
261                                         gint      *minimum_width,
262                                         gint      *natural_width)
263 {
264   GtkBin *bin = GTK_BIN (widget);
265   GtkBinPrivate *priv = bin->priv;
266   gint    hdelta, vdelta, child_min, child_nat;
267
268   if (priv->child)
269     {
270       get_child_padding_delta (bin, &hdelta, &vdelta);
271
272       gtk_widget_get_preferred_width_for_height (priv->child,
273                                                  height - vdelta,
274                                                  &child_min, &child_nat);
275
276       if (minimum_width)
277         *minimum_width = child_min + hdelta;
278       
279       if (natural_width)
280         *natural_width = child_nat + hdelta;
281     }
282   else
283     GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
284 }
285
286 static void
287 gtk_bin_get_preferred_height_for_width  (GtkWidget *widget,
288                                          gint       width,
289                                          gint      *minimum_height,
290                                          gint      *natural_height)
291 {
292   GtkBin *bin = GTK_BIN (widget);
293   GtkBinPrivate *priv = bin->priv;
294   gint    hdelta, vdelta, child_min, child_nat;
295
296   if (priv->child)
297     {
298       get_child_padding_delta (bin, &hdelta, &vdelta);
299
300       gtk_widget_get_preferred_height_for_width (priv->child,
301                                                  width - hdelta,
302                                                  &child_min, &child_nat);
303
304       if (minimum_height)
305         *minimum_height = child_min + vdelta;
306       
307       if (natural_height)
308         *natural_height = child_nat + vdelta;
309     }
310   else
311     GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
312 }
313
314 static void
315 gtk_bin_size_allocate (GtkWidget     *widget,
316                        GtkAllocation *allocation)
317 {
318   GtkBin *bin = GTK_BIN (widget);
319   GtkBinPrivate *priv = bin->priv;
320
321   gtk_widget_set_allocation (widget, allocation);
322
323   if (priv->child)
324     gtk_widget_size_allocate (priv->child, allocation);
325 }
326
327 /**
328  * gtk_bin_get_child:
329  * @bin: a #GtkBin
330  * 
331  * Gets the child of the #GtkBin, or %NULL if the bin contains
332  * no child widget. The returned widget does not have a reference
333  * added, so you do not need to unref it.
334  *
335  * Return value: (transfer none): pointer to child of the #GtkBin
336  **/
337 GtkWidget*
338 gtk_bin_get_child (GtkBin *bin)
339 {
340   g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
341
342   return bin->priv->child;
343 }
344
345 void
346 _gtk_bin_set_child (GtkBin    *bin,
347                     GtkWidget *widget)
348 {
349   bin->priv->child = widget;
350 }