1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 * @Short_description: A menu widget
32 * A #GtkMenu is a #GtkMenuShell that implements a drop down menu
33 * consisting of a list of #GtkMenuItem objects which can be navigated
34 * and activated by the user to perform application functions.
36 * A #GtkMenu is most commonly dropped down by activating a
37 * #GtkMenuItem in a #GtkMenuBar or popped up by activating a
38 * #GtkMenuItem in another #GtkMenu.
40 * A #GtkMenu can also be popped up by activating a #GtkOptionMenu.
41 * Other composite widgets such as the #GtkNotebook can pop up a
44 * Applications can display a #GtkMenu as a popup menu by calling the
45 * gtk_menu_popup() function. The example below shows how an application
46 * can pop up a menu when the 3rd mouse button is pressed.
49 * <title>Connecting the popup signal handler.</title>
51 * /<!---->* connect our handler which will popup the menu *<!---->/
52 * g_signal_connect_swapped (window, "button_press_event",
53 * G_CALLBACK (my_popup_handler), menu);
58 * <title>Signal handler which displays a popup menu.</title>
61 * my_popup_handler (GtkWidget *widget, GdkEvent *event)
64 * GdkEventButton *event_button;
66 * g_return_val_if_fail (widget != NULL, FALSE);
67 * g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
68 * g_return_val_if_fail (event != NULL, FALSE);
70 * /<!---->* The "widget" is the menu that was supplied when
71 * * g_signal_connect_swapped() was called.
73 * menu = GTK_MENU (widget);
75 * if (event->type == GDK_BUTTON_PRESS)
77 * event_button = (GdkEventButton *) event;
78 * if (event_button->button == 3)
80 * gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
81 * event_button->button, event_button->time);
96 #include <gobject/gvaluecollector.h>
98 #include "gtkaccellabel.h"
99 #include "gtkaccelmap.h"
100 #include "gtkbindings.h"
101 #include "gtkcheckmenuitem.h"
103 #include "gtkmarshalers.h"
104 #include "gtkmenuprivate.h"
105 #include "gtkmenuitemprivate.h"
106 #include "gtkmenushellprivate.h"
107 #include "gtkwindow.h"
109 #include "gtkscrollbar.h"
110 #include "gtksettings.h"
111 #include "gtkprivate.h"
112 #include "gtkwidgetprivate.h"
114 #include "gtktypebuiltins.h"
116 #undef GTK_DISABLE_DEPRECATED
117 #include "gtktearoffmenuitem.h"
120 #include "a11y/gtkmenuaccessible.h"
122 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
123 * extends below the submenu
126 #define MENU_SCROLL_STEP1 8
127 #define MENU_SCROLL_STEP2 15
128 #define MENU_SCROLL_FAST_ZONE 8
129 #define MENU_SCROLL_TIMEOUT1 50
130 #define MENU_SCROLL_TIMEOUT2 20
132 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
133 #define ATTACHED_MENUS "gtk-attached-menus"
135 typedef struct _GtkMenuAttachData GtkMenuAttachData;
136 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
138 struct _GtkMenuAttachData
140 GtkWidget *attach_widget;
141 GtkMenuDetachFunc detacher;
144 struct _OldGtkMenuPrivate
148 gboolean initially_pushed_in;
150 GDestroyNotify position_func_data_destroy;
152 /* info used for the table */
155 gint requested_height;
159 /* Cached layout information */
168 GtkStateFlags lower_arrow_state;
169 GtkStateFlags upper_arrow_state;
171 /* navigation region */
174 int navigation_width;
175 int navigation_height;
177 guint have_layout : 1;
178 guint seen_item_enter : 1;
179 guint have_position : 1;
180 guint ignore_button_release : 1;
181 guint no_toggle_size : 1;
184 struct _GtkMenuPopdownData
196 gint effective_left_attach;
197 gint effective_right_attach;
198 gint effective_top_attach;
199 gint effective_bottom_attach;
216 PROP_RESERVE_TOGGLE_SIZE
221 CHILD_PROP_LEFT_ATTACH,
222 CHILD_PROP_RIGHT_ATTACH,
223 CHILD_PROP_TOP_ATTACH,
224 CHILD_PROP_BOTTOM_ATTACH
227 static void gtk_menu_set_property (GObject *object,
231 static void gtk_menu_get_property (GObject *object,
235 static void gtk_menu_set_child_property(GtkContainer *container,
240 static void gtk_menu_get_child_property(GtkContainer *container,
245 static void gtk_menu_destroy (GtkWidget *widget);
246 static void gtk_menu_realize (GtkWidget *widget);
247 static void gtk_menu_unrealize (GtkWidget *widget);
248 static void gtk_menu_size_allocate (GtkWidget *widget,
249 GtkAllocation *allocation);
250 static void gtk_menu_show (GtkWidget *widget);
251 static gboolean gtk_menu_draw (GtkWidget *widget,
253 static gboolean gtk_menu_key_press (GtkWidget *widget,
255 static gboolean gtk_menu_scroll (GtkWidget *widget,
256 GdkEventScroll *event);
257 static gboolean gtk_menu_button_press (GtkWidget *widget,
258 GdkEventButton *event);
259 static gboolean gtk_menu_button_release (GtkWidget *widget,
260 GdkEventButton *event);
261 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
262 GdkEventMotion *event);
263 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
264 GdkEventCrossing *event);
265 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
266 GdkEventCrossing *event);
267 static void gtk_menu_scroll_to (GtkMenu *menu,
269 static void gtk_menu_grab_notify (GtkWidget *widget,
270 gboolean was_grabbed);
272 static void gtk_menu_stop_scrolling (GtkMenu *menu);
273 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
274 static gboolean gtk_menu_scroll_timeout (gpointer data);
275 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
276 static void gtk_menu_start_scrolling (GtkMenu *menu);
278 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
279 GtkWidget *menu_item);
280 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
281 GtkWidget *menu_item);
282 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
285 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
287 static void gtk_menu_handle_scrolling (GtkMenu *menu,
292 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
294 static void gtk_menu_style_updated (GtkWidget *widget);
295 static gboolean gtk_menu_focus (GtkWidget *widget,
296 GtkDirectionType direction);
297 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
298 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
299 GtkMenuDirectionType direction);
300 static void gtk_menu_real_move_scroll (GtkMenu *menu,
303 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
304 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
305 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
308 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
309 GtkMenuItem *menu_item,
310 GdkEventCrossing *event);
312 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
313 static void gtk_menu_show_all (GtkWidget *widget);
314 static void gtk_menu_position (GtkMenu *menu,
315 gboolean set_scroll_offset);
316 static void gtk_menu_reparent (GtkMenu *menu,
317 GtkWidget *new_parent,
319 static void gtk_menu_remove (GtkContainer *menu,
322 static void gtk_menu_update_title (GtkMenu *menu);
324 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
325 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
327 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
329 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
330 gboolean group_changed);
332 static void gtk_menu_get_preferred_width (GtkWidget *widget,
335 static void gtk_menu_get_preferred_height (GtkWidget *widget,
338 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
344 static const gchar attach_data_key[] = "gtk-menu-attach-data";
346 static guint menu_signals[LAST_SIGNAL] = { 0 };
348 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
351 menu_queue_resize (GtkMenu *menu)
353 GtkMenuPrivate *priv = menu->priv;
355 priv->have_layout = FALSE;
356 gtk_widget_queue_resize (GTK_WIDGET (menu));
360 attach_info_free (AttachInfo *info)
362 g_slice_free (AttachInfo, info);
366 get_attach_info (GtkWidget *child)
368 GObject *object = G_OBJECT (child);
369 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
373 ai = g_slice_new0 (AttachInfo);
374 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
375 (GDestroyNotify) attach_info_free);
382 is_grid_attached (AttachInfo *ai)
384 return (ai->left_attach >= 0 &&
385 ai->right_attach >= 0 &&
386 ai->top_attach >= 0 &&
387 ai->bottom_attach >= 0);
391 menu_ensure_layout (GtkMenu *menu)
393 GtkMenuPrivate *priv = menu->priv;
395 if (!priv->have_layout)
397 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
401 gint max_right_attach;
402 gint max_bottom_attach;
404 /* Find extents of gridded portion
406 max_right_attach = 1;
407 max_bottom_attach = 0;
409 for (l = menu_shell->priv->children; l; l = l->next)
411 GtkWidget *child = l->data;
412 AttachInfo *ai = get_attach_info (child);
414 if (is_grid_attached (ai))
416 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
417 max_right_attach = MAX (max_right_attach, ai->right_attach);
421 /* Find empty rows */
422 row_occupied = g_malloc0 (max_bottom_attach);
424 for (l = menu_shell->priv->children; l; l = l->next)
426 GtkWidget *child = l->data;
427 AttachInfo *ai = get_attach_info (child);
429 if (is_grid_attached (ai))
433 for (i = ai->top_attach; i < ai->bottom_attach; i++)
434 row_occupied[i] = TRUE;
438 /* Lay non-grid-items out in those rows
441 for (l = menu_shell->priv->children; l; l = l->next)
443 GtkWidget *child = l->data;
444 AttachInfo *ai = get_attach_info (child);
446 if (!is_grid_attached (ai))
448 while (current_row < max_bottom_attach && row_occupied[current_row])
451 ai->effective_left_attach = 0;
452 ai->effective_right_attach = max_right_attach;
453 ai->effective_top_attach = current_row;
454 ai->effective_bottom_attach = current_row + 1;
460 ai->effective_left_attach = ai->left_attach;
461 ai->effective_right_attach = ai->right_attach;
462 ai->effective_top_attach = ai->top_attach;
463 ai->effective_bottom_attach = ai->bottom_attach;
467 g_free (row_occupied);
469 priv->n_rows = MAX (current_row, max_bottom_attach);
470 priv->n_columns = max_right_attach;
471 priv->have_layout = TRUE;
477 gtk_menu_get_n_columns (GtkMenu *menu)
479 GtkMenuPrivate *priv = menu->priv;
481 menu_ensure_layout (menu);
483 return priv->n_columns;
487 gtk_menu_get_n_rows (GtkMenu *menu)
489 GtkMenuPrivate *priv = menu->priv;
491 menu_ensure_layout (menu);
497 get_effective_child_attach (GtkWidget *child,
503 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
506 menu_ensure_layout (menu);
508 ai = get_attach_info (child);
511 *l = ai->effective_left_attach;
513 *r = ai->effective_right_attach;
515 *t = ai->effective_top_attach;
517 *b = ai->effective_bottom_attach;
522 gtk_menu_class_init (GtkMenuClass *class)
524 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
525 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
526 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
527 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
528 GtkBindingSet *binding_set;
530 gobject_class->set_property = gtk_menu_set_property;
531 gobject_class->get_property = gtk_menu_get_property;
533 widget_class->destroy = gtk_menu_destroy;
534 widget_class->realize = gtk_menu_realize;
535 widget_class->unrealize = gtk_menu_unrealize;
536 widget_class->size_allocate = gtk_menu_size_allocate;
537 widget_class->show = gtk_menu_show;
538 widget_class->draw = gtk_menu_draw;
539 widget_class->scroll_event = gtk_menu_scroll;
540 widget_class->key_press_event = gtk_menu_key_press;
541 widget_class->button_press_event = gtk_menu_button_press;
542 widget_class->button_release_event = gtk_menu_button_release;
543 widget_class->motion_notify_event = gtk_menu_motion_notify;
544 widget_class->show_all = gtk_menu_show_all;
545 widget_class->enter_notify_event = gtk_menu_enter_notify;
546 widget_class->leave_notify_event = gtk_menu_leave_notify;
547 widget_class->style_updated = gtk_menu_style_updated;
548 widget_class->focus = gtk_menu_focus;
549 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
550 widget_class->grab_notify = gtk_menu_grab_notify;
551 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
552 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
553 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
555 container_class->remove = gtk_menu_remove;
556 container_class->get_child_property = gtk_menu_get_child_property;
557 container_class->set_child_property = gtk_menu_set_child_property;
559 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
560 menu_shell_class->deactivate = gtk_menu_deactivate;
561 menu_shell_class->select_item = gtk_menu_select_item;
562 menu_shell_class->insert = gtk_menu_real_insert;
563 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
564 menu_shell_class->move_current = gtk_menu_move_current;
567 * GtkMenu::move-scroll:
569 * @scroll_type: a #GtkScrollType
571 menu_signals[MOVE_SCROLL] =
572 g_signal_new_class_handler (I_("move-scroll"),
573 G_OBJECT_CLASS_TYPE (gobject_class),
574 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
575 G_CALLBACK (gtk_menu_real_move_scroll),
577 _gtk_marshal_VOID__ENUM,
579 GTK_TYPE_SCROLL_TYPE);
584 * The index of the currently selected menu item, or -1 if no
585 * menu item is selected.
589 g_object_class_install_property (gobject_class,
591 g_param_spec_int ("active",
593 P_("The currently selected menu item"),
595 GTK_PARAM_READWRITE));
598 * GtkMenu:accel-group:
600 * The accel group holding accelerators for the menu.
604 g_object_class_install_property (gobject_class,
606 g_param_spec_object ("accel-group",
608 P_("The accel group holding accelerators for the menu"),
609 GTK_TYPE_ACCEL_GROUP,
610 GTK_PARAM_READWRITE));
613 * GtkMenu:accel-path:
615 * An accel path used to conveniently construct accel paths of child items.
619 g_object_class_install_property (gobject_class,
621 g_param_spec_string ("accel-path",
623 P_("An accel path used to conveniently construct accel paths of child items"),
625 GTK_PARAM_READWRITE));
628 * GtkMenu:attach-widget:
630 * The widget the menu is attached to. Setting this property attaches
631 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
632 * use gtk_menu_attach_to_widget() directly.
636 g_object_class_install_property (gobject_class,
638 g_param_spec_object ("attach-widget",
640 P_("The widget the menu is attached to"),
642 GTK_PARAM_READWRITE));
644 g_object_class_install_property (gobject_class,
646 g_param_spec_string ("tearoff-title",
648 P_("A title that may be displayed by the window manager when this menu is torn-off"),
650 GTK_PARAM_READWRITE));
653 * GtkMenu:tearoff-state:
655 * A boolean that indicates whether the menu is torn-off.
659 g_object_class_install_property (gobject_class,
661 g_param_spec_boolean ("tearoff-state",
663 P_("A boolean that indicates whether the menu is torn-off"),
665 GTK_PARAM_READWRITE));
670 * The monitor the menu will be popped up on.
674 g_object_class_install_property (gobject_class,
676 g_param_spec_int ("monitor",
678 P_("The monitor the menu will be popped up on"),
680 GTK_PARAM_READWRITE));
682 gtk_widget_class_install_style_property (widget_class,
683 g_param_spec_int ("vertical-padding",
684 P_("Vertical Padding"),
685 P_("Extra space at the top and bottom of the menu"),
689 GTK_PARAM_READABLE));
692 * GtkMenu:reserve-toggle-size:
694 * A boolean that indicates whether the menu reserves space for
695 * toggles and icons, regardless of their actual presence.
697 * This property should only be changed from its default value
698 * for special-purposes such as tabular menus. Regular menus that
699 * are connected to a menu bar or context menus should reserve
700 * toggle space for consistency.
704 g_object_class_install_property (gobject_class,
705 PROP_RESERVE_TOGGLE_SIZE,
706 g_param_spec_boolean ("reserve-toggle-size",
707 P_("Reserve Toggle Size"),
708 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
710 GTK_PARAM_READWRITE));
712 gtk_widget_class_install_style_property (widget_class,
713 g_param_spec_int ("horizontal-padding",
714 P_("Horizontal Padding"),
715 P_("Extra space at the left and right edges of the menu"),
719 GTK_PARAM_READABLE));
721 gtk_widget_class_install_style_property (widget_class,
722 g_param_spec_int ("vertical-offset",
723 P_("Vertical Offset"),
724 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
728 GTK_PARAM_READABLE));
730 gtk_widget_class_install_style_property (widget_class,
731 g_param_spec_int ("horizontal-offset",
732 P_("Horizontal Offset"),
733 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
737 GTK_PARAM_READABLE));
739 gtk_widget_class_install_style_property (widget_class,
740 g_param_spec_boolean ("double-arrows",
742 P_("When scrolling, always show both arrows."),
744 GTK_PARAM_READABLE));
747 * GtkMenu:arrow-placement:
749 * Indicates where scroll arrows should be placed.
753 gtk_widget_class_install_style_property (widget_class,
754 g_param_spec_enum ("arrow-placement",
755 P_("Arrow Placement"),
756 P_("Indicates where scroll arrows should be placed"),
757 GTK_TYPE_ARROW_PLACEMENT,
759 GTK_PARAM_READABLE));
761 gtk_container_class_install_child_property (container_class,
762 CHILD_PROP_LEFT_ATTACH,
763 g_param_spec_int ("left-attach",
765 P_("The column number to attach the left side of the child to"),
767 GTK_PARAM_READWRITE));
769 gtk_container_class_install_child_property (container_class,
770 CHILD_PROP_RIGHT_ATTACH,
771 g_param_spec_int ("right-attach",
773 P_("The column number to attach the right side of the child to"),
775 GTK_PARAM_READWRITE));
777 gtk_container_class_install_child_property (container_class,
778 CHILD_PROP_TOP_ATTACH,
779 g_param_spec_int ("top-attach",
781 P_("The row number to attach the top of the child to"),
783 GTK_PARAM_READWRITE));
785 gtk_container_class_install_child_property (container_class,
786 CHILD_PROP_BOTTOM_ATTACH,
787 g_param_spec_int ("bottom-attach",
789 P_("The row number to attach the bottom of the child to"),
791 GTK_PARAM_READWRITE));
794 * GtkMenu:arrow-scaling
796 * Arbitrary constant to scale down the size of the scroll arrow.
800 gtk_widget_class_install_style_property (widget_class,
801 g_param_spec_float ("arrow-scaling",
803 P_("Arbitrary constant to scale down the size of the scroll arrow"),
805 GTK_PARAM_READABLE));
807 binding_set = gtk_binding_set_by_class (class);
808 gtk_binding_entry_add_signal (binding_set,
810 I_("move-current"), 1,
811 GTK_TYPE_MENU_DIRECTION_TYPE,
813 gtk_binding_entry_add_signal (binding_set,
816 GTK_TYPE_MENU_DIRECTION_TYPE,
818 gtk_binding_entry_add_signal (binding_set,
821 GTK_TYPE_MENU_DIRECTION_TYPE,
823 gtk_binding_entry_add_signal (binding_set,
826 GTK_TYPE_MENU_DIRECTION_TYPE,
828 gtk_binding_entry_add_signal (binding_set,
831 GTK_TYPE_MENU_DIRECTION_TYPE,
832 GTK_MENU_DIR_PARENT);
833 gtk_binding_entry_add_signal (binding_set,
836 GTK_TYPE_MENU_DIRECTION_TYPE,
837 GTK_MENU_DIR_PARENT);
838 gtk_binding_entry_add_signal (binding_set,
841 GTK_TYPE_MENU_DIRECTION_TYPE,
843 gtk_binding_entry_add_signal (binding_set,
846 GTK_TYPE_MENU_DIRECTION_TYPE,
848 gtk_binding_entry_add_signal (binding_set,
851 GTK_TYPE_SCROLL_TYPE,
853 gtk_binding_entry_add_signal (binding_set,
856 GTK_TYPE_SCROLL_TYPE,
858 gtk_binding_entry_add_signal (binding_set,
861 GTK_TYPE_SCROLL_TYPE,
863 gtk_binding_entry_add_signal (binding_set,
866 GTK_TYPE_SCROLL_TYPE,
868 gtk_binding_entry_add_signal (binding_set,
871 GTK_TYPE_SCROLL_TYPE,
873 gtk_binding_entry_add_signal (binding_set,
874 GDK_KEY_KP_Page_Up, 0,
876 GTK_TYPE_SCROLL_TYPE,
878 gtk_binding_entry_add_signal (binding_set,
879 GDK_KEY_Page_Down, 0,
881 GTK_TYPE_SCROLL_TYPE,
882 GTK_SCROLL_PAGE_DOWN);
883 gtk_binding_entry_add_signal (binding_set,
884 GDK_KEY_KP_Page_Down, 0,
886 GTK_TYPE_SCROLL_TYPE,
887 GTK_SCROLL_PAGE_DOWN);
889 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
891 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
896 gtk_menu_set_property (GObject *object,
901 GtkMenu *menu = GTK_MENU (object);
906 gtk_menu_set_active (menu, g_value_get_int (value));
908 case PROP_ACCEL_GROUP:
909 gtk_menu_set_accel_group (menu, g_value_get_object (value));
911 case PROP_ACCEL_PATH:
912 gtk_menu_set_accel_path (menu, g_value_get_string (value));
914 case PROP_ATTACH_WIDGET:
918 widget = gtk_menu_get_attach_widget (menu);
920 gtk_menu_detach (menu);
922 widget = (GtkWidget*) g_value_get_object (value);
924 gtk_menu_attach_to_widget (menu, widget, NULL);
927 case PROP_TEAROFF_STATE:
928 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
930 case PROP_TEAROFF_TITLE:
931 gtk_menu_set_title (menu, g_value_get_string (value));
934 gtk_menu_set_monitor (menu, g_value_get_int (value));
936 case PROP_RESERVE_TOGGLE_SIZE:
937 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
940 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
946 gtk_menu_get_property (GObject *object,
951 GtkMenu *menu = GTK_MENU (object);
956 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
958 case PROP_ACCEL_GROUP:
959 g_value_set_object (value, gtk_menu_get_accel_group (menu));
961 case PROP_ACCEL_PATH:
962 g_value_set_string (value, gtk_menu_get_accel_path (menu));
964 case PROP_ATTACH_WIDGET:
965 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
967 case PROP_TEAROFF_STATE:
968 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
970 case PROP_TEAROFF_TITLE:
971 g_value_set_string (value, gtk_menu_get_title (menu));
974 g_value_set_int (value, gtk_menu_get_monitor (menu));
976 case PROP_RESERVE_TOGGLE_SIZE:
977 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
980 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
986 gtk_menu_set_child_property (GtkContainer *container,
992 GtkMenu *menu = GTK_MENU (container);
993 AttachInfo *ai = get_attach_info (child);
997 case CHILD_PROP_LEFT_ATTACH:
998 ai->left_attach = g_value_get_int (value);
1000 case CHILD_PROP_RIGHT_ATTACH:
1001 ai->right_attach = g_value_get_int (value);
1003 case CHILD_PROP_TOP_ATTACH:
1004 ai->top_attach = g_value_get_int (value);
1006 case CHILD_PROP_BOTTOM_ATTACH:
1007 ai->bottom_attach = g_value_get_int (value);
1011 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1015 menu_queue_resize (menu);
1019 gtk_menu_get_child_property (GtkContainer *container,
1025 AttachInfo *ai = get_attach_info (child);
1027 switch (property_id)
1029 case CHILD_PROP_LEFT_ATTACH:
1030 g_value_set_int (value, ai->left_attach);
1032 case CHILD_PROP_RIGHT_ATTACH:
1033 g_value_set_int (value, ai->right_attach);
1035 case CHILD_PROP_TOP_ATTACH:
1036 g_value_set_int (value, ai->top_attach);
1038 case CHILD_PROP_BOTTOM_ATTACH:
1039 g_value_set_int (value, ai->bottom_attach);
1043 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1049 gtk_menu_window_event (GtkWidget *window,
1053 gboolean handled = FALSE;
1055 g_object_ref (window);
1056 g_object_ref (menu);
1058 switch (event->type)
1061 case GDK_KEY_RELEASE:
1062 handled = gtk_widget_event (menu, event);
1068 g_object_unref (window);
1069 g_object_unref (menu);
1075 gtk_menu_init (GtkMenu *menu)
1077 GtkMenuPrivate *priv;
1078 GtkStyleContext *context;
1080 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1084 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1085 "type", GTK_WINDOW_POPUP,
1088 "signal::event", gtk_menu_window_event, menu,
1089 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1091 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1092 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1094 /* Refloat the menu, so that reference counting for the menu isn't
1095 * affected by it being a child of the toplevel
1097 g_object_force_floating (G_OBJECT (menu));
1098 priv->needs_destruction_ref = TRUE;
1100 priv->monitor_num = -1;
1102 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1103 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1107 gtk_menu_destroy (GtkWidget *widget)
1109 GtkMenu *menu = GTK_MENU (widget);
1110 GtkMenuPrivate *priv = menu->priv;
1111 GtkMenuAttachData *data;
1113 gtk_menu_remove_scroll_timeout (menu);
1115 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1117 gtk_menu_detach (menu);
1119 gtk_menu_stop_navigating_submenu (menu);
1121 if (priv->old_active_menu_item)
1122 g_clear_object (&priv->old_active_menu_item);
1124 /* Add back the reference count for being a child */
1125 if (priv->needs_destruction_ref)
1127 priv->needs_destruction_ref = FALSE;
1128 g_object_ref (widget);
1131 if (priv->accel_group)
1132 g_clear_object (&priv->accel_group);
1135 gtk_widget_destroy (priv->toplevel);
1137 if (priv->tearoff_window)
1138 gtk_widget_destroy (priv->tearoff_window);
1142 g_free (priv->heights);
1143 priv->heights = NULL;
1148 g_free (priv->title);
1152 if (priv->position_func_data_destroy)
1154 priv->position_func_data_destroy (priv->position_func_data);
1155 priv->position_func_data = NULL;
1156 priv->position_func_data_destroy = NULL;
1159 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1163 menu_change_screen (GtkMenu *menu,
1164 GdkScreen *new_screen)
1166 GtkMenuPrivate *priv = menu->priv;
1168 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1170 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1176 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1177 gtk_menu_position (menu, TRUE);
1180 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1181 priv->monitor_num = -1;
1185 attach_widget_screen_changed (GtkWidget *attach_widget,
1186 GdkScreen *previous_screen,
1189 if (gtk_widget_has_screen (attach_widget) &&
1190 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1191 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1195 * gtk_menu_attach_to_widget:
1197 * @attach_widget: the #GtkWidget that the menu will be attached to
1198 * @detacher: (scope async)(allow-none): the user supplied callback function
1199 * that will be called when the menu calls gtk_menu_detach()
1201 * Attaches the menu to the widget and provides a callback function
1202 * that will be invoked when the menu calls gtk_menu_detach() during
1206 gtk_menu_attach_to_widget (GtkMenu *menu,
1207 GtkWidget *attach_widget,
1208 GtkMenuDetachFunc detacher)
1210 GtkMenuAttachData *data;
1213 g_return_if_fail (GTK_IS_MENU (menu));
1214 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1216 /* keep this function in sync with gtk_widget_set_parent() */
1217 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1220 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1221 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1225 g_object_ref_sink (menu);
1227 data = g_slice_new (GtkMenuAttachData);
1228 data->attach_widget = attach_widget;
1230 g_signal_connect (attach_widget, "screen-changed",
1231 G_CALLBACK (attach_widget_screen_changed), menu);
1232 attach_widget_screen_changed (attach_widget, NULL, menu);
1234 data->detacher = detacher;
1235 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1236 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1237 if (!g_list_find (list, menu))
1238 list = g_list_prepend (list, menu);
1240 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1241 (GDestroyNotify) g_list_free);
1243 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1244 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1246 /* we don't need to set the style here, since
1247 * we are a toplevel widget.
1250 /* Fallback title for menu comes from attach widget */
1251 gtk_menu_update_title (menu);
1253 g_object_notify (G_OBJECT (menu), "attach-widget");
1257 * gtk_menu_get_attach_widget:
1260 * Returns the #GtkWidget that the menu is attached to.
1262 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1265 gtk_menu_get_attach_widget (GtkMenu *menu)
1267 GtkMenuAttachData *data;
1269 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1271 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1273 return data->attach_widget;
1281 * Detaches the menu from the widget to which it had been attached.
1282 * This function will call the callback function, @detacher, provided
1283 * when the gtk_menu_attach_to_widget() function was called.
1286 gtk_menu_detach (GtkMenu *menu)
1288 GtkMenuAttachData *data;
1291 g_return_if_fail (GTK_IS_MENU (menu));
1293 /* keep this function in sync with gtk_widget_unparent() */
1294 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1297 g_warning ("gtk_menu_detach(): menu is not attached");
1300 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1302 g_signal_handlers_disconnect_by_func (data->attach_widget,
1303 (gpointer) attach_widget_screen_changed,
1307 data->detacher (data->attach_widget, menu);
1308 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1309 list = g_list_remove (list, menu);
1311 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1312 (GDestroyNotify) g_list_free);
1314 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1316 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1317 gtk_widget_unrealize (GTK_WIDGET (menu));
1319 g_slice_free (GtkMenuAttachData, data);
1321 /* Fallback title for menu comes from attach widget */
1322 gtk_menu_update_title (menu);
1324 g_object_unref (menu);
1328 gtk_menu_remove (GtkContainer *container,
1331 GtkMenu *menu = GTK_MENU (container);
1332 GtkMenuPrivate *priv = menu->priv;
1334 /* Clear out old_active_menu_item if it matches the item we are removing */
1335 if (priv->old_active_menu_item == widget)
1336 g_clear_object (&priv->old_active_menu_item);
1338 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1340 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1342 menu_queue_resize (menu);
1348 * Creates a new #GtkMenu
1350 * Returns: a new #GtkMenu
1355 return g_object_new (GTK_TYPE_MENU, NULL);
1359 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1363 GtkMenu *menu = GTK_MENU (menu_shell);
1364 GtkMenuPrivate *priv = menu->priv;
1365 AttachInfo *ai = get_attach_info (child);
1367 ai->left_attach = -1;
1368 ai->right_attach = -1;
1369 ai->top_attach = -1;
1370 ai->bottom_attach = -1;
1372 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1373 gtk_widget_set_parent_window (child, priv->bin_window);
1375 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1377 menu_queue_resize (menu);
1381 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1383 GtkMenuPrivate *priv = menu->priv;
1389 cairo_surface_t *surface;
1390 cairo_pattern_t *pattern;
1393 priv->tearoff_active = FALSE;
1394 priv->saved_scroll_offset = priv->scroll_offset;
1396 window = gtk_widget_get_window (priv->tearoff_window);
1397 width = gdk_window_get_width (window);
1398 height = gdk_window_get_height (window);
1400 surface = gdk_window_create_similar_surface (window,
1401 CAIRO_CONTENT_COLOR,
1405 cr = cairo_create (surface);
1406 gdk_cairo_set_source_window (cr, window, 0, 0);
1410 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1412 pattern = cairo_pattern_create_for_surface (surface);
1413 gdk_window_set_background_pattern (window, pattern);
1415 cairo_pattern_destroy (pattern);
1416 cairo_surface_destroy (surface);
1421 popup_grab_on_window (GdkWindow *window,
1422 GdkDevice *keyboard,
1424 guint32 activate_time)
1427 gdk_device_grab (keyboard, window,
1428 GDK_OWNERSHIP_WINDOW, TRUE,
1429 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1430 NULL, activate_time) != GDK_GRAB_SUCCESS)
1434 gdk_device_grab (pointer, window,
1435 GDK_OWNERSHIP_WINDOW, TRUE,
1436 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1437 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1438 GDK_POINTER_MOTION_MASK,
1439 NULL, activate_time) != GDK_GRAB_SUCCESS)
1442 gdk_device_ungrab (keyboard, activate_time);
1451 * gtk_menu_popup_for_device:
1453 * @device: (allow-none): a #GdkDevice
1454 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1455 * menu item, or %NULL
1456 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1457 * the popup, or %NULL
1458 * @func: (allow-none): a user supplied function used to position the menu,
1460 * @data: (allow-none): user supplied data to be passed to @func
1461 * @destroy: (allow-none): destroy notify for @data
1462 * @button: the mouse button which was pressed to initiate the event
1463 * @activate_time: the time at which the activation event occurred
1465 * Displays a menu and makes it available for selection.
1467 * Applications can use this function to display context-sensitive menus,
1468 * and will typically supply %NULL for the @parent_menu_shell,
1469 * @parent_menu_item, @func, @data and @destroy parameters. The default
1470 * menu positioning function will position the menu at the current position
1471 * of @device (or its corresponding pointer).
1473 * The @button parameter should be the mouse button pressed to initiate
1474 * the menu popup. If the menu popup was initiated by something other than
1475 * a mouse button press, such as a mouse button release or a keypress,
1476 * @button should be 0.
1478 * The @activate_time parameter is used to conflict-resolve initiation of
1479 * concurrent requests for mouse/keyboard grab requests. To function
1480 * properly, this needs to be the time stamp of the user event (such as
1481 * a mouse click or key press) that caused the initiation of the popup.
1482 * Only if no such event is available, gtk_get_current_event_time() can
1488 gtk_menu_popup_for_device (GtkMenu *menu,
1490 GtkWidget *parent_menu_shell,
1491 GtkWidget *parent_menu_item,
1492 GtkMenuPositionFunc func,
1494 GDestroyNotify destroy,
1496 guint32 activate_time)
1498 GtkMenuPrivate *priv = menu->priv;
1500 GtkWidget *xgrab_shell;
1502 GdkEvent *current_event;
1503 GtkMenuShell *menu_shell;
1504 gboolean grab_keyboard;
1505 GtkWidget *parent_toplevel;
1506 GdkDevice *keyboard, *pointer;
1508 g_return_if_fail (GTK_IS_MENU (menu));
1509 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1512 device = gtk_get_current_event_device ();
1516 GdkDisplay *display;
1517 GdkDeviceManager *device_manager;
1520 display = gtk_widget_get_display (GTK_WIDGET (menu));
1521 device_manager = gdk_display_get_device_manager (display);
1522 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1524 device = devices->data;
1526 g_list_free (devices);
1529 widget = GTK_WIDGET (menu);
1530 menu_shell = GTK_MENU_SHELL (menu);
1532 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1535 pointer = gdk_device_get_associated_device (device);
1540 keyboard = gdk_device_get_associated_device (device);
1543 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1545 priv->seen_item_enter = FALSE;
1547 /* Find the last viewable ancestor, and make an X grab on it
1549 parent = GTK_WIDGET (menu);
1553 gboolean viewable = TRUE;
1554 GtkWidget *tmp = parent;
1558 if (!gtk_widget_get_mapped (tmp))
1563 tmp = gtk_widget_get_parent (tmp);
1567 xgrab_shell = parent;
1569 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1572 /* We want to receive events generated when we map the menu;
1573 * unfortunately, since there is probably already an implicit
1574 * grab in place from the button that the user used to pop up
1575 * the menu, we won't receive then -- in particular, the EnterNotify
1576 * when the menu pops up under the pointer.
1578 * If we are grabbing on a parent menu shell, no problem; just grab
1579 * on that menu shell first before popping up the window with
1580 * owner_events = TRUE.
1582 * When grabbing on the menu itself, things get more convoluted --
1583 * we do an explicit grab on a specially created window with
1584 * owner_events = TRUE, which we override further down with a
1585 * grab on the menu. (We can't grab on the menu until it is mapped;
1586 * we probably could just leave the grab on the other window,
1587 * with a little reorganization of the code in gtkmenu*).
1589 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1590 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1595 if (xgrab_shell && xgrab_shell != widget)
1597 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1599 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1600 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1605 GdkWindow *transfer_window;
1607 xgrab_shell = widget;
1608 transfer_window = menu_grab_transfer_window_get (menu);
1609 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1611 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1612 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1616 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1618 /* We failed to make our pointer/keyboard grab.
1619 * Rather than leaving the user with a stuck up window,
1620 * we just abort here. Presumably the user will try again.
1622 menu_shell->priv->parent_menu_shell = NULL;
1623 menu_grab_transfer_window_destroy (menu);
1627 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1628 menu_shell->priv->active = TRUE;
1629 menu_shell->priv->button = button;
1631 /* If we are popping up the menu from something other than, a button
1632 * press then, as a heuristic, we ignore enter events for the menu
1633 * until we get a MOTION_NOTIFY.
1636 current_event = gtk_get_current_event ();
1639 if ((current_event->type != GDK_BUTTON_PRESS) &&
1640 (current_event->type != GDK_ENTER_NOTIFY))
1641 menu_shell->priv->ignore_enter = TRUE;
1643 gdk_event_free (current_event);
1646 menu_shell->priv->ignore_enter = TRUE;
1650 gtk_menu_tearoff_bg_copy (menu);
1652 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1655 parent_toplevel = NULL;
1656 if (parent_menu_shell)
1657 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1658 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1660 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1662 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1665 /* Set transient for to get the right window group and parent */
1666 if (GTK_IS_WINDOW (parent_toplevel))
1667 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1668 GTK_WINDOW (parent_toplevel));
1670 priv->parent_menu_item = parent_menu_item;
1671 priv->position_func = func;
1672 priv->position_func_data = data;
1673 priv->position_func_data_destroy = destroy;
1674 menu_shell->priv->activate_time = activate_time;
1676 /* We need to show the menu here rather in the init function
1677 * because code expects to be able to tell if the menu is onscreen
1678 * by looking at gtk_widget_get_visible (menu)
1680 gtk_widget_show (GTK_WIDGET (menu));
1682 /* Position the menu, possibly changing the size request
1684 gtk_menu_position (menu, TRUE);
1686 /* Compute the size of the toplevel and realize it so we
1687 * can scroll correctly.
1689 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1691 GtkRequisition tmp_request;
1692 GtkAllocation tmp_allocation = { 0, };
1694 /* Instead of trusting the menu position function to queue a
1695 * resize when the menu goes out of bounds, invalidate the cached
1698 gtk_widget_queue_resize (GTK_WIDGET (menu));
1699 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1701 tmp_allocation.width = tmp_request.width;
1702 tmp_allocation.height = tmp_request.height;
1704 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1706 gtk_widget_realize (priv->toplevel);
1709 gtk_menu_scroll_to (menu, priv->scroll_offset);
1711 /* if no item is selected, select the first one */
1712 if (!menu_shell->priv->active_menu_item)
1714 gboolean touchscreen_mode;
1716 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1717 "gtk-touchscreen-mode", &touchscreen_mode,
1720 if (touchscreen_mode)
1721 gtk_menu_shell_select_first (menu_shell, TRUE);
1724 /* Once everything is set up correctly, map the toplevel */
1725 gtk_widget_show (priv->toplevel);
1727 if (xgrab_shell == widget)
1728 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1730 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1732 if (parent_menu_shell)
1734 gboolean keyboard_mode;
1736 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1737 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1739 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1740 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1742 _gtk_menu_shell_update_mnemonics (menu_shell);
1746 * gtk_menu_popup: (skip)
1748 * @parent_menu_shell: (allow-none): the menu shell containing the
1749 * triggering menu item, or %NULL
1750 * @parent_menu_item: (allow-none): the menu item whose activation
1751 * triggered the popup, or %NULL
1752 * @func: (allow-none): a user supplied function used to position
1753 * the menu, or %NULL
1754 * @data: user supplied data to be passed to @func.
1755 * @button: the mouse button which was pressed to initiate the event.
1756 * @activate_time: the time at which the activation event occurred.
1758 * Displays a menu and makes it available for selection.
1760 * Applications can use this function to display context-sensitive
1761 * menus, and will typically supply %NULL for the @parent_menu_shell,
1762 * @parent_menu_item, @func and @data parameters. The default menu
1763 * positioning function will position the menu at the current mouse
1766 * The @button parameter should be the mouse button pressed to initiate
1767 * the menu popup. If the menu popup was initiated by something other
1768 * than a mouse button press, such as a mouse button release or a keypress,
1769 * @button should be 0.
1771 * The @activate_time parameter is used to conflict-resolve initiation
1772 * of concurrent requests for mouse/keyboard grab requests. To function
1773 * properly, this needs to be the timestamp of the user event (such as
1774 * a mouse click or key press) that caused the initiation of the popup.
1775 * Only if no such event is available, gtk_get_current_event_time() can
1779 gtk_menu_popup (GtkMenu *menu,
1780 GtkWidget *parent_menu_shell,
1781 GtkWidget *parent_menu_item,
1782 GtkMenuPositionFunc func,
1785 guint32 activate_time)
1787 g_return_if_fail (GTK_IS_MENU (menu));
1789 gtk_menu_popup_for_device (menu,
1794 button, activate_time);
1801 * Removes the menu from the screen.
1804 gtk_menu_popdown (GtkMenu *menu)
1806 GtkMenuPrivate *priv;
1807 GtkMenuShell *menu_shell;
1810 g_return_if_fail (GTK_IS_MENU (menu));
1812 menu_shell = GTK_MENU_SHELL (menu);
1815 menu_shell->priv->parent_menu_shell = NULL;
1816 menu_shell->priv->active = FALSE;
1817 menu_shell->priv->ignore_enter = FALSE;
1819 priv->have_position = FALSE;
1821 gtk_menu_stop_scrolling (menu);
1822 gtk_menu_stop_navigating_submenu (menu);
1824 if (menu_shell->priv->active_menu_item)
1826 if (priv->old_active_menu_item)
1827 g_object_unref (priv->old_active_menu_item);
1828 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1829 g_object_ref (priv->old_active_menu_item);
1832 gtk_menu_shell_deselect (menu_shell);
1834 /* The X Grab, if present, will automatically be removed
1835 * when we hide the window
1839 gtk_widget_hide (priv->toplevel);
1840 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1843 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1847 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1849 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1851 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1855 /* We popped up the menu from the tearoff, so we need to
1856 * release the grab - we aren't actually hiding the menu.
1858 if (menu_shell->priv->have_xgrab && pointer)
1860 GdkDevice *keyboard;
1862 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1863 keyboard = gdk_device_get_associated_device (pointer);
1866 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1870 /* gtk_menu_popdown is called each time a menu item is selected from
1871 * a torn off menu. Only scroll back to the saved position if the
1872 * non-tearoff menu was popped down.
1874 if (!priv->tearoff_active)
1875 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1876 priv->tearoff_active = TRUE;
1879 gtk_widget_hide (GTK_WIDGET (menu));
1881 menu_shell->priv->have_xgrab = FALSE;
1884 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1886 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1888 menu_grab_transfer_window_destroy (menu);
1892 * gtk_menu_get_active:
1895 * Returns the selected menu item from the menu. This is used by the
1898 * Returns: (transfer none): the #GtkMenuItem that was last selected
1899 * in the menu. If a selection has not yet been made, the
1900 * first menu item is selected.
1903 gtk_menu_get_active (GtkMenu *menu)
1905 GtkMenuPrivate *priv = menu->priv;
1909 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1911 if (!priv->old_active_menu_item)
1914 children = GTK_MENU_SHELL (menu)->priv->children;
1918 child = children->data;
1919 children = children->next;
1921 if (gtk_bin_get_child (GTK_BIN (child)))
1926 priv->old_active_menu_item = child;
1927 if (priv->old_active_menu_item)
1928 g_object_ref (priv->old_active_menu_item);
1931 return priv->old_active_menu_item;
1935 * gtk_menu_set_active:
1937 * @index: the index of the menu item to select. Iindex values are
1940 * Selects the specified menu item within the menu. This is used by
1941 * the #GtkOptionMenu and should not be used by anyone else.
1944 gtk_menu_set_active (GtkMenu *menu,
1947 GtkMenuPrivate *priv = menu->priv;
1951 g_return_if_fail (GTK_IS_MENU (menu));
1953 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1956 child = tmp_list->data;
1957 if (gtk_bin_get_child (GTK_BIN (child)))
1959 if (priv->old_active_menu_item)
1960 g_object_unref (priv->old_active_menu_item);
1961 priv->old_active_menu_item = child;
1962 g_object_ref (priv->old_active_menu_item);
1968 * gtk_menu_set_accel_group:
1970 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1973 * Set the #GtkAccelGroup which holds global accelerators for the
1974 * menu. This accelerator group needs to also be added to all windows
1975 * that this menu is being used in with gtk_window_add_accel_group(),
1976 * in order for those windows to support all the accelerators
1977 * contained in this group.
1980 gtk_menu_set_accel_group (GtkMenu *menu,
1981 GtkAccelGroup *accel_group)
1983 GtkMenuPrivate *priv = menu->priv;
1984 g_return_if_fail (GTK_IS_MENU (menu));
1986 if (priv->accel_group != accel_group)
1988 if (priv->accel_group)
1989 g_object_unref (priv->accel_group);
1990 priv->accel_group = accel_group;
1991 if (priv->accel_group)
1992 g_object_ref (priv->accel_group);
1993 _gtk_menu_refresh_accel_paths (menu, TRUE);
1998 * gtk_menu_get_accel_group:
2001 * Gets the #GtkAccelGroup which holds global accelerators for the
2002 * menu. See gtk_menu_set_accel_group().
2004 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
2007 gtk_menu_get_accel_group (GtkMenu *menu)
2009 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2011 return menu->priv->accel_group;
2015 gtk_menu_real_can_activate_accel (GtkWidget *widget,
2018 /* Menu items chain here to figure whether they can activate their
2019 * accelerators. Unlike ordinary widgets, menus allow accel
2020 * activation even if invisible since that's the usual case for
2021 * submenus/popup-menus. however, the state of the attach widget
2022 * affects the "activeness" of the menu.
2024 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
2027 return gtk_widget_can_activate_accel (awidget, signal_id);
2029 return gtk_widget_is_sensitive (widget);
2033 * gtk_menu_set_accel_path
2034 * @menu: a valid #GtkMenu
2035 * @accel_path: (allow-none): a valid accelerator path
2037 * Sets an accelerator path for this menu from which accelerator paths
2038 * for its immediate children, its menu items, can be constructed.
2039 * The main purpose of this function is to spare the programmer the
2040 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2041 * each menu item that should support runtime user changable accelerators.
2042 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2043 * each menu item of this menu, that contains a label describing its
2044 * purpose, automatically gets an accel path assigned.
2046 * For example, a menu containing menu items "New" and "Exit", will, after
2047 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2048 * has been called, assign its items the accel paths:
2049 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2051 * Assigning accel paths to menu items then enables the user to change
2052 * their accelerators at runtime. More details about accelerator paths
2053 * and their default setups can be found at gtk_accel_map_add_entry().
2055 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2056 * if you pass a static string, you can save some memory by interning
2057 * it first with g_intern_static_string().
2060 gtk_menu_set_accel_path (GtkMenu *menu,
2061 const gchar *accel_path)
2063 GtkMenuPrivate *priv = menu->priv;
2064 g_return_if_fail (GTK_IS_MENU (menu));
2067 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2069 /* FIXME: accel_path should be defined as const gchar* */
2070 priv->accel_path = (gchar*)g_intern_string (accel_path);
2071 if (priv->accel_path)
2072 _gtk_menu_refresh_accel_paths (menu, FALSE);
2076 * gtk_menu_get_accel_path
2077 * @menu: a valid #GtkMenu
2079 * Retrieves the accelerator path set on the menu.
2081 * Returns: the accelerator path set on the menu.
2086 gtk_menu_get_accel_path (GtkMenu *menu)
2088 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2090 return menu->priv->accel_path;
2095 gboolean group_changed;
2099 refresh_accel_paths_foreach (GtkWidget *widget,
2102 GtkMenuPrivate *priv;
2103 AccelPropagation *prop = data;
2105 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2107 priv = prop->menu->priv;
2108 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2111 prop->group_changed);
2116 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2117 gboolean group_changed)
2119 GtkMenuPrivate *priv = menu->priv;
2120 g_return_if_fail (GTK_IS_MENU (menu));
2122 if (priv->accel_path && priv->accel_group)
2124 AccelPropagation prop;
2127 prop.group_changed = group_changed;
2128 gtk_container_foreach (GTK_CONTAINER (menu),
2129 refresh_accel_paths_foreach,
2135 * gtk_menu_reposition:
2138 * Repositions the menu according to its position function.
2141 gtk_menu_reposition (GtkMenu *menu)
2143 g_return_if_fail (GTK_IS_MENU (menu));
2145 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2146 gtk_menu_position (menu, FALSE);
2150 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2155 value = gtk_adjustment_get_value (adjustment);
2156 if (menu->priv->scroll_offset != value)
2157 gtk_menu_scroll_to (menu, value);
2161 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2164 GtkMenuPrivate *priv = menu->priv;
2165 GdkGeometry geometry_hints;
2167 if (!priv->tearoff_window)
2170 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2172 GtkRequisition requisition;
2174 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2175 &requisition, NULL);
2176 width += requisition.width;
2179 geometry_hints.min_width = width;
2180 geometry_hints.max_width = width;
2182 geometry_hints.min_height = 0;
2183 geometry_hints.max_height = priv->requested_height;
2185 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2188 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2192 gtk_menu_update_title (GtkMenu *menu)
2194 GtkMenuPrivate *priv = menu->priv;
2196 if (priv->tearoff_window)
2199 GtkWidget *attach_widget;
2201 title = gtk_menu_get_title (menu);
2204 attach_widget = gtk_menu_get_attach_widget (menu);
2205 if (GTK_IS_MENU_ITEM (attach_widget))
2207 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2208 if (GTK_IS_LABEL (child))
2209 title = gtk_label_get_text (GTK_LABEL (child));
2214 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2219 gtk_menu_get_toplevel (GtkWidget *menu)
2221 GtkWidget *attach, *toplevel;
2223 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2225 if (GTK_IS_MENU_ITEM (attach))
2226 attach = gtk_widget_get_parent (attach);
2228 if (GTK_IS_MENU (attach))
2229 return gtk_menu_get_toplevel (attach);
2230 else if (GTK_IS_WIDGET (attach))
2232 toplevel = gtk_widget_get_toplevel (attach);
2233 if (gtk_widget_is_toplevel (toplevel))
2241 tearoff_window_destroyed (GtkWidget *widget,
2244 gtk_menu_set_tearoff_state (menu, FALSE);
2248 * gtk_menu_set_tearoff_state:
2250 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2252 * Changes the tearoff state of the menu. A menu is normally
2253 * displayed as drop down menu which persists as long as the menu is
2254 * active. It can also be displayed as a tearoff menu which persists
2255 * until it is closed or reattached.
2258 gtk_menu_set_tearoff_state (GtkMenu *menu,
2261 GtkMenuPrivate *priv = menu->priv;
2264 g_return_if_fail (GTK_IS_MENU (menu));
2266 if (priv->torn_off != torn_off)
2268 priv->torn_off = torn_off;
2269 priv->tearoff_active = torn_off;
2273 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2274 gtk_menu_popdown (menu);
2276 if (!priv->tearoff_window)
2278 GtkWidget *toplevel;
2280 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2281 "type", GTK_WINDOW_TOPLEVEL,
2282 "screen", gtk_widget_get_screen (priv->toplevel),
2283 "app-paintable", TRUE,
2286 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2287 GDK_WINDOW_TYPE_HINT_MENU);
2288 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2289 g_signal_connect (priv->tearoff_window, "destroy",
2290 G_CALLBACK (tearoff_window_destroyed), menu);
2291 g_signal_connect (priv->tearoff_window, "event",
2292 G_CALLBACK (gtk_menu_window_event), menu);
2294 gtk_menu_update_title (menu);
2296 gtk_widget_realize (priv->tearoff_window);
2298 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2299 if (toplevel != NULL)
2300 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2301 GTK_WINDOW (toplevel));
2303 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2304 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2305 priv->tearoff_hbox);
2307 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2308 priv->tearoff_adjustment = gtk_adjustment_new (0,
2309 0, priv->requested_height,
2313 g_object_connect (priv->tearoff_adjustment,
2314 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2316 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2318 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2319 priv->tearoff_scrollbar,
2322 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2323 gtk_widget_show (priv->tearoff_scrollbar);
2325 gtk_widget_show (priv->tearoff_hbox);
2328 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2330 /* Update menu->requisition */
2331 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2333 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2335 gtk_widget_realize (priv->tearoff_window);
2336 gtk_menu_position (menu, TRUE);
2338 gtk_widget_show (GTK_WIDGET (menu));
2339 gtk_widget_show (priv->tearoff_window);
2341 gtk_menu_scroll_to (menu, 0);
2346 gtk_widget_hide (GTK_WIDGET (menu));
2347 gtk_widget_hide (priv->tearoff_window);
2348 if (GTK_IS_CONTAINER (priv->toplevel))
2349 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2350 gtk_widget_destroy (priv->tearoff_window);
2352 priv->tearoff_window = NULL;
2353 priv->tearoff_hbox = NULL;
2354 priv->tearoff_scrollbar = NULL;
2355 priv->tearoff_adjustment = NULL;
2358 g_object_notify (G_OBJECT (menu), "tearoff-state");
2363 * gtk_menu_get_tearoff_state:
2366 * Returns whether the menu is torn off.
2367 * See gtk_menu_set_tearoff_state().
2369 * Return value: %TRUE if the menu is currently torn off.
2372 gtk_menu_get_tearoff_state (GtkMenu *menu)
2374 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2376 return menu->priv->torn_off;
2380 * gtk_menu_set_title:
2382 * @title: a string containing the title for the menu
2384 * Sets the title string for the menu.
2386 * The title is displayed when the menu is shown as a tearoff
2387 * menu. If @title is %NULL, the menu will see if it is attached
2388 * to a parent menu item, and if so it will try to use the same
2389 * text as that menu item's label.
2392 gtk_menu_set_title (GtkMenu *menu,
2395 GtkMenuPrivate *priv = menu->priv;
2398 g_return_if_fail (GTK_IS_MENU (menu));
2400 old_title = priv->title;
2401 priv->title = g_strdup (title);
2404 gtk_menu_update_title (menu);
2405 g_object_notify (G_OBJECT (menu), "tearoff-title");
2409 * gtk_menu_get_title:
2412 * Returns the title of the menu. See gtk_menu_set_title().
2414 * Return value: the title of the menu, or %NULL if the menu
2415 * has no title set on it. This string is owned by GTK+
2416 * and should not be modified or freed.
2419 gtk_menu_get_title (GtkMenu *menu)
2421 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2423 return menu->priv->title;
2427 * gtk_menu_reorder_child:
2429 * @child: the #GtkMenuItem to move
2430 * @position: the new position to place @child.
2431 * Positions are numbered from 0 to n - 1
2433 * Moves @child to a new @position in the list of @menu
2437 gtk_menu_reorder_child (GtkMenu *menu,
2441 GtkMenuShell *menu_shell;
2443 g_return_if_fail (GTK_IS_MENU (menu));
2444 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2446 menu_shell = GTK_MENU_SHELL (menu);
2448 if (g_list_find (menu_shell->priv->children, child))
2450 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2451 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2453 menu_queue_resize (menu);
2458 gtk_menu_style_updated (GtkWidget *widget)
2460 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2462 if (gtk_widget_get_realized (widget))
2464 GtkMenu *menu = GTK_MENU (widget);
2465 GtkMenuPrivate *priv = menu->priv;
2466 GtkStyleContext *context;
2468 context = gtk_widget_get_style_context (widget);
2470 gtk_style_context_set_background (context, priv->bin_window);
2471 gtk_style_context_set_background (context, priv->view_window);
2472 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2477 get_arrows_border (GtkMenu *menu,
2480 GtkMenuPrivate *priv = menu->priv;
2481 guint scroll_arrow_height;
2482 GtkArrowPlacement arrow_placement;
2484 gtk_widget_style_get (GTK_WIDGET (menu),
2485 "scroll-arrow-vlength", &scroll_arrow_height,
2486 "arrow_placement", &arrow_placement,
2489 switch (arrow_placement)
2491 case GTK_ARROWS_BOTH:
2492 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2493 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2496 case GTK_ARROWS_START:
2497 border->top = (priv->upper_arrow_visible ||
2498 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2502 case GTK_ARROWS_END:
2504 border->bottom = (priv->upper_arrow_visible ||
2505 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2509 border->left = border->right = 0;
2513 get_menu_padding (GtkWidget *widget,
2516 GtkStyleContext *context;
2517 GtkStateFlags state;
2519 context = gtk_widget_get_style_context (widget);
2520 state = gtk_widget_get_state_flags (widget);
2522 gtk_style_context_get_padding (context, state, padding);
2526 gtk_menu_realize (GtkWidget *widget)
2528 GtkMenu *menu = GTK_MENU (widget);
2529 GtkMenuPrivate *priv = menu->priv;
2530 GtkAllocation allocation;
2531 GtkStyleContext *context;
2533 GdkWindowAttr attributes;
2534 gint attributes_mask;
2538 guint vertical_padding;
2539 guint horizontal_padding;
2540 GtkBorder arrow_border, padding;
2542 g_return_if_fail (GTK_IS_MENU (widget));
2544 gtk_widget_set_realized (widget, TRUE);
2546 gtk_widget_get_allocation (widget, &allocation);
2548 attributes.window_type = GDK_WINDOW_CHILD;
2549 attributes.x = allocation.x;
2550 attributes.y = allocation.y;
2551 attributes.width = allocation.width;
2552 attributes.height = allocation.height;
2553 attributes.wclass = GDK_INPUT_OUTPUT;
2554 attributes.visual = gtk_widget_get_visual (widget);
2555 attributes.event_mask = gtk_widget_get_events (widget);
2556 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2557 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2559 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2561 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2562 &attributes, attributes_mask);
2563 gtk_widget_set_window (widget, window);
2564 gdk_window_set_user_data (window, widget);
2566 get_menu_padding (widget, &padding);
2567 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2568 context = gtk_widget_get_style_context (widget);
2570 gtk_widget_style_get (GTK_WIDGET (menu),
2571 "vertical-padding", &vertical_padding,
2572 "horizontal-padding", &horizontal_padding,
2575 gtk_widget_get_allocation (widget, &allocation);
2577 attributes.x = border_width + padding.left + horizontal_padding;
2578 attributes.y = border_width + padding.top + vertical_padding;
2579 attributes.width = allocation.width -
2580 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2581 attributes.height = allocation.height -
2582 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2584 get_arrows_border (menu, &arrow_border);
2585 attributes.y += arrow_border.top;
2586 attributes.height -= arrow_border.top;
2587 attributes.height -= arrow_border.bottom;
2589 attributes.width = MAX (1, attributes.width);
2590 attributes.height = MAX (1, attributes.height);
2592 priv->view_window = gdk_window_new (window,
2593 &attributes, attributes_mask);
2594 gdk_window_set_user_data (priv->view_window, menu);
2596 gtk_widget_get_allocation (widget, &allocation);
2600 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2601 padding.left + padding.right;
2602 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2603 padding.top + padding.bottom;
2605 attributes.width = MAX (1, attributes.width);
2606 attributes.height = MAX (1, attributes.height);
2608 priv->bin_window = gdk_window_new (priv->view_window,
2609 &attributes, attributes_mask);
2610 gdk_window_set_user_data (priv->bin_window, menu);
2612 children = GTK_MENU_SHELL (menu)->priv->children;
2615 child = children->data;
2616 children = children->next;
2618 gtk_widget_set_parent_window (child, priv->bin_window);
2621 gtk_style_context_set_background (context, priv->bin_window);
2622 gtk_style_context_set_background (context, priv->view_window);
2623 gtk_style_context_set_background (context, window);
2625 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2626 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2627 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2629 gdk_window_show (priv->bin_window);
2630 gdk_window_show (priv->view_window);
2634 gtk_menu_focus (GtkWidget *widget,
2635 GtkDirectionType direction)
2637 /* A menu or its menu items cannot have focus */
2641 /* See notes in gtk_menu_popup() for information
2642 * about the "grab transfer window"
2645 menu_grab_transfer_window_get (GtkMenu *menu)
2647 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2650 GdkWindowAttr attributes;
2651 gint attributes_mask;
2653 attributes.x = -100;
2654 attributes.y = -100;
2655 attributes.width = 10;
2656 attributes.height = 10;
2657 attributes.window_type = GDK_WINDOW_TEMP;
2658 attributes.wclass = GDK_INPUT_ONLY;
2659 attributes.override_redirect = TRUE;
2660 attributes.event_mask = 0;
2662 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2664 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2665 &attributes, attributes_mask);
2666 gdk_window_set_user_data (window, menu);
2668 gdk_window_show (window);
2670 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2677 menu_grab_transfer_window_destroy (GtkMenu *menu)
2679 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2682 gdk_window_set_user_data (window, NULL);
2683 gdk_window_destroy (window);
2684 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2689 gtk_menu_unrealize (GtkWidget *widget)
2691 GtkMenu *menu = GTK_MENU (widget);
2692 GtkMenuPrivate *priv = menu->priv;
2694 menu_grab_transfer_window_destroy (menu);
2696 gdk_window_set_user_data (priv->view_window, NULL);
2697 gdk_window_destroy (priv->view_window);
2698 priv->view_window = NULL;
2700 gdk_window_set_user_data (priv->bin_window, NULL);
2701 gdk_window_destroy (priv->bin_window);
2702 priv->bin_window = NULL;
2704 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2708 calculate_line_heights (GtkMenu *menu,
2710 guint **ret_min_heights,
2711 guint **ret_nat_heights)
2714 GtkMenuPrivate *priv;
2715 GtkMenuShell *menu_shell;
2716 GtkWidget *child, *widget;
2718 guint horizontal_padding;
2727 widget = GTK_WIDGET (menu);
2728 menu_shell = GTK_MENU_SHELL (widget);
2730 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2731 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2732 n_heights = gtk_menu_get_n_rows (menu);
2733 n_columns = gtk_menu_get_n_columns (menu);
2734 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2736 gtk_widget_style_get (GTK_WIDGET (menu),
2737 "horizontal-padding", &horizontal_padding,
2739 get_menu_padding (widget, &padding);
2741 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2742 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2744 for (children = menu_shell->priv->children; children; children = children->next)
2749 gint child_min, child_nat;
2751 child = children->data;
2753 if (!gtk_widget_get_visible (child))
2756 get_effective_child_attach (child, &l, &r, &t, &b);
2758 part = avail_width / (r - l);
2760 gtk_widget_get_preferred_height_for_width (child, part,
2761 &child_min, &child_nat);
2763 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2765 part = MAX (child_min, toggle_size) / (b - t);
2766 min_heights[t] = MAX (min_heights[t], part);
2768 part = MAX (child_nat, toggle_size) / (b - t);
2769 nat_heights[t] = MAX (nat_heights[t], part);
2772 if (ret_min_heights)
2773 *ret_min_heights = min_heights;
2775 g_free (min_heights);
2777 if (ret_nat_heights)
2778 *ret_nat_heights = nat_heights;
2780 g_free (nat_heights);
2786 gtk_menu_size_allocate (GtkWidget *widget,
2787 GtkAllocation *allocation)
2790 GtkMenuPrivate *priv;
2791 GtkMenuShell *menu_shell;
2793 GtkAllocation child_allocation;
2798 guint vertical_padding;
2799 guint horizontal_padding;
2802 g_return_if_fail (GTK_IS_MENU (widget));
2803 g_return_if_fail (allocation != NULL);
2805 menu = GTK_MENU (widget);
2806 menu_shell = GTK_MENU_SHELL (widget);
2809 gtk_widget_set_allocation (widget, allocation);
2811 gtk_widget_style_get (GTK_WIDGET (menu),
2812 "vertical-padding", &vertical_padding,
2813 "horizontal-padding", &horizontal_padding,
2816 get_menu_padding (widget, &padding);
2817 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2819 g_free (priv->heights);
2820 priv->heights_length = calculate_line_heights (menu,
2825 /* refresh our cached height request */
2826 priv->requested_height = (2 * (border_width + vertical_padding)) +
2827 padding.top + padding.bottom;
2828 for (i = 0; i < priv->heights_length; i++)
2829 priv->requested_height += priv->heights[i];
2831 x = border_width + padding.left + horizontal_padding;
2832 y = border_width + padding.top + vertical_padding;
2833 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2834 padding.left - padding.right;
2835 height = allocation->height - (2 * (border_width + vertical_padding)) -
2836 padding.top - padding.bottom;
2838 if (menu_shell->priv->active)
2839 gtk_menu_scroll_to (menu, priv->scroll_offset);
2841 if (!priv->tearoff_active)
2843 GtkBorder arrow_border;
2845 get_arrows_border (menu, &arrow_border);
2846 y += arrow_border.top;
2847 height -= arrow_border.top;
2848 height -= arrow_border.bottom;
2851 width = MAX (1, width);
2852 height = MAX (1, height);
2854 if (gtk_widget_get_realized (widget))
2856 gdk_window_move_resize (gtk_widget_get_window (widget),
2857 allocation->x, allocation->y,
2858 allocation->width, allocation->height);
2860 gdk_window_move_resize (priv->view_window, x, y, width, height);
2863 if (menu_shell->priv->children)
2865 gint base_width = width / gtk_menu_get_n_columns (menu);
2867 children = menu_shell->priv->children;
2870 child = children->data;
2871 children = children->next;
2873 if (gtk_widget_get_visible (child))
2878 get_effective_child_attach (child, &l, &r, &t, &b);
2880 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2883 tmp = gtk_menu_get_n_columns (menu) - l;
2884 l = gtk_menu_get_n_columns (menu) - r;
2888 child_allocation.width = (r - l) * base_width;
2889 child_allocation.height = 0;
2890 child_allocation.x = l * base_width;
2891 child_allocation.y = 0;
2893 for (i = 0; i < b; i++)
2896 child_allocation.y += priv->heights[i];
2898 child_allocation.height += priv->heights[i];
2901 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2904 gtk_widget_size_allocate (child, &child_allocation);
2905 gtk_widget_queue_draw (child);
2909 /* Resize the item window */
2910 if (gtk_widget_get_realized (widget))
2916 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2917 height += priv->heights[i];
2919 width = gtk_menu_get_n_columns (menu) * base_width;
2920 gdk_window_resize (priv->bin_window, width, height);
2923 if (priv->tearoff_active)
2925 if (height >= priv->requested_height)
2927 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2929 gtk_widget_hide (priv->tearoff_scrollbar);
2930 gtk_menu_set_tearoff_hints (menu, allocation->width);
2932 gtk_menu_scroll_to (menu, 0);
2937 gtk_adjustment_configure (priv->tearoff_adjustment,
2938 gtk_adjustment_get_value (priv->tearoff_adjustment),
2940 priv->requested_height,
2941 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2942 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2943 allocation->height);
2945 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2947 gtk_widget_show (priv->tearoff_scrollbar);
2948 gtk_menu_set_tearoff_hints (menu, allocation->width);
2956 get_arrows_visible_area (GtkMenu *menu,
2957 GdkRectangle *border,
2958 GdkRectangle *upper,
2959 GdkRectangle *lower,
2962 GtkArrowPlacement arrow_placement;
2963 GtkWidget *widget = GTK_WIDGET (menu);
2965 guint vertical_padding;
2966 guint horizontal_padding;
2967 gint scroll_arrow_height;
2968 GtkBorder menu_padding;
2970 gtk_widget_style_get (widget,
2971 "vertical-padding", &vertical_padding,
2972 "horizontal-padding", &horizontal_padding,
2973 "scroll-arrow-vlength", &scroll_arrow_height,
2974 "arrow-placement", &arrow_placement,
2977 get_menu_padding (widget, &menu_padding);
2978 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2979 border->x = border_width + menu_padding.left + horizontal_padding;
2980 border->y = border_width + menu_padding.top + vertical_padding;
2981 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2982 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2984 switch (arrow_placement)
2986 case GTK_ARROWS_BOTH:
2987 upper->x = border->x;
2988 upper->y = border->y;
2989 upper->width = border->width - 2 * border->x;
2990 upper->height = scroll_arrow_height;
2992 lower->x = border->x;
2993 lower->y = border->height - border->y - scroll_arrow_height;
2994 lower->width = border->width - 2 * border->x;
2995 lower->height = scroll_arrow_height;
2998 case GTK_ARROWS_START:
2999 upper->x = border->x;
3000 upper->y = border->y;
3001 upper->width = (border->width - 2 * border->x) / 2;
3002 upper->height = scroll_arrow_height;
3004 lower->x = border->x + upper->width;
3005 lower->y = border->y;
3006 lower->width = (border->width - 2 * border->x) / 2;
3007 lower->height = scroll_arrow_height;
3010 case GTK_ARROWS_END:
3011 upper->x = border->x;
3012 upper->y = border->height - border->y - scroll_arrow_height;
3013 upper->width = (border->width - 2 * border->x) / 2;
3014 upper->height = scroll_arrow_height;
3016 lower->x = border->x + upper->width;
3017 lower->y = border->height - border->y - scroll_arrow_height;
3018 lower->width = (border->width - 2 * border->x) / 2;
3019 lower->height = scroll_arrow_height;
3023 g_assert_not_reached();
3024 upper->x = upper->y = upper->width = upper->height = 0;
3025 lower->x = lower->y = lower->width = lower->height = 0;
3028 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
3032 gtk_menu_draw (GtkWidget *widget,
3036 GtkMenuPrivate *priv;
3037 GtkStyleContext *context;
3038 GdkRectangle border;
3042 GtkBorder menu_padding;
3044 menu = GTK_MENU (widget);
3046 context = gtk_widget_get_style_context (widget);
3048 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3049 get_menu_padding (widget, &menu_padding);
3051 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3053 gfloat arrow_scaling;
3056 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3057 arrow_size = arrow_scaling * arrow_space;
3059 gtk_render_background (context, cr, 0, 0,
3060 gtk_widget_get_allocated_width (widget),
3061 gtk_widget_get_allocated_height (widget));
3062 gtk_render_frame (context, cr, 0, 0,
3063 gtk_widget_get_allocated_width (widget),
3064 gtk_widget_get_allocated_height (widget));
3066 gtk_style_context_save (context);
3067 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3069 if (priv->upper_arrow_visible && !priv->tearoff_active)
3071 gtk_style_context_save (context);
3072 gtk_style_context_set_state (context, priv->upper_arrow_state);
3074 gtk_render_background (context, cr,
3076 upper.width, upper.height);
3077 gtk_render_frame (context, cr,
3079 upper.width, upper.height);
3081 gtk_render_arrow (context, cr, 0,
3082 upper.x + (upper.width - arrow_size) / 2,
3083 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3086 gtk_style_context_restore (context);
3089 if (priv->lower_arrow_visible && !priv->tearoff_active)
3091 gtk_style_context_save (context);
3092 gtk_style_context_set_state (context, priv->lower_arrow_state);
3094 gtk_render_background (context, cr,
3096 lower.width, lower.height);
3097 gtk_render_frame (context, cr,
3099 lower.width, lower.height);
3101 gtk_render_arrow (context, cr, G_PI,
3102 lower.x + (lower.width - arrow_size) / 2,
3103 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3106 gtk_style_context_restore (context);
3109 gtk_style_context_restore (context);
3112 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3114 gint y = -border.y + priv->scroll_offset;
3117 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3119 if (!priv->tearoff_active)
3121 GtkBorder arrow_border;
3123 get_arrows_border (menu, &arrow_border);
3124 y -= arrow_border.top;
3127 gtk_render_background (context, cr,
3129 border.width, border.height);
3130 gtk_render_frame (context, cr,
3132 border.width, border.height);
3137 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3143 gtk_menu_show (GtkWidget *widget)
3145 GtkMenu *menu = GTK_MENU (widget);
3147 _gtk_menu_refresh_accel_paths (menu, FALSE);
3149 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3154 gtk_menu_get_preferred_width (GtkWidget *widget,
3159 GtkMenuShell *menu_shell;
3160 GtkMenuPrivate *priv;
3163 guint max_toggle_size;
3164 guint max_accel_width;
3165 guint horizontal_padding;
3167 gint child_min, child_nat;
3168 gint min_width, nat_width;
3171 menu = GTK_MENU (widget);
3172 menu_shell = GTK_MENU_SHELL (widget);
3175 min_width = nat_width = 0;
3177 max_toggle_size = 0;
3178 max_accel_width = 0;
3180 children = menu_shell->priv->children;
3187 child = children->data;
3188 children = children->next;
3190 if (! gtk_widget_get_visible (child))
3193 get_effective_child_attach (child, &l, &r, &t, &b);
3195 /* It's important to size_request the child
3196 * before doing the toggle size request, in
3197 * case the toggle size request depends on the size
3198 * request of a child of the child (e.g. for ImageMenuItem)
3200 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3202 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3203 max_toggle_size = MAX (max_toggle_size, toggle_size);
3204 max_accel_width = MAX (max_accel_width,
3205 GTK_MENU_ITEM (child)->priv->accelerator_width);
3207 part = child_min / (r - l);
3208 min_width = MAX (min_width, part);
3210 part = child_nat / (r - l);
3211 nat_width = MAX (nat_width, part);
3214 /* If the menu doesn't include any images or check items
3215 * reserve the space so that all menus are consistent.
3216 * We only do this for 'ordinary' menus, not for combobox
3217 * menus or multi-column menus
3219 if (max_toggle_size == 0 &&
3220 gtk_menu_get_n_columns (menu) == 1 &&
3221 !priv->no_toggle_size)
3223 GtkStyleContext *context;
3224 GtkWidgetPath *menu_path, *check_path;
3225 guint toggle_spacing;
3226 guint indicator_size;
3228 context = gtk_widget_get_style_context (widget);
3229 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3231 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3232 check_path = gtk_widget_path_copy (menu_path);
3233 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3235 gtk_style_context_set_path (context, check_path);
3236 gtk_widget_path_free (check_path);
3238 gtk_style_context_get_style (context,
3239 "toggle-spacing", &toggle_spacing,
3240 "indicator-size", &indicator_size,
3243 max_toggle_size = indicator_size + toggle_spacing;
3245 /* Restore real widget path */
3246 gtk_style_context_set_path (context, menu_path);
3247 gtk_widget_path_free (menu_path);
3250 min_width += 2 * max_toggle_size + max_accel_width;
3251 min_width *= gtk_menu_get_n_columns (menu);
3253 nat_width += 2 * max_toggle_size + max_accel_width;
3254 nat_width *= gtk_menu_get_n_columns (menu);
3256 gtk_widget_style_get (GTK_WIDGET (menu),
3257 "horizontal-padding", &horizontal_padding,
3260 get_menu_padding (widget, &padding);
3261 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3262 min_width += (2 * (border_width + horizontal_padding)) +
3263 padding.left + padding.right;
3264 nat_width += (2 * (border_width + horizontal_padding)) +
3265 padding.left + padding.right;
3267 priv->toggle_size = max_toggle_size;
3268 priv->accel_size = max_accel_width;
3271 *minimum_size = min_width;
3274 *natural_size = nat_width;
3276 /* Don't resize the tearoff if it is not active,
3277 * because it won't redraw (it is only a background pixmap).
3279 if (priv->tearoff_active)
3280 gtk_menu_set_tearoff_hints (menu, min_width);
3284 gtk_menu_get_preferred_height (GtkWidget *widget,
3290 /* Menus are height-for-width only, just return the height
3291 * for the minimum width
3293 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3294 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3298 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3304 GtkMenu *menu = GTK_MENU (widget);
3305 GtkMenuPrivate *priv = menu->priv;
3306 guint *min_heights, *nat_heights;
3307 guint vertical_padding, border_width;
3309 gint min_height, nat_height;
3311 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3312 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3313 get_menu_padding (widget, &padding);
3315 min_height = nat_height = (border_width + vertical_padding) * 2 +
3316 padding.top + padding.bottom;
3319 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3321 for (i = 0; i < n_heights; i++)
3323 min_height += min_heights[i];
3324 nat_height += nat_heights[i];
3327 if (priv->have_position)
3329 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3330 GdkRectangle monitor;
3332 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
3334 if (priv->position_y + min_height > monitor.y + monitor.height)
3335 min_height = monitor.y + monitor.height - priv->position_y;
3337 if (priv->position_y + nat_height > monitor.y + monitor.height)
3338 nat_height = monitor.y + monitor.height - priv->position_y;
3340 if (priv->position_y < monitor.y)
3342 min_height -= monitor.y - priv->position_y;
3343 nat_height -= monitor.y - priv->position_y;
3348 *minimum_size = min_height;
3351 *natural_size = nat_height;
3353 g_free (min_heights);
3354 g_free (nat_heights);
3360 gtk_menu_button_scroll (GtkMenu *menu,
3361 GdkEventButton *event)
3363 GtkMenuPrivate *priv = menu->priv;
3365 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3367 gboolean touchscreen_mode;
3369 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3370 "gtk-touchscreen-mode", &touchscreen_mode,
3373 if (touchscreen_mode)
3374 gtk_menu_handle_scrolling (menu,
3375 event->x_root, event->y_root,
3376 event->type == GDK_BUTTON_PRESS,
3386 pointer_in_menu_window (GtkWidget *widget,
3390 GtkMenu *menu = GTK_MENU (widget);
3391 GtkMenuPrivate *priv = menu->priv;
3392 GtkAllocation allocation;
3394 if (gtk_widget_get_mapped (priv->toplevel))
3396 GtkMenuShell *menu_shell;
3397 gint window_x, window_y;
3399 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3400 &window_x, &window_y);
3402 gtk_widget_get_allocation (widget, &allocation);
3403 if (x_root >= window_x && x_root < window_x + allocation.width &&
3404 y_root >= window_y && y_root < window_y + allocation.height)
3407 menu_shell = GTK_MENU_SHELL (widget);
3409 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3410 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3418 gtk_menu_button_press (GtkWidget *widget,
3419 GdkEventButton *event)
3421 if (event->type != GDK_BUTTON_PRESS)
3424 /* Don't pass down to menu shell for presses over scroll arrows
3426 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3429 /* Don't pass down to menu shell if a non-menuitem part of the menu
3430 * was clicked. The check for the event_widget being a GtkMenuShell
3431 * works because we have the pointer grabbed on menu_shell->window
3432 * with owner_events=TRUE, so all events that are either outside
3433 * the menu or on its border are delivered relative to
3434 * menu_shell->window.
3436 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3437 pointer_in_menu_window (widget, event->x_root, event->y_root))
3440 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3444 gtk_menu_button_release (GtkWidget *widget,
3445 GdkEventButton *event)
3447 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3449 if (priv->ignore_button_release)
3451 priv->ignore_button_release = FALSE;
3455 if (event->type != GDK_BUTTON_RELEASE)
3458 /* Don't pass down to menu shell for releases over scroll arrows
3460 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3463 /* Don't pass down to menu shell if a non-menuitem part of the menu
3464 * was clicked (see comment in button_press()).
3466 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3467 pointer_in_menu_window (widget, event->x_root, event->y_root))
3469 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3470 * bail out early here so it is in a consistent state for the
3471 * next button_press/button_release in GtkMenuShell.
3474 if (GTK_MENU_SHELL (widget)->priv->active)
3475 GTK_MENU_SHELL (widget)->priv->button = 0;
3480 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3483 static const gchar *
3484 get_accel_path (GtkWidget *menu_item,
3489 GClosure *accel_closure;
3490 GtkAccelGroup *accel_group;
3492 path = _gtk_widget_get_accel_path (menu_item, locked);
3495 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3501 label = gtk_bin_get_child (GTK_BIN (menu_item));
3503 if (GTK_IS_ACCEL_LABEL (label))
3505 g_object_get (label,
3506 "accel-closure", &accel_closure,
3510 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3512 *locked = gtk_accel_group_get_is_locked (accel_group);
3522 gtk_menu_key_press (GtkWidget *widget,
3525 GtkMenuShell *menu_shell;
3527 gboolean delete = FALSE;
3528 gboolean can_change_accels;
3529 gchar *accel = NULL;
3530 guint accel_key, accel_mods;
3531 GdkModifierType consumed_modifiers;
3532 GdkDisplay *display;
3534 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3535 g_return_val_if_fail (event != NULL, FALSE);
3537 menu_shell = GTK_MENU_SHELL (widget);
3538 menu = GTK_MENU (widget);
3540 gtk_menu_stop_navigating_submenu (menu);
3542 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3545 display = gtk_widget_get_display (widget);
3547 g_object_get (gtk_widget_get_settings (widget),
3548 "gtk-menu-bar-accel", &accel,
3549 "gtk-can-change-accels", &can_change_accels,
3552 if (accel && *accel)
3555 GdkModifierType mods = 0;
3557 gtk_accelerator_parse (accel, &keyval, &mods);
3560 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3562 /* FIXME this is wrong, needs to be in the global accel resolution
3563 * thing, to properly consider i18n etc., but that probably requires
3564 * AccelGroup changes etc.
3566 if (event->keyval == keyval && (mods & event->state) == mods)
3568 gtk_menu_shell_cancel (menu_shell);
3576 switch (event->keyval)
3578 case GDK_KEY_Delete:
3579 case GDK_KEY_KP_Delete:
3580 case GDK_KEY_BackSpace:
3587 /* Figure out what modifiers went into determining the key symbol */
3588 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3589 event->hardware_keycode,
3590 event->state, event->group,
3591 NULL, NULL, NULL, &consumed_modifiers);
3593 accel_key = gdk_keyval_to_lower (event->keyval);
3594 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3596 /* If lowercasing affects the keysym, then we need to include SHIFT
3597 * in the modifiers, we re-uppercase when we match against the keyval,
3598 * but display and save in caseless form.
3600 if (accel_key != event->keyval)
3601 accel_mods |= GDK_SHIFT_MASK;
3603 /* Modify the accelerators */
3604 if (can_change_accels &&
3605 menu_shell->priv->active_menu_item &&
3606 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3607 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3608 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3610 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3611 gboolean locked, replace_accels = TRUE;
3614 path = get_accel_path (menu_item, &locked);
3615 if (!path || locked)
3617 /* Can't change accelerators on menu_items without paths
3618 * (basically, those items are accelerator-locked).
3620 gtk_widget_error_bell (widget);
3626 /* For the keys that act to delete the current setting,
3627 * we delete the current setting if there is one, otherwise,
3628 * we set the key as the accelerator.
3634 if (gtk_accel_map_lookup_entry (path, &key) &&
3635 (key.accel_key || key.accel_mods))
3641 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3645 /* We failed, probably because this key is in use
3646 * and locked already.
3648 gtk_widget_error_bell (widget);
3657 check_threshold (GtkWidget *widget,
3666 ABS (start_x - x) > THRESHOLD ||
3667 ABS (start_y - y) > THRESHOLD;
3671 definitely_within_item (GtkWidget *widget,
3675 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3678 w = gdk_window_get_width (window);
3679 h = gdk_window_get_height (window);
3682 check_threshold (widget, 0, 0, x, y) &&
3683 check_threshold (widget, w - 1, 0, x, y) &&
3684 check_threshold (widget, w - 1, h - 1, x, y) &&
3685 check_threshold (widget, 0, h - 1, x, y);
3689 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3691 GtkMenuPrivate *priv = menu->priv;
3693 return priv->navigation_height && priv->navigation_width;
3697 gtk_menu_motion_notify (GtkWidget *widget,
3698 GdkEventMotion *event)
3700 GtkWidget *menu_item;
3702 GtkMenuShell *menu_shell;
3705 gboolean need_enter;
3707 if (GTK_IS_MENU (widget))
3709 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3711 if (priv->ignore_button_release)
3712 priv->ignore_button_release = FALSE;
3714 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3718 /* We received the event for one of two reasons:
3720 * a) We are the active menu, and did gtk_grab_add()
3721 * b) The widget is a child of ours, and the event was propagated
3723 * Since for computation of navigation regions, we want the menu which
3724 * is the parent of the menu item, for a), we need to find that menu,
3725 * which may be different from 'widget'.
3727 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3728 parent = gtk_widget_get_parent (menu_item);
3729 if (!GTK_IS_MENU_ITEM (menu_item) ||
3730 !GTK_IS_MENU (parent))
3733 menu_shell = GTK_MENU_SHELL (parent);
3734 menu = GTK_MENU (menu_shell);
3736 if (definitely_within_item (menu_item, event->x, event->y))
3737 menu_shell->priv->activate_time = 0;
3739 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3741 /* Check to see if we are within an active submenu's navigation region
3743 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3746 /* Make sure we pop down if we enter a non-selectable menu item, so we
3747 * don't show a submenu when the cursor is outside the stay-up triangle.
3749 if (!_gtk_menu_item_is_selectable (menu_item))
3751 /* We really want to deselect, but this gives the menushell code
3752 * a chance to do some bookkeeping about the menuitem.
3754 gtk_menu_shell_select_item (menu_shell, menu_item);
3760 /* The menu is now sensitive to enter events on its items, but
3761 * was previously sensitive. So we fake an enter event.
3763 menu_shell->priv->ignore_enter = FALSE;
3765 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3766 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3768 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3771 send_event->crossing.window = g_object_ref (event->window);
3772 send_event->crossing.time = event->time;
3773 send_event->crossing.send_event = TRUE;
3774 send_event->crossing.x_root = event->x_root;
3775 send_event->crossing.y_root = event->y_root;
3776 send_event->crossing.x = event->x;
3777 send_event->crossing.y = event->y;
3778 send_event->crossing.state = event->state;
3779 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3781 /* We send the event to 'widget', the currently active menu,
3782 * instead of 'menu', the menu that the pointer is in. This
3783 * will ensure that the event will be ignored unless the
3784 * menuitem is a child of the active menu or some parent
3785 * menu of the active menu.
3787 result = gtk_widget_event (widget, send_event);
3788 gdk_event_free (send_event);
3798 get_double_arrows (GtkMenu *menu)
3800 GtkMenuPrivate *priv = menu->priv;
3801 gboolean double_arrows;
3802 GtkArrowPlacement arrow_placement;
3804 gtk_widget_style_get (GTK_WIDGET (menu),
3805 "double-arrows", &double_arrows,
3806 "arrow-placement", &arrow_placement,
3809 if (arrow_placement != GTK_ARROWS_BOTH)
3812 return double_arrows || (priv->initially_pushed_in &&
3813 priv->scroll_offset != 0);
3817 gtk_menu_scroll_by (GtkMenu *menu,
3820 GtkMenuPrivate *priv = menu->priv;
3821 GtkBorder arrow_border;
3825 gboolean double_arrows;
3827 widget = GTK_WIDGET (menu);
3828 offset = priv->scroll_offset + step;
3830 get_arrows_border (menu, &arrow_border);
3832 double_arrows = get_double_arrows (menu);
3834 /* If we scroll upward and the non-visible top part
3835 * is smaller than the scroll arrow it would be
3836 * pretty stupid to show the arrow and taking more
3837 * screen space than just scrolling to the top.
3840 if ((step < 0) && (offset < arrow_border.top))
3843 /* Don't scroll over the top if we weren't before: */
3844 if ((priv->scroll_offset >= 0) && (offset < 0))
3847 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3849 if (priv->scroll_offset == 0 &&
3850 view_height >= priv->requested_height)
3853 /* Don't scroll past the bottom if we weren't before: */
3854 if (priv->scroll_offset > 0)
3855 view_height -= arrow_border.top;
3857 /* When both arrows are always shown,
3858 * reduce view height even more.
3861 view_height -= arrow_border.bottom;
3863 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3864 (offset + view_height > priv->requested_height))
3865 offset = priv->requested_height - view_height;
3867 if (offset != priv->scroll_offset)
3868 gtk_menu_scroll_to (menu, offset);
3872 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3873 gboolean touchscreen_mode)
3875 GtkMenuPrivate *priv = menu->priv;
3876 gboolean upper_visible;
3877 gboolean lower_visible;
3879 upper_visible = priv->upper_arrow_visible;
3880 lower_visible = priv->lower_arrow_visible;
3882 gtk_menu_scroll_by (menu, priv->scroll_step);
3884 if (touchscreen_mode &&
3885 (upper_visible != priv->upper_arrow_visible ||
3886 lower_visible != priv->lower_arrow_visible))
3888 /* We are about to hide a scroll arrow while the mouse is pressed,
3889 * this would cause the uncovered menu item to be activated on button
3890 * release. Therefore we need to ignore button release here
3892 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3893 priv->ignore_button_release = TRUE;
3898 gtk_menu_scroll_timeout (gpointer data)
3901 gboolean touchscreen_mode;
3903 menu = GTK_MENU (data);
3905 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3906 "gtk-touchscreen-mode", &touchscreen_mode,
3909 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3915 gtk_menu_scroll_timeout_initial (gpointer data)
3919 gboolean touchscreen_mode;
3921 menu = GTK_MENU (data);
3923 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3924 "gtk-timeout-repeat", &timeout,
3925 "gtk-touchscreen-mode", &touchscreen_mode,
3928 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3930 gtk_menu_remove_scroll_timeout (menu);
3932 menu->priv->scroll_timeout =
3933 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3939 gtk_menu_start_scrolling (GtkMenu *menu)
3942 gboolean touchscreen_mode;
3944 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3945 "gtk-timeout-repeat", &timeout,
3946 "gtk-touchscreen-mode", &touchscreen_mode,
3949 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3951 menu->priv->scroll_timeout =
3952 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3956 gtk_menu_scroll (GtkWidget *widget,
3957 GdkEventScroll *event)
3959 GtkMenu *menu = GTK_MENU (widget);
3961 switch (event->direction)
3963 case GDK_SCROLL_RIGHT:
3964 case GDK_SCROLL_DOWN:
3965 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3967 case GDK_SCROLL_LEFT:
3969 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3977 get_arrows_sensitive_area (GtkMenu *menu,
3978 GdkRectangle *upper,
3979 GdkRectangle *lower)
3981 GtkArrowPlacement arrow_placement;
3982 GtkWidget *widget = GTK_WIDGET (menu);
3986 guint vertical_padding;
3988 gint scroll_arrow_height;
3991 window = gtk_widget_get_window (widget);
3992 width = gdk_window_get_width (window);
3993 height = gdk_window_get_height (window);
3995 gtk_widget_style_get (widget,
3996 "vertical-padding", &vertical_padding,
3997 "scroll-arrow-vlength", &scroll_arrow_height,
3998 "arrow-placement", &arrow_placement,
4001 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
4002 get_menu_padding (widget, &padding);
4004 gdk_window_get_position (window, &win_x, &win_y);
4006 switch (arrow_placement)
4008 case GTK_ARROWS_BOTH:
4013 upper->width = width;
4014 upper->height = scroll_arrow_height + border + padding.top;
4020 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
4021 lower->width = width;
4022 lower->height = scroll_arrow_height + border + padding.bottom;
4026 case GTK_ARROWS_START:
4031 upper->width = width / 2;
4032 upper->height = scroll_arrow_height + border + padding.top;
4037 lower->x = win_x + width / 2;
4039 lower->width = width / 2;
4040 lower->height = scroll_arrow_height + border + padding.bottom;
4044 case GTK_ARROWS_END:
4048 upper->y = win_y + height - border - scroll_arrow_height;
4049 upper->width = width / 2;
4050 upper->height = scroll_arrow_height + border + padding.top;
4055 lower->x = win_x + width / 2;
4056 lower->y = win_y + height - border - scroll_arrow_height;
4057 lower->width = width / 2;
4058 lower->height = scroll_arrow_height + border + padding.bottom;
4066 gtk_menu_handle_scrolling (GtkMenu *menu,
4072 GtkMenuPrivate *priv = menu->priv;
4073 GtkMenuShell *menu_shell;
4076 gboolean scroll_fast = FALSE;
4078 gboolean touchscreen_mode;
4080 menu_shell = GTK_MENU_SHELL (menu);
4082 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4083 "gtk-touchscreen-mode", &touchscreen_mode,
4086 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4091 /* upper arrow handling */
4093 get_arrows_sensitive_area (menu, &rect, NULL);
4096 if (priv->upper_arrow_visible && !priv->tearoff_active &&
4097 (x >= rect.x) && (x < rect.x + rect.width) &&
4098 (y >= rect.y) && (y < rect.y + rect.height))
4103 if (touchscreen_mode)
4104 priv->upper_arrow_prelight = in_arrow;
4106 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4108 gboolean arrow_pressed = FALSE;
4110 if (priv->upper_arrow_visible && !priv->tearoff_active)
4112 if (touchscreen_mode)
4114 if (enter && priv->upper_arrow_prelight)
4116 if (priv->scroll_timeout == 0)
4118 /* Deselect the active item so that
4119 * any submenus are popped down
4121 gtk_menu_shell_deselect (menu_shell);
4123 gtk_menu_remove_scroll_timeout (menu);
4124 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4128 /* Only do stuff on click. */
4129 gtk_menu_start_scrolling (menu);
4130 arrow_pressed = TRUE;
4135 arrow_pressed = TRUE;
4140 gtk_menu_stop_scrolling (menu);
4143 else /* !touchscreen_mode */
4145 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4147 if (enter && in_arrow &&
4148 (!priv->upper_arrow_prelight ||
4149 priv->scroll_fast != scroll_fast))
4151 priv->upper_arrow_prelight = TRUE;
4152 priv->scroll_fast = scroll_fast;
4154 /* Deselect the active item so that
4155 * any submenus are popped down
4157 gtk_menu_shell_deselect (menu_shell);
4159 gtk_menu_remove_scroll_timeout (menu);
4160 priv->scroll_step = scroll_fast
4161 ? -MENU_SCROLL_STEP2
4162 : -MENU_SCROLL_STEP1;
4164 priv->scroll_timeout =
4165 gdk_threads_add_timeout (scroll_fast
4166 ? MENU_SCROLL_TIMEOUT2
4167 : MENU_SCROLL_TIMEOUT1,
4168 gtk_menu_scroll_timeout, menu);
4170 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4172 gtk_menu_stop_scrolling (menu);
4177 /* gtk_menu_start_scrolling() might have hit the top of the
4178 * menu, so check if the button isn't insensitive before
4179 * changing it to something else.
4181 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4183 GtkStateFlags arrow_state = 0;
4186 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4188 if (priv->upper_arrow_prelight)
4189 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4191 if (arrow_state != priv->upper_arrow_state)
4193 priv->upper_arrow_state = arrow_state;
4195 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4201 /* lower arrow handling */
4203 get_arrows_sensitive_area (menu, NULL, &rect);
4206 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4207 (x >= rect.x) && (x < rect.x + rect.width) &&
4208 (y >= rect.y) && (y < rect.y + rect.height))
4213 if (touchscreen_mode)
4214 priv->lower_arrow_prelight = in_arrow;
4216 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4218 gboolean arrow_pressed = FALSE;
4220 if (priv->lower_arrow_visible && !priv->tearoff_active)
4222 if (touchscreen_mode)
4224 if (enter && priv->lower_arrow_prelight)
4226 if (priv->scroll_timeout == 0)
4228 /* Deselect the active item so that
4229 * any submenus are popped down
4231 gtk_menu_shell_deselect (menu_shell);
4233 gtk_menu_remove_scroll_timeout (menu);
4234 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4238 /* Only do stuff on click. */
4239 gtk_menu_start_scrolling (menu);
4240 arrow_pressed = TRUE;
4245 arrow_pressed = TRUE;
4250 gtk_menu_stop_scrolling (menu);
4253 else /* !touchscreen_mode */
4255 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4257 if (enter && in_arrow &&
4258 (!priv->lower_arrow_prelight ||
4259 priv->scroll_fast != scroll_fast))
4261 priv->lower_arrow_prelight = TRUE;
4262 priv->scroll_fast = scroll_fast;
4264 /* Deselect the active item so that
4265 * any submenus are popped down
4267 gtk_menu_shell_deselect (menu_shell);
4269 gtk_menu_remove_scroll_timeout (menu);
4270 priv->scroll_step = scroll_fast
4272 : MENU_SCROLL_STEP1;
4274 priv->scroll_timeout =
4275 gdk_threads_add_timeout (scroll_fast
4276 ? MENU_SCROLL_TIMEOUT2
4277 : MENU_SCROLL_TIMEOUT1,
4278 gtk_menu_scroll_timeout, menu);
4280 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4282 gtk_menu_stop_scrolling (menu);
4287 /* gtk_menu_start_scrolling() might have hit the bottom of the
4288 * menu, so check if the button isn't insensitive before
4289 * changing it to something else.
4291 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4293 GtkStateFlags arrow_state = 0;
4296 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4298 if (priv->lower_arrow_prelight)
4299 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4301 if (arrow_state != priv->lower_arrow_state)
4303 priv->lower_arrow_state = arrow_state;
4305 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4313 gtk_menu_enter_notify (GtkWidget *widget,
4314 GdkEventCrossing *event)
4316 GtkWidget *menu_item;
4318 gboolean touchscreen_mode;
4320 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4321 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4322 event->mode == GDK_CROSSING_STATE_CHANGED)
4325 g_object_get (gtk_widget_get_settings (widget),
4326 "gtk-touchscreen-mode", &touchscreen_mode,
4329 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4330 if (GTK_IS_MENU (widget))
4332 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4334 if (!menu_shell->priv->ignore_enter)
4335 gtk_menu_handle_scrolling (GTK_MENU (widget),
4336 event->x_root, event->y_root, TRUE, TRUE);
4339 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4341 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4343 if (GTK_IS_MENU (menu))
4345 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4346 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4348 if (priv->seen_item_enter)
4350 /* This is the second enter we see for an item
4351 * on this menu. This means a release should always
4354 menu_shell->priv->activate_time = 0;
4356 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4357 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4359 if (definitely_within_item (menu_item, event->x, event->y))
4361 /* This is an actual user-enter (ie. not a pop-under)
4362 * In this case, the user must either have entered
4363 * sufficiently far enough into the item, or he must move
4364 * far enough away from the enter point. (see
4365 * gtk_menu_motion_notify())
4367 menu_shell->priv->activate_time = 0;
4371 priv->seen_item_enter = TRUE;
4375 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4376 * will not correspond to the event widget's parent. Check to see
4377 * if we are in the parent's navigation region.
4379 parent = gtk_widget_get_parent (menu_item);
4380 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4381 gtk_menu_navigating_submenu (GTK_MENU (parent),
4382 event->x_root, event->y_root))
4385 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4389 gtk_menu_leave_notify (GtkWidget *widget,
4390 GdkEventCrossing *event)
4392 GtkMenuShell *menu_shell;
4394 GtkMenuItem *menu_item;
4395 GtkWidget *event_widget;
4397 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4398 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4399 event->mode == GDK_CROSSING_STATE_CHANGED)
4402 menu = GTK_MENU (widget);
4403 menu_shell = GTK_MENU_SHELL (widget);
4405 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4408 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4410 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4412 if (!GTK_IS_MENU_ITEM (event_widget))
4415 menu_item = GTK_MENU_ITEM (event_widget);
4417 /* Here we check to see if we're leaving an active menu item
4418 * with a submenu, in which case we enter submenu navigation mode.
4420 if (menu_shell->priv->active_menu_item != NULL
4421 && menu_item->priv->submenu != NULL
4422 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4424 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4426 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4429 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4431 /* We are leaving an active menu item with nonactive submenu.
4432 * Deselect it so we don't surprise the user with by popping
4433 * up a submenu _after_ he left the item.
4435 gtk_menu_shell_deselect (menu_shell);
4440 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4444 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4446 GtkMenuPrivate *priv = menu->priv;
4448 priv->navigation_x = 0;
4449 priv->navigation_y = 0;
4450 priv->navigation_width = 0;
4451 priv->navigation_height = 0;
4453 if (priv->navigation_timeout)
4455 g_source_remove (priv->navigation_timeout);
4456 priv->navigation_timeout = 0;
4460 /* When the timeout is elapsed, the navigation region is destroyed
4461 * and the menuitem under the pointer (if any) is selected.
4464 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4466 GtkMenuPopdownData *popdown_data = user_data;
4467 GtkMenu *menu = popdown_data->menu;
4468 GtkMenuPrivate *priv = menu->priv;
4469 GdkWindow *child_window;
4471 gtk_menu_stop_navigating_submenu (menu);
4473 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4475 child_window = gdk_window_get_device_position (priv->bin_window,
4476 popdown_data->device,
4481 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4483 send_event->crossing.window = g_object_ref (child_window);
4484 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4485 send_event->crossing.send_event = TRUE;
4486 gdk_event_set_device (send_event, popdown_data->device);
4488 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4490 gdk_event_free (send_event);
4498 gtk_menu_navigating_submenu (GtkMenu *menu,
4502 GtkMenuPrivate *priv = menu->priv;
4505 if (!gtk_menu_has_navigation_triangle (menu))
4508 width = priv->navigation_width;
4509 height = priv->navigation_height;
4511 /* Check if x/y are in the triangle spanned by the navigation parameters */
4513 /* 1) Move the coordinates so the triangle starts at 0,0 */
4514 event_x -= priv->navigation_x;
4515 event_y -= priv->navigation_y;
4517 /* 2) Ensure both legs move along the positive axis */
4529 /* 3) Check that the given coordinate is inside the triangle. The formula
4530 * is a transformed form of this formula: x/w + y/h <= 1
4532 if (event_x >= 0 && event_y >= 0 &&
4533 event_x * height + event_y * width <= width * height)
4539 gtk_menu_stop_navigating_submenu (menu);
4545 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4546 GtkMenuItem *menu_item,
4547 GdkEventCrossing *event)
4549 GtkMenuPrivate *priv = menu->priv;
4550 gint submenu_left = 0;
4551 gint submenu_right = 0;
4552 gint submenu_top = 0;
4553 gint submenu_bottom = 0;
4555 GtkWidget *event_widget;
4556 GtkMenuPopdownData *popdown_data;
4559 g_return_if_fail (menu_item->priv->submenu != NULL);
4560 g_return_if_fail (event != NULL);
4562 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4564 window = gtk_widget_get_window (menu_item->priv->submenu);
4565 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4567 submenu_right = submenu_left + gdk_window_get_width (window);
4568 submenu_bottom = submenu_top + gdk_window_get_height (window);
4570 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4572 if (event->x >= 0 && event->x < width)
4576 gtk_menu_stop_navigating_submenu (menu);
4578 /* The navigation region is the triangle closest to the x/y
4579 * location of the rectangle. This is why the width or height
4582 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4585 priv->navigation_x = submenu_left;
4586 priv->navigation_width = event->x_root - submenu_left;
4591 priv->navigation_x = submenu_right;
4592 priv->navigation_width = event->x_root - submenu_right;
4598 priv->navigation_y = event->y_root;
4599 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4601 if (priv->navigation_height >= 0)
4607 priv->navigation_y = event->y_root;
4608 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4610 if (priv->navigation_height <= 0)
4614 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4615 "gtk-menu-popdown-delay", &popdown_delay,
4618 popdown_data = g_new (GtkMenuPopdownData, 1);
4619 popdown_data->menu = menu;
4620 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4622 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4624 gtk_menu_stop_navigating_submenu_cb,
4626 (GDestroyNotify) g_free);
4631 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4635 g_return_if_fail (GTK_IS_MENU (menu_shell));
4637 parent = menu_shell->priv->parent_menu_shell;
4639 menu_shell->priv->activate_time = 0;
4640 gtk_menu_popdown (GTK_MENU (menu_shell));
4643 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4647 gtk_menu_position (GtkMenu *menu,
4648 gboolean set_scroll_offset)
4650 GtkMenuPrivate *priv = menu->priv;
4652 GtkRequisition requisition;
4657 GdkScreen *pointer_screen;
4658 GdkRectangle monitor;
4661 widget = GTK_WIDGET (menu);
4663 screen = gtk_widget_get_screen (widget);
4664 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4665 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4667 /* Realize so we have the proper width and heigh to figure out
4668 * the right place to popup the menu.
4670 gtk_widget_realize (priv->toplevel);
4671 requisition.width = gtk_widget_get_allocated_width (widget);
4672 requisition.height = gtk_widget_get_allocated_height (widget);
4674 if (pointer_screen != screen)
4676 /* Pointer is on a different screen; roughly center the
4677 * menu on the screen. If someone was using multiscreen
4678 * + Xinerama together they'd probably want something
4679 * fancier; but that is likely to be vanishingly rare.
4681 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4682 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4685 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4686 priv->initially_pushed_in = FALSE;
4688 /* Set the type hint here to allow custom position functions
4689 * to set a different hint
4691 if (!gtk_widget_get_visible (priv->toplevel))
4692 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4694 if (priv->position_func)
4696 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4697 priv->position_func_data);
4699 if (priv->monitor_num < 0)
4700 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4702 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4706 gint space_left, space_right, space_above, space_below;
4710 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4712 get_menu_padding (widget, &padding);
4714 /* The placement of popup menus horizontally works like this (with
4715 * RTL in parentheses)
4717 * - If there is enough room to the right (left) of the mouse cursor,
4718 * position the menu there.
4720 * - Otherwise, if if there is enough room to the left (right) of the
4721 * mouse cursor, position the menu there.
4723 * - Otherwise if the menu is smaller than the monitor, position it
4724 * on the side of the mouse cursor that has the most space available
4726 * - Otherwise (if there is simply not enough room for the menu on the
4727 * monitor), position it as far left (right) as possible.
4729 * Positioning in the vertical direction is similar: first try below
4730 * mouse cursor, then above.
4732 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4734 space_left = x - monitor.x;
4735 space_right = monitor.x + monitor.width - x - 1;
4736 space_above = y - monitor.y;
4737 space_below = monitor.y + monitor.height - y - 1;
4739 /* Position horizontally. */
4741 /* the amount of space we need to position the menu.
4742 * Note the menu is offset "thickness" pixels
4744 needed_width = requisition.width - padding.left;
4746 if (needed_width <= space_left ||
4747 needed_width <= space_right)
4749 if ((rtl && needed_width <= space_left) ||
4750 (!rtl && needed_width > space_right))
4753 x = x + padding.left - requisition.width + 1;
4757 /* position right */
4758 x = x - padding.right;
4761 /* x is clamped on-screen further down */
4763 else if (requisition.width <= monitor.width)
4765 /* the menu is too big to fit on either side of the mouse
4766 * cursor, but smaller than the monitor. Position it on
4767 * the side that has the most space
4769 if (space_left > space_right)
4777 x = monitor.x + monitor.width - requisition.width;
4780 else /* menu is simply too big for the monitor */
4785 x = monitor.x + monitor.width - requisition.width;
4794 /* Position vertically.
4795 * The algorithm is the same as above, but simpler
4796 * because we don't have to take RTL into account.
4798 needed_height = requisition.height - padding.top;
4800 if (needed_height <= space_above ||
4801 needed_height <= space_below)
4803 if (needed_height <= space_below)
4804 y = y - padding.top;
4806 y = y + padding.bottom - requisition.height + 1;
4808 y = CLAMP (y, monitor.y,
4809 monitor.y + monitor.height - requisition.height);
4811 else if (needed_height > space_below && needed_height > space_above)
4813 if (space_below >= space_above)
4814 y = monitor.y + monitor.height - requisition.height;
4826 if (priv->initially_pushed_in)
4828 menu_height = requisition.height;
4830 if (y + menu_height > monitor.y + monitor.height)
4832 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4833 y = (monitor.y + monitor.height) - menu_height;
4838 scroll_offset += monitor.y - y;
4843 /* FIXME: should this be done in the various position_funcs ? */
4844 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4846 if (GTK_MENU_SHELL (menu)->priv->active)
4848 priv->have_position = TRUE;
4849 priv->position_x = x;
4850 priv->position_y = y;
4853 if (y + requisition.height > monitor.y + monitor.height)
4854 requisition.height = (monitor.y + monitor.height) - y;
4858 scroll_offset += monitor.y - y;
4859 requisition.height -= monitor.y - y;
4863 if (scroll_offset > 0)
4865 GtkBorder arrow_border;
4867 get_arrows_border (menu, &arrow_border);
4868 scroll_offset += arrow_border.top;
4871 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4874 if (!GTK_MENU_SHELL (menu)->priv->active)
4876 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4877 requisition.width, requisition.height);
4880 if (set_scroll_offset)
4881 priv->scroll_offset = scroll_offset;
4885 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4887 GtkMenuPrivate *priv = menu->priv;
4889 if (priv->scroll_timeout)
4891 g_source_remove (priv->scroll_timeout);
4892 priv->scroll_timeout = 0;
4897 gtk_menu_stop_scrolling (GtkMenu *menu)
4899 GtkMenuPrivate *priv = menu->priv;
4900 gboolean touchscreen_mode;
4902 gtk_menu_remove_scroll_timeout (menu);
4904 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4905 "gtk-touchscreen-mode", &touchscreen_mode,
4908 if (!touchscreen_mode)
4910 priv->upper_arrow_prelight = FALSE;
4911 priv->lower_arrow_prelight = FALSE;
4916 gtk_menu_scroll_to (GtkMenu *menu,
4919 GtkMenuPrivate *priv = menu->priv;
4920 GtkAllocation allocation;
4921 GtkBorder arrow_border, padding;
4924 gint view_width, view_height;
4927 guint vertical_padding;
4928 guint horizontal_padding;
4929 gboolean double_arrows;
4931 widget = GTK_WIDGET (menu);
4933 if (priv->tearoff_active && priv->tearoff_adjustment)
4934 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4936 /* Move/resize the viewport according to arrows: */
4937 gtk_widget_get_allocation (widget, &allocation);
4938 view_width = allocation.width;
4939 view_height = allocation.height;
4941 gtk_widget_style_get (GTK_WIDGET (menu),
4942 "vertical-padding", &vertical_padding,
4943 "horizontal-padding", &horizontal_padding,
4946 get_menu_padding (widget, &padding);
4947 double_arrows = get_double_arrows (menu);
4949 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4951 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4952 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4953 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4954 padding.top - padding.bottom;
4956 x = border_width + padding.left + horizontal_padding;
4957 y = border_width + padding.top + vertical_padding;
4959 if (double_arrows && !priv->tearoff_active)
4961 if (view_height < menu_height ||
4962 (offset > 0 && priv->scroll_offset > 0) ||
4963 (offset < 0 && priv->scroll_offset < 0))
4965 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4966 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4968 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4969 gtk_widget_queue_draw (GTK_WIDGET (menu));
4971 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4973 get_arrows_border (menu, &arrow_border);
4974 y += arrow_border.top;
4975 view_height -= arrow_border.top;
4976 view_height -= arrow_border.bottom;
4979 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4982 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4984 if (priv->upper_arrow_prelight)
4985 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4987 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4990 if (offset >= menu_height - view_height)
4991 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4994 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4996 if (priv->lower_arrow_prelight)
4997 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4999 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
5002 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
5003 (priv->lower_arrow_state != lower_arrow_previous_state))
5004 gtk_widget_queue_draw (GTK_WIDGET (menu));
5006 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5007 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5009 /* At the upper border, possibly remove timeout */
5010 if (priv->scroll_step < 0)
5012 gtk_menu_stop_scrolling (menu);
5013 gtk_widget_queue_draw (GTK_WIDGET (menu));
5017 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5018 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5020 /* At the lower border, possibly remove timeout */
5021 if (priv->scroll_step > 0)
5023 gtk_menu_stop_scrolling (menu);
5024 gtk_widget_queue_draw (GTK_WIDGET (menu));
5028 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
5032 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
5033 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
5035 gtk_menu_stop_scrolling (menu);
5036 gtk_widget_queue_draw (GTK_WIDGET (menu));
5039 else if (!priv->tearoff_active)
5041 gboolean last_visible;
5043 last_visible = priv->upper_arrow_visible;
5044 priv->upper_arrow_visible = offset > 0;
5046 /* upper_arrow_visible may have changed, so requery the border */
5047 get_arrows_border (menu, &arrow_border);
5048 view_height -= arrow_border.top;
5050 if ((last_visible != priv->upper_arrow_visible) &&
5051 !priv->upper_arrow_visible)
5053 priv->upper_arrow_prelight = FALSE;
5055 /* If we hide the upper arrow, possibly remove timeout */
5056 if (priv->scroll_step < 0)
5058 gtk_menu_stop_scrolling (menu);
5059 gtk_widget_queue_draw (GTK_WIDGET (menu));
5063 last_visible = priv->lower_arrow_visible;
5064 priv->lower_arrow_visible = offset < menu_height - view_height;
5066 /* lower_arrow_visible may have changed, so requery the border */
5067 get_arrows_border (menu, &arrow_border);
5068 view_height -= arrow_border.bottom;
5070 if ((last_visible != priv->lower_arrow_visible) &&
5071 !priv->lower_arrow_visible)
5073 priv->lower_arrow_prelight = FALSE;
5075 /* If we hide the lower arrow, possibly remove timeout */
5076 if (priv->scroll_step > 0)
5078 gtk_menu_stop_scrolling (menu);
5079 gtk_widget_queue_draw (GTK_WIDGET (menu));
5083 y += arrow_border.top;
5086 /* Scroll the menu: */
5087 if (gtk_widget_get_realized (widget))
5088 gdk_window_move (priv->bin_window, 0, -offset);
5090 if (gtk_widget_get_realized (widget))
5091 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5093 priv->scroll_offset = offset;
5097 compute_child_offset (GtkMenu *menu,
5098 GtkWidget *menu_item,
5101 gboolean *is_last_child)
5103 GtkMenuPrivate *priv = menu->priv;
5104 gint item_top_attach;
5105 gint item_bottom_attach;
5106 gint child_offset = 0;
5109 get_effective_child_attach (menu_item, NULL, NULL,
5110 &item_top_attach, &item_bottom_attach);
5112 /* there is a possibility that we get called before _size_request,
5113 * so check the height table for safety.
5115 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5118 /* when we have a row with only invisible children, its height will
5119 * be zero, so there's no need to check WIDGET_VISIBLE here
5121 for (i = 0; i < item_top_attach; i++)
5122 child_offset += priv->heights[i];
5125 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5127 *offset = child_offset;
5129 *height = priv->heights[item_top_attach];
5135 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5136 GtkWidget *menu_item)
5138 GtkMenu *menu = GTK_MENU (menu_shell);
5139 GtkMenuPrivate *priv = menu->priv;
5140 GtkWidget *widget = GTK_WIDGET (menu_shell);
5141 gint child_offset, child_height;
5145 gboolean last_child = 0;
5147 /* We need to check if the selected item fully visible.
5148 * If not we need to scroll the menu so that it becomes fully
5151 if (compute_child_offset (menu, menu_item,
5152 &child_offset, &child_height, &last_child))
5154 guint vertical_padding;
5155 gboolean double_arrows;
5158 y = priv->scroll_offset;
5159 height = gdk_window_get_height (gtk_widget_get_window (widget));
5161 gtk_widget_style_get (widget,
5162 "vertical-padding", &vertical_padding,
5165 double_arrows = get_double_arrows (menu);
5166 get_menu_padding (widget, &padding);
5168 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5169 padding.top + padding.bottom +
5170 2 * vertical_padding;
5171 if (child_offset < y)
5173 /* Ignore the enter event we might get if the pointer
5176 menu_shell->priv->ignore_enter = TRUE;
5177 gtk_menu_scroll_to (menu, child_offset);
5181 GtkBorder arrow_border;
5185 get_arrows_border (menu, &arrow_border);
5186 if (!priv->tearoff_active)
5187 arrow_height = arrow_border.top + arrow_border.bottom;
5189 if (child_offset + child_height > y + height - arrow_height)
5192 if ((!last_child && !priv->tearoff_active) || double_arrows)
5193 arrow_height += arrow_border.bottom;
5195 y = child_offset + child_height - height + arrow_height;
5196 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5198 /* Need upper arrow */
5199 arrow_height += arrow_border.top;
5200 y = child_offset + child_height - height + arrow_height;
5202 /* Ignore the enter event we might get if the pointer
5205 menu_shell->priv->ignore_enter = TRUE;
5206 gtk_menu_scroll_to (menu, y);
5213 gtk_menu_select_item (GtkMenuShell *menu_shell,
5214 GtkWidget *menu_item)
5216 GtkMenu *menu = GTK_MENU (menu_shell);
5218 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5219 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5221 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5225 /* Reparent the menu, taking care of the refcounting
5227 * If unrealize is true we force a unrealize while reparenting the parent.
5228 * This can help eliminate flicker in some cases.
5230 * What happens is that when the menu is unrealized and then re-realized,
5231 * the allocations are as follows:
5233 * parent - 1x1 at (0,0)
5234 * child1 - 100x20 at (0,0)
5235 * child2 - 100x20 at (0,20)
5236 * child3 - 100x20 at (0,40)
5238 * That is, the parent is small but the children are full sized. Then,
5239 * when the queued_resize gets processed, the parent gets resized to
5242 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5243 * contains the following logic:
5245 * - if a move or resize operation on a window would change the clip
5246 * region on the children, then before the window is resized
5247 * the background for children is temporarily set to None, the
5248 * move/resize done, and the background for the children restored.
5250 * So, at the point where the parent is resized to final size, the
5251 * background for the children is temporarily None, and thus they
5252 * are not cleared to the background color and the previous background
5253 * (the image of the menu) is left in place.
5256 gtk_menu_reparent (GtkMenu *menu,
5257 GtkWidget *new_parent,
5260 GObject *object = G_OBJECT (menu);
5261 GtkWidget *widget = GTK_WIDGET (menu);
5262 gboolean was_floating = g_object_is_floating (object);
5264 g_object_ref_sink (object);
5268 g_object_ref (object);
5269 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5270 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5271 g_object_unref (object);
5274 gtk_widget_reparent (widget, new_parent);
5277 g_object_force_floating (object);
5279 g_object_unref (object);
5283 gtk_menu_show_all (GtkWidget *widget)
5285 /* Show children, but not self. */
5286 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5290 * gtk_menu_set_screen:
5292 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5293 * determined by the widget the menu is attached to
5295 * Sets the #GdkScreen on which the menu will be displayed.
5300 gtk_menu_set_screen (GtkMenu *menu,
5303 g_return_if_fail (GTK_IS_MENU (menu));
5304 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5306 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5310 menu_change_screen (menu, screen);
5314 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5316 attach_widget_screen_changed (attach_widget, NULL, menu);
5323 * @child: a #GtkMenuItem
5324 * @left_attach: The column number to attach the left side of the item to
5325 * @right_attach: The column number to attach the right side of the item to
5326 * @top_attach: The row number to attach the top of the item to
5327 * @bottom_attach: The row number to attach the bottom of the item to
5329 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5330 * an item will occupy is specified by @left_attach, @right_attach,
5331 * @top_attach and @bottom_attach. These each represent the leftmost,
5332 * rightmost, uppermost and lower column and row numbers of the table.
5333 * (Columns and rows are indexed from zero).
5335 * Note that this function is not related to gtk_menu_detach().
5340 gtk_menu_attach (GtkMenu *menu,
5345 guint bottom_attach)
5347 GtkMenuShell *menu_shell;
5350 g_return_if_fail (GTK_IS_MENU (menu));
5351 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5352 parent = gtk_widget_get_parent (child);
5353 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5354 g_return_if_fail (left_attach < right_attach);
5355 g_return_if_fail (top_attach < bottom_attach);
5357 menu_shell = GTK_MENU_SHELL (menu);
5361 AttachInfo *ai = get_attach_info (child);
5363 ai->left_attach = left_attach;
5364 ai->right_attach = right_attach;
5365 ai->top_attach = top_attach;
5366 ai->bottom_attach = bottom_attach;
5368 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5370 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5372 menu_queue_resize (menu);
5376 gtk_container_child_set (GTK_CONTAINER (parent), child,
5377 "left-attach", left_attach,
5378 "right-attach", right_attach,
5379 "top-attach", top_attach,
5380 "bottom-attach", bottom_attach,
5386 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5390 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5391 "gtk-menu-popup-delay", &popup_delay,
5398 find_child_containing (GtkMenuShell *menu_shell,
5406 /* find a child which includes the area given by
5407 * left, right, top, bottom.
5409 for (list = menu_shell->priv->children; list; list = list->next)
5413 if (!_gtk_menu_item_is_selectable (list->data))
5416 get_effective_child_attach (list->data, &l, &r, &t, &b);
5418 if (l <= left && right <= r && t <= top && bottom <= b)
5419 return GTK_WIDGET (list->data);
5426 gtk_menu_move_current (GtkMenuShell *menu_shell,
5427 GtkMenuDirectionType direction)
5429 GtkMenu *menu = GTK_MENU (menu_shell);
5432 GtkWidget *match = NULL;
5434 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5438 case GTK_MENU_DIR_CHILD:
5439 direction = GTK_MENU_DIR_PARENT;
5441 case GTK_MENU_DIR_PARENT:
5442 direction = GTK_MENU_DIR_CHILD;
5448 /* use special table menu key bindings */
5449 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5451 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5453 if (direction == GTK_MENU_DIR_NEXT)
5455 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5457 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5465 for (i = 0; i < t; i++)
5467 match = find_child_containing (menu_shell,
5468 l, l + 1, i, i + 1);
5474 else if (direction == GTK_MENU_DIR_PREV)
5476 for (i = t; i > 0; i--)
5478 match = find_child_containing (menu_shell,
5479 l, l + 1, i - 1, i);
5487 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5489 match = find_child_containing (menu_shell,
5490 l, l + 1, i - 1, i);
5496 else if (direction == GTK_MENU_DIR_PARENT)
5498 /* we go one left if possible */
5500 match = find_child_containing (menu_shell,
5501 l - 1, l, t, t + 1);
5505 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5508 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5509 match = menu_shell->priv->active_menu_item;
5512 else if (direction == GTK_MENU_DIR_CHILD)
5514 /* we go one right if possible */
5515 if (r < gtk_menu_get_n_columns (menu))
5516 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5520 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5522 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5524 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5525 match = menu_shell->priv->active_menu_item;
5531 gtk_menu_shell_select_item (menu_shell, match);
5536 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5540 get_visible_size (GtkMenu *menu)
5542 GtkMenuPrivate *priv = menu->priv;
5543 GtkAllocation allocation;
5544 GtkWidget *widget = GTK_WIDGET (menu);
5545 GtkContainer *container = GTK_CONTAINER (menu);
5549 gtk_widget_get_allocation (widget, &allocation);
5550 get_menu_padding (widget, &padding);
5552 menu_height = (allocation.height -
5553 (2 * gtk_container_get_border_width (container)) -
5554 padding.top - padding.bottom);
5556 if (!priv->tearoff_active)
5558 GtkBorder arrow_border;
5560 get_arrows_border (menu, &arrow_border);
5561 menu_height -= arrow_border.top;
5562 menu_height -= arrow_border.bottom;
5568 /* Find the sensitive on-screen child containing @y, or if none,
5569 * the nearest selectable onscreen child. (%NULL if none)
5572 child_at (GtkMenu *menu,
5575 GtkMenuPrivate *priv = menu->priv;
5576 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5577 GtkWidget *child = NULL;
5578 gint child_offset = 0;
5581 gint lower, upper; /* Onscreen bounds */
5583 menu_height = get_visible_size (menu);
5584 lower = priv->scroll_offset;
5585 upper = priv->scroll_offset + menu_height;
5587 for (children = menu_shell->priv->children; children; children = children->next)
5589 if (gtk_widget_get_visible (children->data))
5591 GtkRequisition child_requisition;
5593 gtk_widget_get_preferred_size (children->data,
5594 &child_requisition, NULL);
5596 if (_gtk_menu_item_is_selectable (children->data) &&
5597 child_offset >= lower &&
5598 child_offset + child_requisition.height <= upper)
5600 child = children->data;
5602 if (child_offset + child_requisition.height > y &&
5603 !GTK_IS_TEAROFF_MENU_ITEM (child))
5607 child_offset += child_requisition.height;
5615 get_menu_height (GtkMenu *menu)
5617 GtkMenuPrivate *priv = menu->priv;
5618 GtkAllocation allocation;
5619 GtkWidget *widget = GTK_WIDGET (menu);
5623 gtk_widget_get_allocation (widget, &allocation);
5624 get_menu_padding (widget, &padding);
5626 height = allocation.height;
5627 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5628 padding.top + padding.bottom;
5630 if (!priv->tearoff_active)
5632 GtkBorder arrow_border;
5634 get_arrows_border (menu, &arrow_border);
5635 height -= arrow_border.top;
5636 height -= arrow_border.bottom;
5643 gtk_menu_real_move_scroll (GtkMenu *menu,
5646 GtkMenuPrivate *priv = menu->priv;
5647 gint page_size = get_visible_size (menu);
5648 gint end_position = get_menu_height (menu);
5649 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5653 case GTK_SCROLL_PAGE_UP:
5654 case GTK_SCROLL_PAGE_DOWN:
5658 gint child_offset = 0;
5659 gboolean old_upper_arrow_visible;
5662 if (type == GTK_SCROLL_PAGE_UP)
5667 if (menu_shell->priv->active_menu_item)
5671 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5672 &child_offset, &child_height, NULL);
5673 child_offset += child_height / 2;
5676 menu_shell->priv->ignore_enter = TRUE;
5677 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5678 old_offset = priv->scroll_offset;
5680 new_offset = priv->scroll_offset + step;
5681 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5683 gtk_menu_scroll_to (menu, new_offset);
5685 if (menu_shell->priv->active_menu_item)
5687 GtkWidget *new_child;
5688 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5689 GtkBorder arrow_border;
5690 get_arrows_border (menu, &arrow_border);
5692 if (priv->scroll_offset != old_offset)
5693 step = priv->scroll_offset - old_offset;
5695 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5697 new_child = child_at (menu, child_offset + step);
5699 gtk_menu_shell_select_item (menu_shell, new_child);
5703 case GTK_SCROLL_START:
5704 /* Ignore the enter event we might get if the pointer is on the menu */
5705 menu_shell->priv->ignore_enter = TRUE;
5706 gtk_menu_scroll_to (menu, 0);
5707 gtk_menu_shell_select_first (menu_shell, TRUE);
5709 case GTK_SCROLL_END:
5710 /* Ignore the enter event we might get if the pointer is on the menu */
5711 menu_shell->priv->ignore_enter = TRUE;
5712 gtk_menu_scroll_to (menu, end_position - page_size);
5713 _gtk_menu_shell_select_last (menu_shell, TRUE);
5722 * gtk_menu_set_monitor:
5724 * @monitor_num: the number of the monitor on which the menu should
5727 * Informs GTK+ on which monitor a menu should be popped up.
5728 * See gdk_screen_get_monitor_geometry().
5730 * This function should be called from a #GtkMenuPositionFunc
5731 * if the menu should not appear on the same monitor as the pointer.
5732 * This information can't be reliably inferred from the coordinates
5733 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5734 * these coordinates may extend beyond the monitor boundaries or even
5735 * the screen boundaries.
5740 gtk_menu_set_monitor (GtkMenu *menu,
5743 GtkMenuPrivate *priv = menu->priv;
5745 g_return_if_fail (GTK_IS_MENU (menu));
5747 priv->monitor_num = monitor_num;
5751 * gtk_menu_get_monitor:
5754 * Retrieves the number of the monitor on which to show the menu.
5756 * Returns: the number of the monitor on which the menu should
5757 * be popped up or -1, if no monitor has been set
5762 gtk_menu_get_monitor (GtkMenu *menu)
5764 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5766 return menu->priv->monitor_num;
5770 * gtk_menu_get_for_attach_widget:
5771 * @widget: a #GtkWidget
5773 * Returns a list of the menus which are attached to this widget.
5774 * This list is owned by GTK+ and must not be modified.
5776 * Return value: (element-type GtkWidget) (transfer none): the list
5777 * of menus attached to his widget.
5782 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5786 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5788 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5794 gtk_menu_grab_notify (GtkWidget *widget,
5795 gboolean was_grabbed)
5797 GtkWidget *toplevel;
5798 GtkWindowGroup *group;
5802 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5805 !gtk_widget_device_is_shadowed (widget, pointer))
5808 toplevel = gtk_widget_get_toplevel (widget);
5810 if (!GTK_IS_WINDOW (toplevel))
5813 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5814 grab = gtk_window_group_get_current_device_grab (group, pointer);
5816 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5817 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5821 * gtk_menu_set_reserve_toggle_size:
5823 * @reserve_toggle_size: whether to reserve size for toggles
5825 * Sets whether the menu should reserve space for drawing toggles
5826 * or icons, regardless of their actual presence.
5831 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5832 gboolean reserve_toggle_size)
5834 GtkMenuPrivate *priv = menu->priv;
5835 gboolean no_toggle_size;
5837 g_return_if_fail (GTK_IS_MENU (menu));
5839 no_toggle_size = !reserve_toggle_size;
5841 if (priv->no_toggle_size != no_toggle_size)
5843 priv->no_toggle_size = no_toggle_size;
5845 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5850 * gtk_menu_get_reserve_toggle_size:
5853 * Returns whether the menu reserves space for toggles and
5854 * icons, regardless of their actual presence.
5856 * Returns: Whether the menu reserves toggle space
5861 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5863 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5865 return !menu->priv->no_toggle_size;