]> Pileus Git - ~andy/gtk/blob - gtk/gtkfixed.c
Remove "draw" virtual method and signal
[~andy/gtk] / gtk / gtkfixed.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 "gtkfixed.h"
28
29
30 static void gtk_fixed_class_init    (GtkFixedClass    *klass);
31 static void gtk_fixed_init          (GtkFixed         *fixed);
32 static void gtk_fixed_map           (GtkWidget        *widget);
33 static void gtk_fixed_realize       (GtkWidget        *widget);
34 static void gtk_fixed_size_request  (GtkWidget        *widget,
35                                      GtkRequisition   *requisition);
36 static void gtk_fixed_size_allocate (GtkWidget        *widget,
37                                      GtkAllocation    *allocation);
38 static void gtk_fixed_paint         (GtkWidget        *widget,
39                                      GdkRectangle     *area);
40 static gint gtk_fixed_expose        (GtkWidget        *widget,
41                                      GdkEventExpose   *event);
42 static void gtk_fixed_add           (GtkContainer     *container,
43                                      GtkWidget        *widget);
44 static void gtk_fixed_remove        (GtkContainer     *container,
45                                      GtkWidget        *widget);
46 static void gtk_fixed_forall        (GtkContainer     *container,
47                                      gboolean          include_internals,
48                                      GtkCallback       callback,
49                                      gpointer          callback_data);
50 static GtkType gtk_fixed_child_type (GtkContainer     *container);
51
52
53 static GtkContainerClass *parent_class = NULL;
54
55
56 GtkType
57 gtk_fixed_get_type (void)
58 {
59   static GtkType fixed_type = 0;
60
61   if (!fixed_type)
62     {
63       static const GtkTypeInfo fixed_info =
64       {
65         "GtkFixed",
66         sizeof (GtkFixed),
67         sizeof (GtkFixedClass),
68         (GtkClassInitFunc) gtk_fixed_class_init,
69         (GtkObjectInitFunc) gtk_fixed_init,
70         /* reserved_1 */ NULL,
71         /* reserved_2 */ NULL,
72         (GtkClassInitFunc) NULL,
73       };
74
75       fixed_type = gtk_type_unique (GTK_TYPE_CONTAINER, &fixed_info);
76     }
77
78   return fixed_type;
79 }
80
81 static void
82 gtk_fixed_class_init (GtkFixedClass *class)
83 {
84   GtkObjectClass *object_class;
85   GtkWidgetClass *widget_class;
86   GtkContainerClass *container_class;
87
88   object_class = (GtkObjectClass*) class;
89   widget_class = (GtkWidgetClass*) class;
90   container_class = (GtkContainerClass*) class;
91
92   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
93
94   widget_class->map = gtk_fixed_map;
95   widget_class->realize = gtk_fixed_realize;
96   widget_class->size_request = gtk_fixed_size_request;
97   widget_class->size_allocate = gtk_fixed_size_allocate;
98   widget_class->expose_event = gtk_fixed_expose;
99
100   container_class->add = gtk_fixed_add;
101   container_class->remove = gtk_fixed_remove;
102   container_class->forall = gtk_fixed_forall;
103   container_class->child_type = gtk_fixed_child_type;
104 }
105
106 static GtkType
107 gtk_fixed_child_type (GtkContainer     *container)
108 {
109   return GTK_TYPE_WIDGET;
110 }
111
112 static void
113 gtk_fixed_init (GtkFixed *fixed)
114 {
115   GTK_WIDGET_UNSET_FLAGS (fixed, GTK_NO_WINDOW);
116  
117   fixed->children = NULL;
118 }
119
120 GtkWidget*
121 gtk_fixed_new (void)
122 {
123   GtkFixed *fixed;
124
125   fixed = gtk_type_new (GTK_TYPE_FIXED);
126   return GTK_WIDGET (fixed);
127 }
128
129 void
130 gtk_fixed_put (GtkFixed       *fixed,
131                GtkWidget      *widget,
132                gint16         x,
133                gint16         y)
134 {
135   GtkFixedChild *child_info;
136
137   g_return_if_fail (fixed != NULL);
138   g_return_if_fail (GTK_IS_FIXED (fixed));
139   g_return_if_fail (widget != NULL);
140
141   child_info = g_new (GtkFixedChild, 1);
142   child_info->widget = widget;
143   child_info->x = x;
144   child_info->y = y;
145
146   gtk_widget_set_parent (widget, GTK_WIDGET (fixed));
147
148   fixed->children = g_list_append (fixed->children, child_info); 
149
150   if (GTK_WIDGET_REALIZED (fixed))
151     gtk_widget_realize (widget);
152
153   if (GTK_WIDGET_VISIBLE (fixed) && GTK_WIDGET_VISIBLE (widget))
154     {
155       if (GTK_WIDGET_MAPPED (fixed))
156         gtk_widget_map (widget);
157       
158       gtk_widget_queue_resize (GTK_WIDGET (fixed));
159     }
160 }
161
162 void
163 gtk_fixed_move (GtkFixed       *fixed,
164                 GtkWidget      *widget,
165                 gint16         x,
166                 gint16         y)
167 {
168   GtkFixedChild *child;
169   GList *children;
170
171   g_return_if_fail (fixed != NULL);
172   g_return_if_fail (GTK_IS_FIXED (fixed));
173   g_return_if_fail (widget != NULL);
174
175   children = fixed->children;
176   while (children)
177     {
178       child = children->data;
179       children = children->next;
180
181       if (child->widget == widget)
182         {
183           child->x = x;
184           child->y = y;
185
186           if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (fixed))
187             gtk_widget_queue_resize (GTK_WIDGET (fixed));
188
189           break;
190         }
191     }
192 }
193
194 static void
195 gtk_fixed_map (GtkWidget *widget)
196 {
197   GtkFixed *fixed;
198   GtkFixedChild *child;
199   GList *children;
200
201   g_return_if_fail (widget != NULL);
202   g_return_if_fail (GTK_IS_FIXED (widget));
203
204   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
205   fixed = GTK_FIXED (widget);
206
207   children = fixed->children;
208   while (children)
209     {
210       child = children->data;
211       children = children->next;
212
213       if (GTK_WIDGET_VISIBLE (child->widget) &&
214           !GTK_WIDGET_MAPPED (child->widget))
215         gtk_widget_map (child->widget);
216     }
217
218   gdk_window_show (widget->window);
219 }
220
221 static void
222 gtk_fixed_realize (GtkWidget *widget)
223 {
224   GdkWindowAttr attributes;
225   gint attributes_mask;
226
227   g_return_if_fail (widget != NULL);
228   g_return_if_fail (GTK_IS_FIXED (widget));
229
230   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
231
232   attributes.window_type = GDK_WINDOW_CHILD;
233   attributes.x = widget->allocation.x;
234   attributes.y = widget->allocation.y;
235   attributes.width = widget->allocation.width;
236   attributes.height = widget->allocation.height;
237   attributes.wclass = GDK_INPUT_OUTPUT;
238   attributes.visual = gtk_widget_get_visual (widget);
239   attributes.colormap = gtk_widget_get_colormap (widget);
240   attributes.event_mask = gtk_widget_get_events (widget);
241   attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
242
243   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
244
245   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, 
246                                    attributes_mask);
247   gdk_window_set_user_data (widget->window, widget);
248
249   widget->style = gtk_style_attach (widget->style, widget->window);
250   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
251 }
252
253 static void
254 gtk_fixed_size_request (GtkWidget      *widget,
255                         GtkRequisition *requisition)
256 {
257   GtkFixed *fixed;  
258   GtkFixedChild *child;
259   GList *children;
260   GtkRequisition child_requisition;
261
262   g_return_if_fail (widget != NULL);
263   g_return_if_fail (GTK_IS_FIXED (widget));
264   g_return_if_fail (requisition != NULL);
265
266   fixed = GTK_FIXED (widget);
267   requisition->width = 0;
268   requisition->height = 0;
269
270   children = fixed->children;
271   while (children)
272     {
273       child = children->data;
274       children = children->next;
275
276       if (GTK_WIDGET_VISIBLE (child->widget))
277         {
278           gtk_widget_size_request (child->widget, &child_requisition);
279
280           requisition->height = MAX (requisition->height,
281                                      child->y +
282                                      child_requisition.height);
283           requisition->width = MAX (requisition->width,
284                                     child->x +
285                                     child_requisition.width);
286         }
287     }
288
289   requisition->height += GTK_CONTAINER (fixed)->border_width * 2;
290   requisition->width += GTK_CONTAINER (fixed)->border_width * 2;
291 }
292
293 static void
294 gtk_fixed_size_allocate (GtkWidget     *widget,
295                          GtkAllocation *allocation)
296 {
297   GtkFixed *fixed;
298   GtkFixedChild *child;
299   GtkAllocation child_allocation;
300   GtkRequisition child_requisition;
301   GList *children;
302   guint16 border_width;
303
304   g_return_if_fail (widget != NULL);
305   g_return_if_fail (GTK_IS_FIXED(widget));
306   g_return_if_fail (allocation != NULL);
307
308   fixed = GTK_FIXED (widget);
309
310   widget->allocation = *allocation;
311   if (GTK_WIDGET_REALIZED (widget))
312     gdk_window_move_resize (widget->window,
313                             allocation->x, 
314                             allocation->y,
315                             allocation->width, 
316                             allocation->height);
317
318   border_width = GTK_CONTAINER (fixed)->border_width;
319   
320   children = fixed->children;
321   while (children)
322     {
323       child = children->data;
324       children = children->next;
325       
326       if (GTK_WIDGET_VISIBLE (child->widget))
327         {
328           gtk_widget_get_child_requisition (child->widget, &child_requisition);
329           child_allocation.x = child->x + border_width;
330           child_allocation.y = child->y + border_width;
331           child_allocation.width = child_requisition.width;
332           child_allocation.height = child_requisition.height;
333           gtk_widget_size_allocate (child->widget, &child_allocation);
334         }
335     }
336 }
337
338 static void
339 gtk_fixed_paint (GtkWidget    *widget,
340                  GdkRectangle *area)
341 {
342   g_return_if_fail (widget != NULL);
343   g_return_if_fail (GTK_IS_FIXED (widget));
344   g_return_if_fail (area != NULL);
345
346   if (GTK_WIDGET_DRAWABLE (widget))
347     gdk_window_clear_area (widget->window,
348                            area->x, area->y,
349                            area->width, area->height);
350 }
351
352 static gint
353 gtk_fixed_expose (GtkWidget      *widget,
354                   GdkEventExpose *event)
355 {
356   GtkFixed *fixed;
357   GtkFixedChild *child;
358   GdkEventExpose child_event;
359   GList *children;
360
361   g_return_val_if_fail (widget != NULL, FALSE);
362   g_return_val_if_fail (GTK_IS_FIXED (widget), FALSE);
363   g_return_val_if_fail (event != NULL, FALSE);
364
365   if (GTK_WIDGET_DRAWABLE (widget))
366     {
367       fixed = GTK_FIXED (widget);
368
369       child_event = *event;
370
371       children = fixed->children;
372       while (children)
373         {
374           child = children->data;
375           children = children->next;
376
377           if (GTK_WIDGET_NO_WINDOW (child->widget) &&
378               gtk_widget_intersect (child->widget, &event->area, 
379                                     &child_event.area))
380             gtk_widget_event (child->widget, (GdkEvent*) &child_event);
381         }
382     }
383
384   return FALSE;
385 }
386
387 static void
388 gtk_fixed_add (GtkContainer *container,
389                GtkWidget    *widget)
390 {
391   g_return_if_fail (container != NULL);
392   g_return_if_fail (GTK_IS_FIXED (container));
393   g_return_if_fail (widget != NULL);
394
395   gtk_fixed_put (GTK_FIXED (container), widget, 0, 0);
396 }
397
398 static void
399 gtk_fixed_remove (GtkContainer *container,
400                   GtkWidget    *widget)
401 {
402   GtkFixed *fixed;
403   GtkFixedChild *child;
404   GList *children;
405
406   g_return_if_fail (container != NULL);
407   g_return_if_fail (GTK_IS_FIXED (container));
408   g_return_if_fail (widget != NULL);
409
410   fixed = GTK_FIXED (container);
411
412   children = fixed->children;
413   while (children)
414     {
415       child = children->data;
416
417       if (child->widget == widget)
418         {
419           gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
420           
421           gtk_widget_unparent (widget);
422
423           fixed->children = g_list_remove_link (fixed->children, children);
424           g_list_free (children);
425           g_free (child);
426
427           if (was_visible && GTK_WIDGET_VISIBLE (container))
428             gtk_widget_queue_resize (GTK_WIDGET (container));
429
430           break;
431         }
432
433       children = children->next;
434     }
435 }
436
437 static void
438 gtk_fixed_forall (GtkContainer *container,
439                   gboolean      include_internals,
440                   GtkCallback   callback,
441                   gpointer      callback_data)
442 {
443   GtkFixed *fixed;
444   GtkFixedChild *child;
445   GList *children;
446
447   g_return_if_fail (container != NULL);
448   g_return_if_fail (GTK_IS_FIXED (container));
449   g_return_if_fail (callback != NULL);
450
451   fixed = GTK_FIXED (container);
452
453   children = fixed->children;
454   while (children)
455     {
456       child = children->data;
457       children = children->next;
458
459       (* callback) (child->widget, callback_data);
460     }
461 }