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