gtktoolbar.c 52.5 KB
Newer Older
1
2
3
4
5
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * GtkToolbar copyright (C) Federico Mena
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
8
9
10
11
12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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
17
18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
19
20
 */

21
/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23
24
25
26
27
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

28
29
#include <string.h>

30
#include "gtkbutton.h"
31
32
#include "gtktogglebutton.h"
#include "gtkradiobutton.h"
33
34
#include "gtklabel.h"
#include "gtkvbox.h"
35
#include "gtkhbox.h"
36
#include "gtktoolbar.h"
37
38
39
#include "gtkstock.h"
#include "gtkiconfactory.h"
#include "gtkimage.h"
40
#include "gtksettings.h"
Havoc Pennington's avatar
Havoc Pennington committed
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
53
54
#define SPACE_LINE_DIVISION 10
#define SPACE_LINE_START    3
#define SPACE_LINE_END      7
55

56
enum {
57
58
  PROP_0,
  PROP_ORIENTATION,
59
  PROP_TOOLBAR_STYLE
60
61
};

62
enum {
63
64
65
  ORIENTATION_CHANGED,
  STYLE_CHANGED,
  LAST_SIGNAL
66
67
};

68
69
70
71
72
73
74
75
typedef struct _GtkToolbarChildSpace GtkToolbarChildSpace;
struct _GtkToolbarChildSpace
{
  GtkToolbarChild child;

  gint alloc_x, alloc_y;
};

76
77
static void gtk_toolbar_class_init               (GtkToolbarClass *class);
static void gtk_toolbar_init                     (GtkToolbar      *toolbar);
78
79
80
81
82
83
84
85
static void gtk_toolbar_set_property             (GObject         *object,
						  guint            prop_id,
						  const GValue    *value,
						  GParamSpec      *pspec);
static void gtk_toolbar_get_property             (GObject         *object,
						  guint            prop_id,
						  GValue          *value,
						  GParamSpec      *pspec);
86
static void gtk_toolbar_destroy                  (GtkObject       *object);
87
88
static gint gtk_toolbar_expose                   (GtkWidget       *widget,
						  GdkEventExpose  *event);
89
90
91
92
static void gtk_toolbar_size_request             (GtkWidget       *widget,
				                  GtkRequisition  *requisition);
static void gtk_toolbar_size_allocate            (GtkWidget       *widget,
				                  GtkAllocation   *allocation);
Havoc Pennington's avatar
Havoc Pennington committed
93
94
static void gtk_toolbar_style_set                (GtkWidget       *widget,
                                                  GtkStyle        *prev_style);
95
96
static gboolean gtk_toolbar_focus                (GtkWidget       *widget,
                                                  GtkDirectionType dir);
Owen Taylor's avatar
Owen Taylor committed
97
98
static void gtk_toolbar_screen_changed           (GtkWidget       *widget,
						  GdkScreen       *previous_screen);
99
static void gtk_toolbar_show_all                 (GtkWidget       *widget);
100
101
static void gtk_toolbar_add                      (GtkContainer    *container,
				                  GtkWidget       *widget);
102
103
static void gtk_toolbar_remove                   (GtkContainer    *container,
						  GtkWidget       *widget);
104
105
static void gtk_toolbar_forall                   (GtkContainer    *container,
						  gboolean	   include_internals,
106
107
				                  GtkCallback      callback,
				                  gpointer         callback_data);
108

109
110
111
112
113
static void gtk_real_toolbar_orientation_changed (GtkToolbar      *toolbar,
						  GtkOrientation   orientation);
static void gtk_real_toolbar_style_changed       (GtkToolbar      *toolbar,
						  GtkToolbarStyle  style);

114
115
116
117
118
119
120
121
122
static GtkWidget * gtk_toolbar_internal_insert_element (GtkToolbar          *toolbar,
                                                        GtkToolbarChildType  type,
                                                        GtkWidget           *widget,
                                                        const char          *text,
                                                        const char          *tooltip_text,
                                                        const char          *tooltip_private_text,
                                                        GtkWidget           *icon,
                                                        GtkSignalFunc        callback,
                                                        gpointer             user_data,
123
                                                        gint                 position);
124
125
126
127
128
129
130
131

static GtkWidget * gtk_toolbar_internal_insert_item (GtkToolbar    *toolbar,
                                                     const char    *text,
                                                     const char    *tooltip_text,
                                                     const char    *tooltip_private_text,
                                                     GtkWidget     *icon,
                                                     GtkSignalFunc  callback,
                                                     gpointer       user_data,
132
                                                     gint           position);
133

