]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolbar.c
Fix nasty typo (#99653): strlen (original + 1) -> strlen (original) + 1
[~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_screen_changed           (GtkWidget       *widget,
98                                                   GdkScreen       *previous_screen);
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->screen_changed = gtk_toolbar_screen_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 gtk_toolbar_screen_changed (GtkWidget *widget,
333                             GdkScreen *previous_screen)
334 {
335   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
336   GtkSettings *old_settings = toolbar_get_settings (toolbar);
337   GtkSettings *settings;
338
339   if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
340     settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
341   else
342     settings = NULL;
343
344   if (settings == old_settings)
345     return;
346
347   if (old_settings)
348     {
349       g_signal_handler_disconnect (old_settings, toolbar->style_set_connection);
350       g_signal_handler_disconnect (old_settings, toolbar->icon_size_connection);
351
352       g_object_unref (old_settings);
353     }
354   
355   if (settings)
356     {
357       toolbar->style_set_connection =
358         g_signal_connect_swapped (settings,
359                                   "notify::gtk-toolbar-style",
360                                   G_CALLBACK (style_change_notify),
361                                   toolbar);
362       
363       toolbar->icon_size_connection =
364         g_signal_connect_swapped (settings,
365                                   "notify::gtk-toolbar-icon-size",
366                                    G_CALLBACK (icon_size_change_notify),
367                                    toolbar);
368
369
370       g_object_ref (settings);
371       g_object_set_data (G_OBJECT (toolbar), "gtk-toolbar-settings", settings);
372     }
373   else
374     g_object_set_data (G_OBJECT (toolbar), "gtk-toolbar-settings", NULL);
375
376   style_change_notify (toolbar);
377   icon_size_change_notify (toolbar);
378 }
379
380 static void
381 gtk_toolbar_init (GtkToolbar *toolbar)
382 {
383   GTK_WIDGET_SET_FLAGS (toolbar, GTK_NO_WINDOW);
384   GTK_WIDGET_UNSET_FLAGS (toolbar, GTK_CAN_FOCUS);
385
386   toolbar->num_children = 0;
387   toolbar->children     = NULL;
388   toolbar->orientation  = GTK_ORIENTATION_HORIZONTAL;
389   toolbar->icon_size    = DEFAULT_ICON_SIZE;
390   toolbar->style        = DEFAULT_TOOLBAR_STYLE;
391   toolbar->tooltips     = gtk_tooltips_new ();
392   g_object_ref (toolbar->tooltips);
393   gtk_object_sink (GTK_OBJECT (toolbar->tooltips));
394   
395   toolbar->button_maxw  = 0;
396   toolbar->button_maxh  = 0;
397
398   toolbar->style_set = FALSE;
399   toolbar->icon_size_set = FALSE;
400 }
401
402 static void
403 gtk_toolbar_set_property (GObject      *object,
404                           guint         prop_id,
405                           const GValue *value,
406                           GParamSpec   *pspec)
407 {
408   GtkToolbar *toolbar = GTK_TOOLBAR (object);
409   
410   switch (prop_id)
411     {
412     case PROP_ORIENTATION:
413       gtk_toolbar_set_orientation (toolbar, g_value_get_enum (value));
414       break;
415     case PROP_TOOLBAR_STYLE:
416       gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
417       break;
418     }
419 }
420
421 static void
422 gtk_toolbar_get_property (GObject      *object,
423                           guint         prop_id,
424                           GValue       *value,
425                           GParamSpec   *pspec)
426 {
427   GtkToolbar *toolbar = GTK_TOOLBAR (object);
428
429   switch (prop_id)
430     {
431     case PROP_ORIENTATION:
432       g_value_set_enum (value, toolbar->orientation);
433       break;
434     case PROP_TOOLBAR_STYLE:
435       g_value_set_enum (value, toolbar->style);
436       break;
437     default:
438       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
439       break;
440     }
441 }
442
443 GtkWidget*
444 gtk_toolbar_new (void)
445 {
446   GtkToolbar *toolbar;
447
448   toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
449
450   return GTK_WIDGET (toolbar);
451 }
452
453 static void
454 gtk_toolbar_destroy (GtkObject *object)
455 {
456   GtkToolbar *toolbar;
457   GList *children;
458
459   g_return_if_fail (GTK_IS_TOOLBAR (object));
460
461   toolbar = GTK_TOOLBAR (object);
462
463   if (toolbar->tooltips)
464     {
465       g_object_unref (toolbar->tooltips);
466       toolbar->tooltips = NULL;
467     }
468
469   for (children = toolbar->children; children; children = children->next)
470     {
471       GtkToolbarChild *child;
472
473       child = children->data;
474
475       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
476         {
477           g_object_ref (child->widget);
478           gtk_widget_unparent (child->widget);
479           gtk_widget_destroy (child->widget);
480           g_object_unref (child->widget);
481         }
482
483       g_free (child);
484     }
485   g_list_free (toolbar->children);
486   toolbar->children = NULL;
487   
488   GTK_OBJECT_CLASS (parent_class)->destroy (object);
489 }
490
491 static void
492 gtk_toolbar_paint_space_line (GtkWidget       *widget,
493                               GdkRectangle    *area,
494                               GtkToolbarChild *child)
495 {
496   GtkToolbar *toolbar;
497   GtkToolbarChildSpace *child_space;
498   gint space_size;
499   
500   g_return_if_fail (GTK_IS_TOOLBAR (widget));
501   g_return_if_fail (child != NULL);
502   g_return_if_fail (child->type == GTK_TOOLBAR_CHILD_SPACE);
503
504   toolbar = GTK_TOOLBAR (widget);
505
506   child_space = (GtkToolbarChildSpace *) child;
507   space_size = get_space_size (toolbar);
508   
509   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
510     gtk_paint_vline (widget->style, widget->window,
511                      GTK_WIDGET_STATE (widget), area, widget,
512                      "toolbar",
513                      child_space->alloc_y + toolbar->button_maxh *
514                      SPACE_LINE_START / SPACE_LINE_DIVISION,
515                      child_space->alloc_y + toolbar->button_maxh *
516                      SPACE_LINE_END / SPACE_LINE_DIVISION,
517                      child_space->alloc_x +
518                      (space_size -
519                       widget->style->xthickness) / 2);
520   else
521     gtk_paint_hline (widget->style, widget->window,
522                      GTK_WIDGET_STATE (widget), area, widget,
523                      "toolbar",
524                      child_space->alloc_x + toolbar->button_maxw *
525                      SPACE_LINE_START / SPACE_LINE_DIVISION,
526                      child_space->alloc_x + toolbar->button_maxw *
527                      SPACE_LINE_END / SPACE_LINE_DIVISION,
528                      child_space->alloc_y +
529                      (space_size -
530                       widget->style->ythickness) / 2);
531 }
532
533 static gint
534 gtk_toolbar_expose (GtkWidget      *widget,
535                     GdkEventExpose *event)
536 {
537   GtkToolbar *toolbar;
538   GList *children;
539   GtkToolbarChild *child;
540   gint border_width;
541   
542   g_return_val_if_fail (GTK_IS_TOOLBAR (widget), FALSE);
543   g_return_val_if_fail (event != NULL, FALSE);
544
545   border_width = GTK_CONTAINER (widget)->border_width;
546   
547   if (GTK_WIDGET_DRAWABLE (widget))
548     {
549       GtkShadowType shadow_type;
550
551       toolbar = GTK_TOOLBAR (widget);
552
553       gtk_widget_style_get (widget, "shadow_type", &shadow_type, NULL);
554       
555       gtk_paint_box (widget->style,
556                      widget->window,
557                      GTK_WIDGET_STATE (widget),
558                      shadow_type,
559                      &event->area, widget, "toolbar",
560                      widget->allocation.x + border_width,
561                      widget->allocation.y + border_width,
562                      widget->allocation.width - border_width,
563                      widget->allocation.height - border_width);
564       
565       for (children = toolbar->children; children; children = children->next)
566         {
567           child = children->data;
568
569           if (child->type == GTK_TOOLBAR_CHILD_SPACE)
570             {
571               if (get_space_style (toolbar) == GTK_TOOLBAR_SPACE_LINE)
572                 gtk_toolbar_paint_space_line (widget, &event->area, child);
573             }
574           else
575             gtk_container_propagate_expose (GTK_CONTAINER (widget),
576                                             child->widget,
577                                             event);
578         }
579     }
580
581   return FALSE;
582 }
583
584 static void
585 gtk_toolbar_size_request (GtkWidget      *widget,
586                           GtkRequisition *requisition)
587 {
588   GtkToolbar *toolbar;
589   GList *children;
590   GtkToolbarChild *child;
591   gint nbuttons;
592   gint button_maxw, button_maxh;
593   gint widget_maxw, widget_maxh;
594   GtkRequisition child_requisition;
595   gint space_size;
596   gint ipadding;
597   
598   g_return_if_fail (GTK_IS_TOOLBAR (widget));
599   g_return_if_fail (requisition != NULL);
600
601   toolbar = GTK_TOOLBAR (widget);
602
603   requisition->width = GTK_CONTAINER (toolbar)->border_width * 2;
604   requisition->height = GTK_CONTAINER (toolbar)->border_width * 2;
605   nbuttons = 0;
606   button_maxw = 0;
607   button_maxh = 0;
608   widget_maxw = 0;
609   widget_maxh = 0;
610
611   space_size = get_space_size (toolbar);
612   
613   for (children = toolbar->children; children; children = children->next)
614     {
615       child = children->data;
616
617       switch (child->type)
618         {
619         case GTK_TOOLBAR_CHILD_SPACE:
620           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
621             requisition->width += space_size;
622           else
623             requisition->height += space_size;
624
625           break;
626
627         case GTK_TOOLBAR_CHILD_BUTTON:
628         case GTK_TOOLBAR_CHILD_RADIOBUTTON:
629         case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
630           if (GTK_WIDGET_VISIBLE (child->widget))
631             {              
632               gtk_widget_size_request (child->widget, &child_requisition);
633
634               nbuttons++;
635               button_maxw = MAX (button_maxw, child_requisition.width);
636               button_maxh = MAX (button_maxh, child_requisition.height);
637             }
638
639           break;
640
641         case GTK_TOOLBAR_CHILD_WIDGET:
642           if (GTK_WIDGET_VISIBLE (child->widget))
643             {
644               gtk_widget_size_request (child->widget, &child_requisition);
645
646               widget_maxw = MAX (widget_maxw, child_requisition.width);
647               widget_maxh = MAX (widget_maxh, child_requisition.height);
648
649               if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
650                 requisition->width += child_requisition.width;
651               else
652                 requisition->height += child_requisition.height;
653             }
654
655           break;
656
657         default:
658           g_assert_not_reached ();
659         }
660     }
661
662   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
663     {
664       requisition->width += nbuttons * button_maxw;
665       requisition->height += MAX (button_maxh, widget_maxh);
666     }
667   else
668     {
669       requisition->width += MAX (button_maxw, widget_maxw);
670       requisition->height += nbuttons * button_maxh;
671     }
672
673   /* Extra spacing */
674   gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
675   
676   requisition->width += 2 * ipadding;
677   requisition->height += 2 * ipadding;
678   
679   toolbar->button_maxw = button_maxw;
680   toolbar->button_maxh = button_maxh;
681 }
682
683 static void
684 gtk_toolbar_size_allocate (GtkWidget     *widget,
685                            GtkAllocation *allocation)
686 {
687   GtkToolbar *toolbar;
688   GList *children;
689   GtkToolbarChild *child;
690   GtkToolbarChildSpace *child_space;
691   GtkAllocation alloc;
692   GtkRequisition child_requisition;
693   gint x_border_width, y_border_width;
694   gint space_size;
695   gint ipadding;
696   GtkTextDirection direction;
697   gint ltr_x = 0;               /* Quiet GCC */
698   
699   g_return_if_fail (GTK_IS_TOOLBAR (widget));
700   g_return_if_fail (allocation != NULL);
701
702   toolbar = GTK_TOOLBAR (widget);
703   widget->allocation = *allocation;
704   
705   direction = gtk_widget_get_direction (widget);
706
707   x_border_width = GTK_CONTAINER (toolbar)->border_width;
708   y_border_width = GTK_CONTAINER (toolbar)->border_width;
709
710   gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
711   
712   x_border_width += ipadding;
713   y_border_width += ipadding;
714   
715   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
716     ltr_x = allocation->x + x_border_width;
717   else
718     alloc.y = allocation->y + y_border_width;
719
720   space_size = get_space_size (toolbar);
721   
722   for (children = toolbar->children; children; children = children->next)
723     {
724       child = children->data;
725
726       switch (child->type)
727         {
728         case GTK_TOOLBAR_CHILD_SPACE:
729
730           child_space = (GtkToolbarChildSpace *) child;
731
732           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
733             {
734               if (direction == GTK_TEXT_DIR_LTR)
735                 child_space->alloc_x = ltr_x;
736               else
737                 child_space->alloc_x = allocation->width - ltr_x - space_size;
738               child_space->alloc_y = allocation->y + (allocation->height - toolbar->button_maxh) / 2;
739               ltr_x += space_size;
740             }
741           else
742             {
743               child_space->alloc_x = allocation->x + (allocation->width - toolbar->button_maxw) / 2;
744               child_space->alloc_y = alloc.y;
745               alloc.y += space_size;
746             }
747
748           break;
749
750         case GTK_TOOLBAR_CHILD_BUTTON:
751         case GTK_TOOLBAR_CHILD_RADIOBUTTON:
752         case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
753           if (!GTK_WIDGET_VISIBLE (child->widget))
754             break;
755
756           alloc.width = toolbar->button_maxw;
757           alloc.height = toolbar->button_maxh;
758
759           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) 
760             {
761               if (direction == GTK_TEXT_DIR_LTR)
762                 alloc.x = ltr_x;
763               else
764                 alloc.x = allocation->width - ltr_x - alloc.width;
765               alloc.y = allocation->y + (allocation->height - toolbar->button_maxh) / 2;
766             }
767           else
768             alloc.x = allocation->x + (allocation->width - toolbar->button_maxw) / 2;
769           
770           gtk_widget_size_allocate (child->widget, &alloc);
771
772           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
773             ltr_x += toolbar->button_maxw;
774           else
775             alloc.y += toolbar->button_maxh;
776
777           break;
778
779         case GTK_TOOLBAR_CHILD_WIDGET:
780           if (!GTK_WIDGET_VISIBLE (child->widget))
781             break;
782
783           gtk_widget_get_child_requisition (child->widget, &child_requisition);
784           
785           alloc.width = child_requisition.width;
786           alloc.height = child_requisition.height;
787
788           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) 
789             {
790               if (direction == GTK_TEXT_DIR_LTR)
791                 alloc.x = ltr_x;
792               else
793                 alloc.x = allocation->width - ltr_x - alloc.width;
794               alloc.y = allocation->y + (allocation->height - child_requisition.height) / 2;
795             }
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             ltr_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   GtkWidget *retval;
1142
1143   if (gtk_stock_lookup (stock_id, &item))
1144     {
1145       image = gtk_image_new_from_stock (stock_id, toolbar->icon_size);
1146       label = item.label;
1147     }
1148   else
1149     label = stock_id;
1150
1151   label_no_mnemonic = elide_underscores (label);
1152   
1153   retval =  gtk_toolbar_internal_insert_item (toolbar,
1154                                               label_no_mnemonic,
1155                                               tooltip_text,
1156                                               tooltip_private_text,
1157                                               image,
1158                                               callback,
1159                                               user_data,
1160                                               position);
1161
1162   g_free (label_no_mnemonic);
1163
1164   return retval;
1165 }
1166
1167
1168
1169 void
1170 gtk_toolbar_append_space (GtkToolbar *toolbar)
1171 {
1172   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1173                               NULL, NULL,
1174                               NULL, NULL,
1175                               NULL, NULL, NULL,
1176                               toolbar->num_children);
1177 }
1178
1179 void
1180 gtk_toolbar_prepend_space (GtkToolbar *toolbar)
1181 {
1182   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1183                               NULL, NULL,
1184                               NULL, NULL,
1185                               NULL, NULL, NULL,
1186                               0);
1187 }
1188
1189 void
1190 gtk_toolbar_insert_space (GtkToolbar *toolbar,
1191                           gint        position)
1192 {
1193   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1194                               NULL, NULL,
1195                               NULL, NULL,
1196                               NULL, NULL, NULL,
1197                               position);
1198 }
1199
1200 /**
1201  * gtk_toolbar_remove_space:
1202  * @toolbar: a #GtkToolbar.
1203  * @position: the index of the space to remove.
1204  * 
1205  * Removes a space from the specified position.
1206  **/
1207 void
1208 gtk_toolbar_remove_space (GtkToolbar *toolbar,
1209                           gint        position)
1210 {
1211   GList *children;
1212   GtkToolbarChild *child;
1213   gint i;
1214   
1215   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1216   
1217   i = 0;
1218   for (children = toolbar->children; children; children = children->next)
1219     {
1220       child = children->data;
1221
1222       if (i == position)
1223         {
1224           if (child->type == GTK_TOOLBAR_CHILD_SPACE)
1225             {
1226               toolbar->children = g_list_remove_link (toolbar->children, children);
1227               g_free (child);
1228               g_list_free (children);
1229               toolbar->num_children--;
1230               
1231               gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1232             }
1233           else
1234             {
1235               g_warning ("Toolbar position %d is not a space", position);
1236             }
1237
1238           return;
1239         }
1240
1241       ++i;
1242     }
1243
1244   g_warning ("Toolbar position %d doesn't exist", position);
1245 }
1246
1247 /**
1248  * gtk_toolbar_append_widget:
1249  * @toolbar: a #GtkToolbar.
1250  * @widget: a #GtkWidget to add to the toolbar. 
1251  * @tooltip_text: the element's tooltip.
1252  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
1253  * 
1254  * Adds a widget to the end of the given toolbar.
1255  **/ 
1256 void
1257 gtk_toolbar_append_widget (GtkToolbar  *toolbar,
1258                            GtkWidget   *widget,
1259                            const gchar *tooltip_text,
1260                            const gchar *tooltip_private_text)
1261 {
1262   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1263                               widget, NULL,
1264                               tooltip_text, tooltip_private_text,
1265                               NULL, NULL, NULL,
1266                               toolbar->num_children);
1267 }
1268
1269 /**
1270  * gtk_toolbar_prepend_widget:
1271  * @toolbar: a #GtkToolbar.
1272  * @widget: a #GtkWidget to add to the toolbar. 
1273  * @tooltip_text: the element's tooltip.
1274  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
1275  * 
1276  * Adds a widget to the beginning of the given toolbar.
1277  **/ 
1278 void
1279 gtk_toolbar_prepend_widget (GtkToolbar  *toolbar,
1280                             GtkWidget   *widget,
1281                             const gchar *tooltip_text,
1282                             const gchar *tooltip_private_text)
1283 {
1284   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1285                               widget, NULL,
1286                               tooltip_text, tooltip_private_text,
1287                               NULL, NULL, NULL,
1288                               0);
1289 }
1290
1291 /**
1292  * gtk_toolbar_insert_widget:
1293  * @toolbar: a #GtkToolbar.
1294  * @widget: a #GtkWidget to add to the toolbar. 
1295  * @tooltip_text: the element's tooltip.
1296  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
1297  * @position: the number of widgets to insert this widget after.
1298  * 
1299  * Inserts a widget in the toolbar at the given position.
1300  **/ 
1301 void
1302 gtk_toolbar_insert_widget (GtkToolbar *toolbar,
1303                            GtkWidget  *widget,
1304                            const char *tooltip_text,
1305                            const char *tooltip_private_text,
1306                            gint        position)
1307 {
1308   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1309                               widget, NULL,
1310                               tooltip_text, tooltip_private_text,
1311                               NULL, NULL, NULL,
1312                               position);
1313 }
1314
1315 GtkWidget*
1316 gtk_toolbar_append_element (GtkToolbar          *toolbar,
1317                             GtkToolbarChildType  type,
1318                             GtkWidget           *widget,
1319                             const char          *text,
1320                             const char          *tooltip_text,
1321                             const char          *tooltip_private_text,
1322                             GtkWidget           *icon,
1323                             GtkSignalFunc        callback,
1324                             gpointer             user_data)
1325 {
1326   return gtk_toolbar_insert_element (toolbar, type, widget, text,
1327                                      tooltip_text, tooltip_private_text,
1328                                      icon, callback, user_data,
1329                                      toolbar->num_children);
1330 }
1331
1332 GtkWidget *
1333 gtk_toolbar_prepend_element (GtkToolbar          *toolbar,
1334                              GtkToolbarChildType  type,
1335                              GtkWidget           *widget,
1336                              const char          *text,
1337                              const char          *tooltip_text,
1338                              const char          *tooltip_private_text,
1339                              GtkWidget           *icon,
1340                              GtkSignalFunc        callback,
1341                              gpointer             user_data)
1342 {
1343   return gtk_toolbar_insert_element (toolbar, type, widget, text,
1344                                      tooltip_text, tooltip_private_text,
1345                                      icon, callback, user_data, 0);
1346 }
1347
1348 /**
1349  * gtk_toolbar_insert_element:
1350  * @toolbar: a #GtkToolbar.
1351  * @type: a value of type #GtkToolbarChildType that determines what @widget
1352  *   will be.
1353  * @widget: a #GtkWidget, or %NULL. 
1354  * @text: the element's label.
1355  * @tooltip_text: the element's tooltip.
1356  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
1357  * @icon: a #GtkWidget that provides pictorial representation of the element's function.
1358  * @callback: the function to be executed when the button is pressed.
1359  * @user_data: any data you wish to pass to the callback.
1360  * @position: the number of widgets to insert this element after.
1361  *
1362  * Inserts a new element in the toolbar at the given position. 
1363  *
1364  * If @type == %GTK_TOOLBAR_CHILD_WIDGET, @widget is used as the new element.
1365  * If @type == %GTK_TOOLBAR_CHILD_RADIOBUTTON, @widget is used to determine
1366  * the radio group for the new element. In all other cases, @widget must
1367  * be %NULL.
1368  *
1369  * Return value: the new toolbar element as a #GtkWidget.
1370  **/
1371 GtkWidget *
1372 gtk_toolbar_insert_element (GtkToolbar          *toolbar,
1373                             GtkToolbarChildType  type,
1374                             GtkWidget           *widget,
1375                             const char          *text,
1376                             const char          *tooltip_text,
1377                             const char          *tooltip_private_text,
1378                             GtkWidget           *icon,
1379                             GtkSignalFunc        callback,
1380                             gpointer             user_data,
1381                             gint                 position)
1382 {
1383   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
1384   if (type == GTK_TOOLBAR_CHILD_WIDGET)
1385     {
1386       g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1387     }
1388   else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
1389     g_return_val_if_fail (widget == NULL, NULL);
1390   
1391   return gtk_toolbar_internal_insert_element (toolbar, type, widget, text,
1392                                               tooltip_text, tooltip_private_text,
1393                                               icon, callback, user_data,
1394                                               position);
1395 }
1396
1397 static GtkWidget *
1398 gtk_toolbar_internal_insert_element (GtkToolbar          *toolbar,
1399                                      GtkToolbarChildType  type,
1400                                      GtkWidget           *widget,
1401                                      const char          *text,
1402                                      const char          *tooltip_text,
1403                                      const char          *tooltip_private_text,
1404                                      GtkWidget           *icon,
1405                                      GtkSignalFunc        callback,
1406                                      gpointer             user_data,
1407                                      gint                 position)
1408 {
1409   GtkToolbarChild *child;
1410   GtkWidget *box;
1411
1412   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
1413   if (type == GTK_TOOLBAR_CHILD_WIDGET)
1414     {
1415       g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1416     }
1417   else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
1418     g_return_val_if_fail (widget == NULL, NULL);
1419
1420   if (type == GTK_TOOLBAR_CHILD_SPACE)
1421     child = (GtkToolbarChild *) g_new (GtkToolbarChildSpace, 1);
1422   else
1423     child = g_new (GtkToolbarChild, 1);
1424
1425   child->type = type;
1426   child->icon = NULL;
1427   child->label = NULL;
1428
1429   switch (type)
1430     {
1431     case GTK_TOOLBAR_CHILD_SPACE:
1432       child->widget = NULL;
1433       ((GtkToolbarChildSpace *) child)->alloc_x =
1434         ((GtkToolbarChildSpace *) child)->alloc_y = 0;
1435       break;
1436
1437     case GTK_TOOLBAR_CHILD_WIDGET:
1438       child->widget = widget;
1439       break;
1440
1441     case GTK_TOOLBAR_CHILD_BUTTON:
1442     case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
1443     case GTK_TOOLBAR_CHILD_RADIOBUTTON:
1444       if (type == GTK_TOOLBAR_CHILD_BUTTON)
1445         {
1446           child->widget = gtk_button_new ();
1447           gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1448         }
1449       else if (type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1450         {
1451           child->widget = gtk_toggle_button_new ();
1452           gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1453           gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child->widget),
1454                                       FALSE);
1455         }
1456       else
1457         {
1458           child->widget = gtk_radio_button_new (widget
1459                                                 ? gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget))
1460                                                 : NULL);
1461           gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1462           gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child->widget), FALSE);
1463         }
1464
1465       GTK_WIDGET_UNSET_FLAGS (child->widget, GTK_CAN_FOCUS);
1466
1467       if (callback)
1468         g_signal_connect (child->widget, "clicked",
1469                           callback, user_data);
1470
1471       if (toolbar->style == GTK_TOOLBAR_BOTH_HORIZ)
1472           box = gtk_hbox_new (FALSE, 0);
1473       else
1474           box = gtk_vbox_new (FALSE, 0);
1475       gtk_container_add (GTK_CONTAINER (child->widget), box);
1476       gtk_widget_show (box);
1477
1478       if (text)
1479         {
1480           child->label = gtk_label_new (text);
1481           gtk_box_pack_end (GTK_BOX (box), child->label, FALSE, FALSE, 0);
1482           if (toolbar->style != GTK_TOOLBAR_ICONS)
1483             gtk_widget_show (child->label);
1484         }
1485
1486       if (icon)
1487         {
1488           child->icon = GTK_WIDGET (icon);
1489           gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1490           if (toolbar->style != GTK_TOOLBAR_TEXT)
1491             gtk_widget_show (child->icon);
1492         }
1493
1494       if (type != GTK_TOOLBAR_CHILD_WIDGET)
1495         {
1496           /* Mark child as ours */
1497           g_object_set_data (G_OBJECT (child->widget),
1498                              "gtk-toolbar-is-child",
1499                              GINT_TO_POINTER (TRUE));
1500         }
1501       gtk_widget_show (child->widget);
1502       break;
1503
1504     default:
1505       g_assert_not_reached ();
1506     }
1507
1508   if ((type != GTK_TOOLBAR_CHILD_SPACE) && tooltip_text)
1509     gtk_tooltips_set_tip (toolbar->tooltips, child->widget,
1510                           tooltip_text, tooltip_private_text);
1511
1512   toolbar->children = g_list_insert (toolbar->children, child, position);
1513   toolbar->num_children++;
1514
1515   if (type != GTK_TOOLBAR_CHILD_SPACE)
1516     gtk_widget_set_parent (child->widget, GTK_WIDGET (toolbar));
1517   else
1518     gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1519
1520   return child->widget;
1521 }
1522
1523 void
1524 gtk_toolbar_set_orientation (GtkToolbar     *toolbar,
1525                              GtkOrientation  orientation)
1526 {
1527   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1528
1529   g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0, orientation);
1530 }
1531
1532 /**
1533  * gtk_toolbar_get_orientation:
1534  * @toolbar: a #GtkToolbar
1535  * 
1536  * Retrieves the current orientation of the toolbar. See
1537  * gtk_toolbar_set_orientation().
1538  *
1539  * Return value: the orientation
1540  **/
1541 GtkOrientation
1542 gtk_toolbar_get_orientation (GtkToolbar *toolbar)
1543 {
1544   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
1545
1546   return toolbar->orientation;
1547 }
1548
1549 void
1550 gtk_toolbar_set_style (GtkToolbar      *toolbar,
1551                        GtkToolbarStyle  style)
1552 {
1553   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1554
1555   toolbar->style_set = TRUE;
1556   g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
1557 }
1558
1559 /**
1560  * gtk_toolbar_get_style:
1561  * @toolbar: a #GtkToolbar
1562  *
1563  * Retrieves whether the toolbar has text, icons, or both . See
1564  * gtk_toolbar_set_style().
1565  
1566  * Return value: the current style of @toolbar
1567  **/
1568 GtkToolbarStyle
1569 gtk_toolbar_get_style (GtkToolbar *toolbar)
1570 {
1571   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
1572
1573   return toolbar->style;
1574 }
1575
1576 /**
1577  * gtk_toolbar_unset_style:
1578  * @toolbar: a #GtkToolbar
1579  * 
1580  * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
1581  * user preferences will be used to determine the toolbar style.
1582  **/
1583 void
1584 gtk_toolbar_unset_style (GtkToolbar *toolbar)
1585 {
1586   GtkToolbarStyle style;
1587   
1588   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1589
1590   if (toolbar->style_set)
1591     {
1592       GtkSettings *settings = toolbar_get_settings (toolbar);
1593
1594       if (settings)
1595         g_object_get (settings,
1596                       "gtk-toolbar-style", &style,
1597                       NULL);
1598       else
1599         style = DEFAULT_TOOLBAR_STYLE;
1600
1601       if (style != toolbar->style)
1602         g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
1603       
1604       toolbar->style_set = FALSE;
1605     }
1606 }
1607
1608 void
1609 gtk_toolbar_set_tooltips (GtkToolbar *toolbar,
1610                           gboolean    enable)
1611 {
1612   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1613
1614   if (enable)
1615     gtk_tooltips_enable (toolbar->tooltips);
1616   else
1617     gtk_tooltips_disable (toolbar->tooltips);
1618 }
1619
1620 /**
1621  * gtk_toolbar_get_tooltips:
1622  * @toolbar: a #GtkToolbar
1623  *
1624  * Retrieves whether tooltips are enabled. See
1625  * gtk_toolbar_set_tooltips().
1626  *
1627  * Return value: %TRUE if tooltips are enabled
1628  **/
1629 gboolean
1630 gtk_toolbar_get_tooltips (GtkToolbar *toolbar)
1631 {
1632   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1633
1634   return toolbar->tooltips->enabled;
1635 }
1636
1637 static void
1638 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1639 {
1640   GList *children;
1641   GtkToolbarChild *child;
1642   GtkReliefStyle relief;
1643   
1644   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1645
1646   relief = get_button_relief (toolbar);
1647   
1648   for (children = toolbar->children; children; children = children->next)
1649     {
1650       child = children->data;
1651       if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
1652           child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
1653           child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1654         gtk_button_set_relief (GTK_BUTTON (child->widget), relief);
1655     }
1656 }
1657
1658 static void
1659 gtk_real_toolbar_orientation_changed (GtkToolbar     *toolbar,
1660                                       GtkOrientation  orientation)
1661 {
1662   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1663
1664   if (toolbar->orientation != orientation)
1665     {
1666       toolbar->orientation = orientation;
1667       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1668       g_object_notify (G_OBJECT (toolbar), "orientation");
1669     }
1670 }
1671
1672 static GtkWidget *
1673 get_first_child (GtkContainer *container)
1674 {
1675   GList *children = gtk_container_get_children (container);
1676   GtkWidget *result = children ? children->data : NULL;
1677   g_list_free (children);
1678   
1679   return result;
1680 }
1681
1682 static void
1683 gtk_real_toolbar_style_changed (GtkToolbar      *toolbar,
1684                                 GtkToolbarStyle  style)
1685 {
1686   GList *children;
1687   GtkToolbarChild *child;
1688   GtkWidget* box = NULL;
1689   
1690   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1691
1692   if (toolbar->style != style)
1693     {
1694       toolbar->style = style;
1695
1696       for (children = toolbar->children; children; children = children->next)
1697         {
1698           child = children->data;
1699
1700           if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
1701               child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
1702               child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1703             switch (style)
1704               {
1705               case GTK_TOOLBAR_ICONS:
1706                 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1707                   gtk_widget_show (child->icon);
1708
1709                 if (child->label && GTK_WIDGET_VISIBLE (child->label))
1710                   gtk_widget_hide (child->label);
1711
1712                 break;
1713
1714               case GTK_TOOLBAR_TEXT:
1715                 if (child->icon && GTK_WIDGET_VISIBLE (child->icon))
1716                   gtk_widget_hide (child->icon);
1717                 
1718                 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1719                   gtk_widget_show (child->label);
1720
1721                 break;
1722
1723               case GTK_TOOLBAR_BOTH:
1724                 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1725                   gtk_widget_show (child->icon);
1726
1727                 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1728                   gtk_widget_show (child->label);
1729
1730                 box = get_first_child (GTK_CONTAINER (child->widget));
1731
1732                 if (GTK_IS_HBOX (box))
1733                 {
1734                     if (child->icon)
1735                     {
1736                         g_object_ref (child->icon);
1737                         gtk_container_remove (GTK_CONTAINER (box),
1738                                               child->icon);
1739                     }
1740                     if (child->label)
1741                     {
1742                         g_object_ref (child->label);
1743                         gtk_container_remove (GTK_CONTAINER (box),
1744                                               child->label);
1745                     }
1746                     gtk_container_remove (GTK_CONTAINER (child->widget),
1747                                           box);
1748                     
1749                     box = gtk_vbox_new (FALSE, 0);
1750                     gtk_widget_show (box);
1751                     
1752                     if (child->label)
1753                     {
1754                         gtk_box_pack_end (GTK_BOX (box), child->label, FALSE, FALSE, 0);
1755                         g_object_unref (child->label);
1756                     }
1757                     if (child->icon)
1758                     {
1759                         gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1760                         g_object_unref (child->icon);
1761                     }
1762                     gtk_container_add (GTK_CONTAINER (child->widget),
1763                                        box);
1764                 }
1765                 
1766                 break;
1767                 
1768               case GTK_TOOLBAR_BOTH_HORIZ:
1769                 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1770                   gtk_widget_show (child->icon);
1771                 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1772                   gtk_widget_show (child->label);
1773
1774                 box = get_first_child (GTK_CONTAINER (child->widget));
1775                 
1776                 if (GTK_IS_VBOX (box))
1777                 {
1778                     if (child->icon)
1779                     {
1780                         g_object_ref (child->icon);
1781                         gtk_container_remove (GTK_CONTAINER (box),
1782                                               child->icon);
1783                     }
1784                     if (child->label)
1785                     {
1786                         g_object_ref (child->label);
1787                         gtk_container_remove (GTK_CONTAINER (box),
1788                                               child->label);
1789                     }
1790                     gtk_container_remove (GTK_CONTAINER (child->widget),
1791                                           box);
1792
1793                     box = gtk_hbox_new (FALSE, 0);
1794                     gtk_widget_show (box);
1795                     
1796                     if (child->label)
1797                     {
1798                         gtk_box_pack_end (GTK_BOX (box), child->label, TRUE, TRUE, 0);
1799                         g_object_unref (child->label);
1800                     }
1801                     if (child->icon)
1802                     {
1803                         gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1804                         g_object_unref (child->icon);
1805                     }
1806                     gtk_container_add (GTK_CONTAINER (child->widget), box);
1807                     
1808                 }
1809                 
1810                 break;
1811
1812               default:
1813                 g_assert_not_reached ();
1814               }
1815         }
1816
1817       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1818       g_object_notify (G_OBJECT (toolbar), "toolbar_style");
1819     }
1820 }
1821
1822
1823 static GtkReliefStyle
1824 get_button_relief (GtkToolbar *toolbar)
1825 {
1826   GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
1827
1828   gtk_widget_ensure_style (GTK_WIDGET (toolbar));
1829   gtk_widget_style_get (GTK_WIDGET (toolbar),
1830                         "button_relief", &button_relief,
1831                         NULL);
1832
1833   return button_relief;
1834 }
1835
1836 static gint
1837 get_space_size (GtkToolbar *toolbar)
1838 {
1839   gint space_size = DEFAULT_SPACE_SIZE;
1840
1841   gtk_widget_style_get (GTK_WIDGET (toolbar),
1842                         "space_size", &space_size,
1843                         NULL);
1844
1845   return space_size;
1846 }
1847
1848 static GtkToolbarSpaceStyle
1849 get_space_style (GtkToolbar *toolbar)
1850 {
1851   GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE;
1852
1853   gtk_widget_style_get (GTK_WIDGET (toolbar),
1854                         "space_style", &space_style,
1855                         NULL);
1856
1857
1858   return space_style;  
1859 }