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