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 "gtktearoffmenuitem.h"
108 #include "gtkwindow.h"
110 #include "gtkscrollbar.h"
111 #include "gtksettings.h"
112 #include "gtkprivate.h"
113 #include "gtkwidgetprivate.h"
115 #include "gtktypebuiltins.h"
116 #include "a11y/gtkmenuaccessible.h"
118 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
119 * extends below the submenu
122 #define MENU_SCROLL_STEP1 8
123 #define MENU_SCROLL_STEP2 15
124 #define MENU_SCROLL_FAST_ZONE 8
125 #define MENU_SCROLL_TIMEOUT1 50
126 #define MENU_SCROLL_TIMEOUT2 20
128 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
129 #define ATTACHED_MENUS "gtk-attached-menus"
131 typedef struct _GtkMenuAttachData GtkMenuAttachData;
132 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
134 struct _GtkMenuAttachData
136 GtkWidget *attach_widget;
137 GtkMenuDetachFunc detacher;
140 struct _OldGtkMenuPrivate
144 gboolean initially_pushed_in;
146 GDestroyNotify position_func_data_destroy;
148 /* info used for the table */
151 gint requested_height;
155 /* Cached layout information */
164 GtkStateFlags lower_arrow_state;
165 GtkStateFlags upper_arrow_state;
167 /* navigation region */
170 int navigation_width;
171 int navigation_height;
173 guint have_layout : 1;
174 guint seen_item_enter : 1;
175 guint have_position : 1;
176 guint ignore_button_release : 1;
177 guint no_toggle_size : 1;
180 struct _GtkMenuPopdownData
192 gint effective_left_attach;
193 gint effective_right_attach;
194 gint effective_top_attach;
195 gint effective_bottom_attach;
212 PROP_RESERVE_TOGGLE_SIZE
217 CHILD_PROP_LEFT_ATTACH,
218 CHILD_PROP_RIGHT_ATTACH,
219 CHILD_PROP_TOP_ATTACH,
220 CHILD_PROP_BOTTOM_ATTACH
223 static void gtk_menu_set_property (GObject *object,
227 static void gtk_menu_get_property (GObject *object,
231 static void gtk_menu_set_child_property(GtkContainer *container,
236 static void gtk_menu_get_child_property(GtkContainer *container,
241 static void gtk_menu_destroy (GtkWidget *widget);
242 static void gtk_menu_realize (GtkWidget *widget);
243 static void gtk_menu_unrealize (GtkWidget *widget);
244 static void gtk_menu_size_allocate (GtkWidget *widget,
245 GtkAllocation *allocation);
246 static void gtk_menu_show (GtkWidget *widget);
247 static gboolean gtk_menu_draw (GtkWidget *widget,
249 static gboolean gtk_menu_key_press (GtkWidget *widget,
251 static gboolean gtk_menu_scroll (GtkWidget *widget,
252 GdkEventScroll *event);
253 static gboolean gtk_menu_button_press (GtkWidget *widget,
254 GdkEventButton *event);
255 static gboolean gtk_menu_button_release (GtkWidget *widget,
256 GdkEventButton *event);
257 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
258 GdkEventMotion *event);
259 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
260 GdkEventCrossing *event);
261 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
262 GdkEventCrossing *event);
263 static void gtk_menu_scroll_to (GtkMenu *menu,
265 static void gtk_menu_grab_notify (GtkWidget *widget,
266 gboolean was_grabbed);
268 static void gtk_menu_stop_scrolling (GtkMenu *menu);
269 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
270 static gboolean gtk_menu_scroll_timeout (gpointer data);
271 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
272 static void gtk_menu_start_scrolling (GtkMenu *menu);
274 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
275 GtkWidget *menu_item);
276 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
277 GtkWidget *menu_item);
278 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
281 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
283 static void gtk_menu_handle_scrolling (GtkMenu *menu,
288 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
290 static void gtk_menu_style_updated (GtkWidget *widget);
291 static gboolean gtk_menu_focus (GtkWidget *widget,
292 GtkDirectionType direction);
293 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
294 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
295 GtkMenuDirectionType direction);
296 static void gtk_menu_real_move_scroll (GtkMenu *menu,
299 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
300 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
301 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
304 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
305 GtkMenuItem *menu_item,
306 GdkEventCrossing *event);
308 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
309 static void gtk_menu_show_all (GtkWidget *widget);
310 static void gtk_menu_position (GtkMenu *menu,
311 gboolean set_scroll_offset);
312 static void gtk_menu_reparent (GtkMenu *menu,
313 GtkWidget *new_parent,
315 static void gtk_menu_remove (GtkContainer *menu,
318 static void gtk_menu_update_title (GtkMenu *menu);
320 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
321 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
323 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
325 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
326 gboolean group_changed);
328 static void gtk_menu_get_preferred_width (GtkWidget *widget,
331 static void gtk_menu_get_preferred_height (GtkWidget *widget,
334 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
340 static const gchar attach_data_key[] = "gtk-menu-attach-data";
342 static guint menu_signals[LAST_SIGNAL] = { 0 };
344 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
347 menu_queue_resize (GtkMenu *menu)
349 GtkMenuPrivate *priv = menu->priv;
351 priv->have_layout = FALSE;
352 gtk_widget_queue_resize (GTK_WIDGET (menu));
356 attach_info_free (AttachInfo *info)
358 g_slice_free (AttachInfo, info);
362 get_attach_info (GtkWidget *child)
364 GObject *object = G_OBJECT (child);
365 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
369 ai = g_slice_new0 (AttachInfo);
370 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
371 (GDestroyNotify) attach_info_free);
378 is_grid_attached (AttachInfo *ai)
380 return (ai->left_attach >= 0 &&
381 ai->right_attach >= 0 &&
382 ai->top_attach >= 0 &&
383 ai->bottom_attach >= 0);
387 menu_ensure_layout (GtkMenu *menu)
389 GtkMenuPrivate *priv = menu->priv;
391 if (!priv->have_layout)
393 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
397 gint max_right_attach;
398 gint max_bottom_attach;
400 /* Find extents of gridded portion
402 max_right_attach = 1;
403 max_bottom_attach = 0;
405 for (l = menu_shell->priv->children; l; l = l->next)
407 GtkWidget *child = l->data;
408 AttachInfo *ai = get_attach_info (child);
410 if (is_grid_attached (ai))
412 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
413 max_right_attach = MAX (max_right_attach, ai->right_attach);
417 /* Find empty rows */
418 row_occupied = g_malloc0 (max_bottom_attach);
420 for (l = menu_shell->priv->children; l; l = l->next)
422 GtkWidget *child = l->data;
423 AttachInfo *ai = get_attach_info (child);
425 if (is_grid_attached (ai))
429 for (i = ai->top_attach; i < ai->bottom_attach; i++)
430 row_occupied[i] = TRUE;
434 /* Lay non-grid-items out in those rows
437 for (l = menu_shell->priv->children; l; l = l->next)
439 GtkWidget *child = l->data;
440 AttachInfo *ai = get_attach_info (child);
442 if (!is_grid_attached (ai))
444 while (current_row < max_bottom_attach && row_occupied[current_row])
447 ai->effective_left_attach = 0;
448 ai->effective_right_attach = max_right_attach;
449 ai->effective_top_attach = current_row;
450 ai->effective_bottom_attach = current_row + 1;
456 ai->effective_left_attach = ai->left_attach;
457 ai->effective_right_attach = ai->right_attach;
458 ai->effective_top_attach = ai->top_attach;
459 ai->effective_bottom_attach = ai->bottom_attach;
463 g_free (row_occupied);
465 priv->n_rows = MAX (current_row, max_bottom_attach);
466 priv->n_columns = max_right_attach;
467 priv->have_layout = TRUE;
473 gtk_menu_get_n_columns (GtkMenu *menu)
475 GtkMenuPrivate *priv = menu->priv;
477 menu_ensure_layout (menu);
479 return priv->n_columns;
483 gtk_menu_get_n_rows (GtkMenu *menu)
485 GtkMenuPrivate *priv = menu->priv;
487 menu_ensure_layout (menu);
493 get_effective_child_attach (GtkWidget *child,
499 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
502 menu_ensure_layout (menu);
504 ai = get_attach_info (child);
507 *l = ai->effective_left_attach;
509 *r = ai->effective_right_attach;
511 *t = ai->effective_top_attach;
513 *b = ai->effective_bottom_attach;
518 gtk_menu_class_init (GtkMenuClass *class)
520 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
521 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
522 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
523 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
524 GtkBindingSet *binding_set;
526 gobject_class->set_property = gtk_menu_set_property;
527 gobject_class->get_property = gtk_menu_get_property;
529 widget_class->destroy = gtk_menu_destroy;
530 widget_class->realize = gtk_menu_realize;
531 widget_class->unrealize = gtk_menu_unrealize;
532 widget_class->size_allocate = gtk_menu_size_allocate;
533 widget_class->show = gtk_menu_show;
534 widget_class->draw = gtk_menu_draw;
535 widget_class->scroll_event = gtk_menu_scroll;
536 widget_class->key_press_event = gtk_menu_key_press;
537 widget_class->button_press_event = gtk_menu_button_press;
538 widget_class->button_release_event = gtk_menu_button_release;
539 widget_class->motion_notify_event = gtk_menu_motion_notify;
540 widget_class->show_all = gtk_menu_show_all;
541 widget_class->enter_notify_event = gtk_menu_enter_notify;
542 widget_class->leave_notify_event = gtk_menu_leave_notify;
543 widget_class->style_updated = gtk_menu_style_updated;
544 widget_class->focus = gtk_menu_focus;
545 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
546 widget_class->grab_notify = gtk_menu_grab_notify;
547 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
548 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
549 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
551 container_class->remove = gtk_menu_remove;
552 container_class->get_child_property = gtk_menu_get_child_property;
553 container_class->set_child_property = gtk_menu_set_child_property;
555 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
556 menu_shell_class->deactivate = gtk_menu_deactivate;
557 menu_shell_class->select_item = gtk_menu_select_item;
558 menu_shell_class->insert = gtk_menu_real_insert;
559 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
560 menu_shell_class->move_current = gtk_menu_move_current;
563 * GtkMenu::move-scroll:
565 * @scroll_type: a #GtkScrollType
567 menu_signals[MOVE_SCROLL] =
568 g_signal_new_class_handler (I_("move-scroll"),
569 G_OBJECT_CLASS_TYPE (gobject_class),
570 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
571 G_CALLBACK (gtk_menu_real_move_scroll),
573 _gtk_marshal_VOID__ENUM,
575 GTK_TYPE_SCROLL_TYPE);
580 * The index of the currently selected menu item, or -1 if no
581 * menu item is selected.
585 g_object_class_install_property (gobject_class,
587 g_param_spec_int ("active",
589 P_("The currently selected menu item"),
591 GTK_PARAM_READWRITE));
594 * GtkMenu:accel-group:
596 * The accel group holding accelerators for the menu.
600 g_object_class_install_property (gobject_class,
602 g_param_spec_object ("accel-group",
604 P_("The accel group holding accelerators for the menu"),
605 GTK_TYPE_ACCEL_GROUP,
606 GTK_PARAM_READWRITE));
609 * GtkMenu:accel-path:
611 * An accel path used to conveniently construct accel paths of child items.
615 g_object_class_install_property (gobject_class,
617 g_param_spec_string ("accel-path",
619 P_("An accel path used to conveniently construct accel paths of child items"),
621 GTK_PARAM_READWRITE));
624 * GtkMenu:attach-widget:
626 * The widget the menu is attached to. Setting this property attaches
627 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
628 * use gtk_menu_attach_to_widget() directly.
632 g_object_class_install_property (gobject_class,
634 g_param_spec_object ("attach-widget",
636 P_("The widget the menu is attached to"),
638 GTK_PARAM_READWRITE));
640 g_object_class_install_property (gobject_class,
642 g_param_spec_string ("tearoff-title",
644 P_("A title that may be displayed by the window manager when this menu is torn-off"),
646 GTK_PARAM_READWRITE));
649 * GtkMenu:tearoff-state:
651 * A boolean that indicates whether the menu is torn-off.
655 g_object_class_install_property (gobject_class,
657 g_param_spec_boolean ("tearoff-state",
659 P_("A boolean that indicates whether the menu is torn-off"),
661 GTK_PARAM_READWRITE));
666 * The monitor the menu will be popped up on.
670 g_object_class_install_property (gobject_class,
672 g_param_spec_int ("monitor",
674 P_("The monitor the menu will be popped up on"),
676 GTK_PARAM_READWRITE));
678 gtk_widget_class_install_style_property (widget_class,
679 g_param_spec_int ("vertical-padding",
680 P_("Vertical Padding"),
681 P_("Extra space at the top and bottom of the menu"),
685 GTK_PARAM_READABLE));
688 * GtkMenu:reserve-toggle-size:
690 * A boolean that indicates whether the menu reserves space for
691 * toggles and icons, regardless of their actual presence.
693 * This property should only be changed from its default value
694 * for special-purposes such as tabular menus. Regular menus that
695 * are connected to a menu bar or context menus should reserve
696 * toggle space for consistency.
700 g_object_class_install_property (gobject_class,
701 PROP_RESERVE_TOGGLE_SIZE,
702 g_param_spec_boolean ("reserve-toggle-size",
703 P_("Reserve Toggle Size"),
704 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
706 GTK_PARAM_READWRITE));
708 gtk_widget_class_install_style_property (widget_class,
709 g_param_spec_int ("horizontal-padding",
710 P_("Horizontal Padding"),
711 P_("Extra space at the left and right edges of the menu"),
715 GTK_PARAM_READABLE));
717 gtk_widget_class_install_style_property (widget_class,
718 g_param_spec_int ("vertical-offset",
719 P_("Vertical Offset"),
720 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
724 GTK_PARAM_READABLE));
726 gtk_widget_class_install_style_property (widget_class,
727 g_param_spec_int ("horizontal-offset",
728 P_("Horizontal Offset"),
729 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
733 GTK_PARAM_READABLE));
735 gtk_widget_class_install_style_property (widget_class,
736 g_param_spec_boolean ("double-arrows",
738 P_("When scrolling, always show both arrows."),
740 GTK_PARAM_READABLE));
743 * GtkMenu:arrow-placement:
745 * Indicates where scroll arrows should be placed.
749 gtk_widget_class_install_style_property (widget_class,
750 g_param_spec_enum ("arrow-placement",
751 P_("Arrow Placement"),
752 P_("Indicates where scroll arrows should be placed"),
753 GTK_TYPE_ARROW_PLACEMENT,
755 GTK_PARAM_READABLE));
757 gtk_container_class_install_child_property (container_class,
758 CHILD_PROP_LEFT_ATTACH,
759 g_param_spec_int ("left-attach",
761 P_("The column number to attach the left side of the child to"),
763 GTK_PARAM_READWRITE));
765 gtk_container_class_install_child_property (container_class,
766 CHILD_PROP_RIGHT_ATTACH,
767 g_param_spec_int ("right-attach",
769 P_("The column number to attach the right side of the child to"),
771 GTK_PARAM_READWRITE));
773 gtk_container_class_install_child_property (container_class,
774 CHILD_PROP_TOP_ATTACH,
775 g_param_spec_int ("top-attach",
777 P_("The row number to attach the top of the child to"),
779 GTK_PARAM_READWRITE));
781 gtk_container_class_install_child_property (container_class,
782 CHILD_PROP_BOTTOM_ATTACH,
783 g_param_spec_int ("bottom-attach",
785 P_("The row number to attach the bottom of the child to"),
787 GTK_PARAM_READWRITE));
790 * GtkMenu:arrow-scaling
792 * Arbitrary constant to scale down the size of the scroll arrow.
796 gtk_widget_class_install_style_property (widget_class,
797 g_param_spec_float ("arrow-scaling",
799 P_("Arbitrary constant to scale down the size of the scroll arrow"),
801 GTK_PARAM_READABLE));
803 binding_set = gtk_binding_set_by_class (class);
804 gtk_binding_entry_add_signal (binding_set,
806 I_("move-current"), 1,
807 GTK_TYPE_MENU_DIRECTION_TYPE,
809 gtk_binding_entry_add_signal (binding_set,
812 GTK_TYPE_MENU_DIRECTION_TYPE,
814 gtk_binding_entry_add_signal (binding_set,
817 GTK_TYPE_MENU_DIRECTION_TYPE,
819 gtk_binding_entry_add_signal (binding_set,
822 GTK_TYPE_MENU_DIRECTION_TYPE,
824 gtk_binding_entry_add_signal (binding_set,
827 GTK_TYPE_MENU_DIRECTION_TYPE,
828 GTK_MENU_DIR_PARENT);
829 gtk_binding_entry_add_signal (binding_set,
832 GTK_TYPE_MENU_DIRECTION_TYPE,
833 GTK_MENU_DIR_PARENT);
834 gtk_binding_entry_add_signal (binding_set,
837 GTK_TYPE_MENU_DIRECTION_TYPE,
839 gtk_binding_entry_add_signal (binding_set,
842 GTK_TYPE_MENU_DIRECTION_TYPE,
844 gtk_binding_entry_add_signal (binding_set,
847 GTK_TYPE_SCROLL_TYPE,
849 gtk_binding_entry_add_signal (binding_set,
852 GTK_TYPE_SCROLL_TYPE,
854 gtk_binding_entry_add_signal (binding_set,
857 GTK_TYPE_SCROLL_TYPE,
859 gtk_binding_entry_add_signal (binding_set,
862 GTK_TYPE_SCROLL_TYPE,
864 gtk_binding_entry_add_signal (binding_set,
867 GTK_TYPE_SCROLL_TYPE,
869 gtk_binding_entry_add_signal (binding_set,
870 GDK_KEY_KP_Page_Up, 0,
872 GTK_TYPE_SCROLL_TYPE,
874 gtk_binding_entry_add_signal (binding_set,
875 GDK_KEY_Page_Down, 0,
877 GTK_TYPE_SCROLL_TYPE,
878 GTK_SCROLL_PAGE_DOWN);
879 gtk_binding_entry_add_signal (binding_set,
880 GDK_KEY_KP_Page_Down, 0,
882 GTK_TYPE_SCROLL_TYPE,
883 GTK_SCROLL_PAGE_DOWN);
885 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
887 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
892 gtk_menu_set_property (GObject *object,
897 GtkMenu *menu = GTK_MENU (object);
902 gtk_menu_set_active (menu, g_value_get_int (value));
904 case PROP_ACCEL_GROUP:
905 gtk_menu_set_accel_group (menu, g_value_get_object (value));
907 case PROP_ACCEL_PATH:
908 gtk_menu_set_accel_path (menu, g_value_get_string (value));
910 case PROP_ATTACH_WIDGET:
914 widget = gtk_menu_get_attach_widget (menu);
916 gtk_menu_detach (menu);
918 widget = (GtkWidget*) g_value_get_object (value);
920 gtk_menu_attach_to_widget (menu, widget, NULL);
923 case PROP_TEAROFF_STATE:
924 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
926 case PROP_TEAROFF_TITLE:
927 gtk_menu_set_title (menu, g_value_get_string (value));
930 gtk_menu_set_monitor (menu, g_value_get_int (value));
932 case PROP_RESERVE_TOGGLE_SIZE:
933 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
936 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
942 gtk_menu_get_property (GObject *object,
947 GtkMenu *menu = GTK_MENU (object);
952 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
954 case PROP_ACCEL_GROUP:
955 g_value_set_object (value, gtk_menu_get_accel_group (menu));
957 case PROP_ACCEL_PATH:
958 g_value_set_string (value, gtk_menu_get_accel_path (menu));
960 case PROP_ATTACH_WIDGET:
961 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
963 case PROP_TEAROFF_STATE:
964 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
966 case PROP_TEAROFF_TITLE:
967 g_value_set_string (value, gtk_menu_get_title (menu));
970 g_value_set_int (value, gtk_menu_get_monitor (menu));
972 case PROP_RESERVE_TOGGLE_SIZE:
973 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
976 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
982 gtk_menu_set_child_property (GtkContainer *container,
988 GtkMenu *menu = GTK_MENU (container);
989 AttachInfo *ai = get_attach_info (child);
993 case CHILD_PROP_LEFT_ATTACH:
994 ai->left_attach = g_value_get_int (value);
996 case CHILD_PROP_RIGHT_ATTACH:
997 ai->right_attach = g_value_get_int (value);
999 case CHILD_PROP_TOP_ATTACH:
1000 ai->top_attach = g_value_get_int (value);
1002 case CHILD_PROP_BOTTOM_ATTACH:
1003 ai->bottom_attach = g_value_get_int (value);
1007 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1011 menu_queue_resize (menu);
1015 gtk_menu_get_child_property (GtkContainer *container,
1021 AttachInfo *ai = get_attach_info (child);
1023 switch (property_id)
1025 case CHILD_PROP_LEFT_ATTACH:
1026 g_value_set_int (value, ai->left_attach);
1028 case CHILD_PROP_RIGHT_ATTACH:
1029 g_value_set_int (value, ai->right_attach);
1031 case CHILD_PROP_TOP_ATTACH:
1032 g_value_set_int (value, ai->top_attach);
1034 case CHILD_PROP_BOTTOM_ATTACH:
1035 g_value_set_int (value, ai->bottom_attach);
1039 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1045 gtk_menu_window_event (GtkWidget *window,
1049 gboolean handled = FALSE;
1051 g_object_ref (window);
1052 g_object_ref (menu);
1054 switch (event->type)
1057 case GDK_KEY_RELEASE:
1058 handled = gtk_widget_event (menu, event);
1064 g_object_unref (window);
1065 g_object_unref (menu);
1071 gtk_menu_init (GtkMenu *menu)
1073 GtkMenuPrivate *priv;
1074 GtkStyleContext *context;
1076 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1080 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1081 "type", GTK_WINDOW_POPUP,
1084 "signal::event", gtk_menu_window_event, menu,
1085 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1087 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1088 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1090 /* Refloat the menu, so that reference counting for the menu isn't
1091 * affected by it being a child of the toplevel
1093 g_object_force_floating (G_OBJECT (menu));
1094 priv->needs_destruction_ref = TRUE;
1096 priv->monitor_num = -1;
1098 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1099 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1103 gtk_menu_destroy (GtkWidget *widget)
1105 GtkMenu *menu = GTK_MENU (widget);
1106 GtkMenuPrivate *priv = menu->priv;
1107 GtkMenuAttachData *data;
1109 gtk_menu_remove_scroll_timeout (menu);
1111 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1113 gtk_menu_detach (menu);
1115 gtk_menu_stop_navigating_submenu (menu);
1117 if (priv->old_active_menu_item)
1118 g_clear_object (&priv->old_active_menu_item);
1120 /* Add back the reference count for being a child */
1121 if (priv->needs_destruction_ref)
1123 priv->needs_destruction_ref = FALSE;
1124 g_object_ref (widget);
1127 if (priv->accel_group)
1128 g_clear_object (&priv->accel_group);
1131 gtk_widget_destroy (priv->toplevel);
1133 if (priv->tearoff_window)
1134 gtk_widget_destroy (priv->tearoff_window);
1138 g_free (priv->heights);
1139 priv->heights = NULL;
1144 g_free (priv->title);
1148 if (priv->position_func_data_destroy)
1150 priv->position_func_data_destroy (priv->position_func_data);
1151 priv->position_func_data = NULL;
1152 priv->position_func_data_destroy = NULL;
1155 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1159 menu_change_screen (GtkMenu *menu,
1160 GdkScreen *new_screen)
1162 GtkMenuPrivate *priv = menu->priv;
1164 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1166 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1172 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1173 gtk_menu_position (menu, TRUE);
1176 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1177 priv->monitor_num = -1;
1181 attach_widget_screen_changed (GtkWidget *attach_widget,
1182 GdkScreen *previous_screen,
1185 if (gtk_widget_has_screen (attach_widget) &&
1186 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1187 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1191 * gtk_menu_attach_to_widget:
1193 * @attach_widget: the #GtkWidget that the menu will be attached to
1194 * @detacher: (scope async)(allow-none): the user supplied callback function
1195 * that will be called when the menu calls gtk_menu_detach()
1197 * Attaches the menu to the widget and provides a callback function
1198 * that will be invoked when the menu calls gtk_menu_detach() during
1202 gtk_menu_attach_to_widget (GtkMenu *menu,
1203 GtkWidget *attach_widget,
1204 GtkMenuDetachFunc detacher)
1206 GtkMenuAttachData *data;
1209 g_return_if_fail (GTK_IS_MENU (menu));
1210 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1212 /* keep this function in sync with gtk_widget_set_parent() */
1213 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1216 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1217 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1221 g_object_ref_sink (menu);
1223 data = g_slice_new (GtkMenuAttachData);
1224 data->attach_widget = attach_widget;
1226 g_signal_connect (attach_widget, "screen-changed",
1227 G_CALLBACK (attach_widget_screen_changed), menu);
1228 attach_widget_screen_changed (attach_widget, NULL, menu);
1230 data->detacher = detacher;
1231 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1232 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1233 if (!g_list_find (list, menu))
1234 list = g_list_prepend (list, menu);
1236 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1237 (GDestroyNotify) g_list_free);
1239 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1240 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1242 /* we don't need to set the style here, since
1243 * we are a toplevel widget.
1246 /* Fallback title for menu comes from attach widget */
1247 gtk_menu_update_title (menu);
1249 g_object_notify (G_OBJECT (menu), "attach-widget");
1253 * gtk_menu_get_attach_widget:
1256 * Returns the #GtkWidget that the menu is attached to.
1258 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1261 gtk_menu_get_attach_widget (GtkMenu *menu)
1263 GtkMenuAttachData *data;
1265 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1267 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1269 return data->attach_widget;
1277 * Detaches the menu from the widget to which it had been attached.
1278 * This function will call the callback function, @detacher, provided
1279 * when the gtk_menu_attach_to_widget() function was called.
1282 gtk_menu_detach (GtkMenu *menu)
1284 GtkMenuAttachData *data;
1287 g_return_if_fail (GTK_IS_MENU (menu));
1289 /* keep this function in sync with gtk_widget_unparent() */
1290 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1293 g_warning ("gtk_menu_detach(): menu is not attached");
1296 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1298 g_signal_handlers_disconnect_by_func (data->attach_widget,
1299 (gpointer) attach_widget_screen_changed,
1303 data->detacher (data->attach_widget, menu);
1304 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1305 list = g_list_remove (list, menu);
1307 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1308 (GDestroyNotify) g_list_free);
1310 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1312 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1313 gtk_widget_unrealize (GTK_WIDGET (menu));
1315 g_slice_free (GtkMenuAttachData, data);
1317 /* Fallback title for menu comes from attach widget */
1318 gtk_menu_update_title (menu);
1320 g_object_unref (menu);
1324 gtk_menu_remove (GtkContainer *container,
1327 GtkMenu *menu = GTK_MENU (container);
1328 GtkMenuPrivate *priv = menu->priv;
1330 /* Clear out old_active_menu_item if it matches the item we are removing */
1331 if (priv->old_active_menu_item == widget)
1332 g_clear_object (&priv->old_active_menu_item);
1334 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1336 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1338 menu_queue_resize (menu);
1344 * Creates a new #GtkMenu
1346 * Returns: a new #GtkMenu
1351 return g_object_new (GTK_TYPE_MENU, NULL);
1355 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1359 GtkMenu *menu = GTK_MENU (menu_shell);
1360 GtkMenuPrivate *priv = menu->priv;
1361 AttachInfo *ai = get_attach_info (child);
1363 ai->left_attach = -1;
1364 ai->right_attach = -1;
1365 ai->top_attach = -1;
1366 ai->bottom_attach = -1;
1368 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1369 gtk_widget_set_parent_window (child, priv->bin_window);
1371 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1373 menu_queue_resize (menu);
1377 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1379 GtkMenuPrivate *priv = menu->priv;
1385 cairo_surface_t *surface;
1386 cairo_pattern_t *pattern;
1389 priv->tearoff_active = FALSE;
1390 priv->saved_scroll_offset = priv->scroll_offset;
1392 window = gtk_widget_get_window (priv->tearoff_window);
1393 width = gdk_window_get_width (window);
1394 height = gdk_window_get_height (window);
1396 surface = gdk_window_create_similar_surface (window,
1397 CAIRO_CONTENT_COLOR,
1401 cr = cairo_create (surface);
1402 gdk_cairo_set_source_window (cr, window, 0, 0);
1406 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1408 pattern = cairo_pattern_create_for_surface (surface);
1409 gdk_window_set_background_pattern (window, pattern);
1411 cairo_pattern_destroy (pattern);
1412 cairo_surface_destroy (surface);
1417 popup_grab_on_window (GdkWindow *window,
1418 GdkDevice *keyboard,
1420 guint32 activate_time)
1423 gdk_device_grab (keyboard, window,
1424 GDK_OWNERSHIP_WINDOW, TRUE,
1425 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1426 NULL, activate_time) != GDK_GRAB_SUCCESS)
1430 gdk_device_grab (pointer, window,
1431 GDK_OWNERSHIP_WINDOW, TRUE,
1432 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1433 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1434 GDK_POINTER_MOTION_MASK,
1435 NULL, activate_time) != GDK_GRAB_SUCCESS)
1438 gdk_device_ungrab (keyboard, activate_time);
1447 * gtk_menu_popup_for_device:
1449 * @device: (allow-none): a #GdkDevice
1450 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1451 * menu item, or %NULL
1452 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1453 * the popup, or %NULL
1454 * @func: (allow-none): a user supplied function used to position the menu,
1456 * @data: (allow-none): user supplied data to be passed to @func
1457 * @destroy: (allow-none): destroy notify for @data
1458 * @button: the mouse button which was pressed to initiate the event
1459 * @activate_time: the time at which the activation event occurred
1461 * Displays a menu and makes it available for selection.
1463 * Applications can use this function to display context-sensitive menus,
1464 * and will typically supply %NULL for the @parent_menu_shell,
1465 * @parent_menu_item, @func, @data and @destroy parameters. The default
1466 * menu positioning function will position the menu at the current position
1467 * of @device (or its corresponding pointer).
1469 * The @button parameter should be the mouse button pressed to initiate
1470 * the menu popup. If the menu popup was initiated by something other than
1471 * a mouse button press, such as a mouse button release or a keypress,
1472 * @button should be 0.
1474 * The @activate_time parameter is used to conflict-resolve initiation of
1475 * concurrent requests for mouse/keyboard grab requests. To function
1476 * properly, this needs to be the time stamp of the user event (such as
1477 * a mouse click or key press) that caused the initiation of the popup.
1478 * Only if no such event is available, gtk_get_current_event_time() can
1484 gtk_menu_popup_for_device (GtkMenu *menu,
1486 GtkWidget *parent_menu_shell,
1487 GtkWidget *parent_menu_item,
1488 GtkMenuPositionFunc func,
1490 GDestroyNotify destroy,
1492 guint32 activate_time)
1494 GtkMenuPrivate *priv = menu->priv;
1496 GtkWidget *xgrab_shell;
1498 GdkEvent *current_event;
1499 GtkMenuShell *menu_shell;
1500 gboolean grab_keyboard;
1501 GtkWidget *parent_toplevel;
1502 GdkDevice *keyboard, *pointer;
1504 g_return_if_fail (GTK_IS_MENU (menu));
1505 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1508 device = gtk_get_current_event_device ();
1512 GdkDisplay *display;
1513 GdkDeviceManager *device_manager;
1516 display = gtk_widget_get_display (GTK_WIDGET (menu));
1517 device_manager = gdk_display_get_device_manager (display);
1518 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1520 device = devices->data;
1522 g_list_free (devices);
1525 widget = GTK_WIDGET (menu);
1526 menu_shell = GTK_MENU_SHELL (menu);
1528 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1531 pointer = gdk_device_get_associated_device (device);
1536 keyboard = gdk_device_get_associated_device (device);
1539 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1541 priv->seen_item_enter = FALSE;
1543 /* Find the last viewable ancestor, and make an X grab on it
1545 parent = GTK_WIDGET (menu);
1549 gboolean viewable = TRUE;
1550 GtkWidget *tmp = parent;
1554 if (!gtk_widget_get_mapped (tmp))
1559 tmp = gtk_widget_get_parent (tmp);
1563 xgrab_shell = parent;
1565 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1568 /* We want to receive events generated when we map the menu;
1569 * unfortunately, since there is probably already an implicit
1570 * grab in place from the button that the user used to pop up
1571 * the menu, we won't receive then -- in particular, the EnterNotify
1572 * when the menu pops up under the pointer.
1574 * If we are grabbing on a parent menu shell, no problem; just grab
1575 * on that menu shell first before popping up the window with
1576 * owner_events = TRUE.
1578 * When grabbing on the menu itself, things get more convoluted --
1579 * we do an explicit grab on a specially created window with
1580 * owner_events = TRUE, which we override further down with a
1581 * grab on the menu. (We can't grab on the menu until it is mapped;
1582 * we probably could just leave the grab on the other window,
1583 * with a little reorganization of the code in gtkmenu*).
1585 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1586 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1591 if (xgrab_shell && xgrab_shell != widget)
1593 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1595 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1596 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1601 GdkWindow *transfer_window;
1603 xgrab_shell = widget;
1604 transfer_window = menu_grab_transfer_window_get (menu);
1605 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1607 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1608 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1612 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1614 /* We failed to make our pointer/keyboard grab.
1615 * Rather than leaving the user with a stuck up window,
1616 * we just abort here. Presumably the user will try again.
1618 menu_shell->priv->parent_menu_shell = NULL;
1619 menu_grab_transfer_window_destroy (menu);
1623 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1624 menu_shell->priv->active = TRUE;
1625 menu_shell->priv->button = button;
1627 /* If we are popping up the menu from something other than, a button
1628 * press then, as a heuristic, we ignore enter events for the menu
1629 * until we get a MOTION_NOTIFY.
1632 current_event = gtk_get_current_event ();
1635 if ((current_event->type != GDK_BUTTON_PRESS) &&
1636 (current_event->type != GDK_ENTER_NOTIFY))
1637 menu_shell->priv->ignore_enter = TRUE;
1639 gdk_event_free (current_event);
1642 menu_shell->priv->ignore_enter = TRUE;
1646 gtk_menu_tearoff_bg_copy (menu);
1648 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1651 parent_toplevel = NULL;
1652 if (parent_menu_shell)
1653 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1654 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1656 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1658 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1661 /* Set transient for to get the right window group and parent */
1662 if (GTK_IS_WINDOW (parent_toplevel))
1663 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1664 GTK_WINDOW (parent_toplevel));
1666 priv->parent_menu_item = parent_menu_item;
1667 priv->position_func = func;
1668 priv->position_func_data = data;
1669 priv->position_func_data_destroy = destroy;
1670 menu_shell->priv->activate_time = activate_time;
1672 /* We need to show the menu here rather in the init function
1673 * because code expects to be able to tell if the menu is onscreen
1674 * by looking at gtk_widget_get_visible (menu)
1676 gtk_widget_show (GTK_WIDGET (menu));
1678 /* Position the menu, possibly changing the size request
1680 gtk_menu_position (menu, TRUE);
1682 /* Compute the size of the toplevel and realize it so we
1683 * can scroll correctly.
1685 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1687 GtkRequisition tmp_request;
1688 GtkAllocation tmp_allocation = { 0, };
1690 /* Instead of trusting the menu position function to queue a
1691 * resize when the menu goes out of bounds, invalidate the cached
1694 gtk_widget_queue_resize (GTK_WIDGET (menu));
1695 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1697 tmp_allocation.width = tmp_request.width;
1698 tmp_allocation.height = tmp_request.height;
1700 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1702 gtk_widget_realize (priv->toplevel);
1705 gtk_menu_scroll_to (menu, priv->scroll_offset);
1707 /* if no item is selected, select the first one */
1708 if (!menu_shell->priv->active_menu_item)
1710 gboolean touchscreen_mode;
1712 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1713 "gtk-touchscreen-mode", &touchscreen_mode,
1716 if (touchscreen_mode)
1717 gtk_menu_shell_select_first (menu_shell, TRUE);
1720 /* Once everything is set up correctly, map the toplevel */
1721 gtk_widget_show (priv->toplevel);
1723 if (xgrab_shell == widget)
1724 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1726 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1728 if (parent_menu_shell)
1730 gboolean keyboard_mode;
1732 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1733 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1735 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1736 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1738 _gtk_menu_shell_update_mnemonics (menu_shell);
1742 * gtk_menu_popup: (skip)
1744 * @parent_menu_shell: (allow-none): the menu shell containing the
1745 * triggering menu item, or %NULL
1746 * @parent_menu_item: (allow-none): the menu item whose activation
1747 * triggered the popup, or %NULL
1748 * @func: (allow-none): a user supplied function used to position
1749 * the menu, or %NULL
1750 * @data: user supplied data to be passed to @func.
1751 * @button: the mouse button which was pressed to initiate the event.
1752 * @activate_time: the time at which the activation event occurred.
1754 * Displays a menu and makes it available for selection.
1756 * Applications can use this function to display context-sensitive
1757 * menus, and will typically supply %NULL for the @parent_menu_shell,
1758 * @parent_menu_item, @func and @data parameters. The default menu
1759 * positioning function will position the menu at the current mouse
1762 * The @button parameter should be the mouse button pressed to initiate
1763 * the menu popup. If the menu popup was initiated by something other
1764 * than a mouse button press, such as a mouse button release or a keypress,
1765 * @button should be 0.
1767 * The @activate_time parameter is used to conflict-resolve initiation
1768 * of concurrent requests for mouse/keyboard grab requests. To function
1769 * properly, this needs to be the timestamp of the user event (such as
1770 * a mouse click or key press) that caused the initiation of the popup.
1771 * Only if no such event is available, gtk_get_current_event_time() can
1775 gtk_menu_popup (GtkMenu *menu,
1776 GtkWidget *parent_menu_shell,
1777 GtkWidget *parent_menu_item,
1778 GtkMenuPositionFunc func,
1781 guint32 activate_time)
1783 g_return_if_fail (GTK_IS_MENU (menu));
1785 gtk_menu_popup_for_device (menu,
1790 button, activate_time);
1797 * Removes the menu from the screen.
1800 gtk_menu_popdown (GtkMenu *menu)
1802 GtkMenuPrivate *priv;
1803 GtkMenuShell *menu_shell;
1806 g_return_if_fail (GTK_IS_MENU (menu));
1808 menu_shell = GTK_MENU_SHELL (menu);
1811 menu_shell->priv->parent_menu_shell = NULL;
1812 menu_shell->priv->active = FALSE;
1813 menu_shell->priv->ignore_enter = FALSE;
1815 priv->have_position = FALSE;
1817 gtk_menu_stop_scrolling (menu);
1818 gtk_menu_stop_navigating_submenu (menu);
1820 if (menu_shell->priv->active_menu_item)
1822 if (priv->old_active_menu_item)
1823 g_object_unref (priv->old_active_menu_item);
1824 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1825 g_object_ref (priv->old_active_menu_item);
1828 gtk_menu_shell_deselect (menu_shell);
1830 /* The X Grab, if present, will automatically be removed
1831 * when we hide the window
1835 gtk_widget_hide (priv->toplevel);
1836 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1839 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1843 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1845 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1847 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1851 /* We popped up the menu from the tearoff, so we need to
1852 * release the grab - we aren't actually hiding the menu.
1854 if (menu_shell->priv->have_xgrab && pointer)
1856 GdkDevice *keyboard;
1858 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1859 keyboard = gdk_device_get_associated_device (pointer);
1862 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1866 /* gtk_menu_popdown is called each time a menu item is selected from
1867 * a torn off menu. Only scroll back to the saved position if the
1868 * non-tearoff menu was popped down.
1870 if (!priv->tearoff_active)
1871 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1872 priv->tearoff_active = TRUE;
1875 gtk_widget_hide (GTK_WIDGET (menu));
1877 menu_shell->priv->have_xgrab = FALSE;
1880 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1882 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1884 menu_grab_transfer_window_destroy (menu);
1888 * gtk_menu_get_active:
1891 * Returns the selected menu item from the menu. This is used by the
1894 * Returns: (transfer none): the #GtkMenuItem that was last selected
1895 * in the menu. If a selection has not yet been made, the
1896 * first menu item is selected.
1899 gtk_menu_get_active (GtkMenu *menu)
1901 GtkMenuPrivate *priv = menu->priv;
1905 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1907 if (!priv->old_active_menu_item)
1910 children = GTK_MENU_SHELL (menu)->priv->children;
1914 child = children->data;
1915 children = children->next;
1917 if (gtk_bin_get_child (GTK_BIN (child)))
1922 priv->old_active_menu_item = child;
1923 if (priv->old_active_menu_item)
1924 g_object_ref (priv->old_active_menu_item);
1927 return priv->old_active_menu_item;
1931 * gtk_menu_set_active:
1933 * @index: the index of the menu item to select. Iindex values are
1936 * Selects the specified menu item within the menu. This is used by
1937 * the #GtkOptionMenu and should not be used by anyone else.
1940 gtk_menu_set_active (GtkMenu *menu,
1943 GtkMenuPrivate *priv = menu->priv;
1947 g_return_if_fail (GTK_IS_MENU (menu));
1949 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1952 child = tmp_list->data;
1953 if (gtk_bin_get_child (GTK_BIN (child)))
1955 if (priv->old_active_menu_item)
1956 g_object_unref (priv->old_active_menu_item);
1957 priv->old_active_menu_item = child;
1958 g_object_ref (priv->old_active_menu_item);
1964 * gtk_menu_set_accel_group:
1966 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1969 * Set the #GtkAccelGroup which holds global accelerators for the
1970 * menu. This accelerator group needs to also be added to all windows
1971 * that this menu is being used in with gtk_window_add_accel_group(),
1972 * in order for those windows to support all the accelerators
1973 * contained in this group.
1976 gtk_menu_set_accel_group (GtkMenu *menu,
1977 GtkAccelGroup *accel_group)
1979 GtkMenuPrivate *priv = menu->priv;
1980 g_return_if_fail (GTK_IS_MENU (menu));
1982 if (priv->accel_group != accel_group)
1984 if (priv->accel_group)
1985 g_object_unref (priv->accel_group);
1986 priv->accel_group = accel_group;
1987 if (priv->accel_group)
1988 g_object_ref (priv->accel_group);
1989 _gtk_menu_refresh_accel_paths (menu, TRUE);
1994 * gtk_menu_get_accel_group:
1997 * Gets the #GtkAccelGroup which holds global accelerators for the
1998 * menu. See gtk_menu_set_accel_group().
2000 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
2003 gtk_menu_get_accel_group (GtkMenu *menu)
2005 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2007 return menu->priv->accel_group;
2011 gtk_menu_real_can_activate_accel (GtkWidget *widget,
2014 /* Menu items chain here to figure whether they can activate their
2015 * accelerators. Unlike ordinary widgets, menus allow accel
2016 * activation even if invisible since that's the usual case for
2017 * submenus/popup-menus. however, the state of the attach widget
2018 * affects the "activeness" of the menu.
2020 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
2023 return gtk_widget_can_activate_accel (awidget, signal_id);
2025 return gtk_widget_is_sensitive (widget);
2029 * gtk_menu_set_accel_path
2030 * @menu: a valid #GtkMenu
2031 * @accel_path: (allow-none): a valid accelerator path
2033 * Sets an accelerator path for this menu from which accelerator paths
2034 * for its immediate children, its menu items, can be constructed.
2035 * The main purpose of this function is to spare the programmer the
2036 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2037 * each menu item that should support runtime user changable accelerators.
2038 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2039 * each menu item of this menu, that contains a label describing its
2040 * purpose, automatically gets an accel path assigned.
2042 * For example, a menu containing menu items "New" and "Exit", will, after
2043 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2044 * has been called, assign its items the accel paths:
2045 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2047 * Assigning accel paths to menu items then enables the user to change
2048 * their accelerators at runtime. More details about accelerator paths
2049 * and their default setups can be found at gtk_accel_map_add_entry().
2051 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2052 * if you pass a static string, you can save some memory by interning
2053 * it first with g_intern_static_string().
2056 gtk_menu_set_accel_path (GtkMenu *menu,
2057 const gchar *accel_path)
2059 GtkMenuPrivate *priv = menu->priv;
2060 g_return_if_fail (GTK_IS_MENU (menu));
2063 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2065 /* FIXME: accel_path should be defined as const gchar* */
2066 priv->accel_path = (gchar*)g_intern_string (accel_path);
2067 if (priv->accel_path)
2068 _gtk_menu_refresh_accel_paths (menu, FALSE);
2072 * gtk_menu_get_accel_path
2073 * @menu: a valid #GtkMenu
2075 * Retrieves the accelerator path set on the menu.
2077 * Returns: the accelerator path set on the menu.
2082 gtk_menu_get_accel_path (GtkMenu *menu)
2084 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2086 return menu->priv->accel_path;
2091 gboolean group_changed;
2095 refresh_accel_paths_foreach (GtkWidget *widget,
2098 GtkMenuPrivate *priv;
2099 AccelPropagation *prop = data;
2101 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2103 priv = prop->menu->priv;
2104 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2107 prop->group_changed);
2112 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2113 gboolean group_changed)
2115 GtkMenuPrivate *priv = menu->priv;
2116 g_return_if_fail (GTK_IS_MENU (menu));
2118 if (priv->accel_path && priv->accel_group)
2120 AccelPropagation prop;
2123 prop.group_changed = group_changed;
2124 gtk_container_foreach (GTK_CONTAINER (menu),
2125 refresh_accel_paths_foreach,
2131 * gtk_menu_reposition:
2134 * Repositions the menu according to its position function.
2137 gtk_menu_reposition (GtkMenu *menu)
2139 g_return_if_fail (GTK_IS_MENU (menu));
2141 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2142 gtk_menu_position (menu, FALSE);
2146 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2151 value = gtk_adjustment_get_value (adjustment);
2152 if (menu->priv->scroll_offset != value)
2153 gtk_menu_scroll_to (menu, value);
2157 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2160 GtkMenuPrivate *priv = menu->priv;
2161 GdkGeometry geometry_hints;
2163 if (!priv->tearoff_window)
2166 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2168 GtkRequisition requisition;
2170 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2171 &requisition, NULL);
2172 width += requisition.width;
2175 geometry_hints.min_width = width;
2176 geometry_hints.max_width = width;
2178 geometry_hints.min_height = 0;
2179 geometry_hints.max_height = priv->requested_height;
2181 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2184 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2188 gtk_menu_update_title (GtkMenu *menu)
2190 GtkMenuPrivate *priv = menu->priv;
2192 if (priv->tearoff_window)
2195 GtkWidget *attach_widget;
2197 title = gtk_menu_get_title (menu);
2200 attach_widget = gtk_menu_get_attach_widget (menu);
2201 if (GTK_IS_MENU_ITEM (attach_widget))
2203 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2204 if (GTK_IS_LABEL (child))
2205 title = gtk_label_get_text (GTK_LABEL (child));
2210 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2215 gtk_menu_get_toplevel (GtkWidget *menu)
2217 GtkWidget *attach, *toplevel;
2219 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2221 if (GTK_IS_MENU_ITEM (attach))
2222 attach = gtk_widget_get_parent (attach);
2224 if (GTK_IS_MENU (attach))
2225 return gtk_menu_get_toplevel (attach);
2226 else if (GTK_IS_WIDGET (attach))
2228 toplevel = gtk_widget_get_toplevel (attach);
2229 if (gtk_widget_is_toplevel (toplevel))
2237 tearoff_window_destroyed (GtkWidget *widget,
2240 gtk_menu_set_tearoff_state (menu, FALSE);
2244 * gtk_menu_set_tearoff_state:
2246 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2248 * Changes the tearoff state of the menu. A menu is normally
2249 * displayed as drop down menu which persists as long as the menu is
2250 * active. It can also be displayed as a tearoff menu which persists
2251 * until it is closed or reattached.
2254 gtk_menu_set_tearoff_state (GtkMenu *menu,
2257 GtkMenuPrivate *priv = menu->priv;
2260 g_return_if_fail (GTK_IS_MENU (menu));
2262 if (priv->torn_off != torn_off)
2264 priv->torn_off = torn_off;
2265 priv->tearoff_active = torn_off;
2269 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2270 gtk_menu_popdown (menu);
2272 if (!priv->tearoff_window)
2274 GtkWidget *toplevel;
2276 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2277 "type", GTK_WINDOW_TOPLEVEL,
2278 "screen", gtk_widget_get_screen (priv->toplevel),
2279 "app-paintable", TRUE,
2282 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2283 GDK_WINDOW_TYPE_HINT_MENU);
2284 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2285 g_signal_connect (priv->tearoff_window, "destroy",
2286 G_CALLBACK (tearoff_window_destroyed), menu);
2287 g_signal_connect (priv->tearoff_window, "event",
2288 G_CALLBACK (gtk_menu_window_event), menu);
2290 gtk_menu_update_title (menu);
2292 gtk_widget_realize (priv->tearoff_window);
2294 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2295 if (toplevel != NULL)
2296 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2297 GTK_WINDOW (toplevel));
2299 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2300 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2301 priv->tearoff_hbox);
2303 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2304 priv->tearoff_adjustment = gtk_adjustment_new (0,
2305 0, priv->requested_height,
2309 g_object_connect (priv->tearoff_adjustment,
2310 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2312 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2314 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2315 priv->tearoff_scrollbar,
2318 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2319 gtk_widget_show (priv->tearoff_scrollbar);
2321 gtk_widget_show (priv->tearoff_hbox);
2324 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2326 /* Update menu->requisition */
2327 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2329 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2331 gtk_widget_realize (priv->tearoff_window);
2332 gtk_menu_position (menu, TRUE);
2334 gtk_widget_show (GTK_WIDGET (menu));
2335 gtk_widget_show (priv->tearoff_window);
2337 gtk_menu_scroll_to (menu, 0);
2342 gtk_widget_hide (GTK_WIDGET (menu));
2343 gtk_widget_hide (priv->tearoff_window);
2344 if (GTK_IS_CONTAINER (priv->toplevel))
2345 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2346 gtk_widget_destroy (priv->tearoff_window);
2348 priv->tearoff_window = NULL;
2349 priv->tearoff_hbox = NULL;
2350 priv->tearoff_scrollbar = NULL;
2351 priv->tearoff_adjustment = NULL;
2354 g_object_notify (G_OBJECT (menu), "tearoff-state");
2359 * gtk_menu_get_tearoff_state:
2362 * Returns whether the menu is torn off.
2363 * See gtk_menu_set_tearoff_state().
2365 * Return value: %TRUE if the menu is currently torn off.
2368 gtk_menu_get_tearoff_state (GtkMenu *menu)
2370 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2372 return menu->priv->torn_off;
2376 * gtk_menu_set_title:
2378 * @title: a string containing the title for the menu
2380 * Sets the title string for the menu.
2382 * The title is displayed when the menu is shown as a tearoff
2383 * menu. If @title is %NULL, the menu will see if it is attached
2384 * to a parent menu item, and if so it will try to use the same
2385 * text as that menu item's label.
2388 gtk_menu_set_title (GtkMenu *menu,
2391 GtkMenuPrivate *priv = menu->priv;
2394 g_return_if_fail (GTK_IS_MENU (menu));
2396 old_title = priv->title;
2397 priv->title = g_strdup (title);
2400 gtk_menu_update_title (menu);
2401 g_object_notify (G_OBJECT (menu), "tearoff-title");
2405 * gtk_menu_get_title:
2408 * Returns the title of the menu. See gtk_menu_set_title().
2410 * Return value: the title of the menu, or %NULL if the menu
2411 * has no title set on it. This string is owned by GTK+
2412 * and should not be modified or freed.
2415 gtk_menu_get_title (GtkMenu *menu)
2417 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2419 return menu->priv->title;
2423 * gtk_menu_reorder_child:
2425 * @child: the #GtkMenuItem to move
2426 * @position: the new position to place @child.
2427 * Positions are numbered from 0 to n - 1
2429 * Moves @child to a new @position in the list of @menu
2433 gtk_menu_reorder_child (GtkMenu *menu,
2437 GtkMenuShell *menu_shell;
2439 g_return_if_fail (GTK_IS_MENU (menu));
2440 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2442 menu_shell = GTK_MENU_SHELL (menu);
2444 if (g_list_find (menu_shell->priv->children, child))
2446 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2447 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2449 menu_queue_resize (menu);
2454 gtk_menu_style_updated (GtkWidget *widget)
2456 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2458 if (gtk_widget_get_realized (widget))
2460 GtkMenu *menu = GTK_MENU (widget);
2461 GtkMenuPrivate *priv = menu->priv;
2462 GtkStyleContext *context;
2464 context = gtk_widget_get_style_context (widget);
2466 gtk_style_context_set_background (context, priv->bin_window);
2467 gtk_style_context_set_background (context, priv->view_window);
2468 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2473 get_arrows_border (GtkMenu *menu,
2476 GtkMenuPrivate *priv = menu->priv;
2477 guint scroll_arrow_height;
2478 GtkArrowPlacement arrow_placement;
2480 gtk_widget_style_get (GTK_WIDGET (menu),
2481 "scroll-arrow-vlength", &scroll_arrow_height,
2482 "arrow_placement", &arrow_placement,
2485 switch (arrow_placement)
2487 case GTK_ARROWS_BOTH:
2488 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2489 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2492 case GTK_ARROWS_START:
2493 border->top = (priv->upper_arrow_visible ||
2494 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2498 case GTK_ARROWS_END:
2500 border->bottom = (priv->upper_arrow_visible ||
2501 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2505 border->left = border->right = 0;
2509 get_menu_padding (GtkWidget *widget,
2512 GtkStyleContext *context;
2513 GtkStateFlags state;
2515 context = gtk_widget_get_style_context (widget);
2516 state = gtk_widget_get_state_flags (widget);
2518 gtk_style_context_get_padding (context, state, padding);
2522 gtk_menu_realize (GtkWidget *widget)
2524 GtkMenu *menu = GTK_MENU (widget);
2525 GtkMenuPrivate *priv = menu->priv;
2526 GtkAllocation allocation;
2527 GtkStyleContext *context;
2529 GdkWindowAttr attributes;
2530 gint attributes_mask;
2534 guint vertical_padding;
2535 guint horizontal_padding;
2536 GtkBorder arrow_border, padding;
2538 g_return_if_fail (GTK_IS_MENU (widget));
2540 gtk_widget_set_realized (widget, TRUE);
2542 gtk_widget_get_allocation (widget, &allocation);
2544 attributes.window_type = GDK_WINDOW_CHILD;
2545 attributes.x = allocation.x;
2546 attributes.y = allocation.y;
2547 attributes.width = allocation.width;
2548 attributes.height = allocation.height;
2549 attributes.wclass = GDK_INPUT_OUTPUT;
2550 attributes.visual = gtk_widget_get_visual (widget);
2551 attributes.event_mask = gtk_widget_get_events (widget);
2552 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2553 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2555 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2557 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2558 &attributes, attributes_mask);
2559 gtk_widget_set_window (widget, window);
2560 gdk_window_set_user_data (window, widget);
2562 get_menu_padding (widget, &padding);
2563 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2564 context = gtk_widget_get_style_context (widget);
2566 gtk_widget_style_get (GTK_WIDGET (menu),
2567 "vertical-padding", &vertical_padding,
2568 "horizontal-padding", &horizontal_padding,
2571 gtk_widget_get_allocation (widget, &allocation);
2573 attributes.x = border_width + padding.left + horizontal_padding;
2574 attributes.y = border_width + padding.top + vertical_padding;
2575 attributes.width = allocation.width -
2576 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2577 attributes.height = allocation.height -
2578 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2580 get_arrows_border (menu, &arrow_border);
2581 attributes.y += arrow_border.top;
2582 attributes.height -= arrow_border.top;
2583 attributes.height -= arrow_border.bottom;
2585 attributes.width = MAX (1, attributes.width);
2586 attributes.height = MAX (1, attributes.height);
2588 priv->view_window = gdk_window_new (window,
2589 &attributes, attributes_mask);
2590 gdk_window_set_user_data (priv->view_window, menu);
2592 gtk_widget_get_allocation (widget, &allocation);
2596 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2597 padding.left + padding.right;
2598 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2599 padding.top + padding.bottom;
2601 attributes.width = MAX (1, attributes.width);
2602 attributes.height = MAX (1, attributes.height);
2604 priv->bin_window = gdk_window_new (priv->view_window,
2605 &attributes, attributes_mask);
2606 gdk_window_set_user_data (priv->bin_window, menu);
2608 children = GTK_MENU_SHELL (menu)->priv->children;
2611 child = children->data;
2612 children = children->next;
2614 gtk_widget_set_parent_window (child, priv->bin_window);
2617 gtk_style_context_set_background (context, priv->bin_window);
2618 gtk_style_context_set_background (context, priv->view_window);
2619 gtk_style_context_set_background (context, window);
2621 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2622 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2623 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2625 gdk_window_show (priv->bin_window);
2626 gdk_window_show (priv->view_window);
2630 gtk_menu_focus (GtkWidget *widget,
2631 GtkDirectionType direction)
2633 /* A menu or its menu items cannot have focus */
2637 /* See notes in gtk_menu_popup() for information
2638 * about the "grab transfer window"
2641 menu_grab_transfer_window_get (GtkMenu *menu)
2643 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2646 GdkWindowAttr attributes;
2647 gint attributes_mask;
2649 attributes.x = -100;
2650 attributes.y = -100;
2651 attributes.width = 10;
2652 attributes.height = 10;
2653 attributes.window_type = GDK_WINDOW_TEMP;
2654 attributes.wclass = GDK_INPUT_ONLY;
2655 attributes.override_redirect = TRUE;
2656 attributes.event_mask = 0;
2658 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2660 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2661 &attributes, attributes_mask);
2662 gdk_window_set_user_data (window, menu);
2664 gdk_window_show (window);
2666 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2673 menu_grab_transfer_window_destroy (GtkMenu *menu)
2675 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2678 gdk_window_set_user_data (window, NULL);
2679 gdk_window_destroy (window);
2680 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2685 gtk_menu_unrealize (GtkWidget *widget)
2687 GtkMenu *menu = GTK_MENU (widget);
2688 GtkMenuPrivate *priv = menu->priv;
2690 menu_grab_transfer_window_destroy (menu);
2692 gdk_window_set_user_data (priv->view_window, NULL);
2693 gdk_window_destroy (priv->view_window);
2694 priv->view_window = NULL;
2696 gdk_window_set_user_data (priv->bin_window, NULL);
2697 gdk_window_destroy (priv->bin_window);
2698 priv->bin_window = NULL;
2700 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2704 calculate_line_heights (GtkMenu *menu,
2706 guint **ret_min_heights,
2707 guint **ret_nat_heights)
2710 GtkMenuPrivate *priv;
2711 GtkMenuShell *menu_shell;
2712 GtkWidget *child, *widget;
2714 guint horizontal_padding;
2723 widget = GTK_WIDGET (menu);
2724 menu_shell = GTK_MENU_SHELL (widget);
2726 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2727 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2728 n_heights = gtk_menu_get_n_rows (menu);
2729 n_columns = gtk_menu_get_n_columns (menu);
2730 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2732 gtk_widget_style_get (GTK_WIDGET (menu),
2733 "horizontal-padding", &horizontal_padding,
2735 get_menu_padding (widget, &padding);
2737 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2738 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2740 for (children = menu_shell->priv->children; children; children = children->next)
2745 gint child_min, child_nat;
2747 child = children->data;
2749 if (!gtk_widget_get_visible (child))
2752 get_effective_child_attach (child, &l, &r, &t, &b);
2754 part = avail_width / (r - l);
2756 gtk_widget_get_preferred_height_for_width (child, part,
2757 &child_min, &child_nat);
2759 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2761 part = MAX (child_min, toggle_size) / (b - t);
2762 min_heights[t] = MAX (min_heights[t], part);
2764 part = MAX (child_nat, toggle_size) / (b - t);
2765 nat_heights[t] = MAX (nat_heights[t], part);
2768 if (ret_min_heights)
2769 *ret_min_heights = min_heights;
2771 g_free (min_heights);
2773 if (ret_nat_heights)
2774 *ret_nat_heights = nat_heights;
2776 g_free (nat_heights);
2782 gtk_menu_size_allocate (GtkWidget *widget,
2783 GtkAllocation *allocation)
2786 GtkMenuPrivate *priv;
2787 GtkMenuShell *menu_shell;
2789 GtkAllocation child_allocation;
2794 guint vertical_padding;
2795 guint horizontal_padding;
2798 g_return_if_fail (GTK_IS_MENU (widget));
2799 g_return_if_fail (allocation != NULL);
2801 menu = GTK_MENU (widget);
2802 menu_shell = GTK_MENU_SHELL (widget);
2805 gtk_widget_set_allocation (widget, allocation);
2807 gtk_widget_style_get (GTK_WIDGET (menu),
2808 "vertical-padding", &vertical_padding,
2809 "horizontal-padding", &horizontal_padding,
2812 get_menu_padding (widget, &padding);
2813 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2815 g_free (priv->heights);
2816 priv->heights_length = calculate_line_heights (menu,
2821 /* refresh our cached height request */
2822 priv->requested_height = (2 * (border_width + vertical_padding)) +
2823 padding.top + padding.bottom;
2824 for (i = 0; i < priv->heights_length; i++)
2825 priv->requested_height += priv->heights[i];
2827 x = border_width + padding.left + horizontal_padding;
2828 y = border_width + padding.top + vertical_padding;
2829 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2830 padding.left - padding.right;
2831 height = allocation->height - (2 * (border_width + vertical_padding)) -
2832 padding.top - padding.bottom;
2834 if (menu_shell->priv->active)
2835 gtk_menu_scroll_to (menu, priv->scroll_offset);
2837 if (!priv->tearoff_active)
2839 GtkBorder arrow_border;
2841 get_arrows_border (menu, &arrow_border);
2842 y += arrow_border.top;
2843 height -= arrow_border.top;
2844 height -= arrow_border.bottom;
2847 width = MAX (1, width);
2848 height = MAX (1, height);
2850 if (gtk_widget_get_realized (widget))
2852 gdk_window_move_resize (gtk_widget_get_window (widget),
2853 allocation->x, allocation->y,
2854 allocation->width, allocation->height);
2856 gdk_window_move_resize (priv->view_window, x, y, width, height);
2859 if (menu_shell->priv->children)
2861 gint base_width = width / gtk_menu_get_n_columns (menu);
2863 children = menu_shell->priv->children;
2866 child = children->data;
2867 children = children->next;
2869 if (gtk_widget_get_visible (child))
2874 get_effective_child_attach (child, &l, &r, &t, &b);
2876 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2879 tmp = gtk_menu_get_n_columns (menu) - l;
2880 l = gtk_menu_get_n_columns (menu) - r;
2884 child_allocation.width = (r - l) * base_width;
2885 child_allocation.height = 0;
2886 child_allocation.x = l * base_width;
2887 child_allocation.y = 0;
2889 for (i = 0; i < b; i++)
2892 child_allocation.y += priv->heights[i];
2894 child_allocation.height += priv->heights[i];
2897 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2900 gtk_widget_size_allocate (child, &child_allocation);
2901 gtk_widget_queue_draw (child);
2905 /* Resize the item window */
2906 if (gtk_widget_get_realized (widget))
2912 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2913 height += priv->heights[i];
2915 width = gtk_menu_get_n_columns (menu) * base_width;
2916 gdk_window_resize (priv->bin_window, width, height);
2919 if (priv->tearoff_active)
2921 if (height >= priv->requested_height)
2923 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2925 gtk_widget_hide (priv->tearoff_scrollbar);
2926 gtk_menu_set_tearoff_hints (menu, allocation->width);
2928 gtk_menu_scroll_to (menu, 0);
2933 gtk_adjustment_configure (priv->tearoff_adjustment,
2934 gtk_adjustment_get_value (priv->tearoff_adjustment),
2936 priv->requested_height,
2937 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2938 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2939 allocation->height);
2941 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2943 gtk_widget_show (priv->tearoff_scrollbar);
2944 gtk_menu_set_tearoff_hints (menu, allocation->width);
2952 get_arrows_visible_area (GtkMenu *menu,
2953 GdkRectangle *border,
2954 GdkRectangle *upper,
2955 GdkRectangle *lower,
2958 GtkArrowPlacement arrow_placement;
2959 GtkWidget *widget = GTK_WIDGET (menu);
2961 guint vertical_padding;
2962 guint horizontal_padding;
2963 gint scroll_arrow_height;
2964 GtkBorder menu_padding;
2966 gtk_widget_style_get (widget,
2967 "vertical-padding", &vertical_padding,
2968 "horizontal-padding", &horizontal_padding,
2969 "scroll-arrow-vlength", &scroll_arrow_height,
2970 "arrow-placement", &arrow_placement,
2973 get_menu_padding (widget, &menu_padding);
2974 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2975 border->x = border_width + menu_padding.left + horizontal_padding;
2976 border->y = border_width + menu_padding.top + vertical_padding;
2977 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2978 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2980 switch (arrow_placement)
2982 case GTK_ARROWS_BOTH:
2983 upper->x = border->x;
2984 upper->y = border->y;
2985 upper->width = border->width - 2 * border->x;
2986 upper->height = scroll_arrow_height;
2988 lower->x = border->x;
2989 lower->y = border->height - border->y - scroll_arrow_height;
2990 lower->width = border->width - 2 * border->x;
2991 lower->height = scroll_arrow_height;
2994 case GTK_ARROWS_START:
2995 upper->x = border->x;
2996 upper->y = border->y;
2997 upper->width = (border->width - 2 * border->x) / 2;
2998 upper->height = scroll_arrow_height;
3000 lower->x = border->x + upper->width;
3001 lower->y = border->y;
3002 lower->width = (border->width - 2 * border->x) / 2;
3003 lower->height = scroll_arrow_height;
3006 case GTK_ARROWS_END:
3007 upper->x = border->x;
3008 upper->y = border->height - border->y - scroll_arrow_height;
3009 upper->width = (border->width - 2 * border->x) / 2;
3010 upper->height = scroll_arrow_height;
3012 lower->x = border->x + upper->width;
3013 lower->y = border->height - border->y - scroll_arrow_height;
3014 lower->width = (border->width - 2 * border->x) / 2;
3015 lower->height = scroll_arrow_height;
3019 g_assert_not_reached();
3020 upper->x = upper->y = upper->width = upper->height = 0;
3021 lower->x = lower->y = lower->width = lower->height = 0;
3024 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
3028 gtk_menu_draw (GtkWidget *widget,
3032 GtkMenuPrivate *priv;
3033 GtkStyleContext *context;
3034 GdkRectangle border;
3038 GtkBorder menu_padding;
3040 menu = GTK_MENU (widget);
3042 context = gtk_widget_get_style_context (widget);
3044 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3045 get_menu_padding (widget, &menu_padding);
3047 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3049 gfloat arrow_scaling;
3052 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3053 arrow_size = arrow_scaling * arrow_space;
3055 gtk_render_background (context, cr, 0, 0,
3056 gtk_widget_get_allocated_width (widget),
3057 gtk_widget_get_allocated_height (widget));
3058 gtk_render_frame (context, cr, 0, 0,
3059 gtk_widget_get_allocated_width (widget),
3060 gtk_widget_get_allocated_height (widget));
3062 gtk_style_context_save (context);
3063 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3065 if (priv->upper_arrow_visible && !priv->tearoff_active)
3067 gtk_style_context_save (context);
3068 gtk_style_context_set_state (context, priv->upper_arrow_state);
3070 gtk_render_background (context, cr,
3072 upper.width, upper.height);
3073 gtk_render_frame (context, cr,
3075 upper.width, upper.height);
3077 gtk_render_arrow (context, cr, 0,
3078 upper.x + (upper.width - arrow_size) / 2,
3079 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3082 gtk_style_context_restore (context);
3085 if (priv->lower_arrow_visible && !priv->tearoff_active)
3087 gtk_style_context_save (context);
3088 gtk_style_context_set_state (context, priv->lower_arrow_state);
3090 gtk_render_background (context, cr,
3092 lower.width, lower.height);
3093 gtk_render_frame (context, cr,
3095 lower.width, lower.height);
3097 gtk_render_arrow (context, cr, G_PI,
3098 lower.x + (lower.width - arrow_size) / 2,
3099 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3102 gtk_style_context_restore (context);
3105 gtk_style_context_restore (context);
3108 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3110 gint y = -border.y + priv->scroll_offset;
3113 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3115 if (!priv->tearoff_active)
3117 GtkBorder arrow_border;
3119 get_arrows_border (menu, &arrow_border);
3120 y -= arrow_border.top;
3123 gtk_render_background (context, cr,
3125 border.width, border.height);
3126 gtk_render_frame (context, cr,
3128 border.width, border.height);
3133 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3139 gtk_menu_show (GtkWidget *widget)
3141 GtkMenu *menu = GTK_MENU (widget);
3143 _gtk_menu_refresh_accel_paths (menu, FALSE);
3145 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3150 gtk_menu_get_preferred_width (GtkWidget *widget,
3155 GtkMenuShell *menu_shell;
3156 GtkMenuPrivate *priv;
3159 guint max_toggle_size;
3160 guint max_accel_width;
3161 guint horizontal_padding;
3163 gint child_min, child_nat;
3164 gint min_width, nat_width;
3167 menu = GTK_MENU (widget);
3168 menu_shell = GTK_MENU_SHELL (widget);
3171 min_width = nat_width = 0;
3173 max_toggle_size = 0;
3174 max_accel_width = 0;
3176 children = menu_shell->priv->children;
3183 child = children->data;
3184 children = children->next;
3186 if (! gtk_widget_get_visible (child))
3189 get_effective_child_attach (child, &l, &r, &t, &b);
3191 /* It's important to size_request the child
3192 * before doing the toggle size request, in
3193 * case the toggle size request depends on the size
3194 * request of a child of the child (e.g. for ImageMenuItem)
3196 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3198 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3199 max_toggle_size = MAX (max_toggle_size, toggle_size);
3200 max_accel_width = MAX (max_accel_width,
3201 GTK_MENU_ITEM (child)->priv->accelerator_width);
3203 part = child_min / (r - l);
3204 min_width = MAX (min_width, part);
3206 part = child_nat / (r - l);
3207 nat_width = MAX (nat_width, part);
3210 /* If the menu doesn't include any images or check items
3211 * reserve the space so that all menus are consistent.
3212 * We only do this for 'ordinary' menus, not for combobox
3213 * menus or multi-column menus
3215 if (max_toggle_size == 0 &&
3216 gtk_menu_get_n_columns (menu) == 1 &&
3217 !priv->no_toggle_size)
3219 GtkStyleContext *context;
3220 GtkWidgetPath *menu_path, *check_path;
3221 guint toggle_spacing;
3222 guint indicator_size;
3224 context = gtk_widget_get_style_context (widget);
3225 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3227 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3228 check_path = gtk_widget_path_copy (menu_path);
3229 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3231 gtk_style_context_set_path (context, check_path);
3232 gtk_widget_path_free (check_path);
3234 gtk_style_context_get_style (context,
3235 "toggle-spacing", &toggle_spacing,
3236 "indicator-size", &indicator_size,
3239 max_toggle_size = indicator_size + toggle_spacing;
3241 /* Restore real widget path */
3242 gtk_style_context_set_path (context, menu_path);
3243 gtk_widget_path_free (menu_path);
3246 min_width += 2 * max_toggle_size + max_accel_width;
3247 min_width *= gtk_menu_get_n_columns (menu);
3249 nat_width += 2 * max_toggle_size + max_accel_width;
3250 nat_width *= gtk_menu_get_n_columns (menu);
3252 gtk_widget_style_get (GTK_WIDGET (menu),
3253 "horizontal-padding", &horizontal_padding,
3256 get_menu_padding (widget, &padding);
3257 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3258 min_width += (2 * (border_width + horizontal_padding)) +
3259 padding.left + padding.right;
3260 nat_width += (2 * (border_width + horizontal_padding)) +
3261 padding.left + padding.right;
3263 priv->toggle_size = max_toggle_size;
3264 priv->accel_size = max_accel_width;
3267 *minimum_size = min_width;
3270 *natural_size = nat_width;
3272 /* Don't resize the tearoff if it is not active,
3273 * because it won't redraw (it is only a background pixmap).
3275 if (priv->tearoff_active)
3276 gtk_menu_set_tearoff_hints (menu, min_width);
3280 gtk_menu_get_preferred_height (GtkWidget *widget,
3286 /* Menus are height-for-width only, just return the height
3287 * for the minimum width
3289 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3290 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3294 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3300 GtkMenu *menu = GTK_MENU (widget);
3301 GtkMenuPrivate *priv = menu->priv;
3302 guint *min_heights, *nat_heights;
3303 guint vertical_padding, border_width;
3305 gint min_height, nat_height;
3307 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3308 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3309 get_menu_padding (widget, &padding);
3311 min_height = nat_height = (border_width + vertical_padding) * 2 +
3312 padding.top + padding.bottom;
3315 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3317 for (i = 0; i < n_heights; i++)
3319 min_height += min_heights[i];
3320 nat_height += nat_heights[i];
3323 if (priv->have_position)
3325 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3326 GdkRectangle monitor;
3328 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
3330 if (priv->position_y + min_height > monitor.y + monitor.height)
3331 min_height = monitor.y + monitor.height - priv->position_y;
3333 if (priv->position_y + nat_height > monitor.y + monitor.height)
3334 nat_height = monitor.y + monitor.height - priv->position_y;
3336 if (priv->position_y < monitor.y)
3338 min_height -= monitor.y - priv->position_y;
3339 nat_height -= monitor.y - priv->position_y;
3344 *minimum_size = min_height;
3347 *natural_size = nat_height;
3349 g_free (min_heights);
3350 g_free (nat_heights);
3356 gtk_menu_button_scroll (GtkMenu *menu,
3357 GdkEventButton *event)
3359 GtkMenuPrivate *priv = menu->priv;
3361 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3363 gboolean touchscreen_mode;
3365 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3366 "gtk-touchscreen-mode", &touchscreen_mode,
3369 if (touchscreen_mode)
3370 gtk_menu_handle_scrolling (menu,
3371 event->x_root, event->y_root,
3372 event->type == GDK_BUTTON_PRESS,
3382 pointer_in_menu_window (GtkWidget *widget,
3386 GtkMenu *menu = GTK_MENU (widget);
3387 GtkMenuPrivate *priv = menu->priv;
3388 GtkAllocation allocation;
3390 if (gtk_widget_get_mapped (priv->toplevel))
3392 GtkMenuShell *menu_shell;
3393 gint window_x, window_y;
3395 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3396 &window_x, &window_y);
3398 gtk_widget_get_allocation (widget, &allocation);
3399 if (x_root >= window_x && x_root < window_x + allocation.width &&
3400 y_root >= window_y && y_root < window_y + allocation.height)
3403 menu_shell = GTK_MENU_SHELL (widget);
3405 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3406 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3414 gtk_menu_button_press (GtkWidget *widget,
3415 GdkEventButton *event)
3417 if (event->type != GDK_BUTTON_PRESS)
3420 /* Don't pass down to menu shell for presses over scroll arrows
3422 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3425 /* Don't pass down to menu shell if a non-menuitem part of the menu
3426 * was clicked. The check for the event_widget being a GtkMenuShell
3427 * works because we have the pointer grabbed on menu_shell->window
3428 * with owner_events=TRUE, so all events that are either outside
3429 * the menu or on its border are delivered relative to
3430 * menu_shell->window.
3432 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3433 pointer_in_menu_window (widget, event->x_root, event->y_root))
3436 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3440 gtk_menu_button_release (GtkWidget *widget,
3441 GdkEventButton *event)
3443 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3445 if (priv->ignore_button_release)
3447 priv->ignore_button_release = FALSE;
3451 if (event->type != GDK_BUTTON_RELEASE)
3454 /* Don't pass down to menu shell for releases over scroll arrows
3456 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3459 /* Don't pass down to menu shell if a non-menuitem part of the menu
3460 * was clicked (see comment in button_press()).
3462 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3463 pointer_in_menu_window (widget, event->x_root, event->y_root))
3465 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3466 * bail out early here so it is in a consistent state for the
3467 * next button_press/button_release in GtkMenuShell.
3470 if (GTK_MENU_SHELL (widget)->priv->active)
3471 GTK_MENU_SHELL (widget)->priv->button = 0;
3476 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3479 static const gchar *
3480 get_accel_path (GtkWidget *menu_item,
3485 GClosure *accel_closure;
3486 GtkAccelGroup *accel_group;
3488 path = _gtk_widget_get_accel_path (menu_item, locked);
3491 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3497 label = gtk_bin_get_child (GTK_BIN (menu_item));
3499 if (GTK_IS_ACCEL_LABEL (label))
3501 g_object_get (label,
3502 "accel-closure", &accel_closure,
3506 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3508 *locked = gtk_accel_group_get_is_locked (accel_group);
3518 gtk_menu_key_press (GtkWidget *widget,
3521 GtkMenuShell *menu_shell;
3523 gboolean delete = FALSE;
3524 gboolean can_change_accels;
3525 gchar *accel = NULL;
3526 guint accel_key, accel_mods;
3527 GdkModifierType consumed_modifiers;
3528 GdkDisplay *display;
3530 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3531 g_return_val_if_fail (event != NULL, FALSE);
3533 menu_shell = GTK_MENU_SHELL (widget);
3534 menu = GTK_MENU (widget);
3536 gtk_menu_stop_navigating_submenu (menu);
3538 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3541 display = gtk_widget_get_display (widget);
3543 g_object_get (gtk_widget_get_settings (widget),
3544 "gtk-menu-bar-accel", &accel,
3545 "gtk-can-change-accels", &can_change_accels,
3548 if (accel && *accel)
3551 GdkModifierType mods = 0;
3553 gtk_accelerator_parse (accel, &keyval, &mods);
3556 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3558 /* FIXME this is wrong, needs to be in the global accel resolution
3559 * thing, to properly consider i18n etc., but that probably requires
3560 * AccelGroup changes etc.
3562 if (event->keyval == keyval && (mods & event->state) == mods)
3564 gtk_menu_shell_cancel (menu_shell);
3572 switch (event->keyval)
3574 case GDK_KEY_Delete:
3575 case GDK_KEY_KP_Delete:
3576 case GDK_KEY_BackSpace:
3583 /* Figure out what modifiers went into determining the key symbol */
3584 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3585 event->hardware_keycode,
3586 event->state, event->group,
3587 NULL, NULL, NULL, &consumed_modifiers);
3589 accel_key = gdk_keyval_to_lower (event->keyval);
3590 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3592 /* If lowercasing affects the keysym, then we need to include SHIFT
3593 * in the modifiers, we re-uppercase when we match against the keyval,
3594 * but display and save in caseless form.
3596 if (accel_key != event->keyval)
3597 accel_mods |= GDK_SHIFT_MASK;
3599 /* Modify the accelerators */
3600 if (can_change_accels &&
3601 menu_shell->priv->active_menu_item &&
3602 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3603 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3604 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3606 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3607 gboolean locked, replace_accels = TRUE;
3610 path = get_accel_path (menu_item, &locked);
3611 if (!path || locked)
3613 /* Can't change accelerators on menu_items without paths
3614 * (basically, those items are accelerator-locked).
3616 gtk_widget_error_bell (widget);
3622 /* For the keys that act to delete the current setting,
3623 * we delete the current setting if there is one, otherwise,
3624 * we set the key as the accelerator.
3630 if (gtk_accel_map_lookup_entry (path, &key) &&
3631 (key.accel_key || key.accel_mods))
3637 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3641 /* We failed, probably because this key is in use
3642 * and locked already.
3644 gtk_widget_error_bell (widget);
3653 check_threshold (GtkWidget *widget,
3662 ABS (start_x - x) > THRESHOLD ||
3663 ABS (start_y - y) > THRESHOLD;
3667 definitely_within_item (GtkWidget *widget,
3671 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3674 w = gdk_window_get_width (window);
3675 h = gdk_window_get_height (window);
3678 check_threshold (widget, 0, 0, x, y) &&
3679 check_threshold (widget, w - 1, 0, x, y) &&
3680 check_threshold (widget, w - 1, h - 1, x, y) &&
3681 check_threshold (widget, 0, h - 1, x, y);
3685 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3687 GtkMenuPrivate *priv = menu->priv;
3689 return priv->navigation_height && priv->navigation_width;
3693 gtk_menu_motion_notify (GtkWidget *widget,
3694 GdkEventMotion *event)
3696 GtkWidget *menu_item;
3698 GtkMenuShell *menu_shell;
3701 gboolean need_enter;
3703 if (GTK_IS_MENU (widget))
3705 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3707 if (priv->ignore_button_release)
3708 priv->ignore_button_release = FALSE;
3710 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3714 /* We received the event for one of two reasons:
3716 * a) We are the active menu, and did gtk_grab_add()
3717 * b) The widget is a child of ours, and the event was propagated
3719 * Since for computation of navigation regions, we want the menu which
3720 * is the parent of the menu item, for a), we need to find that menu,
3721 * which may be different from 'widget'.
3723 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3724 parent = gtk_widget_get_parent (menu_item);
3725 if (!GTK_IS_MENU_ITEM (menu_item) ||
3726 !GTK_IS_MENU (parent))
3729 menu_shell = GTK_MENU_SHELL (parent);
3730 menu = GTK_MENU (menu_shell);
3732 if (definitely_within_item (menu_item, event->x, event->y))
3733 menu_shell->priv->activate_time = 0;
3735 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3737 /* Check to see if we are within an active submenu's navigation region
3739 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3742 /* Make sure we pop down if we enter a non-selectable menu item, so we
3743 * don't show a submenu when the cursor is outside the stay-up triangle.
3745 if (!_gtk_menu_item_is_selectable (menu_item))
3747 /* We really want to deselect, but this gives the menushell code
3748 * a chance to do some bookkeeping about the menuitem.
3750 gtk_menu_shell_select_item (menu_shell, menu_item);
3756 /* The menu is now sensitive to enter events on its items, but
3757 * was previously sensitive. So we fake an enter event.
3759 menu_shell->priv->ignore_enter = FALSE;
3761 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3762 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3764 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3767 send_event->crossing.window = g_object_ref (event->window);
3768 send_event->crossing.time = event->time;
3769 send_event->crossing.send_event = TRUE;
3770 send_event->crossing.x_root = event->x_root;
3771 send_event->crossing.y_root = event->y_root;
3772 send_event->crossing.x = event->x;
3773 send_event->crossing.y = event->y;
3774 send_event->crossing.state = event->state;
3775 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3777 /* We send the event to 'widget', the currently active menu,
3778 * instead of 'menu', the menu that the pointer is in. This
3779 * will ensure that the event will be ignored unless the
3780 * menuitem is a child of the active menu or some parent
3781 * menu of the active menu.
3783 result = gtk_widget_event (widget, send_event);
3784 gdk_event_free (send_event);
3794 get_double_arrows (GtkMenu *menu)
3796 GtkMenuPrivate *priv = menu->priv;
3797 gboolean double_arrows;
3798 GtkArrowPlacement arrow_placement;
3800 gtk_widget_style_get (GTK_WIDGET (menu),
3801 "double-arrows", &double_arrows,
3802 "arrow-placement", &arrow_placement,
3805 if (arrow_placement != GTK_ARROWS_BOTH)
3808 return double_arrows || (priv->initially_pushed_in &&
3809 priv->scroll_offset != 0);
3813 gtk_menu_scroll_by (GtkMenu *menu,
3816 GtkMenuPrivate *priv = menu->priv;
3817 GtkBorder arrow_border;
3821 gboolean double_arrows;
3823 widget = GTK_WIDGET (menu);
3824 offset = priv->scroll_offset + step;
3826 get_arrows_border (menu, &arrow_border);
3828 double_arrows = get_double_arrows (menu);
3830 /* If we scroll upward and the non-visible top part
3831 * is smaller than the scroll arrow it would be
3832 * pretty stupid to show the arrow and taking more
3833 * screen space than just scrolling to the top.
3836 if ((step < 0) && (offset < arrow_border.top))
3839 /* Don't scroll over the top if we weren't before: */
3840 if ((priv->scroll_offset >= 0) && (offset < 0))
3843 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3845 if (priv->scroll_offset == 0 &&
3846 view_height >= priv->requested_height)
3849 /* Don't scroll past the bottom if we weren't before: */
3850 if (priv->scroll_offset > 0)
3851 view_height -= arrow_border.top;
3853 /* When both arrows are always shown,
3854 * reduce view height even more.
3857 view_height -= arrow_border.bottom;
3859 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3860 (offset + view_height > priv->requested_height))
3861 offset = priv->requested_height - view_height;
3863 if (offset != priv->scroll_offset)
3864 gtk_menu_scroll_to (menu, offset);
3868 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3869 gboolean touchscreen_mode)
3871 GtkMenuPrivate *priv = menu->priv;
3872 gboolean upper_visible;
3873 gboolean lower_visible;
3875 upper_visible = priv->upper_arrow_visible;
3876 lower_visible = priv->lower_arrow_visible;
3878 gtk_menu_scroll_by (menu, priv->scroll_step);
3880 if (touchscreen_mode &&
3881 (upper_visible != priv->upper_arrow_visible ||
3882 lower_visible != priv->lower_arrow_visible))
3884 /* We are about to hide a scroll arrow while the mouse is pressed,
3885 * this would cause the uncovered menu item to be activated on button
3886 * release. Therefore we need to ignore button release here
3888 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3889 priv->ignore_button_release = TRUE;
3894 gtk_menu_scroll_timeout (gpointer data)
3897 gboolean touchscreen_mode;
3899 menu = GTK_MENU (data);
3901 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3902 "gtk-touchscreen-mode", &touchscreen_mode,
3905 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3911 gtk_menu_scroll_timeout_initial (gpointer data)
3915 gboolean touchscreen_mode;
3917 menu = GTK_MENU (data);
3919 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3920 "gtk-timeout-repeat", &timeout,
3921 "gtk-touchscreen-mode", &touchscreen_mode,
3924 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3926 gtk_menu_remove_scroll_timeout (menu);
3928 menu->priv->scroll_timeout =
3929 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3935 gtk_menu_start_scrolling (GtkMenu *menu)
3938 gboolean touchscreen_mode;
3940 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3941 "gtk-timeout-repeat", &timeout,
3942 "gtk-touchscreen-mode", &touchscreen_mode,
3945 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3947 menu->priv->scroll_timeout =
3948 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3952 gtk_menu_scroll (GtkWidget *widget,
3953 GdkEventScroll *event)
3955 GtkMenu *menu = GTK_MENU (widget);
3957 switch (event->direction)
3959 case GDK_SCROLL_RIGHT:
3960 case GDK_SCROLL_DOWN:
3961 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3963 case GDK_SCROLL_LEFT:
3965 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3973 get_arrows_sensitive_area (GtkMenu *menu,
3974 GdkRectangle *upper,
3975 GdkRectangle *lower)
3977 GtkArrowPlacement arrow_placement;
3978 GtkWidget *widget = GTK_WIDGET (menu);
3982 guint vertical_padding;
3984 gint scroll_arrow_height;
3987 window = gtk_widget_get_window (widget);
3988 width = gdk_window_get_width (window);
3989 height = gdk_window_get_height (window);
3991 gtk_widget_style_get (widget,
3992 "vertical-padding", &vertical_padding,
3993 "scroll-arrow-vlength", &scroll_arrow_height,
3994 "arrow-placement", &arrow_placement,
3997 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3998 get_menu_padding (widget, &padding);
4000 gdk_window_get_position (window, &win_x, &win_y);
4002 switch (arrow_placement)
4004 case GTK_ARROWS_BOTH:
4009 upper->width = width;
4010 upper->height = scroll_arrow_height + border + padding.top;
4016 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
4017 lower->width = width;
4018 lower->height = scroll_arrow_height + border + padding.bottom;
4022 case GTK_ARROWS_START:
4027 upper->width = width / 2;
4028 upper->height = scroll_arrow_height + border + padding.top;
4033 lower->x = win_x + width / 2;
4035 lower->width = width / 2;
4036 lower->height = scroll_arrow_height + border + padding.bottom;
4040 case GTK_ARROWS_END:
4044 upper->y = win_y + height - border - scroll_arrow_height;
4045 upper->width = width / 2;
4046 upper->height = scroll_arrow_height + border + padding.top;
4051 lower->x = win_x + width / 2;
4052 lower->y = win_y + height - border - scroll_arrow_height;
4053 lower->width = width / 2;
4054 lower->height = scroll_arrow_height + border + padding.bottom;
4062 gtk_menu_handle_scrolling (GtkMenu *menu,
4068 GtkMenuPrivate *priv = menu->priv;
4069 GtkMenuShell *menu_shell;
4072 gboolean scroll_fast = FALSE;
4074 gboolean touchscreen_mode;
4076 menu_shell = GTK_MENU_SHELL (menu);
4078 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4079 "gtk-touchscreen-mode", &touchscreen_mode,
4082 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4087 /* upper arrow handling */
4089 get_arrows_sensitive_area (menu, &rect, NULL);
4092 if (priv->upper_arrow_visible && !priv->tearoff_active &&
4093 (x >= rect.x) && (x < rect.x + rect.width) &&
4094 (y >= rect.y) && (y < rect.y + rect.height))
4099 if (touchscreen_mode)
4100 priv->upper_arrow_prelight = in_arrow;
4102 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4104 gboolean arrow_pressed = FALSE;
4106 if (priv->upper_arrow_visible && !priv->tearoff_active)
4108 if (touchscreen_mode)
4110 if (enter && priv->upper_arrow_prelight)
4112 if (priv->scroll_timeout == 0)
4114 /* Deselect the active item so that
4115 * any submenus are popped down
4117 gtk_menu_shell_deselect (menu_shell);
4119 gtk_menu_remove_scroll_timeout (menu);
4120 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4124 /* Only do stuff on click. */
4125 gtk_menu_start_scrolling (menu);
4126 arrow_pressed = TRUE;
4131 arrow_pressed = TRUE;
4136 gtk_menu_stop_scrolling (menu);
4139 else /* !touchscreen_mode */
4141 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4143 if (enter && in_arrow &&
4144 (!priv->upper_arrow_prelight ||
4145 priv->scroll_fast != scroll_fast))
4147 priv->upper_arrow_prelight = TRUE;
4148 priv->scroll_fast = scroll_fast;
4150 /* Deselect the active item so that
4151 * any submenus are popped down
4153 gtk_menu_shell_deselect (menu_shell);
4155 gtk_menu_remove_scroll_timeout (menu);
4156 priv->scroll_step = scroll_fast
4157 ? -MENU_SCROLL_STEP2
4158 : -MENU_SCROLL_STEP1;
4160 priv->scroll_timeout =
4161 gdk_threads_add_timeout (scroll_fast
4162 ? MENU_SCROLL_TIMEOUT2
4163 : MENU_SCROLL_TIMEOUT1,
4164 gtk_menu_scroll_timeout, menu);
4166 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4168 gtk_menu_stop_scrolling (menu);
4173 /* gtk_menu_start_scrolling() might have hit the top of the
4174 * menu, so check if the button isn't insensitive before
4175 * changing it to something else.
4177 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4179 GtkStateFlags arrow_state = 0;
4182 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4184 if (priv->upper_arrow_prelight)
4185 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4187 if (arrow_state != priv->upper_arrow_state)
4189 priv->upper_arrow_state = arrow_state;
4191 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4197 /* lower arrow handling */
4199 get_arrows_sensitive_area (menu, NULL, &rect);
4202 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4203 (x >= rect.x) && (x < rect.x + rect.width) &&
4204 (y >= rect.y) && (y < rect.y + rect.height))
4209 if (touchscreen_mode)
4210 priv->lower_arrow_prelight = in_arrow;
4212 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4214 gboolean arrow_pressed = FALSE;
4216 if (priv->lower_arrow_visible && !priv->tearoff_active)
4218 if (touchscreen_mode)
4220 if (enter && priv->lower_arrow_prelight)
4222 if (priv->scroll_timeout == 0)
4224 /* Deselect the active item so that
4225 * any submenus are popped down
4227 gtk_menu_shell_deselect (menu_shell);
4229 gtk_menu_remove_scroll_timeout (menu);
4230 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4234 /* Only do stuff on click. */
4235 gtk_menu_start_scrolling (menu);
4236 arrow_pressed = TRUE;
4241 arrow_pressed = TRUE;
4246 gtk_menu_stop_scrolling (menu);
4249 else /* !touchscreen_mode */
4251 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4253 if (enter && in_arrow &&
4254 (!priv->lower_arrow_prelight ||
4255 priv->scroll_fast != scroll_fast))
4257 priv->lower_arrow_prelight = TRUE;
4258 priv->scroll_fast = scroll_fast;
4260 /* Deselect the active item so that
4261 * any submenus are popped down
4263 gtk_menu_shell_deselect (menu_shell);
4265 gtk_menu_remove_scroll_timeout (menu);
4266 priv->scroll_step = scroll_fast
4268 : MENU_SCROLL_STEP1;
4270 priv->scroll_timeout =
4271 gdk_threads_add_timeout (scroll_fast
4272 ? MENU_SCROLL_TIMEOUT2
4273 : MENU_SCROLL_TIMEOUT1,
4274 gtk_menu_scroll_timeout, menu);
4276 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4278 gtk_menu_stop_scrolling (menu);
4283 /* gtk_menu_start_scrolling() might have hit the bottom of the
4284 * menu, so check if the button isn't insensitive before
4285 * changing it to something else.
4287 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4289 GtkStateFlags arrow_state = 0;
4292 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4294 if (priv->lower_arrow_prelight)
4295 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4297 if (arrow_state != priv->lower_arrow_state)
4299 priv->lower_arrow_state = arrow_state;
4301 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4309 gtk_menu_enter_notify (GtkWidget *widget,
4310 GdkEventCrossing *event)
4312 GtkWidget *menu_item;
4314 gboolean touchscreen_mode;
4316 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4317 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4318 event->mode == GDK_CROSSING_STATE_CHANGED)
4321 g_object_get (gtk_widget_get_settings (widget),
4322 "gtk-touchscreen-mode", &touchscreen_mode,
4325 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4326 if (GTK_IS_MENU (widget))
4328 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4330 if (!menu_shell->priv->ignore_enter)
4331 gtk_menu_handle_scrolling (GTK_MENU (widget),
4332 event->x_root, event->y_root, TRUE, TRUE);
4335 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4337 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4339 if (GTK_IS_MENU (menu))
4341 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4342 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4344 if (priv->seen_item_enter)
4346 /* This is the second enter we see for an item
4347 * on this menu. This means a release should always
4350 menu_shell->priv->activate_time = 0;
4352 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4353 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4355 if (definitely_within_item (menu_item, event->x, event->y))
4357 /* This is an actual user-enter (ie. not a pop-under)
4358 * In this case, the user must either have entered
4359 * sufficiently far enough into the item, or he must move
4360 * far enough away from the enter point. (see
4361 * gtk_menu_motion_notify())
4363 menu_shell->priv->activate_time = 0;
4367 priv->seen_item_enter = TRUE;
4371 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4372 * will not correspond to the event widget's parent. Check to see
4373 * if we are in the parent's navigation region.
4375 parent = gtk_widget_get_parent (menu_item);
4376 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4377 gtk_menu_navigating_submenu (GTK_MENU (parent),
4378 event->x_root, event->y_root))
4381 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4385 gtk_menu_leave_notify (GtkWidget *widget,
4386 GdkEventCrossing *event)
4388 GtkMenuShell *menu_shell;
4390 GtkMenuItem *menu_item;
4391 GtkWidget *event_widget;
4393 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4394 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4395 event->mode == GDK_CROSSING_STATE_CHANGED)
4398 menu = GTK_MENU (widget);
4399 menu_shell = GTK_MENU_SHELL (widget);
4401 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4404 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4406 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4408 if (!GTK_IS_MENU_ITEM (event_widget))
4411 menu_item = GTK_MENU_ITEM (event_widget);
4413 /* Here we check to see if we're leaving an active menu item
4414 * with a submenu, in which case we enter submenu navigation mode.
4416 if (menu_shell->priv->active_menu_item != NULL
4417 && menu_item->priv->submenu != NULL
4418 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4420 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4422 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4425 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4427 /* We are leaving an active menu item with nonactive submenu.
4428 * Deselect it so we don't surprise the user with by popping
4429 * up a submenu _after_ he left the item.
4431 gtk_menu_shell_deselect (menu_shell);
4436 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4440 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4442 GtkMenuPrivate *priv = menu->priv;
4444 priv->navigation_x = 0;
4445 priv->navigation_y = 0;
4446 priv->navigation_width = 0;
4447 priv->navigation_height = 0;
4449 if (priv->navigation_timeout)
4451 g_source_remove (priv->navigation_timeout);
4452 priv->navigation_timeout = 0;
4456 /* When the timeout is elapsed, the navigation region is destroyed
4457 * and the menuitem under the pointer (if any) is selected.
4460 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4462 GtkMenuPopdownData *popdown_data = user_data;
4463 GtkMenu *menu = popdown_data->menu;
4464 GtkMenuPrivate *priv = menu->priv;
4465 GdkWindow *child_window;
4467 gtk_menu_stop_navigating_submenu (menu);
4469 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4471 child_window = gdk_window_get_device_position (priv->bin_window,
4472 popdown_data->device,
4477 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4479 send_event->crossing.window = g_object_ref (child_window);
4480 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4481 send_event->crossing.send_event = TRUE;
4482 gdk_event_set_device (send_event, popdown_data->device);
4484 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4486 gdk_event_free (send_event);
4494 gtk_menu_navigating_submenu (GtkMenu *menu,
4498 GtkMenuPrivate *priv = menu->priv;
4501 if (!gtk_menu_has_navigation_triangle (menu))
4504 width = priv->navigation_width;
4505 height = priv->navigation_height;
4507 /* Check if x/y are in the triangle spanned by the navigation parameters */
4509 /* 1) Move the coordinates so the triangle starts at 0,0 */
4510 event_x -= priv->navigation_x;
4511 event_y -= priv->navigation_y;
4513 /* 2) Ensure both legs move along the positive axis */
4525 /* 3) Check that the given coordinate is inside the triangle. The formula
4526 * is a transformed form of this formula: x/w + y/h <= 1
4528 if (event_x >= 0 && event_y >= 0 &&
4529 event_x * height + event_y * width <= width * height)
4535 gtk_menu_stop_navigating_submenu (menu);
4541 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4542 GtkMenuItem *menu_item,
4543 GdkEventCrossing *event)
4545 GtkMenuPrivate *priv = menu->priv;
4546 gint submenu_left = 0;
4547 gint submenu_right = 0;
4548 gint submenu_top = 0;
4549 gint submenu_bottom = 0;
4551 GtkWidget *event_widget;
4552 GtkMenuPopdownData *popdown_data;
4555 g_return_if_fail (menu_item->priv->submenu != NULL);
4556 g_return_if_fail (event != NULL);
4558 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4560 window = gtk_widget_get_window (menu_item->priv->submenu);
4561 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4563 submenu_right = submenu_left + gdk_window_get_width (window);
4564 submenu_bottom = submenu_top + gdk_window_get_height (window);
4566 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4568 if (event->x >= 0 && event->x < width)
4572 gtk_menu_stop_navigating_submenu (menu);
4574 /* The navigation region is the triangle closest to the x/y
4575 * location of the rectangle. This is why the width or height
4578 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4581 priv->navigation_x = submenu_left;
4582 priv->navigation_width = event->x_root - submenu_left;
4587 priv->navigation_x = submenu_right;
4588 priv->navigation_width = event->x_root - submenu_right;
4594 priv->navigation_y = event->y_root;
4595 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4597 if (priv->navigation_height >= 0)
4603 priv->navigation_y = event->y_root;
4604 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4606 if (priv->navigation_height <= 0)
4610 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4611 "gtk-menu-popdown-delay", &popdown_delay,
4614 popdown_data = g_new (GtkMenuPopdownData, 1);
4615 popdown_data->menu = menu;
4616 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4618 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4620 gtk_menu_stop_navigating_submenu_cb,
4622 (GDestroyNotify) g_free);
4627 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4631 g_return_if_fail (GTK_IS_MENU (menu_shell));
4633 parent = menu_shell->priv->parent_menu_shell;
4635 menu_shell->priv->activate_time = 0;
4636 gtk_menu_popdown (GTK_MENU (menu_shell));
4639 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4643 gtk_menu_position (GtkMenu *menu,
4644 gboolean set_scroll_offset)
4646 GtkMenuPrivate *priv = menu->priv;
4648 GtkRequisition requisition;
4653 GdkScreen *pointer_screen;
4654 GdkRectangle monitor;
4657 widget = GTK_WIDGET (menu);
4659 screen = gtk_widget_get_screen (widget);
4660 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4661 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4663 /* Realize so we have the proper width and heigh to figure out
4664 * the right place to popup the menu.
4666 gtk_widget_realize (priv->toplevel);
4667 requisition.width = gtk_widget_get_allocated_width (widget);
4668 requisition.height = gtk_widget_get_allocated_height (widget);
4670 if (pointer_screen != screen)
4672 /* Pointer is on a different screen; roughly center the
4673 * menu on the screen. If someone was using multiscreen
4674 * + Xinerama together they'd probably want something
4675 * fancier; but that is likely to be vanishingly rare.
4677 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4678 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4681 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4682 priv->initially_pushed_in = FALSE;
4684 /* Set the type hint here to allow custom position functions
4685 * to set a different hint
4687 if (!gtk_widget_get_visible (priv->toplevel))
4688 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4690 if (priv->position_func)
4692 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4693 priv->position_func_data);
4695 if (priv->monitor_num < 0)
4696 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4698 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4702 gint space_left, space_right, space_above, space_below;
4706 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4708 get_menu_padding (widget, &padding);
4710 /* The placement of popup menus horizontally works like this (with
4711 * RTL in parentheses)
4713 * - If there is enough room to the right (left) of the mouse cursor,
4714 * position the menu there.
4716 * - Otherwise, if if there is enough room to the left (right) of the
4717 * mouse cursor, position the menu there.
4719 * - Otherwise if the menu is smaller than the monitor, position it
4720 * on the side of the mouse cursor that has the most space available
4722 * - Otherwise (if there is simply not enough room for the menu on the
4723 * monitor), position it as far left (right) as possible.
4725 * Positioning in the vertical direction is similar: first try below
4726 * mouse cursor, then above.
4728 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4730 space_left = x - monitor.x;
4731 space_right = monitor.x + monitor.width - x - 1;
4732 space_above = y - monitor.y;
4733 space_below = monitor.y + monitor.height - y - 1;
4735 /* Position horizontally. */
4737 /* the amount of space we need to position the menu.
4738 * Note the menu is offset "thickness" pixels
4740 needed_width = requisition.width - padding.left;
4742 if (needed_width <= space_left ||
4743 needed_width <= space_right)
4745 if ((rtl && needed_width <= space_left) ||
4746 (!rtl && needed_width > space_right))
4749 x = x + padding.left - requisition.width + 1;
4753 /* position right */
4754 x = x - padding.right;
4757 /* x is clamped on-screen further down */
4759 else if (requisition.width <= monitor.width)
4761 /* the menu is too big to fit on either side of the mouse
4762 * cursor, but smaller than the monitor. Position it on
4763 * the side that has the most space
4765 if (space_left > space_right)
4773 x = monitor.x + monitor.width - requisition.width;
4776 else /* menu is simply too big for the monitor */
4781 x = monitor.x + monitor.width - requisition.width;
4790 /* Position vertically.
4791 * The algorithm is the same as above, but simpler
4792 * because we don't have to take RTL into account.
4794 needed_height = requisition.height - padding.top;
4796 if (needed_height <= space_above ||
4797 needed_height <= space_below)
4799 if (needed_height <= space_below)
4800 y = y - padding.top;
4802 y = y + padding.bottom - requisition.height + 1;
4804 y = CLAMP (y, monitor.y,
4805 monitor.y + monitor.height - requisition.height);
4807 else if (needed_height > space_below && needed_height > space_above)
4809 if (space_below >= space_above)
4810 y = monitor.y + monitor.height - requisition.height;
4822 if (priv->initially_pushed_in)
4824 menu_height = requisition.height;
4826 if (y + menu_height > monitor.y + monitor.height)
4828 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4829 y = (monitor.y + monitor.height) - menu_height;
4834 scroll_offset += monitor.y - y;
4839 /* FIXME: should this be done in the various position_funcs ? */
4840 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4842 if (GTK_MENU_SHELL (menu)->priv->active)
4844 priv->have_position = TRUE;
4845 priv->position_x = x;
4846 priv->position_y = y;
4849 if (y + requisition.height > monitor.y + monitor.height)
4850 requisition.height = (monitor.y + monitor.height) - y;
4854 scroll_offset += monitor.y - y;
4855 requisition.height -= monitor.y - y;
4859 if (scroll_offset > 0)
4861 GtkBorder arrow_border;
4863 get_arrows_border (menu, &arrow_border);
4864 scroll_offset += arrow_border.top;
4867 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4870 if (!GTK_MENU_SHELL (menu)->priv->active)
4872 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4873 requisition.width, requisition.height);
4876 if (set_scroll_offset)
4877 priv->scroll_offset = scroll_offset;
4881 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4883 GtkMenuPrivate *priv = menu->priv;
4885 if (priv->scroll_timeout)
4887 g_source_remove (priv->scroll_timeout);
4888 priv->scroll_timeout = 0;
4893 gtk_menu_stop_scrolling (GtkMenu *menu)
4895 GtkMenuPrivate *priv = menu->priv;
4896 gboolean touchscreen_mode;
4898 gtk_menu_remove_scroll_timeout (menu);
4900 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4901 "gtk-touchscreen-mode", &touchscreen_mode,
4904 if (!touchscreen_mode)
4906 priv->upper_arrow_prelight = FALSE;
4907 priv->lower_arrow_prelight = FALSE;
4912 gtk_menu_scroll_to (GtkMenu *menu,
4915 GtkMenuPrivate *priv = menu->priv;
4916 GtkAllocation allocation;
4917 GtkBorder arrow_border, padding;
4920 gint view_width, view_height;
4923 guint vertical_padding;
4924 guint horizontal_padding;
4925 gboolean double_arrows;
4927 widget = GTK_WIDGET (menu);
4929 if (priv->tearoff_active && priv->tearoff_adjustment)
4930 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4932 /* Move/resize the viewport according to arrows: */
4933 gtk_widget_get_allocation (widget, &allocation);
4934 view_width = allocation.width;
4935 view_height = allocation.height;
4937 gtk_widget_style_get (GTK_WIDGET (menu),
4938 "vertical-padding", &vertical_padding,
4939 "horizontal-padding", &horizontal_padding,
4942 get_menu_padding (widget, &padding);
4943 double_arrows = get_double_arrows (menu);
4945 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4947 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4948 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4949 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4950 padding.top - padding.bottom;
4952 x = border_width + padding.left + horizontal_padding;
4953 y = border_width + padding.top + vertical_padding;
4955 if (double_arrows && !priv->tearoff_active)
4957 if (view_height < menu_height ||
4958 (offset > 0 && priv->scroll_offset > 0) ||
4959 (offset < 0 && priv->scroll_offset < 0))
4961 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4962 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4964 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4965 gtk_widget_queue_draw (GTK_WIDGET (menu));
4967 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4969 get_arrows_border (menu, &arrow_border);
4970 y += arrow_border.top;
4971 view_height -= arrow_border.top;
4972 view_height -= arrow_border.bottom;
4975 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4978 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4980 if (priv->upper_arrow_prelight)
4981 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4983 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4986 if (offset >= menu_height - view_height)
4987 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4990 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4992 if (priv->lower_arrow_prelight)
4993 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4995 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4998 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4999 (priv->lower_arrow_state != lower_arrow_previous_state))
5000 gtk_widget_queue_draw (GTK_WIDGET (menu));
5002 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5003 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5005 /* At the upper border, possibly remove timeout */
5006 if (priv->scroll_step < 0)
5008 gtk_menu_stop_scrolling (menu);
5009 gtk_widget_queue_draw (GTK_WIDGET (menu));
5013 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5014 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5016 /* At the lower border, possibly remove timeout */
5017 if (priv->scroll_step > 0)
5019 gtk_menu_stop_scrolling (menu);
5020 gtk_widget_queue_draw (GTK_WIDGET (menu));
5024 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
5028 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
5029 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
5031 gtk_menu_stop_scrolling (menu);
5032 gtk_widget_queue_draw (GTK_WIDGET (menu));
5035 else if (!priv->tearoff_active)
5037 gboolean last_visible;
5039 last_visible = priv->upper_arrow_visible;
5040 priv->upper_arrow_visible = offset > 0;
5042 /* upper_arrow_visible may have changed, so requery the border */
5043 get_arrows_border (menu, &arrow_border);
5044 view_height -= arrow_border.top;
5046 if ((last_visible != priv->upper_arrow_visible) &&
5047 !priv->upper_arrow_visible)
5049 priv->upper_arrow_prelight = FALSE;
5051 /* If we hide the upper arrow, possibly remove timeout */
5052 if (priv->scroll_step < 0)
5054 gtk_menu_stop_scrolling (menu);
5055 gtk_widget_queue_draw (GTK_WIDGET (menu));
5059 last_visible = priv->lower_arrow_visible;
5060 priv->lower_arrow_visible = offset < menu_height - view_height;
5062 /* lower_arrow_visible may have changed, so requery the border */
5063 get_arrows_border (menu, &arrow_border);
5064 view_height -= arrow_border.bottom;
5066 if ((last_visible != priv->lower_arrow_visible) &&
5067 !priv->lower_arrow_visible)
5069 priv->lower_arrow_prelight = FALSE;
5071 /* If we hide the lower arrow, possibly remove timeout */
5072 if (priv->scroll_step > 0)
5074 gtk_menu_stop_scrolling (menu);
5075 gtk_widget_queue_draw (GTK_WIDGET (menu));
5079 y += arrow_border.top;
5082 /* Scroll the menu: */
5083 if (gtk_widget_get_realized (widget))
5084 gdk_window_move (priv->bin_window, 0, -offset);
5086 if (gtk_widget_get_realized (widget))
5087 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5089 priv->scroll_offset = offset;
5093 compute_child_offset (GtkMenu *menu,
5094 GtkWidget *menu_item,
5097 gboolean *is_last_child)
5099 GtkMenuPrivate *priv = menu->priv;
5100 gint item_top_attach;
5101 gint item_bottom_attach;
5102 gint child_offset = 0;
5105 get_effective_child_attach (menu_item, NULL, NULL,
5106 &item_top_attach, &item_bottom_attach);
5108 /* there is a possibility that we get called before _size_request,
5109 * so check the height table for safety.
5111 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5114 /* when we have a row with only invisible children, its height will
5115 * be zero, so there's no need to check WIDGET_VISIBLE here
5117 for (i = 0; i < item_top_attach; i++)
5118 child_offset += priv->heights[i];
5121 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5123 *offset = child_offset;
5125 *height = priv->heights[item_top_attach];
5131 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5132 GtkWidget *menu_item)
5134 GtkMenu *menu = GTK_MENU (menu_shell);
5135 GtkMenuPrivate *priv = menu->priv;
5136 GtkWidget *widget = GTK_WIDGET (menu_shell);
5137 gint child_offset, child_height;
5141 gboolean last_child = 0;
5143 /* We need to check if the selected item fully visible.
5144 * If not we need to scroll the menu so that it becomes fully
5147 if (compute_child_offset (menu, menu_item,
5148 &child_offset, &child_height, &last_child))
5150 guint vertical_padding;
5151 gboolean double_arrows;
5154 y = priv->scroll_offset;
5155 height = gdk_window_get_height (gtk_widget_get_window (widget));
5157 gtk_widget_style_get (widget,
5158 "vertical-padding", &vertical_padding,
5161 double_arrows = get_double_arrows (menu);
5162 get_menu_padding (widget, &padding);
5164 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5165 padding.top + padding.bottom +
5166 2 * vertical_padding;
5167 if (child_offset < y)
5169 /* Ignore the enter event we might get if the pointer
5172 menu_shell->priv->ignore_enter = TRUE;
5173 gtk_menu_scroll_to (menu, child_offset);
5177 GtkBorder arrow_border;
5181 get_arrows_border (menu, &arrow_border);
5182 if (!priv->tearoff_active)
5183 arrow_height = arrow_border.top + arrow_border.bottom;
5185 if (child_offset + child_height > y + height - arrow_height)
5188 if ((!last_child && !priv->tearoff_active) || double_arrows)
5189 arrow_height += arrow_border.bottom;
5191 y = child_offset + child_height - height + arrow_height;
5192 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5194 /* Need upper arrow */
5195 arrow_height += arrow_border.top;
5196 y = child_offset + child_height - height + arrow_height;
5198 /* Ignore the enter event we might get if the pointer
5201 menu_shell->priv->ignore_enter = TRUE;
5202 gtk_menu_scroll_to (menu, y);
5209 gtk_menu_select_item (GtkMenuShell *menu_shell,
5210 GtkWidget *menu_item)
5212 GtkMenu *menu = GTK_MENU (menu_shell);
5214 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5215 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5217 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5221 /* Reparent the menu, taking care of the refcounting
5223 * If unrealize is true we force a unrealize while reparenting the parent.
5224 * This can help eliminate flicker in some cases.
5226 * What happens is that when the menu is unrealized and then re-realized,
5227 * the allocations are as follows:
5229 * parent - 1x1 at (0,0)
5230 * child1 - 100x20 at (0,0)
5231 * child2 - 100x20 at (0,20)
5232 * child3 - 100x20 at (0,40)
5234 * That is, the parent is small but the children are full sized. Then,
5235 * when the queued_resize gets processed, the parent gets resized to
5238 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5239 * contains the following logic:
5241 * - if a move or resize operation on a window would change the clip
5242 * region on the children, then before the window is resized
5243 * the background for children is temporarily set to None, the
5244 * move/resize done, and the background for the children restored.
5246 * So, at the point where the parent is resized to final size, the
5247 * background for the children is temporarily None, and thus they
5248 * are not cleared to the background color and the previous background
5249 * (the image of the menu) is left in place.
5252 gtk_menu_reparent (GtkMenu *menu,
5253 GtkWidget *new_parent,
5256 GObject *object = G_OBJECT (menu);
5257 GtkWidget *widget = GTK_WIDGET (menu);
5258 gboolean was_floating = g_object_is_floating (object);
5260 g_object_ref_sink (object);
5264 g_object_ref (object);
5265 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5266 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5267 g_object_unref (object);
5270 gtk_widget_reparent (widget, new_parent);
5273 g_object_force_floating (object);
5275 g_object_unref (object);
5279 gtk_menu_show_all (GtkWidget *widget)
5281 /* Show children, but not self. */
5282 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5286 * gtk_menu_set_screen:
5288 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5289 * determined by the widget the menu is attached to
5291 * Sets the #GdkScreen on which the menu will be displayed.
5296 gtk_menu_set_screen (GtkMenu *menu,
5299 g_return_if_fail (GTK_IS_MENU (menu));
5300 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5302 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5306 menu_change_screen (menu, screen);
5310 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5312 attach_widget_screen_changed (attach_widget, NULL, menu);
5319 * @child: a #GtkMenuItem
5320 * @left_attach: The column number to attach the left side of the item to
5321 * @right_attach: The column number to attach the right side of the item to
5322 * @top_attach: The row number to attach the top of the item to
5323 * @bottom_attach: The row number to attach the bottom of the item to
5325 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5326 * an item will occupy is specified by @left_attach, @right_attach,
5327 * @top_attach and @bottom_attach. These each represent the leftmost,
5328 * rightmost, uppermost and lower column and row numbers of the table.
5329 * (Columns and rows are indexed from zero).
5331 * Note that this function is not related to gtk_menu_detach().
5336 gtk_menu_attach (GtkMenu *menu,
5341 guint bottom_attach)
5343 GtkMenuShell *menu_shell;
5346 g_return_if_fail (GTK_IS_MENU (menu));
5347 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5348 parent = gtk_widget_get_parent (child);
5349 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5350 g_return_if_fail (left_attach < right_attach);
5351 g_return_if_fail (top_attach < bottom_attach);
5353 menu_shell = GTK_MENU_SHELL (menu);
5357 AttachInfo *ai = get_attach_info (child);
5359 ai->left_attach = left_attach;
5360 ai->right_attach = right_attach;
5361 ai->top_attach = top_attach;
5362 ai->bottom_attach = bottom_attach;
5364 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5366 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5368 menu_queue_resize (menu);
5372 gtk_container_child_set (GTK_CONTAINER (parent), child,
5373 "left-attach", left_attach,
5374 "right-attach", right_attach,
5375 "top-attach", top_attach,
5376 "bottom-attach", bottom_attach,
5382 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5386 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5387 "gtk-menu-popup-delay", &popup_delay,
5394 find_child_containing (GtkMenuShell *menu_shell,
5402 /* find a child which includes the area given by
5403 * left, right, top, bottom.
5405 for (list = menu_shell->priv->children; list; list = list->next)
5409 if (!_gtk_menu_item_is_selectable (list->data))
5412 get_effective_child_attach (list->data, &l, &r, &t, &b);
5414 if (l <= left && right <= r && t <= top && bottom <= b)
5415 return GTK_WIDGET (list->data);
5422 gtk_menu_move_current (GtkMenuShell *menu_shell,
5423 GtkMenuDirectionType direction)
5425 GtkMenu *menu = GTK_MENU (menu_shell);
5428 GtkWidget *match = NULL;
5430 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5434 case GTK_MENU_DIR_CHILD:
5435 direction = GTK_MENU_DIR_PARENT;
5437 case GTK_MENU_DIR_PARENT:
5438 direction = GTK_MENU_DIR_CHILD;
5444 /* use special table menu key bindings */
5445 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5447 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5449 if (direction == GTK_MENU_DIR_NEXT)
5451 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5453 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5461 for (i = 0; i < t; i++)
5463 match = find_child_containing (menu_shell,
5464 l, l + 1, i, i + 1);
5470 else if (direction == GTK_MENU_DIR_PREV)
5472 for (i = t; i > 0; i--)
5474 match = find_child_containing (menu_shell,
5475 l, l + 1, i - 1, i);
5483 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5485 match = find_child_containing (menu_shell,
5486 l, l + 1, i - 1, i);
5492 else if (direction == GTK_MENU_DIR_PARENT)
5494 /* we go one left if possible */
5496 match = find_child_containing (menu_shell,
5497 l - 1, l, t, t + 1);
5501 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5504 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5505 match = menu_shell->priv->active_menu_item;
5508 else if (direction == GTK_MENU_DIR_CHILD)
5510 /* we go one right if possible */
5511 if (r < gtk_menu_get_n_columns (menu))
5512 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5516 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5518 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5520 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5521 match = menu_shell->priv->active_menu_item;
5527 gtk_menu_shell_select_item (menu_shell, match);
5532 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5536 get_visible_size (GtkMenu *menu)
5538 GtkMenuPrivate *priv = menu->priv;
5539 GtkAllocation allocation;
5540 GtkWidget *widget = GTK_WIDGET (menu);
5541 GtkContainer *container = GTK_CONTAINER (menu);
5545 gtk_widget_get_allocation (widget, &allocation);
5546 get_menu_padding (widget, &padding);
5548 menu_height = (allocation.height -
5549 (2 * gtk_container_get_border_width (container)) -
5550 padding.top - padding.bottom);
5552 if (!priv->tearoff_active)
5554 GtkBorder arrow_border;
5556 get_arrows_border (menu, &arrow_border);
5557 menu_height -= arrow_border.top;
5558 menu_height -= arrow_border.bottom;
5564 /* Find the sensitive on-screen child containing @y, or if none,
5565 * the nearest selectable onscreen child. (%NULL if none)
5568 child_at (GtkMenu *menu,
5571 GtkMenuPrivate *priv = menu->priv;
5572 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5573 GtkWidget *child = NULL;
5574 gint child_offset = 0;
5577 gint lower, upper; /* Onscreen bounds */
5579 menu_height = get_visible_size (menu);
5580 lower = priv->scroll_offset;
5581 upper = priv->scroll_offset + menu_height;
5583 for (children = menu_shell->priv->children; children; children = children->next)
5585 if (gtk_widget_get_visible (children->data))
5587 GtkRequisition child_requisition;
5589 gtk_widget_get_preferred_size (children->data,
5590 &child_requisition, NULL);
5592 if (_gtk_menu_item_is_selectable (children->data) &&
5593 child_offset >= lower &&
5594 child_offset + child_requisition.height <= upper)
5596 child = children->data;
5598 if (child_offset + child_requisition.height > y &&
5599 !GTK_IS_TEAROFF_MENU_ITEM (child))
5603 child_offset += child_requisition.height;
5611 get_menu_height (GtkMenu *menu)
5613 GtkMenuPrivate *priv = menu->priv;
5614 GtkAllocation allocation;
5615 GtkWidget *widget = GTK_WIDGET (menu);
5619 gtk_widget_get_allocation (widget, &allocation);
5620 get_menu_padding (widget, &padding);
5622 height = allocation.height;
5623 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5624 padding.top + padding.bottom;
5626 if (!priv->tearoff_active)
5628 GtkBorder arrow_border;
5630 get_arrows_border (menu, &arrow_border);
5631 height -= arrow_border.top;
5632 height -= arrow_border.bottom;
5639 gtk_menu_real_move_scroll (GtkMenu *menu,
5642 GtkMenuPrivate *priv = menu->priv;
5643 gint page_size = get_visible_size (menu);
5644 gint end_position = get_menu_height (menu);
5645 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5649 case GTK_SCROLL_PAGE_UP:
5650 case GTK_SCROLL_PAGE_DOWN:
5654 gint child_offset = 0;
5655 gboolean old_upper_arrow_visible;
5658 if (type == GTK_SCROLL_PAGE_UP)
5663 if (menu_shell->priv->active_menu_item)
5667 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5668 &child_offset, &child_height, NULL);
5669 child_offset += child_height / 2;
5672 menu_shell->priv->ignore_enter = TRUE;
5673 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5674 old_offset = priv->scroll_offset;
5676 new_offset = priv->scroll_offset + step;
5677 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5679 gtk_menu_scroll_to (menu, new_offset);
5681 if (menu_shell->priv->active_menu_item)
5683 GtkWidget *new_child;
5684 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5685 GtkBorder arrow_border;
5686 get_arrows_border (menu, &arrow_border);
5688 if (priv->scroll_offset != old_offset)
5689 step = priv->scroll_offset - old_offset;
5691 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5693 new_child = child_at (menu, child_offset + step);
5695 gtk_menu_shell_select_item (menu_shell, new_child);
5699 case GTK_SCROLL_START:
5700 /* Ignore the enter event we might get if the pointer is on the menu */
5701 menu_shell->priv->ignore_enter = TRUE;
5702 gtk_menu_scroll_to (menu, 0);
5703 gtk_menu_shell_select_first (menu_shell, TRUE);
5705 case GTK_SCROLL_END:
5706 /* Ignore the enter event we might get if the pointer is on the menu */
5707 menu_shell->priv->ignore_enter = TRUE;
5708 gtk_menu_scroll_to (menu, end_position - page_size);
5709 _gtk_menu_shell_select_last (menu_shell, TRUE);
5718 * gtk_menu_set_monitor:
5720 * @monitor_num: the number of the monitor on which the menu should
5723 * Informs GTK+ on which monitor a menu should be popped up.
5724 * See gdk_screen_get_monitor_geometry().
5726 * This function should be called from a #GtkMenuPositionFunc
5727 * if the menu should not appear on the same monitor as the pointer.
5728 * This information can't be reliably inferred from the coordinates
5729 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5730 * these coordinates may extend beyond the monitor boundaries or even
5731 * the screen boundaries.
5736 gtk_menu_set_monitor (GtkMenu *menu,
5739 GtkMenuPrivate *priv = menu->priv;
5741 g_return_if_fail (GTK_IS_MENU (menu));
5743 priv->monitor_num = monitor_num;
5747 * gtk_menu_get_monitor:
5750 * Retrieves the number of the monitor on which to show the menu.
5752 * Returns: the number of the monitor on which the menu should
5753 * be popped up or -1, if no monitor has been set
5758 gtk_menu_get_monitor (GtkMenu *menu)
5760 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5762 return menu->priv->monitor_num;
5766 * gtk_menu_get_for_attach_widget:
5767 * @widget: a #GtkWidget
5769 * Returns a list of the menus which are attached to this widget.
5770 * This list is owned by GTK+ and must not be modified.
5772 * Return value: (element-type GtkWidget) (transfer none): the list
5773 * of menus attached to his widget.
5778 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5782 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5784 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5790 gtk_menu_grab_notify (GtkWidget *widget,
5791 gboolean was_grabbed)
5793 GtkWidget *toplevel;
5794 GtkWindowGroup *group;
5798 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5801 !gtk_widget_device_is_shadowed (widget, pointer))
5804 toplevel = gtk_widget_get_toplevel (widget);
5806 if (!GTK_IS_WINDOW (toplevel))
5809 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5810 grab = gtk_window_group_get_current_device_grab (group, pointer);
5812 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5813 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5817 * gtk_menu_set_reserve_toggle_size:
5819 * @reserve_toggle_size: whether to reserve size for toggles
5821 * Sets whether the menu should reserve space for drawing toggles
5822 * or icons, regardless of their actual presence.
5827 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5828 gboolean reserve_toggle_size)
5830 GtkMenuPrivate *priv = menu->priv;
5831 gboolean no_toggle_size;
5833 g_return_if_fail (GTK_IS_MENU (menu));
5835 no_toggle_size = !reserve_toggle_size;
5837 if (priv->no_toggle_size != no_toggle_size)
5839 priv->no_toggle_size = no_toggle_size;
5841 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5846 * gtk_menu_get_reserve_toggle_size:
5849 * Returns whether the menu reserves space for toggles and
5850 * icons, regardless of their actual presence.
5852 * Returns: Whether the menu reserves toggle space
5857 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5859 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5861 return !menu->priv->no_toggle_size;