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