1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
27 * @Short_description: A container with just one child
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.
34 * Many GTK+ widgets are subclasses of #GtkBin, including #GtkWindow,
35 * #GtkButton, #GtkFrame, #GtkHandleBox or #GtkScrolledWindow.
40 #include "gtksizerequest.h"
49 static void gtk_bin_add (GtkContainer *container,
51 static void gtk_bin_remove (GtkContainer *container,
53 static void gtk_bin_forall (GtkContainer *container,
54 gboolean include_internals,
56 gpointer callback_data);
57 static GType gtk_bin_child_type (GtkContainer *container);
59 static void gtk_bin_get_preferred_width (GtkWidget *widget,
62 static void gtk_bin_get_preferred_height (GtkWidget *widget,
64 gint *natural_height);
65 static void gtk_bin_get_preferred_width_for_height (GtkWidget *widget,
69 static void gtk_bin_get_preferred_height_for_width (GtkWidget *widget,
72 gint *natural_height);
73 static void gtk_bin_size_allocate (GtkWidget *widget,
74 GtkAllocation *allocation);
76 G_DEFINE_ABSTRACT_TYPE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER)
79 gtk_bin_class_init (GtkBinClass *class)
81 GtkWidgetClass *widget_class = (GtkWidgetClass*) class;
82 GtkContainerClass *container_class = (GtkContainerClass*) class;
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;
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;
95 g_type_class_add_private (class, sizeof (GtkBinPrivate));
99 gtk_bin_init (GtkBin *bin)
103 bin->priv = G_TYPE_INSTANCE_GET_PRIVATE (bin,
108 gtk_widget_set_has_window (GTK_WIDGET (bin), FALSE);
115 gtk_bin_child_type (GtkContainer *container)
117 GtkBinPrivate *priv = GTK_BIN (container)->priv;
120 return GTK_TYPE_WIDGET;
126 gtk_bin_add (GtkContainer *container,
129 GtkBin *bin = GTK_BIN (container);
130 GtkBinPrivate *priv = bin->priv;
132 if (priv->child != NULL)
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)));
144 gtk_widget_set_parent (child, GTK_WIDGET (bin));
149 gtk_bin_remove (GtkContainer *container,
152 GtkBin *bin = GTK_BIN (container);
153 GtkBinPrivate *priv = bin->priv;
154 gboolean widget_was_visible;
156 g_return_if_fail (priv->child == child);
158 widget_was_visible = gtk_widget_get_visible (child);
160 gtk_widget_unparent (child);
163 /* queue resize regardless of gtk_widget_get_visible (container),
164 * since that's what is needed by toplevels, which derive from GtkBin.
166 if (widget_was_visible)
167 gtk_widget_queue_resize (GTK_WIDGET (container));
171 gtk_bin_forall (GtkContainer *container,
172 gboolean include_internals,
173 GtkCallback callback,
174 gpointer callback_data)
176 GtkBin *bin = GTK_BIN (container);
177 GtkBinPrivate *priv = bin->priv;
180 (* callback) (priv->child, callback_data);
184 gtk_bin_get_effective_border_width (GtkBin *bin)
186 if (GTK_CONTAINER_CLASS (GTK_BIN_GET_CLASS (bin))->_handle_border_width)
189 return gtk_container_get_border_width (GTK_CONTAINER (bin));
193 gtk_bin_get_preferred_width (GtkWidget *widget,
197 GtkBin *bin = GTK_BIN (widget);
198 GtkBinPrivate *priv = bin->priv;
201 if (priv->child && gtk_widget_get_visible (priv->child))
203 gint child_min, child_nat;
204 gtk_widget_get_preferred_width (priv->child,
205 &child_min, &child_nat);
206 *minimum_width = child_min;
207 *natural_width = child_nat;
210 border_width = gtk_bin_get_effective_border_width (bin);
211 *minimum_width += 2 * border_width;
212 *natural_width += 2 * border_width;
216 gtk_bin_get_preferred_height (GtkWidget *widget,
217 gint *minimum_height,
218 gint *natural_height)
220 GtkBin *bin = GTK_BIN (widget);
221 GtkBinPrivate *priv = bin->priv;
224 if (priv->child && gtk_widget_get_visible (priv->child))
226 gint child_min, child_nat;
227 gtk_widget_get_preferred_height (priv->child,
228 &child_min, &child_nat);
229 *minimum_height = child_min;
230 *natural_height = child_nat;
233 border_width = gtk_bin_get_effective_border_width (bin);
234 *minimum_height += 2 * border_width;
235 *natural_height += 2 * border_width;
239 gtk_bin_get_preferred_width_for_height (GtkWidget *widget,
244 GtkBin *bin = GTK_BIN (widget);
245 GtkBinPrivate *priv = bin->priv;
248 border_width = gtk_bin_get_effective_border_width (bin);
250 if (priv->child && gtk_widget_get_visible (priv->child))
252 gint child_min, child_nat;
253 gtk_widget_get_preferred_width_for_height (priv->child, height - 2 * border_width,
254 &child_min, &child_nat);
256 *minimum_width = child_min;
257 *natural_width = child_nat;
260 *minimum_width += 2 * border_width;
261 *natural_width += 2 * border_width;
265 gtk_bin_get_preferred_height_for_width (GtkWidget *widget,
267 gint *minimum_height,
268 gint *natural_height)
270 GtkBin *bin = GTK_BIN (widget);
271 GtkBinPrivate *priv = bin->priv;
274 border_width = gtk_bin_get_effective_border_width (bin);
276 if (priv->child && gtk_widget_get_visible (priv->child))
278 gint child_min, child_nat;
279 gtk_widget_get_preferred_height_for_width (priv->child, width - 2 * border_width,
280 &child_min, &child_nat);
282 *minimum_height = child_min;
283 *natural_height = child_nat;
286 *minimum_height += 2 * border_width;
287 *natural_height += 2 * border_width;
291 gtk_bin_size_allocate (GtkWidget *widget,
292 GtkAllocation *allocation)
294 GtkBin *bin = GTK_BIN (widget);
295 GtkBinPrivate *priv = bin->priv;
297 gtk_widget_set_allocation (widget, allocation);
299 if (priv->child && gtk_widget_get_visible (priv->child))
301 GtkAllocation child_allocation;
302 gint border_width = gtk_bin_get_effective_border_width (bin);
304 child_allocation.x = allocation->x + border_width;
305 child_allocation.y = allocation->y + border_width;
306 child_allocation.width = allocation->width - 2 * border_width;
307 child_allocation.height = allocation->height - 2 * border_width;
309 gtk_widget_size_allocate (priv->child, &child_allocation);
317 * Gets the child of the #GtkBin, or %NULL if the bin contains
318 * no child widget. The returned widget does not have a reference
319 * added, so you do not need to unref it.
321 * Return value: (transfer none): pointer to child of the #GtkBin
324 gtk_bin_get_child (GtkBin *bin)
326 g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
328 return bin->priv->child;
332 _gtk_bin_set_child (GtkBin *bin,
335 bin->priv->child = widget;