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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
19 #include "gdk/gdkkeysyms.h"
20 #include "gtkbindings.h"
22 #include "gtkmenubar.h"
23 #include "gtkmenuitem.h"
26 #define BORDER_SPACING 2
27 #define CHILD_SPACING 3
30 static void gtk_menu_bar_class_init (GtkMenuBarClass *klass);
31 static void gtk_menu_bar_init (GtkMenuBar *menu_bar);
32 static void gtk_menu_bar_size_request (GtkWidget *widget,
33 GtkRequisition *requisition);
34 static void gtk_menu_bar_size_allocate (GtkWidget *widget,
35 GtkAllocation *allocation);
36 static void gtk_menu_bar_paint (GtkWidget *widget);
37 static void gtk_menu_bar_draw (GtkWidget *widget,
39 static gint gtk_menu_bar_expose (GtkWidget *widget,
40 GdkEventExpose *event);
44 gtk_menu_bar_get_type (void)
46 static GtkType menu_bar_type = 0;
50 GtkTypeInfo menu_bar_info =
54 sizeof (GtkMenuBarClass),
55 (GtkClassInitFunc) gtk_menu_bar_class_init,
56 (GtkObjectInitFunc) gtk_menu_bar_init,
57 /* reserved_1 */ NULL,
58 /* reserved_2 */ NULL,
59 (GtkClassInitFunc) NULL,
62 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
69 gtk_menu_bar_class_init (GtkMenuBarClass *class)
71 GtkWidgetClass *widget_class;
72 GtkMenuShellClass *menu_shell_class;
74 GtkBindingSet *binding_set;
76 widget_class = (GtkWidgetClass*) class;
77 menu_shell_class = (GtkMenuShellClass*) class;
79 widget_class->draw = gtk_menu_bar_draw;
80 widget_class->size_request = gtk_menu_bar_size_request;
81 widget_class->size_allocate = gtk_menu_bar_size_allocate;
82 widget_class->expose_event = gtk_menu_bar_expose;
84 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
86 binding_set = gtk_binding_set_by_class (class);
87 gtk_binding_entry_add_signal (binding_set,
90 GTK_TYPE_MENU_DIRECTION_TYPE,
92 gtk_binding_entry_add_signal (binding_set,
95 GTK_TYPE_MENU_DIRECTION_TYPE,
97 gtk_binding_entry_add_signal (binding_set,
100 GTK_TYPE_MENU_DIRECTION_TYPE,
101 GTK_MENU_DIR_PARENT);
102 gtk_binding_entry_add_signal (binding_set,
105 GTK_TYPE_MENU_DIRECTION_TYPE,
110 gtk_menu_bar_init (GtkMenuBar *menu_bar)
115 gtk_menu_bar_new (void)
117 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
121 gtk_menu_bar_append (GtkMenuBar *menu_bar,
124 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
128 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
131 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
135 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
139 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
144 gtk_menu_bar_size_request (GtkWidget *widget,
145 GtkRequisition *requisition)
147 GtkMenuBar *menu_bar;
148 GtkMenuShell *menu_shell;
153 g_return_if_fail (widget != NULL);
154 g_return_if_fail (GTK_IS_MENU_BAR (widget));
155 g_return_if_fail (requisition != NULL);
157 requisition->width = 0;
158 requisition->height = 0;
160 if (GTK_WIDGET_VISIBLE (widget))
162 menu_bar = GTK_MENU_BAR (widget);
163 menu_shell = GTK_MENU_SHELL (widget);
166 children = menu_shell->children;
170 child = children->data;
171 children = children->next;
173 if (GTK_WIDGET_VISIBLE (child))
175 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
176 gtk_widget_size_request (child, &child->requisition);
178 requisition->width += child->requisition.width;
179 requisition->height = MAX (requisition->height, child->requisition.height);
180 /* Support for the right justified help menu */
181 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
182 && (GTK_MENU_ITEM(child)->right_justify))
184 requisition->width += CHILD_SPACING;
191 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
192 widget->style->klass->xthickness +
194 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
195 widget->style->klass->ythickness +
199 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
204 gtk_menu_bar_size_allocate (GtkWidget *widget,
205 GtkAllocation *allocation)
207 GtkMenuBar *menu_bar;
208 GtkMenuShell *menu_shell;
211 GtkAllocation child_allocation;
214 g_return_if_fail (widget != NULL);
215 g_return_if_fail (GTK_IS_MENU_BAR (widget));
216 g_return_if_fail (allocation != NULL);
218 menu_bar = GTK_MENU_BAR (widget);
219 menu_shell = GTK_MENU_SHELL (widget);
221 widget->allocation = *allocation;
222 if (GTK_WIDGET_REALIZED (widget))
223 gdk_window_move_resize (widget->window,
224 allocation->x, allocation->y,
225 allocation->width, allocation->height);
227 if (menu_shell->children)
229 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
230 widget->style->klass->xthickness +
232 offset = child_allocation.x; /* Window edge to menubar start */
234 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
235 widget->style->klass->ythickness +
237 child_allocation.height = MAX (1, allocation->height - child_allocation.y * 2);
239 children = menu_shell->children;
242 child = children->data;
243 children = children->next;
245 /* Support for the right justified help menu */
246 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
247 && (GTK_MENU_ITEM(child)->right_justify))
249 child_allocation.x = allocation->width -
250 child->requisition.width - CHILD_SPACING - offset;
252 if (GTK_WIDGET_VISIBLE (child))
254 child_allocation.width = child->requisition.width;
256 gtk_widget_size_allocate (child, &child_allocation);
258 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
265 gtk_menu_bar_paint (GtkWidget *widget)
267 g_return_if_fail (widget != NULL);
268 g_return_if_fail (GTK_IS_MENU_BAR (widget));
270 if (GTK_WIDGET_DRAWABLE (widget))
272 gtk_draw_shadow (widget->style,
277 widget->allocation.width,
278 widget->allocation.height);
283 gtk_menu_bar_draw (GtkWidget *widget,
286 GtkMenuShell *menu_shell;
288 GdkRectangle child_area;
291 g_return_if_fail (widget != NULL);
292 g_return_if_fail (GTK_IS_MENU_BAR (widget));
293 g_return_if_fail (area != NULL);
295 if (GTK_WIDGET_DRAWABLE (widget))
297 gtk_menu_bar_paint (widget);
299 menu_shell = GTK_MENU_SHELL (widget);
301 children = menu_shell->children;
304 child = children->data;
305 children = children->next;
307 if (gtk_widget_intersect (child, area, &child_area))
308 gtk_widget_draw (child, &child_area);
314 gtk_menu_bar_expose (GtkWidget *widget,
315 GdkEventExpose *event)
317 GtkMenuShell *menu_shell;
319 GdkEventExpose child_event;
322 g_return_val_if_fail (widget != NULL, FALSE);
323 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
324 g_return_val_if_fail (event != NULL, FALSE);
326 if (GTK_WIDGET_DRAWABLE (widget))
328 gtk_menu_bar_paint (widget);
330 menu_shell = GTK_MENU_SHELL (widget);
331 child_event = *event;
333 children = menu_shell->children;
336 child = children->data;
337 children = children->next;
339 if (GTK_WIDGET_NO_WINDOW (child) &&
340 gtk_widget_intersect (child, &event->area, &child_event.area))
341 gtk_widget_event (child, (GdkEvent*) &child_event);