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/.
30 static void gtk_bin_class_init (GtkBinClass *klass);
31 static void gtk_bin_init (GtkBin *bin);
32 static void gtk_bin_map (GtkWidget *widget);
33 static void gtk_bin_unmap (GtkWidget *widget);
34 static gint gtk_bin_expose (GtkWidget *widget,
35 GdkEventExpose *event);
36 static void gtk_bin_add (GtkContainer *container,
38 static void gtk_bin_remove (GtkContainer *container,
40 static void gtk_bin_forall (GtkContainer *container,
41 gboolean include_internals,
43 gpointer callback_data);
44 static GtkType gtk_bin_child_type (GtkContainer*container);
47 static GtkContainerClass *parent_class = NULL;
51 gtk_bin_get_type (void)
53 static guint bin_type = 0;
57 static const GtkTypeInfo bin_info =
62 (GtkClassInitFunc) gtk_bin_class_init,
63 (GtkObjectInitFunc) gtk_bin_init,
64 /* reserved_1 */ NULL,
65 /* reserved_2 */ NULL,
66 (GtkClassInitFunc) NULL,
69 bin_type = gtk_type_unique (GTK_TYPE_CONTAINER, &bin_info);
76 gtk_bin_class_init (GtkBinClass *class)
78 GtkObjectClass *object_class;
79 GtkWidgetClass *widget_class;
80 GtkContainerClass *container_class;
82 object_class = (GtkObjectClass*) class;
83 widget_class = (GtkWidgetClass*) class;
84 container_class = (GtkContainerClass*) class;
86 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
88 widget_class->map = gtk_bin_map;
89 widget_class->unmap = gtk_bin_unmap;
90 widget_class->expose_event = gtk_bin_expose;
92 container_class->add = gtk_bin_add;
93 container_class->remove = gtk_bin_remove;
94 container_class->forall = gtk_bin_forall;
95 container_class->child_type = gtk_bin_child_type;
99 gtk_bin_init (GtkBin *bin)
101 GTK_WIDGET_SET_FLAGS (bin, GTK_NO_WINDOW);
108 gtk_bin_child_type (GtkContainer *container)
110 if (!GTK_BIN (container)->child)
111 return GTK_TYPE_WIDGET;
113 return GTK_TYPE_NONE;
117 gtk_bin_map (GtkWidget *widget)
121 g_return_if_fail (widget != NULL);
122 g_return_if_fail (GTK_IS_BIN (widget));
124 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
125 bin = GTK_BIN (widget);
128 GTK_WIDGET_VISIBLE (bin->child) &&
129 !GTK_WIDGET_MAPPED (bin->child))
130 gtk_widget_map (bin->child);
132 if (!GTK_WIDGET_NO_WINDOW (widget))
133 gdk_window_show (widget->window);
137 gtk_bin_unmap (GtkWidget *widget)
141 g_return_if_fail (widget != NULL);
142 g_return_if_fail (GTK_IS_BIN (widget));
144 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
145 bin = GTK_BIN (widget);
147 if (!GTK_WIDGET_NO_WINDOW (widget))
148 gdk_window_hide (widget->window);
150 if (bin->child && GTK_WIDGET_MAPPED (bin->child))
151 gtk_widget_unmap (bin->child);
155 gtk_bin_expose (GtkWidget *widget,
156 GdkEventExpose *event)
159 GdkEventExpose child_event;
161 g_return_val_if_fail (widget != NULL, FALSE);
162 g_return_val_if_fail (GTK_IS_BIN (widget), FALSE);
163 g_return_val_if_fail (event != NULL, FALSE);
165 if (GTK_WIDGET_DRAWABLE (widget))
167 bin = GTK_BIN (widget);
169 child_event = *event;
170 if (bin->child && GTK_WIDGET_DRAWABLE (bin->child) &&
171 GTK_WIDGET_NO_WINDOW (bin->child) &&
172 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
173 gtk_widget_event (bin->child, (GdkEvent*) &child_event);
181 gtk_bin_add (GtkContainer *container,
186 g_return_if_fail (container != NULL);
187 g_return_if_fail (GTK_IS_BIN (container));
188 g_return_if_fail (child != NULL);
189 g_return_if_fail (GTK_IS_WIDGET (child));
191 bin = GTK_BIN (container);
193 if (bin->child != NULL)
195 g_warning ("Attempting to add a widget with type %s to a %s, "
196 "but as a GtkBin subclass a %s can only contain one widget at a time; "
197 "it already contains a widget of type %s",
198 g_type_name (G_TYPE_FROM_INSTANCE (child)),
199 g_type_name (G_TYPE_FROM_INSTANCE (bin)),
200 g_type_name (G_TYPE_FROM_INSTANCE (bin)),
201 g_type_name (G_TYPE_FROM_INSTANCE (bin->child)));
205 gtk_widget_set_parent (child, GTK_WIDGET (bin));
208 if (GTK_WIDGET_REALIZED (child->parent))
209 gtk_widget_realize (child);
211 if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
213 if (GTK_WIDGET_MAPPED (child->parent))
214 gtk_widget_map (child);
216 gtk_widget_queue_resize (child);
221 gtk_bin_remove (GtkContainer *container,
225 gboolean widget_was_visible;
227 g_return_if_fail (container != NULL);
228 g_return_if_fail (GTK_IS_BIN (container));
229 g_return_if_fail (child != NULL);
230 g_return_if_fail (GTK_IS_WIDGET (child));
232 bin = GTK_BIN (container);
233 g_return_if_fail (bin->child == child);
235 widget_was_visible = GTK_WIDGET_VISIBLE (child);
237 gtk_widget_unparent (child);
240 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
241 * since that's what is needed by toplevels, which derive from GtkBin.
243 if (widget_was_visible)
244 gtk_widget_queue_resize (GTK_WIDGET (container));
248 gtk_bin_forall (GtkContainer *container,
249 gboolean include_internals,
250 GtkCallback callback,
251 gpointer callback_data)
255 g_return_if_fail (container != NULL);
256 g_return_if_fail (GTK_IS_BIN (container));
257 g_return_if_fail (callback != NULL);
259 bin = GTK_BIN (container);
262 (* callback) (bin->child, callback_data);