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/.
28 #include <string.h> /* memset */
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
34 #include "gtkmenuitem.h"
35 #include "gtksignal.h"
36 #include "gtkwindow.h"
38 #include "gtkvscrollbar.h"
39 #include "gtksettings.h"
42 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
43 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
45 #define SUBMENU_NAV_REGION_PADDING 2
46 #define SUBMENU_NAV_HYSTERESIS_TIMEOUT 333
48 #define MENU_SCROLL_STEP 10
49 #define MENU_SCROLL_ARROW_HEIGHT 16
50 #define MENU_SCROLL_FAST_ZONE 4
51 #define MENU_SCROLL_TIMEOUT1 150
52 #define MENU_SCROLL_TIMEOUT2 50
54 typedef struct _GtkMenuAttachData GtkMenuAttachData;
56 struct _GtkMenuAttachData
58 GtkWidget *attach_widget;
59 GtkMenuDetachFunc detacher;
63 static void gtk_menu_class_init (GtkMenuClass *klass);
64 static void gtk_menu_init (GtkMenu *menu);
65 static void gtk_menu_destroy (GtkObject *object);
66 static void gtk_menu_realize (GtkWidget *widget);
67 static void gtk_menu_unrealize (GtkWidget *widget);
68 static void gtk_menu_size_request (GtkWidget *widget,
69 GtkRequisition *requisition);
70 static void gtk_menu_size_allocate (GtkWidget *widget,
71 GtkAllocation *allocation);
72 static void gtk_menu_paint (GtkWidget *widget);
73 static gboolean gtk_menu_expose (GtkWidget *widget,
74 GdkEventExpose *event);
75 static gboolean gtk_menu_key_press (GtkWidget *widget,
77 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
78 GdkEventMotion *event);
79 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
80 GdkEventCrossing *event);
81 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
82 GdkEventCrossing *event);
83 static void gtk_menu_scroll_to (GtkMenu *menu,
85 static void gtk_menu_stop_scrolling (GtkMenu *menu);
86 static gboolean gtk_menu_scroll_timeout (gpointer data);
87 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
88 GtkWidget *menu_item);
89 static void gtk_menu_insert (GtkMenuShell *menu_shell,
92 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
94 static void gtk_menu_handle_scrolling (GtkMenu *menu,
96 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
99 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
100 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
101 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
104 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
105 GtkMenuItem *menu_item,
106 GdkEventCrossing *event);
108 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
109 static void gtk_menu_show_all (GtkWidget *widget);
110 static void gtk_menu_hide_all (GtkWidget *widget);
111 static void gtk_menu_position (GtkMenu *menu);
112 static void gtk_menu_reparent (GtkMenu *menu,
113 GtkWidget *new_parent,
115 static void gtk_menu_remove (GtkContainer *menu,
118 static GtkMenuShellClass *parent_class = NULL;
119 static const gchar *attach_data_key = "gtk-menu-attach-data";
122 gtk_menu_get_type (void)
124 static GtkType menu_type = 0;
128 static const GtkTypeInfo menu_info =
132 sizeof (GtkMenuClass),
133 (GtkClassInitFunc) gtk_menu_class_init,
134 (GtkObjectInitFunc) gtk_menu_init,
135 /* reserved_1 */ NULL,
136 /* reserved_2 */ NULL,
137 (GtkClassInitFunc) NULL,
140 menu_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_info);
147 gtk_menu_class_init (GtkMenuClass *class)
149 GtkObjectClass *object_class;
150 GtkWidgetClass *widget_class;
151 GtkContainerClass *container_class;
152 GtkMenuShellClass *menu_shell_class;
154 GtkBindingSet *binding_set;
156 object_class = (GtkObjectClass*) class;
157 widget_class = (GtkWidgetClass*) class;
158 container_class = (GtkContainerClass*) class;
159 menu_shell_class = (GtkMenuShellClass*) class;
160 parent_class = gtk_type_class (gtk_menu_shell_get_type ());
162 object_class->destroy = gtk_menu_destroy;
164 widget_class->realize = gtk_menu_realize;
165 widget_class->unrealize = gtk_menu_unrealize;
166 widget_class->size_request = gtk_menu_size_request;
167 widget_class->size_allocate = gtk_menu_size_allocate;
168 widget_class->expose_event = gtk_menu_expose;
169 widget_class->key_press_event = gtk_menu_key_press;
170 widget_class->motion_notify_event = gtk_menu_motion_notify;
171 widget_class->show_all = gtk_menu_show_all;
172 widget_class->hide_all = gtk_menu_hide_all;
173 widget_class->enter_notify_event = gtk_menu_enter_notify;
174 widget_class->leave_notify_event = gtk_menu_leave_notify;
176 container_class->remove = gtk_menu_remove;
178 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
179 menu_shell_class->deactivate = gtk_menu_deactivate;
180 menu_shell_class->select_item = gtk_menu_select_item;
181 menu_shell_class->insert = gtk_menu_insert;
183 binding_set = gtk_binding_set_by_class (class);
184 gtk_binding_entry_add_signal (binding_set,
187 GTK_TYPE_MENU_DIRECTION_TYPE,
189 gtk_binding_entry_add_signal (binding_set,
192 GTK_TYPE_MENU_DIRECTION_TYPE,
194 gtk_binding_entry_add_signal (binding_set,
197 GTK_TYPE_MENU_DIRECTION_TYPE,
199 gtk_binding_entry_add_signal (binding_set,
202 GTK_TYPE_MENU_DIRECTION_TYPE,
204 gtk_binding_entry_add_signal (binding_set,
207 GTK_TYPE_MENU_DIRECTION_TYPE,
208 GTK_MENU_DIR_PARENT);
209 gtk_binding_entry_add_signal (binding_set,
212 GTK_TYPE_MENU_DIRECTION_TYPE,
213 GTK_MENU_DIR_PARENT);
214 gtk_binding_entry_add_signal (binding_set,
217 GTK_TYPE_MENU_DIRECTION_TYPE,
219 gtk_binding_entry_add_signal (binding_set,
222 GTK_TYPE_MENU_DIRECTION_TYPE,
227 gtk_menu_window_event (GtkWidget *window,
231 gboolean handled = FALSE;
233 gtk_widget_ref (window);
234 gtk_widget_ref (menu);
239 case GDK_KEY_RELEASE:
240 handled = gtk_widget_event (menu, event);
246 gtk_widget_unref (window);
247 gtk_widget_unref (menu);
253 gtk_menu_init (GtkMenu *menu)
255 menu->parent_menu_item = NULL;
256 menu->old_active_menu_item = NULL;
257 menu->accel_group = NULL;
258 menu->position_func = NULL;
259 menu->position_func_data = NULL;
260 menu->toggle_size = 0;
262 menu->toplevel = g_object_connect (gtk_widget_new (GTK_TYPE_WINDOW,
263 "type", GTK_WINDOW_POPUP,
266 "signal::event", gtk_menu_window_event, menu,
267 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
269 gtk_window_set_policy (GTK_WINDOW (menu->toplevel),
271 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
273 /* Refloat the menu, so that reference counting for the menu isn't
274 * affected by it being a child of the toplevel
276 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
277 menu->needs_destruction_ref_count = TRUE;
279 menu->view_window = NULL;
280 menu->bin_window = NULL;
282 menu->scroll_offset = 0;
283 menu->scroll_step = 0;
284 menu->timeout_id = 0;
285 menu->scroll_fast = FALSE;
287 menu->tearoff_window = NULL;
288 menu->tearoff_hbox = NULL;
289 menu->torn_off = FALSE;
290 menu->tearoff_active = FALSE;
291 menu->tearoff_adjustment = NULL;
292 menu->tearoff_scrollbar = NULL;
294 menu->upper_arrow_visible = FALSE;
295 menu->lower_arrow_visible = FALSE;
296 menu->upper_arrow_prelight = FALSE;
297 menu->lower_arrow_prelight = FALSE;
299 MENU_NEEDS_RESIZE (menu) = TRUE;
303 gtk_menu_destroy (GtkObject *object)
306 GtkMenuAttachData *data;
308 g_return_if_fail (GTK_IS_MENU (object));
310 menu = GTK_MENU (object);
312 gtk_menu_stop_scrolling (menu);
314 data = gtk_object_get_data (object, attach_data_key);
316 gtk_menu_detach (menu);
318 gtk_menu_stop_navigating_submenu (menu);
320 gtk_menu_set_accel_group (menu, NULL);
322 if (menu->old_active_menu_item)
324 gtk_widget_unref (menu->old_active_menu_item);
325 menu->old_active_menu_item = NULL;
328 /* Add back the reference count for being a child */
329 if (menu->needs_destruction_ref_count)
331 menu->needs_destruction_ref_count = FALSE;
332 gtk_object_ref (object);
336 gtk_widget_destroy (menu->toplevel);
337 if (menu->tearoff_window)
338 gtk_widget_destroy (menu->tearoff_window);
340 GTK_OBJECT_CLASS (parent_class)->destroy (object);
345 gtk_menu_attach_to_widget (GtkMenu *menu,
346 GtkWidget *attach_widget,
347 GtkMenuDetachFunc detacher)
349 GtkMenuAttachData *data;
351 g_return_if_fail (GTK_IS_MENU (menu));
352 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
353 g_return_if_fail (detacher != NULL);
355 /* keep this function in sync with gtk_widget_set_parent()
358 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
361 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
362 gtk_type_name (GTK_OBJECT_TYPE (data->attach_widget)));
366 gtk_object_ref (GTK_OBJECT (menu));
367 gtk_object_sink (GTK_OBJECT (menu));
369 data = g_new (GtkMenuAttachData, 1);
370 data->attach_widget = attach_widget;
371 data->detacher = detacher;
372 gtk_object_set_data (GTK_OBJECT (menu), attach_data_key, data);
374 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
375 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
377 /* we don't need to set the style here, since
378 * we are a toplevel widget.
383 gtk_menu_get_attach_widget (GtkMenu *menu)
385 GtkMenuAttachData *data;
387 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
389 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
391 return data->attach_widget;
396 gtk_menu_detach (GtkMenu *menu)
398 GtkMenuAttachData *data;
400 g_return_if_fail (GTK_IS_MENU (menu));
402 /* keep this function in sync with gtk_widget_unparent()
404 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
407 g_warning ("gtk_menu_detach(): menu is not attached");
410 gtk_object_remove_data (GTK_OBJECT (menu), attach_data_key);
412 data->detacher (data->attach_widget, menu);
414 if (GTK_WIDGET_REALIZED (menu))
415 gtk_widget_unrealize (GTK_WIDGET (menu));
419 gtk_widget_unref (GTK_WIDGET (menu));
423 gtk_menu_remove (GtkContainer *container,
427 g_return_if_fail (GTK_IS_MENU (container));
428 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
430 menu = GTK_MENU (container);
432 /* Clear out old_active_menu_item if it matches the item we are removing
434 if (menu->old_active_menu_item == widget)
436 gtk_widget_unref (menu->old_active_menu_item);
437 menu->old_active_menu_item = NULL;
440 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
447 return GTK_WIDGET (gtk_type_new (gtk_menu_get_type ()));
451 gtk_menu_insert (GtkMenuShell *menu_shell,
455 if (GTK_WIDGET_REALIZED (menu_shell))
456 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
458 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
462 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
467 widget = GTK_WIDGET (menu);
473 GdkGCValues gc_values;
475 menu->tearoff_active = FALSE;
476 menu->saved_scroll_offset = menu->scroll_offset;
478 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
479 gc = gdk_gc_new_with_values (widget->window,
480 &gc_values, GDK_GC_SUBWINDOW);
482 gdk_window_get_size (menu->tearoff_window->window, &width, &height);
484 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
489 gdk_draw_pixmap (pixmap, gc,
490 menu->tearoff_window->window,
494 gtk_widget_set_usize (menu->tearoff_window,
498 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
499 gdk_pixmap_unref (pixmap);
504 gtk_menu_popup (GtkMenu *menu,
505 GtkWidget *parent_menu_shell,
506 GtkWidget *parent_menu_item,
507 GtkMenuPositionFunc func,
510 guint32 activate_time)
513 GtkWidget *xgrab_shell;
515 GdkEvent *current_event;
516 GtkMenuShell *menu_shell;
518 g_return_if_fail (GTK_IS_MENU (menu));
520 widget = GTK_WIDGET (menu);
521 menu_shell = GTK_MENU_SHELL (menu);
523 menu_shell->parent_menu_shell = parent_menu_shell;
524 menu_shell->active = TRUE;
525 menu_shell->button = button;
527 /* If we are popping up the menu from something other than, a button
528 * press then, as a heuristic, we ignore enter events for the menu
529 * until we get a MOTION_NOTIFY.
532 current_event = gtk_get_current_event ();
535 if ((current_event->type != GDK_BUTTON_PRESS) &&
536 (current_event->type != GDK_ENTER_NOTIFY))
537 menu_shell->ignore_enter = TRUE;
542 gtk_menu_tearoff_bg_copy (menu);
544 gtk_menu_reparent (menu, menu->toplevel, FALSE);
547 menu->parent_menu_item = parent_menu_item;
548 menu->position_func = func;
549 menu->position_func_data = data;
550 menu_shell->activate_time = activate_time;
552 gtk_menu_position (menu);
554 /* We need to show the menu _here_ because code expects to be
555 * able to tell if the menu is onscreen by looking at the
556 * GTK_WIDGET_VISIBLE (menu)
558 gtk_widget_show (GTK_WIDGET (menu));
559 gtk_widget_show (menu->toplevel);
563 /* Also, if we're popping up from a key event, select the first
564 * item in the menu. Bad hack, but no better way to do it
565 * in current menu framework.
567 if (current_event->type == GDK_KEY_PRESS &&
568 GTK_MENU_SHELL (menu)->children)
570 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu),
571 GTK_MENU_SHELL (menu)->children->data);
574 gdk_event_free (current_event);
577 gtk_menu_scroll_to (menu, menu->scroll_offset);
579 /* Find the last viewable ancestor, and make an X grab on it
581 parent = GTK_WIDGET (menu);
585 gboolean viewable = TRUE;
586 GtkWidget *tmp = parent;
590 if (!GTK_WIDGET_MAPPED (tmp))
599 xgrab_shell = parent;
601 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
604 if (xgrab_shell && (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab))
606 if ((gdk_pointer_grab (xgrab_shell->window, TRUE,
607 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
608 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
609 GDK_POINTER_MOTION_MASK,
610 NULL, NULL, activate_time) == 0))
612 if (gdk_keyboard_grab (xgrab_shell->window, TRUE,
614 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
616 gdk_pointer_ungrab (activate_time);
620 gtk_grab_add (GTK_WIDGET (menu));
624 gtk_menu_popdown (GtkMenu *menu)
626 GtkMenuShell *menu_shell;
628 g_return_if_fail (GTK_IS_MENU (menu));
630 menu_shell = GTK_MENU_SHELL (menu);
632 menu_shell->parent_menu_shell = NULL;
633 menu_shell->active = FALSE;
634 menu_shell->ignore_enter = FALSE;
636 gtk_menu_stop_scrolling (menu);
638 gtk_menu_stop_navigating_submenu (menu);
640 if (menu_shell->active_menu_item)
642 if (menu->old_active_menu_item)
643 gtk_widget_unref (menu->old_active_menu_item);
644 menu->old_active_menu_item = menu_shell->active_menu_item;
645 gtk_widget_ref (menu->old_active_menu_item);
648 gtk_menu_shell_deselect (menu_shell);
650 /* The X Grab, if present, will automatically be removed when we hide
652 gtk_widget_hide (menu->toplevel);
657 gdk_window_get_size (menu->tearoff_window->window, &width, &height);
658 gtk_widget_set_usize (menu->tearoff_window,
662 if (GTK_BIN (menu->toplevel)->child)
664 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
668 /* We popped up the menu from the tearoff, so we need to
669 * release the grab - we aren't actually hiding the menu.
671 if (menu_shell->have_xgrab)
673 gdk_pointer_ungrab (GDK_CURRENT_TIME);
674 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
678 /* gtk_menu_popdown is called each time a menu item is selected from
679 * a torn off menu. Only scroll back to the saved position if the
680 * non-tearoff menu was popped down.
682 if (!menu->tearoff_active)
683 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
684 menu->tearoff_active = TRUE;
687 gtk_widget_hide (GTK_WIDGET (menu));
689 menu_shell->have_xgrab = FALSE;
690 gtk_grab_remove (GTK_WIDGET (menu));
694 gtk_menu_get_active (GtkMenu *menu)
699 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
701 if (!menu->old_active_menu_item)
704 children = GTK_MENU_SHELL (menu)->children;
708 child = children->data;
709 children = children->next;
711 if (GTK_BIN (child)->child)
716 menu->old_active_menu_item = child;
717 if (menu->old_active_menu_item)
718 gtk_widget_ref (menu->old_active_menu_item);
721 return menu->old_active_menu_item;
725 gtk_menu_set_active (GtkMenu *menu,
731 g_return_if_fail (GTK_IS_MENU (menu));
733 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
736 child = tmp_list->data;
737 if (GTK_BIN (child)->child)
739 if (menu->old_active_menu_item)
740 gtk_widget_unref (menu->old_active_menu_item);
741 menu->old_active_menu_item = child;
742 gtk_widget_ref (menu->old_active_menu_item);
748 gtk_menu_set_accel_group (GtkMenu *menu,
749 GtkAccelGroup *accel_group)
751 g_return_if_fail (GTK_IS_MENU (menu));
753 if (menu->accel_group != accel_group)
755 if (menu->accel_group)
756 gtk_accel_group_unref (menu->accel_group);
757 menu->accel_group = accel_group;
758 if (menu->accel_group)
759 gtk_accel_group_ref (menu->accel_group);
764 gtk_menu_get_accel_group (GtkMenu *menu)
766 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
768 return menu->accel_group;
772 gtk_menu_reposition (GtkMenu *menu)
774 g_return_if_fail (GTK_IS_MENU (menu));
776 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
777 gtk_menu_position (menu);
781 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
784 g_return_if_fail (GTK_IS_MENU (menu));
786 if (adjustment->value != menu->scroll_offset)
787 gtk_menu_scroll_to (menu, adjustment->value);
791 gtk_menu_set_tearoff_hints (GtkMenu *menu,
794 GdkGeometry geometry_hints;
796 if (!menu->tearoff_window)
799 geometry_hints.min_width = width;
800 geometry_hints.max_width = width;
802 geometry_hints.min_height = 0;
803 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
804 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
807 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
812 gtk_menu_set_tearoff_state (GtkMenu *menu,
817 g_return_if_fail (GTK_IS_MENU (menu));
819 if (menu->torn_off != torn_off)
821 menu->torn_off = torn_off;
822 menu->tearoff_active = torn_off;
826 if (GTK_WIDGET_VISIBLE (menu))
827 gtk_menu_popdown (menu);
829 if (!menu->tearoff_window)
831 GtkWidget *attach_widget;
834 menu->tearoff_window = g_object_connect (gtk_widget_new (GTK_TYPE_WINDOW,
835 "type", GTK_WINDOW_TOPLEVEL,
837 "signal::destroy", gtk_widget_destroyed, &menu->tearoff_window,
839 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
840 GDK_WINDOW_TYPE_HINT_MENU);
841 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
842 gtk_widget_set_app_paintable (menu->tearoff_window, TRUE);
843 gtk_signal_connect (GTK_OBJECT (menu->tearoff_window),
845 GTK_SIGNAL_FUNC (gtk_menu_window_event),
847 gtk_widget_realize (menu->tearoff_window);
849 title = gtk_object_get_data (GTK_OBJECT (menu), "gtk-menu-title");
852 attach_widget = gtk_menu_get_attach_widget (menu);
853 if (GTK_IS_MENU_ITEM (attach_widget))
855 GtkWidget *child = GTK_BIN (attach_widget)->child;
856 if (GTK_IS_LABEL (child))
857 gtk_label_get (GTK_LABEL (child), &title);
862 gdk_window_set_title (menu->tearoff_window->window, title);
864 gdk_window_set_decorations (menu->tearoff_window->window,
869 gtk_window_set_policy (GTK_WINDOW (menu->tearoff_window),
872 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
873 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
875 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
876 menu->tearoff_adjustment =
877 GTK_ADJUSTMENT (gtk_adjustment_new (0,
879 GTK_WIDGET (menu)->requisition.height,
883 g_object_connect (GTK_OBJECT (menu->tearoff_adjustment),
884 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
886 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
888 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
889 menu->tearoff_scrollbar,
892 if (menu->tearoff_adjustment->upper > height)
893 gtk_widget_show (menu->tearoff_scrollbar);
895 gtk_widget_show (menu->tearoff_hbox);
898 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
900 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
901 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
902 width += menu->tearoff_scrollbar->requisition.width;
904 gtk_menu_set_tearoff_hints (menu, width);
906 gtk_widget_realize (menu->tearoff_window);
907 gtk_menu_position (menu);
909 gtk_widget_show (GTK_WIDGET (menu));
910 gtk_widget_show (menu->tearoff_window);
912 gtk_menu_scroll_to (menu, 0);
917 gtk_widget_hide (menu->tearoff_window);
918 gtk_menu_reparent (menu, menu->toplevel, FALSE);
924 * gtk_menu_get_tearoff_state:
927 * Returns whether the menu is torn off. See
928 * gtk_menu_set_tearoff_state ().
930 * Return value: %TRUE if the menu is currently torn off.
933 gtk_menu_get_tearoff_state (GtkMenu *menu)
935 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
937 return menu->torn_off;
941 gtk_menu_set_title (GtkMenu *menu,
944 g_return_if_fail (GTK_IS_MENU (menu));
946 gtk_object_set_data_full (GTK_OBJECT (menu), "gtk-menu-title",
947 g_strdup (title), (GtkDestroyNotify) g_free);
951 * gtk_menu_get_title:
954 * Returns the title of the menu. See gtk_menu_set_title().
956 * Return value: the title of the menu, or %NULL if the menu has no
959 G_CONST_RETURN gchar *
960 gtk_menu_get_title (GtkMenu *menu)
962 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
964 return gtk_object_get_data (GTK_OBJECT (menu), "gtk-menu-title");
968 gtk_menu_reorder_child (GtkMenu *menu,
972 GtkMenuShell *menu_shell;
973 g_return_if_fail (GTK_IS_MENU (menu));
974 g_return_if_fail (GTK_IS_MENU_ITEM (child));
975 menu_shell = GTK_MENU_SHELL (menu);
976 if (g_list_find (menu_shell->children, child))
978 menu_shell->children = g_list_remove (menu_shell->children, child);
979 menu_shell->children = g_list_insert (menu_shell->children, child, position);
980 if (GTK_WIDGET_VISIBLE (menu_shell))
981 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
986 gtk_menu_realize (GtkWidget *widget)
988 GdkWindowAttr attributes;
989 gint attributes_mask;
995 g_return_if_fail (GTK_IS_MENU (widget));
997 menu = GTK_MENU (widget);
999 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1001 attributes.window_type = GDK_WINDOW_CHILD;
1002 attributes.x = widget->allocation.x;
1003 attributes.y = widget->allocation.y;
1004 attributes.width = widget->allocation.width;
1005 attributes.height = widget->allocation.height;
1006 attributes.wclass = GDK_INPUT_OUTPUT;
1007 attributes.visual = gtk_widget_get_visual (widget);
1008 attributes.colormap = gtk_widget_get_colormap (widget);
1010 attributes.event_mask = gtk_widget_get_events (widget);
1011 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1012 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1014 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1015 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1016 gdk_window_set_user_data (widget->window, widget);
1018 border_width = GTK_CONTAINER (widget)->border_width;
1020 attributes.x = border_width + widget->style->xthickness;
1021 attributes.y = border_width + widget->style->ythickness;
1022 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1023 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1025 if (menu->upper_arrow_visible)
1027 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1028 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1030 if (menu->lower_arrow_visible)
1031 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1033 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1034 gdk_window_set_user_data (menu->view_window, menu);
1038 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness) * 2);
1040 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1041 gdk_window_set_user_data (menu->bin_window, menu);
1043 children = GTK_MENU_SHELL (menu)->children;
1046 child = children->data;
1047 children = children->next;
1049 gtk_widget_set_parent_window (child, menu->bin_window);
1052 widget->style = gtk_style_attach (widget->style, widget->window);
1053 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1054 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1055 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1057 gtk_menu_paint (widget);
1059 gdk_window_show (menu->bin_window);
1060 gdk_window_show (menu->view_window);
1064 gtk_menu_unrealize (GtkWidget *widget)
1068 g_return_if_fail (GTK_IS_MENU (widget));
1070 menu = GTK_MENU (widget);
1072 gdk_window_set_user_data (menu->view_window, NULL);
1073 gdk_window_destroy (menu->view_window);
1074 menu->view_window = NULL;
1076 gdk_window_set_user_data (menu->bin_window, NULL);
1077 gdk_window_destroy (menu->bin_window);
1078 menu->bin_window = NULL;
1080 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1084 gtk_menu_size_request (GtkWidget *widget,
1085 GtkRequisition *requisition)
1088 GtkMenuShell *menu_shell;
1091 guint max_toggle_size;
1092 guint max_accel_width;
1093 GtkRequisition child_requisition;
1096 g_return_if_fail (GTK_IS_MENU (widget));
1097 g_return_if_fail (requisition != NULL);
1099 menu = GTK_MENU (widget);
1100 menu_shell = GTK_MENU_SHELL (widget);
1102 requisition->width = 0;
1103 requisition->height = 0;
1105 max_toggle_size = 0;
1106 max_accel_width = 0;
1108 children = menu_shell->children;
1111 child = children->data;
1112 children = children->next;
1114 if (GTK_WIDGET_VISIBLE (child))
1118 /* It's important to size_request the child
1119 * before doing the toggle size request, in
1120 * case the toggle size request depends on the size
1121 * request of a child of the child (e.g. for ImageMenuItem)
1124 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
1125 gtk_widget_size_request (child, &child_requisition);
1127 requisition->width = MAX (requisition->width, child_requisition.width);
1128 requisition->height += child_requisition.height;
1130 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
1131 max_toggle_size = MAX (max_toggle_size, toggle_size);
1132 max_accel_width = MAX (max_accel_width, GTK_MENU_ITEM (child)->accelerator_width);
1136 requisition->width += max_toggle_size + max_accel_width;
1137 requisition->width += (GTK_CONTAINER (menu)->border_width +
1138 widget->style->xthickness) * 2;
1139 requisition->height += (GTK_CONTAINER (menu)->border_width +
1140 widget->style->ythickness) * 2;
1142 menu->toggle_size = max_toggle_size;
1144 /* If the requested width was different than the allocated width, we need to change
1145 * the geometry hints for the tear off window so that the window can actually be resized.
1146 * Don't resize the tearoff if it is not active, beacuse it won't redraw (it is only a background pixmap).
1148 if ((requisition->width != GTK_WIDGET (menu)->allocation.width) && menu->tearoff_active)
1150 width = requisition->width;
1151 if (menu->tearoff_scrollbar && GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1152 width += menu->tearoff_scrollbar->requisition.width;
1153 gtk_menu_set_tearoff_hints (menu, width);
1158 gtk_menu_size_allocate (GtkWidget *widget,
1159 GtkAllocation *allocation)
1162 GtkMenuShell *menu_shell;
1164 GtkAllocation child_allocation;
1169 g_return_if_fail (GTK_IS_MENU (widget));
1170 g_return_if_fail (allocation != NULL);
1172 menu = GTK_MENU (widget);
1173 menu_shell = GTK_MENU_SHELL (widget);
1175 widget->allocation = *allocation;
1177 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
1178 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness;
1180 width = MAX (1, allocation->width - x * 2);
1181 height = MAX (1, allocation->height - y * 2);
1183 if (menu->upper_arrow_visible && !menu->tearoff_active)
1185 y += MENU_SCROLL_ARROW_HEIGHT;
1186 height -= MENU_SCROLL_ARROW_HEIGHT;
1189 if (menu->lower_arrow_visible && !menu->tearoff_active)
1190 height -= MENU_SCROLL_ARROW_HEIGHT;
1192 if (GTK_WIDGET_REALIZED (widget))
1194 gdk_window_move_resize (widget->window,
1195 allocation->x, allocation->y,
1196 allocation->width, allocation->height);
1198 gdk_window_move_resize (menu->view_window,
1205 if (menu_shell->children)
1207 child_allocation.x = 0;
1208 child_allocation.y = 0;
1209 child_allocation.width = width;
1211 children = menu_shell->children;
1214 child = children->data;
1215 children = children->next;
1217 if (GTK_WIDGET_VISIBLE (child))
1219 GtkRequisition child_requisition;
1220 gtk_widget_get_child_requisition (child, &child_requisition);
1222 child_allocation.height = child_requisition.height;
1224 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
1226 gtk_widget_size_allocate (child, &child_allocation);
1227 gtk_widget_queue_draw (child);
1229 child_allocation.y += child_allocation.height;
1233 /* Resize the item window */
1234 if (GTK_WIDGET_REALIZED (widget))
1236 gdk_window_resize (menu->bin_window,
1237 child_allocation.width,
1238 child_allocation.y);
1242 if (menu->tearoff_active)
1244 if (allocation->height >= widget->requisition.height)
1246 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1248 gtk_widget_hide (menu->tearoff_scrollbar);
1249 gtk_menu_set_tearoff_hints (menu, allocation->width);
1250 gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
1252 gtk_menu_scroll_to (menu, 0);
1257 menu->tearoff_adjustment->upper = widget->requisition.height;
1258 menu->tearoff_adjustment->page_size = allocation->height;
1260 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
1261 menu->tearoff_adjustment->upper)
1264 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
1267 gtk_menu_scroll_to (menu, value);
1270 gtk_adjustment_changed (menu->tearoff_adjustment);
1272 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1274 gtk_menu_set_tearoff_hints (menu,
1275 allocation->width + menu->tearoff_scrollbar->requisition.width);
1276 gtk_widget_show (menu->tearoff_scrollbar);
1277 gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
1285 gtk_menu_paint (GtkWidget *widget)
1294 g_return_if_fail (GTK_IS_MENU (widget));
1296 menu = GTK_MENU (widget);
1298 if (GTK_WIDGET_DRAWABLE (widget))
1300 gtk_paint_box (widget->style,
1304 NULL, widget, "menu",
1307 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
1308 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
1309 gdk_window_get_size (widget->window, &width, &height);
1311 if (menu->upper_arrow_visible && !menu->tearoff_active)
1313 gtk_paint_box (widget->style,
1315 menu->upper_arrow_prelight ?
1316 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1318 NULL, widget, "menu",
1322 MENU_SCROLL_ARROW_HEIGHT);
1324 gtk_paint_arrow (widget->style,
1326 menu->upper_arrow_prelight ?
1327 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1329 NULL, widget, "menu",
1332 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1334 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1335 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1338 if (menu->lower_arrow_visible && !menu->tearoff_active)
1340 gtk_paint_box (widget->style,
1342 menu->lower_arrow_prelight ?
1343 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1345 NULL, widget, "menu",
1347 height - border_y - MENU_SCROLL_ARROW_HEIGHT + 1,
1349 MENU_SCROLL_ARROW_HEIGHT);
1351 gtk_paint_arrow (widget->style,
1353 menu->lower_arrow_prelight ?
1354 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1356 NULL, widget, "menu",
1359 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1360 height - MENU_SCROLL_ARROW_HEIGHT + 1,
1361 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1362 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1365 if (menu->scroll_offset < 0)
1366 gtk_paint_box (widget->style,
1370 NULL, widget, "menu",
1373 -menu->scroll_offset);
1375 menu_height = widget->requisition.height - 2*border_y;
1376 top_pos = height - 2*border_y - (menu->upper_arrow_visible ? MENU_SCROLL_ARROW_HEIGHT : 0);
1378 if (menu_height - menu->scroll_offset < top_pos)
1379 gtk_paint_box (widget->style,
1383 NULL, widget, "menu",
1385 menu_height - menu->scroll_offset,
1387 top_pos - (menu_height - menu->scroll_offset));
1392 gtk_menu_expose (GtkWidget *widget,
1393 GdkEventExpose *event)
1395 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1396 g_return_val_if_fail (event != NULL, FALSE);
1398 if (GTK_WIDGET_DRAWABLE (widget))
1400 gtk_menu_paint (widget);
1402 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1409 gtk_menu_key_press (GtkWidget *widget,
1412 GtkMenuShell *menu_shell;
1414 gboolean delete = FALSE;
1415 gchar *accel = NULL;
1417 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1418 g_return_val_if_fail (event != NULL, FALSE);
1420 menu_shell = GTK_MENU_SHELL (widget);
1421 menu = GTK_MENU (widget);
1423 gtk_menu_stop_navigating_submenu (menu);
1425 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
1428 g_object_get (G_OBJECT (gtk_settings_get_default ()),
1429 "gtk-menu-bar-accel",
1436 GdkModifierType mods = 0;
1437 gboolean handled = FALSE;
1439 gtk_accelerator_parse (accel, &keyval, &mods);
1442 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
1444 /* FIXME this is wrong, needs to be in the global accel resolution
1445 * thing, to properly consider i18n etc., but that probably requires
1446 * AccelGroup changes etc.
1448 if (event->keyval == keyval &&
1449 (mods & event->state) == mods)
1451 gtk_signal_emit_by_name (GTK_OBJECT (menu), "cancel");
1460 switch (event->keyval)
1471 /* Modify the accelerators */
1472 if (menu_shell->active_menu_item &&
1473 GTK_BIN (menu_shell->active_menu_item)->child &&
1474 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL &&
1475 !gtk_widget_accelerators_locked (menu_shell->active_menu_item) &&
1477 (gtk_accelerator_valid (event->keyval, event->state) &&
1479 (event->keyval >= GDK_F1 && event->keyval <= GDK_F35)))))
1481 GtkMenuItem *menu_item;
1482 GtkAccelGroup *accel_group;
1484 menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1486 if (!GTK_MENU (widget)->accel_group)
1487 accel_group = gtk_accel_group_get_default ();
1489 accel_group = GTK_MENU (widget)->accel_group;
1491 gtk_widget_remove_accelerators (GTK_WIDGET (menu_item),
1492 gtk_signal_name (menu_item->accelerator_signal),
1496 0 == gtk_widget_accelerator_signal (GTK_WIDGET (menu_item),
1503 slist = gtk_accel_group_entries_from_object (G_OBJECT (menu_item));
1506 GtkAccelEntry *ac_entry;
1508 ac_entry = slist->data;
1510 if (ac_entry->signal_id == menu_item->accelerator_signal)
1513 slist = slist->next;
1517 gtk_widget_add_accelerator (GTK_WIDGET (menu_item),
1518 gtk_signal_name (menu_item->accelerator_signal),
1530 gtk_menu_motion_notify (GtkWidget *widget,
1531 GdkEventMotion *event)
1533 GtkWidget *menu_item;
1535 GtkMenuShell *menu_shell;
1537 gboolean need_enter;
1540 if (GTK_IS_MENU (widget))
1541 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
1543 /* We received the event for one of two reasons:
1545 * a) We are the active menu, and did gtk_grab_add()
1546 * b) The widget is a child of ours, and the event was propagated
1548 * Since for computation of navigation regions, we want the menu which
1549 * is the parent of the menu item, for a), we need to find that menu,
1550 * which may be different from 'widget'.
1552 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1553 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) || !GTK_WIDGET_IS_SENSITIVE (menu_item) ||
1554 !GTK_IS_MENU (menu_item->parent))
1557 menu_shell = GTK_MENU_SHELL (menu_item->parent);
1558 menu = GTK_MENU (menu_shell);
1560 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
1562 /* Check to see if we are within an active submenu's navigation region
1564 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
1569 /* The menu is now sensitive to enter events on its items, but
1570 * was previously sensitive. So we fake an enter event.
1574 menu_shell->ignore_enter = FALSE;
1576 gdk_window_get_size (event->window, &width, &height);
1577 if (event->x >= 0 && event->x < width &&
1578 event->y >= 0 && event->y < height)
1580 GdkEvent send_event;
1582 memset (&send_event, 0, sizeof (send_event));
1583 send_event.crossing.type = GDK_ENTER_NOTIFY;
1584 send_event.crossing.window = event->window;
1585 send_event.crossing.time = event->time;
1586 send_event.crossing.send_event = TRUE;
1587 send_event.crossing.x_root = event->x_root;
1588 send_event.crossing.y_root = event->y_root;
1589 send_event.crossing.x = event->x;
1590 send_event.crossing.y = event->y;
1592 /* We send the event to 'widget', the currently active menu,
1593 * instead of 'menu', the menu that the pointer is in. This
1594 * will ensure that the event will be ignored unless the
1595 * menuitem is a child of the active menu or some parent
1596 * menu of the active menu.
1598 return gtk_widget_event (widget, &send_event);
1606 gtk_menu_scroll_timeout (gpointer data)
1611 gint view_width, view_height;
1613 GDK_THREADS_ENTER ();
1615 menu = GTK_MENU (data);
1616 widget = GTK_WIDGET (menu);
1618 offset = menu->scroll_offset + menu->scroll_step;
1620 /* If we scroll upward and the non-visible top part
1621 * is smaller than the scroll arrow it would be
1622 * pretty stupid to show the arrow and taking more
1623 * screen space than just scrolling to the top.
1625 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
1628 /* Don't scroll over the top if we weren't before: */
1629 if ((menu->scroll_offset >= 0) && (offset < 0))
1632 gdk_window_get_size (widget->window, &view_width, &view_height);
1634 /* Don't scroll past the bottom if we weren't before: */
1635 if (menu->scroll_offset > 0)
1636 view_height -= MENU_SCROLL_ARROW_HEIGHT;
1638 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
1639 (offset + view_height > widget->requisition.height))
1640 offset = widget->requisition.height - view_height;
1642 gtk_menu_scroll_to (menu, offset);
1644 GDK_THREADS_LEAVE ();
1650 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
1652 GtkMenuShell *menu_shell;
1658 gboolean scroll_fast = FALSE;
1660 menu_shell = GTK_MENU_SHELL (menu);
1662 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
1663 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
1665 border = GTK_CONTAINER (menu)->border_width + GTK_WIDGET (menu)->style->ythickness;
1667 if (menu->upper_arrow_visible && !menu->tearoff_active)
1672 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
1675 if ((x >= rect.x) && (x < rect.x + rect.width) &&
1676 (y >= rect.y) && (y < rect.y + rect.height))
1679 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
1682 if (enter && in_arrow &&
1683 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
1685 menu->upper_arrow_prelight = TRUE;
1686 menu->scroll_fast = scroll_fast;
1687 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
1689 /* Deselect the active item so that any submenus are poped down */
1690 gtk_menu_shell_deselect (menu_shell);
1692 gtk_menu_stop_scrolling (menu);
1693 menu->scroll_step = -MENU_SCROLL_STEP;
1694 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
1695 gtk_menu_scroll_timeout,
1698 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
1700 menu->upper_arrow_prelight = FALSE;
1701 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
1703 gtk_menu_stop_scrolling (menu);
1707 if (menu->lower_arrow_visible && !menu->tearoff_active)
1710 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
1712 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
1715 if ((x >= rect.x) && (x < rect.x + rect.width) &&
1716 (y >= rect.y) && (y < rect.y + rect.height))
1719 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
1722 if (enter && in_arrow &&
1723 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
1725 menu->lower_arrow_prelight = TRUE;
1726 menu->scroll_fast = scroll_fast;
1727 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
1729 /* Deselect the active item so that any submenus are poped down */
1730 gtk_menu_shell_deselect (menu_shell);
1732 gtk_menu_stop_scrolling (menu);
1733 menu->scroll_step = MENU_SCROLL_STEP;
1734 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
1735 gtk_menu_scroll_timeout,
1738 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
1740 menu->lower_arrow_prelight = FALSE;
1741 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
1743 gtk_menu_stop_scrolling (menu);
1749 gtk_menu_enter_notify (GtkWidget *widget,
1750 GdkEventCrossing *event)
1752 GtkWidget *menu_item;
1754 if (widget && GTK_IS_MENU (widget))
1755 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
1757 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
1758 * will not correspond to the event widget's parent. Check to see
1759 * if we are in the parent's navigation region.
1761 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1762 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
1763 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
1766 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
1770 gtk_menu_leave_notify (GtkWidget *widget,
1771 GdkEventCrossing *event)
1773 GtkMenuShell *menu_shell;
1775 GtkMenuItem *menu_item;
1776 GtkWidget *event_widget;
1778 menu = GTK_MENU (widget);
1779 menu_shell = GTK_MENU_SHELL (widget);
1781 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
1784 gtk_menu_handle_scrolling (menu, FALSE);
1786 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1788 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1791 menu_item = GTK_MENU_ITEM (event_widget);
1793 /* Here we check to see if we're leaving an active menu item with a submenu,
1794 * in which case we enter submenu navigation mode.
1796 if (menu_shell->active_menu_item != NULL
1797 && menu_item->submenu != NULL
1798 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
1800 if (menu_item->submenu->window != NULL)
1802 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
1807 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
1811 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
1813 if (menu->navigation_region)
1815 gdk_region_destroy (menu->navigation_region);
1816 menu->navigation_region = NULL;
1819 if (menu->navigation_timeout)
1821 gtk_timeout_remove (menu->navigation_timeout);
1822 menu->navigation_timeout = 0;
1826 /* When the timeout is elapsed, the navigation region is destroyed
1827 * and the menuitem under the pointer (if any) is selected.
1830 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
1832 GtkMenu *menu = user_data;
1833 GdkWindow *child_window;
1835 GDK_THREADS_ENTER ();
1837 gtk_menu_stop_navigating_submenu (menu);
1839 if (GTK_WIDGET_REALIZED (menu))
1841 child_window = gdk_window_get_pointer (GTK_WIDGET (menu)->window, NULL, NULL, NULL);
1845 GdkEventCrossing send_event;
1847 memset (&send_event, 0, sizeof (send_event));
1848 send_event.window = child_window;
1849 send_event.type = GDK_ENTER_NOTIFY;
1850 send_event.time = GDK_CURRENT_TIME; /* Bogus */
1851 send_event.send_event = TRUE;
1853 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), &send_event);
1857 GDK_THREADS_LEAVE ();
1863 gtk_menu_navigating_submenu (GtkMenu *menu,
1867 if (menu->navigation_region)
1869 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
1873 gtk_menu_stop_navigating_submenu (menu);
1881 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
1882 GtkMenuItem *menu_item,
1883 GdkEventCrossing *event)
1885 gint submenu_left = 0;
1886 gint submenu_right = 0;
1887 gint submenu_top = 0;
1888 gint submenu_bottom = 0;
1892 GtkWidget *event_widget;
1894 g_return_if_fail (menu_item->submenu != NULL);
1895 g_return_if_fail (event != NULL);
1897 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1899 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
1900 gdk_window_get_size (menu_item->submenu->window, &width, &height);
1901 submenu_right = submenu_left + width;
1902 submenu_bottom = submenu_top + height;
1904 gdk_window_get_size (event_widget->window, &width, &height);
1906 if (event->x >= 0 && event->x < width)
1908 /* Set navigation region */
1909 /* We fudge/give a little padding in case the user
1910 * ``misses the vertex'' of the triangle/is off by a pixel or two.
1912 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
1913 point[0].x = event->x_root - SUBMENU_NAV_REGION_PADDING;
1915 point[0].x = event->x_root + SUBMENU_NAV_REGION_PADDING;
1917 /* Exiting the top or bottom? */
1920 point[0].y = event->y_root + SUBMENU_NAV_REGION_PADDING;
1921 point[1].y = submenu_top;
1923 if (point[0].y <= point[1].y)
1928 point[0].y = event->y_root - SUBMENU_NAV_REGION_PADDING;
1929 point[1].y = submenu_bottom;
1931 if (point[0].y >= point[1].y)
1935 /* Submenu is to the left or right? */
1936 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
1937 point[1].x = submenu_left; /* right */
1939 point[1].x = submenu_right; /* left */
1941 point[2].x = point[1].x;
1942 point[2].y = point[0].y;
1944 gtk_menu_stop_navigating_submenu (menu);
1946 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
1948 menu->navigation_timeout = gtk_timeout_add (SUBMENU_NAV_HYSTERESIS_TIMEOUT,
1949 gtk_menu_stop_navigating_submenu_cb, menu);
1954 gtk_menu_deactivate (GtkMenuShell *menu_shell)
1958 g_return_if_fail (GTK_IS_MENU (menu_shell));
1960 parent = menu_shell->parent_menu_shell;
1962 menu_shell->activate_time = 0;
1963 gtk_menu_popdown (GTK_MENU (menu_shell));
1966 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
1970 gtk_menu_position (GtkMenu *menu)
1973 GtkRequisition requisition;
1981 g_return_if_fail (GTK_IS_MENU (menu));
1983 widget = GTK_WIDGET (menu);
1985 screen_width = gdk_screen_width ();
1986 screen_height = gdk_screen_height ();
1988 gdk_window_get_pointer (NULL, &x, &y, NULL);
1990 /* We need the requisition to figure out the right place to
1991 * popup the menu. In fact, we always need to ask here, since
1992 * if a size_request was queued while we weren't popped up,
1993 * the requisition won't have been recomputed yet.
1995 gtk_widget_size_request (widget, &requisition);
1999 if (menu->position_func)
2000 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
2003 x = CLAMP (x - 2, 0, MAX (0, screen_width - requisition.width));
2004 y = CLAMP (y - 2, 0, MAX (0, screen_height - requisition.height));
2011 menu_height = GTK_WIDGET (menu)->requisition.height;
2013 if (y + menu_height > screen_height)
2015 scroll_offset -= y + menu_height - screen_height;
2016 y = screen_height - menu_height;
2026 if (y + requisition.height > screen_height)
2027 requisition.height = screen_height - y;
2032 requisition.height -= -y;
2036 if (scroll_offset > 0)
2037 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
2039 /* FIXME: The MAX() here is because gtk_widget_set_uposition
2040 * is broken. Once we provide an alternate interface that
2041 * allows negative values, then we can remove them.
2044 gtk_widget_set_uposition (GTK_MENU_SHELL (menu)->active ?
2045 menu->toplevel : menu->tearoff_window,
2047 gtk_widget_set_usize (GTK_MENU_SHELL (menu)->active ?
2048 menu->toplevel : menu->tearoff_hbox,
2049 -1, requisition.height);
2050 menu->scroll_offset = scroll_offset;
2054 gtk_menu_stop_scrolling (GtkMenu *menu)
2056 if (menu->timeout_id)
2058 g_source_remove (menu->timeout_id);
2059 menu->timeout_id = 0;
2065 gtk_menu_scroll_to (GtkMenu *menu,
2070 gint view_width, view_height;
2072 gboolean last_visible;
2075 widget = GTK_WIDGET (menu);
2077 if (menu->tearoff_active &&
2078 menu->tearoff_adjustment &&
2079 (menu->tearoff_adjustment->value != offset))
2081 menu->tearoff_adjustment->value = offset;
2082 gtk_adjustment_value_changed (menu->tearoff_adjustment);
2085 /* Scroll the menu: */
2086 gdk_window_move (menu->bin_window, 0, -offset);
2088 /* Move/resize the viewport according to arrows: */
2089 gdk_window_get_size (widget->window, &view_width, &view_height);
2091 border_width = GTK_CONTAINER (menu)->border_width;
2092 view_width -= (border_width + widget->style->xthickness) * 2;
2093 view_height -= (border_width + widget->style->ythickness) * 2;
2094 menu_height = widget->requisition.height - (border_width + widget->style->ythickness) * 2;
2096 x = border_width + widget->style->xthickness;
2097 y = border_width + widget->style->ythickness;
2099 if (!menu->tearoff_active)
2101 last_visible = menu->upper_arrow_visible;
2102 menu->upper_arrow_visible = (offset > 0);
2104 if (menu->upper_arrow_visible)
2105 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2107 if ( (last_visible != menu->upper_arrow_visible) &&
2108 !menu->upper_arrow_visible)
2110 menu->upper_arrow_prelight = FALSE;
2112 /* If we hid the upper arrow, possibly remove timeout */
2113 if (menu->scroll_step < 0)
2114 gtk_menu_stop_scrolling (menu);
2117 last_visible = menu->lower_arrow_visible;
2118 menu->lower_arrow_visible = (view_height + offset < menu_height);
2120 if (menu->lower_arrow_visible)
2121 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2123 if ( (last_visible != menu->lower_arrow_visible) &&
2124 !menu->lower_arrow_visible)
2126 menu->lower_arrow_prelight = FALSE;
2128 /* If we hid the lower arrow, possibly remove timeout */
2129 if (menu->scroll_step > 0)
2130 gtk_menu_stop_scrolling (menu);
2133 if (menu->upper_arrow_visible)
2134 y += MENU_SCROLL_ARROW_HEIGHT;
2138 gdk_window_move_resize (menu->view_window,
2144 menu->scroll_offset = offset;
2148 gtk_menu_select_item (GtkMenuShell *menu_shell,
2149 GtkWidget *menu_item)
2154 GtkRequisition child_requisition;
2155 gint child_offset, child_height;
2159 gboolean last_child = 0;
2161 g_return_if_fail (GTK_IS_MENU (menu_shell));
2163 menu = GTK_MENU (menu_shell);
2165 /* We need to check if the selected item fully visible.
2166 * If not we need to scroll the menu so that it becomes fully
2173 children = menu_shell->children;
2176 child = children->data;
2177 children = children->next;
2179 if (GTK_WIDGET_VISIBLE (child))
2181 gtk_widget_size_request (child, &child_requisition);
2182 child_offset += child_height;
2183 child_height = child_requisition.height;
2186 if (child == menu_item)
2188 last_child = (children == NULL);
2193 if (child == menu_item)
2195 y = menu->scroll_offset;
2196 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
2198 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness;
2200 if (child_offset + child_height <= y)
2202 /* Ignore the enter event we might get if the pointer is on the menu
2204 menu_shell->ignore_enter = TRUE;
2205 gtk_menu_scroll_to (menu, child_offset);
2210 if (menu->upper_arrow_visible && !menu->tearoff_active)
2211 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2212 if (menu->lower_arrow_visible && !menu->tearoff_active)
2213 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2215 if (child_offset >= y + height - arrow_height)
2218 if (!last_child && !menu->tearoff_active)
2219 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2221 y = child_offset + child_height - height + arrow_height;
2222 if ((y > 0) && !menu->tearoff_active)
2224 /* Need upper arrow */
2225 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2226 y = child_offset + child_height - height + arrow_height;
2228 /* Ignore the enter event we might get if the pointer is on the menu
2230 menu_shell->ignore_enter = TRUE;
2231 gtk_menu_scroll_to (menu, y);
2237 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
2241 /* Reparent the menu, taking care of the refcounting
2243 * If unrealize is true we force a unrealize while reparenting the parent.
2244 * This can help eliminate flicker in some cases.
2246 * What happens is that when the menu is unrealized and then re-realized,
2247 * the allocations are as follows:
2249 * parent - 1x1 at (0,0)
2250 * child1 - 100x20 at (0,0)
2251 * child2 - 100x20 at (0,20)
2252 * child3 - 100x20 at (0,40)
2254 * That is, the parent is small but the children are full sized. Then,
2255 * when the queued_resize gets processed, the parent gets resized to
2258 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
2259 * contains the following logic:
2261 * - if a move or resize operation on a window would change the clip
2262 * region on the children, then before the window is resized
2263 * the background for children is temporarily set to None, the
2264 * move/resize done, and the background for the children restored.
2266 * So, at the point where the parent is resized to final size, the
2267 * background for the children is temporarily None, and thus they
2268 * are not cleared to the background color and the previous background
2269 * (the image of the menu) is left in place.
2272 gtk_menu_reparent (GtkMenu *menu,
2273 GtkWidget *new_parent,
2276 GtkObject *object = GTK_OBJECT (menu);
2277 GtkWidget *widget = GTK_WIDGET (menu);
2278 gboolean was_floating = GTK_OBJECT_FLOATING (object);
2280 gtk_object_ref (object);
2281 gtk_object_sink (object);
2285 gtk_object_ref (object);
2286 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
2287 gtk_container_add (GTK_CONTAINER (new_parent), widget);
2288 gtk_object_unref (object);
2291 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
2294 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
2296 gtk_object_unref (object);
2300 gtk_menu_show_all (GtkWidget *widget)
2302 g_return_if_fail (GTK_IS_MENU (widget));
2304 /* Show children, but not self. */
2305 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2310 gtk_menu_hide_all (GtkWidget *widget)
2312 g_return_if_fail (GTK_IS_MENU (widget));
2314 /* Hide children, but not self. */
2315 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);