]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
New function to emit the "cancel" signal on a menu shell.
[~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 "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
31 #include "gtkmain.h"
32 #include "gtkmarshalers.h"
33 #include "gtkmenubar.h"
34 #include "gtkmenuitem.h"
35 #include "gtksettings.h"
36 #include "gtkintl.h"
37 #include "gtkwindow.h"
38
39
40 #define BORDER_SPACING  0
41 #define CHILD_SPACING   3
42 #define DEFAULT_IPADDING 1
43
44 static void gtk_menu_bar_class_init        (GtkMenuBarClass *klass);
45 static void gtk_menu_bar_size_request      (GtkWidget       *widget,
46                                             GtkRequisition  *requisition);
47 static void gtk_menu_bar_size_allocate     (GtkWidget       *widget,
48                                             GtkAllocation   *allocation);
49 static void gtk_menu_bar_paint             (GtkWidget       *widget,
50                                             GdkRectangle    *area);
51 static gint gtk_menu_bar_expose            (GtkWidget       *widget,
52                                             GdkEventExpose  *event);
53 static void gtk_menu_bar_hierarchy_changed (GtkWidget       *widget,
54                                             GtkWidget       *old_toplevel);
55 static gint gtk_menu_bar_get_popup_delay  (GtkMenuShell    *menu_shell);
56                                             
57
58 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
59
60 static GtkMenuShellClass *parent_class = NULL;
61
62 GType
63 gtk_menu_bar_get_type (void)
64 {
65   static GType menu_bar_type = 0;
66
67   if (!menu_bar_type)
68     {
69       static const GTypeInfo menu_bar_info =
70       {
71         sizeof (GtkMenuBarClass),
72         NULL,           /* base_init */
73         NULL,           /* base_finalize */
74         (GClassInitFunc) gtk_menu_bar_class_init,
75         NULL,           /* class_finalize */
76         NULL,           /* class_data */
77         sizeof (GtkMenuBar),
78         0,              /* n_preallocs */
79         NULL,           /* instance_init */
80       };
81
82       menu_bar_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenuBar",
83                                               &menu_bar_info, 0);
84     }
85
86   return menu_bar_type;
87 }
88
89 static void
90 gtk_menu_bar_class_init (GtkMenuBarClass *class)
91 {
92   GtkObjectClass *object_class;
93   GtkWidgetClass *widget_class;
94   GtkMenuShellClass *menu_shell_class;
95
96   GtkBindingSet *binding_set;
97
98   parent_class = g_type_class_peek_parent (class);
99   
100   object_class = (GtkObjectClass*) class;
101   widget_class = (GtkWidgetClass*) class;
102   menu_shell_class = (GtkMenuShellClass*) class;
103
104   widget_class->size_request = gtk_menu_bar_size_request;
105   widget_class->size_allocate = gtk_menu_bar_size_allocate;
106   widget_class->expose_event = gtk_menu_bar_expose;
107   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
108   
109   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
110   menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
111
112   binding_set = gtk_binding_set_by_class (class);
113   gtk_binding_entry_add_signal (binding_set,
114                                 GDK_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_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_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_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_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_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_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_KP_Down, 0,
150                                 "move_current", 1,
151                                 GTK_TYPE_MENU_DIRECTION_TYPE,
152                                 GTK_MENU_DIR_CHILD);
153
154   gtk_widget_class_install_style_property (widget_class,
155                                            g_param_spec_enum ("shadow_type",
156                                                               _("Shadow type"),
157                                                               _("Style of bevel around the menubar"),
158                                                               GTK_TYPE_SHADOW_TYPE,
159                                                               GTK_SHADOW_OUT,
160                                                               G_PARAM_READABLE));
161
162   gtk_widget_class_install_style_property (widget_class,
163                                            g_param_spec_int ("internal_padding",
164                                                              _("Internal padding"),
165                                                              _("Amount of border space between the menubar shadow and the menu items"),
166                                                              0,
167                                                              G_MAXINT,
168                                                              DEFAULT_IPADDING,
169                                                              G_PARAM_READABLE));
170
171   gtk_settings_install_property (g_param_spec_int ("gtk-menu-bar-popup-delay",
172                                                    _("Delay before drop down menus appear"),
173                                                    _("Delay before the submenus of a menu bar appear"),
174                                                    0,
175                                                    G_MAXINT,
176                                                    0,
177                                                    G_PARAM_READWRITE));
178 }
179  
180 GtkWidget*
181 gtk_menu_bar_new (void)
182 {
183   return g_object_new (GTK_TYPE_MENU_BAR, NULL);
184 }
185
186 static void
187 gtk_menu_bar_size_request (GtkWidget      *widget,
188                            GtkRequisition *requisition)
189 {
190   GtkMenuBar *menu_bar;
191   GtkMenuShell *menu_shell;
192   GtkWidget *child;
193   GList *children;
194   gint nchildren;
195   GtkRequisition child_requisition;
196   gint ipadding;
197
198   g_return_if_fail (GTK_IS_MENU_BAR (widget));
199   g_return_if_fail (requisition != NULL);
200
201   requisition->width = 0;
202   requisition->height = 0;
203   
204   if (GTK_WIDGET_VISIBLE (widget))
205     {
206       menu_bar = GTK_MENU_BAR (widget);
207       menu_shell = GTK_MENU_SHELL (widget);
208
209       nchildren = 0;
210       children = menu_shell->children;
211
212       while (children)
213         {
214           child = children->data;
215           children = children->next;
216
217           if (GTK_WIDGET_VISIBLE (child))
218             {
219               gint toggle_size;
220               
221               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
222               gtk_widget_size_request (child, &child_requisition);
223               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
224                                                  &toggle_size);
225               
226               requisition->width += child_requisition.width;
227               requisition->width += toggle_size;
228               
229               requisition->height = MAX (requisition->height, child_requisition.height);
230               /* Support for the right justified help menu */
231               if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
232                   GTK_MENU_ITEM(child)->right_justify)
233                 {
234                   requisition->width += CHILD_SPACING;
235                 }
236
237               nchildren += 1;
238             }
239         }
240
241       gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
242       
243       requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
244                              ipadding + 
245                              BORDER_SPACING) * 2;
246       requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
247                               ipadding +
248                               BORDER_SPACING) * 2;
249
250       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
251         {
252           requisition->width += widget->style->xthickness * 2;
253           requisition->height += widget->style->ythickness * 2;
254         }
255
256       if (nchildren > 0)
257         requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
258     }
259 }
260
261 static void
262 gtk_menu_bar_size_allocate (GtkWidget     *widget,
263                             GtkAllocation *allocation)
264 {
265   GtkMenuBar *menu_bar;
266   GtkMenuShell *menu_shell;
267   GtkWidget *child;
268   GList *children;
269   GtkAllocation child_allocation;
270   GtkRequisition child_requisition;
271   guint offset;
272   gint ipadding;
273   GtkTextDirection direction;
274   gint ltr_x;
275
276   g_return_if_fail (GTK_IS_MENU_BAR (widget));
277   g_return_if_fail (allocation != NULL);
278
279   menu_bar = GTK_MENU_BAR (widget);
280   menu_shell = GTK_MENU_SHELL (widget);
281
282   direction = gtk_widget_get_direction (widget);
283
284   widget->allocation = *allocation;
285   if (GTK_WIDGET_REALIZED (widget))
286     gdk_window_move_resize (widget->window,
287                             allocation->x, allocation->y,
288                             allocation->width, allocation->height);
289
290   gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
291   
292   if (menu_shell->children)
293     {
294       child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
295                             ipadding + 
296                             BORDER_SPACING);
297       child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
298                             ipadding +
299                             BORDER_SPACING);
300
301       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
302         {
303           child_allocation.x += widget->style->xthickness;
304           child_allocation.y += widget->style->ythickness;
305         }
306       
307       child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
308
309       offset = child_allocation.x;      /* Window edge to menubar start */
310       ltr_x = child_allocation.x;
311
312       children = menu_shell->children;
313       while (children)
314         {
315           gint toggle_size;          
316
317           child = children->data;
318           children = children->next;
319
320           gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
321                                              &toggle_size);
322           gtk_widget_get_child_requisition (child, &child_requisition);
323
324           child_requisition.width += toggle_size;
325           
326           /* Support for the right justified help menu */
327           if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
328               && (GTK_MENU_ITEM(child)->right_justify)) 
329             {
330               ltr_x = allocation->width -
331                 child_requisition.width - offset;
332             }
333           if (GTK_WIDGET_VISIBLE (child))
334             {
335               if (direction == GTK_TEXT_DIR_LTR) 
336                 child_allocation.x = ltr_x;
337               else
338                 child_allocation.x = allocation->width -
339                   child_requisition.width - ltr_x; 
340
341               child_allocation.width = child_requisition.width;
342
343               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
344                                                   toggle_size);
345               gtk_widget_size_allocate (child, &child_allocation);
346
347               ltr_x += child_allocation.width + CHILD_SPACING * 2;
348             }
349         }
350     }
351 }
352
353 static void
354 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
355 {
356   g_return_if_fail (GTK_IS_MENU_BAR (widget));
357
358   if (GTK_WIDGET_DRAWABLE (widget))
359     {
360       gint border;
361
362       border = GTK_CONTAINER (widget)->border_width;
363       
364       gtk_paint_box (widget->style,
365                      widget->window,
366                      GTK_WIDGET_STATE (widget),
367                      get_shadow_type (GTK_MENU_BAR (widget)),
368                      area, widget, "menubar",
369                      border, border,
370                      widget->allocation.width - border * 2,
371                      widget->allocation.height - border * 2);
372     }
373 }
374
375 static gint
376 gtk_menu_bar_expose (GtkWidget      *widget,
377                      GdkEventExpose *event)
378 {
379   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
380   g_return_val_if_fail (event != NULL, FALSE);
381
382   if (GTK_WIDGET_DRAWABLE (widget))
383     {
384       gtk_menu_bar_paint (widget, &event->area);
385
386       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
387     }
388
389   return FALSE;
390 }
391
392 static GList *
393 get_menu_bars (GtkWindow *window)
394 {
395   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
396 }
397
398 static GList *
399 get_viewable_menu_bars (GtkWindow *window)
400 {
401   GList *menu_bars;
402   GList *viewable_menu_bars = NULL;
403
404   for (menu_bars = get_menu_bars (window);
405        menu_bars;
406        menu_bars = menu_bars->next)
407     {
408       GtkWidget *widget = menu_bars->data;
409       gboolean viewable = TRUE;
410       
411       while (widget)
412         {
413           if (!GTK_WIDGET_MAPPED (widget))
414             viewable = FALSE;
415           
416           widget = widget->parent;
417         }
418
419       if (viewable)
420         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
421     }
422
423   return g_list_reverse (viewable_menu_bars);
424 }
425
426 static void
427 set_menu_bars (GtkWindow *window,
428                GList     *menubars)
429 {
430   g_object_set_data (G_OBJECT (window), "gtk-menu-bar-list", menubars);
431 }
432
433 static gboolean
434 window_key_press_handler (GtkWidget   *widget,
435                           GdkEventKey *event,
436                           gpointer     data)
437 {
438   gchar *accel = NULL;
439   gboolean retval = FALSE;
440   
441   g_object_get (gtk_widget_get_settings (widget),
442                 "gtk-menu-bar-accel",
443                 &accel,
444                 NULL);
445
446   if (accel)
447     {
448       guint keyval = 0;
449       GdkModifierType mods = 0;
450
451       gtk_accelerator_parse (accel, &keyval, &mods);
452
453       if (keyval == 0)
454         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
455
456       /* FIXME this is wrong, needs to be in the global accel resolution
457        * thing, to properly consider i18n etc., but that probably requires
458        * AccelGroup changes etc.
459        */
460       if (event->keyval == keyval &&
461           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
462            (mods & gtk_accelerator_get_default_mod_mask ())))
463         {
464           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
465           GList *menubars;
466
467           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
468                                                 GTK_DIR_TAB_FORWARD, NULL);
469           g_list_free (tmp_menubars);
470           
471           if (menubars)
472             {
473               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
474
475               _gtk_menu_shell_activate (menu_shell);
476               gtk_menu_shell_select_first (menu_shell, FALSE);
477               
478               g_list_free (menubars);
479               
480               retval = TRUE;          
481             }
482         }
483
484       g_free (accel);
485     }
486
487   return retval;
488 }
489
490 static void
491 add_to_window (GtkWindow  *window,
492                GtkMenuBar *menubar)
493 {
494   GList *menubars = get_menu_bars (window);
495
496   if (!menubars)
497     {
498       g_signal_connect (window,
499                         "key_press_event",
500                         G_CALLBACK (window_key_press_handler),
501                         NULL);
502     }
503
504   set_menu_bars (window, g_list_prepend (menubars, menubar));
505 }
506
507 static void
508 remove_from_window (GtkWindow  *window,
509                     GtkMenuBar *menubar)
510 {
511   GList *menubars = get_menu_bars (window);
512
513   menubars = g_object_get_data (G_OBJECT (window),
514                                     "gtk-menu-bar-list");
515
516   menubars = g_list_remove (menubars, menubar);
517
518   if (!menubars)
519     {
520       g_signal_handlers_disconnect_by_func (window,
521                                             window_key_press_handler,
522                                             NULL);
523     }
524
525   set_menu_bars (window, menubars);
526 }
527
528 static void
529 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
530                                 GtkWidget *old_toplevel)
531 {
532   GtkWidget *toplevel;  
533   GtkMenuBar *menubar;
534
535   menubar = GTK_MENU_BAR (widget);
536
537   toplevel = gtk_widget_get_toplevel (widget);
538
539   if (old_toplevel)
540     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
541   
542   if (GTK_WIDGET_TOPLEVEL (toplevel))
543     add_to_window (GTK_WINDOW (toplevel), menubar);
544 }
545
546 /**
547  * _gtk_menu_bar_cycle_focus:
548  * @menubar: a #GtkMenuBar
549  * @dir: direction in which to cycle the focus
550  * 
551  * Move the focus between menubars in the toplevel.
552  **/
553 void
554 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
555                            GtkDirectionType  dir)
556 {
557   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
558   GtkMenuItem *to_activate = NULL;
559
560   if (GTK_WIDGET_TOPLEVEL (toplevel))
561     {
562       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
563       GList *menubars;
564       GList *current;
565
566       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
567                                             dir, GTK_WIDGET (menubar));
568       g_list_free (tmp_menubars);
569
570       if (menubars)
571         {
572           current = g_list_find (menubars, menubar);
573
574           if (current && current->next)
575             {
576               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
577               if (new_menushell->children)
578                 to_activate = new_menushell->children->data;
579             }
580         }
581           
582       g_list_free (menubars);
583     }
584
585   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
586
587   if (to_activate)
588     g_signal_emit_by_name (to_activate, "activate_item");
589 }
590
591 static GtkShadowType
592 get_shadow_type (GtkMenuBar *menubar)
593 {
594   GtkShadowType shadow_type = GTK_SHADOW_OUT;
595   
596   gtk_widget_style_get (GTK_WIDGET (menubar),
597                         "shadow_type", &shadow_type,
598                         NULL);
599
600   return shadow_type;
601 }
602
603 static gint
604 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
605 {
606   gint popup_delay;
607   
608   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
609                 "gtk-menu-bar-popup-delay", &popup_delay,
610                 NULL);
611
612   return popup_delay;
613 }