]> Pileus Git - ~andy/gtk/blob - gtk/gtkbbox.c
I submitted this patch twice to gtk-devel-list, and received no comments,
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include "gtkbbox.h"
20
21
22 static void gtk_button_box_class_init    (GtkButtonBoxClass   *klass);
23 static void gtk_button_box_init          (GtkButtonBox        *box);
24
25
26 static gint default_child_min_width = 85;
27 static gint default_child_min_height = 27;
28 static gint default_child_ipad_x = 7;
29 static gint default_child_ipad_y = 0;
30
31
32 GtkType
33 gtk_button_box_get_type (void)
34 {
35   static GtkType button_box_type = 0;
36
37   if (!button_box_type)
38     {
39       static const GtkTypeInfo button_box_info =
40       {
41         "GtkButtonBox",
42         sizeof (GtkButtonBox),
43         sizeof (GtkButtonBoxClass),
44         (GtkClassInitFunc) gtk_button_box_class_init,
45         (GtkObjectInitFunc) gtk_button_box_init,
46         /* reserved_1 */ NULL,
47         /* reserved_2 */ NULL,
48         (GtkClassInitFunc) NULL,
49       };
50
51       button_box_type = gtk_type_unique (gtk_box_get_type (), &button_box_info);
52     }
53
54   return button_box_type;
55 }
56
57 static void
58 gtk_button_box_class_init (GtkButtonBoxClass *class)
59 {
60   GtkWidgetClass *widget_class;
61
62   widget_class = (GtkWidgetClass*) class;
63 }
64
65 static void
66 gtk_button_box_init (GtkButtonBox *button_box)
67 {
68   button_box->spacing = GTK_BUTTONBOX_DEFAULT;
69   button_box->child_min_width = GTK_BUTTONBOX_DEFAULT;
70   button_box->child_min_height = GTK_BUTTONBOX_DEFAULT;
71   button_box->child_ipad_x = GTK_BUTTONBOX_DEFAULT;
72   button_box->child_ipad_y = GTK_BUTTONBOX_DEFAULT;
73   button_box->layout_style = GTK_BUTTONBOX_DEFAULT_STYLE;
74 }
75
76
77 /* set default values for child size and child internal padding */
78 /* default spacing is in defined in subclasses */
79
80 void gtk_button_box_set_child_size_default (gint width, gint height)
81 {
82   default_child_min_width = width;
83   default_child_min_height = height;
84 }
85
86 void gtk_button_box_set_child_ipadding_default (gint ipad_x, gint ipad_y)
87 {
88   default_child_ipad_x = ipad_x;
89   default_child_ipad_y = ipad_y;
90 }
91
92 /* get default values for child size and child internal padding */
93
94 void gtk_button_box_get_child_size_default (gint *width, gint *height)
95 {
96   *width = default_child_min_width;
97   *height = default_child_min_height;
98 }
99
100 void gtk_button_box_get_child_ipadding_default (gint *ipad_x, gint *ipad_y)
101 {
102   *ipad_x = default_child_ipad_x;
103   *ipad_y = default_child_ipad_y;
104 }
105
106 /* set per widget values for spacing, child size and child internal padding */
107
108 void gtk_button_box_set_spacing (GtkButtonBox *widget, gint spacing)
109 {
110   widget->spacing = spacing;
111 }
112
113 void gtk_button_box_set_child_size (GtkButtonBox *widget, gint width, gint height)
114 {
115   widget->child_min_width = width;
116   widget->child_min_height = height;
117 }
118
119 void gtk_button_box_set_child_ipadding (GtkButtonBox *widget,
120                                         gint ipad_x, gint ipad_y)
121 {
122   widget->child_ipad_x = ipad_x;
123   widget->child_ipad_y = ipad_y;
124 }
125
126 void gtk_button_box_set_layout (GtkButtonBox *widget, 
127                                 GtkButtonBoxStyle layout_style)
128 {
129   g_return_if_fail (layout_style >= GTK_BUTTONBOX_DEFAULT_STYLE &&
130                     layout_style <= GTK_BUTTONBOX_END);
131
132   widget->layout_style = layout_style;
133 }
134
135
136 /* get per widget values for spacing, child size and child internal padding */
137
138 gint gtk_button_box_get_spacing (GtkButtonBox *widget)
139 {
140   return widget->spacing;
141 }
142
143 void gtk_button_box_get_child_size (GtkButtonBox *widget,
144                                      gint *width, gint *height)
145 {
146   *width  = widget->child_min_width;
147   *height = widget->child_min_height;
148 }
149
150 void gtk_button_box_get_child_ipadding (GtkButtonBox *widget,
151                                          gint* ipad_x, gint *ipad_y)
152 {
153   *ipad_x = widget->child_ipad_x;
154   *ipad_y = widget->child_ipad_y;
155 }
156
157 GtkButtonBoxStyle gtk_button_box_get_layout (GtkButtonBox *widget)
158 {
159   return widget->layout_style;
160 }
161
162
163
164 /* Ask children how much space they require and round up 
165    to match minimum size and internal padding.
166    Returns the size each single child should have. */
167 void
168 gtk_button_box_child_requisition (GtkWidget *widget,
169                                   int *nvis_children,
170                                   int *width,
171                                   int *height)
172 {
173   GtkButtonBox *bbox;
174   GtkBoxChild *child;
175   GList *children;
176   gint nchildren;
177   gint needed_width;
178   gint needed_height;
179   GtkRequisition child_requisition;
180   gint ipad_w;
181   gint ipad_h;
182   gint width_default;
183   gint height_default;
184   gint ipad_x_default;
185   gint ipad_y_default;
186   
187   gint child_min_width;
188   gint child_min_height;
189   gint ipad_x;
190   gint ipad_y;
191   
192   g_return_if_fail (widget != NULL);
193   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
194
195   bbox = GTK_BUTTON_BOX (widget);
196
197   gtk_button_box_get_child_size_default (&width_default, &height_default);
198   gtk_button_box_get_child_ipadding_default (&ipad_x_default, &ipad_y_default);
199   
200   child_min_width = bbox->child_min_width   != GTK_BUTTONBOX_DEFAULT
201           ? bbox->child_min_width : width_default;
202   child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
203           ? bbox->child_min_height : height_default;
204   ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
205           ? bbox->child_ipad_x : ipad_x_default;
206   ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
207           ? bbox->child_ipad_y : ipad_y_default;
208
209   nchildren = 0;
210   children = GTK_BOX(bbox)->children;
211   needed_width = child_min_width;
212   needed_height = child_min_height;  
213   ipad_w = ipad_x * 2;
214   ipad_h = ipad_y * 2;
215   
216   while (children)
217     {
218       child = children->data;
219       children = children->next;
220
221       if (GTK_WIDGET_VISIBLE (child->widget))
222         {
223           nchildren += 1;
224           gtk_widget_size_request (child->widget, &child_requisition);
225           if (child_requisition.width + ipad_w > needed_width)
226                   needed_width = child_requisition.width + ipad_w;
227           if (child_requisition.height + ipad_h > needed_height)
228                   needed_height = child_requisition.height + ipad_h;
229         }
230     }
231   
232   *nvis_children = nchildren;
233   *width = needed_width;
234   *height = needed_height;
235 }