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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * @Short_description: A menu widget
30 * A #GtkMenu is a #GtkMenuShell that implements a drop down menu
31 * consisting of a list of #GtkMenuItem objects which can be navigated
32 * and activated by the user to perform application functions.
34 * A #GtkMenu is most commonly dropped down by activating a
35 * #GtkMenuItem in a #GtkMenuBar or popped up by activating a
36 * #GtkMenuItem in another #GtkMenu.
38 * A #GtkMenu can also be popped up by activating a #GtkOptionMenu.
39 * Other composite widgets such as the #GtkNotebook can pop up a
42 * Applications can display a #GtkMenu as a popup menu by calling the
43 * gtk_menu_popup() function. The example below shows how an application
44 * can pop up a menu when the 3rd mouse button is pressed.
47 * <title>Connecting the popup signal handler.</title>
49 * /<!---->* connect our handler which will popup the menu *<!---->/
50 * g_signal_connect_swapped (window, "button_press_event",
51 * G_CALLBACK (my_popup_handler), menu);
56 * <title>Signal handler which displays a popup menu.</title>
59 * my_popup_handler (GtkWidget *widget, GdkEvent *event)
62 * GdkEventButton *event_button;
64 * g_return_val_if_fail (widget != NULL, FALSE);
65 * g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
66 * g_return_val_if_fail (event != NULL, FALSE);
68 * /<!---->* The "widget" is the menu that was supplied when
69 * * g_signal_connect_swapped() was called.
71 * menu = GTK_MENU (widget);
73 * if (event->type == GDK_BUTTON_PRESS)
75 * event_button = (GdkEventButton *) event;
76 * if (event_button->button == GDK_BUTTON_SECONDARY)
78 * gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
79 * event_button->button, event_button->time);
94 #include <gobject/gvaluecollector.h>
96 #include "gtkaccellabel.h"
97 #include "gtkaccelmap.h"
98 #include "gtkbindings.h"
99 #include "gtkcheckmenuitem.h"
101 #include "gtkmarshalers.h"
102 #include "gtkmenuprivate.h"
103 #include "gtkmenuitemprivate.h"
104 #include "gtkmenushellprivate.h"
105 #include "gtkwindow.h"
107 #include "gtkscrollbar.h"
108 #include "gtksettings.h"
109 #include "gtkprivate.h"
110 #include "gtkwidgetprivate.h"
113 #include "gtktypebuiltins.h"
114 #include "gtkwidgetprivate.h"
116 #include "deprecated/gtktearoffmenuitem.h"
119 #include "a11y/gtkmenuaccessible.h"
121 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
122 * extends below the submenu
125 #define MENU_SCROLL_STEP1 8
126 #define MENU_SCROLL_STEP2 15
127 #define MENU_SCROLL_FAST_ZONE 8
128 #define MENU_SCROLL_TIMEOUT1 50
129 #define MENU_SCROLL_TIMEOUT2 20
131 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
132 #define ATTACHED_MENUS "gtk-attached-menus"
134 typedef struct _GtkMenuAttachData GtkMenuAttachData;
135 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
137 struct _GtkMenuAttachData
139 GtkWidget *attach_widget;
140 GtkMenuDetachFunc detacher;
143 struct _GtkMenuPopdownData
155 gint effective_left_attach;
156 gint effective_right_attach;
157 gint effective_top_attach;
158 gint effective_bottom_attach;
175 PROP_RESERVE_TOGGLE_SIZE
180 CHILD_PROP_LEFT_ATTACH,
181 CHILD_PROP_RIGHT_ATTACH,
182 CHILD_PROP_TOP_ATTACH,
183 CHILD_PROP_BOTTOM_ATTACH
186 static void gtk_menu_set_property (GObject *object,
190 static void gtk_menu_get_property (GObject *object,
194 static void gtk_menu_set_child_property(GtkContainer *container,
199 static void gtk_menu_get_child_property(GtkContainer *container,
204 static void gtk_menu_destroy (GtkWidget *widget);
205 static void gtk_menu_realize (GtkWidget *widget);
206 static void gtk_menu_unrealize (GtkWidget *widget);
207 static void gtk_menu_size_allocate (GtkWidget *widget,
208 GtkAllocation *allocation);
209 static void gtk_menu_show (GtkWidget *widget);
210 static gboolean gtk_menu_draw (GtkWidget *widget,
212 static gboolean gtk_menu_key_press (GtkWidget *widget,
214 static gboolean gtk_menu_scroll (GtkWidget *widget,
215 GdkEventScroll *event);
216 static gboolean gtk_menu_button_press (GtkWidget *widget,
217 GdkEventButton *event);
218 static gboolean gtk_menu_button_release (GtkWidget *widget,
219 GdkEventButton *event);
220 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
221 GdkEventMotion *event);
222 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
223 GdkEventCrossing *event);
224 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
225 GdkEventCrossing *event);
226 static void gtk_menu_scroll_to (GtkMenu *menu,
228 static void gtk_menu_grab_notify (GtkWidget *widget,
229 gboolean was_grabbed);
230 static gboolean gtk_menu_captured_event (GtkWidget *widget,
234 static void gtk_menu_stop_scrolling (GtkMenu *menu);
235 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
236 static gboolean gtk_menu_scroll_timeout (gpointer data);
237 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
238 static void gtk_menu_start_scrolling (GtkMenu *menu);
240 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
241 GtkWidget *menu_item);
242 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
243 GtkWidget *menu_item);
244 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
247 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
249 static void gtk_menu_handle_scrolling (GtkMenu *menu,
254 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
256 static void gtk_menu_style_updated (GtkWidget *widget);
257 static gboolean gtk_menu_focus (GtkWidget *widget,
258 GtkDirectionType direction);
259 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
260 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
261 GtkMenuDirectionType direction);
262 static void gtk_menu_real_move_scroll (GtkMenu *menu,
265 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
266 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
267 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
270 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
271 GtkMenuItem *menu_item,
272 GdkEventCrossing *event);
274 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
275 static void gtk_menu_show_all (GtkWidget *widget);
276 static void gtk_menu_position (GtkMenu *menu,
277 gboolean set_scroll_offset);
278 static void gtk_menu_reparent (GtkMenu *menu,
279 GtkWidget *new_parent,
281 static void gtk_menu_remove (GtkContainer *menu,
284 static void gtk_menu_update_title (GtkMenu *menu);
286 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
287 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
289 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
291 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
292 gboolean group_changed);
294 static void gtk_menu_get_preferred_width (GtkWidget *widget,
297 static void gtk_menu_get_preferred_height (GtkWidget *widget,
300 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
306 static const gchar attach_data_key[] = "gtk-menu-attach-data";
308 static guint menu_signals[LAST_SIGNAL] = { 0 };
310 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
313 menu_queue_resize (GtkMenu *menu)
315 GtkMenuPrivate *priv = menu->priv;
317 priv->have_layout = FALSE;
318 gtk_widget_queue_resize (GTK_WIDGET (menu));
322 attach_info_free (AttachInfo *info)
324 g_slice_free (AttachInfo, info);
328 get_attach_info (GtkWidget *child)
330 GObject *object = G_OBJECT (child);
331 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
335 ai = g_slice_new0 (AttachInfo);
336 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
337 (GDestroyNotify) attach_info_free);
344 is_grid_attached (AttachInfo *ai)
346 return (ai->left_attach >= 0 &&
347 ai->right_attach >= 0 &&
348 ai->top_attach >= 0 &&
349 ai->bottom_attach >= 0);
353 menu_ensure_layout (GtkMenu *menu)
355 GtkMenuPrivate *priv = menu->priv;
357 if (!priv->have_layout)
359 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
363 gint max_right_attach;
364 gint max_bottom_attach;
366 /* Find extents of gridded portion
368 max_right_attach = 1;
369 max_bottom_attach = 0;
371 for (l = menu_shell->priv->children; l; l = l->next)
373 GtkWidget *child = l->data;
374 AttachInfo *ai = get_attach_info (child);
376 if (is_grid_attached (ai))
378 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
379 max_right_attach = MAX (max_right_attach, ai->right_attach);
383 /* Find empty rows */
384 row_occupied = g_malloc0 (max_bottom_attach);
386 for (l = menu_shell->priv->children; l; l = l->next)
388 GtkWidget *child = l->data;
389 AttachInfo *ai = get_attach_info (child);
391 if (is_grid_attached (ai))
395 for (i = ai->top_attach; i < ai->bottom_attach; i++)
396 row_occupied[i] = TRUE;
400 /* Lay non-grid-items out in those rows
403 for (l = menu_shell->priv->children; l; l = l->next)
405 GtkWidget *child = l->data;
406 AttachInfo *ai = get_attach_info (child);
408 if (!is_grid_attached (ai))
410 while (current_row < max_bottom_attach && row_occupied[current_row])
413 ai->effective_left_attach = 0;
414 ai->effective_right_attach = max_right_attach;
415 ai->effective_top_attach = current_row;
416 ai->effective_bottom_attach = current_row + 1;
422 ai->effective_left_attach = ai->left_attach;
423 ai->effective_right_attach = ai->right_attach;
424 ai->effective_top_attach = ai->top_attach;
425 ai->effective_bottom_attach = ai->bottom_attach;
429 g_free (row_occupied);
431 priv->n_rows = MAX (current_row, max_bottom_attach);
432 priv->n_columns = max_right_attach;
433 priv->have_layout = TRUE;
439 gtk_menu_get_n_columns (GtkMenu *menu)
441 GtkMenuPrivate *priv = menu->priv;
443 menu_ensure_layout (menu);
445 return priv->n_columns;
449 gtk_menu_get_n_rows (GtkMenu *menu)
451 GtkMenuPrivate *priv = menu->priv;
453 menu_ensure_layout (menu);
459 get_effective_child_attach (GtkWidget *child,
465 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
468 menu_ensure_layout (menu);
470 ai = get_attach_info (child);
473 *l = ai->effective_left_attach;
475 *r = ai->effective_right_attach;
477 *t = ai->effective_top_attach;
479 *b = ai->effective_bottom_attach;
484 gtk_menu_class_init (GtkMenuClass *class)
486 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
487 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
488 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
489 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
490 GtkBindingSet *binding_set;
492 gobject_class->set_property = gtk_menu_set_property;
493 gobject_class->get_property = gtk_menu_get_property;
495 widget_class->destroy = gtk_menu_destroy;
496 widget_class->realize = gtk_menu_realize;
497 widget_class->unrealize = gtk_menu_unrealize;
498 widget_class->size_allocate = gtk_menu_size_allocate;
499 widget_class->show = gtk_menu_show;
500 widget_class->draw = gtk_menu_draw;
501 widget_class->scroll_event = gtk_menu_scroll;
502 widget_class->key_press_event = gtk_menu_key_press;
503 widget_class->button_press_event = gtk_menu_button_press;
504 widget_class->button_release_event = gtk_menu_button_release;
505 widget_class->motion_notify_event = gtk_menu_motion_notify;
506 widget_class->show_all = gtk_menu_show_all;
507 widget_class->enter_notify_event = gtk_menu_enter_notify;
508 widget_class->leave_notify_event = gtk_menu_leave_notify;
509 widget_class->style_updated = gtk_menu_style_updated;
510 widget_class->focus = gtk_menu_focus;
511 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
512 widget_class->grab_notify = gtk_menu_grab_notify;
513 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
514 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
515 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
517 container_class->remove = gtk_menu_remove;
518 container_class->get_child_property = gtk_menu_get_child_property;
519 container_class->set_child_property = gtk_menu_set_child_property;
521 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
522 menu_shell_class->deactivate = gtk_menu_deactivate;
523 menu_shell_class->select_item = gtk_menu_select_item;
524 menu_shell_class->insert = gtk_menu_real_insert;
525 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
526 menu_shell_class->move_current = gtk_menu_move_current;
529 * GtkMenu::move-scroll:
531 * @scroll_type: a #GtkScrollType
533 menu_signals[MOVE_SCROLL] =
534 g_signal_new_class_handler (I_("move-scroll"),
535 G_OBJECT_CLASS_TYPE (gobject_class),
536 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
537 G_CALLBACK (gtk_menu_real_move_scroll),
539 _gtk_marshal_VOID__ENUM,
541 GTK_TYPE_SCROLL_TYPE);
546 * The index of the currently selected menu item, or -1 if no
547 * menu item is selected.
551 g_object_class_install_property (gobject_class,
553 g_param_spec_int ("active",
555 P_("The currently selected menu item"),
557 GTK_PARAM_READWRITE));
560 * GtkMenu:accel-group:
562 * The accel group holding accelerators for the menu.
566 g_object_class_install_property (gobject_class,
568 g_param_spec_object ("accel-group",
570 P_("The accel group holding accelerators for the menu"),
571 GTK_TYPE_ACCEL_GROUP,
572 GTK_PARAM_READWRITE));
575 * GtkMenu:accel-path:
577 * An accel path used to conveniently construct accel paths of child items.
581 g_object_class_install_property (gobject_class,
583 g_param_spec_string ("accel-path",
585 P_("An accel path used to conveniently construct accel paths of child items"),
587 GTK_PARAM_READWRITE));
590 * GtkMenu:attach-widget:
592 * The widget the menu is attached to. Setting this property attaches
593 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
594 * use gtk_menu_attach_to_widget() directly.
598 g_object_class_install_property (gobject_class,
600 g_param_spec_object ("attach-widget",
602 P_("The widget the menu is attached to"),
604 GTK_PARAM_READWRITE));
606 g_object_class_install_property (gobject_class,
608 g_param_spec_string ("tearoff-title",
610 P_("A title that may be displayed by the window manager when this menu is torn-off"),
612 GTK_PARAM_READWRITE));
615 * GtkMenu:tearoff-state:
617 * A boolean that indicates whether the menu is torn-off.
621 g_object_class_install_property (gobject_class,
623 g_param_spec_boolean ("tearoff-state",
625 P_("A boolean that indicates whether the menu is torn-off"),
627 GTK_PARAM_READWRITE));
632 * The monitor the menu will be popped up on.
636 g_object_class_install_property (gobject_class,
638 g_param_spec_int ("monitor",
640 P_("The monitor the menu will be popped up on"),
642 GTK_PARAM_READWRITE));
644 gtk_widget_class_install_style_property (widget_class,
645 g_param_spec_int ("vertical-padding",
646 P_("Vertical Padding"),
647 P_("Extra space at the top and bottom of the menu"),
651 GTK_PARAM_READABLE));
654 * GtkMenu:reserve-toggle-size:
656 * A boolean that indicates whether the menu reserves space for
657 * toggles and icons, regardless of their actual presence.
659 * This property should only be changed from its default value
660 * for special-purposes such as tabular menus. Regular menus that
661 * are connected to a menu bar or context menus should reserve
662 * toggle space for consistency.
666 g_object_class_install_property (gobject_class,
667 PROP_RESERVE_TOGGLE_SIZE,
668 g_param_spec_boolean ("reserve-toggle-size",
669 P_("Reserve Toggle Size"),
670 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
672 GTK_PARAM_READWRITE));
674 gtk_widget_class_install_style_property (widget_class,
675 g_param_spec_int ("horizontal-padding",
676 P_("Horizontal Padding"),
677 P_("Extra space at the left and right edges of the menu"),
681 GTK_PARAM_READABLE));
683 gtk_widget_class_install_style_property (widget_class,
684 g_param_spec_int ("vertical-offset",
685 P_("Vertical Offset"),
686 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
690 GTK_PARAM_READABLE));
692 gtk_widget_class_install_style_property (widget_class,
693 g_param_spec_int ("horizontal-offset",
694 P_("Horizontal Offset"),
695 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
699 GTK_PARAM_READABLE));
701 gtk_widget_class_install_style_property (widget_class,
702 g_param_spec_boolean ("double-arrows",
704 P_("When scrolling, always show both arrows."),
706 GTK_PARAM_READABLE));
709 * GtkMenu:arrow-placement:
711 * Indicates where scroll arrows should be placed.
715 gtk_widget_class_install_style_property (widget_class,
716 g_param_spec_enum ("arrow-placement",
717 P_("Arrow Placement"),
718 P_("Indicates where scroll arrows should be placed"),
719 GTK_TYPE_ARROW_PLACEMENT,
721 GTK_PARAM_READABLE));
723 gtk_container_class_install_child_property (container_class,
724 CHILD_PROP_LEFT_ATTACH,
725 g_param_spec_int ("left-attach",
727 P_("The column number to attach the left side of the child to"),
729 GTK_PARAM_READWRITE));
731 gtk_container_class_install_child_property (container_class,
732 CHILD_PROP_RIGHT_ATTACH,
733 g_param_spec_int ("right-attach",
735 P_("The column number to attach the right side of the child to"),
737 GTK_PARAM_READWRITE));
739 gtk_container_class_install_child_property (container_class,
740 CHILD_PROP_TOP_ATTACH,
741 g_param_spec_int ("top-attach",
743 P_("The row number to attach the top of the child to"),
745 GTK_PARAM_READWRITE));
747 gtk_container_class_install_child_property (container_class,
748 CHILD_PROP_BOTTOM_ATTACH,
749 g_param_spec_int ("bottom-attach",
751 P_("The row number to attach the bottom of the child to"),
753 GTK_PARAM_READWRITE));
756 * GtkMenu:arrow-scaling
758 * Arbitrary constant to scale down the size of the scroll arrow.
762 gtk_widget_class_install_style_property (widget_class,
763 g_param_spec_float ("arrow-scaling",
765 P_("Arbitrary constant to scale down the size of the scroll arrow"),
767 GTK_PARAM_READABLE));
769 binding_set = gtk_binding_set_by_class (class);
770 gtk_binding_entry_add_signal (binding_set,
772 I_("move-current"), 1,
773 GTK_TYPE_MENU_DIRECTION_TYPE,
775 gtk_binding_entry_add_signal (binding_set,
778 GTK_TYPE_MENU_DIRECTION_TYPE,
780 gtk_binding_entry_add_signal (binding_set,
783 GTK_TYPE_MENU_DIRECTION_TYPE,
785 gtk_binding_entry_add_signal (binding_set,
788 GTK_TYPE_MENU_DIRECTION_TYPE,
790 gtk_binding_entry_add_signal (binding_set,
793 GTK_TYPE_MENU_DIRECTION_TYPE,
794 GTK_MENU_DIR_PARENT);
795 gtk_binding_entry_add_signal (binding_set,
798 GTK_TYPE_MENU_DIRECTION_TYPE,
799 GTK_MENU_DIR_PARENT);
800 gtk_binding_entry_add_signal (binding_set,
803 GTK_TYPE_MENU_DIRECTION_TYPE,
805 gtk_binding_entry_add_signal (binding_set,
808 GTK_TYPE_MENU_DIRECTION_TYPE,
810 gtk_binding_entry_add_signal (binding_set,
813 GTK_TYPE_SCROLL_TYPE,
815 gtk_binding_entry_add_signal (binding_set,
818 GTK_TYPE_SCROLL_TYPE,
820 gtk_binding_entry_add_signal (binding_set,
823 GTK_TYPE_SCROLL_TYPE,
825 gtk_binding_entry_add_signal (binding_set,
828 GTK_TYPE_SCROLL_TYPE,
830 gtk_binding_entry_add_signal (binding_set,
833 GTK_TYPE_SCROLL_TYPE,
835 gtk_binding_entry_add_signal (binding_set,
836 GDK_KEY_KP_Page_Up, 0,
838 GTK_TYPE_SCROLL_TYPE,
840 gtk_binding_entry_add_signal (binding_set,
841 GDK_KEY_Page_Down, 0,
843 GTK_TYPE_SCROLL_TYPE,
844 GTK_SCROLL_PAGE_DOWN);
845 gtk_binding_entry_add_signal (binding_set,
846 GDK_KEY_KP_Page_Down, 0,
848 GTK_TYPE_SCROLL_TYPE,
849 GTK_SCROLL_PAGE_DOWN);
851 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
853 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
858 gtk_menu_set_property (GObject *object,
863 GtkMenu *menu = GTK_MENU (object);
868 gtk_menu_set_active (menu, g_value_get_int (value));
870 case PROP_ACCEL_GROUP:
871 gtk_menu_set_accel_group (menu, g_value_get_object (value));
873 case PROP_ACCEL_PATH:
874 gtk_menu_set_accel_path (menu, g_value_get_string (value));
876 case PROP_ATTACH_WIDGET:
880 widget = gtk_menu_get_attach_widget (menu);
882 gtk_menu_detach (menu);
884 widget = (GtkWidget*) g_value_get_object (value);
886 gtk_menu_attach_to_widget (menu, widget, NULL);
889 case PROP_TEAROFF_STATE:
890 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
892 case PROP_TEAROFF_TITLE:
893 gtk_menu_set_title (menu, g_value_get_string (value));
896 gtk_menu_set_monitor (menu, g_value_get_int (value));
898 case PROP_RESERVE_TOGGLE_SIZE:
899 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
902 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
908 gtk_menu_get_property (GObject *object,
913 GtkMenu *menu = GTK_MENU (object);
918 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
920 case PROP_ACCEL_GROUP:
921 g_value_set_object (value, gtk_menu_get_accel_group (menu));
923 case PROP_ACCEL_PATH:
924 g_value_set_string (value, gtk_menu_get_accel_path (menu));
926 case PROP_ATTACH_WIDGET:
927 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
929 case PROP_TEAROFF_STATE:
930 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
932 case PROP_TEAROFF_TITLE:
933 g_value_set_string (value, gtk_menu_get_title (menu));
936 g_value_set_int (value, gtk_menu_get_monitor (menu));
938 case PROP_RESERVE_TOGGLE_SIZE:
939 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
942 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
948 gtk_menu_set_child_property (GtkContainer *container,
954 GtkMenu *menu = GTK_MENU (container);
955 AttachInfo *ai = get_attach_info (child);
959 case CHILD_PROP_LEFT_ATTACH:
960 ai->left_attach = g_value_get_int (value);
962 case CHILD_PROP_RIGHT_ATTACH:
963 ai->right_attach = g_value_get_int (value);
965 case CHILD_PROP_TOP_ATTACH:
966 ai->top_attach = g_value_get_int (value);
968 case CHILD_PROP_BOTTOM_ATTACH:
969 ai->bottom_attach = g_value_get_int (value);
973 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
977 menu_queue_resize (menu);
981 gtk_menu_get_child_property (GtkContainer *container,
987 AttachInfo *ai = get_attach_info (child);
991 case CHILD_PROP_LEFT_ATTACH:
992 g_value_set_int (value, ai->left_attach);
994 case CHILD_PROP_RIGHT_ATTACH:
995 g_value_set_int (value, ai->right_attach);
997 case CHILD_PROP_TOP_ATTACH:
998 g_value_set_int (value, ai->top_attach);
1000 case CHILD_PROP_BOTTOM_ATTACH:
1001 g_value_set_int (value, ai->bottom_attach);
1005 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1011 gtk_menu_window_event (GtkWidget *window,
1015 gboolean handled = FALSE;
1017 g_object_ref (window);
1018 g_object_ref (menu);
1020 switch (event->type)
1023 case GDK_KEY_RELEASE:
1024 handled = gtk_widget_event (menu, event);
1026 case GDK_WINDOW_STATE:
1027 /* Window for the menu has been closed by the display server or by GDK.
1028 * Update the internal state as if the user had clicked outside the
1031 if (event->window_state.new_window_state & GDK_WINDOW_STATE_WITHDRAWN &&
1032 event->window_state.changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
1033 gtk_menu_shell_deactivate (GTK_MENU_SHELL(menu));
1039 g_object_unref (window);
1040 g_object_unref (menu);
1046 gtk_menu_init (GtkMenu *menu)
1048 GtkMenuPrivate *priv;
1049 GtkStyleContext *context;
1051 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1055 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1056 "type", GTK_WINDOW_POPUP,
1059 "signal::event", gtk_menu_window_event, menu,
1060 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1062 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1063 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1065 /* Refloat the menu, so that reference counting for the menu isn't
1066 * affected by it being a child of the toplevel
1068 g_object_force_floating (G_OBJECT (menu));
1069 priv->needs_destruction_ref = TRUE;
1071 priv->monitor_num = -1;
1072 priv->drag_start_y = -1;
1074 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1075 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1077 _gtk_widget_set_captured_event_handler (GTK_WIDGET (menu), gtk_menu_captured_event);
1081 gtk_menu_destroy (GtkWidget *widget)
1083 GtkMenu *menu = GTK_MENU (widget);
1084 GtkMenuPrivate *priv = menu->priv;
1085 GtkMenuAttachData *data;
1087 gtk_menu_remove_scroll_timeout (menu);
1089 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1091 gtk_menu_detach (menu);
1093 gtk_menu_stop_navigating_submenu (menu);
1095 if (priv->old_active_menu_item)
1096 g_clear_object (&priv->old_active_menu_item);
1098 /* Add back the reference count for being a child */
1099 if (priv->needs_destruction_ref)
1101 priv->needs_destruction_ref = FALSE;
1102 g_object_ref (widget);
1105 if (priv->accel_group)
1106 g_clear_object (&priv->accel_group);
1109 gtk_widget_destroy (priv->toplevel);
1111 if (priv->tearoff_window)
1112 gtk_widget_destroy (priv->tearoff_window);
1116 g_free (priv->heights);
1117 priv->heights = NULL;
1122 g_free (priv->title);
1126 if (priv->position_func_data_destroy)
1128 priv->position_func_data_destroy (priv->position_func_data);
1129 priv->position_func_data = NULL;
1130 priv->position_func_data_destroy = NULL;
1133 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1137 menu_change_screen (GtkMenu *menu,
1138 GdkScreen *new_screen)
1140 GtkMenuPrivate *priv = menu->priv;
1142 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1144 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1150 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1151 gtk_menu_position (menu, TRUE);
1154 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1155 priv->monitor_num = -1;
1159 attach_widget_screen_changed (GtkWidget *attach_widget,
1160 GdkScreen *previous_screen,
1163 if (gtk_widget_has_screen (attach_widget) &&
1164 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1165 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1169 * gtk_menu_attach_to_widget:
1171 * @attach_widget: the #GtkWidget that the menu will be attached to
1172 * @detacher: (scope async)(allow-none): the user supplied callback function
1173 * that will be called when the menu calls gtk_menu_detach()
1175 * Attaches the menu to the widget and provides a callback function
1176 * that will be invoked when the menu calls gtk_menu_detach() during
1180 gtk_menu_attach_to_widget (GtkMenu *menu,
1181 GtkWidget *attach_widget,
1182 GtkMenuDetachFunc detacher)
1184 GtkMenuAttachData *data;
1187 g_return_if_fail (GTK_IS_MENU (menu));
1188 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1190 /* keep this function in sync with gtk_widget_set_parent() */
1191 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1194 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1195 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1199 g_object_ref_sink (menu);
1201 data = g_slice_new (GtkMenuAttachData);
1202 data->attach_widget = attach_widget;
1204 g_signal_connect (attach_widget, "screen-changed",
1205 G_CALLBACK (attach_widget_screen_changed), menu);
1206 attach_widget_screen_changed (attach_widget, NULL, menu);
1208 data->detacher = detacher;
1209 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1210 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1211 if (!g_list_find (list, menu))
1212 list = g_list_prepend (list, menu);
1214 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1215 (GDestroyNotify) g_list_free);
1217 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1218 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1220 /* Attach the widget to the toplevel window. */
1221 gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), attach_widget);
1223 /* Fallback title for menu comes from attach widget */
1224 gtk_menu_update_title (menu);
1226 g_object_notify (G_OBJECT (menu), "attach-widget");
1230 * gtk_menu_get_attach_widget:
1233 * Returns the #GtkWidget that the menu is attached to.
1235 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1238 gtk_menu_get_attach_widget (GtkMenu *menu)
1240 GtkMenuAttachData *data;
1242 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1244 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1246 return data->attach_widget;
1254 * Detaches the menu from the widget to which it had been attached.
1255 * This function will call the callback function, @detacher, provided
1256 * when the gtk_menu_attach_to_widget() function was called.
1259 gtk_menu_detach (GtkMenu *menu)
1261 GtkMenuAttachData *data;
1264 g_return_if_fail (GTK_IS_MENU (menu));
1266 /* keep this function in sync with gtk_widget_unparent() */
1267 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1270 g_warning ("gtk_menu_detach(): menu is not attached");
1273 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1275 /* Detach the toplevel window. */
1276 gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), NULL);
1278 g_signal_handlers_disconnect_by_func (data->attach_widget,
1279 (gpointer) attach_widget_screen_changed,
1283 data->detacher (data->attach_widget, menu);
1284 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1285 list = g_list_remove (list, menu);
1287 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1288 (GDestroyNotify) g_list_free);
1290 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1292 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1293 gtk_widget_unrealize (GTK_WIDGET (menu));
1295 g_slice_free (GtkMenuAttachData, data);
1297 /* Fallback title for menu comes from attach widget */
1298 gtk_menu_update_title (menu);
1300 g_object_unref (menu);
1304 gtk_menu_remove (GtkContainer *container,
1307 GtkMenu *menu = GTK_MENU (container);
1308 GtkMenuPrivate *priv = menu->priv;
1310 /* Clear out old_active_menu_item if it matches the item we are removing */
1311 if (priv->old_active_menu_item == widget)
1312 g_clear_object (&priv->old_active_menu_item);
1314 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1316 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1318 menu_queue_resize (menu);
1324 * Creates a new #GtkMenu
1326 * Returns: a new #GtkMenu
1331 return g_object_new (GTK_TYPE_MENU, NULL);
1335 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1339 GtkMenu *menu = GTK_MENU (menu_shell);
1340 GtkMenuPrivate *priv = menu->priv;
1341 AttachInfo *ai = get_attach_info (child);
1343 ai->left_attach = -1;
1344 ai->right_attach = -1;
1345 ai->top_attach = -1;
1346 ai->bottom_attach = -1;
1348 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1349 gtk_widget_set_parent_window (child, priv->bin_window);
1351 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1353 menu_queue_resize (menu);
1357 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1359 GtkMenuPrivate *priv = menu->priv;
1365 cairo_surface_t *surface;
1366 cairo_pattern_t *pattern;
1369 priv->tearoff_active = FALSE;
1370 priv->saved_scroll_offset = priv->scroll_offset;
1372 window = gtk_widget_get_window (priv->tearoff_window);
1373 width = gdk_window_get_width (window);
1374 height = gdk_window_get_height (window);
1376 surface = gdk_window_create_similar_surface (window,
1377 CAIRO_CONTENT_COLOR,
1381 cr = cairo_create (surface);
1382 gdk_cairo_set_source_window (cr, window, 0, 0);
1386 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1388 pattern = cairo_pattern_create_for_surface (surface);
1389 gdk_window_set_background_pattern (window, pattern);
1391 cairo_pattern_destroy (pattern);
1392 cairo_surface_destroy (surface);
1397 popup_grab_on_window (GdkWindow *window,
1398 GdkDevice *keyboard,
1400 guint32 activate_time)
1403 gdk_device_grab (keyboard, window,
1404 GDK_OWNERSHIP_WINDOW, TRUE,
1405 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1406 NULL, activate_time) != GDK_GRAB_SUCCESS)
1410 gdk_device_grab (pointer, window,
1411 GDK_OWNERSHIP_WINDOW, TRUE,
1412 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1413 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1414 GDK_POINTER_MOTION_MASK,
1415 NULL, activate_time) != GDK_GRAB_SUCCESS)
1418 gdk_device_ungrab (keyboard, activate_time);
1427 * gtk_menu_popup_for_device:
1429 * @device: (allow-none): a #GdkDevice
1430 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1431 * menu item, or %NULL
1432 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1433 * the popup, or %NULL
1434 * @func: (allow-none): a user supplied function used to position the menu,
1436 * @data: (allow-none): user supplied data to be passed to @func
1437 * @destroy: (allow-none): destroy notify for @data
1438 * @button: the mouse button which was pressed to initiate the event
1439 * @activate_time: the time at which the activation event occurred
1441 * Displays a menu and makes it available for selection.
1443 * Applications can use this function to display context-sensitive menus,
1444 * and will typically supply %NULL for the @parent_menu_shell,
1445 * @parent_menu_item, @func, @data and @destroy parameters. The default
1446 * menu positioning function will position the menu at the current position
1447 * of @device (or its corresponding pointer).
1449 * The @button parameter should be the mouse button pressed to initiate
1450 * the menu popup. If the menu popup was initiated by something other than
1451 * a mouse button press, such as a mouse button release or a keypress,
1452 * @button should be 0.
1454 * The @activate_time parameter is used to conflict-resolve initiation of
1455 * concurrent requests for mouse/keyboard grab requests. To function
1456 * properly, this needs to be the time stamp of the user event (such as
1457 * a mouse click or key press) that caused the initiation of the popup.
1458 * Only if no such event is available, gtk_get_current_event_time() can
1462 * Rename to: gtk_menu_popup
1465 gtk_menu_popup_for_device (GtkMenu *menu,
1467 GtkWidget *parent_menu_shell,
1468 GtkWidget *parent_menu_item,
1469 GtkMenuPositionFunc func,
1471 GDestroyNotify destroy,
1473 guint32 activate_time)
1475 GtkMenuPrivate *priv = menu->priv;
1477 GtkWidget *xgrab_shell;
1479 GdkEvent *current_event;
1480 GtkMenuShell *menu_shell;
1481 gboolean grab_keyboard;
1482 GtkWidget *parent_toplevel;
1483 GdkDevice *keyboard, *pointer;
1485 g_return_if_fail (GTK_IS_MENU (menu));
1486 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1489 device = gtk_get_current_event_device ();
1493 GdkDisplay *display;
1494 GdkDeviceManager *device_manager;
1497 display = gtk_widget_get_display (GTK_WIDGET (menu));
1498 device_manager = gdk_display_get_device_manager (display);
1499 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1501 device = devices->data;
1503 g_list_free (devices);
1506 widget = GTK_WIDGET (menu);
1507 menu_shell = GTK_MENU_SHELL (menu);
1509 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1512 pointer = gdk_device_get_associated_device (device);
1517 keyboard = gdk_device_get_associated_device (device);
1520 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1522 priv->seen_item_enter = FALSE;
1524 /* Find the last viewable ancestor, and make an X grab on it
1526 parent = GTK_WIDGET (menu);
1530 gboolean viewable = TRUE;
1531 GtkWidget *tmp = parent;
1535 if (!gtk_widget_get_mapped (tmp))
1540 tmp = gtk_widget_get_parent (tmp);
1544 xgrab_shell = parent;
1546 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1549 /* We want to receive events generated when we map the menu;
1550 * unfortunately, since there is probably already an implicit
1551 * grab in place from the button that the user used to pop up
1552 * the menu, we won't receive then -- in particular, the EnterNotify
1553 * when the menu pops up under the pointer.
1555 * If we are grabbing on a parent menu shell, no problem; just grab
1556 * on that menu shell first before popping up the window with
1557 * owner_events = TRUE.
1559 * When grabbing on the menu itself, things get more convoluted --
1560 * we do an explicit grab on a specially created window with
1561 * owner_events = TRUE, which we override further down with a
1562 * grab on the menu. (We can't grab on the menu until it is mapped;
1563 * we probably could just leave the grab on the other window,
1564 * with a little reorganization of the code in gtkmenu*).
1566 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1567 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1572 if (xgrab_shell && xgrab_shell != widget)
1574 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1576 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1577 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1582 GdkWindow *transfer_window;
1584 xgrab_shell = widget;
1585 transfer_window = menu_grab_transfer_window_get (menu);
1586 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1588 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1589 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1593 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1595 /* We failed to make our pointer/keyboard grab.
1596 * Rather than leaving the user with a stuck up window,
1597 * we just abort here. Presumably the user will try again.
1599 menu_shell->priv->parent_menu_shell = NULL;
1600 menu_grab_transfer_window_destroy (menu);
1604 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1605 menu_shell->priv->active = TRUE;
1606 menu_shell->priv->button = button;
1608 /* If we are popping up the menu from something other than, a button
1609 * press then, as a heuristic, we ignore enter events for the menu
1610 * until we get a MOTION_NOTIFY.
1613 current_event = gtk_get_current_event ();
1616 if ((current_event->type != GDK_BUTTON_PRESS) &&
1617 (current_event->type != GDK_ENTER_NOTIFY))
1618 menu_shell->priv->ignore_enter = TRUE;
1620 gdk_event_free (current_event);
1623 menu_shell->priv->ignore_enter = TRUE;
1627 gtk_menu_tearoff_bg_copy (menu);
1629 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1632 parent_toplevel = NULL;
1633 if (parent_menu_shell)
1634 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1635 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1637 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1639 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1642 /* Set transient for to get the right window group and parent */
1643 if (GTK_IS_WINDOW (parent_toplevel))
1644 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1645 GTK_WINDOW (parent_toplevel));
1647 priv->parent_menu_item = parent_menu_item;
1648 priv->position_func = func;
1649 priv->position_func_data = data;
1650 priv->position_func_data_destroy = destroy;
1651 menu_shell->priv->activate_time = activate_time;
1653 /* We need to show the menu here rather in the init function
1654 * because code expects to be able to tell if the menu is onscreen
1655 * by looking at gtk_widget_get_visible (menu)
1657 gtk_widget_show (GTK_WIDGET (menu));
1659 /* Position the menu, possibly changing the size request
1661 gtk_menu_position (menu, TRUE);
1663 /* Compute the size of the toplevel and realize it so we
1664 * can scroll correctly.
1666 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1668 GtkRequisition tmp_request;
1669 GtkAllocation tmp_allocation = { 0, };
1671 /* Instead of trusting the menu position function to queue a
1672 * resize when the menu goes out of bounds, invalidate the cached
1675 gtk_widget_queue_resize (GTK_WIDGET (menu));
1676 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1678 tmp_allocation.width = tmp_request.width;
1679 tmp_allocation.height = tmp_request.height;
1681 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1683 gtk_widget_realize (priv->toplevel);
1686 gtk_menu_scroll_to (menu, priv->scroll_offset);
1688 /* if no item is selected, select the first one */
1689 if (!menu_shell->priv->active_menu_item)
1691 gboolean touchscreen_mode;
1693 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1694 "gtk-touchscreen-mode", &touchscreen_mode,
1697 if (touchscreen_mode)
1698 gtk_menu_shell_select_first (menu_shell, TRUE);
1701 /* Once everything is set up correctly, map the toplevel */
1702 gtk_widget_show (priv->toplevel);
1704 if (xgrab_shell == widget)
1705 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1707 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1709 if (parent_menu_shell)
1711 gboolean keyboard_mode;
1713 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1714 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1716 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1717 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1719 _gtk_menu_shell_update_mnemonics (menu_shell);
1723 * gtk_menu_popup: (skip)
1725 * @parent_menu_shell: (allow-none): the menu shell containing the
1726 * triggering menu item, or %NULL
1727 * @parent_menu_item: (allow-none): the menu item whose activation
1728 * triggered the popup, or %NULL
1729 * @func: (allow-none): a user supplied function used to position
1730 * the menu, or %NULL
1731 * @data: user supplied data to be passed to @func.
1732 * @button: the mouse button which was pressed to initiate the event.
1733 * @activate_time: the time at which the activation event occurred.
1735 * Displays a menu and makes it available for selection.
1737 * Applications can use this function to display context-sensitive
1738 * menus, and will typically supply %NULL for the @parent_menu_shell,
1739 * @parent_menu_item, @func and @data parameters. The default menu
1740 * positioning function will position the menu at the current mouse
1743 * The @button parameter should be the mouse button pressed to initiate
1744 * the menu popup. If the menu popup was initiated by something other
1745 * than a mouse button press, such as a mouse button release or a keypress,
1746 * @button should be 0.
1748 * The @activate_time parameter is used to conflict-resolve initiation
1749 * of concurrent requests for mouse/keyboard grab requests. To function
1750 * properly, this needs to be the timestamp of the user event (such as
1751 * a mouse click or key press) that caused the initiation of the popup.
1752 * Only if no such event is available, gtk_get_current_event_time() can
1756 gtk_menu_popup (GtkMenu *menu,
1757 GtkWidget *parent_menu_shell,
1758 GtkWidget *parent_menu_item,
1759 GtkMenuPositionFunc func,
1762 guint32 activate_time)
1764 g_return_if_fail (GTK_IS_MENU (menu));
1766 gtk_menu_popup_for_device (menu,
1771 button, activate_time);
1778 * Removes the menu from the screen.
1781 gtk_menu_popdown (GtkMenu *menu)
1783 GtkMenuPrivate *priv;
1784 GtkMenuShell *menu_shell;
1787 g_return_if_fail (GTK_IS_MENU (menu));
1789 menu_shell = GTK_MENU_SHELL (menu);
1792 menu_shell->priv->parent_menu_shell = NULL;
1793 menu_shell->priv->active = FALSE;
1794 menu_shell->priv->ignore_enter = FALSE;
1796 priv->have_position = FALSE;
1798 gtk_menu_stop_scrolling (menu);
1799 gtk_menu_stop_navigating_submenu (menu);
1801 if (menu_shell->priv->active_menu_item)
1803 if (priv->old_active_menu_item)
1804 g_object_unref (priv->old_active_menu_item);
1805 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1806 g_object_ref (priv->old_active_menu_item);
1809 gtk_menu_shell_deselect (menu_shell);
1811 /* The X Grab, if present, will automatically be removed
1812 * when we hide the window
1816 gtk_widget_hide (priv->toplevel);
1817 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1820 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1824 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1826 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1828 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1832 /* We popped up the menu from the tearoff, so we need to
1833 * release the grab - we aren't actually hiding the menu.
1835 if (menu_shell->priv->have_xgrab && pointer)
1837 GdkDevice *keyboard;
1839 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1840 keyboard = gdk_device_get_associated_device (pointer);
1843 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1847 /* gtk_menu_popdown is called each time a menu item is selected from
1848 * a torn off menu. Only scroll back to the saved position if the
1849 * non-tearoff menu was popped down.
1851 if (!priv->tearoff_active)
1852 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1853 priv->tearoff_active = TRUE;
1856 gtk_widget_hide (GTK_WIDGET (menu));
1858 menu_shell->priv->have_xgrab = FALSE;
1861 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1863 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1865 menu_grab_transfer_window_destroy (menu);
1869 * gtk_menu_get_active:
1872 * Returns the selected menu item from the menu. This is used by the
1875 * Returns: (transfer none): the #GtkMenuItem that was last selected
1876 * in the menu. If a selection has not yet been made, the
1877 * first menu item is selected.
1880 gtk_menu_get_active (GtkMenu *menu)
1882 GtkMenuPrivate *priv = menu->priv;
1886 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1888 if (!priv->old_active_menu_item)
1891 children = GTK_MENU_SHELL (menu)->priv->children;
1895 child = children->data;
1896 children = children->next;
1898 if (gtk_bin_get_child (GTK_BIN (child)))
1903 priv->old_active_menu_item = child;
1904 if (priv->old_active_menu_item)
1905 g_object_ref (priv->old_active_menu_item);
1908 return priv->old_active_menu_item;
1912 * gtk_menu_set_active:
1914 * @index: the index of the menu item to select. Iindex values are
1917 * Selects the specified menu item within the menu. This is used by
1918 * the #GtkOptionMenu and should not be used by anyone else.
1921 gtk_menu_set_active (GtkMenu *menu,
1924 GtkMenuPrivate *priv = menu->priv;
1928 g_return_if_fail (GTK_IS_MENU (menu));
1930 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1933 child = tmp_list->data;
1934 if (gtk_bin_get_child (GTK_BIN (child)))
1936 if (priv->old_active_menu_item)
1937 g_object_unref (priv->old_active_menu_item);
1938 priv->old_active_menu_item = child;
1939 g_object_ref (priv->old_active_menu_item);
1945 * gtk_menu_set_accel_group:
1947 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1950 * Set the #GtkAccelGroup which holds global accelerators for the
1951 * menu. This accelerator group needs to also be added to all windows
1952 * that this menu is being used in with gtk_window_add_accel_group(),
1953 * in order for those windows to support all the accelerators
1954 * contained in this group.
1957 gtk_menu_set_accel_group (GtkMenu *menu,
1958 GtkAccelGroup *accel_group)
1960 GtkMenuPrivate *priv = menu->priv;
1961 g_return_if_fail (GTK_IS_MENU (menu));
1963 if (priv->accel_group != accel_group)
1965 if (priv->accel_group)
1966 g_object_unref (priv->accel_group);
1967 priv->accel_group = accel_group;
1968 if (priv->accel_group)
1969 g_object_ref (priv->accel_group);
1970 _gtk_menu_refresh_accel_paths (menu, TRUE);
1975 * gtk_menu_get_accel_group:
1978 * Gets the #GtkAccelGroup which holds global accelerators for the
1979 * menu. See gtk_menu_set_accel_group().
1981 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
1984 gtk_menu_get_accel_group (GtkMenu *menu)
1986 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1988 return menu->priv->accel_group;
1992 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1995 /* Menu items chain here to figure whether they can activate their
1996 * accelerators. Unlike ordinary widgets, menus allow accel
1997 * activation even if invisible since that's the usual case for
1998 * submenus/popup-menus. however, the state of the attach widget
1999 * affects the "activeness" of the menu.
2001 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
2004 return gtk_widget_can_activate_accel (awidget, signal_id);
2006 return gtk_widget_is_sensitive (widget);
2010 * gtk_menu_set_accel_path
2011 * @menu: a valid #GtkMenu
2012 * @accel_path: (allow-none): a valid accelerator path
2014 * Sets an accelerator path for this menu from which accelerator paths
2015 * for its immediate children, its menu items, can be constructed.
2016 * The main purpose of this function is to spare the programmer the
2017 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2018 * each menu item that should support runtime user changable accelerators.
2019 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2020 * each menu item of this menu, that contains a label describing its
2021 * purpose, automatically gets an accel path assigned.
2023 * For example, a menu containing menu items "New" and "Exit", will, after
2024 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2025 * has been called, assign its items the accel paths:
2026 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2028 * Assigning accel paths to menu items then enables the user to change
2029 * their accelerators at runtime. More details about accelerator paths
2030 * and their default setups can be found at gtk_accel_map_add_entry().
2032 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2033 * if you pass a static string, you can save some memory by interning
2034 * it first with g_intern_static_string().
2037 gtk_menu_set_accel_path (GtkMenu *menu,
2038 const gchar *accel_path)
2040 GtkMenuPrivate *priv = menu->priv;
2041 g_return_if_fail (GTK_IS_MENU (menu));
2044 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2046 /* FIXME: accel_path should be defined as const gchar* */
2047 priv->accel_path = (gchar*)g_intern_string (accel_path);
2048 if (priv->accel_path)
2049 _gtk_menu_refresh_accel_paths (menu, FALSE);
2053 * gtk_menu_get_accel_path
2054 * @menu: a valid #GtkMenu
2056 * Retrieves the accelerator path set on the menu.
2058 * Returns: the accelerator path set on the menu.
2063 gtk_menu_get_accel_path (GtkMenu *menu)
2065 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2067 return menu->priv->accel_path;
2072 gboolean group_changed;
2076 refresh_accel_paths_foreach (GtkWidget *widget,
2079 GtkMenuPrivate *priv;
2080 AccelPropagation *prop = data;
2082 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2084 priv = prop->menu->priv;
2085 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2088 prop->group_changed);
2093 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2094 gboolean group_changed)
2096 GtkMenuPrivate *priv = menu->priv;
2097 g_return_if_fail (GTK_IS_MENU (menu));
2099 if (priv->accel_path && priv->accel_group)
2101 AccelPropagation prop;
2104 prop.group_changed = group_changed;
2105 gtk_container_foreach (GTK_CONTAINER (menu),
2106 refresh_accel_paths_foreach,
2112 * gtk_menu_reposition:
2115 * Repositions the menu according to its position function.
2118 gtk_menu_reposition (GtkMenu *menu)
2120 g_return_if_fail (GTK_IS_MENU (menu));
2122 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2123 gtk_menu_position (menu, FALSE);
2127 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2132 value = gtk_adjustment_get_value (adjustment);
2133 if (menu->priv->scroll_offset != value)
2134 gtk_menu_scroll_to (menu, value);
2138 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2141 GtkMenuPrivate *priv = menu->priv;
2142 GdkGeometry geometry_hints;
2144 if (!priv->tearoff_window)
2147 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2149 GtkRequisition requisition;
2151 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2152 &requisition, NULL);
2153 width += requisition.width;
2156 geometry_hints.min_width = width;
2157 geometry_hints.max_width = width;
2159 geometry_hints.min_height = 0;
2160 geometry_hints.max_height = priv->requested_height;
2162 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2165 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2169 gtk_menu_update_title (GtkMenu *menu)
2171 GtkMenuPrivate *priv = menu->priv;
2173 if (priv->tearoff_window)
2176 GtkWidget *attach_widget;
2178 title = gtk_menu_get_title (menu);
2181 attach_widget = gtk_menu_get_attach_widget (menu);
2182 if (GTK_IS_MENU_ITEM (attach_widget))
2184 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2185 if (GTK_IS_LABEL (child))
2186 title = gtk_label_get_text (GTK_LABEL (child));
2191 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2196 gtk_menu_get_toplevel (GtkWidget *menu)
2198 GtkWidget *attach, *toplevel;
2200 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2202 if (GTK_IS_MENU_ITEM (attach))
2203 attach = gtk_widget_get_parent (attach);
2205 if (GTK_IS_MENU (attach))
2206 return gtk_menu_get_toplevel (attach);
2207 else if (GTK_IS_WIDGET (attach))
2209 toplevel = gtk_widget_get_toplevel (attach);
2210 if (gtk_widget_is_toplevel (toplevel))
2218 tearoff_window_destroyed (GtkWidget *widget,
2221 gtk_menu_set_tearoff_state (menu, FALSE);
2225 * gtk_menu_set_tearoff_state:
2227 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2229 * Changes the tearoff state of the menu. A menu is normally
2230 * displayed as drop down menu which persists as long as the menu is
2231 * active. It can also be displayed as a tearoff menu which persists
2232 * until it is closed or reattached.
2235 gtk_menu_set_tearoff_state (GtkMenu *menu,
2238 GtkMenuPrivate *priv = menu->priv;
2241 g_return_if_fail (GTK_IS_MENU (menu));
2243 if (priv->torn_off != torn_off)
2245 priv->torn_off = torn_off;
2246 priv->tearoff_active = torn_off;
2250 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2251 gtk_menu_popdown (menu);
2253 if (!priv->tearoff_window)
2255 GtkWidget *toplevel;
2257 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2258 "type", GTK_WINDOW_TOPLEVEL,
2259 "screen", gtk_widget_get_screen (priv->toplevel),
2260 "app-paintable", TRUE,
2263 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2264 GDK_WINDOW_TYPE_HINT_MENU);
2265 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2266 g_signal_connect (priv->tearoff_window, "destroy",
2267 G_CALLBACK (tearoff_window_destroyed), menu);
2268 g_signal_connect (priv->tearoff_window, "event",
2269 G_CALLBACK (gtk_menu_window_event), menu);
2271 gtk_menu_update_title (menu);
2273 gtk_widget_realize (priv->tearoff_window);
2275 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2276 if (toplevel != NULL)
2277 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2278 GTK_WINDOW (toplevel));
2280 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2281 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2282 priv->tearoff_hbox);
2284 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2285 priv->tearoff_adjustment = gtk_adjustment_new (0,
2286 0, priv->requested_height,
2290 g_object_connect (priv->tearoff_adjustment,
2291 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2293 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2295 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2296 priv->tearoff_scrollbar,
2299 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2300 gtk_widget_show (priv->tearoff_scrollbar);
2302 gtk_widget_show (priv->tearoff_hbox);
2305 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2307 /* Update menu->requisition */
2308 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2310 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2312 gtk_widget_realize (priv->tearoff_window);
2313 gtk_menu_position (menu, TRUE);
2315 gtk_widget_show (GTK_WIDGET (menu));
2316 gtk_widget_show (priv->tearoff_window);
2318 gtk_menu_scroll_to (menu, 0);
2323 gtk_widget_hide (GTK_WIDGET (menu));
2324 gtk_widget_hide (priv->tearoff_window);
2325 if (GTK_IS_CONTAINER (priv->toplevel))
2326 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2327 gtk_widget_destroy (priv->tearoff_window);
2329 priv->tearoff_window = NULL;
2330 priv->tearoff_hbox = NULL;
2331 priv->tearoff_scrollbar = NULL;
2332 priv->tearoff_adjustment = NULL;
2335 g_object_notify (G_OBJECT (menu), "tearoff-state");
2340 * gtk_menu_get_tearoff_state:
2343 * Returns whether the menu is torn off.
2344 * See gtk_menu_set_tearoff_state().
2346 * Return value: %TRUE if the menu is currently torn off.
2349 gtk_menu_get_tearoff_state (GtkMenu *menu)
2351 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2353 return menu->priv->torn_off;
2357 * gtk_menu_set_title:
2359 * @title: a string containing the title for the menu
2361 * Sets the title string for the menu.
2363 * The title is displayed when the menu is shown as a tearoff
2364 * menu. If @title is %NULL, the menu will see if it is attached
2365 * to a parent menu item, and if so it will try to use the same
2366 * text as that menu item's label.
2369 gtk_menu_set_title (GtkMenu *menu,
2372 GtkMenuPrivate *priv = menu->priv;
2375 g_return_if_fail (GTK_IS_MENU (menu));
2377 old_title = priv->title;
2378 priv->title = g_strdup (title);
2381 gtk_menu_update_title (menu);
2382 g_object_notify (G_OBJECT (menu), "tearoff-title");
2386 * gtk_menu_get_title:
2389 * Returns the title of the menu. See gtk_menu_set_title().
2391 * Return value: the title of the menu, or %NULL if the menu
2392 * has no title set on it. This string is owned by GTK+
2393 * and should not be modified or freed.
2396 gtk_menu_get_title (GtkMenu *menu)
2398 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2400 return menu->priv->title;
2404 * gtk_menu_reorder_child:
2406 * @child: the #GtkMenuItem to move
2407 * @position: the new position to place @child.
2408 * Positions are numbered from 0 to n - 1
2410 * Moves @child to a new @position in the list of @menu
2414 gtk_menu_reorder_child (GtkMenu *menu,
2418 GtkMenuShell *menu_shell;
2420 g_return_if_fail (GTK_IS_MENU (menu));
2421 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2423 menu_shell = GTK_MENU_SHELL (menu);
2425 if (g_list_find (menu_shell->priv->children, child))
2427 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2428 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2430 menu_queue_resize (menu);
2435 gtk_menu_style_updated (GtkWidget *widget)
2437 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2439 if (gtk_widget_get_realized (widget))
2441 GtkMenu *menu = GTK_MENU (widget);
2442 GtkMenuPrivate *priv = menu->priv;
2443 GtkStyleContext *context;
2445 context = gtk_widget_get_style_context (widget);
2447 gtk_style_context_set_background (context, priv->bin_window);
2448 gtk_style_context_set_background (context, priv->view_window);
2449 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2454 get_arrows_border (GtkMenu *menu,
2457 GtkMenuPrivate *priv = menu->priv;
2458 guint scroll_arrow_height;
2459 GtkArrowPlacement arrow_placement;
2461 gtk_widget_style_get (GTK_WIDGET (menu),
2462 "scroll-arrow-vlength", &scroll_arrow_height,
2463 "arrow_placement", &arrow_placement,
2466 switch (arrow_placement)
2468 case GTK_ARROWS_BOTH:
2469 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2470 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2473 case GTK_ARROWS_START:
2474 border->top = (priv->upper_arrow_visible ||
2475 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2479 case GTK_ARROWS_END:
2481 border->bottom = (priv->upper_arrow_visible ||
2482 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2486 border->left = border->right = 0;
2490 get_menu_padding (GtkWidget *widget,
2493 GtkStyleContext *context;
2494 GtkStateFlags state;
2496 context = gtk_widget_get_style_context (widget);
2497 state = gtk_widget_get_state_flags (widget);
2499 gtk_style_context_get_padding (context, state, padding);
2503 gtk_menu_realize (GtkWidget *widget)
2505 GtkMenu *menu = GTK_MENU (widget);
2506 GtkMenuPrivate *priv = menu->priv;
2507 GtkAllocation allocation;
2508 GtkStyleContext *context;
2510 GdkWindowAttr attributes;
2511 gint attributes_mask;
2515 guint vertical_padding;
2516 guint horizontal_padding;
2517 GtkBorder arrow_border, padding;
2519 g_return_if_fail (GTK_IS_MENU (widget));
2521 gtk_widget_set_realized (widget, TRUE);
2523 gtk_widget_get_allocation (widget, &allocation);
2525 attributes.window_type = GDK_WINDOW_CHILD;
2526 attributes.x = allocation.x;
2527 attributes.y = allocation.y;
2528 attributes.width = allocation.width;
2529 attributes.height = allocation.height;
2530 attributes.wclass = GDK_INPUT_OUTPUT;
2531 attributes.visual = gtk_widget_get_visual (widget);
2532 attributes.event_mask = gtk_widget_get_events (widget);
2533 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2534 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2536 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2538 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2539 &attributes, attributes_mask);
2540 gtk_widget_set_window (widget, window);
2541 gdk_window_set_user_data (window, widget);
2543 get_menu_padding (widget, &padding);
2544 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2545 context = gtk_widget_get_style_context (widget);
2547 gtk_widget_style_get (GTK_WIDGET (menu),
2548 "vertical-padding", &vertical_padding,
2549 "horizontal-padding", &horizontal_padding,
2552 gtk_widget_get_allocation (widget, &allocation);
2554 attributes.x = border_width + padding.left + horizontal_padding;
2555 attributes.y = border_width + padding.top + vertical_padding;
2556 attributes.width = allocation.width -
2557 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2558 attributes.height = allocation.height -
2559 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2561 get_arrows_border (menu, &arrow_border);
2562 attributes.y += arrow_border.top;
2563 attributes.height -= arrow_border.top;
2564 attributes.height -= arrow_border.bottom;
2566 attributes.width = MAX (1, attributes.width);
2567 attributes.height = MAX (1, attributes.height);
2569 priv->view_window = gdk_window_new (window,
2570 &attributes, attributes_mask);
2571 gdk_window_set_user_data (priv->view_window, menu);
2573 gtk_widget_get_allocation (widget, &allocation);
2577 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2578 padding.left + padding.right;
2579 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2580 padding.top + padding.bottom;
2582 attributes.width = MAX (1, attributes.width);
2583 attributes.height = MAX (1, attributes.height);
2585 priv->bin_window = gdk_window_new (priv->view_window,
2586 &attributes, attributes_mask);
2587 gdk_window_set_user_data (priv->bin_window, menu);
2589 children = GTK_MENU_SHELL (menu)->priv->children;
2592 child = children->data;
2593 children = children->next;
2595 gtk_widget_set_parent_window (child, priv->bin_window);
2598 gtk_style_context_set_background (context, priv->bin_window);
2599 gtk_style_context_set_background (context, priv->view_window);
2600 gtk_style_context_set_background (context, window);
2602 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2603 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2604 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2606 gdk_window_show (priv->bin_window);
2607 gdk_window_show (priv->view_window);
2611 gtk_menu_focus (GtkWidget *widget,
2612 GtkDirectionType direction)
2614 /* A menu or its menu items cannot have focus */
2618 /* See notes in gtk_menu_popup() for information
2619 * about the "grab transfer window"
2622 menu_grab_transfer_window_get (GtkMenu *menu)
2624 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2627 GdkWindowAttr attributes;
2628 gint attributes_mask;
2630 attributes.x = -100;
2631 attributes.y = -100;
2632 attributes.width = 10;
2633 attributes.height = 10;
2634 attributes.window_type = GDK_WINDOW_TEMP;
2635 attributes.wclass = GDK_INPUT_ONLY;
2636 attributes.override_redirect = TRUE;
2637 attributes.event_mask = 0;
2639 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2641 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2642 &attributes, attributes_mask);
2643 gdk_window_set_user_data (window, menu);
2645 gdk_window_show (window);
2647 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2654 menu_grab_transfer_window_destroy (GtkMenu *menu)
2656 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2659 gdk_window_set_user_data (window, NULL);
2660 gdk_window_destroy (window);
2661 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2666 gtk_menu_unrealize (GtkWidget *widget)
2668 GtkMenu *menu = GTK_MENU (widget);
2669 GtkMenuPrivate *priv = menu->priv;
2671 menu_grab_transfer_window_destroy (menu);
2673 gdk_window_set_user_data (priv->view_window, NULL);
2674 gdk_window_destroy (priv->view_window);
2675 priv->view_window = NULL;
2677 gdk_window_set_user_data (priv->bin_window, NULL);
2678 gdk_window_destroy (priv->bin_window);
2679 priv->bin_window = NULL;
2681 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2685 calculate_line_heights (GtkMenu *menu,
2687 guint **ret_min_heights,
2688 guint **ret_nat_heights)
2691 GtkMenuPrivate *priv;
2692 GtkMenuShell *menu_shell;
2693 GtkWidget *child, *widget;
2695 guint horizontal_padding;
2704 widget = GTK_WIDGET (menu);
2705 menu_shell = GTK_MENU_SHELL (widget);
2707 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2708 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2709 n_heights = gtk_menu_get_n_rows (menu);
2710 n_columns = gtk_menu_get_n_columns (menu);
2711 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2713 gtk_widget_style_get (GTK_WIDGET (menu),
2714 "horizontal-padding", &horizontal_padding,
2716 get_menu_padding (widget, &padding);
2718 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2719 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2721 for (children = menu_shell->priv->children; children; children = children->next)
2726 gint child_min, child_nat;
2728 child = children->data;
2730 if (!gtk_widget_get_visible (child))
2733 get_effective_child_attach (child, &l, &r, &t, &b);
2735 part = avail_width / (r - l);
2737 gtk_widget_get_preferred_height_for_width (child, part,
2738 &child_min, &child_nat);
2740 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2742 part = MAX (child_min, toggle_size) / (b - t);
2743 min_heights[t] = MAX (min_heights[t], part);
2745 part = MAX (child_nat, toggle_size) / (b - t);
2746 nat_heights[t] = MAX (nat_heights[t], part);
2749 if (ret_min_heights)
2750 *ret_min_heights = min_heights;
2752 g_free (min_heights);
2754 if (ret_nat_heights)
2755 *ret_nat_heights = nat_heights;
2757 g_free (nat_heights);
2763 gtk_menu_size_allocate (GtkWidget *widget,
2764 GtkAllocation *allocation)
2767 GtkMenuPrivate *priv;
2768 GtkMenuShell *menu_shell;
2770 GtkAllocation child_allocation;
2775 guint vertical_padding;
2776 guint horizontal_padding;
2779 g_return_if_fail (GTK_IS_MENU (widget));
2780 g_return_if_fail (allocation != NULL);
2782 menu = GTK_MENU (widget);
2783 menu_shell = GTK_MENU_SHELL (widget);
2786 gtk_widget_set_allocation (widget, allocation);
2788 gtk_widget_style_get (GTK_WIDGET (menu),
2789 "vertical-padding", &vertical_padding,
2790 "horizontal-padding", &horizontal_padding,
2793 get_menu_padding (widget, &padding);
2794 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2796 g_free (priv->heights);
2797 priv->heights_length = calculate_line_heights (menu,
2802 /* refresh our cached height request */
2803 priv->requested_height = (2 * (border_width + vertical_padding)) +
2804 padding.top + padding.bottom;
2805 for (i = 0; i < priv->heights_length; i++)
2806 priv->requested_height += priv->heights[i];
2808 x = border_width + padding.left + horizontal_padding;
2809 y = border_width + padding.top + vertical_padding;
2810 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2811 padding.left - padding.right;
2812 height = allocation->height - (2 * (border_width + vertical_padding)) -
2813 padding.top - padding.bottom;
2815 if (menu_shell->priv->active)
2816 gtk_menu_scroll_to (menu, priv->scroll_offset);
2818 if (!priv->tearoff_active)
2820 GtkBorder arrow_border;
2822 get_arrows_border (menu, &arrow_border);
2823 y += arrow_border.top;
2824 height -= arrow_border.top;
2825 height -= arrow_border.bottom;
2828 width = MAX (1, width);
2829 height = MAX (1, height);
2831 if (gtk_widget_get_realized (widget))
2833 gdk_window_move_resize (gtk_widget_get_window (widget),
2834 allocation->x, allocation->y,
2835 allocation->width, allocation->height);
2837 gdk_window_move_resize (priv->view_window, x, y, width, height);
2840 if (menu_shell->priv->children)
2842 gint base_width = width / gtk_menu_get_n_columns (menu);
2844 children = menu_shell->priv->children;
2847 child = children->data;
2848 children = children->next;
2850 if (gtk_widget_get_visible (child))
2855 get_effective_child_attach (child, &l, &r, &t, &b);
2857 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2860 tmp = gtk_menu_get_n_columns (menu) - l;
2861 l = gtk_menu_get_n_columns (menu) - r;
2865 child_allocation.width = (r - l) * base_width;
2866 child_allocation.height = 0;
2867 child_allocation.x = l * base_width;
2868 child_allocation.y = 0;
2870 for (i = 0; i < b; i++)
2873 child_allocation.y += priv->heights[i];
2875 child_allocation.height += priv->heights[i];
2878 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2881 gtk_widget_size_allocate (child, &child_allocation);
2882 gtk_widget_queue_draw (child);
2886 /* Resize the item window */
2887 if (gtk_widget_get_realized (widget))
2893 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2894 height += priv->heights[i];
2896 width = gtk_menu_get_n_columns (menu) * base_width;
2897 gdk_window_resize (priv->bin_window, width, height);
2900 if (priv->tearoff_active)
2902 if (height >= priv->requested_height)
2904 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2906 gtk_widget_hide (priv->tearoff_scrollbar);
2907 gtk_menu_set_tearoff_hints (menu, allocation->width);
2909 gtk_menu_scroll_to (menu, 0);
2914 gtk_adjustment_configure (priv->tearoff_adjustment,
2915 gtk_adjustment_get_value (priv->tearoff_adjustment),
2917 priv->requested_height,
2918 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2919 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2920 allocation->height);
2922 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2924 gtk_widget_show (priv->tearoff_scrollbar);
2925 gtk_menu_set_tearoff_hints (menu, allocation->width);
2933 get_arrows_visible_area (GtkMenu *menu,
2934 GdkRectangle *border,
2935 GdkRectangle *upper,
2936 GdkRectangle *lower,
2939 GtkArrowPlacement arrow_placement;
2940 GtkWidget *widget = GTK_WIDGET (menu);
2942 guint vertical_padding;
2943 guint horizontal_padding;
2944 gint scroll_arrow_height;
2945 GtkBorder menu_padding;
2947 gtk_widget_style_get (widget,
2948 "vertical-padding", &vertical_padding,
2949 "horizontal-padding", &horizontal_padding,
2950 "scroll-arrow-vlength", &scroll_arrow_height,
2951 "arrow-placement", &arrow_placement,
2954 get_menu_padding (widget, &menu_padding);
2955 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2956 border->x = border_width + menu_padding.left + horizontal_padding;
2957 border->y = border_width + menu_padding.top + vertical_padding;
2958 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2959 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2961 switch (arrow_placement)
2963 case GTK_ARROWS_BOTH:
2964 upper->x = border->x;
2965 upper->y = border->y;
2966 upper->width = border->width - 2 * border->x;
2967 upper->height = scroll_arrow_height;
2969 lower->x = border->x;
2970 lower->y = border->height - border->y - scroll_arrow_height;
2971 lower->width = border->width - 2 * border->x;
2972 lower->height = scroll_arrow_height;
2975 case GTK_ARROWS_START:
2976 upper->x = border->x;
2977 upper->y = border->y;
2978 upper->width = (border->width - 2 * border->x) / 2;
2979 upper->height = scroll_arrow_height;
2981 lower->x = border->x + upper->width;
2982 lower->y = border->y;
2983 lower->width = (border->width - 2 * border->x) / 2;
2984 lower->height = scroll_arrow_height;
2987 case GTK_ARROWS_END:
2988 upper->x = border->x;
2989 upper->y = border->height - border->y - scroll_arrow_height;
2990 upper->width = (border->width - 2 * border->x) / 2;
2991 upper->height = scroll_arrow_height;
2993 lower->x = border->x + upper->width;
2994 lower->y = border->height - border->y - scroll_arrow_height;
2995 lower->width = (border->width - 2 * border->x) / 2;
2996 lower->height = scroll_arrow_height;
3000 g_assert_not_reached();
3001 upper->x = upper->y = upper->width = upper->height = 0;
3002 lower->x = lower->y = lower->width = lower->height = 0;
3005 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
3009 gtk_menu_draw (GtkWidget *widget,
3013 GtkMenuPrivate *priv;
3014 GtkStyleContext *context;
3015 GdkRectangle border;
3019 GtkBorder menu_padding;
3021 menu = GTK_MENU (widget);
3023 context = gtk_widget_get_style_context (widget);
3025 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3026 get_menu_padding (widget, &menu_padding);
3028 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3030 gfloat arrow_scaling;
3033 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3034 arrow_size = arrow_scaling * arrow_space;
3036 gtk_render_background (context, cr, 0, 0,
3037 gtk_widget_get_allocated_width (widget),
3038 gtk_widget_get_allocated_height (widget));
3039 gtk_render_frame (context, cr, 0, 0,
3040 gtk_widget_get_allocated_width (widget),
3041 gtk_widget_get_allocated_height (widget));
3043 gtk_style_context_save (context);
3044 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3046 if (priv->upper_arrow_visible && !priv->tearoff_active)
3048 gtk_style_context_save (context);
3049 gtk_style_context_set_state (context, priv->upper_arrow_state);
3051 gtk_render_background (context, cr,
3053 upper.width, upper.height);
3054 gtk_render_frame (context, cr,
3056 upper.width, upper.height);
3058 gtk_render_arrow (context, cr, 0,
3059 upper.x + (upper.width - arrow_size) / 2,
3060 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3063 gtk_style_context_restore (context);
3066 if (priv->lower_arrow_visible && !priv->tearoff_active)
3068 gtk_style_context_save (context);
3069 gtk_style_context_set_state (context, priv->lower_arrow_state);
3071 gtk_render_background (context, cr,
3073 lower.width, lower.height);
3074 gtk_render_frame (context, cr,
3076 lower.width, lower.height);
3078 gtk_render_arrow (context, cr, G_PI,
3079 lower.x + (lower.width - arrow_size) / 2,
3080 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3083 gtk_style_context_restore (context);
3086 gtk_style_context_restore (context);
3089 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3091 gint y = -border.y + priv->scroll_offset;
3094 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3096 if (!priv->tearoff_active)
3098 GtkBorder arrow_border;
3100 get_arrows_border (menu, &arrow_border);
3101 y -= arrow_border.top;
3104 gtk_render_background (context, cr,
3106 border.width, border.height);
3107 gtk_render_frame (context, cr,
3109 border.width, border.height);
3114 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3120 gtk_menu_show (GtkWidget *widget)
3122 GtkMenu *menu = GTK_MENU (widget);
3124 _gtk_menu_refresh_accel_paths (menu, FALSE);
3126 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3131 gtk_menu_get_preferred_width (GtkWidget *widget,
3136 GtkMenuShell *menu_shell;
3137 GtkMenuPrivate *priv;
3140 guint max_toggle_size;
3141 guint max_accel_width;
3142 guint horizontal_padding;
3144 gint child_min, child_nat;
3145 gint min_width, nat_width;
3148 menu = GTK_MENU (widget);
3149 menu_shell = GTK_MENU_SHELL (widget);
3152 min_width = nat_width = 0;
3154 max_toggle_size = 0;
3155 max_accel_width = 0;
3157 children = menu_shell->priv->children;
3164 child = children->data;
3165 children = children->next;
3167 if (! gtk_widget_get_visible (child))
3170 get_effective_child_attach (child, &l, &r, &t, &b);
3172 /* It's important to size_request the child
3173 * before doing the toggle size request, in
3174 * case the toggle size request depends on the size
3175 * request of a child of the child (e.g. for ImageMenuItem)
3177 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3179 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3180 max_toggle_size = MAX (max_toggle_size, toggle_size);
3181 max_accel_width = MAX (max_accel_width,
3182 GTK_MENU_ITEM (child)->priv->accelerator_width);
3184 part = child_min / (r - l);
3185 min_width = MAX (min_width, part);
3187 part = child_nat / (r - l);
3188 nat_width = MAX (nat_width, part);
3191 /* If the menu doesn't include any images or check items
3192 * reserve the space so that all menus are consistent.
3193 * We only do this for 'ordinary' menus, not for combobox
3194 * menus or multi-column menus
3196 if (max_toggle_size == 0 &&
3197 gtk_menu_get_n_columns (menu) == 1 &&
3198 !priv->no_toggle_size)
3200 GtkStyleContext *context;
3201 GtkWidgetPath *menu_path, *check_path;
3202 guint toggle_spacing;
3203 guint indicator_size;
3205 context = gtk_widget_get_style_context (widget);
3206 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3208 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3209 check_path = gtk_widget_path_copy (menu_path);
3210 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3212 gtk_style_context_set_path (context, check_path);
3213 gtk_widget_path_free (check_path);
3215 gtk_style_context_get_style (context,
3216 "toggle-spacing", &toggle_spacing,
3217 "indicator-size", &indicator_size,
3220 max_toggle_size = indicator_size + toggle_spacing;
3222 /* Restore real widget path */
3223 gtk_style_context_set_path (context, menu_path);
3224 gtk_widget_path_free (menu_path);
3227 min_width += 2 * max_toggle_size + max_accel_width;
3228 min_width *= gtk_menu_get_n_columns (menu);
3230 nat_width += 2 * max_toggle_size + max_accel_width;
3231 nat_width *= gtk_menu_get_n_columns (menu);
3233 gtk_widget_style_get (GTK_WIDGET (menu),
3234 "horizontal-padding", &horizontal_padding,
3237 get_menu_padding (widget, &padding);
3238 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3239 min_width += (2 * (border_width + horizontal_padding)) +
3240 padding.left + padding.right;
3241 nat_width += (2 * (border_width + horizontal_padding)) +
3242 padding.left + padding.right;
3244 priv->toggle_size = max_toggle_size;
3245 priv->accel_size = max_accel_width;
3248 *minimum_size = min_width;
3251 *natural_size = nat_width;
3253 /* Don't resize the tearoff if it is not active,
3254 * because it won't redraw (it is only a background pixmap).
3256 if (priv->tearoff_active)
3257 gtk_menu_set_tearoff_hints (menu, min_width);
3261 gtk_menu_get_preferred_height (GtkWidget *widget,
3267 /* Menus are height-for-width only, just return the height
3268 * for the minimum width
3270 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3271 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3275 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3281 GtkMenu *menu = GTK_MENU (widget);
3282 GtkMenuPrivate *priv = menu->priv;
3283 guint *min_heights, *nat_heights;
3284 guint vertical_padding, border_width;
3286 gint min_height, nat_height;
3288 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3289 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3290 get_menu_padding (widget, &padding);
3292 min_height = nat_height = (border_width + vertical_padding) * 2 +
3293 padding.top + padding.bottom;
3296 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3298 for (i = 0; i < n_heights; i++)
3300 min_height += min_heights[i];
3301 nat_height += nat_heights[i];
3304 if (priv->have_position)
3306 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3307 GdkRectangle monitor;
3309 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
3311 if (priv->position_y + min_height > monitor.y + monitor.height)
3312 min_height = monitor.y + monitor.height - priv->position_y;
3314 if (priv->position_y + nat_height > monitor.y + monitor.height)
3315 nat_height = monitor.y + monitor.height - priv->position_y;
3317 if (priv->position_y < monitor.y)
3319 min_height -= monitor.y - priv->position_y;
3320 nat_height -= monitor.y - priv->position_y;
3325 *minimum_size = min_height;
3328 *natural_size = nat_height;
3330 g_free (min_heights);
3331 g_free (nat_heights);
3335 pointer_in_menu_window (GtkWidget *widget,
3339 GtkMenu *menu = GTK_MENU (widget);
3340 GtkMenuPrivate *priv = menu->priv;
3341 GtkAllocation allocation;
3343 if (gtk_widget_get_mapped (priv->toplevel))
3345 GtkMenuShell *menu_shell;
3346 gint window_x, window_y;
3348 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3349 &window_x, &window_y);
3351 gtk_widget_get_allocation (widget, &allocation);
3352 if (x_root >= window_x && x_root < window_x + allocation.width &&
3353 y_root >= window_y && y_root < window_y + allocation.height)
3356 menu_shell = GTK_MENU_SHELL (widget);
3358 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3359 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3367 gtk_menu_button_press (GtkWidget *widget,
3368 GdkEventButton *event)
3370 if (event->type != GDK_BUTTON_PRESS)
3373 /* Don't pass down to menu shell if a non-menuitem part of the menu
3374 * was clicked. The check for the event_widget being a GtkMenuShell
3375 * works because we have the pointer grabbed on menu_shell->window
3376 * with owner_events=TRUE, so all events that are either outside
3377 * the menu or on its border are delivered relative to
3378 * menu_shell->window.
3380 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3381 pointer_in_menu_window (widget, event->x_root, event->y_root))
3384 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3388 gtk_menu_button_release (GtkWidget *widget,
3389 GdkEventButton *event)
3391 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3393 if (priv->ignore_button_release)
3395 priv->ignore_button_release = FALSE;
3399 if (event->type != GDK_BUTTON_RELEASE)
3402 /* Don't pass down to menu shell if a non-menuitem part of the menu
3403 * was clicked (see comment in button_press()).
3405 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3406 pointer_in_menu_window (widget, event->x_root, event->y_root))
3408 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3409 * bail out early here so it is in a consistent state for the
3410 * next button_press/button_release in GtkMenuShell.
3413 if (GTK_MENU_SHELL (widget)->priv->active)
3414 GTK_MENU_SHELL (widget)->priv->button = 0;
3419 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3422 static const gchar *
3423 get_accel_path (GtkWidget *menu_item,
3428 GClosure *accel_closure;
3429 GtkAccelGroup *accel_group;
3431 path = _gtk_widget_get_accel_path (menu_item, locked);
3434 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3440 label = gtk_bin_get_child (GTK_BIN (menu_item));
3442 if (GTK_IS_ACCEL_LABEL (label))
3444 g_object_get (label,
3445 "accel-closure", &accel_closure,
3449 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3451 *locked = gtk_accel_group_get_is_locked (accel_group);
3461 gtk_menu_key_press (GtkWidget *widget,
3464 GtkMenuShell *menu_shell;
3466 gboolean delete = FALSE;
3467 gboolean can_change_accels;
3468 gchar *accel = NULL;
3469 guint accel_key, accel_mods;
3470 GdkModifierType consumed_modifiers;
3471 GdkDisplay *display;
3473 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3474 g_return_val_if_fail (event != NULL, FALSE);
3476 menu_shell = GTK_MENU_SHELL (widget);
3477 menu = GTK_MENU (widget);
3479 gtk_menu_stop_navigating_submenu (menu);
3481 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3484 display = gtk_widget_get_display (widget);
3486 g_object_get (gtk_widget_get_settings (widget),
3487 "gtk-menu-bar-accel", &accel,
3488 "gtk-can-change-accels", &can_change_accels,
3491 if (accel && *accel)
3494 GdkModifierType mods = 0;
3496 gtk_accelerator_parse (accel, &keyval, &mods);
3499 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3501 /* FIXME this is wrong, needs to be in the global accel resolution
3502 * thing, to properly consider i18n etc., but that probably requires
3503 * AccelGroup changes etc.
3505 if (event->keyval == keyval && (mods & event->state) == mods)
3507 gtk_menu_shell_cancel (menu_shell);
3515 switch (event->keyval)
3517 case GDK_KEY_Delete:
3518 case GDK_KEY_KP_Delete:
3519 case GDK_KEY_BackSpace:
3526 /* Figure out what modifiers went into determining the key symbol */
3527 _gtk_translate_keyboard_accel_state (gdk_keymap_get_for_display (display),
3528 event->hardware_keycode,
3530 gtk_accelerator_get_default_mod_mask (),
3532 &accel_key, NULL, NULL, &consumed_modifiers);
3534 accel_key = gdk_keyval_to_lower (accel_key);
3535 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3537 /* If lowercasing affects the keysym, then we need to include SHIFT
3538 * in the modifiers, We re-upper case when we match against the
3539 * keyval, but display and save in caseless form.
3541 if (accel_key != event->keyval)
3542 accel_mods |= GDK_SHIFT_MASK;
3544 /* Modify the accelerators */
3545 if (can_change_accels &&
3546 menu_shell->priv->active_menu_item &&
3547 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3548 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3549 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3551 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3552 gboolean locked, replace_accels = TRUE;
3555 path = get_accel_path (menu_item, &locked);
3556 if (!path || locked)
3558 /* Can't change accelerators on menu_items without paths
3559 * (basically, those items are accelerator-locked).
3561 gtk_widget_error_bell (widget);
3567 /* For the keys that act to delete the current setting,
3568 * we delete the current setting if there is one, otherwise,
3569 * we set the key as the accelerator.
3575 if (gtk_accel_map_lookup_entry (path, &key) &&
3576 (key.accel_key || key.accel_mods))
3582 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3586 /* We failed, probably because this key is in use
3587 * and locked already.
3589 gtk_widget_error_bell (widget);
3598 check_threshold (GtkWidget *widget,
3607 ABS (start_x - x) > THRESHOLD ||
3608 ABS (start_y - y) > THRESHOLD;
3612 definitely_within_item (GtkWidget *widget,
3616 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3619 w = gdk_window_get_width (window);
3620 h = gdk_window_get_height (window);
3623 check_threshold (widget, 0, 0, x, y) &&
3624 check_threshold (widget, w - 1, 0, x, y) &&
3625 check_threshold (widget, w - 1, h - 1, x, y) &&
3626 check_threshold (widget, 0, h - 1, x, y);
3630 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3632 GtkMenuPrivate *priv = menu->priv;
3634 return priv->navigation_height && priv->navigation_width;
3638 gtk_menu_motion_notify (GtkWidget *widget,
3639 GdkEventMotion *event)
3641 GtkWidget *menu_item;
3643 GtkMenuShell *menu_shell;
3645 GdkDevice *source_device;
3647 gboolean need_enter;
3649 source_device = gdk_event_get_source_device ((GdkEvent *) event);
3651 if (GTK_IS_MENU (widget) &&
3652 gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
3654 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3656 if (priv->ignore_button_release)
3657 priv->ignore_button_release = FALSE;
3659 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3663 /* We received the event for one of two reasons:
3665 * a) We are the active menu, and did gtk_grab_add()
3666 * b) The widget is a child of ours, and the event was propagated
3668 * Since for computation of navigation regions, we want the menu which
3669 * is the parent of the menu item, for a), we need to find that menu,
3670 * which may be different from 'widget'.
3672 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3673 parent = gtk_widget_get_parent (menu_item);
3674 if (!GTK_IS_MENU_ITEM (menu_item) ||
3675 !GTK_IS_MENU (parent))
3678 menu_shell = GTK_MENU_SHELL (parent);
3679 menu = GTK_MENU (menu_shell);
3681 if (definitely_within_item (menu_item, event->x, event->y))
3682 menu_shell->priv->activate_time = 0;
3684 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3686 /* Check to see if we are within an active submenu's navigation region
3688 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3691 /* Make sure we pop down if we enter a non-selectable menu item, so we
3692 * don't show a submenu when the cursor is outside the stay-up triangle.
3694 if (!_gtk_menu_item_is_selectable (menu_item))
3696 /* We really want to deselect, but this gives the menushell code
3697 * a chance to do some bookkeeping about the menuitem.
3699 gtk_menu_shell_select_item (menu_shell, menu_item);
3705 /* The menu is now sensitive to enter events on its items, but
3706 * was previously sensitive. So we fake an enter event.
3708 menu_shell->priv->ignore_enter = FALSE;
3710 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3711 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3713 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3716 send_event->crossing.window = g_object_ref (event->window);
3717 send_event->crossing.time = event->time;
3718 send_event->crossing.send_event = TRUE;
3719 send_event->crossing.x_root = event->x_root;
3720 send_event->crossing.y_root = event->y_root;
3721 send_event->crossing.x = event->x;
3722 send_event->crossing.y = event->y;
3723 send_event->crossing.state = event->state;
3724 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3726 /* We send the event to 'widget', the currently active menu,
3727 * instead of 'menu', the menu that the pointer is in. This
3728 * will ensure that the event will be ignored unless the
3729 * menuitem is a child of the active menu or some parent
3730 * menu of the active menu.
3732 result = gtk_widget_event (widget, send_event);
3733 gdk_event_free (send_event);
3743 get_double_arrows (GtkMenu *menu)
3745 GtkMenuPrivate *priv = menu->priv;
3746 gboolean double_arrows;
3747 GtkArrowPlacement arrow_placement;
3749 gtk_widget_style_get (GTK_WIDGET (menu),
3750 "double-arrows", &double_arrows,
3751 "arrow-placement", &arrow_placement,
3754 if (arrow_placement != GTK_ARROWS_BOTH)
3757 return double_arrows || (priv->initially_pushed_in &&
3758 priv->scroll_offset != 0);
3762 gtk_menu_scroll_by (GtkMenu *menu,
3765 GtkMenuPrivate *priv = menu->priv;
3766 GtkBorder arrow_border;
3770 gboolean double_arrows;
3772 widget = GTK_WIDGET (menu);
3773 offset = priv->scroll_offset + step;
3775 get_arrows_border (menu, &arrow_border);
3777 double_arrows = get_double_arrows (menu);
3779 /* If we scroll upward and the non-visible top part
3780 * is smaller than the scroll arrow it would be
3781 * pretty stupid to show the arrow and taking more
3782 * screen space than just scrolling to the top.
3785 if ((step < 0) && (offset < arrow_border.top))
3788 /* Don't scroll over the top if we weren't before: */
3789 if ((priv->scroll_offset >= 0) && (offset < 0))
3792 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3794 if (priv->scroll_offset == 0 &&
3795 view_height >= priv->requested_height)
3798 /* Don't scroll past the bottom if we weren't before: */
3799 if (priv->scroll_offset > 0)
3800 view_height -= arrow_border.top;
3802 /* When both arrows are always shown,
3803 * reduce view height even more.
3806 view_height -= arrow_border.bottom;
3808 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3809 (offset + view_height > priv->requested_height))
3810 offset = priv->requested_height - view_height;
3812 if (offset != priv->scroll_offset)
3813 gtk_menu_scroll_to (menu, offset);
3817 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3818 gboolean touchscreen_mode)
3820 GtkMenuPrivate *priv = menu->priv;
3821 gboolean upper_visible;
3822 gboolean lower_visible;
3824 upper_visible = priv->upper_arrow_visible;
3825 lower_visible = priv->lower_arrow_visible;
3827 gtk_menu_scroll_by (menu, priv->scroll_step);
3829 if (touchscreen_mode &&
3830 (upper_visible != priv->upper_arrow_visible ||
3831 lower_visible != priv->lower_arrow_visible))
3833 /* We are about to hide a scroll arrow while the mouse is pressed,
3834 * this would cause the uncovered menu item to be activated on button
3835 * release. Therefore we need to ignore button release here
3837 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3838 priv->ignore_button_release = TRUE;
3843 gtk_menu_scroll_timeout (gpointer data)
3846 gboolean touchscreen_mode;
3848 menu = GTK_MENU (data);
3850 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3851 "gtk-touchscreen-mode", &touchscreen_mode,
3854 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3860 gtk_menu_scroll_timeout_initial (gpointer data)
3864 gboolean touchscreen_mode;
3866 menu = GTK_MENU (data);
3868 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3869 "gtk-timeout-repeat", &timeout,
3870 "gtk-touchscreen-mode", &touchscreen_mode,
3873 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3875 gtk_menu_remove_scroll_timeout (menu);
3877 menu->priv->scroll_timeout =
3878 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3884 gtk_menu_start_scrolling (GtkMenu *menu)
3887 gboolean touchscreen_mode;
3889 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3890 "gtk-timeout-repeat", &timeout,
3891 "gtk-touchscreen-mode", &touchscreen_mode,
3894 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3896 menu->priv->scroll_timeout =
3897 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3901 gtk_menu_scroll (GtkWidget *widget,
3902 GdkEventScroll *event)
3904 GtkMenu *menu = GTK_MENU (widget);
3906 switch (event->direction)
3908 case GDK_SCROLL_RIGHT:
3909 case GDK_SCROLL_DOWN:
3910 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3912 case GDK_SCROLL_LEFT:
3914 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3922 get_arrows_sensitive_area (GtkMenu *menu,
3923 GdkRectangle *upper,
3924 GdkRectangle *lower)
3926 GtkArrowPlacement arrow_placement;
3927 GtkWidget *widget = GTK_WIDGET (menu);
3931 guint vertical_padding;
3933 gint scroll_arrow_height;
3936 window = gtk_widget_get_window (widget);
3937 width = gdk_window_get_width (window);
3938 height = gdk_window_get_height (window);
3940 gtk_widget_style_get (widget,
3941 "vertical-padding", &vertical_padding,
3942 "scroll-arrow-vlength", &scroll_arrow_height,
3943 "arrow-placement", &arrow_placement,
3946 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3947 get_menu_padding (widget, &padding);
3949 gdk_window_get_position (window, &win_x, &win_y);
3951 switch (arrow_placement)
3953 case GTK_ARROWS_BOTH:
3958 upper->width = width;
3959 upper->height = scroll_arrow_height + border + padding.top;
3965 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3966 lower->width = width;
3967 lower->height = scroll_arrow_height + border + padding.bottom;
3971 case GTK_ARROWS_START:
3976 upper->width = width / 2;
3977 upper->height = scroll_arrow_height + border + padding.top;
3982 lower->x = win_x + width / 2;
3984 lower->width = width / 2;
3985 lower->height = scroll_arrow_height + border + padding.bottom;
3989 case GTK_ARROWS_END:
3993 upper->y = win_y + height - border - scroll_arrow_height;
3994 upper->width = width / 2;
3995 upper->height = scroll_arrow_height + border + padding.top;
4000 lower->x = win_x + width / 2;
4001 lower->y = win_y + height - border - scroll_arrow_height;
4002 lower->width = width / 2;
4003 lower->height = scroll_arrow_height + border + padding.bottom;
4011 gtk_menu_handle_scrolling (GtkMenu *menu,
4017 GtkMenuPrivate *priv = menu->priv;
4018 GtkMenuShell *menu_shell;
4021 gboolean scroll_fast = FALSE;
4023 gboolean touchscreen_mode;
4025 menu_shell = GTK_MENU_SHELL (menu);
4027 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4028 "gtk-touchscreen-mode", &touchscreen_mode,
4031 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4036 /* upper arrow handling */
4038 get_arrows_sensitive_area (menu, &rect, NULL);
4041 if (priv->upper_arrow_visible && !priv->tearoff_active &&
4042 (x >= rect.x) && (x < rect.x + rect.width) &&
4043 (y >= rect.y) && (y < rect.y + rect.height))
4048 if (touchscreen_mode)
4049 priv->upper_arrow_prelight = in_arrow;
4051 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4053 gboolean arrow_pressed = FALSE;
4055 if (priv->upper_arrow_visible && !priv->tearoff_active)
4057 if (touchscreen_mode)
4059 if (enter && priv->upper_arrow_prelight)
4061 if (priv->scroll_timeout == 0)
4063 /* Deselect the active item so that
4064 * any submenus are popped down
4066 gtk_menu_shell_deselect (menu_shell);
4068 gtk_menu_remove_scroll_timeout (menu);
4069 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4073 /* Only do stuff on click. */
4074 gtk_menu_start_scrolling (menu);
4075 arrow_pressed = TRUE;
4080 arrow_pressed = TRUE;
4085 gtk_menu_stop_scrolling (menu);
4088 else /* !touchscreen_mode */
4090 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4092 if (enter && in_arrow &&
4093 (!priv->upper_arrow_prelight ||
4094 priv->scroll_fast != scroll_fast))
4096 priv->upper_arrow_prelight = TRUE;
4097 priv->scroll_fast = scroll_fast;
4099 /* Deselect the active item so that
4100 * any submenus are popped down
4102 gtk_menu_shell_deselect (menu_shell);
4104 gtk_menu_remove_scroll_timeout (menu);
4105 priv->scroll_step = scroll_fast
4106 ? -MENU_SCROLL_STEP2
4107 : -MENU_SCROLL_STEP1;
4109 priv->scroll_timeout =
4110 gdk_threads_add_timeout (scroll_fast
4111 ? MENU_SCROLL_TIMEOUT2
4112 : MENU_SCROLL_TIMEOUT1,
4113 gtk_menu_scroll_timeout, menu);
4115 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4117 gtk_menu_stop_scrolling (menu);
4122 /* gtk_menu_start_scrolling() might have hit the top of the
4123 * menu, so check if the button isn't insensitive before
4124 * changing it to something else.
4126 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4128 GtkStateFlags arrow_state = 0;
4131 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4133 if (priv->upper_arrow_prelight)
4134 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4136 if (arrow_state != priv->upper_arrow_state)
4138 priv->upper_arrow_state = arrow_state;
4140 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4146 /* lower arrow handling */
4148 get_arrows_sensitive_area (menu, NULL, &rect);
4151 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4152 (x >= rect.x) && (x < rect.x + rect.width) &&
4153 (y >= rect.y) && (y < rect.y + rect.height))
4158 if (touchscreen_mode)
4159 priv->lower_arrow_prelight = in_arrow;
4161 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4163 gboolean arrow_pressed = FALSE;
4165 if (priv->lower_arrow_visible && !priv->tearoff_active)
4167 if (touchscreen_mode)
4169 if (enter && priv->lower_arrow_prelight)
4171 if (priv->scroll_timeout == 0)
4173 /* Deselect the active item so that
4174 * any submenus are popped down
4176 gtk_menu_shell_deselect (menu_shell);
4178 gtk_menu_remove_scroll_timeout (menu);
4179 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4183 /* Only do stuff on click. */
4184 gtk_menu_start_scrolling (menu);
4185 arrow_pressed = TRUE;
4190 arrow_pressed = TRUE;
4195 gtk_menu_stop_scrolling (menu);
4198 else /* !touchscreen_mode */
4200 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4202 if (enter && in_arrow &&
4203 (!priv->lower_arrow_prelight ||
4204 priv->scroll_fast != scroll_fast))
4206 priv->lower_arrow_prelight = TRUE;
4207 priv->scroll_fast = scroll_fast;
4209 /* Deselect the active item so that
4210 * any submenus are popped down
4212 gtk_menu_shell_deselect (menu_shell);
4214 gtk_menu_remove_scroll_timeout (menu);
4215 priv->scroll_step = scroll_fast
4217 : MENU_SCROLL_STEP1;
4219 priv->scroll_timeout =
4220 gdk_threads_add_timeout (scroll_fast
4221 ? MENU_SCROLL_TIMEOUT2
4222 : MENU_SCROLL_TIMEOUT1,
4223 gtk_menu_scroll_timeout, menu);
4225 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4227 gtk_menu_stop_scrolling (menu);
4232 /* gtk_menu_start_scrolling() might have hit the bottom of the
4233 * menu, so check if the button isn't insensitive before
4234 * changing it to something else.
4236 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4238 GtkStateFlags arrow_state = 0;
4241 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4243 if (priv->lower_arrow_prelight)
4244 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4246 if (arrow_state != priv->lower_arrow_state)
4248 priv->lower_arrow_state = arrow_state;
4250 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4258 gtk_menu_enter_notify (GtkWidget *widget,
4259 GdkEventCrossing *event)
4261 GtkWidget *menu_item;
4263 GdkDevice *source_device;
4265 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4266 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4267 event->mode == GDK_CROSSING_STATE_CHANGED)
4270 source_device = gdk_event_get_source_device ((GdkEvent *) event);
4271 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4273 if (GTK_IS_MENU (widget) &&
4274 gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
4276 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4278 if (!menu_shell->priv->ignore_enter)
4279 gtk_menu_handle_scrolling (GTK_MENU (widget),
4280 event->x_root, event->y_root, TRUE, TRUE);
4283 if (gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN &&
4284 GTK_IS_MENU_ITEM (menu_item))
4286 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4288 if (GTK_IS_MENU (menu))
4290 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4291 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4293 if (priv->seen_item_enter)
4295 /* This is the second enter we see for an item
4296 * on this menu. This means a release should always
4299 menu_shell->priv->activate_time = 0;
4301 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4302 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4304 if (definitely_within_item (menu_item, event->x, event->y))
4306 /* This is an actual user-enter (ie. not a pop-under)
4307 * In this case, the user must either have entered
4308 * sufficiently far enough into the item, or he must move
4309 * far enough away from the enter point. (see
4310 * gtk_menu_motion_notify())
4312 menu_shell->priv->activate_time = 0;
4316 priv->seen_item_enter = TRUE;
4320 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4321 * will not correspond to the event widget's parent. Check to see
4322 * if we are in the parent's navigation region.
4324 parent = gtk_widget_get_parent (menu_item);
4325 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4326 gtk_menu_navigating_submenu (GTK_MENU (parent),
4327 event->x_root, event->y_root))
4330 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4334 gtk_menu_leave_notify (GtkWidget *widget,
4335 GdkEventCrossing *event)
4337 GtkMenuShell *menu_shell;
4339 GtkMenuItem *menu_item;
4340 GtkWidget *event_widget;
4341 GdkDevice *source_device;
4343 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4344 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4345 event->mode == GDK_CROSSING_STATE_CHANGED)
4348 menu = GTK_MENU (widget);
4349 menu_shell = GTK_MENU_SHELL (widget);
4351 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4354 source_device = gdk_event_get_source_device ((GdkEvent *) event);
4356 if (gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
4357 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4359 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4361 if (!GTK_IS_MENU_ITEM (event_widget))
4364 menu_item = GTK_MENU_ITEM (event_widget);
4366 /* Here we check to see if we're leaving an active menu item
4367 * with a submenu, in which case we enter submenu navigation mode.
4369 if (menu_shell->priv->active_menu_item != NULL
4370 && menu_item->priv->submenu != NULL
4371 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4373 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4375 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4378 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4380 /* We are leaving an active menu item with nonactive submenu.
4381 * Deselect it so we don't surprise the user with by popping
4382 * up a submenu _after_ he left the item.
4384 gtk_menu_shell_deselect (menu_shell);
4389 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4393 pointer_on_menu_widget (GtkMenu *menu,
4397 GtkMenuPrivate *priv = menu->priv;
4398 GtkAllocation allocation;
4399 gint window_x, window_y;
4401 gtk_widget_get_allocation (GTK_WIDGET (menu), &allocation);
4402 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4403 &window_x, &window_y);
4405 if (x_root >= window_x && x_root < window_x + allocation.width &&
4406 y_root >= window_y && y_root < window_y + allocation.height)
4413 gtk_menu_captured_event (GtkWidget *widget,
4416 GdkDevice *source_device;
4417 gboolean retval = FALSE;
4418 GtkMenuPrivate *priv;
4420 gdouble x_root, y_root;
4422 GdkModifierType state;
4424 menu = GTK_MENU (widget);
4427 if (!priv->upper_arrow_visible && !priv->lower_arrow_visible)
4430 source_device = gdk_event_get_source_device (event);
4431 gdk_event_get_root_coords (event, &x_root, &y_root);
4433 switch (event->type)
4435 case GDK_TOUCH_BEGIN:
4436 case GDK_BUTTON_PRESS:
4437 if ((!gdk_event_get_button (event, &button) || button == 1) &&
4438 gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN &&
4439 pointer_on_menu_widget (menu, x_root, y_root))
4441 priv->drag_start_y = event->button.y_root;
4442 priv->initial_drag_offset = priv->scroll_offset;
4443 priv->drag_scroll_started = FALSE;
4446 priv->drag_start_y = -1;
4448 priv->drag_already_pressed = TRUE;
4451 case GDK_BUTTON_RELEASE:
4452 if (priv->drag_scroll_started)
4454 priv->drag_scroll_started = FALSE;
4455 priv->drag_start_y = -1;
4456 priv->drag_already_pressed = FALSE;
4460 case GDK_TOUCH_UPDATE:
4461 case GDK_MOTION_NOTIFY:
4462 if ((!gdk_event_get_state (event, &state) || (state & GDK_BUTTON1_MASK)
4464 gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
4466 if (!priv->drag_already_pressed)
4468 if (pointer_on_menu_widget (menu, x_root, y_root))
4470 priv->drag_start_y = y_root;
4471 priv->initial_drag_offset = priv->scroll_offset;
4472 priv->drag_scroll_started = FALSE;
4475 priv->drag_start_y = -1;
4477 priv->drag_already_pressed = TRUE;
4480 if (priv->drag_start_y < 0 && !priv->drag_scroll_started)
4483 if (priv->drag_scroll_started)
4485 gint offset, view_height;
4486 GtkBorder arrow_border;
4489 y_diff = y_root - priv->drag_start_y;
4490 offset = priv->initial_drag_offset - y_diff;
4492 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
4493 get_arrows_border (menu, &arrow_border);
4495 if (priv->upper_arrow_visible)
4496 view_height -= arrow_border.top;
4498 if (priv->lower_arrow_visible)
4499 view_height -= arrow_border.bottom;
4501 offset = CLAMP (offset, 0, priv->requested_height - view_height);
4502 gtk_menu_scroll_to (menu, offset);
4506 else if (gtk_drag_check_threshold (widget,
4507 0, priv->drag_start_y,
4510 priv->drag_scroll_started = TRUE;
4511 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
4516 case GDK_ENTER_NOTIFY:
4517 case GDK_LEAVE_NOTIFY:
4518 if (priv->drag_scroll_started)
4529 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4531 GtkMenuPrivate *priv = menu->priv;
4533 priv->navigation_x = 0;
4534 priv->navigation_y = 0;
4535 priv->navigation_width = 0;
4536 priv->navigation_height = 0;
4538 if (priv->navigation_timeout)
4540 g_source_remove (priv->navigation_timeout);
4541 priv->navigation_timeout = 0;
4545 /* When the timeout is elapsed, the navigation region is destroyed
4546 * and the menuitem under the pointer (if any) is selected.
4549 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4551 GtkMenuPopdownData *popdown_data = user_data;
4552 GtkMenu *menu = popdown_data->menu;
4553 GtkMenuPrivate *priv = menu->priv;
4554 GdkWindow *child_window;
4556 gtk_menu_stop_navigating_submenu (menu);
4558 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4560 child_window = gdk_window_get_device_position (priv->bin_window,
4561 popdown_data->device,
4566 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4568 send_event->crossing.window = g_object_ref (child_window);
4569 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4570 send_event->crossing.send_event = TRUE;
4571 gdk_event_set_device (send_event, popdown_data->device);
4573 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4575 gdk_event_free (send_event);
4583 gtk_menu_navigating_submenu (GtkMenu *menu,
4587 GtkMenuPrivate *priv = menu->priv;
4590 if (!gtk_menu_has_navigation_triangle (menu))
4593 width = priv->navigation_width;
4594 height = priv->navigation_height;
4596 /* Check if x/y are in the triangle spanned by the navigation parameters */
4598 /* 1) Move the coordinates so the triangle starts at 0,0 */
4599 event_x -= priv->navigation_x;
4600 event_y -= priv->navigation_y;
4602 /* 2) Ensure both legs move along the positive axis */
4614 /* 3) Check that the given coordinate is inside the triangle. The formula
4615 * is a transformed form of this formula: x/w + y/h <= 1
4617 if (event_x >= 0 && event_y >= 0 &&
4618 event_x * height + event_y * width <= width * height)
4624 gtk_menu_stop_navigating_submenu (menu);
4630 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4631 GtkMenuItem *menu_item,
4632 GdkEventCrossing *event)
4634 GtkMenuPrivate *priv = menu->priv;
4635 gint submenu_left = 0;
4636 gint submenu_right = 0;
4637 gint submenu_top = 0;
4638 gint submenu_bottom = 0;
4640 GtkWidget *event_widget;
4641 GtkMenuPopdownData *popdown_data;
4644 g_return_if_fail (menu_item->priv->submenu != NULL);
4645 g_return_if_fail (event != NULL);
4647 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4649 window = gtk_widget_get_window (menu_item->priv->submenu);
4650 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4652 submenu_right = submenu_left + gdk_window_get_width (window);
4653 submenu_bottom = submenu_top + gdk_window_get_height (window);
4655 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4657 if (event->x >= 0 && event->x < width)
4661 gtk_menu_stop_navigating_submenu (menu);
4663 /* The navigation region is the triangle closest to the x/y
4664 * location of the rectangle. This is why the width or height
4667 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4670 priv->navigation_x = submenu_left;
4671 priv->navigation_width = event->x_root - submenu_left;
4676 priv->navigation_x = submenu_right;
4677 priv->navigation_width = event->x_root - submenu_right;
4683 priv->navigation_y = event->y_root;
4684 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4686 if (priv->navigation_height >= 0)
4692 priv->navigation_y = event->y_root;
4693 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4695 if (priv->navigation_height <= 0)
4699 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4700 "gtk-menu-popdown-delay", &popdown_delay,
4703 popdown_data = g_new (GtkMenuPopdownData, 1);
4704 popdown_data->menu = menu;
4705 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4707 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4709 gtk_menu_stop_navigating_submenu_cb,
4711 (GDestroyNotify) g_free);
4716 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4720 g_return_if_fail (GTK_IS_MENU (menu_shell));
4722 parent = menu_shell->priv->parent_menu_shell;
4724 menu_shell->priv->activate_time = 0;
4725 gtk_menu_popdown (GTK_MENU (menu_shell));
4728 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4732 gtk_menu_position (GtkMenu *menu,
4733 gboolean set_scroll_offset)
4735 GtkMenuPrivate *priv = menu->priv;
4737 GtkRequisition requisition;
4741 GdkScreen *pointer_screen;
4742 GdkRectangle monitor;
4745 widget = GTK_WIDGET (menu);
4747 screen = gtk_widget_get_screen (widget);
4748 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4749 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4751 /* Realize so we have the proper width and heigh to figure out
4752 * the right place to popup the menu.
4754 gtk_widget_realize (priv->toplevel);
4755 requisition.width = gtk_widget_get_allocated_width (widget);
4756 requisition.height = gtk_widget_get_allocated_height (widget);
4758 if (pointer_screen != screen)
4760 /* Pointer is on a different screen; roughly center the
4761 * menu on the screen. If someone was using multiscreen
4762 * + Xinerama together they'd probably want something
4763 * fancier; but that is likely to be vanishingly rare.
4765 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4766 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4769 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4770 priv->initially_pushed_in = FALSE;
4772 /* Set the type hint here to allow custom position functions
4773 * to set a different hint
4775 if (!gtk_widget_get_visible (priv->toplevel))
4776 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4778 if (priv->position_func)
4780 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4781 priv->position_func_data);
4783 if (priv->monitor_num < 0)
4784 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4786 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4790 gint space_left, space_right, space_above, space_below;
4794 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4796 get_menu_padding (widget, &padding);
4798 /* The placement of popup menus horizontally works like this (with
4799 * RTL in parentheses)
4801 * - If there is enough room to the right (left) of the mouse cursor,
4802 * position the menu there.
4804 * - Otherwise, if if there is enough room to the left (right) of the
4805 * mouse cursor, position the menu there.
4807 * - Otherwise if the menu is smaller than the monitor, position it
4808 * on the side of the mouse cursor that has the most space available
4810 * - Otherwise (if there is simply not enough room for the menu on the
4811 * monitor), position it as far left (right) as possible.
4813 * Positioning in the vertical direction is similar: first try below
4814 * mouse cursor, then above.
4816 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4818 space_left = x - monitor.x;
4819 space_right = monitor.x + monitor.width - x - 1;
4820 space_above = y - monitor.y;
4821 space_below = monitor.y + monitor.height - y - 1;
4823 /* Position horizontally. */
4825 /* the amount of space we need to position the menu.
4826 * Note the menu is offset "thickness" pixels
4828 needed_width = requisition.width - padding.left;
4830 if (needed_width <= space_left ||
4831 needed_width <= space_right)
4833 if ((rtl && needed_width <= space_left) ||
4834 (!rtl && needed_width > space_right))
4837 x = x + padding.left - requisition.width + 1;
4841 /* position right */
4842 x = x - padding.right;
4845 /* x is clamped on-screen further down */
4847 else if (requisition.width <= monitor.width)
4849 /* the menu is too big to fit on either side of the mouse
4850 * cursor, but smaller than the monitor. Position it on
4851 * the side that has the most space
4853 if (space_left > space_right)
4861 x = monitor.x + monitor.width - requisition.width;
4864 else /* menu is simply too big for the monitor */
4869 x = monitor.x + monitor.width - requisition.width;
4878 /* Position vertically.
4879 * The algorithm is the same as above, but simpler
4880 * because we don't have to take RTL into account.
4882 needed_height = requisition.height - padding.top;
4884 if (needed_height <= space_above ||
4885 needed_height <= space_below)
4887 if (needed_height <= space_below)
4888 y = y - padding.top;
4890 y = y + padding.bottom - requisition.height + 1;
4892 y = CLAMP (y, monitor.y,
4893 monitor.y + monitor.height - requisition.height);
4895 else if (needed_height > space_below && needed_height > space_above)
4897 if (space_below >= space_above)
4898 y = monitor.y + monitor.height - requisition.height;
4910 if (y + requisition.height > monitor.y + monitor.height)
4912 if (priv->initially_pushed_in)
4913 scroll_offset += (monitor.y + monitor.height) - requisition.height - y;
4914 y = (monitor.y + monitor.height) - requisition.height;
4919 if (priv->initially_pushed_in)
4920 scroll_offset += monitor.y - y;
4924 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4926 if (GTK_MENU_SHELL (menu)->priv->active)
4928 priv->have_position = TRUE;
4929 priv->position_x = x;
4930 priv->position_y = y;
4933 if (scroll_offset != 0)
4935 GtkBorder arrow_border;
4937 get_arrows_border (menu, &arrow_border);
4938 scroll_offset += arrow_border.top;
4941 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4944 if (!GTK_MENU_SHELL (menu)->priv->active)
4946 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4947 requisition.width, requisition.height);
4950 if (set_scroll_offset)
4951 priv->scroll_offset = scroll_offset;
4955 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4957 GtkMenuPrivate *priv = menu->priv;
4959 if (priv->scroll_timeout)
4961 g_source_remove (priv->scroll_timeout);
4962 priv->scroll_timeout = 0;
4967 gtk_menu_stop_scrolling (GtkMenu *menu)
4969 GtkMenuPrivate *priv = menu->priv;
4970 gboolean touchscreen_mode;
4972 gtk_menu_remove_scroll_timeout (menu);
4974 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4975 "gtk-touchscreen-mode", &touchscreen_mode,
4978 if (!touchscreen_mode)
4980 priv->upper_arrow_prelight = FALSE;
4981 priv->lower_arrow_prelight = FALSE;
4986 gtk_menu_scroll_to (GtkMenu *menu,
4989 GtkMenuPrivate *priv = menu->priv;
4990 GtkBorder arrow_border, padding;
4993 gint view_width, view_height;
4996 guint vertical_padding;
4997 guint horizontal_padding;
4998 gboolean double_arrows;
5000 widget = GTK_WIDGET (menu);
5002 if (priv->tearoff_active && priv->tearoff_adjustment)
5003 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
5005 /* Move/resize the viewport according to arrows: */
5006 view_width = gtk_widget_get_allocated_width (widget);
5007 view_height = gtk_widget_get_allocated_height (widget);
5009 gtk_widget_style_get (GTK_WIDGET (menu),
5010 "vertical-padding", &vertical_padding,
5011 "horizontal-padding", &horizontal_padding,
5014 get_menu_padding (widget, &padding);
5015 double_arrows = get_double_arrows (menu);
5017 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
5019 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
5020 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
5021 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
5022 padding.top - padding.bottom;
5024 x = border_width + padding.left + horizontal_padding;
5025 y = border_width + padding.top + vertical_padding;
5027 if (double_arrows && !priv->tearoff_active)
5029 if (view_height < menu_height ||
5030 (offset > 0 && priv->scroll_offset > 0) ||
5031 (offset < 0 && priv->scroll_offset < 0))
5033 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
5034 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
5036 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
5037 gtk_widget_queue_draw (GTK_WIDGET (menu));
5039 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
5041 get_arrows_border (menu, &arrow_border);
5042 y += arrow_border.top;
5043 view_height -= arrow_border.top;
5044 view_height -= arrow_border.bottom;
5047 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
5050 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
5052 if (priv->upper_arrow_prelight)
5053 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
5055 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
5058 if (offset >= menu_height - view_height)
5059 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
5062 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
5064 if (priv->lower_arrow_prelight)
5065 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
5067 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
5070 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
5071 (priv->lower_arrow_state != lower_arrow_previous_state))
5072 gtk_widget_queue_draw (GTK_WIDGET (menu));
5074 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5075 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5077 /* At the upper border, possibly remove timeout */
5078 if (priv->scroll_step < 0)
5080 gtk_menu_stop_scrolling (menu);
5081 gtk_widget_queue_draw (GTK_WIDGET (menu));
5085 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5086 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5088 /* At the lower border, possibly remove timeout */
5089 if (priv->scroll_step > 0)
5091 gtk_menu_stop_scrolling (menu);
5092 gtk_widget_queue_draw (GTK_WIDGET (menu));
5096 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
5100 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
5101 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
5103 gtk_menu_stop_scrolling (menu);
5104 gtk_widget_queue_draw (GTK_WIDGET (menu));
5107 else if (!priv->tearoff_active)
5109 gboolean last_visible;
5111 last_visible = priv->upper_arrow_visible;
5112 priv->upper_arrow_visible = offset > 0;
5114 /* upper_arrow_visible may have changed, so requery the border */
5115 get_arrows_border (menu, &arrow_border);
5116 view_height -= arrow_border.top;
5118 if ((last_visible != priv->upper_arrow_visible) &&
5119 !priv->upper_arrow_visible)
5121 priv->upper_arrow_prelight = FALSE;
5123 /* If we hide the upper arrow, possibly remove timeout */
5124 if (priv->scroll_step < 0)
5126 gtk_menu_stop_scrolling (menu);
5127 gtk_widget_queue_draw (GTK_WIDGET (menu));
5131 last_visible = priv->lower_arrow_visible;
5132 priv->lower_arrow_visible = offset < menu_height - view_height;
5134 /* lower_arrow_visible may have changed, so requery the border */
5135 get_arrows_border (menu, &arrow_border);
5136 view_height -= arrow_border.bottom;
5138 if ((last_visible != priv->lower_arrow_visible) &&
5139 !priv->lower_arrow_visible)
5141 priv->lower_arrow_prelight = FALSE;
5143 /* If we hide the lower arrow, possibly remove timeout */
5144 if (priv->scroll_step > 0)
5146 gtk_menu_stop_scrolling (menu);
5147 gtk_widget_queue_draw (GTK_WIDGET (menu));
5151 y += arrow_border.top;
5154 /* Scroll the menu: */
5155 if (gtk_widget_get_realized (widget))
5156 gdk_window_move (priv->bin_window, 0, -offset);
5158 if (gtk_widget_get_realized (widget))
5159 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5161 priv->scroll_offset = offset;
5165 compute_child_offset (GtkMenu *menu,
5166 GtkWidget *menu_item,
5169 gboolean *is_last_child)
5171 GtkMenuPrivate *priv = menu->priv;
5172 gint item_top_attach;
5173 gint item_bottom_attach;
5174 gint child_offset = 0;
5177 get_effective_child_attach (menu_item, NULL, NULL,
5178 &item_top_attach, &item_bottom_attach);
5180 /* there is a possibility that we get called before _size_request,
5181 * so check the height table for safety.
5183 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5186 /* when we have a row with only invisible children, its height will
5187 * be zero, so there's no need to check WIDGET_VISIBLE here
5189 for (i = 0; i < item_top_attach; i++)
5190 child_offset += priv->heights[i];
5193 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5195 *offset = child_offset;
5197 *height = priv->heights[item_top_attach];
5203 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5204 GtkWidget *menu_item)
5206 GtkMenu *menu = GTK_MENU (menu_shell);
5207 GtkMenuPrivate *priv = menu->priv;
5208 GtkWidget *widget = GTK_WIDGET (menu_shell);
5209 gint child_offset, child_height;
5213 gboolean last_child = 0;
5215 /* We need to check if the selected item fully visible.
5216 * If not we need to scroll the menu so that it becomes fully
5219 if (compute_child_offset (menu, menu_item,
5220 &child_offset, &child_height, &last_child))
5222 guint vertical_padding;
5223 gboolean double_arrows;
5226 y = priv->scroll_offset;
5227 height = gdk_window_get_height (gtk_widget_get_window (widget));
5229 gtk_widget_style_get (widget,
5230 "vertical-padding", &vertical_padding,
5233 double_arrows = get_double_arrows (menu);
5234 get_menu_padding (widget, &padding);
5236 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5237 padding.top + padding.bottom +
5238 2 * vertical_padding;
5239 if (child_offset < y)
5241 /* Ignore the enter event we might get if the pointer
5244 menu_shell->priv->ignore_enter = TRUE;
5245 gtk_menu_scroll_to (menu, child_offset);
5249 GtkBorder arrow_border;
5253 get_arrows_border (menu, &arrow_border);
5254 if (!priv->tearoff_active)
5255 arrow_height = arrow_border.top + arrow_border.bottom;
5257 if (child_offset + child_height > y + height - arrow_height)
5260 if ((!last_child && !priv->tearoff_active) || double_arrows)
5261 arrow_height += arrow_border.bottom;
5263 y = child_offset + child_height - height + arrow_height;
5264 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5266 /* Need upper arrow */
5267 arrow_height += arrow_border.top;
5268 y = child_offset + child_height - height + arrow_height;
5270 /* Ignore the enter event we might get if the pointer
5273 menu_shell->priv->ignore_enter = TRUE;
5274 gtk_menu_scroll_to (menu, y);
5281 gtk_menu_select_item (GtkMenuShell *menu_shell,
5282 GtkWidget *menu_item)
5284 GtkMenu *menu = GTK_MENU (menu_shell);
5286 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5287 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5289 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5293 /* Reparent the menu, taking care of the refcounting
5295 * If unrealize is true we force a unrealize while reparenting the parent.
5296 * This can help eliminate flicker in some cases.
5298 * What happens is that when the menu is unrealized and then re-realized,
5299 * the allocations are as follows:
5301 * parent - 1x1 at (0,0)
5302 * child1 - 100x20 at (0,0)
5303 * child2 - 100x20 at (0,20)
5304 * child3 - 100x20 at (0,40)
5306 * That is, the parent is small but the children are full sized. Then,
5307 * when the queued_resize gets processed, the parent gets resized to
5310 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5311 * contains the following logic:
5313 * - if a move or resize operation on a window would change the clip
5314 * region on the children, then before the window is resized
5315 * the background for children is temporarily set to None, the
5316 * move/resize done, and the background for the children restored.
5318 * So, at the point where the parent is resized to final size, the
5319 * background for the children is temporarily None, and thus they
5320 * are not cleared to the background color and the previous background
5321 * (the image of the menu) is left in place.
5324 gtk_menu_reparent (GtkMenu *menu,
5325 GtkWidget *new_parent,
5328 GObject *object = G_OBJECT (menu);
5329 GtkWidget *widget = GTK_WIDGET (menu);
5330 gboolean was_floating = g_object_is_floating (object);
5332 g_object_ref_sink (object);
5336 g_object_ref (object);
5337 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5338 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5339 g_object_unref (object);
5342 gtk_widget_reparent (widget, new_parent);
5345 g_object_force_floating (object);
5347 g_object_unref (object);
5351 gtk_menu_show_all (GtkWidget *widget)
5353 /* Show children, but not self. */
5354 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5358 * gtk_menu_set_screen:
5360 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5361 * determined by the widget the menu is attached to
5363 * Sets the #GdkScreen on which the menu will be displayed.
5368 gtk_menu_set_screen (GtkMenu *menu,
5371 g_return_if_fail (GTK_IS_MENU (menu));
5372 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5374 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5378 menu_change_screen (menu, screen);
5382 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5384 attach_widget_screen_changed (attach_widget, NULL, menu);
5391 * @child: a #GtkMenuItem
5392 * @left_attach: The column number to attach the left side of the item to
5393 * @right_attach: The column number to attach the right side of the item to
5394 * @top_attach: The row number to attach the top of the item to
5395 * @bottom_attach: The row number to attach the bottom of the item to
5397 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5398 * an item will occupy is specified by @left_attach, @right_attach,
5399 * @top_attach and @bottom_attach. These each represent the leftmost,
5400 * rightmost, uppermost and lower column and row numbers of the table.
5401 * (Columns and rows are indexed from zero).
5403 * Note that this function is not related to gtk_menu_detach().
5408 gtk_menu_attach (GtkMenu *menu,
5413 guint bottom_attach)
5415 GtkMenuShell *menu_shell;
5418 g_return_if_fail (GTK_IS_MENU (menu));
5419 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5420 parent = gtk_widget_get_parent (child);
5421 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5422 g_return_if_fail (left_attach < right_attach);
5423 g_return_if_fail (top_attach < bottom_attach);
5425 menu_shell = GTK_MENU_SHELL (menu);
5429 AttachInfo *ai = get_attach_info (child);
5431 ai->left_attach = left_attach;
5432 ai->right_attach = right_attach;
5433 ai->top_attach = top_attach;
5434 ai->bottom_attach = bottom_attach;
5436 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5438 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5440 menu_queue_resize (menu);
5444 gtk_container_child_set (GTK_CONTAINER (parent), child,
5445 "left-attach", left_attach,
5446 "right-attach", right_attach,
5447 "top-attach", top_attach,
5448 "bottom-attach", bottom_attach,
5454 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5458 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5459 "gtk-menu-popup-delay", &popup_delay,
5466 find_child_containing (GtkMenuShell *menu_shell,
5474 /* find a child which includes the area given by
5475 * left, right, top, bottom.
5477 for (list = menu_shell->priv->children; list; list = list->next)
5481 if (!_gtk_menu_item_is_selectable (list->data))
5484 get_effective_child_attach (list->data, &l, &r, &t, &b);
5486 if (l <= left && right <= r && t <= top && bottom <= b)
5487 return GTK_WIDGET (list->data);
5494 gtk_menu_move_current (GtkMenuShell *menu_shell,
5495 GtkMenuDirectionType direction)
5497 GtkMenu *menu = GTK_MENU (menu_shell);
5500 GtkWidget *match = NULL;
5502 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5506 case GTK_MENU_DIR_CHILD:
5507 direction = GTK_MENU_DIR_PARENT;
5509 case GTK_MENU_DIR_PARENT:
5510 direction = GTK_MENU_DIR_CHILD;
5516 /* use special table menu key bindings */
5517 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5519 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5521 if (direction == GTK_MENU_DIR_NEXT)
5523 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5525 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5533 for (i = 0; i < t; i++)
5535 match = find_child_containing (menu_shell,
5536 l, l + 1, i, i + 1);
5542 else if (direction == GTK_MENU_DIR_PREV)
5544 for (i = t; i > 0; i--)
5546 match = find_child_containing (menu_shell,
5547 l, l + 1, i - 1, i);
5555 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5557 match = find_child_containing (menu_shell,
5558 l, l + 1, i - 1, i);
5564 else if (direction == GTK_MENU_DIR_PARENT)
5566 /* we go one left if possible */
5568 match = find_child_containing (menu_shell,
5569 l - 1, l, t, t + 1);
5573 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5576 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5577 match = menu_shell->priv->active_menu_item;
5580 else if (direction == GTK_MENU_DIR_CHILD)
5582 /* we go one right if possible */
5583 if (r < gtk_menu_get_n_columns (menu))
5584 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5588 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5590 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5592 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5593 match = menu_shell->priv->active_menu_item;
5599 gtk_menu_shell_select_item (menu_shell, match);
5604 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5608 get_visible_size (GtkMenu *menu)
5610 GtkMenuPrivate *priv = menu->priv;
5611 GtkAllocation allocation;
5612 GtkWidget *widget = GTK_WIDGET (menu);
5613 GtkContainer *container = GTK_CONTAINER (menu);
5617 gtk_widget_get_allocation (widget, &allocation);
5618 get_menu_padding (widget, &padding);
5620 menu_height = (allocation.height -
5621 (2 * gtk_container_get_border_width (container)) -
5622 padding.top - padding.bottom);
5624 if (!priv->tearoff_active)
5626 GtkBorder arrow_border;
5628 get_arrows_border (menu, &arrow_border);
5629 menu_height -= arrow_border.top;
5630 menu_height -= arrow_border.bottom;
5636 /* Find the sensitive on-screen child containing @y, or if none,
5637 * the nearest selectable onscreen child. (%NULL if none)
5640 child_at (GtkMenu *menu,
5643 GtkMenuPrivate *priv = menu->priv;
5644 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5645 GtkWidget *child = NULL;
5646 gint child_offset = 0;
5649 gint lower, upper; /* Onscreen bounds */
5651 menu_height = get_visible_size (menu);
5652 lower = priv->scroll_offset;
5653 upper = priv->scroll_offset + menu_height;
5655 for (children = menu_shell->priv->children; children; children = children->next)
5657 if (gtk_widget_get_visible (children->data))
5659 GtkRequisition child_requisition;
5661 gtk_widget_get_preferred_size (children->data,
5662 &child_requisition, NULL);
5664 if (_gtk_menu_item_is_selectable (children->data) &&
5665 child_offset >= lower &&
5666 child_offset + child_requisition.height <= upper)
5668 child = children->data;
5670 if (child_offset + child_requisition.height > y &&
5671 !GTK_IS_TEAROFF_MENU_ITEM (child))
5675 child_offset += child_requisition.height;
5683 get_menu_height (GtkMenu *menu)
5685 GtkMenuPrivate *priv = menu->priv;
5686 GtkAllocation allocation;
5687 GtkWidget *widget = GTK_WIDGET (menu);
5691 gtk_widget_get_allocation (widget, &allocation);
5692 get_menu_padding (widget, &padding);
5694 height = allocation.height;
5695 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5696 padding.top + padding.bottom;
5698 if (!priv->tearoff_active)
5700 GtkBorder arrow_border;
5702 get_arrows_border (menu, &arrow_border);
5703 height -= arrow_border.top;
5704 height -= arrow_border.bottom;
5711 gtk_menu_real_move_scroll (GtkMenu *menu,
5714 GtkMenuPrivate *priv = menu->priv;
5715 gint page_size = get_visible_size (menu);
5716 gint end_position = get_menu_height (menu);
5717 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5721 case GTK_SCROLL_PAGE_UP:
5722 case GTK_SCROLL_PAGE_DOWN:
5726 gint child_offset = 0;
5727 gboolean old_upper_arrow_visible;
5730 if (type == GTK_SCROLL_PAGE_UP)
5735 if (menu_shell->priv->active_menu_item)
5739 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5740 &child_offset, &child_height, NULL);
5741 child_offset += child_height / 2;
5744 menu_shell->priv->ignore_enter = TRUE;
5745 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5746 old_offset = priv->scroll_offset;
5748 new_offset = priv->scroll_offset + step;
5749 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5751 gtk_menu_scroll_to (menu, new_offset);
5753 if (menu_shell->priv->active_menu_item)
5755 GtkWidget *new_child;
5756 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5757 GtkBorder arrow_border;
5758 get_arrows_border (menu, &arrow_border);
5760 if (priv->scroll_offset != old_offset)
5761 step = priv->scroll_offset - old_offset;
5763 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5765 new_child = child_at (menu, child_offset + step);
5767 gtk_menu_shell_select_item (menu_shell, new_child);
5771 case GTK_SCROLL_START:
5772 /* Ignore the enter event we might get if the pointer is on the menu */
5773 menu_shell->priv->ignore_enter = TRUE;
5774 gtk_menu_scroll_to (menu, 0);
5775 gtk_menu_shell_select_first (menu_shell, TRUE);
5777 case GTK_SCROLL_END:
5778 /* Ignore the enter event we might get if the pointer is on the menu */
5779 menu_shell->priv->ignore_enter = TRUE;
5780 gtk_menu_scroll_to (menu, end_position - page_size);
5781 _gtk_menu_shell_select_last (menu_shell, TRUE);
5789 * gtk_menu_set_monitor:
5791 * @monitor_num: the number of the monitor on which the menu should
5794 * Informs GTK+ on which monitor a menu should be popped up.
5795 * See gdk_screen_get_monitor_geometry().
5797 * This function should be called from a #GtkMenuPositionFunc
5798 * if the menu should not appear on the same monitor as the pointer.
5799 * This information can't be reliably inferred from the coordinates
5800 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5801 * these coordinates may extend beyond the monitor boundaries or even
5802 * the screen boundaries.
5807 gtk_menu_set_monitor (GtkMenu *menu,
5810 GtkMenuPrivate *priv = menu->priv;
5812 g_return_if_fail (GTK_IS_MENU (menu));
5814 priv->monitor_num = monitor_num;
5818 * gtk_menu_get_monitor:
5821 * Retrieves the number of the monitor on which to show the menu.
5823 * Returns: the number of the monitor on which the menu should
5824 * be popped up or -1, if no monitor has been set
5829 gtk_menu_get_monitor (GtkMenu *menu)
5831 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5833 return menu->priv->monitor_num;
5837 * gtk_menu_get_for_attach_widget:
5838 * @widget: a #GtkWidget
5840 * Returns a list of the menus which are attached to this widget.
5841 * This list is owned by GTK+ and must not be modified.
5843 * Return value: (element-type GtkWidget) (transfer none): the list
5844 * of menus attached to his widget.
5849 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5853 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5855 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5861 gtk_menu_grab_notify (GtkWidget *widget,
5862 gboolean was_grabbed)
5865 GtkWidget *toplevel;
5866 GtkWindowGroup *group;
5870 menu = GTK_MENU (widget);
5871 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5874 !gtk_widget_device_is_shadowed (widget, pointer))
5877 toplevel = gtk_widget_get_toplevel (widget);
5879 if (!GTK_IS_WINDOW (toplevel))
5882 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5883 grab = gtk_window_group_get_current_device_grab (group, pointer);
5885 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5886 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5888 menu->priv->drag_scroll_started = FALSE;
5892 * gtk_menu_set_reserve_toggle_size:
5894 * @reserve_toggle_size: whether to reserve size for toggles
5896 * Sets whether the menu should reserve space for drawing toggles
5897 * or icons, regardless of their actual presence.
5902 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5903 gboolean reserve_toggle_size)
5905 GtkMenuPrivate *priv = menu->priv;
5906 gboolean no_toggle_size;
5908 g_return_if_fail (GTK_IS_MENU (menu));
5910 no_toggle_size = !reserve_toggle_size;
5912 if (priv->no_toggle_size != no_toggle_size)
5914 priv->no_toggle_size = no_toggle_size;
5916 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5921 * gtk_menu_get_reserve_toggle_size:
5924 * Returns whether the menu reserves space for toggles and
5925 * icons, regardless of their actual presence.
5927 * Returns: Whether the menu reserves toggle space
5932 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5934 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5936 return !menu->priv->no_toggle_size;