Havoc Pennington's avatar
Havoc Pennington committed
134
135
136
137
138
139
140
static void        gtk_toolbar_update_button_relief (GtkToolbar *toolbar);

static GtkReliefStyle       get_button_relief (GtkToolbar *toolbar);
static gint                 get_space_size    (GtkToolbar *toolbar);
static GtkToolbarSpaceStyle get_space_style   (GtkToolbar *toolbar);


141
142
static GtkContainerClass *parent_class;

143
static guint toolbar_signals[LAST_SIGNAL] = { 0 };
144

145

Manish Singh's avatar
Manish Singh committed
146
GType
147
gtk_toolbar_get_type (void)
148
{
Manish Singh's avatar
Manish Singh committed
149
  static GType toolbar_type = 0;
150
151
152

  if (!toolbar_type)
    {
Manish Singh's avatar
Manish Singh committed
153
      static const GTypeInfo toolbar_info =
154
155
      {
	sizeof (GtkToolbarClass),
Manish Singh's avatar
Manish Singh committed
156
157
158
159
160
161
162
163
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_toolbar_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkToolbar),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_toolbar_init,
164
165
      };

Manish Singh's avatar
Manish Singh committed
166
167
      toolbar_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkToolbar",
					     &toolbar_info, 0);
168
169
170
    }

  return toolbar_type;
171
172
173
}

static void
174
175
gtk_toolbar_class_init (GtkToolbarClass *class)
{
176
  GObjectClass   *gobject_class;
177
178
179
180
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

181
  gobject_class = G_OBJECT_CLASS (class);
182
183
184
185
  object_class = (GtkObjectClass *) class;
  widget_class = (GtkWidgetClass *) class;
  container_class = (GtkContainerClass *) class;

Manish Singh's avatar
Manish Singh committed
186
  parent_class = g_type_class_peek_parent (class);
187

188
  object_class->destroy = gtk_toolbar_destroy;
189
190
  gobject_class->set_property = gtk_toolbar_set_property;
  gobject_class->get_property = gtk_toolbar_get_property;
191
192
193
194

  widget_class->expose_event = gtk_toolbar_expose;
  widget_class->size_request = gtk_toolbar_size_request;
  widget_class->size_allocate = gtk_toolbar_size_allocate;
Havoc Pennington's avatar
Havoc Pennington committed
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;
Owen Taylor's avatar
Owen Taylor committed
198
  widget_class->screen_changed = gtk_toolbar_screen_changed;
Havoc Pennington's avatar
Havoc Pennington committed
199
  
200
201
  container_class->add = gtk_toolbar_add;
  container_class->remove = gtk_toolbar_remove;
202
  container_class->forall = gtk_toolbar_forall;
203
  
204
205
  class->orientation_changed = gtk_real_toolbar_orientation_changed;
  class->style_changed = gtk_real_toolbar_style_changed;
206
207

  toolbar_signals[ORIENTATION_CHANGED] =
Manish Singh's avatar
Manish Singh committed
208
209
210
211
212
213
214
215
    g_signal_new ("orientation_changed",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__ENUM,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_ORIENTATION);
216
  toolbar_signals[STYLE_CHANGED] =
Manish Singh's avatar
Manish Singh committed
217
218
219
220
221
222
223
224
    g_signal_new ("style_changed",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__ENUM,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_TOOLBAR_STYLE);
225
  
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  g_object_class_install_property (gobject_class,
				   PROP_ORIENTATION,
				   g_param_spec_enum ("orientation",
 						      _("Orientation"),
 						      _("The orientation of the toolbar"),
 						      GTK_TYPE_ORIENTATION,
 						      GTK_ORIENTATION_HORIZONTAL,
 						      G_PARAM_READWRITE));
 
   g_object_class_install_property (gobject_class,
                                    PROP_TOOLBAR_STYLE,
                                    g_param_spec_enum ("toolbar_style",
 						      _("Toolbar Style"),
 						      _("How to draw the toolbar"),
 						      GTK_TYPE_TOOLBAR_STYLE,
 						      GTK_TOOLBAR_ICONS,
 						      G_PARAM_READWRITE));

Havoc Pennington's avatar
Havoc Pennington committed
244
245
246
247
248
249
250
251
252

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("space_size",
							     _("Spacer size"),
							     _("Size of spacers"),
							     0,
							     G_MAXINT,
                                                             DEFAULT_SPACE_SIZE,
							     G_PARAM_READABLE));
253
254
255
256
257
258
259
260
261
262

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("internal_padding",
							     _("Internal padding"),
							     _("Amount of border space between the toolbar shadow and the buttons"),
							     0,
							     G_MAXINT,
                                                             DEFAULT_IPADDING,
                                                             G_PARAM_READABLE));
  
