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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
29 * @Short_description: A container with just one child
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.
36 * Many GTK+ widgets are subclasses of #GtkBin, including #GtkWindow,
37 * #GtkButton, #GtkFrame, #GtkHandleBox or #GtkScrolledWindow.
42 #include "gtksizerequest.h"
46 static void gtk_bin_add (GtkContainer *container,
48 static void gtk_bin_remove (GtkContainer *container,
50 static void gtk_bin_forall (GtkContainer *container,
51 gboolean include_internals,
53 gpointer callback_data);
54 static GType gtk_bin_child_type (GtkContainer *container);
57 static void gtk_bin_size_request_init (GtkSizeRequestIface *iface);
58 static GtkSizeRequestMode gtk_bin_get_request_mode (GtkSizeRequest *widget);
59 static void gtk_bin_get_width_for_height (GtkSizeRequest *widget,
63 static void gtk_bin_get_height_for_width (GtkSizeRequest *widget,
66 gint *natural_height);
68 static GtkSizeRequestIface *parent_size_request_iface;
70 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER,
71 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
72 gtk_bin_size_request_init))
75 gtk_bin_class_init (GtkBinClass *class)
77 GtkContainerClass *container_class;
79 container_class = (GtkContainerClass*) class;
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;
88 gtk_bin_init (GtkBin *bin)
90 gtk_widget_set_has_window (GTK_WIDGET (bin), FALSE);
97 gtk_bin_child_type (GtkContainer *container)
99 if (!GTK_BIN (container)->child)
100 return GTK_TYPE_WIDGET;
106 gtk_bin_add (GtkContainer *container,
109 GtkBin *bin = GTK_BIN (container);
111 if (bin->child != NULL)
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)));
123 gtk_widget_set_parent (child, GTK_WIDGET (bin));
128 gtk_bin_remove (GtkContainer *container,
131 GtkBin *bin = GTK_BIN (container);
132 gboolean widget_was_visible;
134 g_return_if_fail (bin->child == child);
136 widget_was_visible = gtk_widget_get_visible (child);
138 gtk_widget_unparent (child);
141 /* queue resize regardless of gtk_widget_get_visible (container),
142 * since that's what is needed by toplevels, which derive from GtkBin.
144 if (widget_was_visible)
145 gtk_widget_queue_resize (GTK_WIDGET (container));
149 gtk_bin_forall (GtkContainer *container,
150 gboolean include_internals,
151 GtkCallback callback,
152 gpointer callback_data)
154 GtkBin *bin = GTK_BIN (container);
157 (* callback) (bin->child, callback_data);
161 /* GtkBin widgets define the padding and borders independantly so
162 * we cannot provide a generic get_size() for the same reason
163 * we never implemented size_request() here.
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.
171 gtk_bin_size_request_init (GtkSizeRequestIface *iface)
173 parent_size_request_iface = g_type_interface_peek_parent (iface);
175 iface->get_request_mode = gtk_bin_get_request_mode;
176 iface->get_width_for_height = gtk_bin_get_width_for_height;
177 iface->get_height_for_width = gtk_bin_get_height_for_width;
180 static GtkSizeRequestMode
181 gtk_bin_get_request_mode (GtkSizeRequest *widget)
183 GtkBin *bin = GTK_BIN (widget);
186 return gtk_size_request_get_request_mode (GTK_SIZE_REQUEST (bin->child));
188 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
192 get_child_padding_delta (GtkBin *bin,
196 gint hmin, vmin, child_hmin, child_vmin;
198 gtk_size_request_get_width (GTK_SIZE_REQUEST (bin), &hmin, NULL);
199 gtk_size_request_get_height (GTK_SIZE_REQUEST (bin), &vmin, NULL);
201 gtk_size_request_get_width (GTK_SIZE_REQUEST (bin->child), &child_hmin, NULL);
202 gtk_size_request_get_height (GTK_SIZE_REQUEST (bin->child), &child_vmin, NULL);
204 *delta_h = hmin - child_hmin;
205 *delta_v = vmin - child_vmin;
209 gtk_bin_get_width_for_height (GtkSizeRequest *widget,
214 GtkBin *bin = GTK_BIN (widget);
215 gint hdelta, vdelta, child_min, child_nat;
219 get_child_padding_delta (bin, &hdelta, &vdelta);
221 gtk_size_request_get_width_for_height (GTK_SIZE_REQUEST (bin->child),
223 &child_min, &child_nat);
226 *minimum_width = child_min + hdelta;
229 *natural_width = child_nat + hdelta;
232 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);
236 gtk_bin_get_height_for_width (GtkSizeRequest *widget,
238 gint *minimum_height,
239 gint *natural_height)
241 GtkBin *bin = GTK_BIN (widget);
242 gint hdelta, vdelta, child_min, child_nat;
246 get_child_padding_delta (bin, &hdelta, &vdelta);
248 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (bin->child),
250 &child_min, &child_nat);
253 *minimum_height = child_min + vdelta;
256 *natural_height = child_nat + vdelta;
259 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
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.
271 * Return value: (transfer none): pointer to child of the #GtkBin
274 gtk_bin_get_child (GtkBin *bin)
276 g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
281 #define __GTK_BIN_C__
282 #include "gtkaliasdef.c"