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 <string.h> /* memset */
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
36 #include "gtkmenuitem.h"
37 #include "gtkwindow.h"
39 #include "gtkvscrollbar.h"
40 #include "gtksettings.h"
44 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
45 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
47 #define DEFAULT_POPUP_DELAY 225
48 #define DEFAULT_POPDOWN_DELAY 1000
50 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
51 * extends below the submenu
54 #define MENU_SCROLL_STEP1 8
55 #define MENU_SCROLL_STEP2 15
56 #define MENU_SCROLL_ARROW_HEIGHT 16
57 #define MENU_SCROLL_FAST_ZONE 8
58 #define MENU_SCROLL_TIMEOUT1 50
59 #define MENU_SCROLL_TIMEOUT2 50
61 typedef struct _GtkMenuAttachData GtkMenuAttachData;
62 typedef struct _GtkMenuPrivate GtkMenuPrivate;
64 struct _GtkMenuAttachData
66 GtkWidget *attach_widget;
67 GtkMenuDetachFunc detacher;
70 struct _GtkMenuPrivate
72 gboolean have_position;
82 static void gtk_menu_class_init (GtkMenuClass *klass);
83 static void gtk_menu_init (GtkMenu *menu);
84 static void gtk_menu_set_property (GObject *object,
88 static void gtk_menu_get_property (GObject *object,
92 static void gtk_menu_destroy (GtkObject *object);
93 static void gtk_menu_finalize (GObject *object);
94 static void gtk_menu_realize (GtkWidget *widget);
95 static void gtk_menu_unrealize (GtkWidget *widget);
96 static void gtk_menu_size_request (GtkWidget *widget,
97 GtkRequisition *requisition);
98 static void gtk_menu_size_allocate (GtkWidget *widget,
99 GtkAllocation *allocation);
100 static void gtk_menu_paint (GtkWidget *widget,
101 GdkEventExpose *expose);
102 static void gtk_menu_show (GtkWidget *widget);
103 static gboolean gtk_menu_expose (GtkWidget *widget,
104 GdkEventExpose *event);
105 static gboolean gtk_menu_key_press (GtkWidget *widget,
107 static gboolean gtk_menu_button_press (GtkWidget *widget,
108 GdkEventButton *event);
109 static gboolean gtk_menu_button_release (GtkWidget *widget,
110 GdkEventButton *event);
111 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
112 GdkEventMotion *event);
113 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
114 GdkEventCrossing *event);
115 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
116 GdkEventCrossing *event);
117 static void gtk_menu_scroll_to (GtkMenu *menu,
120 static void gtk_menu_stop_scrolling (GtkMenu *menu);
121 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
122 static gboolean gtk_menu_scroll_timeout (gpointer data);
124 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
125 GtkWidget *menu_item);
126 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
127 GtkWidget *menu_item);
128 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
131 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
133 static void gtk_menu_handle_scrolling (GtkMenu *menu,
135 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
137 static void gtk_menu_style_set (GtkWidget *widget,
138 GtkStyle *previous_style);
139 static gboolean gtk_menu_focus (GtkWidget *widget,
140 GtkDirectionType direction);
141 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
144 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
145 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
146 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
149 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
150 GtkMenuItem *menu_item,
151 GdkEventCrossing *event);
153 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
154 static void gtk_menu_show_all (GtkWidget *widget);
155 static void gtk_menu_hide_all (GtkWidget *widget);
156 static void gtk_menu_position (GtkMenu *menu);
157 static void gtk_menu_reparent (GtkMenu *menu,
158 GtkWidget *new_parent,
160 static void gtk_menu_remove (GtkContainer *menu,
163 static void gtk_menu_update_title (GtkMenu *menu);
165 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
166 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
168 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
169 gboolean group_changed);
171 static GtkMenuShellClass *parent_class = NULL;
172 static const gchar *attach_data_key = "gtk-menu-attach-data";
175 gtk_menu_get_private (GtkMenu *menu)
177 GtkMenuPrivate *private;
178 static GQuark private_quark = 0;
181 private_quark = g_quark_from_static_string ("gtk-menu-private");
183 private = g_object_get_qdata (G_OBJECT (menu), private_quark);
187 private = g_new0 (GtkMenuPrivate, 1);
188 private->have_position = FALSE;
190 g_object_set_qdata_full (G_OBJECT (menu), private_quark,
198 gtk_menu_get_type (void)
200 static GType menu_type = 0;
204 static const GTypeInfo menu_info =
206 sizeof (GtkMenuClass),
207 NULL, /* base_init */
208 NULL, /* base_finalize */
209 (GClassInitFunc) gtk_menu_class_init,
210 NULL, /* class_finalize */
211 NULL, /* class_data */
214 (GInstanceInitFunc) gtk_menu_init,
217 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
225 gtk_menu_class_init (GtkMenuClass *class)
227 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
228 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
229 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
230 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
231 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
232 GtkBindingSet *binding_set;
234 parent_class = g_type_class_peek_parent (class);
236 gobject_class->finalize = gtk_menu_finalize;
237 gobject_class->set_property = gtk_menu_set_property;
238 gobject_class->get_property = gtk_menu_get_property;
240 g_object_class_install_property (gobject_class,
242 g_param_spec_string ("tearoff-title",
244 _("A title that may be displayed by the window manager when this menu is torn-off"),
246 G_PARAM_READABLE | G_PARAM_WRITABLE));
248 object_class->destroy = gtk_menu_destroy;
250 widget_class->realize = gtk_menu_realize;
251 widget_class->unrealize = gtk_menu_unrealize;
252 widget_class->size_request = gtk_menu_size_request;
253 widget_class->size_allocate = gtk_menu_size_allocate;
254 widget_class->show = gtk_menu_show;
255 widget_class->expose_event = gtk_menu_expose;
256 widget_class->key_press_event = gtk_menu_key_press;
257 widget_class->button_press_event = gtk_menu_button_press;
258 widget_class->button_release_event = gtk_menu_button_release;
259 widget_class->motion_notify_event = gtk_menu_motion_notify;
260 widget_class->show_all = gtk_menu_show_all;
261 widget_class->hide_all = gtk_menu_hide_all;
262 widget_class->enter_notify_event = gtk_menu_enter_notify;
263 widget_class->leave_notify_event = gtk_menu_leave_notify;
264 widget_class->motion_notify_event = gtk_menu_motion_notify;
265 widget_class->style_set = gtk_menu_style_set;
266 widget_class->focus = gtk_menu_focus;
268 container_class->remove = gtk_menu_remove;
270 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
271 menu_shell_class->deactivate = gtk_menu_deactivate;
272 menu_shell_class->select_item = gtk_menu_select_item;
273 menu_shell_class->insert = gtk_menu_real_insert;
274 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
276 binding_set = gtk_binding_set_by_class (class);
277 gtk_binding_entry_add_signal (binding_set,
280 GTK_TYPE_MENU_DIRECTION_TYPE,
282 gtk_binding_entry_add_signal (binding_set,
285 GTK_TYPE_MENU_DIRECTION_TYPE,
287 gtk_binding_entry_add_signal (binding_set,
290 GTK_TYPE_MENU_DIRECTION_TYPE,
292 gtk_binding_entry_add_signal (binding_set,
295 GTK_TYPE_MENU_DIRECTION_TYPE,
297 gtk_binding_entry_add_signal (binding_set,
300 GTK_TYPE_MENU_DIRECTION_TYPE,
301 GTK_MENU_DIR_PARENT);
302 gtk_binding_entry_add_signal (binding_set,
305 GTK_TYPE_MENU_DIRECTION_TYPE,
306 GTK_MENU_DIR_PARENT);
307 gtk_binding_entry_add_signal (binding_set,
310 GTK_TYPE_MENU_DIRECTION_TYPE,
312 gtk_binding_entry_add_signal (binding_set,
315 GTK_TYPE_MENU_DIRECTION_TYPE,
318 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
319 _("Can change accelerators"),
320 _("Whether menu accelerators can be changed by pressing a key over the menu item"),
324 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
325 _("Delay before submenus appear"),
326 _("Minimum time the pointer must stay over a menu item before the submenu appear"),
332 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
333 _("Delay before hiding a submenu"),
334 _("The time before hiding a submenu when the pointer is moving towards the submenu"),
337 DEFAULT_POPDOWN_DELAY,
344 gtk_menu_set_property (GObject *object,
351 menu = GTK_MENU (object);
355 case PROP_TEAROFF_TITLE:
356 gtk_menu_set_title (menu, g_value_get_string (value));
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 gtk_menu_get_property (GObject *object,
372 menu = GTK_MENU (object);
376 case PROP_TEAROFF_TITLE:
377 g_value_set_string (value, gtk_menu_get_title (menu));
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386 gtk_menu_window_event (GtkWidget *window,
390 gboolean handled = FALSE;
392 g_object_ref (window);
398 case GDK_KEY_RELEASE:
399 handled = gtk_widget_event (menu, event);
405 g_object_unref (window);
406 g_object_unref (menu);
412 gtk_menu_window_size_request (GtkWidget *window,
413 GtkRequisition *requisition,
416 GtkMenuPrivate *private = gtk_menu_get_private (menu);
418 if (private->have_position)
420 GdkScreen *screen = gtk_widget_get_screen (window);
421 gint screen_height = gdk_screen_get_height (screen);
423 if (private->y + requisition->height > screen_height)
424 requisition->height = screen_height - private->y;
429 gtk_menu_init (GtkMenu *menu)
431 menu->parent_menu_item = NULL;
432 menu->old_active_menu_item = NULL;
433 menu->accel_group = NULL;
434 menu->position_func = NULL;
435 menu->position_func_data = NULL;
436 menu->toggle_size = 0;
438 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
439 "type", GTK_WINDOW_POPUP,
442 "signal::event", gtk_menu_window_event, menu,
443 "signal::size_request", gtk_menu_window_size_request, menu,
444 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
446 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
447 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
449 /* Refloat the menu, so that reference counting for the menu isn't
450 * affected by it being a child of the toplevel
452 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
453 menu->needs_destruction_ref_count = TRUE;
455 menu->view_window = NULL;
456 menu->bin_window = NULL;
458 menu->scroll_offset = 0;
459 menu->scroll_step = 0;
460 menu->timeout_id = 0;
461 menu->scroll_fast = FALSE;
463 menu->tearoff_window = NULL;
464 menu->tearoff_hbox = NULL;
465 menu->torn_off = FALSE;
466 menu->tearoff_active = FALSE;
467 menu->tearoff_adjustment = NULL;
468 menu->tearoff_scrollbar = NULL;
470 menu->upper_arrow_visible = FALSE;
471 menu->lower_arrow_visible = FALSE;
472 menu->upper_arrow_prelight = FALSE;
473 menu->lower_arrow_prelight = FALSE;
475 MENU_NEEDS_RESIZE (menu) = TRUE;
479 gtk_menu_destroy (GtkObject *object)
482 GtkMenuAttachData *data;
484 g_return_if_fail (GTK_IS_MENU (object));
486 menu = GTK_MENU (object);
488 gtk_menu_stop_scrolling (menu);
490 data = g_object_get_data (G_OBJECT (object), attach_data_key);
492 gtk_menu_detach (menu);
494 gtk_menu_stop_navigating_submenu (menu);
496 if (menu->old_active_menu_item)
498 g_object_unref (menu->old_active_menu_item);
499 menu->old_active_menu_item = NULL;
502 /* Add back the reference count for being a child */
503 if (menu->needs_destruction_ref_count)
505 menu->needs_destruction_ref_count = FALSE;
506 g_object_ref (object);
509 if (menu->accel_group)
511 g_object_unref (menu->accel_group);
512 menu->accel_group = NULL;
516 gtk_widget_destroy (menu->toplevel);
517 if (menu->tearoff_window)
518 gtk_widget_destroy (menu->tearoff_window);
520 GTK_OBJECT_CLASS (parent_class)->destroy (object);
524 gtk_menu_finalize (GObject *object)
526 GtkMenu *menu = GTK_MENU (object);
528 g_free (menu->accel_path);
530 G_OBJECT_CLASS (parent_class)->finalize (object);
534 attach_widget_screen_changed (GtkWidget *attach_widget,
535 GdkScreen *previous_screen,
538 if (gtk_widget_has_screen (attach_widget) &&
539 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
541 gtk_window_set_screen (GTK_WINDOW (menu->toplevel),
542 gtk_widget_get_screen (attach_widget));
547 gtk_menu_attach_to_widget (GtkMenu *menu,
548 GtkWidget *attach_widget,
549 GtkMenuDetachFunc detacher)
551 GtkMenuAttachData *data;
553 g_return_if_fail (GTK_IS_MENU (menu));
554 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
555 g_return_if_fail (detacher != NULL);
557 /* keep this function in sync with gtk_widget_set_parent()
560 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
563 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
564 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
569 gtk_object_sink (GTK_OBJECT (menu));
571 data = g_new (GtkMenuAttachData, 1);
572 data->attach_widget = attach_widget;
574 g_signal_connect (attach_widget, "screen_changed",
575 G_CALLBACK (attach_widget_screen_changed), menu);
576 attach_widget_screen_changed (attach_widget, NULL, menu);
578 data->detacher = detacher;
579 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
581 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
582 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
584 /* we don't need to set the style here, since
585 * we are a toplevel widget.
588 /* Fallback title for menu comes from attach widget */
589 gtk_menu_update_title (menu);
593 gtk_menu_get_attach_widget (GtkMenu *menu)
595 GtkMenuAttachData *data;
597 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
599 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
601 return data->attach_widget;
606 gtk_menu_detach (GtkMenu *menu)
608 GtkMenuAttachData *data;
610 g_return_if_fail (GTK_IS_MENU (menu));
612 /* keep this function in sync with gtk_widget_unparent()
614 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
617 g_warning ("gtk_menu_detach(): menu is not attached");
620 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
622 g_signal_handlers_disconnect_by_func (data->attach_widget,
623 (gpointer) attach_widget_screen_changed,
626 data->detacher (data->attach_widget, menu);
628 if (GTK_WIDGET_REALIZED (menu))
629 gtk_widget_unrealize (GTK_WIDGET (menu));
633 /* Fallback title for menu comes from attach widget */
634 gtk_menu_update_title (menu);
636 g_object_unref (menu);
640 gtk_menu_remove (GtkContainer *container,
644 g_return_if_fail (GTK_IS_MENU (container));
645 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
647 menu = GTK_MENU (container);
649 /* Clear out old_active_menu_item if it matches the item we are removing
651 if (menu->old_active_menu_item == widget)
653 g_object_unref (menu->old_active_menu_item);
654 menu->old_active_menu_item = NULL;
657 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
664 return g_object_new (GTK_TYPE_MENU, NULL);
668 gtk_menu_real_insert (GtkMenuShell *menu_shell,
672 if (GTK_WIDGET_REALIZED (menu_shell))
673 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
675 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
679 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
684 widget = GTK_WIDGET (menu);
690 GdkGCValues gc_values;
692 menu->tearoff_active = FALSE;
693 menu->saved_scroll_offset = menu->scroll_offset;
695 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
696 gc = gdk_gc_new_with_values (widget->window,
697 &gc_values, GDK_GC_SUBWINDOW);
699 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
701 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
706 gdk_draw_drawable (pixmap, gc,
707 menu->tearoff_window->window,
711 gtk_widget_set_size_request (menu->tearoff_window,
715 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
716 g_object_unref (pixmap);
721 popup_grab_on_window (GdkWindow *window,
722 guint32 activate_time)
724 if ((gdk_pointer_grab (window, TRUE,
725 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
726 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
727 GDK_POINTER_MOTION_MASK,
728 NULL, NULL, activate_time) == 0))
730 if (gdk_keyboard_grab (window, TRUE,
735 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
747 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
748 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
749 * @func: a user supplied function used to position the menu, or %NULL
750 * @data: user supplied data to be passed to @func.
751 * @button: the mouse button which was pressed to initiate the event.
752 * @activate_time: the time at which the activation event occurred.
754 * Displays a menu and makes it available for selection. Applications can use
755 * this function to display context-sensitive menus, and will typically supply
756 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
757 * parameters. The default menu positioning function will position the menu
758 * at the current mouse cursor position.
760 * The @button parameter should be the mouse button pressed to initiate
761 * the menu popup. If the menu popup was initiated by something other than
762 * a mouse button press, such as a mouse button release or a keypress,
763 * @button should be 0.
765 * The @activate_time parameter should be the time stamp of the event that
766 * initiated the popup. If such an event is not available, use
767 * gtk_get_current_event_time() instead.
771 gtk_menu_popup (GtkMenu *menu,
772 GtkWidget *parent_menu_shell,
773 GtkWidget *parent_menu_item,
774 GtkMenuPositionFunc func,
777 guint32 activate_time)
780 GtkWidget *xgrab_shell;
782 GdkEvent *current_event;
783 GtkMenuShell *menu_shell;
785 g_return_if_fail (GTK_IS_MENU (menu));
787 widget = GTK_WIDGET (menu);
788 menu_shell = GTK_MENU_SHELL (menu);
790 menu_shell->parent_menu_shell = parent_menu_shell;
792 /* Find the last viewable ancestor, and make an X grab on it
794 parent = GTK_WIDGET (menu);
798 gboolean viewable = TRUE;
799 GtkWidget *tmp = parent;
803 if (!GTK_WIDGET_MAPPED (tmp))
812 xgrab_shell = parent;
814 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
817 /* We want to receive events generated when we map the menu; unfortunately,
818 * since there is probably already an implicit grab in place from the
819 * button that the user used to pop up the menu, we won't receive then --
820 * in particular, the EnterNotify when the menu pops up under the pointer.
822 * If we are grabbing on a parent menu shell, no problem; just grab on
823 * that menu shell first before popping up the window with owner_events = TRUE.
825 * When grabbing on the menu itself, things get more convuluted - we
826 * we do an explicit grab on a specially created window with
827 * owner_events = TRUE, which we override further down with a grab
828 * on the menu. (We can't grab on the menu until it is mapped; we
829 * probably could just leave the grab on the other window, with a
830 * little reorganization of the code in gtkmenu*).
832 if (xgrab_shell && xgrab_shell != widget)
834 if (popup_grab_on_window (xgrab_shell->window, activate_time))
835 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
839 GdkWindow *transfer_window;
841 xgrab_shell = widget;
842 transfer_window = menu_grab_transfer_window_get (menu);
843 if (popup_grab_on_window (transfer_window, activate_time))
844 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
847 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
849 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
850 * with a stuck up window, we just abort here. Presumably the user will
853 menu_shell->parent_menu_shell = NULL;
854 menu_grab_transfer_window_destroy (menu);
858 menu_shell->active = TRUE;
859 menu_shell->button = button;
861 /* If we are popping up the menu from something other than, a button
862 * press then, as a heuristic, we ignore enter events for the menu
863 * until we get a MOTION_NOTIFY.
866 current_event = gtk_get_current_event ();
869 if ((current_event->type != GDK_BUTTON_PRESS) &&
870 (current_event->type != GDK_ENTER_NOTIFY))
871 menu_shell->ignore_enter = TRUE;
873 gdk_event_free (current_event);
878 gtk_menu_tearoff_bg_copy (menu);
880 gtk_menu_reparent (menu, menu->toplevel, FALSE);
883 menu->parent_menu_item = parent_menu_item;
884 menu->position_func = func;
885 menu->position_func_data = data;
886 menu_shell->activate_time = activate_time;
888 /* We need to show the menu here rather in the init function because
889 * code expects to be able to tell if the menu is onscreen by
890 * looking at the GTK_WIDGET_VISIBLE (menu)
892 gtk_widget_show (GTK_WIDGET (menu));
894 /* Position the menu, possibly changing the size request
896 gtk_menu_position (menu);
898 /* Compute the size of the toplevel and realize it so we
899 * can scroll correctly.
902 GtkRequisition tmp_request;
903 GtkAllocation tmp_allocation = { 0, };
905 gtk_widget_size_request (menu->toplevel, &tmp_request);
907 tmp_allocation.width = tmp_request.width;
908 tmp_allocation.height = tmp_request.height;
910 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
912 gtk_widget_realize (GTK_WIDGET (menu));
915 gtk_menu_scroll_to (menu, menu->scroll_offset);
917 /* Once everything is set up correctly, map the toplevel window on
920 gtk_widget_show (menu->toplevel);
922 if (xgrab_shell == widget)
923 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
925 gtk_grab_add (GTK_WIDGET (menu));
929 gtk_menu_popdown (GtkMenu *menu)
931 GtkMenuPrivate *private;
932 GtkMenuShell *menu_shell;
934 g_return_if_fail (GTK_IS_MENU (menu));
936 menu_shell = GTK_MENU_SHELL (menu);
937 private = gtk_menu_get_private (menu);
939 menu_shell->parent_menu_shell = NULL;
940 menu_shell->active = FALSE;
941 menu_shell->ignore_enter = FALSE;
943 private->have_position = FALSE;
945 gtk_menu_stop_scrolling (menu);
947 gtk_menu_stop_navigating_submenu (menu);
949 if (menu_shell->active_menu_item)
951 if (menu->old_active_menu_item)
952 g_object_unref (menu->old_active_menu_item);
953 menu->old_active_menu_item = menu_shell->active_menu_item;
954 g_object_ref (menu->old_active_menu_item);
957 gtk_menu_shell_deselect (menu_shell);
959 /* The X Grab, if present, will automatically be removed when we hide
961 gtk_widget_hide (menu->toplevel);
965 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
967 if (GTK_BIN (menu->toplevel)->child)
969 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
973 /* We popped up the menu from the tearoff, so we need to
974 * release the grab - we aren't actually hiding the menu.
976 if (menu_shell->have_xgrab)
978 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
980 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
981 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
985 /* gtk_menu_popdown is called each time a menu item is selected from
986 * a torn off menu. Only scroll back to the saved position if the
987 * non-tearoff menu was popped down.
989 if (!menu->tearoff_active)
990 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
991 menu->tearoff_active = TRUE;
994 gtk_widget_hide (GTK_WIDGET (menu));
996 menu_shell->have_xgrab = FALSE;
997 gtk_grab_remove (GTK_WIDGET (menu));
999 menu_grab_transfer_window_destroy (menu);
1003 gtk_menu_get_active (GtkMenu *menu)
1008 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1010 if (!menu->old_active_menu_item)
1013 children = GTK_MENU_SHELL (menu)->children;
1017 child = children->data;
1018 children = children->next;
1020 if (GTK_BIN (child)->child)
1025 menu->old_active_menu_item = child;
1026 if (menu->old_active_menu_item)
1027 g_object_ref (menu->old_active_menu_item);
1030 return menu->old_active_menu_item;
1034 gtk_menu_set_active (GtkMenu *menu,
1040 g_return_if_fail (GTK_IS_MENU (menu));
1042 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1045 child = tmp_list->data;
1046 if (GTK_BIN (child)->child)
1048 if (menu->old_active_menu_item)
1049 g_object_unref (menu->old_active_menu_item);
1050 menu->old_active_menu_item = child;
1051 g_object_ref (menu->old_active_menu_item);
1057 gtk_menu_set_accel_group (GtkMenu *menu,
1058 GtkAccelGroup *accel_group)
1060 g_return_if_fail (GTK_IS_MENU (menu));
1062 if (menu->accel_group != accel_group)
1064 if (menu->accel_group)
1065 g_object_unref (menu->accel_group);
1066 menu->accel_group = accel_group;
1067 if (menu->accel_group)
1068 g_object_ref (menu->accel_group);
1069 _gtk_menu_refresh_accel_paths (menu, TRUE);
1074 gtk_menu_get_accel_group (GtkMenu *menu)
1076 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1078 return menu->accel_group;
1082 * gtk_menu_set_accel_path
1083 * @menu: a valid #GtkMenu
1084 * @accel_path: a valid accelerator path
1086 * Sets an accelerator path for this menu from which accelerator paths
1087 * for its immediate children, its menu items, can be constructed.
1088 * The main purpose of this function is to spare the programmer the
1089 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1090 * each menu item that should support runtime user changable accelerators.
1091 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1092 * each menu item of this menu, that contains a label describing its purpose,
1093 * automatically gets an accel path assigned. For example, a menu containing
1094 * menu items "New" and "Exit", will, after
1095 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1096 * has been called, assign its items the accel paths:
1097 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1098 * Assigning accel paths to menu items then enables the user to change
1099 * their accelerators at runtime. More details about accelerator paths
1100 * and their default setups can be found at gtk_accel_map_add_entry().
1103 gtk_menu_set_accel_path (GtkMenu *menu,
1104 const gchar *accel_path)
1106 g_return_if_fail (GTK_IS_MENU (menu));
1108 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1110 g_free (menu->accel_path);
1111 menu->accel_path = g_strdup (accel_path);
1112 if (menu->accel_path)
1113 _gtk_menu_refresh_accel_paths (menu, FALSE);
1118 gboolean group_changed;
1122 refresh_accel_paths_foreach (GtkWidget *widget,
1125 AccelPropagation *prop = data;
1127 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1128 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1129 prop->menu->accel_path,
1130 prop->menu->accel_group,
1131 prop->group_changed);
1135 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1136 gboolean group_changed)
1138 g_return_if_fail (GTK_IS_MENU (menu));
1140 if (menu->accel_path && menu->accel_group)
1142 AccelPropagation prop;
1145 prop.group_changed = group_changed;
1146 gtk_container_foreach (GTK_CONTAINER (menu),
1147 refresh_accel_paths_foreach,
1153 gtk_menu_reposition (GtkMenu *menu)
1155 g_return_if_fail (GTK_IS_MENU (menu));
1157 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1158 gtk_menu_position (menu);
1162 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1165 g_return_if_fail (GTK_IS_MENU (menu));
1167 if (adjustment->value != menu->scroll_offset)
1168 gtk_menu_scroll_to (menu, adjustment->value);
1172 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1175 GdkGeometry geometry_hints;
1177 if (!menu->tearoff_window)
1180 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1182 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1183 width += menu->tearoff_scrollbar->requisition.width;
1186 geometry_hints.min_width = width;
1187 geometry_hints.max_width = width;
1189 geometry_hints.min_height = 0;
1190 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1192 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1195 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1199 gtk_menu_update_title (GtkMenu *menu)
1201 if (menu->tearoff_window)
1204 GtkWidget *attach_widget;
1206 title = gtk_menu_get_title (menu);
1209 attach_widget = gtk_menu_get_attach_widget (menu);
1210 if (GTK_IS_MENU_ITEM (attach_widget))
1212 GtkWidget *child = GTK_BIN (attach_widget)->child;
1213 if (GTK_IS_LABEL (child))
1214 title = gtk_label_get_text (GTK_LABEL (child));
1219 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1224 gtk_menu_set_tearoff_state (GtkMenu *menu,
1229 g_return_if_fail (GTK_IS_MENU (menu));
1231 if (menu->torn_off != torn_off)
1233 menu->torn_off = torn_off;
1234 menu->tearoff_active = torn_off;
1238 if (GTK_WIDGET_VISIBLE (menu))
1239 gtk_menu_popdown (menu);
1241 if (!menu->tearoff_window)
1243 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1244 "type", GTK_WINDOW_TOPLEVEL,
1245 "screen", gtk_widget_get_screen (menu->toplevel),
1246 "app_paintable", TRUE,
1249 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1250 GDK_WINDOW_TYPE_HINT_MENU);
1251 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1252 g_signal_connect (menu->tearoff_window, "destroy",
1253 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1254 g_signal_connect (menu->tearoff_window, "event",
1255 G_CALLBACK (gtk_menu_window_event), menu);
1257 gtk_menu_update_title (menu);
1259 gtk_widget_realize (menu->tearoff_window);
1261 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1262 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1264 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1265 menu->tearoff_adjustment =
1266 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1268 GTK_WIDGET (menu)->requisition.height,
1272 g_object_connect (menu->tearoff_adjustment,
1273 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1275 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1277 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1278 menu->tearoff_scrollbar,
1281 if (menu->tearoff_adjustment->upper > height)
1282 gtk_widget_show (menu->tearoff_scrollbar);
1284 gtk_widget_show (menu->tearoff_hbox);
1287 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1289 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1291 /* Update menu->requisition
1293 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1295 gtk_menu_set_tearoff_hints (menu, width);
1297 gtk_widget_realize (menu->tearoff_window);
1298 gtk_menu_position (menu);
1300 gtk_widget_show (GTK_WIDGET (menu));
1301 gtk_widget_show (menu->tearoff_window);
1303 gtk_menu_scroll_to (menu, 0);
1308 gtk_widget_hide (menu->tearoff_window);
1309 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1315 * gtk_menu_get_tearoff_state:
1318 * Returns whether the menu is torn off. See
1319 * gtk_menu_set_tearoff_state ().
1321 * Return value: %TRUE if the menu is currently torn off.
1324 gtk_menu_get_tearoff_state (GtkMenu *menu)
1326 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1328 return menu->torn_off;
1332 * gtk_menu_set_title:
1334 * @title: a string containing the title for the menu.
1336 * Sets the title string for the menu. The title is displayed when the menu
1337 * is shown as a tearoff menu.
1340 gtk_menu_set_title (GtkMenu *menu,
1343 g_return_if_fail (GTK_IS_MENU (menu));
1346 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1347 g_strdup (title), (GtkDestroyNotify) g_free);
1349 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1351 gtk_menu_update_title (menu);
1352 g_object_notify (G_OBJECT (menu), "tearoff_title");
1356 * gtk_menu_get_title:
1359 * Returns the title of the menu. See gtk_menu_set_title().
1361 * Return value: the title of the menu, or %NULL if the menu has no
1362 * title set on it. This string is owned by the widget and should
1363 * not be modified or freed.
1365 G_CONST_RETURN gchar *
1366 gtk_menu_get_title (GtkMenu *menu)
1368 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1370 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1374 gtk_menu_reorder_child (GtkMenu *menu,
1378 GtkMenuShell *menu_shell;
1379 g_return_if_fail (GTK_IS_MENU (menu));
1380 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1381 menu_shell = GTK_MENU_SHELL (menu);
1382 if (g_list_find (menu_shell->children, child))
1384 menu_shell->children = g_list_remove (menu_shell->children, child);
1385 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1386 if (GTK_WIDGET_VISIBLE (menu_shell))
1387 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1392 gtk_menu_style_set (GtkWidget *widget,
1393 GtkStyle *previous_style)
1395 if (GTK_WIDGET_REALIZED (widget))
1397 GtkMenu *menu = GTK_MENU (widget);
1399 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1400 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1401 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1406 gtk_menu_realize (GtkWidget *widget)
1408 GdkWindowAttr attributes;
1409 gint attributes_mask;
1415 g_return_if_fail (GTK_IS_MENU (widget));
1417 menu = GTK_MENU (widget);
1419 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1421 attributes.window_type = GDK_WINDOW_CHILD;
1422 attributes.x = widget->allocation.x;
1423 attributes.y = widget->allocation.y;
1424 attributes.width = widget->allocation.width;
1425 attributes.height = widget->allocation.height;
1426 attributes.wclass = GDK_INPUT_OUTPUT;
1427 attributes.visual = gtk_widget_get_visual (widget);
1428 attributes.colormap = gtk_widget_get_colormap (widget);
1430 attributes.event_mask = gtk_widget_get_events (widget);
1431 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1432 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1434 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1435 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1436 gdk_window_set_user_data (widget->window, widget);
1438 border_width = GTK_CONTAINER (widget)->border_width;
1440 attributes.x = border_width + widget->style->xthickness;
1441 attributes.y = border_width + widget->style->ythickness;
1442 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1443 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1445 if (menu->upper_arrow_visible)
1447 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1448 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1450 if (menu->lower_arrow_visible)
1451 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1453 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1454 gdk_window_set_user_data (menu->view_window, menu);
1458 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness) * 2);
1460 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1461 gdk_window_set_user_data (menu->bin_window, menu);
1463 children = GTK_MENU_SHELL (menu)->children;
1466 child = children->data;
1467 children = children->next;
1469 gtk_widget_set_parent_window (child, menu->bin_window);
1472 widget->style = gtk_style_attach (widget->style, widget->window);
1473 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1474 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1475 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1477 if (GTK_MENU_SHELL (widget)->active_menu_item)
1478 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1479 GTK_MENU_SHELL (widget)->active_menu_item);
1481 gdk_window_show (menu->bin_window);
1482 gdk_window_show (menu->view_window);
1486 gtk_menu_focus (GtkWidget *widget,
1487 GtkDirectionType direction)
1490 * A menu or its menu items cannot have focus
1495 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1498 menu_grab_transfer_window_get (GtkMenu *menu)
1500 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1503 GdkWindowAttr attributes;
1504 gint attributes_mask;
1506 attributes.x = -100;
1507 attributes.y = -100;
1508 attributes.width = 10;
1509 attributes.height = 10;
1510 attributes.window_type = GDK_WINDOW_TEMP;
1511 attributes.wclass = GDK_INPUT_ONLY;
1512 attributes.override_redirect = TRUE;
1513 attributes.event_mask = 0;
1515 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1517 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1518 &attributes, attributes_mask);
1519 gdk_window_set_user_data (window, menu);
1521 gdk_window_show (window);
1523 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1530 menu_grab_transfer_window_destroy (GtkMenu *menu)
1532 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1535 gdk_window_set_user_data (window, NULL);
1536 gdk_window_destroy (window);
1537 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1542 gtk_menu_unrealize (GtkWidget *widget)
1546 g_return_if_fail (GTK_IS_MENU (widget));
1548 menu = GTK_MENU (widget);
1550 menu_grab_transfer_window_destroy (menu);
1552 gdk_window_set_user_data (menu->view_window, NULL);
1553 gdk_window_destroy (menu->view_window);
1554 menu->view_window = NULL;
1556 gdk_window_set_user_data (menu->bin_window, NULL);
1557 gdk_window_destroy (menu->bin_window);
1558 menu->bin_window = NULL;
1560 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1564 gtk_menu_size_request (GtkWidget *widget,
1565 GtkRequisition *requisition)
1568 GtkMenuShell *menu_shell;
1571 guint max_toggle_size;
1572 guint max_accel_width;
1573 GtkRequisition child_requisition;
1575 g_return_if_fail (GTK_IS_MENU (widget));
1576 g_return_if_fail (requisition != NULL);
1578 menu = GTK_MENU (widget);
1579 menu_shell = GTK_MENU_SHELL (widget);
1581 requisition->width = 0;
1582 requisition->height = 0;
1584 max_toggle_size = 0;
1585 max_accel_width = 0;
1587 children = menu_shell->children;
1590 child = children->data;
1591 children = children->next;
1593 if (GTK_WIDGET_VISIBLE (child))
1597 /* It's important to size_request the child
1598 * before doing the toggle size request, in
1599 * case the toggle size request depends on the size
1600 * request of a child of the child (e.g. for ImageMenuItem)
1603 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
1604 gtk_widget_size_request (child, &child_requisition);
1606 requisition->width = MAX (requisition->width, child_requisition.width);
1607 requisition->height += child_requisition.height;
1609 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
1610 max_toggle_size = MAX (max_toggle_size, toggle_size);
1611 max_accel_width = MAX (max_accel_width, GTK_MENU_ITEM (child)->accelerator_width);
1615 requisition->width += max_toggle_size + max_accel_width;
1616 requisition->width += (GTK_CONTAINER (menu)->border_width +
1617 widget->style->xthickness) * 2;
1618 requisition->height += (GTK_CONTAINER (menu)->border_width +
1619 widget->style->ythickness) * 2;
1621 menu->toggle_size = max_toggle_size;
1623 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
1625 if (menu->tearoff_active)
1626 gtk_menu_set_tearoff_hints (menu, requisition->width);
1630 gtk_menu_size_allocate (GtkWidget *widget,
1631 GtkAllocation *allocation)
1634 GtkMenuShell *menu_shell;
1636 GtkAllocation child_allocation;
1641 g_return_if_fail (GTK_IS_MENU (widget));
1642 g_return_if_fail (allocation != NULL);
1644 menu = GTK_MENU (widget);
1645 menu_shell = GTK_MENU_SHELL (widget);
1647 widget->allocation = *allocation;
1649 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
1650 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness;
1652 width = MAX (1, allocation->width - x * 2);
1653 height = MAX (1, allocation->height - y * 2);
1655 if (menu_shell->active)
1656 gtk_menu_scroll_to (menu, menu->scroll_offset);
1658 if (menu->upper_arrow_visible && !menu->tearoff_active)
1660 y += MENU_SCROLL_ARROW_HEIGHT;
1661 height -= MENU_SCROLL_ARROW_HEIGHT;
1664 if (menu->lower_arrow_visible && !menu->tearoff_active)
1665 height -= MENU_SCROLL_ARROW_HEIGHT;
1667 if (GTK_WIDGET_REALIZED (widget))
1669 gdk_window_move_resize (widget->window,
1670 allocation->x, allocation->y,
1671 allocation->width, allocation->height);
1673 gdk_window_move_resize (menu->view_window,
1680 if (menu_shell->children)
1682 child_allocation.x = 0;
1683 child_allocation.y = 0;
1684 child_allocation.width = width;
1686 children = menu_shell->children;
1689 child = children->data;
1690 children = children->next;
1692 if (GTK_WIDGET_VISIBLE (child))
1694 GtkRequisition child_requisition;
1695 gtk_widget_get_child_requisition (child, &child_requisition);
1697 child_allocation.height = child_requisition.height;
1699 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
1701 gtk_widget_size_allocate (child, &child_allocation);
1702 gtk_widget_queue_draw (child);
1704 child_allocation.y += child_allocation.height;
1708 /* Resize the item window */
1709 if (GTK_WIDGET_REALIZED (widget))
1711 gdk_window_resize (menu->bin_window,
1712 child_allocation.width,
1713 child_allocation.y);
1717 if (menu->tearoff_active)
1719 if (allocation->height >= widget->requisition.height)
1721 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1723 gtk_widget_hide (menu->tearoff_scrollbar);
1724 gtk_menu_set_tearoff_hints (menu, allocation->width);
1726 gtk_menu_scroll_to (menu, 0);
1731 menu->tearoff_adjustment->upper = widget->requisition.height;
1732 menu->tearoff_adjustment->page_size = allocation->height;
1734 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
1735 menu->tearoff_adjustment->upper)
1738 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
1741 gtk_menu_scroll_to (menu, value);
1744 gtk_adjustment_changed (menu->tearoff_adjustment);
1746 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1748 gtk_widget_show (menu->tearoff_scrollbar);
1749 gtk_menu_set_tearoff_hints (menu, allocation->width);
1757 gtk_menu_paint (GtkWidget *widget,
1758 GdkEventExpose *event)
1762 gint border_x, border_y;
1764 g_return_if_fail (GTK_IS_MENU (widget));
1766 menu = GTK_MENU (widget);
1768 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
1769 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
1770 gdk_drawable_get_size (widget->window, &width, &height);
1772 if (event->window == widget->window)
1774 gtk_paint_box (widget->style,
1778 NULL, widget, "menu",
1780 if (menu->upper_arrow_visible && !menu->tearoff_active)
1782 gtk_paint_box (widget->style,
1784 menu->upper_arrow_prelight ?
1785 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1787 NULL, widget, "menu",
1791 MENU_SCROLL_ARROW_HEIGHT);
1793 gtk_paint_arrow (widget->style,
1795 menu->upper_arrow_prelight ?
1796 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1798 NULL, widget, "menu",
1801 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1803 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1804 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1807 if (menu->lower_arrow_visible && !menu->tearoff_active)
1809 gtk_paint_box (widget->style,
1811 menu->lower_arrow_prelight ?
1812 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1814 NULL, widget, "menu",
1816 height - border_y - MENU_SCROLL_ARROW_HEIGHT + 1,
1818 MENU_SCROLL_ARROW_HEIGHT);
1820 gtk_paint_arrow (widget->style,
1822 menu->lower_arrow_prelight ?
1823 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1825 NULL, widget, "menu",
1828 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1829 height - MENU_SCROLL_ARROW_HEIGHT + 1,
1830 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1831 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1837 gtk_menu_expose (GtkWidget *widget,
1838 GdkEventExpose *event)
1840 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1841 g_return_val_if_fail (event != NULL, FALSE);
1843 if (GTK_WIDGET_DRAWABLE (widget))
1845 gtk_menu_paint (widget, event);
1847 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1854 gtk_menu_show (GtkWidget *widget)
1856 GtkMenu *menu = GTK_MENU (widget);
1858 _gtk_menu_refresh_accel_paths (menu, FALSE);
1860 GTK_WIDGET_CLASS (parent_class)->show (widget);
1864 gtk_menu_button_press (GtkWidget *widget,
1865 GdkEventButton *event)
1867 /* Don't pop down the menu for releases over scroll arrows
1869 if (GTK_IS_MENU (widget))
1871 GtkMenu *menu = GTK_MENU (widget);
1873 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
1877 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1881 gtk_menu_button_release (GtkWidget *widget,
1882 GdkEventButton *event)
1884 /* Don't pop down the menu for releases over scroll arrows
1886 if (GTK_IS_MENU (widget))
1888 GtkMenu *menu = GTK_MENU (widget);
1890 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
1894 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1898 gtk_menu_key_press (GtkWidget *widget,
1901 GtkMenuShell *menu_shell;
1903 gboolean delete = FALSE;
1904 gboolean can_change_accels;
1905 gchar *accel = NULL;
1906 guint accel_key, accel_mods;
1907 GdkModifierType consumed_modifiers;
1908 GdkDisplay *display;
1910 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1911 g_return_val_if_fail (event != NULL, FALSE);
1913 menu_shell = GTK_MENU_SHELL (widget);
1914 menu = GTK_MENU (widget);
1916 gtk_menu_stop_navigating_submenu (menu);
1918 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
1921 display = gtk_widget_get_display (widget);
1923 g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
1924 "gtk-menu-bar-accel", &accel,
1925 "gtk-can-change-accels", &can_change_accels,
1931 GdkModifierType mods = 0;
1932 gboolean handled = FALSE;
1934 gtk_accelerator_parse (accel, &keyval, &mods);
1937 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
1939 /* FIXME this is wrong, needs to be in the global accel resolution
1940 * thing, to properly consider i18n etc., but that probably requires
1941 * AccelGroup changes etc.
1943 if (event->keyval == keyval &&
1944 (mods & event->state) == mods)
1946 g_signal_emit_by_name (menu, "cancel", 0);
1955 switch (event->keyval)
1966 /* Figure out what modifiers went into determining the key symbol */
1967 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
1968 event->hardware_keycode, event->state, event->group,
1969 NULL, NULL, NULL, &consumed_modifiers);
1971 accel_key = gdk_keyval_to_lower (event->keyval);
1972 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
1974 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
1975 * We re-upper case when we match against the keyval, but display and save in caseless form.
1977 if (accel_key != event->keyval)
1978 accel_mods |= GDK_SHIFT_MASK;
1980 /* Modify the accelerators */
1981 if (can_change_accels &&
1982 menu_shell->active_menu_item &&
1983 GTK_BIN (menu_shell->active_menu_item)->child && /* no seperators */
1984 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
1985 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
1987 GtkWidget *menu_item = menu_shell->active_menu_item;
1988 gboolean locked, replace_accels = TRUE;
1991 path = _gtk_widget_get_accel_path (menu_item, &locked);
1992 if (!path || locked)
1994 /* can't change accelerators on menu_items without paths
1995 * (basically, those items are accelerator-locked).
1997 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
1998 gdk_display_beep (display);
2004 /* For the keys that act to delete the current setting, we delete
2005 * the current setting if there is one, otherwise, we set the
2006 * key as the accelerator.
2012 if (gtk_accel_map_lookup_entry (path, &key) &&
2013 (key.accel_key || key.accel_mods))
2019 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2023 /* we failed, probably because this key is in use and
2026 /* g_print("failed to change\n"); */
2027 gdk_display_beep (display);
2036 gtk_menu_motion_notify (GtkWidget *widget,
2037 GdkEventMotion *event)
2039 GtkWidget *menu_item;
2041 GtkMenuShell *menu_shell;
2043 gboolean need_enter;
2045 if (GTK_IS_MENU (widget))
2046 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2048 /* We received the event for one of two reasons:
2050 * a) We are the active menu, and did gtk_grab_add()
2051 * b) The widget is a child of ours, and the event was propagated
2053 * Since for computation of navigation regions, we want the menu which
2054 * is the parent of the menu item, for a), we need to find that menu,
2055 * which may be different from 'widget'.
2057 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2058 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2059 !_gtk_menu_item_is_selectable (menu_item) ||
2060 !GTK_IS_MENU (menu_item->parent))
2063 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2064 menu = GTK_MENU (menu_shell);
2066 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2068 /* Check to see if we are within an active submenu's navigation region
2070 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2075 /* The menu is now sensitive to enter events on its items, but
2076 * was previously sensitive. So we fake an enter event.
2080 menu_shell->ignore_enter = FALSE;
2082 gdk_drawable_get_size (event->window, &width, &height);
2083 if (event->x >= 0 && event->x < width &&
2084 event->y >= 0 && event->y < height)
2086 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2089 send_event->crossing.window = g_object_ref (event->window);
2090 send_event->crossing.time = event->time;
2091 send_event->crossing.send_event = TRUE;
2092 send_event->crossing.x_root = event->x_root;
2093 send_event->crossing.y_root = event->y_root;
2094 send_event->crossing.x = event->x;
2095 send_event->crossing.y = event->y;
2097 /* We send the event to 'widget', the currently active menu,
2098 * instead of 'menu', the menu that the pointer is in. This
2099 * will ensure that the event will be ignored unless the
2100 * menuitem is a child of the active menu or some parent
2101 * menu of the active menu.
2103 result = gtk_widget_event (widget, send_event);
2104 gdk_event_free (send_event);
2114 gtk_menu_scroll_timeout (gpointer data)
2119 gint view_width, view_height;
2121 GDK_THREADS_ENTER ();
2123 menu = GTK_MENU (data);
2124 widget = GTK_WIDGET (menu);
2126 offset = menu->scroll_offset + menu->scroll_step;
2128 /* If we scroll upward and the non-visible top part
2129 * is smaller than the scroll arrow it would be
2130 * pretty stupid to show the arrow and taking more
2131 * screen space than just scrolling to the top.
2133 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2136 /* Don't scroll over the top if we weren't before: */
2137 if ((menu->scroll_offset >= 0) && (offset < 0))
2140 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2142 /* Don't scroll past the bottom if we weren't before: */
2143 if (menu->scroll_offset > 0)
2144 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2146 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2147 (offset + view_height > widget->requisition.height))
2148 offset = widget->requisition.height - view_height;
2150 gtk_menu_scroll_to (menu, offset);
2152 GDK_THREADS_LEAVE ();
2158 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2160 GtkMenuShell *menu_shell;
2166 gboolean scroll_fast = FALSE;
2168 menu_shell = GTK_MENU_SHELL (menu);
2170 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2171 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2173 border = GTK_CONTAINER (menu)->border_width + GTK_WIDGET (menu)->style->ythickness;
2175 if (menu->upper_arrow_visible && !menu->tearoff_active)
2180 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2183 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2184 (y >= rect.y) && (y < rect.y + rect.height))
2187 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2190 if (enter && in_arrow &&
2191 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2193 menu->upper_arrow_prelight = TRUE;
2194 menu->scroll_fast = scroll_fast;
2195 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2197 /* Deselect the active item so that any submenus are poped down */
2198 gtk_menu_shell_deselect (menu_shell);
2200 gtk_menu_remove_scroll_timeout (menu);
2201 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2202 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2203 gtk_menu_scroll_timeout,
2206 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2208 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2210 gtk_menu_stop_scrolling (menu);
2214 if (menu->lower_arrow_visible && !menu->tearoff_active)
2217 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2219 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2222 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2223 (y >= rect.y) && (y < rect.y + rect.height))
2226 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2229 if (enter && in_arrow &&
2230 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2232 menu->lower_arrow_prelight = TRUE;
2233 menu->scroll_fast = scroll_fast;
2234 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2236 /* Deselect the active item so that any submenus are poped down */
2237 gtk_menu_shell_deselect (menu_shell);
2239 gtk_menu_remove_scroll_timeout (menu);
2240 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2241 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2242 gtk_menu_scroll_timeout,
2245 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2247 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2249 gtk_menu_stop_scrolling (menu);
2255 gtk_menu_enter_notify (GtkWidget *widget,
2256 GdkEventCrossing *event)
2258 GtkWidget *menu_item;
2260 if (widget && GTK_IS_MENU (widget))
2262 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2264 if (!menu_shell->ignore_enter)
2265 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2268 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2269 * will not correspond to the event widget's parent. Check to see
2270 * if we are in the parent's navigation region.
2272 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2273 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2274 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2277 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2281 gtk_menu_leave_notify (GtkWidget *widget,
2282 GdkEventCrossing *event)
2284 GtkMenuShell *menu_shell;
2286 GtkMenuItem *menu_item;
2287 GtkWidget *event_widget;
2289 menu = GTK_MENU (widget);
2290 menu_shell = GTK_MENU_SHELL (widget);
2292 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2295 gtk_menu_handle_scrolling (menu, FALSE);
2297 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2299 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2302 menu_item = GTK_MENU_ITEM (event_widget);
2304 /* Here we check to see if we're leaving an active menu item with a submenu,
2305 * in which case we enter submenu navigation mode.
2307 if (menu_shell->active_menu_item != NULL
2308 && menu_item->submenu != NULL
2309 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2311 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2313 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2318 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2322 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2324 if (menu->navigation_region)
2326 gdk_region_destroy (menu->navigation_region);
2327 menu->navigation_region = NULL;
2330 if (menu->navigation_timeout)
2332 gtk_timeout_remove (menu->navigation_timeout);
2333 menu->navigation_timeout = 0;
2337 /* When the timeout is elapsed, the navigation region is destroyed
2338 * and the menuitem under the pointer (if any) is selected.
2341 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2343 GtkMenu *menu = user_data;
2344 GdkWindow *child_window;
2346 GDK_THREADS_ENTER ();
2348 gtk_menu_stop_navigating_submenu (menu);
2350 if (GTK_WIDGET_REALIZED (menu))
2352 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2356 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2358 send_event->crossing.window = g_object_ref (child_window);
2359 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2360 send_event->crossing.send_event = TRUE;
2362 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2364 gdk_event_free (send_event);
2368 GDK_THREADS_LEAVE ();
2374 gtk_menu_navigating_submenu (GtkMenu *menu,
2378 if (menu->navigation_region)
2380 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2384 gtk_menu_stop_navigating_submenu (menu);
2391 #undef DRAW_STAY_UP_TRIANGLE
2393 #ifdef DRAW_STAY_UP_TRIANGLE
2396 draw_stay_up_triangle (GdkWindow *window,
2399 /* Draw ugly color all over the stay-up triangle */
2400 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2401 GdkGCValues gc_values;
2403 GdkRectangle clipbox;
2405 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2406 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2407 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2408 gdk_gc_set_clip_region (ugly_gc, region);
2410 gdk_region_get_clipbox (region, &clipbox);
2412 gdk_draw_rectangle (window,
2415 clipbox.x, clipbox.y,
2416 clipbox.width, clipbox.height);
2418 g_object_unref (G_OBJECT (ugly_gc));
2423 flip_region (GdkRegion *region,
2428 GdkRectangle *rectangles;
2429 GdkRectangle clipbox;
2430 GdkRegion *new_region;
2433 new_region = gdk_region_new ();
2435 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
2436 gdk_region_get_clipbox (region, &clipbox);
2438 for (i = 0; i < n_rectangles; ++i)
2440 GdkRectangle rect = rectangles[i];
2443 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
2446 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
2448 gdk_region_union_with_rect (new_region, &rect);
2451 g_free (rectangles);
2457 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
2458 GtkMenuItem *menu_item,
2459 GdkEventCrossing *event)
2461 gint submenu_left = 0;
2462 gint submenu_right = 0;
2463 gint submenu_top = 0;
2464 gint submenu_bottom = 0;
2468 GtkWidget *event_widget;
2470 g_return_if_fail (menu_item->submenu != NULL);
2471 g_return_if_fail (event != NULL);
2473 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2475 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
2476 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
2478 submenu_right = submenu_left + width;
2479 submenu_bottom = submenu_top + height;
2481 gdk_drawable_get_size (event_widget->window, &width, &height);
2483 if (event->x >= 0 && event->x < width)
2486 gboolean flip_y = FALSE;
2487 gboolean flip_x = FALSE;
2489 gtk_menu_stop_navigating_submenu (menu);
2491 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2494 point[0].x = event->x_root;
2495 point[1].x = submenu_left;
2500 point[0].x = event->x_root + 1;
2501 point[1].x = 2 * (event->x_root + 1) - submenu_right;
2509 point[0].y = event->y_root + 1;
2510 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
2512 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
2520 point[0].y = event->y_root;
2521 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
2523 if (point[0].y >= submenu_bottom)
2527 point[2].x = point[1].x;
2528 point[2].y = point[0].y;
2530 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
2532 if (flip_x || flip_y)
2534 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
2535 gdk_region_destroy (menu->navigation_region);
2536 menu->navigation_region = new_region;
2539 g_object_get (G_OBJECT (gtk_widget_get_settings (GTK_WIDGET (menu))),
2540 "gtk-menu-popdown-delay", &popdown_delay,
2543 menu->navigation_timeout = gtk_timeout_add (popdown_delay,
2544 gtk_menu_stop_navigating_submenu_cb, menu);
2546 #ifdef DRAW_STAY_UP_TRIANGLE
2547 draw_stay_up_triangle (gdk_get_default_root_window(),
2548 menu->navigation_region);
2554 gtk_menu_deactivate (GtkMenuShell *menu_shell)
2558 g_return_if_fail (GTK_IS_MENU (menu_shell));
2560 parent = menu_shell->parent_menu_shell;
2562 menu_shell->activate_time = 0;
2563 gtk_menu_popdown (GTK_MENU (menu_shell));
2566 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
2570 gtk_menu_position (GtkMenu *menu)
2573 GtkRequisition requisition;
2574 GtkMenuPrivate *private;
2580 GdkRectangle monitor;
2583 g_return_if_fail (GTK_IS_MENU (menu));
2585 widget = GTK_WIDGET (menu);
2587 gdk_window_get_pointer (gtk_widget_get_root_window (widget),
2590 screen = gtk_widget_get_screen (widget);
2591 monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
2592 if (monitor_num < 0)
2594 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2596 /* We need the requisition to figure out the right place to
2597 * popup the menu. In fact, we always need to ask here, since
2598 * if a size_request was queued while we weren't popped up,
2599 * the requisition won't have been recomputed yet.
2601 gtk_widget_size_request (widget, &requisition);
2605 if (menu->position_func)
2606 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
2609 x = CLAMP (x - 2, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2610 y = CLAMP (y - 2, monitor.y, MAX (monitor.y, monitor.y + monitor.height - requisition.height));
2617 menu_height = GTK_WIDGET (menu)->requisition.height;
2619 if (y + menu_height > monitor.y + monitor.height)
2621 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
2622 y = (monitor.y + monitor.height) - menu_height;
2632 /* FIXME: should this be done in the various position_funcs ? */
2633 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2635 if (y + requisition.height > monitor.y + monitor.height)
2636 requisition.height = (monitor.y + monitor.height) - y;
2641 requisition.height -= -y;
2645 if (scroll_offset > 0)
2646 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
2648 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
2651 if (GTK_MENU_SHELL (menu)->active)
2653 private = gtk_menu_get_private (menu);
2654 private->have_position = TRUE;
2658 gtk_widget_queue_resize (menu->toplevel);
2662 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
2663 requisition.width, requisition.height);
2666 menu->scroll_offset = scroll_offset;
2670 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
2672 if (menu->timeout_id)
2674 g_source_remove (menu->timeout_id);
2675 menu->timeout_id = 0;
2680 gtk_menu_stop_scrolling (GtkMenu *menu)
2682 gtk_menu_remove_scroll_timeout (menu);
2684 menu->upper_arrow_prelight = FALSE;
2685 menu->lower_arrow_prelight = FALSE;
2689 gtk_menu_scroll_to (GtkMenu *menu,
2694 gint view_width, view_height;
2696 gboolean last_visible;
2699 widget = GTK_WIDGET (menu);
2701 if (menu->tearoff_active &&
2702 menu->tearoff_adjustment &&
2703 (menu->tearoff_adjustment->value != offset))
2705 menu->tearoff_adjustment->value = offset;
2706 gtk_adjustment_value_changed (menu->tearoff_adjustment);
2709 /* Move/resize the viewport according to arrows: */
2710 view_width = widget->allocation.width;
2711 view_height = widget->allocation.height;
2713 border_width = GTK_CONTAINER (menu)->border_width;
2714 view_width -= (border_width + widget->style->xthickness) * 2;
2715 view_height -= (border_width + widget->style->ythickness) * 2;
2716 menu_height = widget->requisition.height - (border_width + widget->style->ythickness) * 2;
2718 x = border_width + widget->style->xthickness;
2719 y = border_width + widget->style->ythickness;
2721 if (!menu->tearoff_active)
2723 last_visible = menu->upper_arrow_visible;
2724 menu->upper_arrow_visible = (offset > 0);
2726 if (menu->upper_arrow_visible)
2727 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2729 if ( (last_visible != menu->upper_arrow_visible) &&
2730 !menu->upper_arrow_visible)
2732 menu->upper_arrow_prelight = FALSE;
2734 /* If we hid the upper arrow, possibly remove timeout */
2735 if (menu->scroll_step < 0)
2736 gtk_menu_stop_scrolling (menu);
2739 last_visible = menu->lower_arrow_visible;
2740 menu->lower_arrow_visible = (view_height + offset < menu_height);
2742 if (menu->lower_arrow_visible)
2743 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2745 if ( (last_visible != menu->lower_arrow_visible) &&
2746 !menu->lower_arrow_visible)
2748 menu->lower_arrow_prelight = FALSE;
2750 /* If we hid the lower arrow, possibly remove timeout */
2751 if (menu->scroll_step > 0)
2752 gtk_menu_stop_scrolling (menu);
2755 if (menu->upper_arrow_visible)
2756 y += MENU_SCROLL_ARROW_HEIGHT;
2759 offset = CLAMP (offset, 0, menu_height - view_height);
2761 /* Scroll the menu: */
2762 if (GTK_WIDGET_REALIZED (menu))
2763 gdk_window_move (menu->bin_window, 0, -offset);
2765 if (GTK_WIDGET_REALIZED (menu))
2766 gdk_window_move_resize (menu->view_window,
2772 menu->scroll_offset = offset;
2776 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
2777 GtkWidget *menu_item)
2782 GtkRequisition child_requisition;
2783 gint child_offset, child_height;
2787 gboolean last_child = 0;
2789 menu = GTK_MENU (menu_shell);
2791 /* We need to check if the selected item fully visible.
2792 * If not we need to scroll the menu so that it becomes fully
2799 children = menu_shell->children;
2802 child = children->data;
2803 children = children->next;
2805 if (GTK_WIDGET_VISIBLE (child))
2807 gtk_widget_size_request (child, &child_requisition);
2808 child_offset += child_height;
2809 child_height = child_requisition.height;
2812 if (child == menu_item)
2814 last_child = (children == NULL);
2819 if (child == menu_item)
2821 y = menu->scroll_offset;
2822 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2824 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness;
2826 if (child_offset + child_height <= y)
2828 /* Ignore the enter event we might get if the pointer is on the menu
2830 menu_shell->ignore_enter = TRUE;
2831 gtk_menu_scroll_to (menu, child_offset);
2836 if (menu->upper_arrow_visible && !menu->tearoff_active)
2837 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2838 if (menu->lower_arrow_visible && !menu->tearoff_active)
2839 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2841 if (child_offset >= y + height - arrow_height)
2844 if (!last_child && !menu->tearoff_active)
2845 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2847 y = child_offset + child_height - height + arrow_height;
2848 if ((y > 0) && !menu->tearoff_active)
2850 /* Need upper arrow */
2851 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2852 y = child_offset + child_height - height + arrow_height;
2854 /* Ignore the enter event we might get if the pointer is on the menu
2856 menu_shell->ignore_enter = TRUE;
2857 gtk_menu_scroll_to (menu, y);
2865 gtk_menu_select_item (GtkMenuShell *menu_shell,
2866 GtkWidget *menu_item)
2868 GtkMenu *menu = GTK_MENU (menu_shell);
2870 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
2871 gtk_menu_scroll_item_visible (menu_shell, menu_item);
2873 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
2877 /* Reparent the menu, taking care of the refcounting
2879 * If unrealize is true we force a unrealize while reparenting the parent.
2880 * This can help eliminate flicker in some cases.
2882 * What happens is that when the menu is unrealized and then re-realized,
2883 * the allocations are as follows:
2885 * parent - 1x1 at (0,0)
2886 * child1 - 100x20 at (0,0)
2887 * child2 - 100x20 at (0,20)
2888 * child3 - 100x20 at (0,40)
2890 * That is, the parent is small but the children are full sized. Then,
2891 * when the queued_resize gets processed, the parent gets resized to
2894 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
2895 * contains the following logic:
2897 * - if a move or resize operation on a window would change the clip
2898 * region on the children, then before the window is resized
2899 * the background for children is temporarily set to None, the
2900 * move/resize done, and the background for the children restored.
2902 * So, at the point where the parent is resized to final size, the
2903 * background for the children is temporarily None, and thus they
2904 * are not cleared to the background color and the previous background
2905 * (the image of the menu) is left in place.
2908 gtk_menu_reparent (GtkMenu *menu,
2909 GtkWidget *new_parent,
2912 GtkObject *object = GTK_OBJECT (menu);
2913 GtkWidget *widget = GTK_WIDGET (menu);
2914 gboolean was_floating = GTK_OBJECT_FLOATING (object);
2916 g_object_ref (object);
2917 gtk_object_sink (object);
2921 g_object_ref (object);
2922 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
2923 gtk_container_add (GTK_CONTAINER (new_parent), widget);
2924 g_object_unref (object);
2927 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
2930 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
2932 g_object_unref (object);
2936 gtk_menu_show_all (GtkWidget *widget)
2938 g_return_if_fail (GTK_IS_MENU (widget));
2940 /* Show children, but not self. */
2941 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2946 gtk_menu_hide_all (GtkWidget *widget)
2948 g_return_if_fail (GTK_IS_MENU (widget));
2950 /* Hide children, but not self. */
2951 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
2955 * gtk_menu_set_screen:
2956 * @menu: a #GtkMenu.
2957 * @screen: a #GdkScreen, or %NULL if the screen should be
2958 * determined by the widget the menu is attached to.
2960 * Sets the #GdkScreen on which the menu will be displayed.
2965 gtk_menu_set_screen (GtkMenu *menu,
2968 g_return_if_fail (GTK_IS_MENU (menu));
2969 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
2971 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
2975 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), screen);
2979 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
2981 attach_widget_screen_changed (attach_widget, NULL, menu);
2987 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
2991 g_object_get (G_OBJECT (gtk_widget_get_settings (GTK_WIDGET (menu_shell))),
2992 "gtk-menu-popup-delay", &popup_delay,