1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /** |
---|
3 | * bonobo-ui-toolbar-item.c |
---|
4 | * |
---|
5 | * Author: Ettore Perazzoli |
---|
6 | * |
---|
7 | * Copyright (C) 2000 Helix Code, Inc. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include <config.h> |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <gnome.h> |
---|
15 | #include "bonobo-ui-toolbar-item.h" |
---|
16 | |
---|
17 | |
---|
18 | #define PARENT_TYPE gtk_bin_get_type () |
---|
19 | static GtkBinClass *parent_class = NULL; |
---|
20 | |
---|
21 | struct _BonoboUIToolbarItemPrivate { |
---|
22 | /* Whether this item wants to have a label when the toolbar style is |
---|
23 | `BONOBO_UI_TOOLBAR_STYLE_PRIORITY_TEXT'. */ |
---|
24 | gboolean want_label; |
---|
25 | |
---|
26 | /* Whether this item wants to be expanded to all the available |
---|
27 | width/height. */ |
---|
28 | gboolean expandable; |
---|
29 | |
---|
30 | /* if set, pack this item on the right side of the toolbar */ |
---|
31 | gboolean pack_end; |
---|
32 | |
---|
33 | /* Orientation for this item. */ |
---|
34 | GtkOrientation orientation; |
---|
35 | |
---|
36 | /* Style for this item. */ |
---|
37 | BonoboUIToolbarItemStyle style; |
---|
38 | |
---|
39 | /* minimum width (or height, if rotated) for this item */ |
---|
40 | int minimum_width; |
---|
41 | }; |
---|
42 | |
---|
43 | enum { |
---|
44 | SET_ORIENTATION, |
---|
45 | SET_STYLE, |
---|
46 | SET_WANT_LABEL, |
---|
47 | ACTIVATE, |
---|
48 | STATE_ALTERED, |
---|
49 | LAST_SIGNAL |
---|
50 | }; |
---|
51 | |
---|
52 | static int signals[LAST_SIGNAL] = { 0 }; |
---|
53 | |
---|
54 | |
---|
55 | /* GtkObject methods. */ |
---|
56 | |
---|
57 | static void |
---|
58 | impl_destroy (GtkObject *object) |
---|
59 | { |
---|
60 | BonoboUIToolbarItem *toolbar_item; |
---|
61 | BonoboUIToolbarItemPrivate *priv; |
---|
62 | |
---|
63 | toolbar_item = BONOBO_UI_TOOLBAR_ITEM (object); |
---|
64 | priv = toolbar_item->priv; |
---|
65 | |
---|
66 | g_free (priv); |
---|
67 | |
---|
68 | if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL) |
---|
69 | (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | /* GtkWidget methods. */ |
---|
74 | |
---|
75 | static void |
---|
76 | impl_size_request (GtkWidget *widget, |
---|
77 | GtkRequisition *requisition_return) |
---|
78 | { |
---|
79 | BonoboUIToolbarItem *toolbar_item; |
---|
80 | BonoboUIToolbarItemPrivate *priv; |
---|
81 | GtkRequisition child_requisition; |
---|
82 | GtkWidget *child; |
---|
83 | int border_width; |
---|
84 | |
---|
85 | toolbar_item = BONOBO_UI_TOOLBAR_ITEM (widget); |
---|
86 | priv = toolbar_item->priv; |
---|
87 | |
---|
88 | border_width = GTK_CONTAINER (widget)->border_width; |
---|
89 | requisition_return->width = border_width; |
---|
90 | requisition_return->height = border_width; |
---|
91 | |
---|
92 | child = GTK_BIN (widget)->child; |
---|
93 | if (child == NULL) |
---|
94 | return; |
---|
95 | |
---|
96 | gtk_widget_size_request (child, &child_requisition); |
---|
97 | |
---|
98 | if (child_requisition.width < priv->minimum_width) |
---|
99 | child_requisition.width = priv->minimum_width; |
---|
100 | |
---|
101 | requisition_return->width += child_requisition.width; |
---|
102 | requisition_return->height += child_requisition.height; |
---|
103 | } |
---|
104 | |
---|
105 | static void |
---|
106 | impl_size_allocate (GtkWidget *widget, |
---|
107 | GtkAllocation *allocation) |
---|
108 | { |
---|
109 | GtkAllocation child_allocation; |
---|
110 | GtkWidget *child; |
---|
111 | int border_width; |
---|
112 | |
---|
113 | widget->allocation = *allocation; |
---|
114 | |
---|
115 | child = GTK_BIN (widget)->child; |
---|
116 | if (child == NULL) |
---|
117 | return; |
---|
118 | |
---|
119 | border_width = GTK_CONTAINER (widget)->border_width; |
---|
120 | |
---|
121 | if (allocation->width > border_width) { |
---|
122 | child_allocation.x = allocation->x + border_width; |
---|
123 | child_allocation.width = allocation->width - border_width; |
---|
124 | } else { |
---|
125 | child_allocation.x = allocation->x; |
---|
126 | child_allocation.width = allocation->width; |
---|
127 | } |
---|
128 | |
---|
129 | if (allocation->height > border_width) { |
---|
130 | child_allocation.y = allocation->y + border_width; |
---|
131 | child_allocation.height = allocation->height - border_width; |
---|
132 | } else { |
---|
133 | child_allocation.y = allocation->y; |
---|
134 | child_allocation.height = allocation->height; |
---|
135 | } |
---|
136 | |
---|
137 | gtk_widget_size_allocate (GTK_BIN (widget)->child, &child_allocation); |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | /* BonoboUIToolbarItem signals. */ |
---|
142 | |
---|
143 | static void |
---|
144 | impl_set_orientation (BonoboUIToolbarItem *item, |
---|
145 | GtkOrientation orientation) |
---|
146 | { |
---|
147 | BonoboUIToolbarItemPrivate *priv; |
---|
148 | |
---|
149 | priv = item->priv; |
---|
150 | |
---|
151 | if (priv->orientation == orientation) |
---|
152 | return; |
---|
153 | |
---|
154 | priv->orientation = orientation; |
---|
155 | |
---|
156 | gtk_widget_queue_resize (GTK_WIDGET (item)); |
---|
157 | } |
---|
158 | |
---|
159 | static void |
---|
160 | impl_set_style (BonoboUIToolbarItem *item, |
---|
161 | BonoboUIToolbarItemStyle style) |
---|
162 | { |
---|
163 | BonoboUIToolbarItemPrivate *priv; |
---|
164 | |
---|
165 | priv = item->priv; |
---|
166 | |
---|
167 | if (priv->style == style) |
---|
168 | return; |
---|
169 | |
---|
170 | priv->style = style; |
---|
171 | |
---|
172 | gtk_widget_queue_resize (GTK_WIDGET (item)); |
---|
173 | } |
---|
174 | |
---|
175 | static void |
---|
176 | impl_set_want_label (BonoboUIToolbarItem *item, |
---|
177 | gboolean want_label) |
---|
178 | { |
---|
179 | BonoboUIToolbarItemPrivate *priv; |
---|
180 | |
---|
181 | priv = item->priv; |
---|
182 | |
---|
183 | priv->want_label = want_label; |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | /* Gtk+ object initialization. */ |
---|
188 | |
---|
189 | static void |
---|
190 | class_init (GtkObjectClass *object_class) |
---|
191 | { |
---|
192 | GtkWidgetClass *widget_class; |
---|
193 | BonoboUIToolbarItemClass *toolbar_item_class; |
---|
194 | |
---|
195 | object_class->destroy = impl_destroy; |
---|
196 | |
---|
197 | widget_class = GTK_WIDGET_CLASS (object_class); |
---|
198 | widget_class->size_request = impl_size_request; |
---|
199 | widget_class->size_allocate = impl_size_allocate; |
---|
200 | |
---|
201 | toolbar_item_class = BONOBO_UI_TOOLBAR_ITEM_CLASS (object_class); |
---|
202 | toolbar_item_class->set_orientation = impl_set_orientation; |
---|
203 | toolbar_item_class->set_style = impl_set_style; |
---|
204 | toolbar_item_class->set_want_label = impl_set_want_label; |
---|
205 | |
---|
206 | signals[SET_ORIENTATION] |
---|
207 | = gtk_signal_new ("set_orientation", |
---|
208 | GTK_RUN_LAST, |
---|
209 | object_class->type, |
---|
210 | GTK_SIGNAL_OFFSET (BonoboUIToolbarItemClass, set_orientation), |
---|
211 | gtk_marshal_NONE__INT, |
---|
212 | GTK_TYPE_NONE, 1, |
---|
213 | GTK_TYPE_INT); |
---|
214 | |
---|
215 | signals[SET_STYLE] |
---|
216 | = gtk_signal_new ("set_style", |
---|
217 | GTK_RUN_LAST, |
---|
218 | object_class->type, |
---|
219 | GTK_SIGNAL_OFFSET (BonoboUIToolbarItemClass, set_style), |
---|
220 | gtk_marshal_NONE__INT, |
---|
221 | GTK_TYPE_NONE, 1, |
---|
222 | GTK_TYPE_INT); |
---|
223 | |
---|
224 | signals[SET_WANT_LABEL] = |
---|
225 | gtk_signal_new ("set_want_label", |
---|
226 | GTK_RUN_FIRST, |
---|
227 | object_class->type, |
---|
228 | GTK_SIGNAL_OFFSET (BonoboUIToolbarItemClass, set_want_label), |
---|
229 | gtk_marshal_NONE__BOOL, |
---|
230 | GTK_TYPE_NONE, 1, |
---|
231 | GTK_TYPE_BOOL); |
---|
232 | |
---|
233 | signals[STATE_ALTERED] |
---|
234 | = gtk_signal_new ("state_altered", |
---|
235 | GTK_RUN_LAST, |
---|
236 | object_class->type, |
---|
237 | GTK_SIGNAL_OFFSET (BonoboUIToolbarItemClass, activate), |
---|
238 | gtk_marshal_NONE__STRING, |
---|
239 | GTK_TYPE_NONE, 1, GTK_TYPE_STRING); |
---|
240 | |
---|
241 | signals[ACTIVATE] |
---|
242 | = gtk_signal_new ("activate", |
---|
243 | GTK_RUN_LAST, |
---|
244 | object_class->type, |
---|
245 | GTK_SIGNAL_OFFSET (BonoboUIToolbarItemClass, activate), |
---|
246 | gtk_marshal_NONE__NONE, |
---|
247 | GTK_TYPE_NONE, 0); |
---|
248 | |
---|
249 | gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL); |
---|
250 | |
---|
251 | parent_class = gtk_type_class (PARENT_TYPE); |
---|
252 | } |
---|
253 | |
---|
254 | static void |
---|
255 | init (GtkObject *object) |
---|
256 | { |
---|
257 | BonoboUIToolbarItem *toolbar_item; |
---|
258 | BonoboUIToolbarItemPrivate *priv; |
---|
259 | |
---|
260 | toolbar_item = BONOBO_UI_TOOLBAR_ITEM (object); |
---|
261 | |
---|
262 | priv = g_new (BonoboUIToolbarItemPrivate, 1); |
---|
263 | |
---|
264 | priv->want_label = FALSE; |
---|
265 | priv->orientation = GTK_ORIENTATION_HORIZONTAL; |
---|
266 | priv->style = BONOBO_UI_TOOLBAR_ITEM_STYLE_ICON_AND_TEXT_VERTICAL; |
---|
267 | priv->expandable = FALSE; |
---|
268 | priv->pack_end = FALSE; |
---|
269 | priv->minimum_width = 0; |
---|
270 | |
---|
271 | toolbar_item->priv = priv; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | GtkType |
---|
276 | bonobo_ui_toolbar_item_get_type (void) |
---|
277 | { |
---|
278 | static GtkType type = 0; |
---|
279 | |
---|
280 | if (type == 0) { |
---|
281 | static const GtkTypeInfo info = { |
---|
282 | "BonoboUIToolbarItem", |
---|
283 | sizeof (BonoboUIToolbarItem), |
---|
284 | sizeof (BonoboUIToolbarItemClass), |
---|
285 | (GtkClassInitFunc) class_init, |
---|
286 | (GtkObjectInitFunc) init, |
---|
287 | /* reserved_1 */ NULL, |
---|
288 | /* reserved_2 */ NULL, |
---|
289 | (GtkClassInitFunc) NULL, |
---|
290 | }; |
---|
291 | |
---|
292 | type = gtk_type_unique (PARENT_TYPE, &info); |
---|
293 | } |
---|
294 | |
---|
295 | return type; |
---|
296 | } |
---|
297 | |
---|
298 | GtkWidget * |
---|
299 | bonobo_ui_toolbar_item_new (void) |
---|
300 | { |
---|
301 | BonoboUIToolbarItem *new; |
---|
302 | |
---|
303 | new = gtk_type_new (bonobo_ui_toolbar_item_get_type ()); |
---|
304 | |
---|
305 | return GTK_WIDGET (new); |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | void |
---|
310 | bonobo_ui_toolbar_item_set_tooltip (BonoboUIToolbarItem *item, |
---|
311 | GtkTooltips *tooltips, |
---|
312 | const char *tooltip) |
---|
313 | { |
---|
314 | BonoboUIToolbarItemClass *klass; |
---|
315 | |
---|
316 | g_return_if_fail (item != NULL); |
---|
317 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
318 | |
---|
319 | klass = BONOBO_UI_TOOLBAR_ITEM_CLASS (((GtkObject *)item)->klass); |
---|
320 | |
---|
321 | if (klass->set_tooltip) |
---|
322 | klass->set_tooltip (item, tooltips, tooltip); |
---|
323 | |
---|
324 | /* FIXME: implement setting of tooltips */ |
---|
325 | } |
---|
326 | |
---|
327 | void |
---|
328 | bonobo_ui_toolbar_item_set_state (BonoboUIToolbarItem *item, |
---|
329 | const char *new_state) |
---|
330 | { |
---|
331 | BonoboUIToolbarItemClass *klass; |
---|
332 | |
---|
333 | g_return_if_fail (item != NULL); |
---|
334 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
335 | |
---|
336 | klass = BONOBO_UI_TOOLBAR_ITEM_CLASS (((GtkObject *)item)->klass); |
---|
337 | |
---|
338 | if (klass->set_state) |
---|
339 | klass->set_state (item, new_state); |
---|
340 | } |
---|
341 | |
---|
342 | void |
---|
343 | bonobo_ui_toolbar_item_set_orientation (BonoboUIToolbarItem *item, |
---|
344 | GtkOrientation orientation) |
---|
345 | { |
---|
346 | g_return_if_fail (item != NULL); |
---|
347 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
348 | g_return_if_fail (orientation == GTK_ORIENTATION_HORIZONTAL |
---|
349 | || orientation == GTK_ORIENTATION_VERTICAL); |
---|
350 | |
---|
351 | gtk_signal_emit (GTK_OBJECT (item), signals[SET_ORIENTATION], orientation); |
---|
352 | } |
---|
353 | |
---|
354 | GtkOrientation |
---|
355 | bonobo_ui_toolbar_item_get_orientation (BonoboUIToolbarItem *item) |
---|
356 | { |
---|
357 | BonoboUIToolbarItemPrivate *priv; |
---|
358 | |
---|
359 | g_return_val_if_fail (item != NULL, GTK_ORIENTATION_HORIZONTAL); |
---|
360 | g_return_val_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item), GTK_ORIENTATION_HORIZONTAL); |
---|
361 | |
---|
362 | priv = item->priv; |
---|
363 | |
---|
364 | return priv->orientation; |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | void |
---|
369 | bonobo_ui_toolbar_item_set_style (BonoboUIToolbarItem *item, |
---|
370 | BonoboUIToolbarItemStyle style) |
---|
371 | { |
---|
372 | g_return_if_fail (item != NULL); |
---|
373 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
374 | g_return_if_fail (style == BONOBO_UI_TOOLBAR_ITEM_STYLE_ICON_ONLY |
---|
375 | || style == BONOBO_UI_TOOLBAR_ITEM_STYLE_TEXT_ONLY |
---|
376 | || style == BONOBO_UI_TOOLBAR_ITEM_STYLE_ICON_AND_TEXT_HORIZONTAL |
---|
377 | || style == BONOBO_UI_TOOLBAR_ITEM_STYLE_ICON_AND_TEXT_VERTICAL); |
---|
378 | |
---|
379 | gtk_signal_emit (GTK_OBJECT (item), signals[SET_STYLE], style); |
---|
380 | } |
---|
381 | |
---|
382 | BonoboUIToolbarItemStyle |
---|
383 | bonobo_ui_toolbar_item_get_style (BonoboUIToolbarItem *item) |
---|
384 | { |
---|
385 | BonoboUIToolbarItemPrivate *priv; |
---|
386 | |
---|
387 | g_return_val_if_fail (item != NULL, |
---|
388 | BONOBO_UI_TOOLBAR_ITEM_STYLE_ICON_AND_TEXT_VERTICAL); |
---|
389 | g_return_val_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item), |
---|
390 | BONOBO_UI_TOOLBAR_ITEM_STYLE_ICON_AND_TEXT_VERTICAL); |
---|
391 | |
---|
392 | priv = item->priv; |
---|
393 | |
---|
394 | return priv->style; |
---|
395 | } |
---|
396 | |
---|
397 | void |
---|
398 | bonobo_ui_toolbar_item_set_want_label (BonoboUIToolbarItem *item, |
---|
399 | gboolean want_label) |
---|
400 | { |
---|
401 | g_return_if_fail (item != NULL); |
---|
402 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
403 | |
---|
404 | gtk_signal_emit (GTK_OBJECT (item), signals[SET_WANT_LABEL], want_label); |
---|
405 | } |
---|
406 | |
---|
407 | gboolean |
---|
408 | bonobo_ui_toolbar_item_get_want_label (BonoboUIToolbarItem *item) |
---|
409 | { |
---|
410 | BonoboUIToolbarItemPrivate *priv; |
---|
411 | |
---|
412 | g_return_val_if_fail (item != NULL, FALSE); |
---|
413 | g_return_val_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item), FALSE); |
---|
414 | |
---|
415 | priv = item->priv; |
---|
416 | |
---|
417 | return priv->want_label; |
---|
418 | } |
---|
419 | |
---|
420 | void |
---|
421 | bonobo_ui_toolbar_item_set_expandable (BonoboUIToolbarItem *item, |
---|
422 | gboolean expandable) |
---|
423 | { |
---|
424 | BonoboUIToolbarItemPrivate *priv; |
---|
425 | |
---|
426 | g_return_if_fail (item != NULL); |
---|
427 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
428 | |
---|
429 | priv = item->priv; |
---|
430 | |
---|
431 | if ((priv->expandable && expandable) || (! priv->expandable && ! expandable)) |
---|
432 | return; |
---|
433 | |
---|
434 | priv->expandable = expandable; |
---|
435 | gtk_widget_queue_resize (GTK_WIDGET (item)); |
---|
436 | } |
---|
437 | |
---|
438 | gboolean |
---|
439 | bonobo_ui_toolbar_item_get_expandable (BonoboUIToolbarItem *item) |
---|
440 | { |
---|
441 | BonoboUIToolbarItemPrivate *priv; |
---|
442 | |
---|
443 | g_return_val_if_fail (item != NULL, FALSE); |
---|
444 | g_return_val_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item), FALSE); |
---|
445 | |
---|
446 | priv = item->priv; |
---|
447 | return priv->expandable; |
---|
448 | } |
---|
449 | |
---|
450 | void |
---|
451 | bonobo_ui_toolbar_item_set_pack_end (BonoboUIToolbarItem *item, |
---|
452 | gboolean pack_end) |
---|
453 | { |
---|
454 | BonoboUIToolbarItemPrivate *priv; |
---|
455 | |
---|
456 | g_return_if_fail (item != NULL); |
---|
457 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
458 | |
---|
459 | priv = item->priv; |
---|
460 | |
---|
461 | if ((priv->pack_end && pack_end) || (! priv->pack_end && ! pack_end)) |
---|
462 | return; |
---|
463 | |
---|
464 | priv->pack_end = pack_end; |
---|
465 | gtk_widget_queue_resize (GTK_WIDGET (item)); |
---|
466 | } |
---|
467 | |
---|
468 | gboolean |
---|
469 | bonobo_ui_toolbar_item_get_pack_end (BonoboUIToolbarItem *item) |
---|
470 | { |
---|
471 | BonoboUIToolbarItemPrivate *priv; |
---|
472 | |
---|
473 | g_return_val_if_fail (item != NULL, FALSE); |
---|
474 | g_return_val_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item), FALSE); |
---|
475 | |
---|
476 | priv = item->priv; |
---|
477 | return priv->pack_end; |
---|
478 | } |
---|
479 | |
---|
480 | void |
---|
481 | bonobo_ui_toolbar_item_activate (BonoboUIToolbarItem *item) |
---|
482 | { |
---|
483 | g_return_if_fail (item != NULL); |
---|
484 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
485 | |
---|
486 | gtk_signal_emit (GTK_OBJECT (item), signals[ACTIVATE]); |
---|
487 | } |
---|
488 | |
---|
489 | void |
---|
490 | bonobo_ui_toolbar_item_set_minimum_width (BonoboUIToolbarItem *item, int minimum_width) |
---|
491 | { |
---|
492 | g_return_if_fail (item != NULL); |
---|
493 | g_return_if_fail (BONOBO_IS_UI_TOOLBAR_ITEM (item)); |
---|
494 | |
---|
495 | item->priv->minimum_width = minimum_width; |
---|
496 | } |
---|