]> Pileus Git - ~andy/gtk/blob - gtk/gtkfixed.c
Use GtkFooPrivate instead GtkFooPriv
[~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 _GtkFixedPrivate
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 (GtkFixedPrivate));
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   GtkFixedPrivate *priv;
126
127   fixed->priv = G_TYPE_INSTANCE_GET_PRIVATE (fixed,
128                                              GTK_TYPE_FIXED,
129                                              GtkFixedPrivate);
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   GtkFixedPrivate *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   GtkFixedPrivate *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 (gtk_widget_get_parent (widget) == 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   GtkAllocation allocation;
291   GdkWindow *window;
292   GdkWindowAttr attributes;
293   gint attributes_mask;
294
295   if (!gtk_widget_get_has_window (widget))
296     GTK_WIDGET_CLASS (gtk_fixed_parent_class)->realize (widget);
297   else
298     {
299       gtk_widget_set_realized (widget, TRUE);
300
301       gtk_widget_get_allocation (widget, &allocation);
302
303       attributes.window_type = GDK_WINDOW_CHILD;
304       attributes.x = allocation.x;
305       attributes.y = allocation.y;
306       attributes.width = allocation.width;
307       attributes.height = allocation.height;
308       attributes.wclass = GDK_INPUT_OUTPUT;
309       attributes.visual = gtk_widget_get_visual (widget);
310       attributes.colormap = gtk_widget_get_colormap (widget);
311       attributes.event_mask = gtk_widget_get_events (widget);
312       attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
313       
314       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
315
316       window = gdk_window_new (gtk_widget_get_parent_window (widget),
317                                &attributes, attributes_mask);
318       gtk_widget_set_window (widget, window);
319       gdk_window_set_user_data (window, widget);
320
321       gtk_widget_style_attach (widget);
322       gtk_style_set_background (gtk_widget_get_style (widget), window, GTK_STATE_NORMAL);
323     }
324 }
325
326 static void
327 gtk_fixed_size_request (GtkWidget      *widget,
328                         GtkRequisition *requisition)
329 {
330   GtkFixedPrivate *priv;
331   GtkFixed *fixed;
332   GtkFixedChild *child;
333   GList *children;
334   GtkRequisition child_requisition;
335   guint border_width;
336
337   fixed = GTK_FIXED (widget);
338   priv = fixed->priv;
339
340   requisition->width = 0;
341   requisition->height = 0;
342
343   children = priv->children;
344   while (children)
345     {
346       child = children->data;
347       children = children->next;
348
349       if (gtk_widget_get_visible (child->widget))
350         {
351           gtk_widget_size_request (child->widget, &child_requisition);
352
353           requisition->height = MAX (requisition->height,
354                                      child->y +
355                                      child_requisition.height);
356           requisition->width = MAX (requisition->width,
357                                     child->x +
358                                     child_requisition.width);
359         }
360     }
361
362   border_width = gtk_container_get_border_width (GTK_CONTAINER (fixed));
363   requisition->height += border_width * 2;
364   requisition->width += border_width * 2;
365 }
366
367 static void
368 gtk_fixed_size_allocate (GtkWidget     *widget,
369                          GtkAllocation *allocation)
370 {
371   GtkFixed *fixed = GTK_FIXED (widget);
372   GtkFixedPrivate *priv = fixed->priv;
373   GtkFixedChild *child;
374   GtkAllocation child_allocation;
375   GtkRequisition child_requisition;
376   GList *children;
377   guint border_width;
378
379   gtk_widget_set_allocation (widget, allocation);
380
381   if (gtk_widget_get_has_window (widget))
382     {
383       if (gtk_widget_get_realized (widget))
384         gdk_window_move_resize (gtk_widget_get_window (widget),
385                                 allocation->x, 
386                                 allocation->y,
387                                 allocation->width, 
388                                 allocation->height);
389     }
390
391   border_width = gtk_container_get_border_width (GTK_CONTAINER (fixed));
392
393   children = priv->children;
394   while (children)
395     {
396       child = children->data;
397       children = children->next;
398       
399       if (gtk_widget_get_visible (child->widget))
400         {
401           gtk_widget_get_child_requisition (child->widget, &child_requisition);
402           child_allocation.x = child->x + border_width;
403           child_allocation.y = child->y + border_width;
404
405           if (!gtk_widget_get_has_window (widget))
406             {
407               child_allocation.x += allocation->x;
408               child_allocation.y += allocation->y;
409             }
410           
411           child_allocation.width = child_requisition.width;
412           child_allocation.height = child_requisition.height;
413           gtk_widget_size_allocate (child->widget, &child_allocation);
414         }
415     }
416 }
417
418 static void
419 gtk_fixed_add (GtkContainer *container,
420                GtkWidget    *widget)
421 {
422   gtk_fixed_put (GTK_FIXED (container), widget, 0, 0);
423 }
424
425 static void
426 gtk_fixed_remove (GtkContainer *container,
427                   GtkWidget    *widget)
428 {
429   GtkFixed *fixed = GTK_FIXED (container);
430   GtkFixedPrivate *priv = fixed->priv;
431   GtkFixedChild *child;
432   GtkWidget *widget_container = GTK_WIDGET (container);
433   GList *children;
434
435   children = priv->children;
436   while (children)
437     {
438       child = children->data;
439
440       if (child->widget == widget)
441         {
442           gboolean was_visible = gtk_widget_get_visible (widget);
443           
444           gtk_widget_unparent (widget);
445
446           priv->children = g_list_remove_link (priv->children, children);
447           g_list_free (children);
448           g_free (child);
449
450           if (was_visible && gtk_widget_get_visible (widget_container))
451             gtk_widget_queue_resize (widget_container);
452
453           break;
454         }
455
456       children = children->next;
457     }
458 }
459
460 static void
461 gtk_fixed_forall (GtkContainer *container,
462                   gboolean      include_internals,
463                   GtkCallback   callback,
464                   gpointer      callback_data)
465 {
466   GtkFixed *fixed = GTK_FIXED (container);
467   GtkFixedPrivate *priv = fixed->priv;
468   GtkFixedChild *child;
469   GList *children;
470
471   children = priv->children;
472   while (children)
473     {
474       child = children->data;
475       children = children->next;
476
477       (* callback) (child->widget, callback_data);
478     }
479 }