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