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 #include "gdk/gdkkeysyms.h"
28 #include "gtkbindings.h"
30 #include "gtkmenubar.h"
31 #include "gtkmenuitem.h"
32 #include "gtksettings.h"
34 #include "gtkwindow.h"
35 #include "gtksignal.h"
43 #define BORDER_SPACING 0
44 #define CHILD_SPACING 3
47 static void gtk_menu_bar_class_init (GtkMenuBarClass *klass);
48 static void gtk_menu_bar_size_request (GtkWidget *widget,
49 GtkRequisition *requisition);
50 static void gtk_menu_bar_size_allocate (GtkWidget *widget,
51 GtkAllocation *allocation);
52 static void gtk_menu_bar_paint (GtkWidget *widget,
54 static gint gtk_menu_bar_expose (GtkWidget *widget,
55 GdkEventExpose *event);
56 static void gtk_menu_bar_hierarchy_changed (GtkWidget *widget);
57 static GtkShadowType get_shadow_type (GtkMenuBar *menubar);
59 static GtkMenuShellClass *parent_class = NULL;
62 gtk_menu_bar_get_type (void)
64 static GtkType menu_bar_type = 0;
68 static const GtkTypeInfo menu_bar_info =
72 sizeof (GtkMenuBarClass),
73 (GtkClassInitFunc) gtk_menu_bar_class_init,
74 (GtkObjectInitFunc) NULL,
75 /* reserved_1 */ NULL,
76 /* reserved_2 */ NULL,
77 (GtkClassInitFunc) NULL,
80 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
87 gtk_menu_bar_class_init (GtkMenuBarClass *class)
89 GtkObjectClass *object_class;
90 GtkWidgetClass *widget_class;
91 GtkMenuShellClass *menu_shell_class;
93 GtkBindingSet *binding_set;
95 parent_class = g_type_class_peek_parent (class);
97 object_class = (GtkObjectClass*) class;
98 widget_class = (GtkWidgetClass*) class;
99 menu_shell_class = (GtkMenuShellClass*) class;
101 widget_class->size_request = gtk_menu_bar_size_request;
102 widget_class->size_allocate = gtk_menu_bar_size_allocate;
103 widget_class->expose_event = gtk_menu_bar_expose;
104 widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
106 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
108 binding_set = gtk_binding_set_by_class (class);
109 gtk_binding_entry_add_signal (binding_set,
112 GTK_TYPE_MENU_DIRECTION_TYPE,
114 gtk_binding_entry_add_signal (binding_set,
117 GTK_TYPE_MENU_DIRECTION_TYPE,
119 gtk_binding_entry_add_signal (binding_set,
122 GTK_TYPE_MENU_DIRECTION_TYPE,
123 GTK_MENU_DIR_PARENT);
124 gtk_binding_entry_add_signal (binding_set,
127 GTK_TYPE_MENU_DIRECTION_TYPE,
130 gtk_settings_install_property (gtk_settings_get_global (),
131 g_param_spec_string ("gtk-menu-bar-accel",
132 _("Menu bar accelerator"),
133 _("Keybinding to activate the menu bar"),
137 gtk_widget_class_install_style_property (widget_class,
138 g_param_spec_enum ("shadow_type",
140 _("Style of bevel around the menubar"),
141 GTK_TYPE_SHADOW_TYPE,
147 gtk_menu_bar_new (void)
149 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
153 gtk_menu_bar_append (GtkMenuBar *menu_bar,
156 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
160 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
163 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
167 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
171 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
176 gtk_menu_bar_size_request (GtkWidget *widget,
177 GtkRequisition *requisition)
179 GtkMenuBar *menu_bar;
180 GtkMenuShell *menu_shell;
184 GtkRequisition child_requisition;
186 g_return_if_fail (widget != NULL);
187 g_return_if_fail (GTK_IS_MENU_BAR (widget));
188 g_return_if_fail (requisition != NULL);
190 requisition->width = 0;
191 requisition->height = 0;
193 if (GTK_WIDGET_VISIBLE (widget))
195 menu_bar = GTK_MENU_BAR (widget);
196 menu_shell = GTK_MENU_SHELL (widget);
199 children = menu_shell->children;
203 child = children->data;
204 children = children->next;
206 if (GTK_WIDGET_VISIBLE (child))
210 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
211 gtk_widget_size_request (child, &child_requisition);
212 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
215 requisition->width += child_requisition.width;
216 requisition->width += toggle_size;
218 requisition->height = MAX (requisition->height, child_requisition.height);
219 /* Support for the right justified help menu */
220 if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
221 GTK_MENU_ITEM(child)->right_justify)
223 requisition->width += CHILD_SPACING;
230 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
231 widget->style->xthickness +
233 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
234 widget->style->ythickness +
238 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
243 gtk_menu_bar_size_allocate (GtkWidget *widget,
244 GtkAllocation *allocation)
246 GtkMenuBar *menu_bar;
247 GtkMenuShell *menu_shell;
250 GtkAllocation child_allocation;
251 GtkRequisition child_requisition;
254 g_return_if_fail (widget != NULL);
255 g_return_if_fail (GTK_IS_MENU_BAR (widget));
256 g_return_if_fail (allocation != NULL);
258 menu_bar = GTK_MENU_BAR (widget);
259 menu_shell = GTK_MENU_SHELL (widget);
261 widget->allocation = *allocation;
262 if (GTK_WIDGET_REALIZED (widget))
263 gdk_window_move_resize (widget->window,
264 allocation->x, allocation->y,
265 allocation->width, allocation->height);
267 if (menu_shell->children)
269 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
270 widget->style->xthickness +
272 offset = child_allocation.x; /* Window edge to menubar start */
274 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
275 widget->style->ythickness +
277 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
279 children = menu_shell->children;
284 child = children->data;
285 children = children->next;
287 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
289 gtk_widget_get_child_requisition (child, &child_requisition);
291 child_requisition.width += toggle_size;
293 /* Support for the right justified help menu */
294 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
295 && (GTK_MENU_ITEM(child)->right_justify))
297 child_allocation.x = allocation->width -
298 child_requisition.width - CHILD_SPACING - offset;
300 if (GTK_WIDGET_VISIBLE (child))
302 child_allocation.width = child_requisition.width;
304 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
306 gtk_widget_size_allocate (child, &child_allocation);
308 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
315 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
317 g_return_if_fail (widget != NULL);
318 g_return_if_fail (GTK_IS_MENU_BAR (widget));
320 if (GTK_WIDGET_DRAWABLE (widget))
322 gtk_paint_box (widget->style,
325 get_shadow_type (GTK_MENU_BAR (widget)),
326 area, widget, "menubar",
333 gtk_menu_bar_expose (GtkWidget *widget,
334 GdkEventExpose *event)
336 g_return_val_if_fail (widget != NULL, FALSE);
337 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
338 g_return_val_if_fail (event != NULL, FALSE);
340 if (GTK_WIDGET_DRAWABLE (widget))
342 gtk_menu_bar_paint (widget, &event->area);
344 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
351 window_key_press_handler (GtkWidget *widget,
356 gboolean retval = FALSE;
358 g_object_get (G_OBJECT (gtk_settings_get_global ()),
359 "gtk-menu-bar-accel",
366 GdkModifierType mods = 0;
368 gtk_accelerator_parse (accel, &keyval, &mods);
371 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
373 /* FIXME this is wrong, needs to be in the global accel resolution
374 * thing, to properly consider i18n etc., but that probably requires
375 * AccelGroup changes etc.
377 if (event->keyval == keyval &&
378 (mods & event->state) == mods)
381 GtkMenuShell *menushell;
383 menubar = GTK_MENU_BAR (data);
384 menushell = GTK_MENU_SHELL (menubar);
386 if (menushell->children)
388 gtk_signal_emit_by_name (GTK_OBJECT (menushell->children->data),
402 add_to_window (GtkWindow *window,
405 GtkMenuBar *old_menubar;
407 old_menubar = g_object_get_data (G_OBJECT (window),
411 return; /* ignore this case; app programmer on crack, but
412 * shouldn't spew stuff, just don't support the accel
416 g_object_set_data (G_OBJECT (window),
420 g_signal_connect_data (G_OBJECT (window),
422 G_CALLBACK (window_key_press_handler),
426 menubar->toplevel = GTK_WIDGET (window);
430 #define g_signal_handlers_disconnect_by_func(obj, func, data) g_signal_handlers_disconnect_matched (obj, G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0, NULL, func, data)
433 remove_from_window (GtkWindow *window,
436 g_return_if_fail (menubar->toplevel == GTK_WIDGET (window));
438 g_signal_handlers_disconnect_by_func (G_OBJECT (window),
439 G_CALLBACK (window_key_press_handler),
442 /* dnotify zeroes menubar->toplevel */
443 g_object_set_data (G_OBJECT (window),
447 menubar->toplevel = NULL;
451 gtk_menu_bar_hierarchy_changed (GtkWidget *widget)
456 menubar = GTK_MENU_BAR (widget);
458 toplevel = gtk_widget_get_toplevel (widget);
460 if (menubar->toplevel &&
461 toplevel != menubar->toplevel)
463 remove_from_window (GTK_WINDOW (menubar->toplevel),
468 GTK_IS_WINDOW (toplevel))
470 add_to_window (GTK_WINDOW (toplevel),
476 get_shadow_type (GtkMenuBar *menubar)
478 GtkShadowType shadow_type = GTK_SHADOW_OUT;
480 gtk_widget_style_get (GTK_WIDGET (menubar),
481 "shadow_type", &shadow_type,