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