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