]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolbar.c
Don't insert mnemonics for stock items (#72918, reported by Mikael
[~andy/gtk] / gtk / gtktoolbar.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * GtkToolbar copyright (C) Federico Mena
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include <string.h>
29
30 #include "gtkbutton.h"
31 #include "gtktogglebutton.h"
32 #include "gtkradiobutton.h"
33 #include "gtklabel.h"
34 #include "gtkvbox.h"
35 #include "gtkhbox.h"
36 #include "gtktoolbar.h"
37 #include "gtkstock.h"
38 #include "gtkiconfactory.h"
39 #include "gtkimage.h"
40 #include "gtksettings.h"
41 #include "gtkintl.h"
42 #include "gtkmarshalers.h"
43
44
45 #define DEFAULT_IPADDING 0
46 #define DEFAULT_SPACE_SIZE  5
47 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
48
49 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR
50 #define DEFAULT_TOOLBAR_STYLE GTK_TOOLBAR_BOTH
51
52 #define SPACE_LINE_DIVISION 10
53 #define SPACE_LINE_START    3
54 #define SPACE_LINE_END      7
55
56 enum {
57   PROP_0,
58   PROP_ORIENTATION,
59   PROP_TOOLBAR_STYLE
60 };
61
62 enum {
63   ORIENTATION_CHANGED,
64   STYLE_CHANGED,
65   LAST_SIGNAL
66 };
67
68 typedef struct _GtkToolbarChildSpace GtkToolbarChildSpace;
69 struct _GtkToolbarChildSpace
70 {
71   GtkToolbarChild child;
72
73   gint alloc_x, alloc_y;
74 };
75
76 static void gtk_toolbar_class_init               (GtkToolbarClass *class);
77 static void gtk_toolbar_init                     (GtkToolbar      *toolbar);
78 static void gtk_toolbar_set_property             (GObject         *object,
79                                                   guint            prop_id,
80                                                   const GValue    *value,
81                                                   GParamSpec      *pspec);
82 static void gtk_toolbar_get_property             (GObject         *object,
83                                                   guint            prop_id,
84                                                   GValue          *value,
85                                                   GParamSpec      *pspec);
86 static void gtk_toolbar_destroy                  (GtkObject       *object);
87 static gint gtk_toolbar_expose                   (GtkWidget       *widget,
88                                                   GdkEventExpose  *event);
89 static void gtk_toolbar_size_request             (GtkWidget       *widget,
90                                                   GtkRequisition  *requisition);
91 static void gtk_toolbar_size_allocate            (GtkWidget       *widget,
92                                                   GtkAllocation   *allocation);
93 static void gtk_toolbar_style_set                (GtkWidget       *widget,
94                                                   GtkStyle        *prev_style);
95 static gboolean gtk_toolbar_focus                (GtkWidget       *widget,
96                                                   GtkDirectionType dir);
97 static void gtk_toolbar_hierarchy_changed        (GtkWidget       *widget,
98                                                   GtkWidget       *previous_toplevel);
99 static void gtk_toolbar_show_all                 (GtkWidget       *widget);
100 static void gtk_toolbar_add                      (GtkContainer    *container,
101                                                   GtkWidget       *widget);
102 static void gtk_toolbar_remove                   (GtkContainer    *container,
103                                                   GtkWidget       *widget);
104 static void gtk_toolbar_forall                   (GtkContainer    *container,
105                                                   gboolean         include_internals,
106                                                   GtkCallback      callback,
107                                                   gpointer         callback_data);
108
109 static void gtk_real_toolbar_orientation_changed (GtkToolbar      *toolbar,
110                                                   GtkOrientation   orientation);
111 static void gtk_real_toolbar_style_changed       (GtkToolbar      *toolbar,
112                                                   GtkToolbarStyle  style);
113
114 static GtkWidget * gtk_toolbar_internal_insert_element (GtkToolbar          *toolbar,
115                                                         GtkToolbarChildType  type,
116                                                         GtkWidget           *widget,
117                                                         const char          *text,
118                                                         const char          *tooltip_text,
119                                                         const char          *tooltip_private_text,
120                                                         GtkWidget           *icon,
121                                                         GtkSignalFunc        callback,
122                                                         gpointer             user_data,
123                                                         gint                 position);
124
125 static GtkWidget * gtk_toolbar_internal_insert_item (GtkToolbar    *toolbar,
126                                                      const char    *text,
127                                                      const char    *tooltip_text,
128                                                      const char    *tooltip_private_text,
129                                                      GtkWidget     *icon,
130                                                      GtkSignalFunc  callback,
131                                                      gpointer       user_data,
132                                                      gint           position);
133
134 static void        gtk_toolbar_update_button_relief (GtkToolbar *toolbar);
135
136 static GtkReliefStyle       get_button_relief (GtkToolbar *toolbar);
137 static gint                 get_space_size    (GtkToolbar *toolbar);
138 static GtkToolbarSpaceStyle get_space_style   (GtkToolbar *toolbar);
139
140
141 static GtkContainerClass *parent_class;
142
143 static guint toolbar_signals[LAST_SIGNAL] = { 0 };
144
145
146 GType
147 gtk_toolbar_get_type (void)
148 {
149   static GType toolbar_type = 0;
150
151   if (!toolbar_type)
152     {
153       static const GTypeInfo toolbar_info =
154       {
155         sizeof (GtkToolbarClass),
156         NULL,           /* base_init */
157         NULL,           /* base_finalize */
158         (GClassInitFunc) gtk_toolbar_class_init,
159         NULL,           /* class_finalize */
160         NULL,           /* class_data */
161         sizeof (GtkToolbar),
162         0,              /* n_preallocs */
163         (GInstanceInitFunc) gtk_toolbar_init,
164       };
165
166       toolbar_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkToolbar",
167                                              &toolbar_info, 0);
168     }
169
170   return toolbar_type;
171 }
172
173 static void
174 gtk_toolbar_class_init (GtkToolbarClass *class)
175 {
176   GObjectClass   *gobject_class;
177   GtkObjectClass *object_class;
178   GtkWidgetClass *widget_class;
179   GtkContainerClass *container_class;
180
181   gobject_class = G_OBJECT_CLASS (class);
182   object_class = (GtkObjectClass *) class;
183   widget_class = (GtkWidgetClass *) class;
184   container_class = (GtkContainerClass *) class;
185
186   parent_class = g_type_class_peek_parent (class);
187
188   object_class->destroy = gtk_toolbar_destroy;
189   gobject_class->set_property = gtk_toolbar_set_property;
190   gobject_class->get_property = gtk_toolbar_get_property;
191
192   widget_class->expose_event = gtk_toolbar_expose;
193   widget_class->size_request = gtk_toolbar_size_request;
194   widget_class->size_allocate = gtk_toolbar_size_allocate;
195   widget_class->style_set = gtk_toolbar_style_set;
196   widget_class->show_all = gtk_toolbar_show_all;
197   widget_class->focus = gtk_toolbar_focus;
198   widget_class->hierarchy_changed = gtk_toolbar_hierarchy_changed;
199   
200   container_class->add = gtk_toolbar_add;
201   container_class->remove = gtk_toolbar_remove;
202   container_class->forall = gtk_toolbar_forall;
203   
204   class->orientation_changed = gtk_real_toolbar_orientation_changed;
205   class->style_changed = gtk_real_toolbar_style_changed;
206
207   toolbar_signals[ORIENTATION_CHANGED] =
208     g_signal_new ("orientation_changed",
209                   G_OBJECT_CLASS_TYPE (gobject_class),
210                   G_SIGNAL_RUN_FIRST,
211                   G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
212                   NULL, NULL,
213                   _gtk_marshal_VOID__ENUM,
214                   G_TYPE_NONE, 1,
215                   GTK_TYPE_ORIENTATION);
216   toolbar_signals[STYLE_CHANGED] =
217     g_signal_new ("style_changed",
218                   G_OBJECT_CLASS_TYPE (gobject_class),
219                   G_SIGNAL_RUN_FIRST,
220                   G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
221                   NULL, NULL,
222                   _gtk_marshal_VOID__ENUM,
223                   G_TYPE_NONE, 1,
224                   GTK_TYPE_TOOLBAR_STYLE);
225   
226   g_object_class_install_property (gobject_class,
227                                    PROP_ORIENTATION,
228                                    g_param_spec_enum ("orientation",
229                                                       _("Orientation"),
230                                                       _("The orientation of the toolbar"),
231                                                       GTK_TYPE_ORIENTATION,
232                                                       GTK_ORIENTATION_HORIZONTAL,
233                                                       G_PARAM_READWRITE));
234  
235    g_object_class_install_property (gobject_class,
236                                     PROP_TOOLBAR_STYLE,
237                                     g_param_spec_enum ("toolbar_style",
238                                                       _("Toolbar Style"),
239                                                       _("How to draw the toolbar"),
240                                                       GTK_TYPE_TOOLBAR_STYLE,
241                                                       GTK_TOOLBAR_ICONS,
242                                                       G_PARAM_READWRITE));
243
244
245   gtk_widget_class_install_style_property (widget_class,
246                                            g_param_spec_int ("space_size",
247                                                              _("Spacer size"),
248                                                              _("Size of spacers"),
249                                                              0,
250                                                              G_MAXINT,
251                                                              DEFAULT_SPACE_SIZE,
252                                                              G_PARAM_READABLE));
253
254   gtk_widget_class_install_style_property (widget_class,
255                                            g_param_spec_int ("internal_padding",
256                                                              _("Internal padding"),
257                                                              _("Amount of border space between the toolbar shadow and the buttons"),
258                                                              0,
259                                                              G_MAXINT,
260                                                              DEFAULT_IPADDING,
261                                                              G_PARAM_READABLE));
262   
263   gtk_widget_class_install_style_property (widget_class,
264                                            g_param_spec_enum ("space_style",
265                                                              _("Space style"),
266                                                              _("Whether spacers are vertical lines or just blank"),
267                                                               GTK_TYPE_TOOLBAR_SPACE_STYLE,
268                                                               DEFAULT_SPACE_STYLE,
269                                                               
270                                                               G_PARAM_READABLE));
271
272   gtk_widget_class_install_style_property (widget_class,
273                                            g_param_spec_enum ("button_relief",
274                                                              _("Button relief"),
275                                                              _("Type of bevel around toolbar buttons"),
276                                                               GTK_TYPE_RELIEF_STYLE,
277                                                               GTK_RELIEF_NONE,
278                                                               G_PARAM_READABLE));
279
280   gtk_widget_class_install_style_property (widget_class,
281                                            g_param_spec_enum ("shadow_type",
282                                                               _("Shadow type"),
283                                                               _("Style of bevel around the toolbar"),
284                                                               GTK_TYPE_SHADOW_TYPE,
285                                                               GTK_SHADOW_OUT,
286                                                               G_PARAM_READABLE));
287
288   gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-style",
289                                                     _("Toolbar style"),
290                                                     _("Whether default toolbars have text only, text and icons, icons only, etc."),
291                                                     GTK_TYPE_TOOLBAR_STYLE,
292                                                     DEFAULT_TOOLBAR_STYLE,
293                                                     G_PARAM_READWRITE));
294
295   gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-icon-size",
296                                                     _("Toolbar icon size"),
297                                                     _("Size of icons in default toolbars"),
298                                                     GTK_TYPE_ICON_SIZE,
299                                                     DEFAULT_ICON_SIZE,
300                                                     G_PARAM_READWRITE));  
301 }
302
303 static void
304 style_change_notify (GtkToolbar *toolbar)
305 {
306   if (!toolbar->style_set)
307     {
308       /* pretend it was set, then unset, thus reverting to new default */
309       toolbar->style_set = TRUE; 
310       gtk_toolbar_unset_style (toolbar);
311     }
312 }
313
314 static void
315 icon_size_change_notify (GtkToolbar *toolbar)
316 {
317   if (!toolbar->icon_size_set)
318     {
319       /* pretend it was set, then unset, thus reverting to new default */
320       toolbar->icon_size_set = TRUE; 
321       gtk_toolbar_unset_icon_size (toolbar);
322     }
323 }
324
325 static GtkSettings *
326 toolbar_get_settings (GtkToolbar *toolbar)
327 {
328   return g_object_get_data (G_OBJECT (toolbar), "gtk-toolbar-settings");
329 }
330
331 static void
332 toolbar_screen_changed (GtkToolbar *toolbar)
333 {
334   GtkSettings *old_settings = toolbar_get_settings (toolbar);
335   GtkSettings *settings;
336
337   if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
338     settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
339   else
340     settings = NULL;
341
342   if (settings == old_settings)
343     return;
344
345   if (old_settings)
346     {
347       g_signal_handler_disconnect (old_settings, toolbar->style_set_connection);
348       g_signal_handler_disconnect (old_settings, toolbar->icon_size_connection);
349
350       g_object_unref (old_settings);
351     }
352   
353   if (settings)
354     {
355       toolbar->style_set_connection =
356         g_signal_connect_swapped (settings,
357                                   "notify::gtk-toolbar-style",
358                                   G_CALLBACK (style_change_notify),
359                                   toolbar);
360       
361       toolbar->icon_size_connection =
362         g_signal_connect_swapped (settings,
363                                   "notify::gtk-toolbar-icon-size",
364                                    G_CALLBACK (icon_size_change_notify),
365                                    toolbar);
366
367
368       g_object_ref (settings);
369       g_object_set_data (G_OBJECT (toolbar), "gtk-toolbar-settings", settings);
370     }
371   else
372     g_object_set_data (G_OBJECT (toolbar), "gtk-toolbar-settings", NULL);
373
374   style_change_notify (toolbar);
375   icon_size_change_notify (toolbar);
376 }
377
378 static void
379 gtk_toolbar_hierarchy_changed (GtkWidget *widget,
380                                GtkWidget *previous_toplevel)
381 {
382   GtkWidget *toplevel;
383   
384   if (previous_toplevel)
385     g_signal_handlers_disconnect_by_func (previous_toplevel,
386                                           toolbar_screen_changed,
387                                           widget);
388
389   toplevel = gtk_widget_get_toplevel (widget);
390   if (GTK_WIDGET_TOPLEVEL (toplevel))
391     g_signal_connect_swapped (toplevel,
392                               "notify::screen",
393                               G_CALLBACK (toolbar_screen_changed),
394                               widget);
395   
396   toolbar_screen_changed (GTK_TOOLBAR (widget));
397 }
398
399 static void
400 gtk_toolbar_init (GtkToolbar *toolbar)
401 {
402   GTK_WIDGET_SET_FLAGS (toolbar, GTK_NO_WINDOW);
403   GTK_WIDGET_UNSET_FLAGS (toolbar, GTK_CAN_FOCUS);
404
405   toolbar->num_children = 0;
406   toolbar->children     = NULL;
407   toolbar->orientation  = GTK_ORIENTATION_HORIZONTAL;
408   toolbar->icon_size    = DEFAULT_ICON_SIZE;
409   toolbar->style        = DEFAULT_TOOLBAR_STYLE;
410   toolbar->tooltips     = gtk_tooltips_new ();
411   g_object_ref (toolbar->tooltips);
412   gtk_object_sink (GTK_OBJECT (toolbar->tooltips));
413   
414   toolbar->button_maxw  = 0;
415   toolbar->button_maxh  = 0;
416
417   toolbar->style_set = FALSE;
418   toolbar->icon_size_set = FALSE;
419 }
420
421 static void
422 gtk_toolbar_set_property (GObject      *object,
423                           guint         prop_id,
424                           const GValue *value,
425                           GParamSpec   *pspec)
426 {
427   GtkToolbar *toolbar = GTK_TOOLBAR (object);
428   
429   switch (prop_id)
430     {
431     case PROP_ORIENTATION:
432       gtk_toolbar_set_orientation (toolbar, g_value_get_enum (value));
433       break;
434     case PROP_TOOLBAR_STYLE:
435       gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
436       break;
437     }
438 }
439
440 static void
441 gtk_toolbar_get_property (GObject      *object,
442                           guint         prop_id,
443                           GValue       *value,
444                           GParamSpec   *pspec)
445 {
446   GtkToolbar *toolbar = GTK_TOOLBAR (object);
447
448   switch (prop_id)
449     {
450     case PROP_ORIENTATION:
451       g_value_set_enum (value, toolbar->orientation);
452       break;
453     case PROP_TOOLBAR_STYLE:
454       g_value_set_enum (value, toolbar->style);
455       break;
456     default:
457       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458       break;
459     }
460 }
461
462 GtkWidget*
463 gtk_toolbar_new (void)
464 {
465   GtkToolbar *toolbar;
466
467   toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
468
469   return GTK_WIDGET (toolbar);
470 }
471
472 static void
473 gtk_toolbar_destroy (GtkObject *object)
474 {
475   GtkToolbar *toolbar;
476   GList *children;
477
478   g_return_if_fail (GTK_IS_TOOLBAR (object));
479
480   toolbar = GTK_TOOLBAR (object);
481
482   if (toolbar->tooltips)
483     {
484       g_object_unref (toolbar->tooltips);
485       toolbar->tooltips = NULL;
486     }
487
488   for (children = toolbar->children; children; children = children->next)
489     {
490       GtkToolbarChild *child;
491
492       child = children->data;
493
494       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
495         {
496           g_object_ref (child->widget);
497           gtk_widget_unparent (child->widget);
498           gtk_widget_destroy (child->widget);
499           g_object_unref (child->widget);
500         }
501
502       g_free (child);
503     }
504   g_list_free (toolbar->children);
505   toolbar->children = NULL;
506   
507   GTK_OBJECT_CLASS (parent_class)->destroy (object);
508 }
509
510 static void
511 gtk_toolbar_paint_space_line (GtkWidget       *widget,
512                               GdkRectangle    *area,
513                               GtkToolbarChild *child)
514 {
515   GtkToolbar *toolbar;
516   GtkToolbarChildSpace *child_space;
517   gint space_size;
518   
519   g_return_if_fail (GTK_IS_TOOLBAR (widget));
520   g_return_if_fail (child != NULL);
521   g_return_if_fail (child->type == GTK_TOOLBAR_CHILD_SPACE);
522
523   toolbar = GTK_TOOLBAR (widget);
524
525   child_space = (GtkToolbarChildSpace *) child;
526   space_size = get_space_size (toolbar);
527   
528   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
529     gtk_paint_vline (widget->style, widget->window,
530                      GTK_WIDGET_STATE (widget), area, widget,
531                      "toolbar",
532                      child_space->alloc_y + toolbar->button_maxh *
533                      SPACE_LINE_START / SPACE_LINE_DIVISION,
534                      child_space->alloc_y + toolbar->button_maxh *
535                      SPACE_LINE_END / SPACE_LINE_DIVISION,
536                      child_space->alloc_x +
537                      (space_size -
538                       widget->style->xthickness) / 2);
539   else
540     gtk_paint_hline (widget->style, widget->window,
541                      GTK_WIDGET_STATE (widget), area, widget,
542                      "toolbar",
543                      child_space->alloc_x + toolbar->button_maxw *
544                      SPACE_LINE_START / SPACE_LINE_DIVISION,
545                      child_space->alloc_x + toolbar->button_maxw *
546                      SPACE_LINE_END / SPACE_LINE_DIVISION,
547                      child_space->alloc_y +
548                      (space_size -
549                       widget->style->ythickness) / 2);
550 }
551
552 static gint
553 gtk_toolbar_expose (GtkWidget      *widget,
554                     GdkEventExpose *event)
555 {
556   GtkToolbar *toolbar;
557   GList *children;
558   GtkToolbarChild *child;
559   gint border_width;
560   
561   g_return_val_if_fail (GTK_IS_TOOLBAR (widget), FALSE);
562   g_return_val_if_fail (event != NULL, FALSE);
563
564   border_width = GTK_CONTAINER (widget)->border_width;
565   
566   if (GTK_WIDGET_DRAWABLE (widget))
567     {
568       GtkShadowType shadow_type;
569
570       toolbar = GTK_TOOLBAR (widget);
571
572       gtk_widget_style_get (widget, "shadow_type", &shadow_type, NULL);
573       
574       gtk_paint_box (widget->style,
575                      widget->window,
576                      GTK_WIDGET_STATE (widget),
577                      shadow_type,
578                      &event->area, widget, "toolbar",
579                      widget->allocation.x + border_width,
580                      widget->allocation.y + border_width,
581                      widget->allocation.width - border_width,
582                      widget->allocation.height - border_width);
583       
584       for (children = toolbar->children; children; children = children->next)
585         {
586           child = children->data;
587
588           if (child->type == GTK_TOOLBAR_CHILD_SPACE)
589             {
590               if (get_space_style (toolbar) == GTK_TOOLBAR_SPACE_LINE)
591                 gtk_toolbar_paint_space_line (widget, &event->area, child);
592             }
593           else
594             gtk_container_propagate_expose (GTK_CONTAINER (widget),
595                                             child->widget,
596                                             event);
597         }
598     }
599
600   return FALSE;
601 }
602
603 static void
604 gtk_toolbar_size_request (GtkWidget      *widget,
605                           GtkRequisition *requisition)
606 {
607   GtkToolbar *toolbar;
608   GList *children;
609   GtkToolbarChild *child;
610   gint nbuttons;
611   gint button_maxw, button_maxh;
612   gint widget_maxw, widget_maxh;
613   GtkRequisition child_requisition;
614   gint space_size;
615   gint ipadding;
616   
617   g_return_if_fail (GTK_IS_TOOLBAR (widget));
618   g_return_if_fail (requisition != NULL);
619
620   toolbar = GTK_TOOLBAR (widget);
621
622   requisition->width = GTK_CONTAINER (toolbar)->border_width * 2;
623   requisition->height = GTK_CONTAINER (toolbar)->border_width * 2;
624   nbuttons = 0;
625   button_maxw = 0;
626   button_maxh = 0;
627   widget_maxw = 0;
628   widget_maxh = 0;
629
630   space_size = get_space_size (toolbar);
631   
632   for (children = toolbar->children; children; children = children->next)
633     {
634       child = children->data;
635
636       switch (child->type)
637         {
638         case GTK_TOOLBAR_CHILD_SPACE:
639           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
640             requisition->width += space_size;
641           else
642             requisition->height += space_size;
643
644           break;
645
646         case GTK_TOOLBAR_CHILD_BUTTON:
647         case GTK_TOOLBAR_CHILD_RADIOBUTTON:
648         case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
649           if (GTK_WIDGET_VISIBLE (child->widget))
650             {              
651               gtk_widget_size_request (child->widget, &child_requisition);
652
653               nbuttons++;
654               button_maxw = MAX (button_maxw, child_requisition.width);
655               button_maxh = MAX (button_maxh, child_requisition.height);
656             }
657
658           break;
659
660         case GTK_TOOLBAR_CHILD_WIDGET:
661           if (GTK_WIDGET_VISIBLE (child->widget))
662             {
663               gtk_widget_size_request (child->widget, &child_requisition);
664
665               widget_maxw = MAX (widget_maxw, child_requisition.width);
666               widget_maxh = MAX (widget_maxh, child_requisition.height);
667
668               if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
669                 requisition->width += child_requisition.width;
670               else
671                 requisition->height += child_requisition.height;
672             }
673
674           break;
675
676         default:
677           g_assert_not_reached ();
678         }
679     }
680
681   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
682     {
683       requisition->width += nbuttons * button_maxw;
684       requisition->height += MAX (button_maxh, widget_maxh);
685     }
686   else
687     {
688       requisition->width += MAX (button_maxw, widget_maxw);
689       requisition->height += nbuttons * button_maxh;
690     }
691
692   /* Extra spacing */
693   gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
694   
695   requisition->width += 2 * ipadding;
696   requisition->height += 2 * ipadding;
697   
698   toolbar->button_maxw = button_maxw;
699   toolbar->button_maxh = button_maxh;
700 }
701
702 static void
703 gtk_toolbar_size_allocate (GtkWidget     *widget,
704                            GtkAllocation *allocation)
705 {
706   GtkToolbar *toolbar;
707   GList *children;
708   GtkToolbarChild *child;
709   GtkToolbarChildSpace *child_space;
710   GtkAllocation alloc;
711   GtkRequisition child_requisition;
712   gint x_border_width, y_border_width;
713   gint space_size;
714   gint ipadding;
715   
716   g_return_if_fail (GTK_IS_TOOLBAR (widget));
717   g_return_if_fail (allocation != NULL);
718
719   toolbar = GTK_TOOLBAR (widget);
720   widget->allocation = *allocation;
721   
722   x_border_width = GTK_CONTAINER (toolbar)->border_width;
723   y_border_width = GTK_CONTAINER (toolbar)->border_width;
724
725   gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
726   
727   x_border_width += ipadding;
728   y_border_width += ipadding;
729   
730   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
731     alloc.x = allocation->x + x_border_width;
732   else
733     alloc.y = allocation->y + y_border_width;
734
735   space_size = get_space_size (toolbar);
736   
737   for (children = toolbar->children; children; children = children->next)
738     {
739       child = children->data;
740
741       switch (child->type)
742         {
743         case GTK_TOOLBAR_CHILD_SPACE:
744
745           child_space = (GtkToolbarChildSpace *) child;
746
747           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
748             {
749               child_space->alloc_x = alloc.x;
750               child_space->alloc_y = allocation->y + (allocation->height - toolbar->button_maxh) / 2;
751               alloc.x += space_size;
752             }
753           else
754             {
755               child_space->alloc_x = allocation->x + (allocation->width - toolbar->button_maxw) / 2;
756               child_space->alloc_y = alloc.y;
757               alloc.y += space_size;
758             }
759
760           break;
761
762         case GTK_TOOLBAR_CHILD_BUTTON:
763         case GTK_TOOLBAR_CHILD_RADIOBUTTON:
764         case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
765           if (!GTK_WIDGET_VISIBLE (child->widget))
766             break;
767
768           alloc.width = toolbar->button_maxw;
769           alloc.height = toolbar->button_maxh;
770
771           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
772             alloc.y = allocation->y + (allocation->height - toolbar->button_maxh) / 2;
773           else
774             alloc.x = allocation->x + (allocation->width - toolbar->button_maxw) / 2;
775
776           gtk_widget_size_allocate (child->widget, &alloc);
777
778           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
779             alloc.x += toolbar->button_maxw;
780           else
781             alloc.y += toolbar->button_maxh;
782
783           break;
784
785         case GTK_TOOLBAR_CHILD_WIDGET:
786           if (!GTK_WIDGET_VISIBLE (child->widget))
787             break;
788
789           gtk_widget_get_child_requisition (child->widget, &child_requisition);
790           
791           alloc.width = child_requisition.width;
792           alloc.height = child_requisition.height;
793
794           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
795             alloc.y = allocation->y + (allocation->height - child_requisition.height) / 2;
796           else
797             alloc.x = allocation->x + (allocation->width - child_requisition.width) / 2;
798
799           gtk_widget_size_allocate (child->widget, &alloc);
800
801           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
802             alloc.x += child_requisition.width;
803           else
804             alloc.y += child_requisition.height;
805
806           break;
807
808         default:
809           g_assert_not_reached ();
810         }
811     }
812 }
813
814 static void
815 gtk_toolbar_style_set (GtkWidget  *widget,
816                        GtkStyle   *prev_style)
817 {
818   if (prev_style)
819     gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
820 }
821
822 static gboolean
823 gtk_toolbar_focus (GtkWidget       *widget,
824                    GtkDirectionType dir)
825 {
826   /* Focus can't go in toolbars */
827   
828   return FALSE;
829 }
830
831 static void
832 child_show_all (GtkWidget *widget)
833 {
834   /* Don't show our own children, since that would
835    * show labels we may intend to hide in icons-only mode
836    */
837   if (!g_object_get_data (G_OBJECT (widget),
838                           "gtk-toolbar-is-child"))
839     gtk_widget_show_all (widget);
840 }
841
842 static void
843 gtk_toolbar_show_all (GtkWidget *widget)
844 {
845   gtk_container_foreach (GTK_CONTAINER (widget),
846                          (GtkCallback) child_show_all,
847                          NULL);
848   gtk_widget_show (widget);
849 }
850
851 static void
852 gtk_toolbar_add (GtkContainer *container,
853                  GtkWidget    *widget)
854 {
855   g_return_if_fail (GTK_IS_TOOLBAR (container));
856   g_return_if_fail (widget != NULL);
857
858   gtk_toolbar_append_widget (GTK_TOOLBAR (container), widget, NULL, NULL);
859 }
860
861 static void
862 gtk_toolbar_remove (GtkContainer *container,
863                     GtkWidget    *widget)
864 {
865   GtkToolbar *toolbar;
866   GList *children;
867   GtkToolbarChild *child;
868
869   g_return_if_fail (GTK_IS_TOOLBAR (container));
870   g_return_if_fail (widget != NULL);
871
872   toolbar = GTK_TOOLBAR (container);
873
874   for (children = toolbar->children; children; children = children->next)
875     {
876       child = children->data;
877
878       if ((child->type != GTK_TOOLBAR_CHILD_SPACE) && (child->widget == widget))
879         {
880           gboolean was_visible;
881
882           was_visible = GTK_WIDGET_VISIBLE (widget);
883           gtk_widget_unparent (widget);
884
885           toolbar->children = g_list_remove_link (toolbar->children, children);
886           g_free (child);
887           g_list_free (children);
888           toolbar->num_children--;
889
890           if (was_visible && GTK_WIDGET_VISIBLE (container))
891             gtk_widget_queue_resize (GTK_WIDGET (container));
892
893           break;
894         }
895     }
896 }
897
898 static void
899 gtk_toolbar_forall (GtkContainer *container,
900                     gboolean      include_internals,
901                     GtkCallback   callback,
902                     gpointer      callback_data)
903 {
904   GtkToolbar *toolbar;
905   GList *children;
906   GtkToolbarChild *child;
907
908   g_return_if_fail (GTK_IS_TOOLBAR (container));
909   g_return_if_fail (callback != NULL);
910
911   toolbar = GTK_TOOLBAR (container);
912
913   for (children = toolbar->children; children; children = children->next)
914     {
915       child = children->data;
916
917       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
918         (*callback) (child->widget, callback_data);
919     }
920 }
921
922 GtkWidget *
923 gtk_toolbar_append_item (GtkToolbar    *toolbar,
924                          const char    *text,
925                          const char    *tooltip_text,
926                          const char    *tooltip_private_text,
927                          GtkWidget     *icon,
928                          GtkSignalFunc  callback,
929                          gpointer       user_data)
930 {
931   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
932                                      NULL, text,
933                                      tooltip_text, tooltip_private_text,
934                                      icon, callback, user_data,
935                                      toolbar->num_children);
936 }
937
938 GtkWidget *
939 gtk_toolbar_prepend_item (GtkToolbar    *toolbar,
940                           const char    *text,
941                           const char    *tooltip_text,
942                           const char    *tooltip_private_text,
943                           GtkWidget     *icon,
944                           GtkSignalFunc  callback,
945                           gpointer       user_data)
946 {
947   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
948                                      NULL, text,
949                                      tooltip_text, tooltip_private_text,
950                                      icon, callback, user_data,
951                                      0);
952 }
953
954 static GtkWidget *
955 gtk_toolbar_internal_insert_item (GtkToolbar    *toolbar,
956                                   const char    *text,
957                                   const char    *tooltip_text,
958                                   const char    *tooltip_private_text,
959                                   GtkWidget     *icon,
960                                   GtkSignalFunc  callback,
961                                   gpointer       user_data,
962                                   gint           position)
963 {
964   return gtk_toolbar_internal_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
965                                               NULL, text,
966                                               tooltip_text, tooltip_private_text,
967                                               icon, callback, user_data,
968                                               position);
969 }
970      
971 GtkWidget *
972 gtk_toolbar_insert_item (GtkToolbar    *toolbar,
973                          const char    *text,
974                          const char    *tooltip_text,
975                          const char    *tooltip_private_text,
976                          GtkWidget     *icon,
977                          GtkSignalFunc  callback,
978                          gpointer       user_data,
979                          gint           position)
980 {
981   return gtk_toolbar_internal_insert_item (toolbar, 
982                                            text, tooltip_text, tooltip_private_text,
983                                            icon, callback, user_data,
984                                            position);
985 }
986
987 /**
988  * gtk_toolbar_set_icon_size:
989  * @toolbar: A #GtkToolbar
990  * @icon_size: The #GtkIconSize that stock icons in the toolbar shall have.
991  *
992  * This function sets the size of stock icons in the toolbar. You
993  * can call it both before you add the icons and after they've been
994  * added. The size you set will override user preferences for the default
995  * icon size.
996  **/
997 void
998 gtk_toolbar_set_icon_size (GtkToolbar  *toolbar,
999                            GtkIconSize  icon_size)
1000 {
1001   GList *children;
1002   GtkToolbarChild *child;
1003   GtkImage *image;
1004   gchar *stock_id;
1005
1006   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1007
1008   toolbar->icon_size_set = TRUE;
1009   
1010   if (toolbar->icon_size == icon_size)
1011     return;
1012   
1013   toolbar->icon_size = icon_size;
1014
1015   for (children = toolbar->children; children; children = children->next)
1016     {
1017       child = children->data;
1018       if ((child->type == GTK_TOOLBAR_CHILD_BUTTON ||
1019            child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON ||
1020            child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON) &&
1021           GTK_IS_IMAGE (child->icon))
1022         {
1023           image = GTK_IMAGE (child->icon);
1024           if (gtk_image_get_storage_type (image) == GTK_IMAGE_STOCK)
1025             {
1026               gtk_image_get_stock (image, &stock_id, NULL);
1027               stock_id = g_strdup (stock_id);
1028               gtk_image_set_from_stock (image,
1029                                         stock_id,
1030                                         icon_size);
1031               g_free (stock_id);
1032             }
1033         }
1034     }
1035   
1036   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1037 }
1038
1039 /**
1040  * gtk_toolbar_get_icon_size:
1041  * @toolbar: a #GtkToolbar
1042  *
1043  * Retrieves the icon size fo the toolbar. See gtk_toolbar_set_icon_size().
1044  *
1045  * Return value: the current icon size for the icons on the toolbar.
1046  **/
1047 GtkIconSize
1048 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
1049 {
1050   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
1051
1052   return toolbar->icon_size;
1053 }
1054
1055 /**
1056  * gtk_toolbar_unset_icon_size:
1057  * @toolbar: a #GtkToolbar
1058  * 
1059  * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
1060  * user preferences will be used to determine the icon size.
1061  **/
1062 void
1063 gtk_toolbar_unset_icon_size (GtkToolbar  *toolbar)
1064 {
1065   GtkIconSize size;
1066
1067   if (toolbar->icon_size_set)
1068     {
1069       GtkSettings *settings = toolbar_get_settings (toolbar);
1070
1071       if (settings)
1072         g_object_get (settings,
1073                       "gtk-toolbar-icon-size", &size,
1074                       NULL);
1075       else
1076         size = DEFAULT_ICON_SIZE;
1077
1078       if (size != toolbar->icon_size)
1079         gtk_toolbar_set_icon_size (toolbar, size);
1080
1081       toolbar->icon_size_set = FALSE;
1082     }
1083 }
1084
1085 static gchar *
1086 elide_underscores (const gchar *original)
1087 {
1088   gchar *q, *result;
1089   const gchar *p;
1090   gboolean last_underscore;
1091
1092   q = result = g_malloc (strlen (original + 1));
1093   last_underscore = FALSE;
1094   
1095   for (p = original; *p; p++)
1096     {
1097       if (!last_underscore && *p == '_')
1098         last_underscore = TRUE;
1099       else
1100         {
1101           last_underscore = FALSE;
1102           *q++ = *p;
1103         }
1104     }
1105   
1106   *q = '\0';
1107   
1108   return result;
1109 }
1110
1111 /**
1112  * gtk_toolbar_insert_stock:
1113  * @toolbar: A #GtkToolbar
1114  * @stock_id: The id of the stock item you want to insert
1115  * @tooltip_text: The text in the tooltip of the toolbar button
1116  * @tooltip_private_text: The private text of the tooltip
1117  * @callback: The callback called when the toolbar button is clicked.
1118  * @user_data: user data passed to callback
1119  * @position: The position the button shall be inserted at.
1120  *            -1 means at the end.
1121  *
1122  * Inserts a stock item at the specified position of the toolbar.  If
1123  * @stock_id is not a known stock item ID, it's inserted verbatim,
1124  * except that underscores used to mark mnemonics are removed.
1125  *
1126  * Returns: the inserted widget
1127  */
1128 GtkWidget*
1129 gtk_toolbar_insert_stock (GtkToolbar      *toolbar,
1130                           const gchar     *stock_id,
1131                           const char      *tooltip_text,
1132                           const char      *tooltip_private_text,
1133                           GtkSignalFunc    callback,
1134                           gpointer         user_data,
1135                           gint             position)
1136 {
1137   GtkStockItem item;
1138   GtkWidget *image = NULL;
1139   const gchar *label;
1140   gchar *label_no_mnemonic;
1141
1142   if (gtk_stock_lookup (stock_id, &item))
1143     {
1144       image = gtk_image_new_from_stock (stock_id, toolbar->icon_size);
1145       label = item.label;
1146     }
1147   else
1148     label = stock_id;
1149
1150   label_no_mnemonic = elide_underscores (label);
1151   
1152   return gtk_toolbar_internal_insert_item (toolbar,
1153                                            label_no_mnemonic,
1154                                            tooltip_text,
1155                                            tooltip_private_text,
1156                                            image,
1157                                            callback,
1158                                            user_data,
1159                                            position);
1160
1161   g_free (label_no_mnemonic);
1162 }
1163
1164
1165
1166 void
1167 gtk_toolbar_append_space (GtkToolbar *toolbar)
1168 {
1169   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1170                               NULL, NULL,
1171                               NULL, NULL,
1172                               NULL, NULL, NULL,
1173                               toolbar->num_children);
1174 }
1175
1176 void
1177 gtk_toolbar_prepend_space (GtkToolbar *toolbar)
1178 {
1179   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1180                               NULL, NULL,
1181                               NULL, NULL,
1182                               NULL, NULL, NULL,
1183                               0);
1184 }
1185
1186 void
1187 gtk_toolbar_insert_space (GtkToolbar *toolbar,
1188                           gint        position)
1189 {
1190   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1191                               NULL, NULL,
1192                               NULL, NULL,
1193                               NULL, NULL, NULL,
1194                               position);
1195 }
1196
1197 /**
1198  * gtk_toolbar_remove_space:
1199  * @toolbar: a #GtkToolbar.
1200  * @position: the index of the space to remove.
1201  * 
1202  * Removes a space from the specified position.
1203  **/
1204 void
1205 gtk_toolbar_remove_space (GtkToolbar *toolbar,
1206                           gint        position)
1207 {
1208   GList *children;
1209   GtkToolbarChild *child;
1210   gint i;
1211   
1212   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1213   
1214   i = 0;
1215   for (children = toolbar->children; children; children = children->next)
1216     {
1217       child = children->data;
1218
1219       if (i == position)
1220         {
1221           if (child->type == GTK_TOOLBAR_CHILD_SPACE)
1222             {
1223               toolbar->children = g_list_remove_link (toolbar->children, children);
1224               g_free (child);
1225               g_list_free (children);
1226               toolbar->num_children--;
1227               
1228               gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1229             }
1230           else
1231             {
1232               g_warning ("Toolbar position %d is not a space", position);
1233             }
1234
1235           return;
1236         }
1237
1238       ++i;
1239     }
1240
1241   g_warning ("Toolbar position %d doesn't exist", position);
1242 }
1243
1244 /**
1245  * gtk_toolbar_append_widget:
1246  * @toolbar: a #GtkToolbar.
1247  * @widget: a #GtkWidget to add to the toolbar. 
1248  * @tooltip_text: the element's tooltip.
1249  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
1250  * 
1251  * Adds a widget to the end of the given toolbar.
1252  **/ 
1253 void
1254 gtk_toolbar_append_widget (GtkToolbar  *toolbar,
1255                            GtkWidget   *widget,
1256                            const gchar *tooltip_text,
1257                            const gchar *tooltip_private_text)
1258 {
1259   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1260                               widget, NULL,
1261                               tooltip_text, tooltip_private_text,
1262                               NULL, NULL, NULL,
1263                               toolbar->num_children);
1264 }
1265
1266 /**
1267  * gtk_toolbar_prepend_widget:
1268  * @toolbar: a #GtkToolbar.
1269  * @widget: a #GtkWidget to add to the toolbar. 
1270  * @tooltip_text: the element's tooltip.
1271  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
1272  * 
1273  * Adds a widget to the beginning of the given toolbar.
1274  **/ 
1275 void
1276 gtk_toolbar_prepend_widget (GtkToolbar  *toolbar,
1277                             GtkWidget   *widget,
1278                             const gchar *tooltip_text,
1279                             const gchar *tooltip_private_text)
1280 {
1281   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1282                               widget, NULL,
1283                               tooltip_text, tooltip_private_text,
1284                               NULL, NULL, NULL,
1285                               0);
1286 }
1287
1288 /**
1289  * gtk_toolbar_insert_widget:
1290  * @toolbar: a #GtkToolbar.
1291  * @widget: a #GtkWidget to add to the toolbar. 
1292  * @tooltip_text: the element's tooltip.
1293  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
1294  * @position: the number of widgets to insert this widget after.
1295  * 
1296  * Inserts a widget in the toolbar at the given position.
1297  **/ 
1298 void
1299 gtk_toolbar_insert_widget (GtkToolbar *toolbar,
1300                            GtkWidget  *widget,
1301                            const char *tooltip_text,
1302                            const char *tooltip_private_text,
1303                            gint        position)
1304 {
1305   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1306                               widget, NULL,
1307                               tooltip_text, tooltip_private_text,
1308                               NULL, NULL, NULL,
1309                               position);
1310 }
1311
1312 GtkWidget*
1313 gtk_toolbar_append_element (GtkToolbar          *toolbar,
1314                             GtkToolbarChildType  type,
1315                             GtkWidget           *widget,
1316                             const char          *text,
1317                             const char          *tooltip_text,
1318                             const char          *tooltip_private_text,
1319                             GtkWidget           *icon,
1320                             GtkSignalFunc        callback,
1321                             gpointer             user_data)
1322 {
1323   return gtk_toolbar_insert_element (toolbar, type, widget, text,
1324                                      tooltip_text, tooltip_private_text,
1325                                      icon, callback, user_data,
1326                                      toolbar->num_children);
1327 }
1328
1329 GtkWidget *
1330 gtk_toolbar_prepend_element (GtkToolbar          *toolbar,
1331                              GtkToolbarChildType  type,
1332                              GtkWidget           *widget,
1333                              const char          *text,
1334                              const char          *tooltip_text,
1335                              const char          *tooltip_private_text,
1336                              GtkWidget           *icon,
1337                              GtkSignalFunc        callback,
1338                              gpointer             user_data)
1339 {
1340   return gtk_toolbar_insert_element (toolbar, type, widget, text,
1341                                      tooltip_text, tooltip_private_text,
1342                                      icon, callback, user_data, 0);
1343 }
1344
1345 /**
1346  * gtk_toolbar_insert_element:
1347  * @toolbar: a #GtkToolbar.
1348  * @type: a value of type #GtkToolbarChildType that determines what @widget
1349  *   will be.
1350  * @widget: a #GtkWidget, or %NULL. 
1351  * @text: the element's label.
1352  * @tooltip_text: the element's tooltip.
1353  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
1354  * @icon: a #GtkWidget that provides pictorial representation of the element's function.
1355  * @callback: the function to be executed when the button is pressed.
1356  * @user_data: any data you wish to pass to the callback.
1357  * @position: the number of widgets to insert this element after.
1358  *
1359  * Inserts a new element in the toolbar at the given position. 
1360  *
1361  * If @type == %GTK_TOOLBAR_CHILD_WIDGET, @widget is used as the new element.
1362  * If @type == %GTK_TOOLBAR_CHILD_RADIOBUTTON, @widget is used to determine
1363  * the radio group for the new element. In all other cases, @widget must
1364  * be %NULL.
1365  *
1366  * Return value: the new toolbar element as a #GtkWidget.
1367  **/
1368 GtkWidget *
1369 gtk_toolbar_insert_element (GtkToolbar          *toolbar,
1370                             GtkToolbarChildType  type,
1371                             GtkWidget           *widget,
1372                             const char          *text,
1373                             const char          *tooltip_text,
1374                             const char          *tooltip_private_text,
1375                             GtkWidget           *icon,
1376                             GtkSignalFunc        callback,
1377                             gpointer             user_data,
1378                             gint                 position)
1379 {
1380   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
1381   if (type == GTK_TOOLBAR_CHILD_WIDGET)
1382     {
1383       g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1384     }
1385   else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
1386     g_return_val_if_fail (widget == NULL, NULL);
1387   
1388   return gtk_toolbar_internal_insert_element (toolbar, type, widget, text,
1389                                               tooltip_text, tooltip_private_text,
1390                                               icon, callback, user_data,
1391                                               position);
1392 }
1393
1394 static GtkWidget *
1395 gtk_toolbar_internal_insert_element (GtkToolbar          *toolbar,
1396                                      GtkToolbarChildType  type,
1397                                      GtkWidget           *widget,
1398                                      const char          *text,
1399                                      const char          *tooltip_text,
1400                                      const char          *tooltip_private_text,
1401                                      GtkWidget           *icon,
1402                                      GtkSignalFunc        callback,
1403                                      gpointer             user_data,
1404                                      gint                 position)
1405 {
1406   GtkToolbarChild *child;
1407   GtkWidget *box;
1408
1409   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
1410   if (type == GTK_TOOLBAR_CHILD_WIDGET)
1411     {
1412       g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1413     }
1414   else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
1415     g_return_val_if_fail (widget == NULL, NULL);
1416
1417   if (type == GTK_TOOLBAR_CHILD_SPACE)
1418     child = (GtkToolbarChild *) g_new (GtkToolbarChildSpace, 1);
1419   else
1420     child = g_new (GtkToolbarChild, 1);
1421
1422   child->type = type;
1423   child->icon = NULL;
1424   child->label = NULL;
1425
1426   switch (type)
1427     {
1428     case GTK_TOOLBAR_CHILD_SPACE:
1429       child->widget = NULL;
1430       ((GtkToolbarChildSpace *) child)->alloc_x =
1431         ((GtkToolbarChildSpace *) child)->alloc_y = 0;
1432       break;
1433
1434     case GTK_TOOLBAR_CHILD_WIDGET:
1435       child->widget = widget;
1436       break;
1437
1438     case GTK_TOOLBAR_CHILD_BUTTON:
1439     case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
1440     case GTK_TOOLBAR_CHILD_RADIOBUTTON:
1441       if (type == GTK_TOOLBAR_CHILD_BUTTON)
1442         {
1443           child->widget = gtk_button_new ();
1444           gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1445         }
1446       else if (type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1447         {
1448           child->widget = gtk_toggle_button_new ();
1449           gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1450           gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child->widget),
1451                                       FALSE);
1452         }
1453       else
1454         {
1455           child->widget = gtk_radio_button_new (widget
1456                                                 ? gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget))
1457                                                 : NULL);
1458           gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1459           gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child->widget), FALSE);
1460         }
1461
1462       GTK_WIDGET_UNSET_FLAGS (child->widget, GTK_CAN_FOCUS);
1463
1464       if (callback)
1465         g_signal_connect (child->widget, "clicked",
1466                           callback, user_data);
1467
1468       if (toolbar->style == GTK_TOOLBAR_BOTH_HORIZ)
1469           box = gtk_hbox_new (FALSE, 0);
1470       else
1471           box = gtk_vbox_new (FALSE, 0);
1472       gtk_container_add (GTK_CONTAINER (child->widget), box);
1473       gtk_widget_show (box);
1474
1475       if (text)
1476         {
1477           child->label = gtk_label_new (text);
1478           gtk_box_pack_end (GTK_BOX (box), child->label, FALSE, FALSE, 0);
1479           if (toolbar->style != GTK_TOOLBAR_ICONS)
1480             gtk_widget_show (child->label);
1481         }
1482
1483       if (icon)
1484         {
1485           child->icon = GTK_WIDGET (icon);
1486           gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1487           if (toolbar->style != GTK_TOOLBAR_TEXT)
1488             gtk_widget_show (child->icon);
1489         }
1490
1491       if (type != GTK_TOOLBAR_CHILD_WIDGET)
1492         {
1493           /* Mark child as ours */
1494           g_object_set_data (G_OBJECT (child->widget),
1495                              "gtk-toolbar-is-child",
1496                              GINT_TO_POINTER (TRUE));
1497         }
1498       gtk_widget_show (child->widget);
1499       break;
1500
1501     default:
1502       g_assert_not_reached ();
1503     }
1504
1505   if ((type != GTK_TOOLBAR_CHILD_SPACE) && tooltip_text)
1506     gtk_tooltips_set_tip (toolbar->tooltips, child->widget,
1507                           tooltip_text, tooltip_private_text);
1508
1509   toolbar->children = g_list_insert (toolbar->children, child, position);
1510   toolbar->num_children++;
1511
1512   if (type != GTK_TOOLBAR_CHILD_SPACE)
1513     gtk_widget_set_parent (child->widget, GTK_WIDGET (toolbar));
1514   else
1515     gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1516
1517   return child->widget;
1518 }
1519
1520 void
1521 gtk_toolbar_set_orientation (GtkToolbar     *toolbar,
1522                              GtkOrientation  orientation)
1523 {
1524   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1525
1526   g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0, orientation);
1527 }
1528
1529 /**
1530  * gtk_toolbar_get_orientation:
1531  * @toolbar: a #GtkToolbar
1532  * 
1533  * Retrieves the current orientation of the toolbar. See
1534  * gtk_toolbar_set_orientation().
1535  *
1536  * Return value: the orientation
1537  **/
1538 GtkOrientation
1539 gtk_toolbar_get_orientation (GtkToolbar *toolbar)
1540 {
1541   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
1542
1543   return toolbar->orientation;
1544 }
1545
1546 void
1547 gtk_toolbar_set_style (GtkToolbar      *toolbar,
1548                        GtkToolbarStyle  style)
1549 {
1550   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1551
1552   toolbar->style_set = TRUE;
1553   g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
1554 }
1555
1556 /**
1557  * gtk_toolbar_get_style:
1558  * @toolbar: a #GtkToolbar
1559  *
1560  * Retrieves whether the toolbar has text, icons, or both . See
1561  * gtk_toolbar_set_style().
1562  
1563  * Return value: the current style of @toolbar
1564  **/
1565 GtkToolbarStyle
1566 gtk_toolbar_get_style (GtkToolbar *toolbar)
1567 {
1568   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
1569
1570   return toolbar->style;
1571 }
1572
1573 /**
1574  * gtk_toolbar_unset_style:
1575  * @toolbar: a #GtkToolbar
1576  * 
1577  * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
1578  * user preferences will be used to determine the toolbar style.
1579  **/
1580 void
1581 gtk_toolbar_unset_style (GtkToolbar *toolbar)
1582 {
1583   GtkToolbarStyle style;
1584   
1585   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1586
1587   if (toolbar->style_set)
1588     {
1589       GtkSettings *settings = toolbar_get_settings (toolbar);
1590
1591       if (settings)
1592         g_object_get (settings,
1593                       "gtk-toolbar-style", &style,
1594                       NULL);
1595       else
1596         style = DEFAULT_TOOLBAR_STYLE;
1597
1598       if (style != toolbar->style)
1599         g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
1600       
1601       toolbar->style_set = FALSE;
1602     }
1603 }
1604
1605 void
1606 gtk_toolbar_set_tooltips (GtkToolbar *toolbar,
1607                           gboolean    enable)
1608 {
1609   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1610
1611   if (enable)
1612     gtk_tooltips_enable (toolbar->tooltips);
1613   else
1614     gtk_tooltips_disable (toolbar->tooltips);
1615 }
1616
1617 /**
1618  * gtk_toolbar_get_tooltips:
1619  * @toolbar: a #GtkToolbar
1620  *
1621  * Retrieves whether tooltips are enabled. See
1622  * gtk_toolbar_set_tooltips().
1623  *
1624  * Return value: %TRUE if tooltips are enabled
1625  **/
1626 gboolean
1627 gtk_toolbar_get_tooltips (GtkToolbar *toolbar)
1628 {
1629   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1630
1631   return toolbar->tooltips->enabled;
1632 }
1633
1634 static void
1635 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1636 {
1637   GList *children;
1638   GtkToolbarChild *child;
1639   GtkReliefStyle relief;
1640   
1641   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1642
1643   relief = get_button_relief (toolbar);
1644   
1645   for (children = toolbar->children; children; children = children->next)
1646     {
1647       child = children->data;
1648       if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
1649           child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
1650           child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1651         gtk_button_set_relief (GTK_BUTTON (child->widget), relief);
1652     }
1653 }
1654
1655 static void
1656 gtk_real_toolbar_orientation_changed (GtkToolbar     *toolbar,
1657                                       GtkOrientation  orientation)
1658 {
1659   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1660
1661   if (toolbar->orientation != orientation)
1662     {
1663       toolbar->orientation = orientation;
1664       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1665       g_object_notify (G_OBJECT (toolbar), "orientation");
1666     }
1667 }
1668
1669 static GtkWidget *
1670 get_first_child (GtkContainer *container)
1671 {
1672   GList *children = gtk_container_get_children (container);
1673   GtkWidget *result = children ? children->data : NULL;
1674   g_list_free (children);
1675   
1676   return result;
1677 }
1678
1679 static void
1680 gtk_real_toolbar_style_changed (GtkToolbar      *toolbar,
1681                                 GtkToolbarStyle  style)
1682 {
1683   GList *children;
1684   GtkToolbarChild *child;
1685   GtkWidget* box = NULL;
1686   
1687   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1688
1689   if (toolbar->style != style)
1690     {
1691       toolbar->style = style;
1692
1693       for (children = toolbar->children; children; children = children->next)
1694         {
1695           child = children->data;
1696
1697           if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
1698               child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
1699               child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1700             switch (style)
1701               {
1702               case GTK_TOOLBAR_ICONS:
1703                 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1704                   gtk_widget_show (child->icon);
1705
1706                 if (child->label && GTK_WIDGET_VISIBLE (child->label))
1707                   gtk_widget_hide (child->label);
1708
1709                 break;
1710
1711               case GTK_TOOLBAR_TEXT:
1712                 if (child->icon && GTK_WIDGET_VISIBLE (child->icon))
1713                   gtk_widget_hide (child->icon);
1714                 
1715                 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1716                   gtk_widget_show (child->label);
1717
1718                 break;
1719
1720               case GTK_TOOLBAR_BOTH:
1721                 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1722                   gtk_widget_show (child->icon);
1723
1724                 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1725                   gtk_widget_show (child->label);
1726
1727                 box = get_first_child (GTK_CONTAINER (child->widget));
1728
1729                 if (GTK_IS_HBOX (box))
1730                 {
1731                     if (child->icon)
1732                     {
1733                         g_object_ref (child->icon);
1734                         gtk_container_remove (GTK_CONTAINER (box),
1735                                               child->icon);
1736                     }
1737                     if (child->label)
1738                     {
1739                         g_object_ref (child->label);
1740                         gtk_container_remove (GTK_CONTAINER (box),
1741                                               child->label);
1742                     }
1743                     gtk_container_remove (GTK_CONTAINER (child->widget),
1744                                           box);
1745                     
1746                     box = gtk_vbox_new (FALSE, 0);
1747                     gtk_widget_show (box);
1748                     
1749                     if (child->label)
1750                     {
1751                         gtk_box_pack_end (GTK_BOX (box), child->label, FALSE, FALSE, 0);
1752                         g_object_unref (child->label);
1753                     }
1754                     if (child->icon)
1755                     {
1756                         gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1757                         g_object_unref (child->icon);
1758                     }
1759                     gtk_container_add (GTK_CONTAINER (child->widget),
1760                                        box);
1761                 }
1762                 
1763                 break;
1764                 
1765               case GTK_TOOLBAR_BOTH_HORIZ:
1766                 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1767                   gtk_widget_show (child->icon);
1768                 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1769                   gtk_widget_show (child->label);
1770
1771                 box = get_first_child (GTK_CONTAINER (child->widget));
1772                 
1773                 if (GTK_IS_VBOX (box))
1774                 {
1775                     if (child->icon)
1776                     {
1777                         g_object_ref (child->icon);
1778                         gtk_container_remove (GTK_CONTAINER (box),
1779                                               child->icon);
1780                     }
1781                     if (child->label)
1782                     {
1783                         g_object_ref (child->label);
1784                         gtk_container_remove (GTK_CONTAINER (box),
1785                                               child->label);
1786                     }
1787                     gtk_container_remove (GTK_CONTAINER (child->widget),
1788                                           box);
1789
1790                     box = gtk_hbox_new (FALSE, 0);
1791                     gtk_widget_show (box);
1792                     
1793                     if (child->label)
1794                     {
1795                         gtk_box_pack_end (GTK_BOX (box), child->label, TRUE, TRUE, 0);
1796                         g_object_unref (child->label);
1797                     }
1798                     if (child->icon)
1799                     {
1800                         gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1801                         g_object_unref (child->icon);
1802                     }
1803                     gtk_container_add (GTK_CONTAINER (child->widget), box);
1804                     
1805                 }
1806                 
1807                 break;
1808
1809               default:
1810                 g_assert_not_reached ();
1811               }
1812         }
1813
1814       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1815       g_object_notify (G_OBJECT (toolbar), "toolbar_style");
1816     }
1817 }
1818
1819
1820 static GtkReliefStyle
1821 get_button_relief (GtkToolbar *toolbar)
1822 {
1823   GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
1824
1825   gtk_widget_ensure_style (GTK_WIDGET (toolbar));
1826   gtk_widget_style_get (GTK_WIDGET (toolbar),
1827                         "button_relief", &button_relief,
1828                         NULL);
1829
1830   return button_relief;
1831 }
1832
1833 static gint
1834 get_space_size (GtkToolbar *toolbar)
1835 {
1836   gint space_size = DEFAULT_SPACE_SIZE;
1837
1838   gtk_widget_style_get (GTK_WIDGET (toolbar),
1839                         "space_size", &space_size,
1840                         NULL);
1841
1842   return space_size;
1843 }
1844
1845 static GtkToolbarSpaceStyle
1846 get_space_style (GtkToolbar *toolbar)
1847 {
1848   GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE;
1849
1850   gtk_widget_style_get (GTK_WIDGET (toolbar),
1851                         "space_style", &space_style,
1852                         NULL);
1853
1854
1855   return space_style;  
1856 }