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