Havoc Pennington's avatar
Havoc Pennington committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_enum ("space_style",
							     _("Space style"),
							     _("Whether spacers are vertical lines or just blank"),
                                                              GTK_TYPE_TOOLBAR_SPACE_STYLE,
                                                              DEFAULT_SPACE_STYLE,
                                                              
                                                              G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_enum ("button_relief",
							     _("Button relief"),
							     _("Type of bevel around toolbar buttons"),
                                                              GTK_TYPE_RELIEF_STYLE,
277
                                                              GTK_RELIEF_NONE,
Havoc Pennington's avatar
Havoc Pennington committed
278
                                                              G_PARAM_READABLE));
279
280
281
282
283
284
285
286
287

  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_enum ("shadow_type",
                                                              _("Shadow type"),
                                                              _("Style of bevel around the toolbar"),
                                                              GTK_TYPE_SHADOW_TYPE,
                                                              GTK_SHADOW_OUT,
                                                              G_PARAM_READABLE));

288
  gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-style",
289
290
291
                                                    _("Toolbar style"),
                                                    _("Whether default toolbars have text only, text and icons, icons only, etc."),
                                                    GTK_TYPE_TOOLBAR_STYLE,
292
                                                    DEFAULT_TOOLBAR_STYLE,
293
294
                                                    G_PARAM_READWRITE));

295
  gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-icon-size",
296
297
298
                                                    _("Toolbar icon size"),
                                                    _("Size of icons in default toolbars"),
                                                    GTK_TYPE_ICON_SIZE,
299
                                                    DEFAULT_ICON_SIZE,
300
301
302
303
                                                    G_PARAM_READWRITE));  
}

static void
304
style_change_notify (GtkToolbar *toolbar)
305
306
307
308
309
310
311
312
313
314
{
  if (!toolbar->style_set)
    {
      /* pretend it was set, then unset, thus reverting to new default */
      toolbar->style_set = TRUE; 
      gtk_toolbar_unset_style (toolbar);
    }
}

static void
315
icon_size_change_notify (GtkToolbar *toolbar)
316
317
318
319
320
321
322
{
  if (!toolbar->icon_size_set)
    {
      /* pretend it was set, then unset, thus reverting to new default */
      toolbar->icon_size_set = TRUE; 
      gtk_toolbar_unset_icon_size (toolbar);
    }
323
324
}

325
326
327
328
329
330
static GtkSettings *
toolbar_get_settings (GtkToolbar *toolbar)
{
  return g_object_get_data (G_OBJECT (toolbar), "gtk-toolbar-settings");
}

331
static void
Owen Taylor's avatar
Owen Taylor committed
332
333
gtk_toolbar_screen_changed (GtkWidget *widget,
			    GdkScreen *previous_screen)
334
{
Owen Taylor's avatar
Owen Taylor committed
335
  GtkToolbar *toolbar = GTK_TOOLBAR (widget);
336
  GtkSettings *old_settings = toolbar_get_settings (toolbar);
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  GtkSettings *settings;

  if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
    settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
  else
    settings = NULL;

  if (settings == old_settings)
    return;

  if (old_settings)
    {
      g_signal_handler_disconnect (old_settings, toolbar->style_set_connection);
      g_signal_handler_disconnect (old_settings, toolbar->icon_size_connection);

      g_object_unref (old_settings);
    }
  
  if (settings)
    {
Manish Singh's avatar
Manish Singh committed
357
358
359
360
361
      toolbar->style_set_connection =
	g_signal_connect_swapped (settings,
				  "notify::gtk-toolbar-style",
				  G_CALLBACK (style_change_notify),
				  toolbar);
362
      
Manish Singh's avatar
Manish Singh committed
363
364
365
366
367
      toolbar->icon_size_connection =
	g_signal_connect_swapped (settings,
				  "notify::gtk-toolbar-icon-size",
				   G_CALLBACK (icon_size_change_notify),
				   toolbar);
368
369
370
371
372
373
374


      g_object_ref (settings);
      g_object_set_data (G_OBJECT (toolbar), "gtk-toolbar-settings", settings);
    }
  else
    g_object_set_data (G_OBJECT (toolbar), "gtk-toolbar-settings", NULL);
375
376
377

  style_change_notify (toolbar);
  icon_size_change_notify (toolbar);
378
379
}

