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