]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
GtkType and macro fixups.
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include "gtkbin.h"
20
21
22 static void gtk_bin_class_init (GtkBinClass    *klass);
23 static void gtk_bin_init       (GtkBin         *bin);
24 static void gtk_bin_map        (GtkWidget      *widget);
25 static void gtk_bin_unmap      (GtkWidget      *widget);
26 static void gtk_bin_draw       (GtkWidget      *widget,
27                                 GdkRectangle   *area);
28 static gint gtk_bin_expose     (GtkWidget      *widget,
29                                 GdkEventExpose *event);
30 static void gtk_bin_add        (GtkContainer   *container,
31                                 GtkWidget      *widget);
32 static void gtk_bin_remove     (GtkContainer   *container,
33                                 GtkWidget      *widget);
34 static void gtk_bin_foreach    (GtkContainer   *container,
35                                 GtkCallback     callback,
36                                 gpointer        callback_data);
37 static GtkType gtk_bin_child_type (GtkContainer*container);
38
39
40 static GtkContainerClass *parent_class = NULL;
41
42
43 GtkType
44 gtk_bin_get_type (void)
45 {
46   static guint bin_type = 0;
47
48   if (!bin_type)
49     {
50       GtkTypeInfo bin_info =
51       {
52         "GtkBin",
53         sizeof (GtkBin),
54         sizeof (GtkBinClass),
55         (GtkClassInitFunc) gtk_bin_class_init,
56         (GtkObjectInitFunc) gtk_bin_init,
57         /* reversed_1 */ NULL,
58         /* reversed_2 */ NULL,
59         (GtkClassInitFunc) NULL,
60       };
61
62       bin_type = gtk_type_unique (GTK_TYPE_CONTAINER, &bin_info);
63     }
64
65   return bin_type;
66 }
67
68 static void
69 gtk_bin_class_init (GtkBinClass *class)
70 {
71   GtkObjectClass *object_class;
72   GtkWidgetClass *widget_class;
73   GtkContainerClass *container_class;
74
75   object_class = (GtkObjectClass*) class;
76   widget_class = (GtkWidgetClass*) class;
77   container_class = (GtkContainerClass*) class;
78
79   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
80
81   widget_class->map = gtk_bin_map;
82   widget_class->unmap = gtk_bin_unmap;
83   widget_class->draw = gtk_bin_draw;
84   widget_class->expose_event = gtk_bin_expose;
85
86   container_class->add = gtk_bin_add;
87   container_class->remove = gtk_bin_remove;
88   container_class->foreach = gtk_bin_foreach;
89   container_class->child_type = gtk_bin_child_type;
90 }
91
92 static void
93 gtk_bin_init (GtkBin *bin)
94 {
95   GTK_WIDGET_SET_FLAGS (bin, GTK_NO_WINDOW);
96
97   bin->child = NULL;
98 }
99
100
101 static GtkType
102 gtk_bin_child_type (GtkContainer *container)
103 {
104   if (!GTK_BIN (container)->child)
105     return GTK_TYPE_WIDGET;
106   else
107     return GTK_TYPE_NONE;
108 }
109
110 static void
111 gtk_bin_map (GtkWidget *widget)
112 {
113   GtkBin *bin;
114
115   g_return_if_fail (widget != NULL);
116   g_return_if_fail (GTK_IS_BIN (widget));
117
118   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
119   bin = GTK_BIN (widget);
120
121   if (!GTK_WIDGET_NO_WINDOW (widget))
122     gdk_window_show (widget->window);
123   else
124     gtk_widget_queue_draw (widget);
125
126   if (bin->child &&
127       GTK_WIDGET_VISIBLE (bin->child) &&
128       !GTK_WIDGET_MAPPED (bin->child))
129     gtk_widget_map (bin->child);
130 }
131
132 static void
133 gtk_bin_unmap (GtkWidget *widget)
134 {
135   GtkBin *bin;
136
137   g_return_if_fail (widget != NULL);
138   g_return_if_fail (GTK_IS_BIN (widget));
139
140   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
141   bin = GTK_BIN (widget);
142
143   if (GTK_WIDGET_NO_WINDOW (widget))
144     gdk_window_clear_area (widget->window,
145                            widget->allocation.x,
146                            widget->allocation.y,
147                            widget->allocation.width,
148                            widget->allocation.height);
149   else
150     gdk_window_hide (widget->window);
151
152   if (bin->child &&
153       GTK_WIDGET_VISIBLE (bin->child) &&
154       GTK_WIDGET_MAPPED (bin->child))
155     gtk_widget_unmap (bin->child);
156 }
157
158 static void
159 gtk_bin_draw (GtkWidget    *widget,
160               GdkRectangle *area)
161 {
162   GtkBin *bin;
163   GdkRectangle child_area;
164
165   g_return_if_fail (widget != NULL);
166   g_return_if_fail (GTK_IS_BIN (widget));
167
168   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
169     {
170       bin = GTK_BIN (widget);
171
172       if (bin->child &&
173           gtk_widget_intersect (bin->child, area, &child_area))
174         gtk_widget_draw (bin->child, &child_area);
175     }
176 }
177
178 static gint
179 gtk_bin_expose (GtkWidget      *widget,
180                 GdkEventExpose *event)
181 {
182   GtkBin *bin;
183   GdkEventExpose child_event;
184
185   g_return_val_if_fail (widget != NULL, FALSE);
186   g_return_val_if_fail (GTK_IS_BIN (widget), FALSE);
187   g_return_val_if_fail (event != NULL, FALSE);
188
189   if (GTK_WIDGET_DRAWABLE (widget))
190     {
191       bin = GTK_BIN (widget);
192
193       child_event = *event;
194       if (bin->child &&
195           GTK_WIDGET_NO_WINDOW (bin->child) &&
196           gtk_widget_intersect (bin->child, &event->area, &child_event.area))
197         gtk_widget_event (bin->child, (GdkEvent*) &child_event);
198     }
199
200   return FALSE;
201 }
202
203
204 static void
205 gtk_bin_add (GtkContainer *container,
206              GtkWidget    *widget)
207 {
208   GtkBin *bin;
209
210   g_return_if_fail (container != NULL);
211   g_return_if_fail (GTK_IS_BIN (container));
212   g_return_if_fail (widget != NULL);
213
214   bin = GTK_BIN (container);
215   g_return_if_fail (bin->child == NULL);
216
217   gtk_widget_set_parent (widget, GTK_WIDGET (container));
218   
219   if (GTK_WIDGET_VISIBLE (widget->parent))
220     {
221       if (GTK_WIDGET_REALIZED (widget->parent) &&
222           !GTK_WIDGET_REALIZED (widget))
223         gtk_widget_realize (widget);
224       
225       if (GTK_WIDGET_MAPPED (widget->parent) &&
226           !GTK_WIDGET_MAPPED (widget))
227         gtk_widget_map (widget);
228     }
229   
230   bin->child = widget;
231   
232   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (container))
233     gtk_widget_queue_resize (widget);
234 }
235
236 static void
237 gtk_bin_remove (GtkContainer *container,
238                 GtkWidget    *widget)
239 {
240   GtkBin *bin;
241
242   g_return_if_fail (container != NULL);
243   g_return_if_fail (GTK_IS_BIN (container));
244   g_return_if_fail (widget != NULL);
245
246   bin = GTK_BIN (container);
247
248   if (bin->child == widget)
249     {
250       gboolean widget_was_visible = GTK_WIDGET_VISIBLE (widget);
251
252       gtk_widget_unparent (widget);
253       bin->child = NULL;
254
255       /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
256        * since that's what is needed by toplevels, which derive from GtkBin.
257        */
258       if (widget_was_visible)
259         gtk_widget_queue_resize (GTK_WIDGET (container));
260     }
261 }
262
263 static void
264 gtk_bin_foreach (GtkContainer *container,
265                  GtkCallback   callback,
266                  gpointer      callback_data)
267 {
268   GtkBin *bin;
269
270   g_return_if_fail (container != NULL);
271   g_return_if_fail (GTK_IS_BIN (container));
272   g_return_if_fail (callback != NULL);
273
274   bin = GTK_BIN (container);
275
276   if (bin->child)
277     (* callback) (bin->child, callback_data);
278 }