380
static void
381
382
383
gtk_toolbar_init (GtkToolbar *toolbar)
{
  GTK_WIDGET_SET_FLAGS (toolbar, GTK_NO_WINDOW);
384
  GTK_WIDGET_UNSET_FLAGS (toolbar, GTK_CAN_FOCUS);
385
386
387
388

  toolbar->num_children = 0;
  toolbar->children     = NULL;
  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
393
394
  g_object_ref (toolbar->tooltips);
  gtk_object_sink (GTK_OBJECT (toolbar->tooltips));
  
395
396
  toolbar->button_maxw  = 0;
  toolbar->button_maxh  = 0;
397
398
399

  toolbar->style_set = FALSE;
  toolbar->icon_size_set = FALSE;
400
401
}

402
static void
403
404
405
406
gtk_toolbar_set_property (GObject      *object,
			  guint         prop_id,
			  const GValue *value,
			  GParamSpec   *pspec)
407
408
409
{
  GtkToolbar *toolbar = GTK_TOOLBAR (object);
  
410
  switch (prop_id)
411
    {
412
413
    case PROP_ORIENTATION:
      gtk_toolbar_set_orientation (toolbar, g_value_get_enum (value));
414
      break;
415
416
    case PROP_TOOLBAR_STYLE:
      gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
417
418
419
420
421
      break;
    }
}

static void
422
423
424
425
gtk_toolbar_get_property (GObject      *object,
			  guint         prop_id,
			  GValue       *value,
			  GParamSpec   *pspec)
426
427
428
{
  GtkToolbar *toolbar = GTK_TOOLBAR (object);

429
  switch (prop_id)
430
    {
431
432
    case PROP_ORIENTATION:
      g_value_set_enum (value, toolbar->orientation);
433
      break;
434
435
    case PROP_TOOLBAR_STYLE:
      g_value_set_enum (value, toolbar->style);
436
437
      break;
    default:
438
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
439
440
441
442
443
      break;
    }
}

GtkWidget*
444
gtk_toolbar_new (void)
445
{
446
  GtkToolbar *toolbar;
447

Manish Singh's avatar
Manish Singh committed
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
464
  if (toolbar->tooltips)
    {
Manish Singh's avatar
Manish Singh committed
465
      g_object_unref (toolbar->tooltips);
466
467
      toolbar->tooltips = NULL;
    }
468

469
470
  for (children = toolbar->children; children; children = children->next)
    {
471
      GtkToolbarChild *child;
472

473
      child = children->data;
474

475
      if (child->type != GTK_TOOLBAR_CHILD_SPACE)
476
	{
Manish Singh's avatar
Manish Singh committed
477
	  g_object_ref (child->widget);
478
	  gtk_widget_unparent (child->widget);
479
	  gtk_widget_destroy (child->widget);
Manish Singh's avatar
Manish Singh committed
480
	  g_object_unref (child->widget);
481
482
	}

483
484
485
      g_free (child);
    }
  g_list_free (toolbar->children);
486
487
  toolbar->children = NULL;
  
488
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
489
490
}

491
492
493
494
495
496
497
static void
gtk_toolbar_paint_space_line (GtkWidget       *widget,
			      GdkRectangle    *area,
			      GtkToolbarChild *child)
{
  GtkToolbar *toolbar;
  GtkToolbarChildSpace *child_space;
Havoc Pennington's avatar
Havoc Pennington committed
498
499
  gint space_size;
  
500
501
502
503
504
505
506
  g_return_if_fail (GTK_IS_TOOLBAR (widget));
  g_return_if_fail (child != NULL);
  g_return_if_fail (child->type == GTK_TOOLBAR_CHILD_SPACE);

  toolbar = GTK_TOOLBAR (widget);

  child_space = (GtkToolbarChildSpace *) child;
Havoc Pennington's avatar
Havoc Pennington committed
507
508
  space_size = get_space_size (toolbar);
  
509
510
511
512
513
514
515
516
517
  if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
    gtk_paint_vline (widget->style, widget->window,
		     GTK_WIDGET_STATE (widget), area, widget,
		     "toolbar",
		     child_space->alloc_y + toolbar->button_maxh *
		     SPACE_LINE_START / SPACE_LINE_DIVISION,
		     child_space->alloc_y + toolbar->button_maxh *
		     SPACE_LINE_END / SPACE_LINE_DIVISION,
		     child_space->alloc_x +
Havoc Pennington's avatar
Havoc Pennington committed
518
		     (space_size -
519
		      widget->style->xthickness) / 2);
520
521
522
523
524
525
526
527
528
  else
    gtk_paint_hline (widget->style, widget->window,
		     GTK_WIDGET_STATE (widget), area, widget,
		     "toolbar",
		     child_space->alloc_x + toolbar->button_maxw *
		     SPACE_LINE_START / SPACE_LINE_DIVISION,
		     child_space->alloc_x + toolbar->button_maxw *
		     SPACE_LINE_END / SPACE_LINE_DIVISION,
		     child_space->alloc_y +
Havoc Pennington's avatar
Havoc Pennington committed
529
		     (space_size -
530
		      widget->style->ythickness) / 2);
531
532
}

