]> Pileus Git - ~andy/gtk/blob - gtk/gtkbbox.c
fix warning
[~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
31 static void gtk_button_box_class_init    (GtkButtonBoxClass   *klass);
32 static void gtk_button_box_init          (GtkButtonBox        *box);
33
34 #define DEFAULT_CHILD_MIN_WIDTH 85
35 #define DEFAULT_CHILD_MIN_HEIGHT 27
36 #define DEFAULT_CHILD_IPAD_X 7
37 #define DEFAULT_CHILD_IPAD_Y 0
38
39 GtkType
40 gtk_button_box_get_type (void)
41 {
42   static GtkType button_box_type = 0;
43
44   if (!button_box_type)
45     {
46       static const GtkTypeInfo button_box_info =
47       {
48         "GtkButtonBox",
49         sizeof (GtkButtonBox),
50         sizeof (GtkButtonBoxClass),
51         (GtkClassInitFunc) gtk_button_box_class_init,
52         (GtkObjectInitFunc) gtk_button_box_init,
53         /* reserved_1 */ NULL,
54         /* reserved_2 */ NULL,
55         (GtkClassInitFunc) NULL,
56       };
57
58       button_box_type = gtk_type_unique (gtk_box_get_type (), &button_box_info);
59     }
60
61   return button_box_type;
62 }
63
64 static void
65 gtk_button_box_class_init (GtkButtonBoxClass *class)
66 {
67   GtkWidgetClass *widget_class;
68
69   widget_class = (GtkWidgetClass*) class;
70
71   /* FIXME we need to override the "spacing" property on GtkBox once
72    * libgobject allows that.
73    */
74
75   gtk_widget_class_install_style_property (widget_class,
76                                            g_param_spec_int ("child_min_width",
77                                                              _("Minimum child width"),
78                                                              _("Minimum width of buttons inside the box"),
79                                                              0,
80                                                              G_MAXINT,
81                                                              DEFAULT_CHILD_MIN_WIDTH,
82                                                              G_PARAM_READABLE));
83
84   gtk_widget_class_install_style_property (widget_class,
85                                            g_param_spec_int ("child_min_height",
86                                                              _("Minimum child height"),
87                                                              _("Minimum height of buttons inside the box"),
88                                                              0,
89                                                              G_MAXINT,
90                                                              DEFAULT_CHILD_MIN_HEIGHT,
91                                                              G_PARAM_READABLE));
92
93   gtk_widget_class_install_style_property (widget_class,
94                                            g_param_spec_int ("child_internal_pad_x",
95                                                              _("Child internal width padding"),
96                                                              _("Amount to increase child's size on either side"),
97                                                              0,
98                                                              G_MAXINT,
99                                                              DEFAULT_CHILD_IPAD_X,
100                                                              G_PARAM_READABLE));
101
102   gtk_widget_class_install_style_property (widget_class,
103                                            g_param_spec_int ("child_internal_pad_y",
104                                                              _("Child internal height padding"),
105                                                              _("Amount to increase child's size on the top and bottom"),
106                                                              0,
107                                                              G_MAXINT,
108                                                              DEFAULT_CHILD_IPAD_Y,
109                                                              G_PARAM_READABLE));
110 }
111
112 static void
113 gtk_button_box_init (GtkButtonBox *button_box)
114 {
115   GTK_BOX (button_box)->spacing = 0;
116   button_box->child_min_width = GTK_BUTTONBOX_DEFAULT;
117   button_box->child_min_height = GTK_BUTTONBOX_DEFAULT;
118   button_box->child_ipad_x = GTK_BUTTONBOX_DEFAULT;
119   button_box->child_ipad_y = GTK_BUTTONBOX_DEFAULT;
120   button_box->layout_style = GTK_BUTTONBOX_DEFAULT_STYLE;
121 }
122
123 /* set per widget values for spacing, child size and child internal padding */
124
125 void gtk_button_box_set_child_size (GtkButtonBox *widget, gint width, gint height)
126 {
127   widget->child_min_width = width;
128   widget->child_min_height = height;
129 }
130
131 void gtk_button_box_set_child_ipadding (GtkButtonBox *widget,
132                                         gint ipad_x, gint ipad_y)
133 {
134   widget->child_ipad_x = ipad_x;
135   widget->child_ipad_y = ipad_y;
136 }
137
138 void gtk_button_box_set_layout (GtkButtonBox *widget, 
139                                 GtkButtonBoxStyle layout_style)
140 {
141   g_return_if_fail (layout_style >= GTK_BUTTONBOX_DEFAULT_STYLE &&
142                     layout_style <= GTK_BUTTONBOX_END);
143
144   widget->layout_style = layout_style;
145 }
146
147
148 /* get per widget values for spacing, child size and child internal padding */
149
150 void gtk_button_box_get_child_size (GtkButtonBox *widget,
151                                      gint *width, gint *height)
152 {
153   *width  = widget->child_min_width;
154   *height = widget->child_min_height;
155 }
156
157 void gtk_button_box_get_child_ipadding (GtkButtonBox *widget,
158                                          gint* ipad_x, gint *ipad_y)
159 {
160   *ipad_x = widget->child_ipad_x;
161   *ipad_y = widget->child_ipad_y;
162 }
163
164 GtkButtonBoxStyle gtk_button_box_get_layout (GtkButtonBox *widget)
165 {
166   return widget->layout_style;
167 }
168
169
170
171 /* Ask children how much space they require and round up 
172    to match minimum size and internal padding.
173    Returns the size each single child should have. */
174 void
175 _gtk_button_box_child_requisition (GtkWidget *widget,
176                                    int *nvis_children,
177                                    int *width,
178                                    int *height)
179 {
180   GtkButtonBox *bbox;
181   GtkBoxChild *child;
182   GList *children;
183   gint nchildren;
184   gint needed_width;
185   gint needed_height;
186   GtkRequisition child_requisition;
187   gint ipad_w;
188   gint ipad_h;
189   gint width_default;
190   gint height_default;
191   gint ipad_x_default;
192   gint ipad_y_default;
193   
194   gint child_min_width;
195   gint child_min_height;
196   gint ipad_x;
197   gint ipad_y;
198   
199   g_return_if_fail (widget != NULL);
200   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
201
202   bbox = GTK_BUTTON_BOX (widget);
203
204   gtk_widget_style_get (widget,
205                         "child_min_width",
206                         &width_default,
207                         "child_min_height",
208                         &height_default,
209                         "child_internal_pad_x",
210                         &ipad_x_default,
211                         "child_internal_pad_y",
212                         &ipad_y_default, NULL);
213   
214   child_min_width = bbox->child_min_width   != GTK_BUTTONBOX_DEFAULT
215           ? bbox->child_min_width : width_default;
216   child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
217           ? bbox->child_min_height : height_default;
218   ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
219           ? bbox->child_ipad_x : ipad_x_default;
220   ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
221           ? bbox->child_ipad_y : ipad_y_default;
222
223   nchildren = 0;
224   children = GTK_BOX(bbox)->children;
225   needed_width = child_min_width;
226   needed_height = child_min_height;  
227   ipad_w = ipad_x * 2;
228   ipad_h = ipad_y * 2;
229   
230   while (children)
231     {
232       child = children->data;
233       children = children->next;
234
235       if (GTK_WIDGET_VISIBLE (child->widget))
236         {
237           nchildren += 1;
238           gtk_widget_size_request (child->widget, &child_requisition);
239           if (child_requisition.width + ipad_w > needed_width)
240                   needed_width = child_requisition.width + ipad_w;
241           if (child_requisition.height + ipad_h > needed_height)
242                   needed_height = child_requisition.height + ipad_h;
243         }
244     }
245   
246   *nvis_children = nchildren;
247   *width = needed_width;
248   *height = needed_height;
249 }