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 set_menu_bars (GtkWindow *window,
379 g_object_set_data (G_OBJECT (window), "gtk-menu-bar-list", menubars);
383 window_key_press_handler (GtkWidget *widget,
388 gboolean retval = FALSE;
390 g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
391 "gtk-menu-bar-accel",
398 GdkModifierType mods = 0;
400 gtk_accelerator_parse (accel, &keyval, &mods);
403 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
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.
409 if (event->keyval == keyval &&
410 ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
411 (mods & gtk_accelerator_get_default_mod_mask ())))
413 GList *menubars = get_menu_bars (GTK_WINDOW (widget));
415 menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), menubars,
416 GTK_DIR_TAB_FORWARD, NULL);
419 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
421 _gtk_menu_shell_activate (menu_shell);
422 _gtk_menu_shell_select_first (menu_shell);
424 g_list_free (menubars);
437 add_to_window (GtkWindow *window,
440 GList *menubars = get_menu_bars (window);
444 g_signal_connect (G_OBJECT (window),
446 G_CALLBACK (window_key_press_handler),
450 set_menu_bars (window, g_list_prepend (menubars, menubar));
454 remove_from_window (GtkWindow *window,
457 GList *menubars = get_menu_bars (window);
459 menubars = g_object_get_data (G_OBJECT (window),
460 "gtk-menu-bar-list");
462 menubars = g_list_remove (menubars, menubar);
466 g_signal_handlers_disconnect_by_func (G_OBJECT (window),
467 (gpointer) window_key_press_handler,
471 set_menu_bars (window, menubars);
475 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
476 GtkWidget *old_toplevel)
481 menubar = GTK_MENU_BAR (widget);
483 toplevel = gtk_widget_get_toplevel (widget);
486 remove_from_window (GTK_WINDOW (old_toplevel), menubar);
488 if (GTK_WIDGET_TOPLEVEL (toplevel))
489 add_to_window (GTK_WINDOW (toplevel), menubar);
493 * _gtk_menu_bar_cycle_focus:
494 * @menubar: a #GtkMenuBar
495 * @dir: direction in which to cycle the focus
497 * Move the focus between menubars in the toplevel.
500 _gtk_menu_bar_cycle_focus (GtkMenuBar *menubar,
501 GtkDirectionType dir)
503 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
505 if (GTK_WIDGET_TOPLEVEL (toplevel))
507 GList *menubars = get_menu_bars (GTK_WINDOW (toplevel));
511 menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), menubars,
512 dir, GTK_WIDGET (menubar));
516 current = g_list_find (menubars, menubar);
517 if (current && current->next)
518 new = current->next->data;
520 new = menubars->data;
524 GtkMenuShell *new_menushell = GTK_MENU_SHELL (new);
526 if (new_menushell->children)
528 g_signal_emit_by_name (menubar, "cancel", 0);
529 gtk_signal_emit_by_name (GTK_OBJECT (new_menushell->children->data),
535 g_list_free (menubars);
540 get_shadow_type (GtkMenuBar *menubar)
542 GtkShadowType shadow_type = GTK_SHADOW_OUT;
544 gtk_widget_style_get (GTK_WIDGET (menubar),
545 "shadow_type", &shadow_type,