]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
Merge branch 'native-layout-incubator'
[~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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /**
28  * SECTION:gtkbin
29  * @Short_description: A container with just one child
30  * @Title: GtkBin
31  *
32  * The #GtkBin widget is a container with just one child.
33  * It is not very useful itself, but it is useful for deriving subclasses,
34  * since it provides common code needed for handling a single child widget.
35  *
36  * Many GTK+ widgets are subclasses of #GtkBin, including #GtkWindow,
37  * #GtkButton, #GtkFrame, #GtkHandleBox or #GtkScrolledWindow.
38  */
39
40 #include "config.h"
41 #include "gtkbin.h"
42 #include "gtkextendedlayout.h"
43 #include "gtkintl.h"
44 #include "gtkalias.h"
45
46 static void gtk_bin_add         (GtkContainer   *container,
47                                  GtkWidget      *widget);
48 static void gtk_bin_remove      (GtkContainer   *container,
49                                  GtkWidget      *widget);
50 static void gtk_bin_forall      (GtkContainer   *container,
51                                  gboolean       include_internals,
52                                  GtkCallback     callback,
53                                  gpointer        callback_data);
54 static GType gtk_bin_child_type (GtkContainer   *container);
55
56
57 static void     gtk_bin_extended_layout_init  (GtkExtendedLayoutIface *iface);
58 static gboolean gtk_bin_is_height_for_width   (GtkExtendedLayout      *layout);
59 static void     gtk_bin_get_width_for_height  (GtkExtendedLayout      *layout,
60                                                gint                    height,
61                                                gint                   *minimum_width,
62                                                gint                   *natural_width);
63 static void     gtk_bin_get_height_for_width  (GtkExtendedLayout      *layout,
64                                                gint                    width,
65                                                gint                   *minimum_height,
66                                                gint                   *natural_height);
67
68 static GtkExtendedLayoutIface *parent_extended_layout_iface;
69
70 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER,
71                                   G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
72                                                          gtk_bin_extended_layout_init))
73
74 static void
75 gtk_bin_class_init (GtkBinClass *class)
76 {
77   GtkContainerClass *container_class;
78
79   container_class = (GtkContainerClass*) class;
80
81   container_class->add = gtk_bin_add;
82   container_class->remove = gtk_bin_remove;
83   container_class->forall = gtk_bin_forall;
84   container_class->child_type = gtk_bin_child_type;
85 }
86
87 static void
88 gtk_bin_init (GtkBin *bin)
89 {
90   gtk_widget_set_has_window (GTK_WIDGET (bin), FALSE);
91
92   bin->child = NULL;
93 }
94
95
96 static GType
97 gtk_bin_child_type (GtkContainer *container)
98 {
99   if (!GTK_BIN (container)->child)
100     return GTK_TYPE_WIDGET;
101   else
102     return G_TYPE_NONE;
103 }
104
105 static void
106 gtk_bin_add (GtkContainer *container,
107              GtkWidget    *child)
108 {
109   GtkBin *bin = GTK_BIN (container);
110
111   if (bin->child != NULL)
112     {
113       g_warning ("Attempting to add a widget with type %s to a %s, "
114                  "but as a GtkBin subclass a %s can only contain one widget at a time; "
115                  "it already contains a widget of type %s",
116                  g_type_name (G_OBJECT_TYPE (child)),
117                  g_type_name (G_OBJECT_TYPE (bin)),
118                  g_type_name (G_OBJECT_TYPE (bin)),
119                  g_type_name (G_OBJECT_TYPE (bin->child)));
120       return;
121     }
122
123   gtk_widget_set_parent (child, GTK_WIDGET (bin));
124   bin->child = child;
125 }
126
127 static void
128 gtk_bin_remove (GtkContainer *container,
129                 GtkWidget    *child)
130 {
131   GtkBin *bin = GTK_BIN (container);
132   gboolean widget_was_visible;
133
134   g_return_if_fail (bin->child == child);
135
136   widget_was_visible = gtk_widget_get_visible (child);
137   
138   gtk_widget_unparent (child);
139   bin->child = NULL;
140   
141   /* queue resize regardless of gtk_widget_get_visible (container),
142    * since that's what is needed by toplevels, which derive from GtkBin.
143    */
144   if (widget_was_visible)
145     gtk_widget_queue_resize (GTK_WIDGET (container));
146 }
147
148 static void
149 gtk_bin_forall (GtkContainer *container,
150                 gboolean      include_internals,
151                 GtkCallback   callback,
152                 gpointer      callback_data)
153 {
154   GtkBin *bin = GTK_BIN (container);
155
156   if (bin->child)
157     (* callback) (bin->child, callback_data);
158 }
159
160
161 /* GtkBin widgets define the padding and borders independantly so
162  * we cannot provide a generic get_desired_size() for the same reason
163  * we never implemented size_request() here.
164  *
165  * But for cases where the GtkBin class's padding is constant and
166  * does not vary based on allocation (most cases), we can at least 
167  * deduce a common code path for the get_width_for_height()/get_height_for_width()
168  * cases by using the delta of the base size requsts.
169  */
170 static void 
171 gtk_bin_extended_layout_init (GtkExtendedLayoutIface *iface)
172 {
173   parent_extended_layout_iface = g_type_interface_peek_parent (iface);
174
175   iface->is_height_for_width   = gtk_bin_is_height_for_width;
176   iface->get_width_for_height  = gtk_bin_get_width_for_height;
177   iface->get_height_for_width  = gtk_bin_get_height_for_width;
178 }
179
180 static gboolean 
181 gtk_bin_is_height_for_width (GtkExtendedLayout      *layout)
182 {
183   GtkBin *bin = GTK_BIN (layout);
184
185   if (bin->child)
186     return gtk_extended_layout_is_height_for_width (GTK_EXTENDED_LAYOUT (bin->child));
187
188   return TRUE;
189 }
190
191 static void
192 get_child_padding_delta (GtkBin         *bin,
193                          gint           *delta_h,
194                          gint           *delta_v)
195 {
196   gint hmin, vmin, child_hmin, child_vmin;
197
198   gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (bin), &hmin, NULL);
199   gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (bin), &vmin, NULL);
200
201   gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (bin->child), &child_hmin, NULL);
202   gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (bin->child), &child_vmin, NULL);
203
204   *delta_h = hmin - child_hmin;
205   *delta_v = vmin - child_vmin;
206 }
207
208 static void 
209 gtk_bin_get_width_for_height (GtkExtendedLayout      *layout,
210                               gint                    height,
211                               gint                   *minimum_width,
212                               gint                   *natural_width)
213 {
214   GtkBin *bin = GTK_BIN (layout);
215   gint    hdelta, vdelta, child_min, child_nat;
216
217   if (bin->child)
218     {
219       get_child_padding_delta (bin, &hdelta, &vdelta);
220       
221       gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (bin->child),
222                                                 height - vdelta,
223                                                 &child_min, &child_nat);
224       
225       if (minimum_width)
226         *minimum_width = child_min + hdelta;
227       
228       if (natural_width)
229         *natural_width = child_nat + hdelta;
230     }
231   else
232     GTK_EXTENDED_LAYOUT_GET_IFACE (layout)->get_desired_width (layout, minimum_width, natural_width);
233 }
234
235 static void
236 gtk_bin_get_height_for_width  (GtkExtendedLayout      *layout,
237                                gint                    width,
238                                gint                   *minimum_height,
239                                gint                   *natural_height)
240 {
241   GtkBin *bin = GTK_BIN (layout);
242   gint    hdelta, vdelta, child_min, child_nat;
243
244   if (bin->child)
245     {
246       get_child_padding_delta (bin, &hdelta, &vdelta);
247       
248       gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (bin->child),
249                                                 width - hdelta,
250                                                 &child_min, &child_nat);
251       
252       if (minimum_height)
253         *minimum_height = child_min + vdelta;
254       
255       if (natural_height)
256         *natural_height = child_nat + vdelta;
257     }
258   else
259     GTK_EXTENDED_LAYOUT_GET_IFACE (layout)->get_desired_height (layout, minimum_height, natural_height);
260 }
261
262
263 /**
264  * gtk_bin_get_child:
265  * @bin: a #GtkBin
266  * 
267  * Gets the child of the #GtkBin, or %NULL if the bin contains
268  * no child widget. The returned widget does not have a reference
269  * added, so you do not need to unref it.
270  *
271  * Return value: (transfer none): pointer to child of the #GtkBin
272  **/
273 GtkWidget*
274 gtk_bin_get_child (GtkBin *bin)
275 {
276   g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
277
278   return bin->child;
279 }
280
281 #define __GTK_BIN_C__
282 #include "gtkaliasdef.c"