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 gint screen_height = gdk_screen_height ();
422 if (private->y + requisition->height > screen_height)
423 requisition->height = screen_height - private->y;
428 gtk_menu_init (GtkMenu *menu)
430 menu->parent_menu_item = NULL;
431 menu->old_active_menu_item = NULL;
432 menu->accel_group = NULL;
433 menu->position_func = NULL;
434 menu->position_func_data = NULL;
435 menu->toggle_size = 0;
437 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
438 "type", GTK_WINDOW_POPUP,
441 "signal::event", gtk_menu_window_event, menu,
442 "signal::size_request", gtk_menu_window_size_request, menu,
443 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
445 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
446 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
448 /* Refloat the menu, so that reference counting for the menu isn't
449 * affected by it being a child of the toplevel
451 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
452 menu->needs_destruction_ref_count = TRUE;
454 menu->view_window = NULL;
455 menu->bin_window = NULL;
457 menu->scroll_offset = 0;
458 menu->scroll_step = 0;
459 menu->timeout_id = 0;
460 menu->scroll_fast = FALSE;
462 menu->tearoff_window = NULL;
463 menu->tearoff_hbox = NULL;
464 menu->torn_off = FALSE;
465 menu->tearoff_active = FALSE;
466 menu->tearoff_adjustment = NULL;
467 menu->tearoff_scrollbar = NULL;
469 menu->upper_arrow_visible = FALSE;
470 menu->lower_arrow_visible = FALSE;
471 menu->upper_arrow_prelight = FALSE;
472 menu->lower_arrow_prelight = FALSE;
474 MENU_NEEDS_RESIZE (menu) = TRUE;
478 gtk_menu_destroy (GtkObject *object)
481 GtkMenuAttachData *data;
483 g_return_if_fail (GTK_IS_MENU (object));
485 menu = GTK_MENU (object);
487 gtk_menu_stop_scrolling (menu);
489 data = g_object_get_data (G_OBJECT (object), attach_data_key);
491 gtk_menu_detach (menu);
493 gtk_menu_stop_navigating_submenu (menu);
495 if (menu->old_active_menu_item)
497 g_object_unref (menu->old_active_menu_item);
498 menu->old_active_menu_item = NULL;
501 /* Add back the reference count for being a child */
502 if (menu->needs_destruction_ref_count)
504 menu->needs_destruction_ref_count = FALSE;
505 g_object_ref (object);
508 if (menu->accel_group)
510 g_object_unref (menu->accel_group);
511 menu->accel_group = NULL;
515 gtk_widget_destroy (menu->toplevel);
516 if (menu->tearoff_window)
517 gtk_widget_destroy (menu->tearoff_window);
519 GTK_OBJECT_CLASS (parent_class)->destroy (object);
523 gtk_menu_finalize (GObject *object)
525 GtkMenu *menu = GTK_MENU (object);
527 g_free (menu->accel_path);
529 G_OBJECT_CLASS (parent_class)->finalize (object);
533 attach_widget_screen_changed (GtkWidget *attach_widget,
534 GdkScreen *previous_screen,
537 if (gtk_widget_has_screen (attach_widget) &&
538 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
540 gtk_window_set_screen (GTK_WINDOW (menu->toplevel),
541 gtk_widget_get_screen (attach_widget));
546 gtk_menu_attach_to_widget (GtkMenu *menu,
547 GtkWidget *attach_widget,
548 GtkMenuDetachFunc detacher)
550 GtkMenuAttachData *data;
552 g_return_if_fail (GTK_IS_MENU (menu));
553 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
554 g_return_if_fail (detacher != NULL);
556 /* keep this function in sync with gtk_widget_set_parent()
559 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
562 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
563 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
568 gtk_object_sink (GTK_OBJECT (menu));
570 data = g_new (GtkMenuAttachData, 1);
571 data->attach_widget = attach_widget;
573 g_signal_connect (attach_widget, "screen_changed",
574 G_CALLBACK (attach_widget_screen_changed), menu);
575 attach_widget_screen_changed (attach_widget, NULL, menu);
577 data->detacher = detacher;
578 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
580 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
581 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
583 /* we don't need to set the style here, since
584 * we are a toplevel widget.
587 /* Fallback title for menu comes from attach widget */
588 gtk_menu_update_title (menu);
592 gtk_menu_get_attach_widget (GtkMenu *menu)
594 GtkMenuAttachData *data;
596 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
598 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
600 return data->attach_widget;
605 gtk_menu_detach (GtkMenu *menu)
607 GtkMenuAttachData *data;
609 g_return_if_fail (GTK_IS_MENU (menu));
611 /* keep this function in sync with gtk_widget_unparent()
613 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
616 g_warning ("gtk_menu_detach(): menu is not attached");
619 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
621 g_signal_handlers_disconnect_by_func (data->attach_widget,
622 (gpointer) attach_widget_screen_changed,
625 data->detacher (data->attach_widget, menu);
627 if (GTK_WIDGET_REALIZED (menu))
628 gtk_widget_unrealize (GTK_WIDGET (menu));
632 /* Fallback title for menu comes from attach widget */
633 gtk_menu_update_title (menu);
635 g_object_unref (menu);
639 gtk_menu_remove (GtkContainer *container,
643 g_return_if_fail (GTK_IS_MENU (container));
644 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
646 menu = GTK_MENU (container);
648 /* Clear out old_active_menu_item if it matches the item we are removing
650 if (menu->old_active_menu_item == widget)
652 g_object_unref (menu->old_active_menu_item);
653 menu->old_active_menu_item = NULL;
656 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
663 return g_object_new (GTK_TYPE_MENU, NULL);
667 gtk_menu_real_insert (GtkMenuShell *menu_shell,
671 if (GTK_WIDGET_REALIZED (menu_shell))
672 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
674 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
678 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
683 widget = GTK_WIDGET (menu);
689 GdkGCValues gc_values;
691 menu->tearoff_active = FALSE;
692 menu->saved_scroll_offset = menu->scroll_offset;
694 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
695 gc = gdk_gc_new_with_values (widget->window,
696 &gc_values, GDK_GC_SUBWINDOW);
698 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
700 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
705 gdk_draw_drawable (pixmap, gc,
706 menu->tearoff_window->window,
710 gtk_widget_set_size_request (menu->tearoff_window,
714 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
715 g_object_unref (pixmap);
720 popup_grab_on_window (GdkWindow *window,
721 guint32 activate_time)
723 if ((gdk_pointer_grab (window, TRUE,
724 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
725 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
726 GDK_POINTER_MOTION_MASK,
727 NULL, NULL, activate_time) == 0))
729 if (gdk_keyboard_grab (window, TRUE,
734 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
746 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
747 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
748 * @func: a user supplied function used to position the menu, or %NULL
749 * @data: user supplied data to be passed to @func.
750 * @button: the mouse button which was pressed to initiate the event.
751 * @activate_time: the time at which the activation event occurred.
753 * Displays a menu and makes it available for selection. Applications can use
754 * this function to display context-sensitive menus, and will typically supply
755 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
756 * parameters. The default menu positioning function will position the menu
757 * at the current mouse cursor position.
759 * The @button parameter should be the mouse button pressed to initiate
760 * the menu popup. If the menu popup was initiated by something other than
761 * a mouse button press, such as a mouse button release or a keypress,
762 * @button should be 0.
764 * The @activate_time parameter should be the time stamp of the event that
765 * initiated the popup. If such an event is not available, use
766 * gtk_get_current_event_time() instead.
770 gtk_menu_popup (GtkMenu *menu,
771 GtkWidget *parent_menu_shell,
772 GtkWidget *parent_menu_item,
773 GtkMenuPositionFunc func,
776 guint32 activate_time)
779 GtkWidget *xgrab_shell;
781 GdkEvent *current_event;
782 GtkMenuShell *menu_shell;
784 g_return_if_fail (GTK_IS_MENU (menu));
786 widget = GTK_WIDGET (menu);
787 menu_shell = GTK_MENU_SHELL (menu);
789 menu_shell->parent_menu_shell = parent_menu_shell;
791 /* Find the last viewable ancestor, and make an X grab on it
793 parent = GTK_WIDGET (menu);
797 gboolean viewable = TRUE;
798 GtkWidget *tmp = parent;
802 if (!GTK_WIDGET_MAPPED (tmp))
811 xgrab_shell = parent;
813 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
816 /* We want to receive events generated when we map the menu; unfortunately,
817 * since there is probably already an implicit grab in place from the
818 * button that the user used to pop up the menu, we won't receive then --
819 * in particular, the EnterNotify when the menu pops up under the pointer.
821 * If we are grabbing on a parent menu shell, no problem; just grab on
822 * that menu shell first before popping up the window with owner_events = TRUE.
824 * When grabbing on the menu itself, things get more convuluted - we
825 * we do an explicit grab on a specially created window with
826 * owner_events = TRUE, which we override further down with a grab
827 * on the menu. (We can't grab on the menu until it is mapped; we
828 * probably could just leave the grab on the other window, with a
829 * little reorganization of the code in gtkmenu*).
831 if (xgrab_shell && xgrab_shell != widget)
833 if (popup_grab_on_window (xgrab_shell->window, activate_time))
834 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
838 GdkWindow *transfer_window;
840 xgrab_shell = widget;
841 transfer_window = menu_grab_transfer_window_get (menu);
842 if (popup_grab_on_window (transfer_window, activate_time))
843 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
846 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
848 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
849 * with a stuck up window, we just abort here. Presumably the user will
852 menu_shell->parent_menu_shell = NULL;
853 menu_grab_transfer_window_destroy (menu);
857 menu_shell->active = TRUE;
858 menu_shell->button = button;
860 /* If we are popping up the menu from something other than, a button
861 * press then, as a heuristic, we ignore enter events for the menu
862 * until we get a MOTION_NOTIFY.
865 current_event = gtk_get_current_event ();
868 if ((current_event->type != GDK_BUTTON_PRESS) &&
869 (current_event->type != GDK_ENTER_NOTIFY))
870 menu_shell->ignore_enter = TRUE;
872 gdk_event_free (current_event);
877 gtk_menu_tearoff_bg_copy (menu);
879 gtk_menu_reparent (menu, menu->toplevel, FALSE);
882 menu->parent_menu_item = parent_menu_item;
883 menu->position_func = func;
884 menu->position_func_data = data;
885 menu_shell->activate_time = activate_time;
887 /* We need to show the menu here rather in the init function because
888 * code expects to be able to tell if the menu is onscreen by
889 * looking at the GTK_WIDGET_VISIBLE (menu)
891 gtk_widget_show (GTK_WIDGET (menu));
893 /* Position the menu, possibly changing the size request
895 gtk_menu_position (menu);
897 /* Compute the size of the toplevel and realize it so we
898 * can scroll correctly.
901 GtkRequisition tmp_request;
902 GtkAllocation tmp_allocation = { 0, };
904 gtk_widget_size_request (menu->toplevel, &tmp_request);
906 tmp_allocation.width = tmp_request.width;
907 tmp_allocation.height = tmp_request.height;
909 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
911 gtk_widget_realize (GTK_WIDGET (menu));
914 gtk_menu_scroll_to (menu, menu->scroll_offset);
916 /* Once everything is set up correctly, map the toplevel window on
919 gtk_widget_show (menu->toplevel);
921 if (xgrab_shell == widget)
922 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
924 gtk_grab_add (GTK_WIDGET (menu));
928 gtk_menu_popdown (GtkMenu *menu)
930 GtkMenuPrivate *private;
931 GtkMenuShell *menu_shell;
933 g_return_if_fail (GTK_IS_MENU (menu));
935 menu_shell = GTK_MENU_SHELL (menu);
936 private = gtk_menu_get_private (menu);
938 menu_shell->parent_menu_shell = NULL;
939 menu_shell->active = FALSE;
940 menu_shell->ignore_enter = FALSE;
942 private->have_position = FALSE;
944 gtk_menu_stop_scrolling (menu);
946 gtk_menu_stop_navigating_submenu (menu);
948 if (menu_shell->active_menu_item)
950 if (menu->old_active_menu_item)
951 g_object_unref (menu->old_active_menu_item);
952 menu->old_active_menu_item = menu_shell->active_menu_item;
953 g_object_ref (menu->old_active_menu_item);
956 gtk_menu_shell_deselect (menu_shell);
958 /* The X Grab, if present, will automatically be removed when we hide
960 gtk_widget_hide (menu->toplevel);
964 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
966 if (GTK_BIN (menu->toplevel)->child)
968 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
972 /* We popped up the menu from the tearoff, so we need to
973 * release the grab - we aren't actually hiding the menu.
975 if (menu_shell->have_xgrab)
977 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
979 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
980 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
984 /* gtk_menu_popdown is called each time a menu item is selected from
985 * a torn off menu. Only scroll back to the saved position if the
986 * non-tearoff menu was popped down.
988 if (!menu->tearoff_active)
989 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
990 menu->tearoff_active = TRUE;
993 gtk_widget_hide (GTK_WIDGET (menu));
995 menu_shell->have_xgrab = FALSE;
996 gtk_grab_remove (GTK_WIDGET (menu));
998 menu_grab_transfer_window_destroy (menu);
1002 gtk_menu_get_active (GtkMenu *menu)
1007 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1009 if (!menu->old_active_menu_item)
1012 children = GTK_MENU_SHELL (menu)->children;
1016 child = children->data;
1017 children = children->next;
1019 if (GTK_BIN (child)->child)
1024 menu->old_active_menu_item = child;
1025 if (menu->old_active_menu_item)
1026 g_object_ref (menu->old_active_menu_item);
1029 return menu->old_active_menu_item;
1033 gtk_menu_set_active (GtkMenu *menu,
1039 g_return_if_fail (GTK_IS_MENU (menu));
1041 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1044 child = tmp_list->data;
1045 if (GTK_BIN (child)->child)
1047 if (menu->old_active_menu_item)
1048 g_object_unref (menu->old_active_menu_item);
1049 menu->old_active_menu_item = child;
1050 g_object_ref (menu->old_active_menu_item);
1056 gtk_menu_set_accel_group (GtkMenu *menu,
1057 GtkAccelGroup *accel_group)
1059 g_return_if_fail (GTK_IS_MENU (menu));
1061 if (menu->accel_group != accel_group)
1063 if (menu->accel_group)
1064 g_object_unref (menu->accel_group);
1065 menu->accel_group = accel_group;
1066 if (menu->accel_group)
1067 g_object_ref (menu->accel_group);
1068 _gtk_menu_refresh_accel_paths (menu, TRUE);
1073 gtk_menu_get_accel_group (GtkMenu *menu)
1075 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1077 return menu->accel_group;
1081 * gtk_menu_set_accel_path
1082 * @menu: a valid #GtkMenu
1083 * @accel_path: a valid accelerator path
1085 * Sets an accelerator path for this menu from which accelerator paths
1086 * for its immediate children, its menu items, can be constructed.
1087 * The main purpose of this function is to spare the programmer the
1088 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1089 * each menu item that should support runtime user changable accelerators.
1090 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1091 * each menu item of this menu, that contains a label describing its purpose,
1092 * automatically gets an accel path assigned. For example, a menu containing
1093 * menu items "New" and "Exit", will, after
1094 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1095 * has been called, assign its items the accel paths:
1096 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1097 * Assigning accel paths to menu items then enables the user to change
1098 * their accelerators at runtime. More details about accelerator paths
1099 * and their default setups can be found at gtk_accel_map_add_entry().
1102 gtk_menu_set_accel_path (GtkMenu *menu,
1103 const gchar *accel_path)
1105 g_return_if_fail (GTK_IS_MENU (menu));
1107 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1109 g_free (menu->accel_path);
1110 menu->accel_path = g_strdup (accel_path);
1111 if (menu->accel_path)
1112 _gtk_menu_refresh_accel_paths (menu, FALSE);
1117 gboolean group_changed;
1121 refresh_accel_paths_foreach (GtkWidget *widget,
1124 AccelPropagation *prop = data;
1126 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1127 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1128 prop->menu->accel_path,
1129 prop->menu->accel_group,
1130 prop->group_changed);
1134 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1135 gboolean group_changed)
1137 g_return_if_fail (GTK_IS_MENU (menu));
1139 if (menu->accel_path && menu->accel_group)
1141 AccelPropagation prop;
1144 prop.group_changed = group_changed;
1145 gtk_container_foreach (GTK_CONTAINER (menu),
1146 refresh_accel_paths_foreach,
1152 gtk_menu_reposition (GtkMenu *menu)
1154 g_return_if_fail (GTK_IS_MENU (menu));
1156 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1157 gtk_menu_position (menu);
1161 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1164 g_return_if_fail (GTK_IS_MENU (menu));
1166 if (adjustment->value != menu->scroll_offset)
1167 gtk_menu_scroll_to (menu, adjustment->value);
1171 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1174 GdkGeometry geometry_hints;
1176 if (!menu->tearoff_window)
1179 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1181 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1182 width += menu->tearoff_scrollbar->requisition.width;
1185 geometry_hints.min_width = width;
1186 geometry_hints.max_width = width;
1188 geometry_hints.min_height = 0;
1189 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1191 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1194 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1198 gtk_menu_update_title (GtkMenu *menu)
1200 if (menu->tearoff_window)
1203 GtkWidget *attach_widget;
1205 title = gtk_menu_get_title (menu);
1208 attach_widget = gtk_menu_get_attach_widget (menu);
1209 if (GTK_IS_MENU_ITEM (attach_widget))
1211 GtkWidget *child = GTK_BIN (attach_widget)->child;
1212 if (GTK_IS_LABEL (child))
1213 title = gtk_label_get_text (GTK_LABEL (child));
1218 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1223 gtk_menu_set_tearoff_state (GtkMenu *menu,
1228 g_return_if_fail (GTK_IS_MENU (menu));
1230 if (menu->torn_off != torn_off)
1232 menu->torn_off = torn_off;
1233 menu->tearoff_active = torn_off;
1237 if (GTK_WIDGET_VISIBLE (menu))
1238 gtk_menu_popdown (menu);
1240 if (!menu->tearoff_window)
1242 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1243 "type", GTK_WINDOW_TOPLEVEL,
1244 "screen", gtk_widget_get_screen (menu->toplevel),
1245 "app_paintable", TRUE,
1248 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1249 GDK_WINDOW_TYPE_HINT_MENU);
1250 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1251 g_signal_connect (menu->tearoff_window, "destroy",
1252 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1253 g_signal_connect (menu->tearoff_window, "event",
1254 G_CALLBACK (gtk_menu_window_event), menu);
1256 gtk_menu_update_title (menu);
1258 gtk_widget_realize (menu->tearoff_window);
1260 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1261 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1263 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1264 menu->tearoff_adjustment =
1265 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1267 GTK_WIDGET (menu)->requisition.height,
1271 g_object_connect (menu->tearoff_adjustment,
1272 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1274 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1276 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1277 menu->tearoff_scrollbar,
1280 if (menu->tearoff_adjustment->upper > height)
1281 gtk_widget_show (menu->tearoff_scrollbar);
1283 gtk_widget_show (menu->tearoff_hbox);
1286 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1288 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1290 /* Update menu->requisition
1292 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1294 gtk_menu_set_tearoff_hints (menu, width);
1296 gtk_widget_realize (menu->tearoff_window);
1297 gtk_menu_position (menu);
1299 gtk_widget_show (GTK_WIDGET (menu));
1300 gtk_widget_show (menu->tearoff_window);
1302 gtk_menu_scroll_to (menu, 0);
1307 gtk_widget_hide (menu->tearoff_window);
1308 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1314 * gtk_menu_get_tearoff_state:
1317 * Returns whether the menu is torn off. See
1318 * gtk_menu_set_tearoff_state ().
1320 * Return value: %TRUE if the menu is currently torn off.
1323 gtk_menu_get_tearoff_state (GtkMenu *menu)
1325 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1327 return menu->torn_off;
1331 * gtk_menu_set_title:
1333 * @title: a string containing the title for the menu.
1335 * Sets the title string for the menu. The title is displayed when the menu
1336 * is shown as a tearoff menu.
1339 gtk_menu_set_title (GtkMenu *menu,
1342 g_return_if_fail (GTK_IS_MENU (menu));
1345 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1346 g_strdup (title), (GtkDestroyNotify) g_free);
1348 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1350 gtk_menu_update_title (menu);
1351 g_object_notify (G_OBJECT (menu), "tearoff_title");
1355 * gtk_menu_get_title:
1358 * Returns the title of the menu. See gtk_menu_set_title().
1360 * Return value: the title of the menu, or %NULL if the menu has no
1361 * title set on it. This string is owned by the widget and should
1362 * not be modified or freed.
1364 G_CONST_RETURN gchar *
1365 gtk_menu_get_title (GtkMenu *menu)
1367 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1369 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1373 gtk_menu_reorder_child (GtkMenu *menu,
1377 GtkMenuShell *menu_shell;
1378 g_return_if_fail (GTK_IS_MENU (menu));
1379 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1380 menu_shell = GTK_MENU_SHELL (menu);
1381 if (g_list_find (menu_shell->children, child))
1383 menu_shell->children = g_list_remove (menu_shell->children, child);
1384 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1385 if (GTK_WIDGET_VISIBLE (menu_shell))
1386 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1391 gtk_menu_style_set (GtkWidget *widget,
1392 GtkStyle *previous_style)
1394 if (GTK_WIDGET_REALIZED (widget))
1396 GtkMenu *menu = GTK_MENU (widget);
1398 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1399 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1400 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1405 gtk_menu_realize (GtkWidget *widget)
1407 GdkWindowAttr attributes;
1408 gint attributes_mask;
1414 g_return_if_fail (GTK_IS_MENU (widget));
1416 menu = GTK_MENU (widget);
1418 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1420 attributes.window_type = GDK_WINDOW_CHILD;
1421 attributes.x = widget->allocation.x;
1422 attributes.y = widget->allocation.y;
1423 attributes.width = widget->allocation.width;
1424 attributes.height = widget->allocation.height;
1425 attributes.wclass = GDK_INPUT_OUTPUT;
1426 attributes.visual = gtk_widget_get_visual (widget);
1427 attributes.colormap = gtk_widget_get_colormap (widget);
1429 attributes.event_mask = gtk_widget_get_events (widget);
1430 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1431 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1433 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1434 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1435 gdk_window_set_user_data (widget->window, widget);
1437 border_width = GTK_CONTAINER (widget)->border_width;
1439 attributes.x = border_width + widget->style->xthickness;
1440 attributes.y = border_width + widget->style->ythickness;
1441 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1442 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1444 if (menu->upper_arrow_visible)
1446 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1447 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1449 if (menu->lower_arrow_visible)
1450 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1452 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1453 gdk_window_set_user_data (menu->view_window, menu);
1457 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness) * 2);
1459 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1460 gdk_window_set_user_data (menu->bin_window, menu);
1462 children = GTK_MENU_SHELL (menu)->children;
1465 child = children->data;
1466 children = children->next;
1468 gtk_widget_set_parent_window (child, menu->bin_window);
1471 widget->style = gtk_style_attach (widget->style, widget->window);
1472 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1473 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1474 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1476 if (GTK_MENU_SHELL (widget)->active_menu_item)
1477 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1478 GTK_MENU_SHELL (widget)->active_menu_item);
1480 gdk_window_show (menu->bin_window);
1481 gdk_window_show (menu->view_window);
1485 gtk_menu_focus (GtkWidget *widget,
1486 GtkDirectionType direction)
1489 * A menu or its menu items cannot have focus
1494 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1497 menu_grab_transfer_window_get (GtkMenu *menu)
1499 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1502 GdkWindowAttr attributes;
1503 gint attributes_mask;
1505 attributes.x = -100;
1506 attributes.y = -100;
1507 attributes.width = 10;
1508 attributes.height = 10;
1509 attributes.window_type = GDK_WINDOW_TEMP;
1510 attributes.wclass = GDK_INPUT_ONLY;
1511 attributes.override_redirect = TRUE;
1512 attributes.event_mask = 0;
1514 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1516 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1517 &attributes, attributes_mask);
1518 gdk_window_set_user_data (window, menu);
1520 gdk_window_show (window);
1522 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1529 menu_grab_transfer_window_destroy (GtkMenu *menu)
1531 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1534 gdk_window_set_user_data (window, NULL);
1535 gdk_window_destroy (window);
1536 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1541 gtk_menu_unrealize (GtkWidget *widget)
1545 g_return_if_fail (GTK_IS_MENU (widget));
1547 menu = GTK_MENU (widget);
1549 menu_grab_transfer_window_destroy (menu);
1551 gdk_window_set_user_data (menu->view_window, NULL);
1552 gdk_window_destroy (menu->view_window);
1553 menu->view_window = NULL;
1555 gdk_window_set_user_data (menu->bin_window, NULL);
1556 gdk_window_destroy (menu->bin_window);
1557 menu->bin_window = NULL;
1559 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1563 gtk_menu_size_request (GtkWidget *widget,
1564 GtkRequisition *requisition)
1567 GtkMenuShell *menu_shell;
1570 guint max_toggle_size;
1571 guint max_accel_width;
1572 GtkRequisition child_requisition;
1574 g_return_if_fail (GTK_IS_MENU (widget));
1575 g_return_if_fail (requisition != NULL);
1577 menu = GTK_MENU (widget);
1578 menu_shell = GTK_MENU_SHELL (widget);
1580 requisition->width = 0;
1581 requisition->height = 0;
1583 max_toggle_size = 0;
1584 max_accel_width = 0;
1586 children = menu_shell->children;
1589 child = children->data;
1590 children = children->next;
1592 if (GTK_WIDGET_VISIBLE (child))
1596 /* It's important to size_request the child
1597 * before doing the toggle size request, in
1598 * case the toggle size request depends on the size
1599 * request of a child of the child (e.g. for ImageMenuItem)
1602 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
1603 gtk_widget_size_request (child, &child_requisition);
1605 requisition->width = MAX (requisition->width, child_requisition.width);
1606 requisition->height += child_requisition.height;
1608 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
1609 max_toggle_size = MAX (max_toggle_size, toggle_size);
1610 max_accel_width = MAX (max_accel_width, GTK_MENU_ITEM (child)->accelerator_width);
1614 requisition->width += max_toggle_size + max_accel_width;
1615 requisition->width += (GTK_CONTAINER (menu)->border_width +
1616 widget->style->xthickness) * 2;
1617 requisition->height += (GTK_CONTAINER (menu)->border_width +
1618 widget->style->ythickness) * 2;
1620 menu->toggle_size = max_toggle_size;
1622 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
1624 if (menu->tearoff_active)
1625 gtk_menu_set_tearoff_hints (menu, requisition->width);
1629 gtk_menu_size_allocate (GtkWidget *widget,
1630 GtkAllocation *allocation)
1633 GtkMenuShell *menu_shell;
1635 GtkAllocation child_allocation;
1640 g_return_if_fail (GTK_IS_MENU (widget));
1641 g_return_if_fail (allocation != NULL);
1643 menu = GTK_MENU (widget);
1644 menu_shell = GTK_MENU_SHELL (widget);
1646 widget->allocation = *allocation;
1648 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
1649 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness;
1651 width = MAX (1, allocation->width - x * 2);
1652 height = MAX (1, allocation->height - y * 2);
1654 if (menu_shell->active)
1655 gtk_menu_scroll_to (menu, menu->scroll_offset);
1657 if (menu->upper_arrow_visible && !menu->tearoff_active)
1659 y += MENU_SCROLL_ARROW_HEIGHT;
1660 height -= MENU_SCROLL_ARROW_HEIGHT;
1663 if (menu->lower_arrow_visible && !menu->tearoff_active)
1664 height -= MENU_SCROLL_ARROW_HEIGHT;
1666 if (GTK_WIDGET_REALIZED (widget))
1668 gdk_window_move_resize (widget->window,
1669 allocation->x, allocation->y,
1670 allocation->width, allocation->height);
1672 gdk_window_move_resize (menu->view_window,
1679 if (menu_shell->children)
1681 child_allocation.x = 0;
1682 child_allocation.y = 0;
1683 child_allocation.width = width;
1685 children = menu_shell->children;
1688 child = children->data;
1689 children = children->next;
1691 if (GTK_WIDGET_VISIBLE (child))
1693 GtkRequisition child_requisition;
1694 gtk_widget_get_child_requisition (child, &child_requisition);
1696 child_allocation.height = child_requisition.height;
1698 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
1700 gtk_widget_size_allocate (child, &child_allocation);
1701 gtk_widget_queue_draw (child);
1703 child_allocation.y += child_allocation.height;
1707 /* Resize the item window */
1708 if (GTK_WIDGET_REALIZED (widget))
1710 gdk_window_resize (menu->bin_window,
1711 child_allocation.width,
1712 child_allocation.y);
1716 if (menu->tearoff_active)
1718 if (allocation->height >= widget->requisition.height)
1720 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1722 gtk_widget_hide (menu->tearoff_scrollbar);
1723 gtk_menu_set_tearoff_hints (menu, allocation->width);
1725 gtk_menu_scroll_to (menu, 0);
1730 menu->tearoff_adjustment->upper = widget->requisition.height;
1731 menu->tearoff_adjustment->page_size = allocation->height;
1733 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
1734 menu->tearoff_adjustment->upper)
1737 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
1740 gtk_menu_scroll_to (menu, value);
1743 gtk_adjustment_changed (menu->tearoff_adjustment);
1745 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1747 gtk_widget_show (menu->tearoff_scrollbar);
1748 gtk_menu_set_tearoff_hints (menu, allocation->width);
1756 gtk_menu_paint (GtkWidget *widget,
1757 GdkEventExpose *event)
1761 gint border_x, border_y;
1763 g_return_if_fail (GTK_IS_MENU (widget));
1765 menu = GTK_MENU (widget);
1767 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
1768 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
1769 gdk_drawable_get_size (widget->window, &width, &height);
1771 if (event->window == widget->window)
1773 gtk_paint_box (widget->style,
1777 NULL, widget, "menu",
1779 if (menu->upper_arrow_visible && !menu->tearoff_active)
1781 gtk_paint_box (widget->style,
1783 menu->upper_arrow_prelight ?
1784 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1786 NULL, widget, "menu",
1790 MENU_SCROLL_ARROW_HEIGHT);
1792 gtk_paint_arrow (widget->style,
1794 menu->upper_arrow_prelight ?
1795 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1797 NULL, widget, "menu",
1800 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1802 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1803 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1806 if (menu->lower_arrow_visible && !menu->tearoff_active)
1808 gtk_paint_box (widget->style,
1810 menu->lower_arrow_prelight ?
1811 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1813 NULL, widget, "menu",
1815 height - border_y - MENU_SCROLL_ARROW_HEIGHT + 1,
1817 MENU_SCROLL_ARROW_HEIGHT);
1819 gtk_paint_arrow (widget->style,
1821 menu->lower_arrow_prelight ?
1822 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1824 NULL, widget, "menu",
1827 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1828 height - MENU_SCROLL_ARROW_HEIGHT + 1,
1829 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1830 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1836 gtk_menu_expose (GtkWidget *widget,
1837 GdkEventExpose *event)
1839 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1840 g_return_val_if_fail (event != NULL, FALSE);
1842 if (GTK_WIDGET_DRAWABLE (widget))
1844 gtk_menu_paint (widget, event);
1846 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1853 gtk_menu_show (GtkWidget *widget)
1855 GtkMenu *menu = GTK_MENU (widget);
1857 _gtk_menu_refresh_accel_paths (menu, FALSE);
1859 GTK_WIDGET_CLASS (parent_class)->show (widget);
1863 gtk_menu_button_press (GtkWidget *widget,
1864 GdkEventButton *event)
1866 /* Don't pop down the menu for releases over scroll arrows
1868 if (GTK_IS_MENU (widget))
1870 GtkMenu *menu = GTK_MENU (widget);
1872 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
1876 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1880 gtk_menu_button_release (GtkWidget *widget,
1881 GdkEventButton *event)
1883 /* Don't pop down the menu for releases over scroll arrows
1885 if (GTK_IS_MENU (widget))
1887 GtkMenu *menu = GTK_MENU (widget);
1889 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
1893 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1897 gtk_menu_key_press (GtkWidget *widget,
1900 GtkMenuShell *menu_shell;
1902 gboolean delete = FALSE;
1903 gboolean can_change_accels;
1904 gchar *accel = NULL;
1905 guint accel_key, accel_mods;
1906 GdkModifierType consumed_modifiers;
1907 GdkDisplay *display;
1909 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1910 g_return_val_if_fail (event != NULL, FALSE);
1912 menu_shell = GTK_MENU_SHELL (widget);
1913 menu = GTK_MENU (widget);
1915 gtk_menu_stop_navigating_submenu (menu);
1917 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
1920 display = gtk_widget_get_display (widget);
1922 g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
1923 "gtk-menu-bar-accel", &accel,
1924 "gtk-can-change-accels", &can_change_accels,
1930 GdkModifierType mods = 0;
1931 gboolean handled = FALSE;
1933 gtk_accelerator_parse (accel, &keyval, &mods);
1936 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
1938 /* FIXME this is wrong, needs to be in the global accel resolution
1939 * thing, to properly consider i18n etc., but that probably requires
1940 * AccelGroup changes etc.
1942 if (event->keyval == keyval &&
1943 (mods & event->state) == mods)
1945 g_signal_emit_by_name (menu, "cancel", 0);
1954 switch (event->keyval)
1965 /* Figure out what modifiers went into determining the key symbol */
1966 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
1967 event->hardware_keycode, event->state, event->group,
1968 NULL, NULL, NULL, &consumed_modifiers);
1970 accel_key = gdk_keyval_to_lower (event->keyval);
1971 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
1973 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
1974 * We re-upper case when we match against the keyval, but display and save in caseless form.
1976 if (accel_key != event->keyval)
1977 accel_mods |= GDK_SHIFT_MASK;
1979 /* Modify the accelerators */
1980 if (can_change_accels &&
1981 menu_shell->active_menu_item &&
1982 GTK_BIN (menu_shell->active_menu_item)->child && /* no seperators */
1983 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
1984 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
1986 GtkWidget *menu_item = menu_shell->active_menu_item;
1987 gboolean locked, replace_accels = TRUE;
1990 path = _gtk_widget_get_accel_path (menu_item, &locked);
1991 if (!path || locked)
1993 /* can't change accelerators on menu_items without paths
1994 * (basically, those items are accelerator-locked).
1996 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
1997 gdk_display_beep (display);
2003 /* For the keys that act to delete the current setting, we delete
2004 * the current setting if there is one, otherwise, we set the
2005 * key as the accelerator.
2011 if (gtk_accel_map_lookup_entry (path, &key) &&
2012 (key.accel_key || key.accel_mods))
2018 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2022 /* we failed, probably because this key is in use and
2025 /* g_print("failed to change\n"); */
2026 gdk_display_beep (display);
2035 gtk_menu_motion_notify (GtkWidget *widget,
2036 GdkEventMotion *event)
2038 GtkWidget *menu_item;
2040 GtkMenuShell *menu_shell;
2042 gboolean need_enter;
2044 if (GTK_IS_MENU (widget))
2045 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2047 /* We received the event for one of two reasons:
2049 * a) We are the active menu, and did gtk_grab_add()
2050 * b) The widget is a child of ours, and the event was propagated
2052 * Since for computation of navigation regions, we want the menu which
2053 * is the parent of the menu item, for a), we need to find that menu,
2054 * which may be different from 'widget'.
2056 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2057 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2058 !_gtk_menu_item_is_selectable (menu_item) ||
2059 !GTK_IS_MENU (menu_item->parent))
2062 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2063 menu = GTK_MENU (menu_shell);
2065 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2067 /* Check to see if we are within an active submenu's navigation region
2069 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2074 /* The menu is now sensitive to enter events on its items, but
2075 * was previously sensitive. So we fake an enter event.
2079 menu_shell->ignore_enter = FALSE;
2081 gdk_drawable_get_size (event->window, &width, &height);
2082 if (event->x >= 0 && event->x < width &&
2083 event->y >= 0 && event->y < height)
2085 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2088 send_event->crossing.window = g_object_ref (event->window);
2089 send_event->crossing.time = event->time;
2090 send_event->crossing.send_event = TRUE;
2091 send_event->crossing.x_root = event->x_root;
2092 send_event->crossing.y_root = event->y_root;
2093 send_event->crossing.x = event->x;
2094 send_event->crossing.y = event->y;
2096 /* We send the event to 'widget', the currently active menu,
2097 * instead of 'menu', the menu that the pointer is in. This
2098 * will ensure that the event will be ignored unless the
2099 * menuitem is a child of the active menu or some parent
2100 * menu of the active menu.
2102 result = gtk_widget_event (widget, send_event);
2103 gdk_event_free (send_event);
2113 gtk_menu_scroll_timeout (gpointer data)
2118 gint view_width, view_height;
2120 GDK_THREADS_ENTER ();
2122 menu = GTK_MENU (data);
2123 widget = GTK_WIDGET (menu);
2125 offset = menu->scroll_offset + menu->scroll_step;
2127 /* If we scroll upward and the non-visible top part
2128 * is smaller than the scroll arrow it would be
2129 * pretty stupid to show the arrow and taking more
2130 * screen space than just scrolling to the top.
2132 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2135 /* Don't scroll over the top if we weren't before: */
2136 if ((menu->scroll_offset >= 0) && (offset < 0))
2139 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2141 /* Don't scroll past the bottom if we weren't before: */
2142 if (menu->scroll_offset > 0)
2143 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2145 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2146 (offset + view_height > widget->requisition.height))
2147 offset = widget->requisition.height - view_height;
2149 gtk_menu_scroll_to (menu, offset);
2151 GDK_THREADS_LEAVE ();
2157 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2159 GtkMenuShell *menu_shell;
2165 gboolean scroll_fast = FALSE;
2167 menu_shell = GTK_MENU_SHELL (menu);
2169 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2170 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2172 border = GTK_CONTAINER (menu)->border_width + GTK_WIDGET (menu)->style->ythickness;
2174 if (menu->upper_arrow_visible && !menu->tearoff_active)
2179 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2182 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2183 (y >= rect.y) && (y < rect.y + rect.height))
2186 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2189 if (enter && in_arrow &&
2190 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2192 menu->upper_arrow_prelight = TRUE;
2193 menu->scroll_fast = scroll_fast;
2194 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2196 /* Deselect the active item so that any submenus are poped down */
2197 gtk_menu_shell_deselect (menu_shell);
2199 gtk_menu_remove_scroll_timeout (menu);
2200 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2201 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2202 gtk_menu_scroll_timeout,
2205 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2207 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2209 gtk_menu_stop_scrolling (menu);
2213 if (menu->lower_arrow_visible && !menu->tearoff_active)
2216 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2218 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2221 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2222 (y >= rect.y) && (y < rect.y + rect.height))
2225 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2228 if (enter && in_arrow &&
2229 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2231 menu->lower_arrow_prelight = TRUE;
2232 menu->scroll_fast = scroll_fast;
2233 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2235 /* Deselect the active item so that any submenus are poped down */
2236 gtk_menu_shell_deselect (menu_shell);
2238 gtk_menu_remove_scroll_timeout (menu);
2239 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2240 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2241 gtk_menu_scroll_timeout,
2244 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2246 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2248 gtk_menu_stop_scrolling (menu);
2254 gtk_menu_enter_notify (GtkWidget *widget,
2255 GdkEventCrossing *event)
2257 GtkWidget *menu_item;
2259 if (widget && GTK_IS_MENU (widget))
2261 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2263 if (!menu_shell->ignore_enter)
2264 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2267 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2268 * will not correspond to the event widget's parent. Check to see
2269 * if we are in the parent's navigation region.
2271 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2272 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2273 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2276 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2280 gtk_menu_leave_notify (GtkWidget *widget,
2281 GdkEventCrossing *event)
2283 GtkMenuShell *menu_shell;
2285 GtkMenuItem *menu_item;
2286 GtkWidget *event_widget;
2288 menu = GTK_MENU (widget);
2289 menu_shell = GTK_MENU_SHELL (widget);
2291 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2294 gtk_menu_handle_scrolling (menu, FALSE);
2296 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2298 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2301 menu_item = GTK_MENU_ITEM (event_widget);
2303 /* Here we check to see if we're leaving an active menu item with a submenu,
2304 * in which case we enter submenu navigation mode.
2306 if (menu_shell->active_menu_item != NULL
2307 && menu_item->submenu != NULL
2308 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2310 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2312 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2317 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2321 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2323 if (menu->navigation_region)
2325 gdk_region_destroy (menu->navigation_region);
2326 menu->navigation_region = NULL;
2329 if (menu->navigation_timeout)
2331 gtk_timeout_remove (menu->navigation_timeout);
2332 menu->navigation_timeout = 0;
2336 /* When the timeout is elapsed, the navigation region is destroyed
2337 * and the menuitem under the pointer (if any) is selected.
2340 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2342 GtkMenu *menu = user_data;
2343 GdkWindow *child_window;
2345 GDK_THREADS_ENTER ();
2347 gtk_menu_stop_navigating_submenu (menu);
2349 if (GTK_WIDGET_REALIZED (menu))
2351 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2355 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2357 send_event->crossing.window = g_object_ref (child_window);
2358 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2359 send_event->crossing.send_event = TRUE;
2361 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2363 gdk_event_free (send_event);
2367 GDK_THREADS_LEAVE ();
2373 gtk_menu_navigating_submenu (GtkMenu *menu,
2377 if (menu->navigation_region)
2379 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2383 gtk_menu_stop_navigating_submenu (menu);
2390 #undef DRAW_STAY_UP_TRIANGLE
2392 #ifdef DRAW_STAY_UP_TRIANGLE
2395 draw_stay_up_triangle (GdkWindow *window,
2398 /* Draw ugly color all over the stay-up triangle */
2399 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2400 GdkGCValues gc_values;
2402 GdkRectangle clipbox;
2404 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2405 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2406 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2407 gdk_gc_set_clip_region (ugly_gc, region);
2409 gdk_region_get_clipbox (region, &clipbox);
2411 gdk_draw_rectangle (window,
2414 clipbox.x, clipbox.y,
2415 clipbox.width, clipbox.height);
2417 g_object_unref (G_OBJECT (ugly_gc));
2422 flip_region (GdkRegion *region,
2427 GdkRectangle *rectangles;
2428 GdkRectangle clipbox;
2429 GdkRegion *new_region;
2432 new_region = gdk_region_new ();
2434 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
2435 gdk_region_get_clipbox (region, &clipbox);
2437 for (i = 0; i < n_rectangles; ++i)
2439 GdkRectangle rect = rectangles[i];
2442 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
2445 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
2447 gdk_region_union_with_rect (new_region, &rect);
2450 g_free (rectangles);
2456 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
2457 GtkMenuItem *menu_item,
2458 GdkEventCrossing *event)
2460 gint submenu_left = 0;
2461 gint submenu_right = 0;
2462 gint submenu_top = 0;
2463 gint submenu_bottom = 0;
2467 GtkWidget *event_widget;
2469 g_return_if_fail (menu_item->submenu != NULL);
2470 g_return_if_fail (event != NULL);
2472 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2474 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
2475 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
2477 submenu_right = submenu_left + width;
2478 submenu_bottom = submenu_top + height;
2480 gdk_drawable_get_size (event_widget->window, &width, &height);
2482 if (event->x >= 0 && event->x < width)
2485 gboolean flip_y = FALSE;
2486 gboolean flip_x = FALSE;
2488 gtk_menu_stop_navigating_submenu (menu);
2490 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2493 point[0].x = event->x_root;
2494 point[1].x = submenu_left;
2499 point[0].x = event->x_root + 1;
2500 point[1].x = 2 * (event->x_root + 1) - submenu_right;
2508 point[0].y = event->y_root + 1;
2509 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
2511 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
2519 point[0].y = event->y_root;
2520 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
2522 if (point[0].y >= submenu_bottom)
2526 point[2].x = point[1].x;
2527 point[2].y = point[0].y;
2529 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
2531 if (flip_x || flip_y)
2533 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
2534 gdk_region_destroy (menu->navigation_region);
2535 menu->navigation_region = new_region;
2538 g_object_get (G_OBJECT (gtk_widget_get_settings (GTK_WIDGET (menu))),
2539 "gtk-menu-popdown-delay", &popdown_delay,
2542 menu->navigation_timeout = gtk_timeout_add (popdown_delay,
2543 gtk_menu_stop_navigating_submenu_cb, menu);
2545 #ifdef DRAW_STAY_UP_TRIANGLE
2546 draw_stay_up_triangle (gdk_get_default_root_window(),
2547 menu->navigation_region);
2553 gtk_menu_deactivate (GtkMenuShell *menu_shell)
2557 g_return_if_fail (GTK_IS_MENU (menu_shell));
2559 parent = menu_shell->parent_menu_shell;
2561 menu_shell->activate_time = 0;
2562 gtk_menu_popdown (GTK_MENU (menu_shell));
2565 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
2569 gtk_menu_position (GtkMenu *menu)
2572 GtkRequisition requisition;
2573 GtkMenuPrivate *private;
2579 GdkRectangle monitor;
2582 g_return_if_fail (GTK_IS_MENU (menu));
2584 widget = GTK_WIDGET (menu);
2586 gdk_window_get_pointer (gtk_widget_get_root_window (widget),
2589 screen = gtk_widget_get_screen (widget);
2590 monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
2591 if (monitor_num < 0)
2593 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2595 /* We need the requisition to figure out the right place to
2596 * popup the menu. In fact, we always need to ask here, since
2597 * if a size_request was queued while we weren't popped up,
2598 * the requisition won't have been recomputed yet.
2600 gtk_widget_size_request (widget, &requisition);
2604 if (menu->position_func)
2605 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
2608 x = CLAMP (x - 2, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2609 y = CLAMP (y - 2, monitor.y, MAX (monitor.y, monitor.y + monitor.height - requisition.height));
2616 menu_height = GTK_WIDGET (menu)->requisition.height;
2618 if (y + menu_height > monitor.y + monitor.height)
2620 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
2621 y = (monitor.y + monitor.height) - menu_height;
2631 /* FIXME: should this be done in the various position_funcs ? */
2632 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2634 if (y + requisition.height > monitor.y + monitor.height)
2635 requisition.height = (monitor.y + monitor.height) - y;
2640 requisition.height -= -y;
2644 if (scroll_offset > 0)
2645 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
2647 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
2650 if (GTK_MENU_SHELL (menu)->active)
2652 private = gtk_menu_get_private (menu);
2653 private->have_position = TRUE;
2657 gtk_widget_queue_resize (menu->toplevel);
2661 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
2662 requisition.width, requisition.height);
2665 menu->scroll_offset = scroll_offset;
2669 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
2671 if (menu->timeout_id)
2673 g_source_remove (menu->timeout_id);
2674 menu->timeout_id = 0;
2679 gtk_menu_stop_scrolling (GtkMenu *menu)
2681 gtk_menu_remove_scroll_timeout (menu);
2683 menu->upper_arrow_prelight = FALSE;
2684 menu->lower_arrow_prelight = FALSE;
2688 gtk_menu_scroll_to (GtkMenu *menu,
2693 gint view_width, view_height;
2695 gboolean last_visible;
2698 widget = GTK_WIDGET (menu);
2700 if (menu->tearoff_active &&
2701 menu->tearoff_adjustment &&
2702 (menu->tearoff_adjustment->value != offset))
2704 menu->tearoff_adjustment->value = offset;
2705 gtk_adjustment_value_changed (menu->tearoff_adjustment);
2708 /* Move/resize the viewport according to arrows: */
2709 view_width = widget->allocation.width;
2710 view_height = widget->allocation.height;
2712 border_width = GTK_CONTAINER (menu)->border_width;
2713 view_width -= (border_width + widget->style->xthickness) * 2;
2714 view_height -= (border_width + widget->style->ythickness) * 2;
2715 menu_height = widget->requisition.height - (border_width + widget->style->ythickness) * 2;
2717 x = border_width + widget->style->xthickness;
2718 y = border_width + widget->style->ythickness;
2720 if (!menu->tearoff_active)
2722 last_visible = menu->upper_arrow_visible;
2723 menu->upper_arrow_visible = (offset > 0);
2725 if (menu->upper_arrow_visible)
2726 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2728 if ( (last_visible != menu->upper_arrow_visible) &&
2729 !menu->upper_arrow_visible)
2731 menu->upper_arrow_prelight = FALSE;
2733 /* If we hid the upper arrow, possibly remove timeout */
2734 if (menu->scroll_step < 0)
2735 gtk_menu_stop_scrolling (menu);
2738 last_visible = menu->lower_arrow_visible;
2739 menu->lower_arrow_visible = (view_height + offset < menu_height);
2741 if (menu->lower_arrow_visible)
2742 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2744 if ( (last_visible != menu->lower_arrow_visible) &&
2745 !menu->lower_arrow_visible)
2747 menu->lower_arrow_prelight = FALSE;
2749 /* If we hid the lower arrow, possibly remove timeout */
2750 if (menu->scroll_step > 0)
2751 gtk_menu_stop_scrolling (menu);
2754 if (menu->upper_arrow_visible)
2755 y += MENU_SCROLL_ARROW_HEIGHT;
2758 offset = CLAMP (offset, 0, menu_height - view_height);
2760 /* Scroll the menu: */
2761 if (GTK_WIDGET_REALIZED (menu))
2762 gdk_window_move (menu->bin_window, 0, -offset);
2764 if (GTK_WIDGET_REALIZED (menu))
2765 gdk_window_move_resize (menu->view_window,
2771 menu->scroll_offset = offset;
2775 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
2776 GtkWidget *menu_item)
2781 GtkRequisition child_requisition;
2782 gint child_offset, child_height;
2786 gboolean last_child = 0;
2788 menu = GTK_MENU (menu_shell);
2790 /* We need to check if the selected item fully visible.
2791 * If not we need to scroll the menu so that it becomes fully
2798 children = menu_shell->children;
2801 child = children->data;
2802 children = children->next;
2804 if (GTK_WIDGET_VISIBLE (child))
2806 gtk_widget_size_request (child, &child_requisition);
2807 child_offset += child_height;
2808 child_height = child_requisition.height;
2811 if (child == menu_item)
2813 last_child = (children == NULL);
2818 if (child == menu_item)
2820 y = menu->scroll_offset;
2821 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2823 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness;
2825 if (child_offset + child_height <= y)
2827 /* Ignore the enter event we might get if the pointer is on the menu
2829 menu_shell->ignore_enter = TRUE;
2830 gtk_menu_scroll_to (menu, child_offset);
2835 if (menu->upper_arrow_visible && !menu->tearoff_active)
2836 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2837 if (menu->lower_arrow_visible && !menu->tearoff_active)
2838 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2840 if (child_offset >= y + height - arrow_height)
2843 if (!last_child && !menu->tearoff_active)
2844 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2846 y = child_offset + child_height - height + arrow_height;
2847 if ((y > 0) && !menu->tearoff_active)
2849 /* Need upper arrow */
2850 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2851 y = child_offset + child_height - height + arrow_height;
2853 /* Ignore the enter event we might get if the pointer is on the menu
2855 menu_shell->ignore_enter = TRUE;
2856 gtk_menu_scroll_to (menu, y);
2864 gtk_menu_select_item (GtkMenuShell *menu_shell,
2865 GtkWidget *menu_item)
2867 GtkMenu *menu = GTK_MENU (menu_shell);
2869 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
2870 gtk_menu_scroll_item_visible (menu_shell, menu_item);
2872 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
2876 /* Reparent the menu, taking care of the refcounting
2878 * If unrealize is true we force a unrealize while reparenting the parent.
2879 * This can help eliminate flicker in some cases.
2881 * What happens is that when the menu is unrealized and then re-realized,
2882 * the allocations are as follows:
2884 * parent - 1x1 at (0,0)
2885 * child1 - 100x20 at (0,0)
2886 * child2 - 100x20 at (0,20)
2887 * child3 - 100x20 at (0,40)
2889 * That is, the parent is small but the children are full sized. Then,
2890 * when the queued_resize gets processed, the parent gets resized to
2893 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
2894 * contains the following logic:
2896 * - if a move or resize operation on a window would change the clip
2897 * region on the children, then before the window is resized
2898 * the background for children is temporarily set to None, the
2899 * move/resize done, and the background for the children restored.
2901 * So, at the point where the parent is resized to final size, the
2902 * background for the children is temporarily None, and thus they
2903 * are not cleared to the background color and the previous background
2904 * (the image of the menu) is left in place.
2907 gtk_menu_reparent (GtkMenu *menu,
2908 GtkWidget *new_parent,
2911 GtkObject *object = GTK_OBJECT (menu);
2912 GtkWidget *widget = GTK_WIDGET (menu);
2913 gboolean was_floating = GTK_OBJECT_FLOATING (object);
2915 g_object_ref (object);
2916 gtk_object_sink (object);
2920 g_object_ref (object);
2921 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
2922 gtk_container_add (GTK_CONTAINER (new_parent), widget);
2923 g_object_unref (object);
2926 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
2929 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
2931 g_object_unref (object);
2935 gtk_menu_show_all (GtkWidget *widget)
2937 g_return_if_fail (GTK_IS_MENU (widget));
2939 /* Show children, but not self. */
2940 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2945 gtk_menu_hide_all (GtkWidget *widget)
2947 g_return_if_fail (GTK_IS_MENU (widget));
2949 /* Hide children, but not self. */
2950 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
2954 * gtk_menu_set_screen:
2955 * @menu: a #GtkMenu.
2956 * @screen: a #GdkScreen, or %NULL if the screen should be
2957 * determined by the widget the menu is attached to.
2959 * Sets the #GdkScreen on which the menu will be displayed.
2964 gtk_menu_set_screen (GtkMenu *menu,
2967 g_return_if_fail (GTK_IS_MENU (menu));
2968 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
2970 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
2974 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), screen);
2978 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
2980 attach_widget_screen_changed (attach_widget, NULL, menu);
2986 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
2990 g_object_get (G_OBJECT (gtk_widget_get_settings (GTK_WIDGET (menu_shell))),
2991 "gtk-menu-popup-delay", &popup_delay,