]> Pileus Git - ~andy/gtk/blob - gtk/gtkvbbox.c
Fix bug 404506, caused by prematurely releasing a DC. By Hiroyuki Yamamoto
[~andy/gtk] / gtk / gtkvbbox.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 <config.h>
28 #include "gtkvbbox.h"
29 #include "gtkintl.h"
30 #include "gtkalias.h"
31
32
33 static void gtk_vbutton_box_size_request  (GtkWidget      *widget,
34                                            GtkRequisition *requisition);
35 static void gtk_vbutton_box_size_allocate (GtkWidget      *widget,
36                                            GtkAllocation  *allocation);
37
38 static gint default_spacing = 10;
39 static GtkButtonBoxStyle default_layout_style = GTK_BUTTONBOX_EDGE;
40
41 G_DEFINE_TYPE (GtkVButtonBox, gtk_vbutton_box, GTK_TYPE_BUTTON_BOX)
42
43 static void
44 gtk_vbutton_box_class_init (GtkVButtonBoxClass *class)
45 {
46   GtkWidgetClass *widget_class;
47
48   widget_class = (GtkWidgetClass*) class;
49
50   widget_class->size_request = gtk_vbutton_box_size_request;
51   widget_class->size_allocate = gtk_vbutton_box_size_allocate;
52 }
53
54 static void
55 gtk_vbutton_box_init (GtkVButtonBox *vbutton_box)
56 {
57   /* button_box_init has done everything allready */
58 }
59
60 GtkWidget*
61 gtk_vbutton_box_new (void)
62 {
63   GtkVButtonBox *vbutton_box;
64
65   vbutton_box = g_object_new (GTK_TYPE_VBUTTON_BOX, NULL);
66
67   return GTK_WIDGET (vbutton_box);
68 }
69
70
71
72 /* set default value for spacing */
73
74 void
75 gtk_vbutton_box_set_spacing_default (gint spacing)
76 {
77   default_spacing = spacing;
78 }
79
80
81 /* set default value for layout style */
82
83 void
84 gtk_vbutton_box_set_layout_default (GtkButtonBoxStyle layout)
85 {
86   g_return_if_fail (layout >= GTK_BUTTONBOX_DEFAULT_STYLE &&
87                     layout <= GTK_BUTTONBOX_END);
88
89   default_layout_style = layout;
90 }
91
92 /* get default value for spacing */
93
94 gint
95 gtk_vbutton_box_get_spacing_default (void)
96 {
97   return default_spacing;
98 }
99
100
101
102 /* get default value for layout style */
103
104 GtkButtonBoxStyle
105 gtk_vbutton_box_get_layout_default (void)
106 {
107   return default_layout_style;
108 }
109
110
111
112
113 static void
114 gtk_vbutton_box_size_request (GtkWidget      *widget,
115                               GtkRequisition *requisition)
116 {
117   GtkBox *box;
118   GtkButtonBox *bbox;
119   gint nvis_children;
120   gint child_width;
121   gint child_height;
122   gint spacing;
123   GtkButtonBoxStyle layout;
124   
125   box = GTK_BOX (widget);
126   bbox = GTK_BUTTON_BOX (widget);
127
128   spacing = box->spacing;
129   layout = bbox->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
130           ? bbox->layout_style : default_layout_style;
131   
132   _gtk_button_box_child_requisition (widget,
133                                      &nvis_children,
134                                      NULL,
135                                      &child_width,
136                                      &child_height);
137
138   if (nvis_children == 0)
139     {
140       requisition->width = 0; 
141       requisition->height = 0;
142     }
143   else
144     {
145       switch (layout)
146       {
147       case GTK_BUTTONBOX_SPREAD:
148         requisition->height =
149                 nvis_children*child_height + ((nvis_children+1)*spacing);
150         break;
151       case GTK_BUTTONBOX_EDGE:
152       case GTK_BUTTONBOX_START:
153       case GTK_BUTTONBOX_END:
154         requisition->height =
155                 nvis_children*child_height + ((nvis_children-1)*spacing);
156         break;
157       default:
158             g_assert_not_reached();
159             break;
160       }
161           
162       requisition->width = child_width;
163     }
164           
165   requisition->width += GTK_CONTAINER (box)->border_width * 2;
166   requisition->height += GTK_CONTAINER (box)->border_width * 2;
167 }
168
169
170
171 static void
172 gtk_vbutton_box_size_allocate (GtkWidget     *widget,
173                                GtkAllocation *allocation)
174 {
175   GtkBox *base_box;
176   GtkButtonBox *box;
177   GtkBoxChild *child;
178   GList *children;
179   GtkAllocation child_allocation;
180   gint nvis_children;
181   gint n_secondaries;
182   gint child_width;
183   gint child_height;
184   gint x = 0;
185   gint y = 0;
186   gint secondary_y = 0;
187   gint height;
188   gint childspace;
189   gint childspacing = 0;
190   GtkButtonBoxStyle layout;
191   gint spacing;
192   
193   base_box = GTK_BOX (widget);
194   box = GTK_BUTTON_BOX (widget);
195   spacing = base_box->spacing;
196   layout = box->layout_style != GTK_BUTTONBOX_DEFAULT_STYLE
197           ? box->layout_style : default_layout_style;
198   _gtk_button_box_child_requisition (widget,
199                                      &nvis_children,
200                                      &n_secondaries,
201                                      &child_width,
202                                      &child_height);
203   widget->allocation = *allocation;
204   height = allocation->height - GTK_CONTAINER (box)->border_width*2;
205   switch (layout)
206   {
207   case GTK_BUTTONBOX_SPREAD:
208     childspacing = (height - (nvis_children * child_height)) / (nvis_children + 1);
209     y = allocation->y + GTK_CONTAINER (box)->border_width + childspacing;
210     secondary_y = y + ((nvis_children - n_secondaries) * (child_height + childspacing));
211     break;
212   case GTK_BUTTONBOX_EDGE:
213     if (nvis_children >= 2)
214       {
215         childspacing = (height - (nvis_children*child_height)) / (nvis_children-1);
216         y = allocation->y + GTK_CONTAINER (box)->border_width;
217         secondary_y = y + ((nvis_children - n_secondaries) * (child_height + childspacing));
218       }
219     else
220       {
221         /* one or zero children, just center */
222         childspacing = height;
223         y = secondary_y = allocation->y + (allocation->height - child_height) / 2;
224       }
225     break;
226   case GTK_BUTTONBOX_START:
227     childspacing = spacing;
228     y = allocation->y + GTK_CONTAINER (box)->border_width;
229     secondary_y = allocation->y + allocation->height
230       - child_height * n_secondaries
231       - spacing * (n_secondaries - 1)
232       - GTK_CONTAINER (box)->border_width;
233     break;
234   case GTK_BUTTONBOX_END:
235     childspacing = spacing;
236     y = allocation->y + allocation->height 
237       - child_height * (nvis_children - n_secondaries)
238       - spacing * (nvis_children - n_secondaries - 1)
239       - GTK_CONTAINER (box)->border_width;
240     secondary_y = allocation->y + GTK_CONTAINER (box)->border_width;
241     break;
242   default:
243     g_assert_not_reached();
244     break;
245   }
246                   
247   
248   x = allocation->x + (allocation->width - child_width) / 2;
249   childspace = child_height + childspacing;
250
251   children = GTK_BOX (box)->children;
252           
253   while (children)
254     {
255       child = children->data;
256       children = children->next;
257
258       if (GTK_WIDGET_VISIBLE (child->widget))
259         {
260           child_allocation.width = child_width;
261           child_allocation.height = child_height;
262           child_allocation.x = x;
263           
264           if (child->is_secondary)
265             {
266               child_allocation.y = secondary_y;
267               secondary_y += childspace;
268             }
269           else
270             {
271               child_allocation.y = y;
272               y += childspace;
273             }
274           
275           gtk_widget_size_allocate (child->widget, &child_allocation);
276         }
277     }
278 }
279   
280 #define __GTK_VBBOX_C__
281 #include "gtkaliasdef.c"