1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #define GTK_MENU_INTERNALS
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
32 #include "gtkmarshalers.h"
33 #include "gtkmenubar.h"
34 #include "gtkmenuitem.h"
35 #include "gtksettings.h"
37 #include "gtkwindow.h"
38 #include "gtksignal.h"
41 #define BORDER_SPACING 0
42 #define CHILD_SPACING 3
43 #define DEFAULT_IPADDING 1
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,
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);
58 static GtkMenuShellClass *parent_class = NULL;
61 gtk_menu_bar_get_type (void)
63 static GtkType menu_bar_type = 0;
67 static const GtkTypeInfo menu_bar_info =
71 sizeof (GtkMenuBarClass),
72 (GtkClassInitFunc) gtk_menu_bar_class_init,
73 (GtkObjectInitFunc) NULL,
74 /* reserved_1 */ NULL,
75 /* reserved_2 */ NULL,
76 (GtkClassInitFunc) NULL,
79 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
86 gtk_menu_bar_class_init (GtkMenuBarClass *class)
88 GtkObjectClass *object_class;
89 GtkWidgetClass *widget_class;
90 GtkMenuShellClass *menu_shell_class;
92 GtkBindingSet *binding_set;
94 parent_class = g_type_class_peek_parent (class);
96 object_class = (GtkObjectClass*) class;
97 widget_class = (GtkWidgetClass*) class;
98 menu_shell_class = (GtkMenuShellClass*) class;
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;
105 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
107 binding_set = gtk_binding_set_by_class (class);
108 gtk_binding_entry_add_signal (binding_set,
111 GTK_TYPE_MENU_DIRECTION_TYPE,
113 gtk_binding_entry_add_signal (binding_set,
116 GTK_TYPE_MENU_DIRECTION_TYPE,
118 gtk_binding_entry_add_signal (binding_set,
121 GTK_TYPE_MENU_DIRECTION_TYPE,
123 gtk_binding_entry_add_signal (binding_set,
126 GTK_TYPE_MENU_DIRECTION_TYPE,
128 gtk_binding_entry_add_signal (binding_set,
131 GTK_TYPE_MENU_DIRECTION_TYPE,
132 GTK_MENU_DIR_PARENT);
133 gtk_binding_entry_add_signal (binding_set,
136 GTK_TYPE_MENU_DIRECTION_TYPE,
137 GTK_MENU_DIR_PARENT);
138 gtk_binding_entry_add_signal (binding_set,
141 GTK_TYPE_MENU_DIRECTION_TYPE,
143 gtk_binding_entry_add_signal (binding_set,
146 GTK_TYPE_MENU_DIRECTION_TYPE,
149 gtk_widget_class_install_style_property (widget_class,
150 g_param_spec_enum ("shadow_type",
152 _("Style of bevel around the menubar"),
153 GTK_TYPE_SHADOW_TYPE,
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"),
169 gtk_menu_bar_new (void)
171 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
175 gtk_menu_bar_size_request (GtkWidget *widget,
176 GtkRequisition *requisition)
178 GtkMenuBar *menu_bar;
179 GtkMenuShell *menu_shell;
183 GtkRequisition child_requisition;
186 g_return_if_fail (GTK_IS_MENU_BAR (widget));
187 g_return_if_fail (requisition != NULL);
189 requisition->width = 0;
190 requisition->height = 0;
192 if (GTK_WIDGET_VISIBLE (widget))
194 menu_bar = GTK_MENU_BAR (widget);
195 menu_shell = GTK_MENU_SHELL (widget);
198 children = menu_shell->children;
202 child = children->data;
203 children = children->next;
205 if (GTK_WIDGET_VISIBLE (child))
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),
214 requisition->width += child_requisition.width;
215 requisition->width += toggle_size;
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)
222 requisition->width += CHILD_SPACING;
229 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
231 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
234 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
238 if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
240 requisition->width += widget->style->xthickness * 2;
241 requisition->height += widget->style->ythickness * 2;
245 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
250 gtk_menu_bar_size_allocate (GtkWidget *widget,
251 GtkAllocation *allocation)
253 GtkMenuBar *menu_bar;
254 GtkMenuShell *menu_shell;
257 GtkAllocation child_allocation;
258 GtkRequisition child_requisition;
262 g_return_if_fail (GTK_IS_MENU_BAR (widget));
263 g_return_if_fail (allocation != NULL);
265 menu_bar = GTK_MENU_BAR (widget);
266 menu_shell = GTK_MENU_SHELL (widget);
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);
274 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
276 if (menu_shell->children)
278 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
281 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
285 if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
287 child_allocation.x += widget->style->xthickness;
288 child_allocation.y += widget->style->ythickness;
291 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
293 offset = child_allocation.x; /* Window edge to menubar start */
295 children = menu_shell->children;
300 child = children->data;
301 children = children->next;
303 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
305 gtk_widget_get_child_requisition (child, &child_requisition);
307 child_requisition.width += toggle_size;
309 /* Support for the right justified help menu */
310 if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
311 && (GTK_MENU_ITEM(child)->right_justify))
313 child_allocation.x = allocation->width -
314 child_requisition.width - offset;
316 if (GTK_WIDGET_VISIBLE (child))
318 child_allocation.width = child_requisition.width;
320 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
322 gtk_widget_size_allocate (child, &child_allocation);
324 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
331 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
333 g_return_if_fail (GTK_IS_MENU_BAR (widget));
335 if (GTK_WIDGET_DRAWABLE (widget))
339 border = GTK_CONTAINER (widget)->border_width;
341 gtk_paint_box (widget->style,
343 GTK_WIDGET_STATE (widget),
344 get_shadow_type (GTK_MENU_BAR (widget)),
345 area, widget, "menubar",
347 widget->allocation.width - border * 2,
348 widget->allocation.height - border * 2);
353 gtk_menu_bar_expose (GtkWidget *widget,
354 GdkEventExpose *event)
356 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
357 g_return_val_if_fail (event != NULL, FALSE);
359 if (GTK_WIDGET_DRAWABLE (widget))
361 gtk_menu_bar_paint (widget, &event->area);
363 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
370 get_menu_bars (GtkWindow *window)
372 return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
376 get_viewable_menu_bars (GtkWindow *window)
379 GList *viewable_menu_bars = NULL;
381 for (menu_bars = get_menu_bars (window);
383 menu_bars = menu_bars->next)
385 GtkWidget *widget = menu_bars->data;
386 gboolean viewable = TRUE;
390 if (!GTK_WIDGET_MAPPED (widget))
393 widget = widget->parent;
397 viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
400 return g_list_reverse (viewable_menu_bars);
404 set_menu_bars (GtkWindow *window,
407 g_object_set_data (G_OBJECT (window), "gtk-menu-bar-list", menubars);
411 window_key_press_handler (GtkWidget *widget,
416 gboolean retval = FALSE;
418 g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
419 "gtk-menu-bar-accel",
426 GdkModifierType mods = 0;
428 gtk_accelerator_parse (accel, &keyval, &mods);
431 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
433 /* FIXME this is wrong, needs to be in the global accel resolution
434 * thing, to properly consider i18n etc., but that probably requires
435 * AccelGroup changes etc.
437 if (event->keyval == keyval &&
438 ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
439 (mods & gtk_accelerator_get_default_mod_mask ())))
441 GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
444 menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
445 GTK_DIR_TAB_FORWARD, NULL);
446 g_list_free (tmp_menubars);
450 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
452 _gtk_menu_shell_activate (menu_shell);
453 _gtk_menu_shell_select_first (menu_shell, FALSE);
455 g_list_free (menubars);
468 add_to_window (GtkWindow *window,
471 GList *menubars = get_menu_bars (window);
475 g_signal_connect (G_OBJECT (window),
477 G_CALLBACK (window_key_press_handler),
481 set_menu_bars (window, g_list_prepend (menubars, menubar));
485 remove_from_window (GtkWindow *window,
488 GList *menubars = get_menu_bars (window);
490 menubars = g_object_get_data (G_OBJECT (window),
491 "gtk-menu-bar-list");
493 menubars = g_list_remove (menubars, menubar);
497 g_signal_handlers_disconnect_by_func (G_OBJECT (window),
498 (gpointer) window_key_press_handler,
502 set_menu_bars (window, menubars);
506 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
507 GtkWidget *old_toplevel)
512 menubar = GTK_MENU_BAR (widget);
514 toplevel = gtk_widget_get_toplevel (widget);
517 remove_from_window (GTK_WINDOW (old_toplevel), menubar);
519 if (GTK_WIDGET_TOPLEVEL (toplevel))
520 add_to_window (GTK_WINDOW (toplevel), menubar);
524 * _gtk_menu_bar_cycle_focus:
525 * @menubar: a #GtkMenuBar
526 * @dir: direction in which to cycle the focus
528 * Move the focus between menubars in the toplevel.
531 _gtk_menu_bar_cycle_focus (GtkMenuBar *menubar,
532 GtkDirectionType dir)
534 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
536 if (GTK_WIDGET_TOPLEVEL (toplevel))
538 GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
543 menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
544 dir, GTK_WIDGET (menubar));
545 g_list_free (tmp_menubars);
549 current = g_list_find (menubars, menubar);
550 if (current && current->next)
551 new = current->next->data;
553 new = menubars->data;
557 GtkMenuShell *new_menushell = GTK_MENU_SHELL (new);
559 if (new_menushell->children)
561 g_signal_emit_by_name (menubar, "cancel", 0);
562 gtk_signal_emit_by_name (GTK_OBJECT (new_menushell->children->data),
568 g_list_free (menubars);
573 get_shadow_type (GtkMenuBar *menubar)
575 GtkShadowType shadow_type = GTK_SHADOW_OUT;
577 gtk_widget_style_get (GTK_WIDGET (menubar),
578 "shadow_type", &shadow_type,