]> Pileus Git - ~andy/gtk/blob - gtk/gtkbox.c
Updated Bulgarian translation by Alexander Shopov <ash@contact.bg>
[~andy/gtk] / gtk / gtkbox.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 "gtkbox.h"
29 #include "gtkprivate.h"
30 #include "gtkintl.h"
31 #include "gtkalias.h"
32
33 enum {
34   PROP_0,
35   PROP_SPACING,
36   PROP_HOMOGENEOUS
37 };
38
39 enum {
40   CHILD_PROP_0,
41   CHILD_PROP_EXPAND,
42   CHILD_PROP_FILL,
43   CHILD_PROP_PADDING,
44   CHILD_PROP_PACK_TYPE,
45   CHILD_PROP_POSITION
46 };
47
48 static void gtk_box_set_property (GObject         *object,
49                                   guint            prop_id,
50                                   const GValue    *value,
51                                   GParamSpec      *pspec);
52 static void gtk_box_get_property (GObject         *object,
53                                   guint            prop_id,
54                                   GValue          *value,
55                                   GParamSpec      *pspec);
56 static void gtk_box_add        (GtkContainer   *container,
57                                 GtkWidget      *widget);
58 static void gtk_box_remove     (GtkContainer   *container,
59                                 GtkWidget      *widget);
60 static void gtk_box_forall     (GtkContainer   *container,
61                                 gboolean        include_internals,
62                                 GtkCallback     callback,
63                                 gpointer        callback_data);
64 static void gtk_box_set_child_property (GtkContainer    *container,
65                                         GtkWidget       *child,
66                                         guint            property_id,
67                                         const GValue    *value,
68                                         GParamSpec      *pspec);
69 static void gtk_box_get_child_property (GtkContainer    *container,
70                                         GtkWidget       *child,
71                                         guint            property_id,
72                                         GValue          *value,
73                                         GParamSpec      *pspec);
74 static GType gtk_box_child_type (GtkContainer   *container);
75      
76
77 G_DEFINE_ABSTRACT_TYPE (GtkBox, gtk_box, GTK_TYPE_CONTAINER)
78
79 static void
80 gtk_box_class_init (GtkBoxClass *class)
81 {
82   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
83   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
84
85   gobject_class->set_property = gtk_box_set_property;
86   gobject_class->get_property = gtk_box_get_property;
87    
88   container_class->add = gtk_box_add;
89   container_class->remove = gtk_box_remove;
90   container_class->forall = gtk_box_forall;
91   container_class->child_type = gtk_box_child_type;
92   container_class->set_child_property = gtk_box_set_child_property;
93   container_class->get_child_property = gtk_box_get_child_property;
94
95   g_object_class_install_property (gobject_class,
96                                    PROP_SPACING,
97                                    g_param_spec_int ("spacing",
98                                                      P_("Spacing"),
99                                                      P_("The amount of space between children"),
100                                                      0,
101                                                      G_MAXINT,
102                                                      0,
103                                                      GTK_PARAM_READWRITE));
104   
105   g_object_class_install_property (gobject_class,
106                                    PROP_HOMOGENEOUS,
107                                    g_param_spec_boolean ("homogeneous",
108                                                          P_("Homogeneous"),
109                                                          P_("Whether the children should all be the same size"),
110                                                          FALSE,
111                                                          GTK_PARAM_READWRITE));
112
113   gtk_container_class_install_child_property (container_class,
114                                               CHILD_PROP_EXPAND,
115                                               g_param_spec_boolean ("expand", 
116                                                                     P_("Expand"), 
117                                                                     P_("Whether the child should receive extra space when the parent grows"),
118                                                                     TRUE,
119                                                                     GTK_PARAM_READWRITE));
120   gtk_container_class_install_child_property (container_class,
121                                               CHILD_PROP_FILL,
122                                               g_param_spec_boolean ("fill", 
123                                                                     P_("Fill"), 
124                                                                     P_("Whether extra space given to the child should be allocated to the child or used as padding"),
125                                                                     TRUE,
126                                                                     GTK_PARAM_READWRITE));
127   gtk_container_class_install_child_property (container_class,
128                                               CHILD_PROP_PADDING,
129                                               g_param_spec_uint ("padding", 
130                                                                  P_("Padding"), 
131                                                                  P_("Extra space to put between the child and its neighbors, in pixels"),
132                                                                  0, G_MAXINT, 0,
133                                                                  GTK_PARAM_READWRITE));
134   gtk_container_class_install_child_property (container_class,
135                                               CHILD_PROP_PACK_TYPE,
136                                               g_param_spec_enum ("pack-type", 
137                                                                  P_("Pack type"), 
138                                                                  P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
139                                                                  GTK_TYPE_PACK_TYPE, GTK_PACK_START,
140                                                                  GTK_PARAM_READWRITE));
141   gtk_container_class_install_child_property (container_class,
142                                               CHILD_PROP_POSITION,
143                                               g_param_spec_int ("position", 
144                                                                 P_("Position"), 
145                                                                 P_("The index of the child in the parent"),
146                                                                 -1, G_MAXINT, 0,
147                                                                 GTK_PARAM_READWRITE));
148 }
149
150 static void
151 gtk_box_init (GtkBox *box)
152 {
153   GTK_WIDGET_SET_FLAGS (box, GTK_NO_WINDOW);
154   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
155   
156   box->children = NULL;
157   box->spacing = 0;
158   box->homogeneous = FALSE;
159 }
160
161 static void 
162 gtk_box_set_property (GObject         *object,
163                       guint            prop_id,
164                       const GValue    *value,
165                       GParamSpec      *pspec)
166 {
167   GtkBox *box;
168
169   box = GTK_BOX (object);
170
171   switch (prop_id)
172     {
173     case PROP_SPACING:
174       gtk_box_set_spacing (box, g_value_get_int (value));
175       break;
176     case PROP_HOMOGENEOUS:
177       gtk_box_set_homogeneous (box, g_value_get_boolean (value));
178       break;
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181       break;
182     }
183 }
184
185 static void gtk_box_get_property (GObject         *object,
186                                   guint            prop_id,
187                                   GValue          *value,
188                                   GParamSpec      *pspec)
189 {
190   GtkBox *box;
191
192   box = GTK_BOX (object);
193
194   switch (prop_id)
195     {
196     case PROP_SPACING:
197       g_value_set_int (value, box->spacing);
198       break;
199     case PROP_HOMOGENEOUS:
200       g_value_set_boolean (value, box->homogeneous);
201       break;
202     default:
203       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204       break;
205     }
206 }
207
208 static GType
209 gtk_box_child_type (GtkContainer   *container)
210 {
211   return GTK_TYPE_WIDGET;
212 }
213
214 static void
215 gtk_box_set_child_property (GtkContainer    *container,
216                             GtkWidget       *child,
217                             guint            property_id,
218                             const GValue    *value,
219                             GParamSpec      *pspec)
220 {
221   gboolean expand = 0;
222   gboolean fill = 0;
223   guint padding = 0;
224   GtkPackType pack_type = 0;
225
226   if (property_id != CHILD_PROP_POSITION)
227     gtk_box_query_child_packing (GTK_BOX (container),
228                                  child,
229                                  &expand,
230                                  &fill,
231                                  &padding,
232                                  &pack_type);
233   switch (property_id)
234     {
235     case CHILD_PROP_EXPAND:
236       gtk_box_set_child_packing (GTK_BOX (container),
237                                  child,
238                                  g_value_get_boolean (value),
239                                  fill,
240                                  padding,
241                                  pack_type);
242       break;
243     case CHILD_PROP_FILL:
244       gtk_box_set_child_packing (GTK_BOX (container),
245                                  child,
246                                  expand,
247                                  g_value_get_boolean (value),
248                                  padding,
249                                  pack_type);
250       break;
251     case CHILD_PROP_PADDING:
252       gtk_box_set_child_packing (GTK_BOX (container),
253                                  child,
254                                  expand,
255                                  fill,
256                                  g_value_get_uint (value),
257                                  pack_type);
258       break;
259     case CHILD_PROP_PACK_TYPE:
260       gtk_box_set_child_packing (GTK_BOX (container),
261                                  child,
262                                  expand,
263                                  fill,
264                                  padding,
265                                  g_value_get_enum (value));
266       break;
267     case CHILD_PROP_POSITION:
268       gtk_box_reorder_child (GTK_BOX (container),
269                              child,
270                              g_value_get_int (value));
271       break;
272     default:
273       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
274       break;
275     }
276 }
277
278 static void
279 gtk_box_get_child_property (GtkContainer *container,
280                             GtkWidget    *child,
281                             guint         property_id,
282                             GValue       *value,
283                             GParamSpec   *pspec)
284 {
285   gboolean expand = 0;
286   gboolean fill = 0;
287   guint padding = 0;
288   GtkPackType pack_type = 0;
289   GList *list;
290
291   if (property_id != CHILD_PROP_POSITION)
292     gtk_box_query_child_packing (GTK_BOX (container),
293                                  child,
294                                  &expand,
295                                  &fill,
296                                  &padding,
297                                  &pack_type);
298   switch (property_id)
299     {
300       guint i;
301     case CHILD_PROP_EXPAND:
302       g_value_set_boolean (value, expand);
303       break;
304     case CHILD_PROP_FILL:
305       g_value_set_boolean (value, fill);
306       break;
307     case CHILD_PROP_PADDING:
308       g_value_set_uint (value, padding);
309       break;
310     case CHILD_PROP_PACK_TYPE:
311       g_value_set_enum (value, pack_type);
312       break;
313     case CHILD_PROP_POSITION:
314       i = 0;
315       for (list = GTK_BOX (container)->children; list; list = list->next)
316         {
317           GtkBoxChild *child_entry;
318
319           child_entry = list->data;
320           if (child_entry->widget == child)
321             break;
322           i++;
323         }
324       g_value_set_int (value, list ? i : -1);
325       break;
326     default:
327       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
328       break;
329     }
330 }
331
332 void
333 gtk_box_pack_start (GtkBox    *box,
334                     GtkWidget *child,
335                     gboolean   expand,
336                     gboolean   fill,
337                     guint      padding)
338 {
339   GtkBoxChild *child_info;
340
341   g_return_if_fail (GTK_IS_BOX (box));
342   g_return_if_fail (GTK_IS_WIDGET (child));
343   g_return_if_fail (child->parent == NULL);
344
345   child_info = g_new (GtkBoxChild, 1);
346   child_info->widget = child;
347   child_info->padding = padding;
348   child_info->expand = expand ? TRUE : FALSE;
349   child_info->fill = fill ? TRUE : FALSE;
350   child_info->pack = GTK_PACK_START;
351   child_info->is_secondary = FALSE;
352
353   box->children = g_list_append (box->children, child_info);
354
355   gtk_widget_freeze_child_notify (child);
356
357   gtk_widget_set_parent (child, GTK_WIDGET (box));
358   
359   gtk_widget_child_notify (child, "expand");
360   gtk_widget_child_notify (child, "fill");
361   gtk_widget_child_notify (child, "padding");
362   gtk_widget_child_notify (child, "pack-type");
363   gtk_widget_child_notify (child, "position");
364   gtk_widget_thaw_child_notify (child);
365 }
366
367 void
368 gtk_box_pack_end (GtkBox    *box,
369                   GtkWidget *child,
370                   gboolean   expand,
371                   gboolean   fill,
372                   guint      padding)
373 {
374   GtkBoxChild *child_info;
375
376   g_return_if_fail (GTK_IS_BOX (box));
377   g_return_if_fail (GTK_IS_WIDGET (child));
378   g_return_if_fail (child->parent == NULL);
379
380   child_info = g_new (GtkBoxChild, 1);
381   child_info->widget = child;
382   child_info->padding = padding;
383   child_info->expand = expand ? TRUE : FALSE;
384   child_info->fill = fill ? TRUE : FALSE;
385   child_info->pack = GTK_PACK_END;
386   child_info->is_secondary = FALSE;
387
388   box->children = g_list_append (box->children, child_info);
389
390   gtk_widget_freeze_child_notify (child);
391
392   gtk_widget_set_parent (child, GTK_WIDGET (box));
393
394   gtk_widget_child_notify (child, "expand");
395   gtk_widget_child_notify (child, "fill");
396   gtk_widget_child_notify (child, "padding");
397   gtk_widget_child_notify (child, "pack-type");
398   gtk_widget_child_notify (child, "position");
399   gtk_widget_thaw_child_notify (child);
400 }
401
402 void
403 gtk_box_pack_start_defaults (GtkBox    *box,
404                              GtkWidget *child)
405 {
406   gtk_box_pack_start (box, child, TRUE, TRUE, 0);
407 }
408
409 void
410 gtk_box_pack_end_defaults (GtkBox    *box,
411                            GtkWidget *child)
412 {
413   gtk_box_pack_end (box, child, TRUE, TRUE, 0);
414 }
415
416 void
417 gtk_box_set_homogeneous (GtkBox  *box,
418                          gboolean homogeneous)
419 {
420   g_return_if_fail (GTK_IS_BOX (box));
421
422   if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
423     {
424       box->homogeneous = homogeneous ? TRUE : FALSE;
425       g_object_notify (G_OBJECT (box), "homogeneous");
426       gtk_widget_queue_resize (GTK_WIDGET (box));
427     }
428 }
429
430 /**
431  * gtk_box_get_homogeneous:
432  * @box: a #GtkBox
433  *
434  * Returns whether the box is homogeneous (all children are the
435  * same size). See gtk_box_set_homogeneous ().
436  *
437  * Return value: %TRUE if the box is homogeneous.
438  **/
439 gboolean
440 gtk_box_get_homogeneous (GtkBox *box)
441 {
442   g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
443
444   return box->homogeneous;
445 }
446
447 void
448 gtk_box_set_spacing (GtkBox *box,
449                      gint    spacing)
450 {
451   g_return_if_fail (GTK_IS_BOX (box));
452
453   if (spacing != box->spacing)
454     {
455       box->spacing = spacing;
456       g_object_notify (G_OBJECT (box), "spacing");
457       gtk_widget_queue_resize (GTK_WIDGET (box));
458     }
459 }
460
461 /**
462  * gtk_box_get_spacing:
463  * @box: a #GtkBox
464  * 
465  * Gets the value set by gtk_box_set_spacing().
466  * 
467  * Return value: spacing between children
468  **/
469 gint
470 gtk_box_get_spacing (GtkBox *box)
471 {
472   g_return_val_if_fail (GTK_IS_BOX (box), 0);
473
474   return box->spacing;
475 }
476
477 void
478 gtk_box_reorder_child (GtkBox    *box,
479                        GtkWidget *child,
480                        gint       position)
481 {
482   GList *old_link;
483   GList *new_link;
484   GtkBoxChild *child_info = NULL;
485   gint old_position;
486
487   g_return_if_fail (GTK_IS_BOX (box));
488   g_return_if_fail (GTK_IS_WIDGET (child));
489
490   old_link = box->children;
491   old_position = 0;
492   while (old_link)
493     {
494       child_info = old_link->data;
495       if (child_info->widget == child)
496         break;
497
498       old_link = old_link->next;
499       old_position++;
500     }
501
502   g_return_if_fail (old_link != NULL);
503
504   if (position == old_position)
505     return;
506
507   box->children = g_list_delete_link (box->children, old_link);
508
509   if (position < 0)
510     new_link = NULL;
511   else
512     new_link = g_list_nth (box->children, position);
513
514   box->children = g_list_insert_before (box->children, new_link, child_info);
515
516   gtk_widget_child_notify (child, "position");
517   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
518     gtk_widget_queue_resize (child);
519 }
520
521 void
522 gtk_box_query_child_packing (GtkBox             *box,
523                              GtkWidget          *child,
524                              gboolean           *expand,
525                              gboolean           *fill,
526                              guint              *padding,
527                              GtkPackType        *pack_type)
528 {
529   GList *list;
530   GtkBoxChild *child_info = NULL;
531
532   g_return_if_fail (GTK_IS_BOX (box));
533   g_return_if_fail (GTK_IS_WIDGET (child));
534
535   list = box->children;
536   while (list)
537     {
538       child_info = list->data;
539       if (child_info->widget == child)
540         break;
541
542       list = list->next;
543     }
544
545   if (list)
546     {
547       if (expand)
548         *expand = child_info->expand;
549       if (fill)
550         *fill = child_info->fill;
551       if (padding)
552         *padding = child_info->padding;
553       if (pack_type)
554         *pack_type = child_info->pack;
555     }
556 }
557
558 void
559 gtk_box_set_child_packing (GtkBox               *box,
560                            GtkWidget            *child,
561                            gboolean              expand,
562                            gboolean              fill,
563                            guint                 padding,
564                            GtkPackType           pack_type)
565 {
566   GList *list;
567   GtkBoxChild *child_info = NULL;
568
569   g_return_if_fail (GTK_IS_BOX (box));
570   g_return_if_fail (GTK_IS_WIDGET (child));
571
572   list = box->children;
573   while (list)
574     {
575       child_info = list->data;
576       if (child_info->widget == child)
577         break;
578
579       list = list->next;
580     }
581
582   gtk_widget_freeze_child_notify (child);
583   if (list)
584     {
585       child_info->expand = expand != FALSE;
586       gtk_widget_child_notify (child, "expand");
587       child_info->fill = fill != FALSE;
588       gtk_widget_child_notify (child, "fill");
589       child_info->padding = padding;
590       gtk_widget_child_notify (child, "padding");
591       if (pack_type == GTK_PACK_END)
592         child_info->pack = GTK_PACK_END;
593       else
594         child_info->pack = GTK_PACK_START;
595       gtk_widget_child_notify (child, "pack-type");
596
597       if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
598         gtk_widget_queue_resize (child);
599     }
600   gtk_widget_thaw_child_notify (child);
601 }
602
603 static void
604 gtk_box_add (GtkContainer *container,
605              GtkWidget    *widget)
606 {
607   gtk_box_pack_start_defaults (GTK_BOX (container), widget);
608 }
609
610 static void
611 gtk_box_remove (GtkContainer *container,
612                 GtkWidget    *widget)
613 {
614   GtkBox *box;
615   GtkBoxChild *child;
616   GList *children;
617
618   box = GTK_BOX (container);
619
620   children = box->children;
621   while (children)
622     {
623       child = children->data;
624
625       if (child->widget == widget)
626         {
627           gboolean was_visible;
628
629           was_visible = GTK_WIDGET_VISIBLE (widget);
630           gtk_widget_unparent (widget);
631
632           box->children = g_list_remove_link (box->children, children);
633           g_list_free (children);
634           g_free (child);
635
636           /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
637            * since that's what is needed by toplevels.
638            */
639           if (was_visible)
640             gtk_widget_queue_resize (GTK_WIDGET (container));
641
642           break;
643         }
644
645       children = children->next;
646     }
647 }
648
649 static void
650 gtk_box_forall (GtkContainer *container,
651                 gboolean      include_internals,
652                 GtkCallback   callback,
653                 gpointer      callback_data)
654 {
655   GtkBox *box;
656   GtkBoxChild *child;
657   GList *children;
658
659   g_return_if_fail (callback != NULL);
660
661   box = GTK_BOX (container);
662
663   children = box->children;
664   while (children)
665     {
666       child = children->data;
667       children = children->next;
668
669       if (child->pack == GTK_PACK_START)
670         (* callback) (child->widget, callback_data);
671     }
672
673   children = g_list_last (box->children);
674   while (children)
675     {
676       child = children->data;
677       children = children->prev;
678
679       if (child->pack == GTK_PACK_END)
680         (* callback) (child->widget, callback_data);
681     }
682 }
683
684 #define __GTK_BOX_C__
685 #include "gtkaliasdef.c"