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