]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
Also intern static strings passed to g_object_set_data()
[~andy/gtk] / gtk / gtkmenubar.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #define GTK_MENU_INTERNALS
28
29 #include <config.h>
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbindings.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkmenubar.h"
35 #include "gtkmenuitem.h"
36 #include "gtksettings.h"
37 #include "gtkintl.h"
38 #include "gtkwindow.h"
39 #include "gtkprivate.h"
40 #include "gtkalias.h"
41
42
43 #define BORDER_SPACING  0
44 #define DEFAULT_IPADDING 1
45
46 /* Properties */
47 enum {
48   PROP_0,
49   PROP_PACK_DIRECTION,
50   PROP_CHILD_PACK_DIRECTION
51 };
52
53 typedef struct _GtkMenuBarPrivate GtkMenuBarPrivate;
54 struct _GtkMenuBarPrivate
55 {
56   GtkPackDirection pack_direction;
57   GtkPackDirection child_pack_direction;
58 };
59
60 #define GTK_MENU_BAR_GET_PRIVATE(o)  \
61   (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_MENU_BAR, GtkMenuBarPrivate))
62
63
64 static void gtk_menu_bar_class_init        (GtkMenuBarClass *klass);
65 static void gtk_menu_bar_set_property      (GObject             *object,
66                                             guint                prop_id,
67                                             const GValue        *value,
68                                             GParamSpec          *pspec);
69 static void gtk_menu_bar_get_property      (GObject             *object,
70                                             guint                prop_id,
71                                             GValue              *value,
72                                             GParamSpec          *pspec);
73 static void gtk_menu_bar_size_request      (GtkWidget       *widget,
74                                             GtkRequisition  *requisition);
75 static void gtk_menu_bar_size_allocate     (GtkWidget       *widget,
76                                             GtkAllocation   *allocation);
77 static void gtk_menu_bar_paint             (GtkWidget       *widget,
78                                             GdkRectangle    *area);
79 static gint gtk_menu_bar_expose            (GtkWidget       *widget,
80                                             GdkEventExpose  *event);
81 static void gtk_menu_bar_hierarchy_changed (GtkWidget       *widget,
82                                             GtkWidget       *old_toplevel);
83 static gint gtk_menu_bar_get_popup_delay   (GtkMenuShell    *menu_shell);
84 static void gtk_menu_bar_move_current      (GtkMenuShell     *menu_shell,
85                                             GtkMenuDirectionType direction);
86                                             
87
88 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
89
90 static GtkMenuShellClass *parent_class = NULL;
91
92 GType
93 gtk_menu_bar_get_type (void)
94 {
95   static GType menu_bar_type = 0;
96
97   if (!menu_bar_type)
98     {
99       static const GTypeInfo menu_bar_info =
100       {
101         sizeof (GtkMenuBarClass),
102         NULL,           /* base_init */
103         NULL,           /* base_finalize */
104         (GClassInitFunc) gtk_menu_bar_class_init,
105         NULL,           /* class_finalize */
106         NULL,           /* class_data */
107         sizeof (GtkMenuBar),
108         0,              /* n_preallocs */
109         NULL,           /* instance_init */
110       };
111
112       menu_bar_type = g_type_register_static (GTK_TYPE_MENU_SHELL, g_intern_static_string ("GtkMenuBar"),
113                                               &menu_bar_info, 0);
114     }
115
116   return menu_bar_type;
117 }
118
119 static void
120 gtk_menu_bar_class_init (GtkMenuBarClass *class)
121 {
122   GObjectClass *gobject_class;
123   GtkObjectClass *object_class;
124   GtkWidgetClass *widget_class;
125   GtkMenuShellClass *menu_shell_class;
126
127   GtkBindingSet *binding_set;
128
129   parent_class = g_type_class_peek_parent (class);
130   
131   gobject_class = (GObjectClass*) class;
132   object_class = (GtkObjectClass*) class;
133   widget_class = (GtkWidgetClass*) class;
134   menu_shell_class = (GtkMenuShellClass*) class;
135
136   gobject_class->get_property = gtk_menu_bar_get_property;
137   gobject_class->set_property = gtk_menu_bar_set_property;
138
139   widget_class->size_request = gtk_menu_bar_size_request;
140   widget_class->size_allocate = gtk_menu_bar_size_allocate;
141   widget_class->expose_event = gtk_menu_bar_expose;
142   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
143   
144   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
145   menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
146   menu_shell_class->move_current = gtk_menu_bar_move_current;
147
148   binding_set = gtk_binding_set_by_class (class);
149   gtk_binding_entry_add_signal (binding_set,
150                                 GDK_Left, 0,
151                                 "move_current", 1,
152                                 GTK_TYPE_MENU_DIRECTION_TYPE,
153                                 GTK_MENU_DIR_PREV);
154   gtk_binding_entry_add_signal (binding_set,
155                                 GDK_KP_Left, 0,
156                                 "move_current", 1,
157                                 GTK_TYPE_MENU_DIRECTION_TYPE,
158                                 GTK_MENU_DIR_PREV);
159   gtk_binding_entry_add_signal (binding_set,
160                                 GDK_Right, 0,
161                                 "move_current", 1,
162                                 GTK_TYPE_MENU_DIRECTION_TYPE,
163                                 GTK_MENU_DIR_NEXT);
164   gtk_binding_entry_add_signal (binding_set,
165                                 GDK_KP_Right, 0,
166                                 "move_current", 1,
167                                 GTK_TYPE_MENU_DIRECTION_TYPE,
168                                 GTK_MENU_DIR_NEXT);
169   gtk_binding_entry_add_signal (binding_set,
170                                 GDK_Up, 0,
171                                 "move_current", 1,
172                                 GTK_TYPE_MENU_DIRECTION_TYPE,
173                                 GTK_MENU_DIR_PARENT);
174   gtk_binding_entry_add_signal (binding_set,
175                                 GDK_KP_Up, 0,
176                                 "move_current", 1,
177                                 GTK_TYPE_MENU_DIRECTION_TYPE,
178                                 GTK_MENU_DIR_PARENT);
179   gtk_binding_entry_add_signal (binding_set,
180                                 GDK_Down, 0,
181                                 "move_current", 1,
182                                 GTK_TYPE_MENU_DIRECTION_TYPE,
183                                 GTK_MENU_DIR_CHILD);
184   gtk_binding_entry_add_signal (binding_set,
185                                 GDK_KP_Down, 0,
186                                 "move_current", 1,
187                                 GTK_TYPE_MENU_DIRECTION_TYPE,
188                                 GTK_MENU_DIR_CHILD);
189
190   /**
191    * GtkMenuBar:pack-direction:
192    *
193    * The pack direction of the menubar. It determines how
194    * menuitems are arranged in the menubar.
195    *
196    * Since: 2.8
197    */
198   g_object_class_install_property (gobject_class,
199                                    PROP_PACK_DIRECTION,
200                                    g_param_spec_enum ("pack-direction",
201                                                       P_("Pack direction"),
202                                                       P_("The pack direction of the menubar"),
203                                                       GTK_TYPE_PACK_DIRECTION,
204                                                       GTK_PACK_DIRECTION_LTR,
205                                                       GTK_PARAM_READWRITE));
206   
207   /**
208    * GtkMenuBar:child-pack-direction:
209    *
210    * The pack direction of the menubar. It determines how
211    * the widgets contained in child menuitems are arranged.
212    *
213    * Since: 2.8
214    */
215   g_object_class_install_property (gobject_class,
216                                    PROP_CHILD_PACK_DIRECTION,
217                                    g_param_spec_enum ("child-pack-direction",
218                                                       P_("Child Pack direction"),
219                                                       P_("The child pack direction of the menubar"),
220                                                       GTK_TYPE_PACK_DIRECTION,
221                                                       GTK_PACK_DIRECTION_LTR,
222                                                       GTK_PARAM_READWRITE));
223   
224
225   gtk_widget_class_install_style_property (widget_class,
226                                            g_param_spec_enum ("shadow-type",
227                                                               P_("Shadow type"),
228                                                               P_("Style of bevel around the menubar"),
229                                                               GTK_TYPE_SHADOW_TYPE,
230                                                               GTK_SHADOW_OUT,
231                                                               GTK_PARAM_READABLE));
232
233   gtk_widget_class_install_style_property (widget_class,
234                                            g_param_spec_int ("internal-padding",
235                                                              P_("Internal padding"),
236                                                              P_("Amount of border space between the menubar shadow and the menu items"),
237                                                              0,
238                                                              G_MAXINT,
239                                                              DEFAULT_IPADDING,
240                                                              GTK_PARAM_READABLE));
241
242   gtk_settings_install_property (g_param_spec_int ("gtk-menu-bar-popup-delay",
243                                                    P_("Delay before drop down menus appear"),
244                                                    P_("Delay before the submenus of a menu bar appear"),
245                                                    0,
246                                                    G_MAXINT,
247                                                    0,
248                                                    GTK_PARAM_READWRITE));
249
250   g_type_class_add_private (gobject_class, sizeof (GtkMenuBarPrivate));  
251 }
252  
253 GtkWidget*
254 gtk_menu_bar_new (void)
255 {
256   return g_object_new (GTK_TYPE_MENU_BAR, NULL);
257 }
258
259 static void
260 gtk_menu_bar_set_property (GObject      *object,
261                            guint         prop_id,
262                            const GValue *value,
263                            GParamSpec   *pspec)
264 {
265   GtkMenuBar *menubar = GTK_MENU_BAR (object);
266   
267   switch (prop_id)
268     {
269     case PROP_PACK_DIRECTION:
270       gtk_menu_bar_set_pack_direction (menubar, g_value_get_enum (value));
271       break;
272     case PROP_CHILD_PACK_DIRECTION:
273       gtk_menu_bar_set_child_pack_direction (menubar, g_value_get_enum (value));
274       break;
275     default:
276       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277       break;
278     }
279 }
280
281 static void
282 gtk_menu_bar_get_property (GObject    *object,
283                            guint       prop_id,
284                            GValue     *value,
285                            GParamSpec *pspec)
286 {
287   GtkMenuBar *menubar = GTK_MENU_BAR (object);
288   
289   switch (prop_id)
290     {
291     case PROP_PACK_DIRECTION:
292       g_value_set_enum (value, gtk_menu_bar_get_pack_direction (menubar));
293       break;
294     case PROP_CHILD_PACK_DIRECTION:
295       g_value_set_enum (value, gtk_menu_bar_get_child_pack_direction (menubar));
296       break;
297     default:
298       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299       break;
300     }
301 }
302
303 static void
304 gtk_menu_bar_size_request (GtkWidget      *widget,
305                            GtkRequisition *requisition)
306 {
307   GtkMenuBar *menu_bar;
308   GtkMenuBarPrivate *priv;
309   GtkMenuShell *menu_shell;
310   GtkWidget *child;
311   GList *children;
312   gint nchildren;
313   GtkRequisition child_requisition;
314   gint ipadding;
315
316   g_return_if_fail (GTK_IS_MENU_BAR (widget));
317   g_return_if_fail (requisition != NULL);
318
319   requisition->width = 0;
320   requisition->height = 0;
321   
322   if (GTK_WIDGET_VISIBLE (widget))
323     {
324       menu_bar = GTK_MENU_BAR (widget);
325       menu_shell = GTK_MENU_SHELL (widget);
326       priv = GTK_MENU_BAR_GET_PRIVATE (menu_bar);
327
328       nchildren = 0;
329       children = menu_shell->children;
330
331       while (children)
332         {
333           child = children->data;
334           children = children->next;
335
336           if (GTK_WIDGET_VISIBLE (child))
337             {
338               gint toggle_size;
339
340               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
341               gtk_widget_size_request (child, &child_requisition);
342               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
343                                                  &toggle_size);
344
345               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
346                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
347                 child_requisition.width += toggle_size;
348               else
349                 child_requisition.height += toggle_size;
350
351               if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
352                   priv->pack_direction == GTK_PACK_DIRECTION_RTL)
353                 {
354                   requisition->width += child_requisition.width;
355                   requisition->height = MAX (requisition->height, child_requisition.height);
356                 }
357               else
358                 {
359                   requisition->width = MAX (requisition->width, child_requisition.width);
360                   requisition->height += child_requisition.height;
361                 }
362               nchildren += 1;
363             }
364         }
365
366       gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
367       
368       requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
369                              ipadding + 
370                              BORDER_SPACING) * 2;
371       requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
372                               ipadding +
373                               BORDER_SPACING) * 2;
374
375       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
376         {
377           requisition->width += widget->style->xthickness * 2;
378           requisition->height += widget->style->ythickness * 2;
379         }
380     }
381 }
382
383 static void
384 gtk_menu_bar_size_allocate (GtkWidget     *widget,
385                             GtkAllocation *allocation)
386 {
387   GtkMenuBar *menu_bar;
388   GtkMenuShell *menu_shell;
389   GtkMenuBarPrivate *priv;
390   GtkWidget *child;
391   GList *children;
392   GtkAllocation child_allocation;
393   GtkRequisition child_requisition;
394   guint offset;
395   GtkTextDirection direction;
396   gint ltr_x, ltr_y;
397   gint ipadding;
398
399   g_return_if_fail (GTK_IS_MENU_BAR (widget));
400   g_return_if_fail (allocation != NULL);
401
402   menu_bar = GTK_MENU_BAR (widget);
403   menu_shell = GTK_MENU_SHELL (widget);
404   priv = GTK_MENU_BAR_GET_PRIVATE (menu_bar);
405
406   direction = gtk_widget_get_direction (widget);
407
408   widget->allocation = *allocation;
409   if (GTK_WIDGET_REALIZED (widget))
410     gdk_window_move_resize (widget->window,
411                             allocation->x, allocation->y,
412                             allocation->width, allocation->height);
413
414   gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
415   
416   if (menu_shell->children)
417     {
418       child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
419                             ipadding + 
420                             BORDER_SPACING);
421       child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
422                             BORDER_SPACING);
423       
424       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
425         {
426           child_allocation.x += widget->style->xthickness;
427           child_allocation.y += widget->style->ythickness;
428         }
429       
430       if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
431           priv->pack_direction == GTK_PACK_DIRECTION_RTL)
432         {
433           child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
434           
435           offset = child_allocation.x;  /* Window edge to menubar start */
436           ltr_x = child_allocation.x;
437           
438           children = menu_shell->children;
439           while (children)
440             {
441               gint toggle_size;          
442               
443               child = children->data;
444               children = children->next;
445               
446               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
447                                                  &toggle_size);
448               gtk_widget_get_child_requisition (child, &child_requisition);
449             
450               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
451                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
452                 child_requisition.width += toggle_size;
453               else
454                 child_requisition.height += toggle_size;
455               
456               /* Support for the right justified help menu */
457               if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
458                   && (GTK_MENU_ITEM(child)->right_justify)) 
459                 {
460                   ltr_x = allocation->width -
461                     child_requisition.width - offset;
462                 }
463               if (GTK_WIDGET_VISIBLE (child))
464                 {
465                   if ((direction == GTK_TEXT_DIR_LTR) == (priv->pack_direction == GTK_PACK_DIRECTION_LTR))
466                     child_allocation.x = ltr_x;
467                   else
468                     child_allocation.x = allocation->width -
469                       child_requisition.width - ltr_x; 
470                   
471                   child_allocation.width = child_requisition.width;
472                   
473                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
474                                                       toggle_size);
475                   gtk_widget_size_allocate (child, &child_allocation);
476                   
477                   ltr_x += child_allocation.width;
478                 }
479             }
480         }
481       else
482         {
483           child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
484           
485           offset = child_allocation.y;  /* Window edge to menubar start */
486           ltr_y = child_allocation.y;
487           
488           children = menu_shell->children;
489           while (children)
490             {
491               gint toggle_size;          
492               
493               child = children->data;
494               children = children->next;
495               
496               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
497                                                  &toggle_size);
498               gtk_widget_get_child_requisition (child, &child_requisition);
499               
500               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
501                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
502                 child_requisition.width += toggle_size;
503               else
504                 child_requisition.height += toggle_size;
505               
506               /* Support for the right justified help menu */
507               if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
508                   && (GTK_MENU_ITEM(child)->right_justify)) 
509                 {
510                   ltr_y = allocation->height -
511                     child_requisition.height - offset;
512                 }
513               if (GTK_WIDGET_VISIBLE (child))
514                 {
515                   if ((direction == GTK_TEXT_DIR_LTR) ==
516                       (priv->pack_direction == GTK_PACK_DIRECTION_TTB))
517                     child_allocation.y = ltr_y;
518                   else
519                     child_allocation.y = allocation->height -
520                       child_requisition.height - ltr_y; 
521                   child_allocation.height = child_requisition.height;
522                   
523                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
524                                                       toggle_size);
525                   gtk_widget_size_allocate (child, &child_allocation);
526                   
527                   ltr_y += child_allocation.height;
528                 }
529             }
530         }
531     }
532 }
533
534 static void
535 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
536 {
537   g_return_if_fail (GTK_IS_MENU_BAR (widget));
538
539   if (GTK_WIDGET_DRAWABLE (widget))
540     {
541       gint border;
542
543       border = GTK_CONTAINER (widget)->border_width;
544       
545       gtk_paint_box (widget->style,
546                      widget->window,
547                      GTK_WIDGET_STATE (widget),
548                      get_shadow_type (GTK_MENU_BAR (widget)),
549                      area, widget, "menubar",
550                      border, border,
551                      widget->allocation.width - border * 2,
552                      widget->allocation.height - border * 2);
553     }
554 }
555
556 static gint
557 gtk_menu_bar_expose (GtkWidget      *widget,
558                      GdkEventExpose *event)
559 {
560   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
561   g_return_val_if_fail (event != NULL, FALSE);
562
563   if (GTK_WIDGET_DRAWABLE (widget))
564     {
565       gtk_menu_bar_paint (widget, &event->area);
566
567       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
568     }
569
570   return FALSE;
571 }
572
573 static GList *
574 get_menu_bars (GtkWindow *window)
575 {
576   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
577 }
578
579 static GList *
580 get_viewable_menu_bars (GtkWindow *window)
581 {
582   GList *menu_bars;
583   GList *viewable_menu_bars = NULL;
584
585   for (menu_bars = get_menu_bars (window);
586        menu_bars;
587        menu_bars = menu_bars->next)
588     {
589       GtkWidget *widget = menu_bars->data;
590       gboolean viewable = TRUE;
591       
592       while (widget)
593         {
594           if (!GTK_WIDGET_MAPPED (widget))
595             viewable = FALSE;
596           
597           widget = widget->parent;
598         }
599
600       if (viewable)
601         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
602     }
603
604   return g_list_reverse (viewable_menu_bars);
605 }
606
607 static void
608 set_menu_bars (GtkWindow *window,
609                GList     *menubars)
610 {
611   g_object_set_data (G_OBJECT (window), g_intern_static_string ("gtk-menu-bar-list"), menubars);
612 }
613
614 static gboolean
615 window_key_press_handler (GtkWidget   *widget,
616                           GdkEventKey *event,
617                           gpointer     data)
618 {
619   gchar *accel = NULL;
620   gboolean retval = FALSE;
621   
622   g_object_get (gtk_widget_get_settings (widget),
623                 "gtk-menu-bar-accel",
624                 &accel,
625                 NULL);
626
627   if (accel)
628     {
629       guint keyval = 0;
630       GdkModifierType mods = 0;
631
632       gtk_accelerator_parse (accel, &keyval, &mods);
633
634       if (keyval == 0)
635         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
636
637       /* FIXME this is wrong, needs to be in the global accel resolution
638        * thing, to properly consider i18n etc., but that probably requires
639        * AccelGroup changes etc.
640        */
641       if (event->keyval == keyval &&
642           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
643            (mods & gtk_accelerator_get_default_mod_mask ())))
644         {
645           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
646           GList *menubars;
647
648           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
649                                                 GTK_DIR_TAB_FORWARD, NULL);
650           g_list_free (tmp_menubars);
651           
652           if (menubars)
653             {
654               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
655
656               _gtk_menu_shell_activate (menu_shell);
657               gtk_menu_shell_select_first (menu_shell, FALSE);
658               
659               g_list_free (menubars);
660               
661               retval = TRUE;          
662             }
663         }
664
665       g_free (accel);
666     }
667
668   return retval;
669 }
670
671 static void
672 add_to_window (GtkWindow  *window,
673                GtkMenuBar *menubar)
674 {
675   GList *menubars = get_menu_bars (window);
676
677   if (!menubars)
678     {
679       g_signal_connect (window,
680                         "key_press_event",
681                         G_CALLBACK (window_key_press_handler),
682                         NULL);
683     }
684
685   set_menu_bars (window, g_list_prepend (menubars, menubar));
686 }
687
688 static void
689 remove_from_window (GtkWindow  *window,
690                     GtkMenuBar *menubar)
691 {
692   GList *menubars = get_menu_bars (window);
693
694   menubars = g_object_get_data (G_OBJECT (window),
695                                     "gtk-menu-bar-list");
696
697   menubars = g_list_remove (menubars, menubar);
698
699   if (!menubars)
700     {
701       g_signal_handlers_disconnect_by_func (window,
702                                             window_key_press_handler,
703                                             NULL);
704     }
705
706   set_menu_bars (window, menubars);
707 }
708
709 static void
710 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
711                                 GtkWidget *old_toplevel)
712 {
713   GtkWidget *toplevel;  
714   GtkMenuBar *menubar;
715
716   menubar = GTK_MENU_BAR (widget);
717
718   toplevel = gtk_widget_get_toplevel (widget);
719
720   if (old_toplevel)
721     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
722   
723   if (GTK_WIDGET_TOPLEVEL (toplevel))
724     add_to_window (GTK_WINDOW (toplevel), menubar);
725 }
726
727 /**
728  * _gtk_menu_bar_cycle_focus:
729  * @menubar: a #GtkMenuBar
730  * @dir: direction in which to cycle the focus
731  * 
732  * Move the focus between menubars in the toplevel.
733  **/
734 void
735 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
736                            GtkDirectionType  dir)
737 {
738   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
739   GtkMenuItem *to_activate = NULL;
740
741   if (GTK_WIDGET_TOPLEVEL (toplevel))
742     {
743       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
744       GList *menubars;
745       GList *current;
746
747       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
748                                             dir, GTK_WIDGET (menubar));
749       g_list_free (tmp_menubars);
750
751       if (menubars)
752         {
753           current = g_list_find (menubars, menubar);
754
755           if (current && current->next)
756             {
757               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
758               if (new_menushell->children)
759                 to_activate = new_menushell->children->data;
760             }
761         }
762           
763       g_list_free (menubars);
764     }
765
766   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
767
768   if (to_activate)
769     g_signal_emit_by_name (to_activate, "activate_item");
770 }
771
772 static GtkShadowType
773 get_shadow_type (GtkMenuBar *menubar)
774 {
775   GtkShadowType shadow_type = GTK_SHADOW_OUT;
776   
777   gtk_widget_style_get (GTK_WIDGET (menubar),
778                         "shadow-type", &shadow_type,
779                         NULL);
780
781   return shadow_type;
782 }
783
784 static gint
785 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
786 {
787   gint popup_delay;
788   
789   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
790                 "gtk-menu-bar-popup-delay", &popup_delay,
791                 NULL);
792
793   return popup_delay;
794 }
795
796 static void
797 gtk_menu_bar_move_current (GtkMenuShell         *menu_shell,
798                            GtkMenuDirectionType direction)
799 {
800   GtkMenuBar *menubar = GTK_MENU_BAR (menu_shell);
801   GtkTextDirection text_dir;
802   GtkPackDirection pack_dir;
803
804   text_dir = gtk_widget_get_direction (GTK_WIDGET (menubar));
805   pack_dir = gtk_menu_bar_get_pack_direction (menubar);
806   
807   if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
808      {
809       if ((text_dir == GTK_TEXT_DIR_RTL) == (pack_dir == GTK_PACK_DIRECTION_LTR))
810         {
811           switch (direction) 
812             {      
813             case GTK_MENU_DIR_PREV:
814               direction = GTK_MENU_DIR_NEXT;
815               break;
816             case GTK_MENU_DIR_NEXT:
817               direction = GTK_MENU_DIR_PREV;
818               break;
819             default: ;
820             }
821         }
822     }
823   else
824     {
825       switch (direction) 
826         {
827         case GTK_MENU_DIR_PARENT:
828           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
829             direction = GTK_MENU_DIR_PREV;
830           else
831             direction = GTK_MENU_DIR_NEXT;
832           break;
833         case GTK_MENU_DIR_CHILD:
834           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
835             direction = GTK_MENU_DIR_NEXT;
836           else
837             direction = GTK_MENU_DIR_PREV;
838           break;
839         case GTK_MENU_DIR_PREV:
840           if (text_dir == GTK_TEXT_DIR_RTL)       
841             direction = GTK_MENU_DIR_CHILD;
842           else
843             direction = GTK_MENU_DIR_PARENT;
844           break;
845         case GTK_MENU_DIR_NEXT:
846           if (text_dir == GTK_TEXT_DIR_RTL)       
847             direction = GTK_MENU_DIR_PARENT;
848           else
849             direction = GTK_MENU_DIR_CHILD;
850           break;
851         default: ;
852         }
853     }
854   
855   GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
856 }
857
858 /**
859  * gtk_menu_bar_get_pack_direction:
860  * @menubar: a #GtkMenuBar
861  * 
862  * Retrieves the current pack direction of the menubar. See
863  * gtk_menu_bar_set_pack_direction().
864  *
865  * Return value: the pack direction
866  *
867  * Since: 2.8
868  **/
869 GtkPackDirection
870 gtk_menu_bar_get_pack_direction (GtkMenuBar *menubar)
871 {
872   GtkMenuBarPrivate *priv;
873
874   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
875                         GTK_PACK_DIRECTION_LTR);
876   
877   priv = GTK_MENU_BAR_GET_PRIVATE (menubar);
878
879   return priv->pack_direction;
880 }
881
882 /**
883  * gtk_menu_bar_set_pack_direction:
884  * @menubar: a #GtkMenuBar.
885  * @pack_dir: a new #GtkPackDirection.
886  * 
887  * Sets how items should be packed inside a menubar.
888  * 
889  * Since: 2.8
890  **/
891 void gtk_menu_bar_set_pack_direction (GtkMenuBar       *menubar,
892                                       GtkPackDirection  pack_dir)
893 {
894   GtkMenuBarPrivate *priv;
895   GList *l;
896
897   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
898
899   priv = GTK_MENU_BAR_GET_PRIVATE (menubar);
900
901   if (priv->pack_direction != pack_dir)
902     {
903       priv->pack_direction = pack_dir;
904
905       gtk_widget_queue_resize (GTK_WIDGET (menubar));
906
907       for (l = GTK_MENU_SHELL (menubar)->children; l; l = l->next)
908         gtk_widget_queue_resize (GTK_WIDGET (l->data));
909
910       g_object_notify (G_OBJECT (menubar), "pack-direction");
911     }
912 }
913
914 /**
915  * gtk_menu_bar_get_child_pack_direction:
916  * @menubar: a #GtkMenuBar
917  * 
918  * Retrieves the current child pack direction of the menubar. See
919  * gtk_menu_bar_set_child_pack_direction().
920  *
921  * Return value: the child pack direction
922  *
923  * Since: 2.8
924  **/
925 GtkPackDirection
926 gtk_menu_bar_get_child_pack_direction (GtkMenuBar *menubar)
927 {
928   GtkMenuBarPrivate *priv;
929
930   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
931                         GTK_PACK_DIRECTION_LTR);
932   
933   priv = GTK_MENU_BAR_GET_PRIVATE (menubar);
934
935   return priv->child_pack_direction;
936 }
937
938 /**
939  * gtk_menu_bar_set_child_pack_direction:
940  * @menubar: a #GtkMenuBar.
941  * @child_pack_dir: a new #GtkPackDirection.
942  * 
943  * Sets how widgets should be packed inside the children of a menubar.
944  * 
945  * Since: 2.8
946  **/
947 void gtk_menu_bar_set_child_pack_direction (GtkMenuBar       *menubar,
948                                             GtkPackDirection  child_pack_dir)
949 {
950   GtkMenuBarPrivate *priv;
951   GList *l;
952
953   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
954
955   priv = GTK_MENU_BAR_GET_PRIVATE (menubar);
956
957   if (priv->child_pack_direction != child_pack_dir)
958     {
959       priv->child_pack_direction = child_pack_dir;
960
961       gtk_widget_queue_resize (GTK_WIDGET (menubar));
962
963       for (l = GTK_MENU_SHELL (menubar)->children; l; l = l->next)
964         gtk_widget_queue_resize (GTK_WIDGET (l->data));
965
966       g_object_notify (G_OBJECT (menubar), "child-pack-direction");
967     }
968 }
969
970 #define __GTK_MENU_BAR_C__
971 #include "gtkaliasdef.c"