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,
38 static void gtk_menu_bar_draw (GtkWidget *widget,
40 static gint gtk_menu_bar_expose (GtkWidget *widget,
41 GdkEventExpose *event);
45 gtk_menu_bar_get_type (void)
47 static GtkType menu_bar_type = 0;
51 GtkTypeInfo menu_bar_info =
55 sizeof (GtkMenuBarClass),
56 (GtkClassInitFunc) gtk_menu_bar_class_init,
57 (GtkObjectInitFunc) gtk_menu_bar_init,
58 /* reserved_1 */ NULL,
59 /* reserved_2 */ NULL,
60 (GtkClassInitFunc) NULL,
63 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
70 gtk_menu_bar_class_init (GtkMenuBarClass *class)
72 GtkWidgetClass *widget_class;
73 GtkMenuShellClass *menu_shell_class;
75 GtkBindingSet *binding_set;
77 widget_class = (GtkWidgetClass*) class;
78 menu_shell_class = (GtkMenuShellClass*) class;
80 widget_class->draw = gtk_menu_bar_draw;
81 widget_class->size_request = gtk_menu_bar_size_request;
82 widget_class->size_allocate = gtk_menu_bar_size_allocate;
83 widget_class->expose_event = gtk_menu_bar_expose;
85 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
87 binding_set = gtk_binding_set_by_class (class);
88 gtk_binding_entry_add_signal (binding_set,
91 GTK_TYPE_MENU_DIRECTION_TYPE,
93 gtk_binding_entry_add_signal (binding_set,
96 GTK_TYPE_MENU_DIRECTION_TYPE,
98 gtk_binding_entry_add_signal (binding_set,
101 GTK_TYPE_MENU_DIRECTION_TYPE,
102 GTK_MENU_DIR_PARENT);
103 gtk_binding_entry_add_signal (binding_set,
106 GTK_TYPE_MENU_DIRECTION_TYPE,
111 gtk_menu_bar_init (GtkMenuBar *menu_bar)
116 gtk_menu_bar_new (void)
118 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
122 gtk_menu_bar_append (GtkMenuBar *menu_bar,
125 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
129 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
132 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
136 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
140 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
145 gtk_menu_bar_size_request (GtkWidget *widget,
146 GtkRequisition *requisition)
148 GtkMenuBar *menu_bar;
149 GtkMenuShell *menu_shell;
154 g_return_if_fail (widget != NULL);
155 g_return_if_fail (GTK_IS_MENU_BAR (widget));
156 g_return_if_fail (requisition != NULL);
158 requisition->width = 0;
159 requisition->height = 0;
161 if (GTK_WIDGET_VISIBLE (widget))
163 menu_bar = GTK_MENU_BAR (widget);
164 menu_shell = GTK_MENU_SHELL (widget);
167 children = menu_shell->children;
171 child = children->data;
172 children = children->next;
174 if (GTK_WIDGET_VISIBLE (child))
176 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
177 gtk_widget_size_request (child, &child->requisition);
179 requisition->width += child->requisition.width;
180 requisition->height = MAX (requisition->height, child->requisition.height);
181 /* Support for the right justified help menu */
182 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
183 && (GTK_MENU_ITEM(child)->right_justify))
185 requisition->width += CHILD_SPACING;
192 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
193 widget->style->klass->xthickness +
195 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
196 widget->style->klass->ythickness +
200 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
205 gtk_menu_bar_size_allocate (GtkWidget *widget,
206 GtkAllocation *allocation)
208 GtkMenuBar *menu_bar;
209 GtkMenuShell *menu_shell;
212 GtkAllocation child_allocation;
215 g_return_if_fail (widget != NULL);
216 g_return_if_fail (GTK_IS_MENU_BAR (widget));
217 g_return_if_fail (allocation != NULL);
219 menu_bar = GTK_MENU_BAR (widget);
220 menu_shell = GTK_MENU_SHELL (widget);
222 widget->allocation = *allocation;
223 if (GTK_WIDGET_REALIZED (widget))
224 gdk_window_move_resize (widget->window,
225 allocation->x, allocation->y,
226 allocation->width, allocation->height);
228 if (menu_shell->children)
230 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
231 widget->style->klass->xthickness +
233 offset = child_allocation.x; /* Window edge to menubar start */
235 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
236 widget->style->klass->ythickness +
238 child_allocation.height = MAX (1, allocation->height - child_allocation.y * 2);
240 children = menu_shell->children;
243 child = children->data;
244 children = children->next;
246 /* Support for the right justified help menu */
247 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
248 && (GTK_MENU_ITEM(child)->right_justify))
250 child_allocation.x = allocation->width -
251 child->requisition.width - CHILD_SPACING - offset;
253 if (GTK_WIDGET_VISIBLE (child))
255 child_allocation.width = child->requisition.width;
257 gtk_widget_size_allocate (child, &child_allocation);
259 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
266 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
268 g_return_if_fail (widget != NULL);
269 g_return_if_fail (GTK_IS_MENU_BAR (widget));
271 if (GTK_WIDGET_DRAWABLE (widget))
273 gtk_paint_box (widget->style,
277 area, widget, "menubar",
284 gtk_menu_bar_draw (GtkWidget *widget,
287 GtkMenuShell *menu_shell;
289 GdkRectangle child_area;
292 g_return_if_fail (widget != NULL);
293 g_return_if_fail (GTK_IS_MENU_BAR (widget));
294 g_return_if_fail (area != NULL);
296 if (GTK_WIDGET_DRAWABLE (widget))
298 gtk_menu_bar_paint (widget, area);
300 menu_shell = GTK_MENU_SHELL (widget);
302 children = menu_shell->children;
305 child = children->data;
306 children = children->next;
308 if (gtk_widget_intersect (child, area, &child_area))
309 gtk_widget_draw (child, &child_area);
315 gtk_menu_bar_expose (GtkWidget *widget,
316 GdkEventExpose *event)
318 GtkMenuShell *menu_shell;
319 GdkEventExpose child_event;
323 g_return_val_if_fail (widget != NULL, FALSE);
324 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
325 g_return_val_if_fail (event != NULL, FALSE);
327 if (GTK_WIDGET_DRAWABLE (widget))
329 gtk_menu_bar_paint (widget, &event->area);
331 menu_shell = GTK_MENU_SHELL (widget);
332 child_event = *event;
334 children = menu_shell->children;
337 child = children->data;
338 children = children->next;
340 if (GTK_WIDGET_NO_WINDOW (child) &&
341 gtk_widget_intersect (child, &event->area, &child_event.area))
342 gtk_widget_event (child, (GdkEvent*) &child_event);