]> Pileus Git - ~andy/gtk/blob - gtk/gtkfixed.c
Typo fixes. (#65607)
[~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 #include "gtkintl.h"
29
30 enum {
31   CHILD_PROP_0,
32   CHILD_PROP_X,
33   CHILD_PROP_Y
34 };
35
36 static void gtk_fixed_class_init    (GtkFixedClass    *klass);
37 static void gtk_fixed_init          (GtkFixed         *fixed);
38 static void gtk_fixed_realize       (GtkWidget        *widget);
39 static void gtk_fixed_size_request  (GtkWidget        *widget,
40                                      GtkRequisition   *requisition);
41 static void gtk_fixed_size_allocate (GtkWidget        *widget,
42                                      GtkAllocation    *allocation);
43 static void gtk_fixed_add           (GtkContainer     *container,
44                                      GtkWidget        *widget);
45 static void gtk_fixed_remove        (GtkContainer     *container,
46                                      GtkWidget        *widget);
47 static void gtk_fixed_forall        (GtkContainer     *container,
48                                      gboolean          include_internals,
49                                      GtkCallback       callback,
50                                      gpointer          callback_data);
51 static GtkType gtk_fixed_child_type (GtkContainer     *container);
52
53 static void gtk_fixed_set_child_property (GtkContainer *container,
54                                           GtkWidget    *child,
55                                           guint         property_id,
56                                           const GValue *value,
57                                           GParamSpec   *pspec);
58 static void gtk_fixed_get_child_property (GtkContainer *container,
59                                           GtkWidget    *child,
60                                           guint         property_id,
61                                           GValue       *value,
62                                           GParamSpec   *pspec);
63
64 static GtkContainerClass *parent_class = NULL;
65
66
67 GtkType
68 gtk_fixed_get_type (void)
69 {
70   static GtkType fixed_type = 0;
71
72   if (!fixed_type)
73     {
74       static const GtkTypeInfo fixed_info =
75       {
76         "GtkFixed",
77         sizeof (GtkFixed),
78         sizeof (GtkFixedClass),
79         (GtkClassInitFunc) gtk_fixed_class_init,
80         (GtkObjectInitFunc) gtk_fixed_init,
81         /* reserved_1 */ NULL,
82         /* reserved_2 */ NULL,
83         (GtkClassInitFunc) NULL,
84       };
85
86       fixed_type = gtk_type_unique (GTK_TYPE_CONTAINER, &fixed_info);
87     }
88
89   return fixed_type;
90 }
91
92 static void
93 gtk_fixed_class_init (GtkFixedClass *class)
94 {
95   GtkObjectClass *object_class;
96   GtkWidgetClass *widget_class;
97   GtkContainerClass *container_class;
98
99   object_class = (GtkObjectClass*) class;
100   widget_class = (GtkWidgetClass*) class;
101   container_class = (GtkContainerClass*) class;
102
103   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
104
105   widget_class->realize = gtk_fixed_realize;
106   widget_class->size_request = gtk_fixed_size_request;
107   widget_class->size_allocate = gtk_fixed_size_allocate;
108
109   container_class->add = gtk_fixed_add;
110   container_class->remove = gtk_fixed_remove;
111   container_class->forall = gtk_fixed_forall;
112   container_class->child_type = gtk_fixed_child_type;
113
114   container_class->set_child_property = gtk_fixed_set_child_property;
115   container_class->get_child_property = gtk_fixed_get_child_property;
116
117   gtk_container_class_install_child_property (container_class,
118                                               CHILD_PROP_X,
119                                               g_param_spec_int ("x",
120                                                                 _("X position"),
121                                                                 _("X position of child widget"),
122                                                                 G_MININT,
123                                                                 G_MAXINT,
124                                                                 0,
125                                                                 G_PARAM_READWRITE));
126
127   gtk_container_class_install_child_property (container_class,
128                                               CHILD_PROP_Y,
129                                               g_param_spec_int ("y",
130                                                                 _("Y position"),
131                                                                 _("Y position of child widget"),
132                                                                 G_MININT,
133                                                                 G_MAXINT,
134                                                                 0,
135                                                                 G_PARAM_READWRITE));
136 }
137
138 static GtkType
139 gtk_fixed_child_type (GtkContainer     *container)
140 {
141   return GTK_TYPE_WIDGET;
142 }
143
144 static void
145 gtk_fixed_init (GtkFixed *fixed)
146 {
147   GTK_WIDGET_SET_FLAGS (fixed, GTK_NO_WINDOW);
148  
149   fixed->children = NULL;
150 }
151
152 GtkWidget*
153 gtk_fixed_new (void)
154 {
155   GtkFixed *fixed;
156
157   fixed = gtk_type_new (GTK_TYPE_FIXED);
158   return GTK_WIDGET (fixed);
159 }
160
161 static GtkFixedChild*
162 get_child (GtkFixed  *fixed,
163            GtkWidget *widget)
164 {
165   GList *children;
166   
167   children = fixed->children;
168   while (children)
169     {
170       GtkFixedChild *child;
171       
172       child = children->data;
173       children = children->next;
174
175       if (child->widget == widget)
176         return child;
177     }
178
179   return NULL;
180 }
181
182 void
183 gtk_fixed_put (GtkFixed       *fixed,
184                GtkWidget      *widget,
185                gint            x,
186                gint            y)
187 {
188   GtkFixedChild *child_info;
189
190   g_return_if_fail (GTK_IS_FIXED (fixed));
191   g_return_if_fail (widget != NULL);
192
193   child_info = g_new (GtkFixedChild, 1);
194   child_info->widget = widget;
195   child_info->x = x;
196   child_info->y = y;
197
198   gtk_widget_set_parent (widget, GTK_WIDGET (fixed));
199
200   fixed->children = g_list_append (fixed->children, child_info);
201 }
202
203 static void
204 gtk_fixed_move_internal (GtkFixed       *fixed,
205                          GtkWidget      *widget,
206                          gboolean        change_x,
207                          gint            x,
208                          gboolean        change_y,
209                          gint            y)
210 {
211   GtkFixedChild *child;
212   
213   g_return_if_fail (GTK_IS_FIXED (fixed));
214   g_return_if_fail (GTK_IS_WIDGET (widget));
215   g_return_if_fail (widget->parent == GTK_WIDGET (fixed));  
216   
217   child = get_child (fixed, widget);
218
219   g_assert (child);
220
221   gtk_widget_freeze_child_notify (widget);
222   
223   if (change_x)
224     {
225       child->x = x;
226       gtk_widget_child_notify (widget, "x");
227     }
228
229   if (change_y)
230     {
231       child->y = y;
232       gtk_widget_child_notify (widget, "y");
233     }
234
235   gtk_widget_thaw_child_notify (widget);
236   
237   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (fixed))
238     gtk_widget_queue_resize (GTK_WIDGET (fixed));
239 }
240
241 void
242 gtk_fixed_move (GtkFixed       *fixed,
243                 GtkWidget      *widget,
244                 gint            x,
245                 gint            y)
246 {
247   g_return_if_fail (GTK_IS_FIXED (fixed));
248   g_return_if_fail (GTK_IS_WIDGET (widget));
249   g_return_if_fail (widget->parent == GTK_WIDGET (fixed));
250
251   gtk_fixed_move_internal (fixed, widget, TRUE, x, TRUE, y);
252 }
253
254 static void
255 gtk_fixed_set_child_property (GtkContainer    *container,
256                               GtkWidget       *child,
257                               guint            property_id,
258                               const GValue    *value,
259                               GParamSpec      *pspec)
260 {
261   switch (property_id)
262     {
263     case CHILD_PROP_X:
264       gtk_fixed_move_internal (GTK_FIXED (container),
265                                child,
266                                TRUE, g_value_get_int (value),
267                                FALSE, 0);
268       break;
269     case CHILD_PROP_Y:
270       gtk_fixed_move_internal (GTK_FIXED (container),
271                                child,
272                                FALSE, 0,
273                                TRUE, g_value_get_int (value));
274       break;
275     default:
276       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
277       break;
278     }
279 }
280
281 static void
282 gtk_fixed_get_child_property (GtkContainer *container,
283                               GtkWidget    *child,
284                               guint         property_id,
285                               GValue       *value,
286                               GParamSpec   *pspec)
287 {
288   GtkFixedChild *fixed_child;
289
290   fixed_child = get_child (GTK_FIXED (container), child);
291   
292   switch (property_id)
293     {
294     case CHILD_PROP_X:
295       g_value_set_int (value, fixed_child->x);
296       break;
297     case CHILD_PROP_Y:
298       g_value_set_int (value, fixed_child->y);
299       break;
300     default:
301       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
302       break;
303     }
304 }
305
306 static void
307 gtk_fixed_realize (GtkWidget *widget)
308 {
309   GdkWindowAttr attributes;
310   gint attributes_mask;
311
312   g_return_if_fail (GTK_IS_FIXED (widget));
313
314   if (GTK_WIDGET_NO_WINDOW (widget))
315     GTK_WIDGET_CLASS (parent_class)->realize (widget);
316   else
317     {
318       GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
319
320       attributes.window_type = GDK_WINDOW_CHILD;
321       attributes.x = widget->allocation.x;
322       attributes.y = widget->allocation.y;
323       attributes.width = widget->allocation.width;
324       attributes.height = widget->allocation.height;
325       attributes.wclass = GDK_INPUT_OUTPUT;
326       attributes.visual = gtk_widget_get_visual (widget);
327       attributes.colormap = gtk_widget_get_colormap (widget);
328       attributes.event_mask = gtk_widget_get_events (widget);
329       attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
330       
331       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
332       
333       widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, 
334                                        attributes_mask);
335       gdk_window_set_user_data (widget->window, widget);
336       
337       widget->style = gtk_style_attach (widget->style, widget->window);
338       gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
339     }
340 }
341
342 static void
343 gtk_fixed_size_request (GtkWidget      *widget,
344                         GtkRequisition *requisition)
345 {
346   GtkFixed *fixed;  
347   GtkFixedChild *child;
348   GList *children;
349   GtkRequisition child_requisition;
350
351   g_return_if_fail (GTK_IS_FIXED (widget));
352   g_return_if_fail (requisition != NULL);
353
354   fixed = GTK_FIXED (widget);
355   requisition->width = 0;
356   requisition->height = 0;
357
358   children = fixed->children;
359   while (children)
360     {
361       child = children->data;
362       children = children->next;
363
364       if (GTK_WIDGET_VISIBLE (child->widget))
365         {
366           gtk_widget_size_request (child->widget, &child_requisition);
367
368           requisition->height = MAX (requisition->height,
369                                      child->y +
370                                      child_requisition.height);
371           requisition->width = MAX (requisition->width,
372                                     child->x +
373                                     child_requisition.width);
374         }
375     }
376
377   requisition->height += GTK_CONTAINER (fixed)->border_width * 2;
378   requisition->width += GTK_CONTAINER (fixed)->border_width * 2;
379 }
380
381 static void
382 gtk_fixed_size_allocate (GtkWidget     *widget,
383                          GtkAllocation *allocation)
384 {
385   GtkFixed *fixed;
386   GtkFixedChild *child;
387   GtkAllocation child_allocation;
388   GtkRequisition child_requisition;
389   GList *children;
390   guint16 border_width;
391
392   g_return_if_fail (GTK_IS_FIXED(widget));
393   g_return_if_fail (allocation != NULL);
394
395   fixed = GTK_FIXED (widget);
396
397   widget->allocation = *allocation;
398
399   if (!GTK_WIDGET_NO_WINDOW (widget))
400     {
401       if (GTK_WIDGET_REALIZED (widget))
402         gdk_window_move_resize (widget->window,
403                                 allocation->x, 
404                                 allocation->y,
405                                 allocation->width, 
406                                 allocation->height);
407     }
408       
409   border_width = GTK_CONTAINER (fixed)->border_width;
410   
411   children = fixed->children;
412   while (children)
413     {
414       child = children->data;
415       children = children->next;
416       
417       if (GTK_WIDGET_VISIBLE (child->widget))
418         {
419           gtk_widget_get_child_requisition (child->widget, &child_requisition);
420           child_allocation.x = child->x + border_width;
421           child_allocation.y = child->y + border_width;
422
423           if (GTK_WIDGET_NO_WINDOW (widget))
424             {
425               child_allocation.x += widget->allocation.x;
426               child_allocation.y += widget->allocation.y;
427             }
428           
429           child_allocation.width = child_requisition.width;
430           child_allocation.height = child_requisition.height;
431           gtk_widget_size_allocate (child->widget, &child_allocation);
432         }
433     }
434 }
435
436 static void
437 gtk_fixed_add (GtkContainer *container,
438                GtkWidget    *widget)
439 {
440   g_return_if_fail (GTK_IS_FIXED (container));
441   g_return_if_fail (widget != NULL);
442
443   gtk_fixed_put (GTK_FIXED (container), widget, 0, 0);
444 }
445
446 static void
447 gtk_fixed_remove (GtkContainer *container,
448                   GtkWidget    *widget)
449 {
450   GtkFixed *fixed;
451   GtkFixedChild *child;
452   GList *children;
453
454   g_return_if_fail (GTK_IS_FIXED (container));
455   g_return_if_fail (widget != NULL);
456
457   fixed = GTK_FIXED (container);
458
459   children = fixed->children;
460   while (children)
461     {
462       child = children->data;
463
464       if (child->widget == widget)
465         {
466           gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
467           
468           gtk_widget_unparent (widget);
469
470           fixed->children = g_list_remove_link (fixed->children, children);
471           g_list_free (children);
472           g_free (child);
473
474           if (was_visible && GTK_WIDGET_VISIBLE (container))
475             gtk_widget_queue_resize (GTK_WIDGET (container));
476
477           break;
478         }
479
480       children = children->next;
481     }
482 }
483
484 static void
485 gtk_fixed_forall (GtkContainer *container,
486                   gboolean      include_internals,
487                   GtkCallback   callback,
488                   gpointer      callback_data)
489 {
490   GtkFixed *fixed;
491   GtkFixedChild *child;
492   GList *children;
493
494   g_return_if_fail (GTK_IS_FIXED (container));
495   g_return_if_fail (callback != NULL);
496
497   fixed = GTK_FIXED (container);
498
499   children = fixed->children;
500   while (children)
501     {
502       child = children->data;
503       children = children->next;
504
505       (* callback) (child->widget, callback_data);
506     }
507 }
508
509 /**
510  * gtk_fixed_set_has_window:
511  * @fixed: a #GtkFixed
512  * @has_window: %TRUE if a separate window should be created
513  * 
514  * Sets whether a #GtkFixed widget is created with a separate
515  * #GdkWindow for widget->window or not. (By default, it will be
516  * created with no separate #GdkWindow). This function must be called
517  * while the #GtkFixed is not realized, for instance, immediately after the
518  * window is created.
519  **/
520 void
521 gtk_fixed_set_has_window (GtkFixed *fixed,
522                           gboolean  has_window)
523 {
524   g_return_if_fail (GTK_IS_FIXED (fixed));
525   g_return_if_fail (!GTK_WIDGET_REALIZED (fixed));
526
527   if (!has_window != GTK_WIDGET_NO_WINDOW (fixed))
528     {
529       if (has_window)
530         GTK_WIDGET_UNSET_FLAGS (fixed, GTK_NO_WINDOW);
531       else
532         GTK_WIDGET_SET_FLAGS (fixed, GTK_NO_WINDOW);
533     }
534 }
535
536 /**
537  * gtk_fixed_get_has_window:
538  * @fixed: a #GtkWidget
539  * 
540  * Gets whether the #GtkFixed has it's own #GdkWindow.
541  * See gdk_fixed_set_has_window().
542  * 
543  * Return value: %TRUE if @fixed has its own window.
544  **/
545 gboolean
546 gtk_fixed_get_has_window (GtkFixed *fixed)
547 {
548   g_return_val_if_fail (GTK_IS_FIXED (fixed), FALSE);
549
550   return !GTK_WIDGET_NO_WINDOW (fixed);
551 }