533
static gint
534
535
gtk_toolbar_expose (GtkWidget      *widget,
		    GdkEventExpose *event)
536
{
537
538
539
  GtkToolbar *toolbar;
  GList *children;
  GtkToolbarChild *child;
540
  gint border_width;
541
  
542
543
  g_return_val_if_fail (GTK_IS_TOOLBAR (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
544

545
546
  border_width = GTK_CONTAINER (widget)->border_width;
  
547
548
  if (GTK_WIDGET_DRAWABLE (widget))
    {
549
550
      GtkShadowType shadow_type;

551
      toolbar = GTK_TOOLBAR (widget);
552

553
554
555
556
557
558
559
560
561
562
563
564
      gtk_widget_style_get (widget, "shadow_type", &shadow_type, NULL);
      
      gtk_paint_box (widget->style,
		     widget->window,
                     GTK_WIDGET_STATE (widget),
                     shadow_type,
		     &event->area, widget, "toolbar",
		     widget->allocation.x + border_width,
                     widget->allocation.y + border_width,
		     widget->allocation.width - border_width,
                     widget->allocation.height - border_width);
      
565
566
567
      for (children = toolbar->children; children; children = children->next)
	{
	  child = children->data;
568

569
570
	  if (child->type == GTK_TOOLBAR_CHILD_SPACE)
	    {
Havoc Pennington's avatar
Havoc Pennington committed
571
	      if (get_space_style (toolbar) == GTK_TOOLBAR_SPACE_LINE)
572
573
		gtk_toolbar_paint_space_line (widget, &event->area, child);
	    }
574
575
576
577
	  else
	    gtk_container_propagate_expose (GTK_CONTAINER (widget),
					    child->widget,
					    event);
578
	}
579
    }
580

581
  return FALSE;
582
583
584
}

static void
585
586
587
588
gtk_toolbar_size_request (GtkWidget      *widget,
			  GtkRequisition *requisition)
{
  GtkToolbar *toolbar;
589
590
591
592
593
  GList *children;
  GtkToolbarChild *child;
  gint nbuttons;
  gint button_maxw, button_maxh;
  gint widget_maxw, widget_maxh;
594
  GtkRequisition child_requisition;
Havoc Pennington's avatar
Havoc Pennington committed
595
  gint space_size;
596
  gint ipadding;
Havoc Pennington's avatar
Havoc Pennington committed
597
  
598
599
600
601
602
603
604
605
606
607
608
609
610
  g_return_if_fail (GTK_IS_TOOLBAR (widget));
  g_return_if_fail (requisition != NULL);

  toolbar = GTK_TOOLBAR (widget);

  requisition->width = GTK_CONTAINER (toolbar)->border_width * 2;
  requisition->height = GTK_CONTAINER (toolbar)->border_width * 2;
  nbuttons = 0;
  button_maxw = 0;
  button_maxh = 0;
  widget_maxw = 0;
  widget_maxh = 0;

Havoc Pennington's avatar
Havoc Pennington committed
611
612
  space_size = get_space_size (toolbar);
  
613
614
615
616
617
618
  for (children = toolbar->children; children; children = children->next)
    {
      child = children->data;

      switch (child->type)
	{
619
	case GTK_TOOLBAR_CHILD_SPACE:
620
	  if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
Havoc Pennington's avatar
Havoc Pennington committed
621
	    requisition->width += space_size;
622
	  else
Havoc Pennington's avatar
Havoc Pennington committed
623
	    requisition->height += space_size;
624
625
626

	  break;

627
	case GTK_TOOLBAR_CHILD_BUTTON:
628
629
	case GTK_TOOLBAR_CHILD_RADIOBUTTON:
	case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
630
	  if (GTK_WIDGET_VISIBLE (child->widget))
Havoc Pennington's avatar
Havoc Pennington committed
631
	    {              
632
	      gtk_widget_size_request (child->widget, &child_requisition);
633
634

	      nbuttons++;
635
636
	      button_maxw = MAX (button_maxw, child_requisition.width);
	      button_maxh = MAX (button_maxh, child_requisition.height);
637
638
639
640
	    }

	  break;

641
	case GTK_TOOLBAR_CHILD_WIDGET:
642
643
	  if (GTK_WIDGET_VISIBLE (child->widget))
	    {
644
	      gtk_widget_size_request (child->widget, &child_requisition);
645

646
647
	      widget_maxw = MAX (widget_maxw, child_requisition.width);
	      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
656
657
658
	    }

	  break;

	default:
	  g_assert_not_reached ();
659
	}
660
661
662
663
664
665
666
667
668
669
670
671
672
    }

  if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      requisition->width += nbuttons * button_maxw;
      requisition->height += MAX (button_maxh, widget_maxh);
    }
  else
    {
      requisition->width += MAX (button_maxw, widget_maxw);
      requisition->height += nbuttons * button_maxh;
    }

673
674
675
  /* Extra spacing */
  gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
  
676
677
  requisition->width += 2 * ipadding;
  requisition->height += 2 * ipadding;
678
  
679
680
  toolbar->button_maxw = button_maxw;
  toolbar->button_maxh = button_maxh;
681
682
683
}

static void
684
685
686
gtk_toolbar_size_allocate (GtkWidget     *widget,
			   GtkAllocation *allocation)
{
687
688
  GtkToolbar *toolbar;
  GList *children;
689
  GtkToolbarChild *child;
690
  GtkToolbarChildSpace *child_space;
691
  GtkAllocation alloc;
692
  GtkRequisition child_requisition;
693
  gint x_border_width, y_border_width;
Havoc Pennington's avatar
Havoc Pennington committed
694
  gint space_size;
695
  gint ipadding;
696
  GtkTextDirection direction;
697
  gint ltr_x = 0;		/* Quiet GCC */
698
  
699
700
701
702
703
  g_return_if_fail (GTK_IS_TOOLBAR (widget));
  g_return_if_fail (allocation != NULL);

  toolbar = GTK_TOOLBAR (widget);
  widget->allocation = *allocation;
704
  
705
706
  direction = gtk_widget_get_direction (widget);

707
708
  x_border_width = GTK_CONTAINER (toolbar)->border_width;
  y_border_width = GTK_CONTAINER (toolbar)->border_width;
709

710
711
  gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
  
712
713
  x_border_width += ipadding;
  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

Havoc Pennington's avatar
Havoc Pennington committed
720
721
  space_size = get_space_size (toolbar);
  
722
723
724
725
726
727
  for (children = toolbar->children; children; children = children->next)
    {
      child = children->data;

      switch (child->type)
	{
728
	case GTK_TOOLBAR_CHILD_SPACE:
729
730
731

	  child_space = (GtkToolbarChildSpace *) child;

732
	  if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
733
	    {
734
735
736
737
	      if (direction == GTK_TEXT_DIR_LTR)
		child_space->alloc_x = ltr_x;
	      else
		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
744
	    {
	      child_space->alloc_x = allocation->x + (allocation->width - toolbar->button_maxw) / 2;
	      child_space->alloc_y = alloc.y;
Havoc Pennington's avatar
Havoc Pennington committed
745
	      alloc.y += space_size;
746
	    }
747
748
749

	  break;

750
	case GTK_TOOLBAR_CHILD_BUTTON:
751
752
	case GTK_TOOLBAR_CHILD_RADIOBUTTON:
	case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
753
754
	  if (!GTK_WIDGET_VISIBLE (child->widget))
	    break;
755

756
757
758
	  alloc.width = toolbar->button_maxw;
	  alloc.height = toolbar->button_maxh;

759
760
761
762
763
764
765
766
	  if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) 
	    {
	      if (direction == GTK_TEXT_DIR_LTR)
		alloc.x = ltr_x;
	      else
		alloc.x = allocation->width - ltr_x - alloc.width;
	      alloc.y = allocation->y + (allocation->height - toolbar->button_maxh) / 2;
	    }
767
768
	  else
	    alloc.x = allocation->x + (allocation->width - toolbar->button_maxw) / 2;
769
	  
770
771
772
	  gtk_widget_size_allocate (child->widget, &alloc);

	  if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
773
	    ltr_x += toolbar->button_maxw;
774
775
776
777
778
	  else
	    alloc.y += toolbar->button_maxh;

	  break;

779
	case GTK_TOOLBAR_CHILD_WIDGET:
780
781
	  if (!GTK_WIDGET_VISIBLE (child->widget))
	    break;
782

783
784
785
786
	  gtk_widget_get_child_requisition (child->widget, &child_requisition);
	  
	  alloc.width = child_requisition.width;
	  alloc.height = child_requisition.height;
787

788
789
790
791
792
793
794
795
	  if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL) 
	    {
	      if (direction == GTK_TEXT_DIR_LTR)
		alloc.x = ltr_x;
	      else
		alloc.x = allocation->width - ltr_x - alloc.width;
	      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
800
801

	  gtk_widget_size_allocate (child->widget, &alloc);

	  if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
802
	    ltr_x += child_requisition.width;
803
	  else
804
	    alloc.y += child_requisition.height;
805
806
807
808
809

	  break;

	default:
	  g_assert_not_reached ();
810
	}
811
    }
812
813
}

Havoc Pennington's avatar
Havoc Pennington committed
814
815
816
817
818
819
820
821
static void
gtk_toolbar_style_set (GtkWidget  *widget,
                       GtkStyle   *prev_style)
{
  if (prev_style)
    gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
}

822
823
824
825
826
827
828
829
830
static gboolean
gtk_toolbar_focus (GtkWidget       *widget,
                   GtkDirectionType dir)
{
  /* Focus can't go in toolbars */
  
  return FALSE;
}

831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
static void
child_show_all (GtkWidget *widget)
{
  /* Don't show our own children, since that would
   * show labels we may intend to hide in icons-only mode
   */
  if (!g_object_get_data (G_OBJECT (widget),
                          "gtk-toolbar-is-child"))
    gtk_widget_show_all (widget);
}

static void
gtk_toolbar_show_all (GtkWidget *widget)
{
  gtk_container_foreach (GTK_CONTAINER (widget),
			 (GtkCallback) child_show_all,
			 NULL);
  gtk_widget_show (widget);
}

851
static void
852
853
854
855
856
857
gtk_toolbar_add (GtkContainer *container,
		 GtkWidget    *widget)
{
  g_return_if_fail (GTK_IS_TOOLBAR (container));
  g_return_if_fail (widget != NULL);

858
  gtk_toolbar_append_widget (GTK_TOOLBAR (container), widget, NULL, NULL);
859
860
861
}

static void
862
863
gtk_toolbar_remove (GtkContainer *container,
		    GtkWidget    *widget)
864
{
865
  GtkToolbar *toolbar;
866
867
  GList *children;
  GtkToolbarChild *child;
868

869
870
  g_return_if_fail (GTK_IS_TOOLBAR (container));
  g_return_if_fail (widget != NULL);
871

872
  toolbar = GTK_TOOLBAR (container);
873

874
875
876
  for (children = toolbar->children; children; children = children->next)
    {
      child = children->data;
877

878
      if ((child->type != GTK_TOOLBAR_CHILD_SPACE) && (child->widget == widget))
879
	{
880
881
882
	  gboolean was_visible;

	  was_visible = GTK_WIDGET_VISIBLE (widget);
883
	  gtk_widget_unparent (widget);
884

885
886
887
888
	  toolbar->children = g_list_remove_link (toolbar->children, children);
	  g_free (child);
	  g_list_free (children);
	  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
900
901
902
gtk_toolbar_forall (GtkContainer *container,
		    gboolean	  include_internals,
		    GtkCallback   callback,
		    gpointer      callback_data)
903
{
904
  GtkToolbar *toolbar;
905
906
  GList *children;
  GtkToolbarChild *child;
907

908
909
  g_return_if_fail (GTK_IS_TOOLBAR (container));
  g_return_if_fail (callback != NULL);
910

911
  toolbar = GTK_TOOLBAR (container);
912

913
914
915
  for (children = toolbar->children; children; children = children->next)
    {
      child = children->data;
916

917
      if (child->type != GTK_TOOLBAR_CHILD_SPACE)
918
919
	(*callback) (child->widget, callback_data);
    }
920
921
}

922
GtkWidget *
923
gtk_toolbar_append_item (GtkToolbar    *toolbar,
924
925
			 const char    *text,
			 const char    *tooltip_text,
926
			 const char    *tooltip_private_text,
927
			 GtkWidget     *icon,
928
929
			 GtkSignalFunc  callback,
			 gpointer       user_data)
930
{
931
932
933
934
935
  return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
				     NULL, text,
				     tooltip_text, tooltip_private_text,
				     icon, callback, user_data,
				     toolbar->num_children);
936
937
}

938
GtkWidget *
939
940
941
gtk_toolbar_prepend_item (GtkToolbar    *toolbar,
			  const char    *text,
			  const char    *tooltip_text,
942
			  const char    *tooltip_private_text,
943
			  GtkWidget     *icon,
944
945
			  GtkSignalFunc  callback,
			  gpointer       user_data)
946
{
947
948
949
950
951
  return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
				     NULL, text,
				     tooltip_text, tooltip_private_text,
				     icon, callback, user_data,
				     0);
952
}
953

954
955
956
957
958
959
960
961
static GtkWidget *
gtk_toolbar_internal_insert_item (GtkToolbar    *toolbar,
                                  const char    *text,
                                  const char    *tooltip_text,
                                  const char    *tooltip_private_text,
                                  GtkWidget     *icon,
                                  GtkSignalFunc  callback,
                                  gpointer       user_data,
962
                                  gint           position)
963
964
965
966
967
{
  return gtk_toolbar_internal_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
                                              NULL, text,
                                              tooltip_text, tooltip_private_text,
                                              icon, callback, user_data,
968
                                              position);
969
970
}
     
971
972
973
974
GtkWidget *
gtk_toolbar_insert_item (GtkToolbar    *toolbar,
			 const char    *text,
			 const char    *tooltip_text,
975
			 const char    *tooltip_private_text,
976
			 GtkWidget     *icon,
977
978
979
980
			 GtkSignalFunc  callback,
			 gpointer       user_data,
			 gint           position)
{
981
982
983
  return gtk_toolbar_internal_insert_item (toolbar, 
                                           text, tooltip_text, tooltip_private_text,
                                           icon, callback, user_data,
984
                                           position);
985
986
}

987
988
989
990
991
992
993
/**
 * gtk_toolbar_set_icon_size:
 * @toolbar: A #GtkToolbar
 * @icon_size: The #GtkIconSize that stock icons in the toolbar shall have.
 *
 * This function sets the size of stock icons in the toolbar. You
 * can call it both before you add the icons and after they've been
994
995
 * added. The size you set will override user preferences for the default
 * icon size.
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
 **/
void
gtk_toolbar_set_icon_size (GtkToolbar  *toolbar,
			   GtkIconSize  icon_size)
{
  GList *children;
  GtkToolbarChild *child;
  GtkImage *image;
  gchar *stock_id;

  g_return_if_fail (GTK_IS_TOOLBAR (toolbar));

1008
1009
  toolbar->icon_size_set = TRUE;
  
1010
1011
1012
1013
1014
1015
1016
1017
  if (toolbar->icon_size == icon_size)
    return;
  
  toolbar->icon_size = icon_size;

  for (children = toolbar->children; children; children = children->next)
    {
      child = children->data;
1018
1019
1020
      if ((child->type == GTK_TOOLBAR_CHILD_BUTTON ||
	   child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON ||
	   child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON) &&
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
	  GTK_IS_IMAGE (child->icon))
	{
	  image = GTK_IMAGE (child->icon);
	  if (gtk_image_get_storage_type (image) == GTK_IMAGE_STOCK)
	    {
	      gtk_image_get_stock (image, &stock_id, NULL);
	      stock_id = g_strdup (stock_id);
	      gtk_image_set_from_stock (image,
					stock_id,
					icon_size);
	      g_free (stock_id);
	    }
	}
    }
  
  gtk_widget_queue_resize (GTK_WIDGET (toolbar));
}

1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
/**
 * gtk_toolbar_get_icon_size:
 * @toolbar: a #GtkToolbar
 *
 * Retrieves the icon size fo the toolbar. See gtk_toolbar_set_icon_size().
 *
 * Return value: the current icon size for the icons on the toolbar.
 **/
GtkIconSize
gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
{
1050
  g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
1051
1052
1053
1054

  return toolbar->icon_size;
}

1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
/**
 * gtk_toolbar_unset_icon_size:
 * @toolbar: a #GtkToolbar
 * 
 * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
 * user preferences will be used to determine the icon size.
 **/
void
gtk_toolbar_unset_icon_size (GtkToolbar  *toolbar)
{
  GtkIconSize size;

  if (toolbar->icon_size_set)
    {
1069
1070
1071
1072
1073
1074
1075
1076
      GtkSettings *settings = toolbar_get_settings (toolbar);

      if (settings)
	g_object_get (settings,
		      "gtk-toolbar-icon-size", &size,
		      NULL);
      else
	size = DEFAULT_ICON_SIZE;
1077
1078
1079
1080
1081
1082
1083
1084

      if (size != toolbar->icon_size)
        gtk_toolbar_set_icon_size (toolbar, size);

      toolbar->icon_size_set = FALSE;
    }
}

1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
static gchar *
elide_underscores (const gchar *original)
{
  gchar *q, *result;
  const gchar *p;
  gboolean last_underscore;

  q = result = g_malloc (strlen (original + 1));
  last_underscore = FALSE;
  
  for (p = original; *p; p++)
    {
      if (!last_underscore && *p == '_')
	last_underscore = TRUE;
      else
	{
	  last_underscore = FALSE;
	  *q++ = *p;
	}
    }
  
  *q = '\0';
  
  return result;
}

1111
1112
1113
1114