]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
ecaff82abfcb642a954aee95ea638304ab7a3668
[~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 #include "gtkbin.h"
28
29
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,
37                                 GtkWidget      *widget);
38 static void gtk_bin_remove     (GtkContainer   *container,
39                                 GtkWidget      *widget);
40 static void gtk_bin_forall     (GtkContainer   *container,
41                                 gboolean        include_internals,
42                                 GtkCallback     callback,
43                                 gpointer        callback_data);
44 static GtkType gtk_bin_child_type (GtkContainer*container);
45
46
47 static GtkContainerClass *parent_class = NULL;
48
49
50 GtkType
51 gtk_bin_get_type (void)
52 {
53   static guint bin_type = 0;
54
55   if (!bin_type)
56     {
57       static const GtkTypeInfo bin_info =
58       {
59         "GtkBin",
60         sizeof (GtkBin),
61         sizeof (GtkBinClass),
62         (GtkClassInitFunc) gtk_bin_class_init,
63         (GtkObjectInitFunc) gtk_bin_init,
64         /* reserved_1 */ NULL,
65         /* reserved_2 */ NULL,
66         (GtkClassInitFunc) NULL,
67       };
68
69       bin_type = gtk_type_unique (GTK_TYPE_CONTAINER, &bin_info);
70     }
71
72   return bin_type;
73 }
74
75 static void
76 gtk_bin_class_init (GtkBinClass *class)
77 {
78   GtkObjectClass *object_class;
79   GtkWidgetClass *widget_class;
80   GtkContainerClass *container_class;
81
82   object_class = (GtkObjectClass*) class;
83   widget_class = (GtkWidgetClass*) class;
84   container_class = (GtkContainerClass*) class;
85
86   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
87
88   widget_class->map = gtk_bin_map;
89   widget_class->unmap = gtk_bin_unmap;
90   widget_class->expose_event = gtk_bin_expose;
91
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;
96 }
97
98 static void
99 gtk_bin_init (GtkBin *bin)
100 {
101   GTK_WIDGET_SET_FLAGS (bin, GTK_NO_WINDOW);
102
103   bin->child = NULL;
104 }
105
106
107 static GtkType
108 gtk_bin_child_type (GtkContainer *container)
109 {
110   if (!GTK_BIN (container)->child)
111     return GTK_TYPE_WIDGET;
112   else
113     return GTK_TYPE_NONE;
114 }
115
116 static void
117 gtk_bin_map (GtkWidget *widget)
118 {
119   GtkBin *bin;
120
121   g_return_if_fail (widget != NULL);
122   g_return_if_fail (GTK_IS_BIN (widget));
123
124   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
125   bin = GTK_BIN (widget);
126
127   if (bin->child &&
128       GTK_WIDGET_VISIBLE (bin->child) &&
129       !GTK_WIDGET_MAPPED (bin->child))
130     gtk_widget_map (bin->child);
131
132   if (!GTK_WIDGET_NO_WINDOW (widget))
133     gdk_window_show (widget->window);
134 }
135
136 static void
137 gtk_bin_unmap (GtkWidget *widget)
138 {
139   GtkBin *bin;
140
141   g_return_if_fail (widget != NULL);
142   g_return_if_fail (GTK_IS_BIN (widget));
143
144   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
145   bin = GTK_BIN (widget);
146
147   if (!GTK_WIDGET_NO_WINDOW (widget))
148     gdk_window_hide (widget->window);
149
150   if (bin->child && GTK_WIDGET_MAPPED (bin->child))
151     gtk_widget_unmap (bin->child);
152 }
153
154 static gint
155 gtk_bin_expose (GtkWidget      *widget,
156                 GdkEventExpose *event)
157 {
158   GtkBin *bin;
159   GdkEventExpose child_event;
160
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);
164
165   if (GTK_WIDGET_DRAWABLE (widget))
166     {
167       bin = GTK_BIN (widget);
168
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);
174     }
175
176   return FALSE;
177 }
178
179
180 static void
181 gtk_bin_add (GtkContainer *container,
182              GtkWidget    *child)
183 {
184   GtkBin *bin;
185
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));
190
191   bin = GTK_BIN (container);
192
193   if (bin->child != NULL)
194     {
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)));
202       return;
203     }
204
205   gtk_widget_set_parent (child, GTK_WIDGET (bin));
206   bin->child = child;
207
208   if (GTK_WIDGET_REALIZED (child->parent))
209     gtk_widget_realize (child);
210
211   if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
212     {
213       if (GTK_WIDGET_MAPPED (child->parent))
214         gtk_widget_map (child);
215
216       gtk_widget_queue_resize (child);
217     }
218 }
219
220 static void
221 gtk_bin_remove (GtkContainer *container,
222                 GtkWidget    *child)
223 {
224   GtkBin *bin;
225   gboolean widget_was_visible;
226
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));
231
232   bin = GTK_BIN (container);
233   g_return_if_fail (bin->child == child);
234
235   widget_was_visible = GTK_WIDGET_VISIBLE (child);
236   
237   gtk_widget_unparent (child);
238   bin->child = NULL;
239   
240   /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
241    * since that's what is needed by toplevels, which derive from GtkBin.
242    */
243   if (widget_was_visible)
244     gtk_widget_queue_resize (GTK_WIDGET (container));
245 }
246
247 static void
248 gtk_bin_forall (GtkContainer *container,
249                 gboolean      include_internals,
250                 GtkCallback   callback,
251                 gpointer      callback_data)
252 {
253   GtkBin *bin;
254
255   g_return_if_fail (container != NULL);
256   g_return_if_fail (GTK_IS_BIN (container));
257   g_return_if_fail (callback != NULL);
258
259   bin = GTK_BIN (container);
260
261   if (bin->child)
262     (* callback) (bin->child, callback_data);
263 }