Enabling multithreaded compilation.
[debian/lxpanel.git] / src / gtkbar.c
CommitLineData
6cc5e1a6
DB
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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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 "gtkbar.h"
28
29//#define DEBUG
30#include "dbg.h"
31
32#define MAX_CHILD_SIZE 150
33
34static void gtk_bar_class_init (GtkBarClass *klass);
35static void gtk_bar_init (GtkBar *box);
36static void gtk_bar_size_request (GtkWidget *widget, GtkRequisition *requisition);
37static void gtk_bar_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
38//static gint gtk_bar_expose (GtkWidget *widget, GdkEventExpose *event);
39
40static GtkBoxClass *parent_class = NULL;
41
42GType
43gtk_bar_get_type (void)
44{
45 static GType bar_type = 0;
46
47 if (!bar_type)
48 {
49 static const GTypeInfo bar_info =
50 {
51 sizeof (GtkBarClass),
52 NULL, /* base_init */
53 NULL, /* base_finalize */
54 (GClassInitFunc) gtk_bar_class_init,
55 NULL, /* class_finalize */
56 NULL, /* class_data */
57 sizeof (GtkBar),
58 0, /* n_preallocs */
59 (GInstanceInitFunc) gtk_bar_init,
60 };
61
62 bar_type = g_type_register_static (GTK_TYPE_BOX, "GtkBar",
63 &bar_info, 0);
64 }
65
66 return bar_type;
67}
68
69static void
70gtk_bar_class_init (GtkBarClass *class)
71{
72 GtkWidgetClass *widget_class;
73
74 parent_class = g_type_class_peek_parent (class);
75 widget_class = (GtkWidgetClass*) class;
76
77 widget_class->size_request = gtk_bar_size_request;
78 widget_class->size_allocate = gtk_bar_size_allocate;
79 //widget_class->expose_event = gtk_bar_expose;
80
81}
82
83static void
84gtk_bar_init (GtkBar *bar)
85{
86 bar->max_child_size = MAX_CHILD_SIZE;
87 //gtk_widget_set_redraw_on_allocate (GTK_WIDGET (bar), TRUE);
88}
89
90GtkWidget*
91gtk_bar_new (GtkOrientation orient, gint spacing)
92{
93 GtkBar *bar;
94
95 bar = g_object_new (GTK_TYPE_BAR, NULL);
96 GTK_BOX (bar)->spacing = spacing;
97 bar->orient = orient;
98 return (GtkWidget *)bar;
99}
100
101
102static void
103gtk_bar_size_request (GtkWidget *widget, GtkRequisition *requisition)
104{
105 GtkBox *box;
106 GtkBar *bar;
107 GtkBoxChild *child;
108 GList *children;
109 gint nvis_children;
110
111 box = GTK_BOX (widget);
112 bar = GTK_BAR (widget);
113 requisition->width = 0;
114 requisition->height = 0;
115 nvis_children = 0;
116
117 children = box->children;
118 while (children) {
119 child = children->data;
120 children = children->next;
121
122 if (GTK_WIDGET_VISIBLE (child->widget)) {
123 GtkRequisition child_requisition;
124
125 gtk_widget_size_request (child->widget, &child_requisition);
126 if (bar->orient == GTK_ORIENTATION_HORIZONTAL)
127 requisition->height = MAX (requisition->height, child_requisition.height);
128 else
129 requisition->width = MAX (requisition->width, child_requisition.width);
130 nvis_children++;
131 }
132 }
133
134 if (nvis_children > 0) {
135 if (bar->orient == GTK_ORIENTATION_HORIZONTAL) {
136 requisition->width = nvis_children * GTK_BAR(widget)->max_child_size;
137 requisition->width += (nvis_children - 1) * box->spacing;
138 } else {
139 requisition->height = nvis_children * GTK_BAR(widget)->max_child_size;
140 requisition->height += (nvis_children - 1) * box->spacing;
141 }
142 }
143
144 requisition->width += GTK_CONTAINER (box)->border_width * 2;
145 requisition->height += GTK_CONTAINER (box)->border_width * 2;
146}
147
148static void
149gtk_bar_size_allocate (GtkWidget *widget,
150 GtkAllocation *allocation)
151{
152 GtkBox *box;
153 GtkBar *bar;
154 GtkBoxChild *child;
155 GList *children;
156 GtkAllocation child_allocation;
157 gint nvis_children;
158 gint bw;
159 gint tmp;
10862fa6 160 gboolean is_rtl = (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL);
6cc5e1a6
DB
161
162 ENTER;
163 box = GTK_BOX (widget);
164 bar = GTK_BAR (widget);
165 widget->allocation = *allocation;
166
167 bw = GTK_CONTAINER (box)->border_width;
168 nvis_children = 0;
169 bar->maxx = bar->maxy = 0;
170 children = box->children;
10862fa6
DB
171 while (children)
172 {
6cc5e1a6
DB
173 child = children->data;
174 children = children->next;
175
176 if (GTK_WIDGET_VISIBLE (child->widget))
177 nvis_children += 1;
178 }
179 gtk_widget_queue_draw(widget);
180 if (nvis_children == 0)
181 RET();
182
10862fa6
DB
183 if( is_rtl )
184 child_allocation.x = allocation->x + allocation->width - bw;
185 else
186 child_allocation.x = allocation->x + bw;
6cc5e1a6
DB
187 child_allocation.y = allocation->y + bw;
188
10862fa6
DB
189 if (bar->orient == GTK_ORIENTATION_HORIZONTAL)
190 {
6cc5e1a6
DB
191 child_allocation.height = MAX (1, (gint) allocation->height - bw * 2);
192 tmp = (allocation->width - bw * 2 - (nvis_children - 1) * box->spacing);
193 child_allocation.width = MAX (1, MIN(tmp / nvis_children, bar->max_child_size));
10862fa6
DB
194
195 if(is_rtl)
196 child_allocation.x -= child_allocation.width;
197 }
198 else
199 {
6cc5e1a6
DB
200 child_allocation.width = MAX (1, (gint) allocation->width - bw * 2);
201 tmp = (allocation->height - bw * 2 - (nvis_children - 1) * box->spacing);
202 child_allocation.height = MAX (1, MIN(tmp / nvis_children, bar->max_child_size));
203 }
204
205 children = box->children;
10862fa6
DB
206 while (children)
207 {
6cc5e1a6
DB
208 child = children->data;
209 children = children->next;
210
10862fa6
DB
211 if (GTK_WIDGET_VISIBLE (child->widget))
212 {
6cc5e1a6
DB
213 gtk_widget_size_allocate (child->widget, &child_allocation);
214 bar->maxx = child_allocation.x;
215 bar->maxy = child_allocation.y;
216 if (bar->orient == GTK_ORIENTATION_HORIZONTAL)
10862fa6
DB
217 {
218 if( is_rtl )
219 child_allocation.x -= (child_allocation.width + box->spacing);
220 else
221 child_allocation.x += child_allocation.width + box->spacing;
222 }
6cc5e1a6
DB
223 else
224 child_allocation.y += child_allocation.height + box->spacing;
225 }
226 }
227 bar->maxx += child_allocation.width;
228 bar->maxy += child_allocation.height;
229 DBG("w, h = %d,%d\n", bar->maxx, bar->maxy);
230
231 RET();
232}
233
234
235void
236gtk_bar_set_max_child_size(GtkBar *bar, gint size)
237{
238 g_return_if_fail (GTK_IS_BAR (bar));
239
240 if (size != bar->max_child_size) {
241 bar->max_child_size = size;
242 //g_object_notify (G_OBJECT (bar), "spacing");
243 gtk_widget_queue_resize (GTK_WIDGET (bar));
244 }
245}
246
247#if 0
248static gint
249gtk_bar_expose (GtkWidget *widget, GdkEventExpose *event)
250{
251 ENTER;
252
253 if (GTK_WIDGET_DRAWABLE (widget)) {
254 int w, h;
255
256 w = GTK_BAR(widget)->maxx;
257 h = GTK_BAR(widget)->maxy;
258 DBG("w, h = %d,%d\n", w, h);
259 if (!GTK_WIDGET_APP_PAINTABLE (widget))
260 gtk_paint_flat_box (widget->style, widget->window,
261 widget->state, GTK_SHADOW_NONE,
262 NULL /*&event->area*/, widget, NULL,
263 0, 0, w, h);
264 }
265 RET(FALSE);
266}
267#endif
268
269void gtk_bar_set_orientation( GtkBar *bar, GtkOrientation ori )
270{
271 if( ori == bar->orient )
272 return;
273 bar->orient = ori;
274 gtk_widget_queue_resize( GTK_WIDGET(bar) );
275}