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