]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
Change FSF Address
[~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
60 static void               gtk_bin_get_preferred_width_for_height  (GtkWidget           *widget,
61                                                                    gint                 height,
62                                                                    gint                *minimum_width,
63                                                                    gint                *natural_width);
64 static void               gtk_bin_get_preferred_height_for_width  (GtkWidget           *widget,
65                                                                    gint                 width,
66                                                                    gint                *minimum_height,
67                                                                    gint                *natural_height);
68
69 G_DEFINE_ABSTRACT_TYPE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER)
70
71 static void
72 gtk_bin_class_init (GtkBinClass *class)
73 {
74   GtkWidgetClass *widget_class = (GtkWidgetClass*) class;
75   GtkContainerClass *container_class = (GtkContainerClass*) class;
76
77   widget_class->get_preferred_width_for_height = gtk_bin_get_preferred_width_for_height;
78   widget_class->get_preferred_height_for_width = gtk_bin_get_preferred_height_for_width;
79
80   container_class->add = gtk_bin_add;
81   container_class->remove = gtk_bin_remove;
82   container_class->forall = gtk_bin_forall;
83   container_class->child_type = gtk_bin_child_type;
84
85   g_type_class_add_private (class, sizeof (GtkBinPrivate));
86 }
87
88 static void
89 gtk_bin_init (GtkBin *bin)
90 {
91   GtkBinPrivate *priv;
92
93   bin->priv = G_TYPE_INSTANCE_GET_PRIVATE (bin,
94                                            GTK_TYPE_BIN,
95                                            GtkBinPrivate);
96   priv = bin->priv;
97
98   gtk_widget_set_has_window (GTK_WIDGET (bin), FALSE);
99
100   priv->child = NULL;
101 }
102
103
104 static GType
105 gtk_bin_child_type (GtkContainer *container)
106 {
107   GtkBinPrivate *priv = GTK_BIN (container)->priv;
108
109   if (!priv->child)
110     return GTK_TYPE_WIDGET;
111   else
112     return G_TYPE_NONE;
113 }
114
115 static void
116 gtk_bin_add (GtkContainer *container,
117              GtkWidget    *child)
118 {
119   GtkBin *bin = GTK_BIN (container);
120   GtkBinPrivate *priv = bin->priv;
121
122   if (priv->child != NULL)
123     {
124       g_warning ("Attempting to add a widget with type %s to a %s, "
125                  "but as a GtkBin subclass a %s can only contain one widget at a time; "
126                  "it already contains a widget of type %s",
127                  g_type_name (G_OBJECT_TYPE (child)),
128                  g_type_name (G_OBJECT_TYPE (bin)),
129                  g_type_name (G_OBJECT_TYPE (bin)),
130                  g_type_name (G_OBJECT_TYPE (priv->child)));
131       return;
132     }
133
134   gtk_widget_set_parent (child, GTK_WIDGET (bin));
135   priv->child = child;
136 }
137
138 static void
139 gtk_bin_remove (GtkContainer *container,
140                 GtkWidget    *child)
141 {
142   GtkBin *bin = GTK_BIN (container);
143   GtkBinPrivate *priv = bin->priv;
144   gboolean widget_was_visible;
145
146   g_return_if_fail (priv->child == child);
147
148   widget_was_visible = gtk_widget_get_visible (child);
149   
150   gtk_widget_unparent (child);
151   priv->child = NULL;
152   
153   /* queue resize regardless of gtk_widget_get_visible (container),
154    * since that's what is needed by toplevels, which derive from GtkBin.
155    */
156   if (widget_was_visible)
157     gtk_widget_queue_resize (GTK_WIDGET (container));
158 }
159
160 static void
161 gtk_bin_forall (GtkContainer *container,
162                 gboolean      include_internals,
163                 GtkCallback   callback,
164                 gpointer      callback_data)
165 {
166   GtkBin *bin = GTK_BIN (container);
167   GtkBinPrivate *priv = bin->priv;
168
169   if (priv->child)
170     (* callback) (priv->child, callback_data);
171 }
172
173
174 /* GtkBin widgets define the padding and borders independantly so
175  * we cannot provide a generic get_size() for the same reason
176  * we never implemented size_request() here.
177  *
178  * But for cases where the GtkBin class's padding is constant and
179  * does not vary based on allocation (most cases), we can at least 
180  * deduce a common code path for the get_width_for_height()/get_height_for_width()
181  * cases by using the delta of the base size requsts.
182  */
183 static void
184 get_child_padding_delta (GtkBin *bin,
185                          gint   *delta_h,
186                          gint   *delta_v)
187 {
188   GtkBinPrivate *priv = bin->priv;
189   gint hmin, vmin, hnat, vnat, child_hmin, child_vmin;
190
191   /* we can't use gtk_widget_get_preferred_width() wrapper 
192    * because we want our "original" request, not any external
193    * adjustments from set_size_request() or whatever.  we have
194    * to ask for natural also because NULL isn't allowed for the
195    * direct vfuncs
196    */
197   GTK_WIDGET_GET_CLASS (bin)->get_preferred_width (GTK_WIDGET (bin), &hmin, &hnat);
198   GTK_WIDGET_GET_CLASS (bin)->adjust_size_request (GTK_WIDGET (bin), 
199                                                    GTK_ORIENTATION_HORIZONTAL, &hmin, &hnat);
200
201   GTK_WIDGET_GET_CLASS (bin)->get_preferred_height (GTK_WIDGET (bin), &vmin, &vnat);
202   GTK_WIDGET_GET_CLASS (bin)->adjust_size_request (GTK_WIDGET (bin), 
203                                                    GTK_ORIENTATION_VERTICAL, &vmin, &vnat);
204
205   gtk_widget_get_preferred_width (priv->child, &child_hmin, NULL);
206   gtk_widget_get_preferred_height (priv->child, &child_vmin, NULL);
207
208   *delta_h = hmin - child_hmin;
209   *delta_v = vmin - child_vmin;
210 }
211
212 static void 
213 gtk_bin_get_preferred_width_for_height (GtkWidget *widget,
214                                         gint       height,
215                                         gint      *minimum_width,
216                                         gint      *natural_width)
217 {
218   GtkBin *bin = GTK_BIN (widget);
219   GtkBinPrivate *priv = bin->priv;
220   gint    hdelta, vdelta, child_min, child_nat;
221
222   if (priv->child)
223     {
224       get_child_padding_delta (bin, &hdelta, &vdelta);
225
226       gtk_widget_get_preferred_width_for_height (priv->child,
227                                                  height - vdelta,
228                                                  &child_min, &child_nat);
229
230       if (minimum_width)
231         *minimum_width = child_min + hdelta;
232       
233       if (natural_width)
234         *natural_width = child_nat + hdelta;
235     }
236   else
237     GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
238 }
239
240 static void
241 gtk_bin_get_preferred_height_for_width  (GtkWidget *widget,
242                                          gint       width,
243                                          gint      *minimum_height,
244                                          gint      *natural_height)
245 {
246   GtkBin *bin = GTK_BIN (widget);
247   GtkBinPrivate *priv = bin->priv;
248   gint    hdelta, vdelta, child_min, child_nat;
249
250   if (priv->child)
251     {
252       get_child_padding_delta (bin, &hdelta, &vdelta);
253
254       gtk_widget_get_preferred_height_for_width (priv->child,
255                                                  width - hdelta,
256                                                  &child_min, &child_nat);
257
258       if (minimum_height)
259         *minimum_height = child_min + vdelta;
260       
261       if (natural_height)
262         *natural_height = child_nat + vdelta;
263     }
264   else
265     GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
266 }
267
268
269 /**
270  * gtk_bin_get_child:
271  * @bin: a #GtkBin
272  * 
273  * Gets the child of the #GtkBin, or %NULL if the bin contains
274  * no child widget. The returned widget does not have a reference
275  * added, so you do not need to unref it.
276  *
277  * Return value: (transfer none): pointer to child of the #GtkBin
278  **/
279 GtkWidget*
280 gtk_bin_get_child (GtkBin *bin)
281 {
282   g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
283
284   return bin->priv->child;
285 }
286
287 void
288 _gtk_bin_set_child (GtkBin    *bin,
289                     GtkWidget *widget)
290 {
291   bin->priv->child = widget;
292 }