]> Pileus Git - ~andy/gtk/blob - gtk/gtkbbox.c
New static function to set the background of all windows.
[~andy/gtk] / gtk / gtkbbox.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 "gtkbbox.h"
28 #include "gtkintl.h"
29
30 enum {
31   PROP_0,
32   PROP_LAYOUT_STYLE,
33   PROP_LAST
34 };
35
36 enum {
37   CHILD_PROP_0,
38   CHILD_PROP_SECONDARY
39 };
40
41 static void gtk_button_box_class_init         (GtkButtonBoxClass *klass);
42 static void gtk_button_box_init               (GtkButtonBox      *box);
43 static void gtk_button_box_set_property       (GObject           *object,
44                                                guint              prop_id,
45                                                const GValue      *value,
46                                                GParamSpec        *pspec);
47 static void gtk_button_box_get_property       (GObject           *object,
48                                                guint              prop_id,
49                                                GValue            *value,
50                                                GParamSpec        *pspec);
51 static void gtk_button_box_set_child_property (GtkContainer      *container,
52                                                GtkWidget         *child,
53                                                guint              property_id,
54                                                const GValue      *value,
55                                                GParamSpec        *pspec);
56 static void gtk_button_box_get_child_property (GtkContainer      *container,
57                                                GtkWidget         *child,
58                                                guint              property_id,
59                                                GValue            *value,
60                                                GParamSpec        *pspec);
61
62 #define DEFAULT_CHILD_MIN_WIDTH 85
63 #define DEFAULT_CHILD_MIN_HEIGHT 27
64 #define DEFAULT_CHILD_IPAD_X 4
65 #define DEFAULT_CHILD_IPAD_Y 0
66
67 GType
68 gtk_button_box_get_type (void)
69 {
70   static GType button_box_type = 0;
71
72   if (!button_box_type)
73     {
74       static const GTypeInfo button_box_info =
75       {
76         sizeof (GtkButtonBoxClass),
77         NULL,           /* base_init */
78         NULL,           /* base_finalize */
79         (GClassInitFunc) gtk_button_box_class_init,
80         NULL,           /* class_finalize */
81         NULL,           /* class_data */
82         sizeof (GtkButtonBox),
83         0,               /* n_preallocs */
84         (GInstanceInitFunc) gtk_button_box_init,
85         NULL,           /* value_table */
86       };
87
88       button_box_type =
89         g_type_register_static (GTK_TYPE_BOX, "GtkButtonBox",
90                                 &button_box_info, G_TYPE_FLAG_ABSTRACT);
91     }
92
93   return button_box_type;
94 }
95
96 static void
97 gtk_button_box_class_init (GtkButtonBoxClass *class)
98 {
99   GtkWidgetClass *widget_class;
100   GObjectClass *gobject_class;
101   GtkContainerClass *container_class;
102
103   gobject_class = G_OBJECT_CLASS (class);
104   widget_class = (GtkWidgetClass*) class;
105   container_class = (GtkContainerClass*) class;
106
107   gobject_class->set_property = gtk_button_box_set_property;
108   gobject_class->get_property = gtk_button_box_get_property;
109
110   container_class->set_child_property = gtk_button_box_set_child_property;
111   container_class->get_child_property = gtk_button_box_get_child_property;
112   
113   /* FIXME we need to override the "spacing" property on GtkBox once
114    * libgobject allows that.
115    */
116
117   gtk_widget_class_install_style_property (widget_class,
118                                            g_param_spec_int ("child_min_width",
119                                                              _("Minimum child width"),
120                                                              _("Minimum width of buttons inside the box"),
121                                                              0,
122                                                              G_MAXINT,
123                                                              DEFAULT_CHILD_MIN_WIDTH,
124                                                              G_PARAM_READABLE));
125
126   gtk_widget_class_install_style_property (widget_class,
127                                            g_param_spec_int ("child_min_height",
128                                                              _("Minimum child height"),
129                                                              _("Minimum height of buttons inside the box"),
130                                                              0,
131                                                              G_MAXINT,
132                                                              DEFAULT_CHILD_MIN_HEIGHT,
133                                                              G_PARAM_READABLE));
134
135   gtk_widget_class_install_style_property (widget_class,
136                                            g_param_spec_int ("child_internal_pad_x",
137                                                              _("Child internal width padding"),
138                                                              _("Amount to increase child's size on either side"),
139                                                              0,
140                                                              G_MAXINT,
141                                                              DEFAULT_CHILD_IPAD_X,
142                                                              G_PARAM_READABLE));
143
144   gtk_widget_class_install_style_property (widget_class,
145                                            g_param_spec_int ("child_internal_pad_y",
146                                                              _("Child internal height padding"),
147                                                              _("Amount to increase child's size on the top and bottom"),
148                                                              0,
149                                                              G_MAXINT,
150                                                              DEFAULT_CHILD_IPAD_Y,
151                                                              G_PARAM_READABLE));
152   g_object_class_install_property (gobject_class,
153                                    PROP_LAYOUT_STYLE,
154                                    g_param_spec_enum ("layout_style",
155                                                       _("Layout style"),
156                                                       _("How to layout the buttons in the box. Possible values are default, spread, edge, start and end"),
157                                                       GTK_TYPE_BUTTON_BOX_STYLE,
158                                                       GTK_BUTTONBOX_DEFAULT_STYLE,
159                                                       G_PARAM_READWRITE));
160
161   gtk_container_class_install_child_property (container_class,
162                                               CHILD_PROP_SECONDARY,
163                                               g_param_spec_boolean ("secondary", 
164                                                                     _("Secondary"),
165                                                                     _("If TRUE, the child appears in a secondary group of children, suitable for, e.g., help buttons"),
166                                                                     FALSE,
167                                                                     G_PARAM_READWRITE));
168 }
169
170 static void
171 gtk_button_box_init (GtkButtonBox *button_box)
172 {
173   GTK_BOX (button_box)->spacing = 0;
174   button_box->child_min_width = GTK_BUTTONBOX_DEFAULT;
175   button_box->child_min_height = GTK_BUTTONBOX_DEFAULT;
176   button_box->child_ipad_x = GTK_BUTTONBOX_DEFAULT;
177   button_box->child_ipad_y = GTK_BUTTONBOX_DEFAULT;
178   button_box->layout_style = GTK_BUTTONBOX_DEFAULT_STYLE;
179 }
180
181 static void
182 gtk_button_box_set_property (GObject         *object,
183                              guint            prop_id,
184                              const GValue    *value,
185                              GParamSpec      *pspec)
186 {
187   switch (prop_id) 
188     {
189     case PROP_LAYOUT_STYLE:
190       gtk_button_box_set_layout (GTK_BUTTON_BOX (object),
191                                  g_value_get_enum (value));
192       break;
193     default:
194       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195       break;
196     }
197 }
198
199 static void
200 gtk_button_box_get_property (GObject         *object,
201                              guint            prop_id,
202                              GValue          *value,
203                              GParamSpec      *pspec)
204 {
205   switch (prop_id)
206     {
207     case PROP_LAYOUT_STYLE:
208       g_value_set_enum (value, GTK_BUTTON_BOX (object)->layout_style);
209       break;
210     default:
211       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212       break;
213     }
214 }
215
216 static void
217 gtk_button_box_set_child_property (GtkContainer    *container,
218                                    GtkWidget       *child,
219                                    guint            property_id,
220                                    const GValue    *value,
221                                    GParamSpec      *pspec)
222 {
223   switch (property_id)
224     {
225     case CHILD_PROP_SECONDARY:
226       gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (container), child,
227                                           g_value_get_boolean (value));
228       break;
229     default:
230       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
231       break;
232     }
233 }
234
235 static void
236 gtk_button_box_get_child_property (GtkContainer *container,
237                                    GtkWidget    *child,
238                                    guint         property_id,
239                                    GValue       *value,
240                                    GParamSpec   *pspec)
241 {
242   GList *list;
243   GtkBoxChild *child_info = NULL;
244
245   list = GTK_BOX (container)->children;
246   while (list)
247     {
248       child_info = list->data;
249       if (child_info->widget == child)
250         break;
251
252       list = list->next;
253     }
254
255   g_assert (list != NULL);
256   
257   switch (property_id)
258     {
259     case CHILD_PROP_SECONDARY:
260       g_value_set_boolean (value, child_info->is_secondary);
261       break;
262     default:
263       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
264       break;
265     }
266 }
267
268 /* set per widget values for spacing, child size and child internal padding */
269
270 void 
271 gtk_button_box_set_child_size (GtkButtonBox *widget, 
272                                gint width, gint height)
273 {
274   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
275
276   widget->child_min_width = width;
277   widget->child_min_height = height;
278 }
279
280 void 
281 gtk_button_box_set_child_ipadding (GtkButtonBox *widget,
282                                    gint ipad_x, gint ipad_y)
283 {
284   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
285
286   widget->child_ipad_x = ipad_x;
287   widget->child_ipad_y = ipad_y;
288 }
289
290 void
291 gtk_button_box_set_layout (GtkButtonBox      *widget, 
292                            GtkButtonBoxStyle  layout_style)
293 {
294   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
295   g_return_if_fail (layout_style >= GTK_BUTTONBOX_DEFAULT_STYLE &&
296                     layout_style <= GTK_BUTTONBOX_END);
297
298   if (widget->layout_style != layout_style)
299     {
300       widget->layout_style = layout_style;
301       g_object_notify (G_OBJECT (widget), "layout_style");
302       gtk_widget_queue_resize (GTK_WIDGET (widget));
303     }
304 }
305
306
307 /* get per widget values for spacing, child size and child internal padding */
308
309 void 
310 gtk_button_box_get_child_size (GtkButtonBox *widget,
311                                gint *width, gint *height)
312 {
313   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
314   g_return_if_fail (width != NULL);
315   g_return_if_fail (height != NULL);
316
317   *width  = widget->child_min_width;
318   *height = widget->child_min_height;
319 }
320
321 void
322 gtk_button_box_get_child_ipadding (GtkButtonBox *widget,
323                                    gint* ipad_x, gint *ipad_y)
324 {
325   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
326   g_return_if_fail (ipad_x != NULL);
327   g_return_if_fail (ipad_y != NULL);
328
329   *ipad_x = widget->child_ipad_x;
330   *ipad_y = widget->child_ipad_y;
331 }
332
333 GtkButtonBoxStyle 
334 gtk_button_box_get_layout (GtkButtonBox *widget)
335 {
336   g_return_val_if_fail (GTK_IS_BUTTON_BOX (widget), GTK_BUTTONBOX_SPREAD);
337   
338   return widget->layout_style;
339 }
340
341 /**
342  * gtk_button_box_set_child_secondary
343  * @widget: a #GtkButtonBox
344  * @child: a child of @widget
345  * @is_secondary: if %TRUE, the @child appears in a secondary group of the
346  *                button box.
347  *
348  * Sets whether @child should appear in a secondary group of children.
349  * A typical use of a secondary child is the help button in a dialog.
350  *
351  * This group appears after the other children if the style
352  * is %GTK_BUTTONBOX_START, %GTK_BUTTONBOX_SPREAD or
353  * %GTK_BUTTONBOX_EDGE, and before the the other children if the style
354  * is %GTK_BUTTONBOX_END. For horizontal button boxes, the definition
355  * of before/after depends on direction of the widget (see
356  * gtk_widget_set_direction()). If the style is %GTK_BUTTONBOX_START
357  * or %GTK_BUTTONBOX_END, then the secondary children are aligned at
358  * the other end of the button box from the main children. For the
359  * other styles, they appear immediately next to the main children.
360  **/
361 void 
362 gtk_button_box_set_child_secondary (GtkButtonBox *widget, 
363                                     GtkWidget    *child,
364                                     gboolean      is_secondary)
365 {
366   GList *list;
367   
368   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
369   g_return_if_fail (GTK_IS_WIDGET (child));
370   g_return_if_fail (child->parent == GTK_WIDGET (widget));
371
372   list = GTK_BOX (widget)->children;
373   while (list)
374     {
375       GtkBoxChild *child_info = list->data;
376       if (child_info->widget == child)
377         {
378           child_info->is_secondary = is_secondary;
379           break;
380         }
381
382       list = list->next;
383     }
384
385   gtk_widget_child_notify (child, "secondary");
386
387   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (child))
388     gtk_widget_queue_resize (child);
389 }
390
391 /* Ask children how much space they require and round up 
392    to match minimum size and internal padding.
393    Returns the size each single child should have. */
394 void
395 _gtk_button_box_child_requisition (GtkWidget *widget,
396                                    int       *nvis_children,
397                                    int       *nvis_secondaries,
398                                    int       *width,
399                                    int       *height)
400 {
401   GtkButtonBox *bbox;
402   GtkBoxChild *child;
403   GList *children;
404   gint nchildren;
405   gint nsecondaries;
406   gint needed_width;
407   gint needed_height;
408   GtkRequisition child_requisition;
409   gint ipad_w;
410   gint ipad_h;
411   gint width_default;
412   gint height_default;
413   gint ipad_x_default;
414   gint ipad_y_default;
415   
416   gint child_min_width;
417   gint child_min_height;
418   gint ipad_x;
419   gint ipad_y;
420   
421   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
422
423   bbox = GTK_BUTTON_BOX (widget);
424
425   gtk_widget_style_get (widget,
426                         "child_min_width",
427                         &width_default,
428                         "child_min_height",
429                         &height_default,
430                         "child_internal_pad_x",
431                         &ipad_x_default,
432                         "child_internal_pad_y",
433                         &ipad_y_default, NULL);
434   
435   child_min_width = bbox->child_min_width   != GTK_BUTTONBOX_DEFAULT
436           ? bbox->child_min_width : width_default;
437   child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
438           ? bbox->child_min_height : height_default;
439   ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
440           ? bbox->child_ipad_x : ipad_x_default;
441   ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
442           ? bbox->child_ipad_y : ipad_y_default;
443
444   nchildren = 0;
445   nsecondaries = 0;
446   children = GTK_BOX(bbox)->children;
447   needed_width = child_min_width;
448   needed_height = child_min_height;  
449   ipad_w = ipad_x * 2;
450   ipad_h = ipad_y * 2;
451   
452   while (children)
453     {
454       child = children->data;
455       children = children->next;
456
457       if (GTK_WIDGET_VISIBLE (child->widget))
458         {
459           nchildren += 1;
460           gtk_widget_size_request (child->widget, &child_requisition);
461           
462           if (child_requisition.width + ipad_w > needed_width)
463             needed_width = child_requisition.width + ipad_w;
464           if (child_requisition.height + ipad_h > needed_height)
465             needed_height = child_requisition.height + ipad_h;
466           if (child->is_secondary)
467             nsecondaries++;
468         }
469     }
470
471   if (nvis_children)
472     *nvis_children = nchildren;
473   if (nvis_secondaries)
474     *nvis_secondaries = nsecondaries;
475   if (width)
476     *width = needed_width;
477   if (height)
478     *height = needed_height;
479 }