1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * @Short_description: A menu widget
30 * A #GtkMenu is a #GtkMenuShell that implements a drop down menu
31 * consisting of a list of #GtkMenuItem objects which can be navigated
32 * and activated by the user to perform application functions.
34 * A #GtkMenu is most commonly dropped down by activating a
35 * #GtkMenuItem in a #GtkMenuBar or popped up by activating a
36 * #GtkMenuItem in another #GtkMenu.
38 * A #GtkMenu can also be popped up by activating a #GtkOptionMenu.
39 * Other composite widgets such as the #GtkNotebook can pop up a
42 * Applications can display a #GtkMenu as a popup menu by calling the
43 * gtk_menu_popup() function. The example below shows how an application
44 * can pop up a menu when the 3rd mouse button is pressed.
47 * <title>Connecting the popup signal handler.</title>
49 * /<!---->* connect our handler which will popup the menu *<!---->/
50 * g_signal_connect_swapped (window, "button_press_event",
51 * G_CALLBACK (my_popup_handler), menu);
56 * <title>Signal handler which displays a popup menu.</title>
59 * my_popup_handler (GtkWidget *widget, GdkEvent *event)
62 * GdkEventButton *event_button;
64 * g_return_val_if_fail (widget != NULL, FALSE);
65 * g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
66 * g_return_val_if_fail (event != NULL, FALSE);
68 * /<!---->* The "widget" is the menu that was supplied when
69 * * g_signal_connect_swapped() was called.
71 * menu = GTK_MENU (widget);
73 * if (event->type == GDK_BUTTON_PRESS)
75 * event_button = (GdkEventButton *) event;
76 * if (event_button->button == GDK_BUTTON_SECONDARY)
78 * gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
79 * event_button->button, event_button->time);
94 #include <gobject/gvaluecollector.h>
96 #include "gtkaccellabel.h"
97 #include "gtkaccelmap.h"
98 #include "gtkbindings.h"
99 #include "gtkcheckmenuitem.h"
101 #include "gtkmarshalers.h"
102 #include "gtkmenuprivate.h"
103 #include "gtkmenuitemprivate.h"
104 #include "gtkmenushellprivate.h"
105 #include "gtkwindow.h"
107 #include "gtkscrollbar.h"
108 #include "gtksettings.h"
109 #include "gtkprivate.h"
110 #include "gtkwidgetprivate.h"
112 #include "gtktypebuiltins.h"
114 #include "deprecated/gtktearoffmenuitem.h"
117 #include "a11y/gtkmenuaccessible.h"
119 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
120 * extends below the submenu
123 #define MENU_SCROLL_STEP1 8
124 #define MENU_SCROLL_STEP2 15
125 #define MENU_SCROLL_FAST_ZONE 8
126 #define MENU_SCROLL_TIMEOUT1 50
127 #define MENU_SCROLL_TIMEOUT2 20
129 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
130 #define ATTACHED_MENUS "gtk-attached-menus"
132 typedef struct _GtkMenuAttachData GtkMenuAttachData;
133 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
135 struct _GtkMenuAttachData
137 GtkWidget *attach_widget;
138 GtkMenuDetachFunc detacher;
141 struct _GtkMenuPopdownData
153 gint effective_left_attach;
154 gint effective_right_attach;
155 gint effective_top_attach;
156 gint effective_bottom_attach;
173 PROP_RESERVE_TOGGLE_SIZE
178 CHILD_PROP_LEFT_ATTACH,
179 CHILD_PROP_RIGHT_ATTACH,
180 CHILD_PROP_TOP_ATTACH,
181 CHILD_PROP_BOTTOM_ATTACH
184 static void gtk_menu_set_property (GObject *object,
188 static void gtk_menu_get_property (GObject *object,
192 static void gtk_menu_set_child_property(GtkContainer *container,
197 static void gtk_menu_get_child_property(GtkContainer *container,
202 static void gtk_menu_destroy (GtkWidget *widget);
203 static void gtk_menu_realize (GtkWidget *widget);
204 static void gtk_menu_unrealize (GtkWidget *widget);
205 static void gtk_menu_size_allocate (GtkWidget *widget,
206 GtkAllocation *allocation);
207 static void gtk_menu_show (GtkWidget *widget);
208 static gboolean gtk_menu_draw (GtkWidget *widget,
210 static gboolean gtk_menu_key_press (GtkWidget *widget,
212 static gboolean gtk_menu_scroll (GtkWidget *widget,
213 GdkEventScroll *event);
214 static gboolean gtk_menu_button_press (GtkWidget *widget,
215 GdkEventButton *event);
216 static gboolean gtk_menu_button_release (GtkWidget *widget,
217 GdkEventButton *event);
218 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
219 GdkEventMotion *event);
220 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
221 GdkEventCrossing *event);
222 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
223 GdkEventCrossing *event);
224 static void gtk_menu_scroll_to (GtkMenu *menu,
226 static void gtk_menu_grab_notify (GtkWidget *widget,
227 gboolean was_grabbed);
229 static void gtk_menu_stop_scrolling (GtkMenu *menu);
230 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
231 static gboolean gtk_menu_scroll_timeout (gpointer data);
232 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
233 static void gtk_menu_start_scrolling (GtkMenu *menu);
235 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
236 GtkWidget *menu_item);
237 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
238 GtkWidget *menu_item);
239 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
242 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
244 static void gtk_menu_handle_scrolling (GtkMenu *menu,
249 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
251 static void gtk_menu_style_updated (GtkWidget *widget);
252 static gboolean gtk_menu_focus (GtkWidget *widget,
253 GtkDirectionType direction);
254 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
255 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
256 GtkMenuDirectionType direction);
257 static void gtk_menu_real_move_scroll (GtkMenu *menu,
260 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
261 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
262 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
265 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
266 GtkMenuItem *menu_item,
267 GdkEventCrossing *event);
269 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
270 static void gtk_menu_show_all (GtkWidget *widget);
271 static void gtk_menu_position (GtkMenu *menu,
272 gboolean set_scroll_offset);
273 static void gtk_menu_reparent (GtkMenu *menu,
274 GtkWidget *new_parent,
276 static void gtk_menu_remove (GtkContainer *menu,
279 static void gtk_menu_update_title (GtkMenu *menu);
281 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
282 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
284 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
286 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
287 gboolean group_changed);
289 static void gtk_menu_get_preferred_width (GtkWidget *widget,
292 static void gtk_menu_get_preferred_height (GtkWidget *widget,
295 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
301 static const gchar attach_data_key[] = "gtk-menu-attach-data";
303 static guint menu_signals[LAST_SIGNAL] = { 0 };
305 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
308 menu_queue_resize (GtkMenu *menu)
310 GtkMenuPrivate *priv = menu->priv;
312 priv->have_layout = FALSE;
313 gtk_widget_queue_resize (GTK_WIDGET (menu));
317 attach_info_free (AttachInfo *info)
319 g_slice_free (AttachInfo, info);
323 get_attach_info (GtkWidget *child)
325 GObject *object = G_OBJECT (child);
326 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
330 ai = g_slice_new0 (AttachInfo);
331 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
332 (GDestroyNotify) attach_info_free);
339 is_grid_attached (AttachInfo *ai)
341 return (ai->left_attach >= 0 &&
342 ai->right_attach >= 0 &&
343 ai->top_attach >= 0 &&
344 ai->bottom_attach >= 0);
348 menu_ensure_layout (GtkMenu *menu)
350 GtkMenuPrivate *priv = menu->priv;
352 if (!priv->have_layout)
354 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
358 gint max_right_attach;
359 gint max_bottom_attach;
361 /* Find extents of gridded portion
363 max_right_attach = 1;
364 max_bottom_attach = 0;
366 for (l = menu_shell->priv->children; l; l = l->next)
368 GtkWidget *child = l->data;
369 AttachInfo *ai = get_attach_info (child);
371 if (is_grid_attached (ai))
373 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
374 max_right_attach = MAX (max_right_attach, ai->right_attach);
378 /* Find empty rows */
379 row_occupied = g_malloc0 (max_bottom_attach);
381 for (l = menu_shell->priv->children; l; l = l->next)
383 GtkWidget *child = l->data;
384 AttachInfo *ai = get_attach_info (child);
386 if (is_grid_attached (ai))
390 for (i = ai->top_attach; i < ai->bottom_attach; i++)
391 row_occupied[i] = TRUE;
395 /* Lay non-grid-items out in those rows
398 for (l = menu_shell->priv->children; l; l = l->next)
400 GtkWidget *child = l->data;
401 AttachInfo *ai = get_attach_info (child);
403 if (!is_grid_attached (ai))
405 while (current_row < max_bottom_attach && row_occupied[current_row])
408 ai->effective_left_attach = 0;
409 ai->effective_right_attach = max_right_attach;
410 ai->effective_top_attach = current_row;
411 ai->effective_bottom_attach = current_row + 1;
417 ai->effective_left_attach = ai->left_attach;
418 ai->effective_right_attach = ai->right_attach;
419 ai->effective_top_attach = ai->top_attach;
420 ai->effective_bottom_attach = ai->bottom_attach;
424 g_free (row_occupied);
426 priv->n_rows = MAX (current_row, max_bottom_attach);
427 priv->n_columns = max_right_attach;
428 priv->have_layout = TRUE;
434 gtk_menu_get_n_columns (GtkMenu *menu)
436 GtkMenuPrivate *priv = menu->priv;
438 menu_ensure_layout (menu);
440 return priv->n_columns;
444 gtk_menu_get_n_rows (GtkMenu *menu)
446 GtkMenuPrivate *priv = menu->priv;
448 menu_ensure_layout (menu);
454 get_effective_child_attach (GtkWidget *child,
460 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
463 menu_ensure_layout (menu);
465 ai = get_attach_info (child);
468 *l = ai->effective_left_attach;
470 *r = ai->effective_right_attach;
472 *t = ai->effective_top_attach;
474 *b = ai->effective_bottom_attach;
479 gtk_menu_class_init (GtkMenuClass *class)
481 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
482 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
483 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
484 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
485 GtkBindingSet *binding_set;
487 gobject_class->set_property = gtk_menu_set_property;
488 gobject_class->get_property = gtk_menu_get_property;
490 widget_class->destroy = gtk_menu_destroy;
491 widget_class->realize = gtk_menu_realize;
492 widget_class->unrealize = gtk_menu_unrealize;
493 widget_class->size_allocate = gtk_menu_size_allocate;
494 widget_class->show = gtk_menu_show;
495 widget_class->draw = gtk_menu_draw;
496 widget_class->scroll_event = gtk_menu_scroll;
497 widget_class->key_press_event = gtk_menu_key_press;
498 widget_class->button_press_event = gtk_menu_button_press;
499 widget_class->button_release_event = gtk_menu_button_release;
500 widget_class->motion_notify_event = gtk_menu_motion_notify;
501 widget_class->show_all = gtk_menu_show_all;
502 widget_class->enter_notify_event = gtk_menu_enter_notify;
503 widget_class->leave_notify_event = gtk_menu_leave_notify;
504 widget_class->style_updated = gtk_menu_style_updated;
505 widget_class->focus = gtk_menu_focus;
506 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
507 widget_class->grab_notify = gtk_menu_grab_notify;
508 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
509 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
510 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
512 container_class->remove = gtk_menu_remove;
513 container_class->get_child_property = gtk_menu_get_child_property;
514 container_class->set_child_property = gtk_menu_set_child_property;
516 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
517 menu_shell_class->deactivate = gtk_menu_deactivate;
518 menu_shell_class->select_item = gtk_menu_select_item;
519 menu_shell_class->insert = gtk_menu_real_insert;
520 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
521 menu_shell_class->move_current = gtk_menu_move_current;
524 * GtkMenu::move-scroll:
526 * @scroll_type: a #GtkScrollType
528 menu_signals[MOVE_SCROLL] =
529 g_signal_new_class_handler (I_("move-scroll"),
530 G_OBJECT_CLASS_TYPE (gobject_class),
531 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
532 G_CALLBACK (gtk_menu_real_move_scroll),
534 _gtk_marshal_VOID__ENUM,
536 GTK_TYPE_SCROLL_TYPE);
541 * The index of the currently selected menu item, or -1 if no
542 * menu item is selected.
546 g_object_class_install_property (gobject_class,
548 g_param_spec_int ("active",
550 P_("The currently selected menu item"),
552 GTK_PARAM_READWRITE));
555 * GtkMenu:accel-group:
557 * The accel group holding accelerators for the menu.
561 g_object_class_install_property (gobject_class,
563 g_param_spec_object ("accel-group",
565 P_("The accel group holding accelerators for the menu"),
566 GTK_TYPE_ACCEL_GROUP,
567 GTK_PARAM_READWRITE));
570 * GtkMenu:accel-path:
572 * An accel path used to conveniently construct accel paths of child items.
576 g_object_class_install_property (gobject_class,
578 g_param_spec_string ("accel-path",
580 P_("An accel path used to conveniently construct accel paths of child items"),
582 GTK_PARAM_READWRITE));
585 * GtkMenu:attach-widget:
587 * The widget the menu is attached to. Setting this property attaches
588 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
589 * use gtk_menu_attach_to_widget() directly.
593 g_object_class_install_property (gobject_class,
595 g_param_spec_object ("attach-widget",
597 P_("The widget the menu is attached to"),
599 GTK_PARAM_READWRITE));
601 g_object_class_install_property (gobject_class,
603 g_param_spec_string ("tearoff-title",
605 P_("A title that may be displayed by the window manager when this menu is torn-off"),
607 GTK_PARAM_READWRITE));
610 * GtkMenu:tearoff-state:
612 * A boolean that indicates whether the menu is torn-off.
616 g_object_class_install_property (gobject_class,
618 g_param_spec_boolean ("tearoff-state",
620 P_("A boolean that indicates whether the menu is torn-off"),
622 GTK_PARAM_READWRITE));
627 * The monitor the menu will be popped up on.
631 g_object_class_install_property (gobject_class,
633 g_param_spec_int ("monitor",
635 P_("The monitor the menu will be popped up on"),
637 GTK_PARAM_READWRITE));
639 gtk_widget_class_install_style_property (widget_class,
640 g_param_spec_int ("vertical-padding",
641 P_("Vertical Padding"),
642 P_("Extra space at the top and bottom of the menu"),
646 GTK_PARAM_READABLE));
649 * GtkMenu:reserve-toggle-size:
651 * A boolean that indicates whether the menu reserves space for
652 * toggles and icons, regardless of their actual presence.
654 * This property should only be changed from its default value
655 * for special-purposes such as tabular menus. Regular menus that
656 * are connected to a menu bar or context menus should reserve
657 * toggle space for consistency.
661 g_object_class_install_property (gobject_class,
662 PROP_RESERVE_TOGGLE_SIZE,
663 g_param_spec_boolean ("reserve-toggle-size",
664 P_("Reserve Toggle Size"),
665 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
667 GTK_PARAM_READWRITE));
669 gtk_widget_class_install_style_property (widget_class,
670 g_param_spec_int ("horizontal-padding",
671 P_("Horizontal Padding"),
672 P_("Extra space at the left and right edges of the menu"),
676 GTK_PARAM_READABLE));
678 gtk_widget_class_install_style_property (widget_class,
679 g_param_spec_int ("vertical-offset",
680 P_("Vertical Offset"),
681 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
685 GTK_PARAM_READABLE));
687 gtk_widget_class_install_style_property (widget_class,
688 g_param_spec_int ("horizontal-offset",
689 P_("Horizontal Offset"),
690 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
694 GTK_PARAM_READABLE));
696 gtk_widget_class_install_style_property (widget_class,
697 g_param_spec_boolean ("double-arrows",
699 P_("When scrolling, always show both arrows."),
701 GTK_PARAM_READABLE));
704 * GtkMenu:arrow-placement:
706 * Indicates where scroll arrows should be placed.
710 gtk_widget_class_install_style_property (widget_class,
711 g_param_spec_enum ("arrow-placement",
712 P_("Arrow Placement"),
713 P_("Indicates where scroll arrows should be placed"),
714 GTK_TYPE_ARROW_PLACEMENT,
716 GTK_PARAM_READABLE));
718 gtk_container_class_install_child_property (container_class,
719 CHILD_PROP_LEFT_ATTACH,
720 g_param_spec_int ("left-attach",
722 P_("The column number to attach the left side of the child to"),
724 GTK_PARAM_READWRITE));
726 gtk_container_class_install_child_property (container_class,
727 CHILD_PROP_RIGHT_ATTACH,
728 g_param_spec_int ("right-attach",
730 P_("The column number to attach the right side of the child to"),
732 GTK_PARAM_READWRITE));
734 gtk_container_class_install_child_property (container_class,
735 CHILD_PROP_TOP_ATTACH,
736 g_param_spec_int ("top-attach",
738 P_("The row number to attach the top of the child to"),
740 GTK_PARAM_READWRITE));
742 gtk_container_class_install_child_property (container_class,
743 CHILD_PROP_BOTTOM_ATTACH,
744 g_param_spec_int ("bottom-attach",
746 P_("The row number to attach the bottom of the child to"),
748 GTK_PARAM_READWRITE));
751 * GtkMenu:arrow-scaling
753 * Arbitrary constant to scale down the size of the scroll arrow.
757 gtk_widget_class_install_style_property (widget_class,
758 g_param_spec_float ("arrow-scaling",
760 P_("Arbitrary constant to scale down the size of the scroll arrow"),
762 GTK_PARAM_READABLE));
764 binding_set = gtk_binding_set_by_class (class);
765 gtk_binding_entry_add_signal (binding_set,
767 I_("move-current"), 1,
768 GTK_TYPE_MENU_DIRECTION_TYPE,
770 gtk_binding_entry_add_signal (binding_set,
773 GTK_TYPE_MENU_DIRECTION_TYPE,
775 gtk_binding_entry_add_signal (binding_set,
778 GTK_TYPE_MENU_DIRECTION_TYPE,
780 gtk_binding_entry_add_signal (binding_set,
783 GTK_TYPE_MENU_DIRECTION_TYPE,
785 gtk_binding_entry_add_signal (binding_set,
788 GTK_TYPE_MENU_DIRECTION_TYPE,
789 GTK_MENU_DIR_PARENT);
790 gtk_binding_entry_add_signal (binding_set,
793 GTK_TYPE_MENU_DIRECTION_TYPE,
794 GTK_MENU_DIR_PARENT);
795 gtk_binding_entry_add_signal (binding_set,
798 GTK_TYPE_MENU_DIRECTION_TYPE,
800 gtk_binding_entry_add_signal (binding_set,
803 GTK_TYPE_MENU_DIRECTION_TYPE,
805 gtk_binding_entry_add_signal (binding_set,
808 GTK_TYPE_SCROLL_TYPE,
810 gtk_binding_entry_add_signal (binding_set,
813 GTK_TYPE_SCROLL_TYPE,
815 gtk_binding_entry_add_signal (binding_set,
818 GTK_TYPE_SCROLL_TYPE,
820 gtk_binding_entry_add_signal (binding_set,
823 GTK_TYPE_SCROLL_TYPE,
825 gtk_binding_entry_add_signal (binding_set,
828 GTK_TYPE_SCROLL_TYPE,
830 gtk_binding_entry_add_signal (binding_set,
831 GDK_KEY_KP_Page_Up, 0,
833 GTK_TYPE_SCROLL_TYPE,
835 gtk_binding_entry_add_signal (binding_set,
836 GDK_KEY_Page_Down, 0,
838 GTK_TYPE_SCROLL_TYPE,
839 GTK_SCROLL_PAGE_DOWN);
840 gtk_binding_entry_add_signal (binding_set,
841 GDK_KEY_KP_Page_Down, 0,
843 GTK_TYPE_SCROLL_TYPE,
844 GTK_SCROLL_PAGE_DOWN);
846 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
848 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
853 gtk_menu_set_property (GObject *object,
858 GtkMenu *menu = GTK_MENU (object);
863 gtk_menu_set_active (menu, g_value_get_int (value));
865 case PROP_ACCEL_GROUP:
866 gtk_menu_set_accel_group (menu, g_value_get_object (value));
868 case PROP_ACCEL_PATH:
869 gtk_menu_set_accel_path (menu, g_value_get_string (value));
871 case PROP_ATTACH_WIDGET:
875 widget = gtk_menu_get_attach_widget (menu);
877 gtk_menu_detach (menu);
879 widget = (GtkWidget*) g_value_get_object (value);
881 gtk_menu_attach_to_widget (menu, widget, NULL);
884 case PROP_TEAROFF_STATE:
885 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
887 case PROP_TEAROFF_TITLE:
888 gtk_menu_set_title (menu, g_value_get_string (value));
891 gtk_menu_set_monitor (menu, g_value_get_int (value));
893 case PROP_RESERVE_TOGGLE_SIZE:
894 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
897 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
903 gtk_menu_get_property (GObject *object,
908 GtkMenu *menu = GTK_MENU (object);
913 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
915 case PROP_ACCEL_GROUP:
916 g_value_set_object (value, gtk_menu_get_accel_group (menu));
918 case PROP_ACCEL_PATH:
919 g_value_set_string (value, gtk_menu_get_accel_path (menu));
921 case PROP_ATTACH_WIDGET:
922 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
924 case PROP_TEAROFF_STATE:
925 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
927 case PROP_TEAROFF_TITLE:
928 g_value_set_string (value, gtk_menu_get_title (menu));
931 g_value_set_int (value, gtk_menu_get_monitor (menu));
933 case PROP_RESERVE_TOGGLE_SIZE:
934 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
937 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
943 gtk_menu_set_child_property (GtkContainer *container,
949 GtkMenu *menu = GTK_MENU (container);
950 AttachInfo *ai = get_attach_info (child);
954 case CHILD_PROP_LEFT_ATTACH:
955 ai->left_attach = g_value_get_int (value);
957 case CHILD_PROP_RIGHT_ATTACH:
958 ai->right_attach = g_value_get_int (value);
960 case CHILD_PROP_TOP_ATTACH:
961 ai->top_attach = g_value_get_int (value);
963 case CHILD_PROP_BOTTOM_ATTACH:
964 ai->bottom_attach = g_value_get_int (value);
968 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
972 menu_queue_resize (menu);
976 gtk_menu_get_child_property (GtkContainer *container,
982 AttachInfo *ai = get_attach_info (child);
986 case CHILD_PROP_LEFT_ATTACH:
987 g_value_set_int (value, ai->left_attach);
989 case CHILD_PROP_RIGHT_ATTACH:
990 g_value_set_int (value, ai->right_attach);
992 case CHILD_PROP_TOP_ATTACH:
993 g_value_set_int (value, ai->top_attach);
995 case CHILD_PROP_BOTTOM_ATTACH:
996 g_value_set_int (value, ai->bottom_attach);
1000 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1006 gtk_menu_window_event (GtkWidget *window,
1010 gboolean handled = FALSE;
1012 g_object_ref (window);
1013 g_object_ref (menu);
1015 switch (event->type)
1018 case GDK_KEY_RELEASE:
1019 handled = gtk_widget_event (menu, event);
1025 g_object_unref (window);
1026 g_object_unref (menu);
1032 gtk_menu_init (GtkMenu *menu)
1034 GtkMenuPrivate *priv;
1035 GtkStyleContext *context;
1037 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1041 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1042 "type", GTK_WINDOW_POPUP,
1045 "signal::event", gtk_menu_window_event, menu,
1046 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1048 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1049 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1051 /* Refloat the menu, so that reference counting for the menu isn't
1052 * affected by it being a child of the toplevel
1054 g_object_force_floating (G_OBJECT (menu));
1055 priv->needs_destruction_ref = TRUE;
1057 priv->monitor_num = -1;
1059 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1060 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1064 gtk_menu_destroy (GtkWidget *widget)
1066 GtkMenu *menu = GTK_MENU (widget);
1067 GtkMenuPrivate *priv = menu->priv;
1068 GtkMenuAttachData *data;
1070 gtk_menu_remove_scroll_timeout (menu);
1072 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1074 gtk_menu_detach (menu);
1076 gtk_menu_stop_navigating_submenu (menu);
1078 if (priv->old_active_menu_item)
1079 g_clear_object (&priv->old_active_menu_item);
1081 /* Add back the reference count for being a child */
1082 if (priv->needs_destruction_ref)
1084 priv->needs_destruction_ref = FALSE;
1085 g_object_ref (widget);
1088 if (priv->accel_group)
1089 g_clear_object (&priv->accel_group);
1092 gtk_widget_destroy (priv->toplevel);
1094 if (priv->tearoff_window)
1095 gtk_widget_destroy (priv->tearoff_window);
1099 g_free (priv->heights);
1100 priv->heights = NULL;
1105 g_free (priv->title);
1109 if (priv->position_func_data_destroy)
1111 priv->position_func_data_destroy (priv->position_func_data);
1112 priv->position_func_data = NULL;
1113 priv->position_func_data_destroy = NULL;
1116 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1120 menu_change_screen (GtkMenu *menu,
1121 GdkScreen *new_screen)
1123 GtkMenuPrivate *priv = menu->priv;
1125 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1127 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1133 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1134 gtk_menu_position (menu, TRUE);
1137 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1138 priv->monitor_num = -1;
1142 attach_widget_screen_changed (GtkWidget *attach_widget,
1143 GdkScreen *previous_screen,
1146 if (gtk_widget_has_screen (attach_widget) &&
1147 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1148 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1152 * gtk_menu_attach_to_widget:
1154 * @attach_widget: the #GtkWidget that the menu will be attached to
1155 * @detacher: (scope async)(allow-none): the user supplied callback function
1156 * that will be called when the menu calls gtk_menu_detach()
1158 * Attaches the menu to the widget and provides a callback function
1159 * that will be invoked when the menu calls gtk_menu_detach() during
1163 gtk_menu_attach_to_widget (GtkMenu *menu,
1164 GtkWidget *attach_widget,
1165 GtkMenuDetachFunc detacher)
1167 GtkMenuAttachData *data;
1170 g_return_if_fail (GTK_IS_MENU (menu));
1171 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1173 /* keep this function in sync with gtk_widget_set_parent() */
1174 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1177 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1178 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1182 g_object_ref_sink (menu);
1184 data = g_slice_new (GtkMenuAttachData);
1185 data->attach_widget = attach_widget;
1187 g_signal_connect (attach_widget, "screen-changed",
1188 G_CALLBACK (attach_widget_screen_changed), menu);
1189 attach_widget_screen_changed (attach_widget, NULL, menu);
1191 data->detacher = detacher;
1192 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1193 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1194 if (!g_list_find (list, menu))
1195 list = g_list_prepend (list, menu);
1197 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1198 (GDestroyNotify) g_list_free);
1200 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1201 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1203 /* Attach the widget to the toplevel window. */
1204 gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), attach_widget);
1206 /* Fallback title for menu comes from attach widget */
1207 gtk_menu_update_title (menu);
1209 g_object_notify (G_OBJECT (menu), "attach-widget");
1213 * gtk_menu_get_attach_widget:
1216 * Returns the #GtkWidget that the menu is attached to.
1218 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1221 gtk_menu_get_attach_widget (GtkMenu *menu)
1223 GtkMenuAttachData *data;
1225 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1227 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1229 return data->attach_widget;
1237 * Detaches the menu from the widget to which it had been attached.
1238 * This function will call the callback function, @detacher, provided
1239 * when the gtk_menu_attach_to_widget() function was called.
1242 gtk_menu_detach (GtkMenu *menu)
1244 GtkMenuAttachData *data;
1247 g_return_if_fail (GTK_IS_MENU (menu));
1249 /* keep this function in sync with gtk_widget_unparent() */
1250 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1253 g_warning ("gtk_menu_detach(): menu is not attached");
1256 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1258 /* Detach the toplevel window. */
1259 gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), NULL);
1261 g_signal_handlers_disconnect_by_func (data->attach_widget,
1262 (gpointer) attach_widget_screen_changed,
1266 data->detacher (data->attach_widget, menu);
1267 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1268 list = g_list_remove (list, menu);
1270 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1271 (GDestroyNotify) g_list_free);
1273 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1275 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1276 gtk_widget_unrealize (GTK_WIDGET (menu));
1278 g_slice_free (GtkMenuAttachData, data);
1280 /* Fallback title for menu comes from attach widget */
1281 gtk_menu_update_title (menu);
1283 g_object_unref (menu);
1287 gtk_menu_remove (GtkContainer *container,
1290 GtkMenu *menu = GTK_MENU (container);
1291 GtkMenuPrivate *priv = menu->priv;
1293 /* Clear out old_active_menu_item if it matches the item we are removing */
1294 if (priv->old_active_menu_item == widget)
1295 g_clear_object (&priv->old_active_menu_item);
1297 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1299 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1301 menu_queue_resize (menu);
1307 * Creates a new #GtkMenu
1309 * Returns: a new #GtkMenu
1314 return g_object_new (GTK_TYPE_MENU, NULL);
1318 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1322 GtkMenu *menu = GTK_MENU (menu_shell);
1323 GtkMenuPrivate *priv = menu->priv;
1324 AttachInfo *ai = get_attach_info (child);
1326 ai->left_attach = -1;
1327 ai->right_attach = -1;
1328 ai->top_attach = -1;
1329 ai->bottom_attach = -1;
1331 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1332 gtk_widget_set_parent_window (child, priv->bin_window);
1334 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1336 menu_queue_resize (menu);
1340 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1342 GtkMenuPrivate *priv = menu->priv;
1348 cairo_surface_t *surface;
1349 cairo_pattern_t *pattern;
1352 priv->tearoff_active = FALSE;
1353 priv->saved_scroll_offset = priv->scroll_offset;
1355 window = gtk_widget_get_window (priv->tearoff_window);
1356 width = gdk_window_get_width (window);
1357 height = gdk_window_get_height (window);
1359 surface = gdk_window_create_similar_surface (window,
1360 CAIRO_CONTENT_COLOR,
1364 cr = cairo_create (surface);
1365 gdk_cairo_set_source_window (cr, window, 0, 0);
1369 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1371 pattern = cairo_pattern_create_for_surface (surface);
1372 gdk_window_set_background_pattern (window, pattern);
1374 cairo_pattern_destroy (pattern);
1375 cairo_surface_destroy (surface);
1380 popup_grab_on_window (GdkWindow *window,
1381 GdkDevice *keyboard,
1383 guint32 activate_time)
1386 gdk_device_grab (keyboard, window,
1387 GDK_OWNERSHIP_WINDOW, TRUE,
1388 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1389 NULL, activate_time) != GDK_GRAB_SUCCESS)
1393 gdk_device_grab (pointer, window,
1394 GDK_OWNERSHIP_WINDOW, TRUE,
1395 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1396 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1397 GDK_POINTER_MOTION_MASK,
1398 NULL, activate_time) != GDK_GRAB_SUCCESS)
1401 gdk_device_ungrab (keyboard, activate_time);
1410 * gtk_menu_popup_for_device:
1412 * @device: (allow-none): a #GdkDevice
1413 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1414 * menu item, or %NULL
1415 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1416 * the popup, or %NULL
1417 * @func: (allow-none): a user supplied function used to position the menu,
1419 * @data: (allow-none): user supplied data to be passed to @func
1420 * @destroy: (allow-none): destroy notify for @data
1421 * @button: the mouse button which was pressed to initiate the event
1422 * @activate_time: the time at which the activation event occurred
1424 * Displays a menu and makes it available for selection.
1426 * Applications can use this function to display context-sensitive menus,
1427 * and will typically supply %NULL for the @parent_menu_shell,
1428 * @parent_menu_item, @func, @data and @destroy parameters. The default
1429 * menu positioning function will position the menu at the current position
1430 * of @device (or its corresponding pointer).
1432 * The @button parameter should be the mouse button pressed to initiate
1433 * the menu popup. If the menu popup was initiated by something other than
1434 * a mouse button press, such as a mouse button release or a keypress,
1435 * @button should be 0.
1437 * The @activate_time parameter is used to conflict-resolve initiation of
1438 * concurrent requests for mouse/keyboard grab requests. To function
1439 * properly, this needs to be the time stamp of the user event (such as
1440 * a mouse click or key press) that caused the initiation of the popup.
1441 * Only if no such event is available, gtk_get_current_event_time() can
1445 * Rename to: gtk_menu_popup
1448 gtk_menu_popup_for_device (GtkMenu *menu,
1450 GtkWidget *parent_menu_shell,
1451 GtkWidget *parent_menu_item,
1452 GtkMenuPositionFunc func,
1454 GDestroyNotify destroy,
1456 guint32 activate_time)
1458 GtkMenuPrivate *priv = menu->priv;
1460 GtkWidget *xgrab_shell;
1462 GdkEvent *current_event;
1463 GtkMenuShell *menu_shell;
1464 gboolean grab_keyboard;
1465 GtkWidget *parent_toplevel;
1466 GdkDevice *keyboard, *pointer;
1468 g_return_if_fail (GTK_IS_MENU (menu));
1469 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1472 device = gtk_get_current_event_device ();
1476 GdkDisplay *display;
1477 GdkDeviceManager *device_manager;
1480 display = gtk_widget_get_display (GTK_WIDGET (menu));
1481 device_manager = gdk_display_get_device_manager (display);
1482 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1484 device = devices->data;
1486 g_list_free (devices);
1489 widget = GTK_WIDGET (menu);
1490 menu_shell = GTK_MENU_SHELL (menu);
1492 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1495 pointer = gdk_device_get_associated_device (device);
1500 keyboard = gdk_device_get_associated_device (device);
1503 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1505 priv->seen_item_enter = FALSE;
1507 /* Find the last viewable ancestor, and make an X grab on it
1509 parent = GTK_WIDGET (menu);
1513 gboolean viewable = TRUE;
1514 GtkWidget *tmp = parent;
1518 if (!gtk_widget_get_mapped (tmp))
1523 tmp = gtk_widget_get_parent (tmp);
1527 xgrab_shell = parent;
1529 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1532 /* We want to receive events generated when we map the menu;
1533 * unfortunately, since there is probably already an implicit
1534 * grab in place from the button that the user used to pop up
1535 * the menu, we won't receive then -- in particular, the EnterNotify
1536 * when the menu pops up under the pointer.
1538 * If we are grabbing on a parent menu shell, no problem; just grab
1539 * on that menu shell first before popping up the window with
1540 * owner_events = TRUE.
1542 * When grabbing on the menu itself, things get more convoluted --
1543 * we do an explicit grab on a specially created window with
1544 * owner_events = TRUE, which we override further down with a
1545 * grab on the menu. (We can't grab on the menu until it is mapped;
1546 * we probably could just leave the grab on the other window,
1547 * with a little reorganization of the code in gtkmenu*).
1549 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1550 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1555 if (xgrab_shell && xgrab_shell != widget)
1557 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1559 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1560 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1565 GdkWindow *transfer_window;
1567 xgrab_shell = widget;
1568 transfer_window = menu_grab_transfer_window_get (menu);
1569 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1571 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1572 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1576 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1578 /* We failed to make our pointer/keyboard grab.
1579 * Rather than leaving the user with a stuck up window,
1580 * we just abort here. Presumably the user will try again.
1582 menu_shell->priv->parent_menu_shell = NULL;
1583 menu_grab_transfer_window_destroy (menu);
1587 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1588 menu_shell->priv->active = TRUE;
1589 menu_shell->priv->button = button;
1591 /* If we are popping up the menu from something other than, a button
1592 * press then, as a heuristic, we ignore enter events for the menu
1593 * until we get a MOTION_NOTIFY.
1596 current_event = gtk_get_current_event ();
1599 if ((current_event->type != GDK_BUTTON_PRESS) &&
1600 (current_event->type != GDK_ENTER_NOTIFY))
1601 menu_shell->priv->ignore_enter = TRUE;
1603 gdk_event_free (current_event);
1606 menu_shell->priv->ignore_enter = TRUE;
1610 gtk_menu_tearoff_bg_copy (menu);
1612 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1615 parent_toplevel = NULL;
1616 if (parent_menu_shell)
1617 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1618 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1620 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1622 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1625 /* Set transient for to get the right window group and parent */
1626 if (GTK_IS_WINDOW (parent_toplevel))
1627 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1628 GTK_WINDOW (parent_toplevel));
1630 priv->parent_menu_item = parent_menu_item;
1631 priv->position_func = func;
1632 priv->position_func_data = data;
1633 priv->position_func_data_destroy = destroy;
1634 menu_shell->priv->activate_time = activate_time;
1636 /* We need to show the menu here rather in the init function
1637 * because code expects to be able to tell if the menu is onscreen
1638 * by looking at gtk_widget_get_visible (menu)
1640 gtk_widget_show (GTK_WIDGET (menu));
1642 /* Position the menu, possibly changing the size request
1644 gtk_menu_position (menu, TRUE);
1646 /* Compute the size of the toplevel and realize it so we
1647 * can scroll correctly.
1649 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1651 GtkRequisition tmp_request;
1652 GtkAllocation tmp_allocation = { 0, };
1654 /* Instead of trusting the menu position function to queue a
1655 * resize when the menu goes out of bounds, invalidate the cached
1658 gtk_widget_queue_resize (GTK_WIDGET (menu));
1659 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1661 tmp_allocation.width = tmp_request.width;
1662 tmp_allocation.height = tmp_request.height;
1664 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1666 gtk_widget_realize (priv->toplevel);
1669 gtk_menu_scroll_to (menu, priv->scroll_offset);
1671 /* if no item is selected, select the first one */
1672 if (!menu_shell->priv->active_menu_item)
1674 gboolean touchscreen_mode;
1676 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1677 "gtk-touchscreen-mode", &touchscreen_mode,
1680 if (touchscreen_mode)
1681 gtk_menu_shell_select_first (menu_shell, TRUE);
1684 /* Once everything is set up correctly, map the toplevel */
1685 gtk_widget_show (priv->toplevel);
1687 if (xgrab_shell == widget)
1688 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1690 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1692 if (parent_menu_shell)
1694 gboolean keyboard_mode;
1696 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1697 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1699 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1700 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1702 _gtk_menu_shell_update_mnemonics (menu_shell);
1706 * gtk_menu_popup: (skip)
1708 * @parent_menu_shell: (allow-none): the menu shell containing the
1709 * triggering menu item, or %NULL
1710 * @parent_menu_item: (allow-none): the menu item whose activation
1711 * triggered the popup, or %NULL
1712 * @func: (allow-none): a user supplied function used to position
1713 * the menu, or %NULL
1714 * @data: user supplied data to be passed to @func.
1715 * @button: the mouse button which was pressed to initiate the event.
1716 * @activate_time: the time at which the activation event occurred.
1718 * Displays a menu and makes it available for selection.
1720 * Applications can use this function to display context-sensitive
1721 * menus, and will typically supply %NULL for the @parent_menu_shell,
1722 * @parent_menu_item, @func and @data parameters. The default menu
1723 * positioning function will position the menu at the current mouse
1726 * The @button parameter should be the mouse button pressed to initiate
1727 * the menu popup. If the menu popup was initiated by something other
1728 * than a mouse button press, such as a mouse button release or a keypress,
1729 * @button should be 0.
1731 * The @activate_time parameter is used to conflict-resolve initiation
1732 * of concurrent requests for mouse/keyboard grab requests. To function
1733 * properly, this needs to be the timestamp of the user event (such as
1734 * a mouse click or key press) that caused the initiation of the popup.
1735 * Only if no such event is available, gtk_get_current_event_time() can
1739 gtk_menu_popup (GtkMenu *menu,
1740 GtkWidget *parent_menu_shell,
1741 GtkWidget *parent_menu_item,
1742 GtkMenuPositionFunc func,
1745 guint32 activate_time)
1747 g_return_if_fail (GTK_IS_MENU (menu));
1749 gtk_menu_popup_for_device (menu,
1754 button, activate_time);
1761 * Removes the menu from the screen.
1764 gtk_menu_popdown (GtkMenu *menu)
1766 GtkMenuPrivate *priv;
1767 GtkMenuShell *menu_shell;
1770 g_return_if_fail (GTK_IS_MENU (menu));
1772 menu_shell = GTK_MENU_SHELL (menu);
1775 menu_shell->priv->parent_menu_shell = NULL;
1776 menu_shell->priv->active = FALSE;
1777 menu_shell->priv->ignore_enter = FALSE;
1779 priv->have_position = FALSE;
1781 gtk_menu_stop_scrolling (menu);
1782 gtk_menu_stop_navigating_submenu (menu);
1784 if (menu_shell->priv->active_menu_item)
1786 if (priv->old_active_menu_item)
1787 g_object_unref (priv->old_active_menu_item);
1788 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1789 g_object_ref (priv->old_active_menu_item);
1792 gtk_menu_shell_deselect (menu_shell);
1794 /* The X Grab, if present, will automatically be removed
1795 * when we hide the window
1799 gtk_widget_hide (priv->toplevel);
1800 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1803 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1807 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1809 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1811 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1815 /* We popped up the menu from the tearoff, so we need to
1816 * release the grab - we aren't actually hiding the menu.
1818 if (menu_shell->priv->have_xgrab && pointer)
1820 GdkDevice *keyboard;
1822 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1823 keyboard = gdk_device_get_associated_device (pointer);
1826 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1830 /* gtk_menu_popdown is called each time a menu item is selected from
1831 * a torn off menu. Only scroll back to the saved position if the
1832 * non-tearoff menu was popped down.
1834 if (!priv->tearoff_active)
1835 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1836 priv->tearoff_active = TRUE;
1839 gtk_widget_hide (GTK_WIDGET (menu));
1841 menu_shell->priv->have_xgrab = FALSE;
1844 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1846 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1848 menu_grab_transfer_window_destroy (menu);
1852 * gtk_menu_get_active:
1855 * Returns the selected menu item from the menu. This is used by the
1858 * Returns: (transfer none): the #GtkMenuItem that was last selected
1859 * in the menu. If a selection has not yet been made, the
1860 * first menu item is selected.
1863 gtk_menu_get_active (GtkMenu *menu)
1865 GtkMenuPrivate *priv = menu->priv;
1869 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1871 if (!priv->old_active_menu_item)
1874 children = GTK_MENU_SHELL (menu)->priv->children;
1878 child = children->data;
1879 children = children->next;
1881 if (gtk_bin_get_child (GTK_BIN (child)))
1886 priv->old_active_menu_item = child;
1887 if (priv->old_active_menu_item)
1888 g_object_ref (priv->old_active_menu_item);
1891 return priv->old_active_menu_item;
1895 * gtk_menu_set_active:
1897 * @index: the index of the menu item to select. Iindex values are
1900 * Selects the specified menu item within the menu. This is used by
1901 * the #GtkOptionMenu and should not be used by anyone else.
1904 gtk_menu_set_active (GtkMenu *menu,
1907 GtkMenuPrivate *priv = menu->priv;
1911 g_return_if_fail (GTK_IS_MENU (menu));
1913 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1916 child = tmp_list->data;
1917 if (gtk_bin_get_child (GTK_BIN (child)))
1919 if (priv->old_active_menu_item)
1920 g_object_unref (priv->old_active_menu_item);
1921 priv->old_active_menu_item = child;
1922 g_object_ref (priv->old_active_menu_item);
1928 * gtk_menu_set_accel_group:
1930 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1933 * Set the #GtkAccelGroup which holds global accelerators for the
1934 * menu. This accelerator group needs to also be added to all windows
1935 * that this menu is being used in with gtk_window_add_accel_group(),
1936 * in order for those windows to support all the accelerators
1937 * contained in this group.
1940 gtk_menu_set_accel_group (GtkMenu *menu,
1941 GtkAccelGroup *accel_group)
1943 GtkMenuPrivate *priv = menu->priv;
1944 g_return_if_fail (GTK_IS_MENU (menu));
1946 if (priv->accel_group != accel_group)
1948 if (priv->accel_group)
1949 g_object_unref (priv->accel_group);
1950 priv->accel_group = accel_group;
1951 if (priv->accel_group)
1952 g_object_ref (priv->accel_group);
1953 _gtk_menu_refresh_accel_paths (menu, TRUE);
1958 * gtk_menu_get_accel_group:
1961 * Gets the #GtkAccelGroup which holds global accelerators for the
1962 * menu. See gtk_menu_set_accel_group().
1964 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
1967 gtk_menu_get_accel_group (GtkMenu *menu)
1969 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1971 return menu->priv->accel_group;
1975 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1978 /* Menu items chain here to figure whether they can activate their
1979 * accelerators. Unlike ordinary widgets, menus allow accel
1980 * activation even if invisible since that's the usual case for
1981 * submenus/popup-menus. however, the state of the attach widget
1982 * affects the "activeness" of the menu.
1984 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1987 return gtk_widget_can_activate_accel (awidget, signal_id);
1989 return gtk_widget_is_sensitive (widget);
1993 * gtk_menu_set_accel_path
1994 * @menu: a valid #GtkMenu
1995 * @accel_path: (allow-none): a valid accelerator path
1997 * Sets an accelerator path for this menu from which accelerator paths
1998 * for its immediate children, its menu items, can be constructed.
1999 * The main purpose of this function is to spare the programmer the
2000 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2001 * each menu item that should support runtime user changable accelerators.
2002 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2003 * each menu item of this menu, that contains a label describing its
2004 * purpose, automatically gets an accel path assigned.
2006 * For example, a menu containing menu items "New" and "Exit", will, after
2007 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2008 * has been called, assign its items the accel paths:
2009 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2011 * Assigning accel paths to menu items then enables the user to change
2012 * their accelerators at runtime. More details about accelerator paths
2013 * and their default setups can be found at gtk_accel_map_add_entry().
2015 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2016 * if you pass a static string, you can save some memory by interning
2017 * it first with g_intern_static_string().
2020 gtk_menu_set_accel_path (GtkMenu *menu,
2021 const gchar *accel_path)
2023 GtkMenuPrivate *priv = menu->priv;
2024 g_return_if_fail (GTK_IS_MENU (menu));
2027 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2029 /* FIXME: accel_path should be defined as const gchar* */
2030 priv->accel_path = (gchar*)g_intern_string (accel_path);
2031 if (priv->accel_path)
2032 _gtk_menu_refresh_accel_paths (menu, FALSE);
2036 * gtk_menu_get_accel_path
2037 * @menu: a valid #GtkMenu
2039 * Retrieves the accelerator path set on the menu.
2041 * Returns: the accelerator path set on the menu.
2046 gtk_menu_get_accel_path (GtkMenu *menu)
2048 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2050 return menu->priv->accel_path;
2055 gboolean group_changed;
2059 refresh_accel_paths_foreach (GtkWidget *widget,
2062 GtkMenuPrivate *priv;
2063 AccelPropagation *prop = data;
2065 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2067 priv = prop->menu->priv;
2068 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2071 prop->group_changed);
2076 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2077 gboolean group_changed)
2079 GtkMenuPrivate *priv = menu->priv;
2080 g_return_if_fail (GTK_IS_MENU (menu));
2082 if (priv->accel_path && priv->accel_group)
2084 AccelPropagation prop;
2087 prop.group_changed = group_changed;
2088 gtk_container_foreach (GTK_CONTAINER (menu),
2089 refresh_accel_paths_foreach,
2095 * gtk_menu_reposition:
2098 * Repositions the menu according to its position function.
2101 gtk_menu_reposition (GtkMenu *menu)
2103 g_return_if_fail (GTK_IS_MENU (menu));
2105 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2106 gtk_menu_position (menu, FALSE);
2110 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2115 value = gtk_adjustment_get_value (adjustment);
2116 if (menu->priv->scroll_offset != value)
2117 gtk_menu_scroll_to (menu, value);
2121 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2124 GtkMenuPrivate *priv = menu->priv;
2125 GdkGeometry geometry_hints;
2127 if (!priv->tearoff_window)
2130 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2132 GtkRequisition requisition;
2134 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2135 &requisition, NULL);
2136 width += requisition.width;
2139 geometry_hints.min_width = width;
2140 geometry_hints.max_width = width;
2142 geometry_hints.min_height = 0;
2143 geometry_hints.max_height = priv->requested_height;
2145 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2148 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2152 gtk_menu_update_title (GtkMenu *menu)
2154 GtkMenuPrivate *priv = menu->priv;
2156 if (priv->tearoff_window)
2159 GtkWidget *attach_widget;
2161 title = gtk_menu_get_title (menu);
2164 attach_widget = gtk_menu_get_attach_widget (menu);
2165 if (GTK_IS_MENU_ITEM (attach_widget))
2167 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2168 if (GTK_IS_LABEL (child))
2169 title = gtk_label_get_text (GTK_LABEL (child));
2174 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2179 gtk_menu_get_toplevel (GtkWidget *menu)
2181 GtkWidget *attach, *toplevel;
2183 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2185 if (GTK_IS_MENU_ITEM (attach))
2186 attach = gtk_widget_get_parent (attach);
2188 if (GTK_IS_MENU (attach))
2189 return gtk_menu_get_toplevel (attach);
2190 else if (GTK_IS_WIDGET (attach))
2192 toplevel = gtk_widget_get_toplevel (attach);
2193 if (gtk_widget_is_toplevel (toplevel))
2201 tearoff_window_destroyed (GtkWidget *widget,
2204 gtk_menu_set_tearoff_state (menu, FALSE);
2208 * gtk_menu_set_tearoff_state:
2210 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2212 * Changes the tearoff state of the menu. A menu is normally
2213 * displayed as drop down menu which persists as long as the menu is
2214 * active. It can also be displayed as a tearoff menu which persists
2215 * until it is closed or reattached.
2218 gtk_menu_set_tearoff_state (GtkMenu *menu,
2221 GtkMenuPrivate *priv = menu->priv;
2224 g_return_if_fail (GTK_IS_MENU (menu));
2226 if (priv->torn_off != torn_off)
2228 priv->torn_off = torn_off;
2229 priv->tearoff_active = torn_off;
2233 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2234 gtk_menu_popdown (menu);
2236 if (!priv->tearoff_window)
2238 GtkWidget *toplevel;
2240 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2241 "type", GTK_WINDOW_TOPLEVEL,
2242 "screen", gtk_widget_get_screen (priv->toplevel),
2243 "app-paintable", TRUE,
2246 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2247 GDK_WINDOW_TYPE_HINT_MENU);
2248 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2249 g_signal_connect (priv->tearoff_window, "destroy",
2250 G_CALLBACK (tearoff_window_destroyed), menu);
2251 g_signal_connect (priv->tearoff_window, "event",
2252 G_CALLBACK (gtk_menu_window_event), menu);
2254 gtk_menu_update_title (menu);
2256 gtk_widget_realize (priv->tearoff_window);
2258 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2259 if (toplevel != NULL)
2260 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2261 GTK_WINDOW (toplevel));
2263 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2264 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2265 priv->tearoff_hbox);
2267 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2268 priv->tearoff_adjustment = gtk_adjustment_new (0,
2269 0, priv->requested_height,
2273 g_object_connect (priv->tearoff_adjustment,
2274 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2276 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2278 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2279 priv->tearoff_scrollbar,
2282 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2283 gtk_widget_show (priv->tearoff_scrollbar);
2285 gtk_widget_show (priv->tearoff_hbox);
2288 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2290 /* Update menu->requisition */
2291 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2293 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2295 gtk_widget_realize (priv->tearoff_window);
2296 gtk_menu_position (menu, TRUE);
2298 gtk_widget_show (GTK_WIDGET (menu));
2299 gtk_widget_show (priv->tearoff_window);
2301 gtk_menu_scroll_to (menu, 0);
2306 gtk_widget_hide (GTK_WIDGET (menu));
2307 gtk_widget_hide (priv->tearoff_window);
2308 if (GTK_IS_CONTAINER (priv->toplevel))
2309 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2310 gtk_widget_destroy (priv->tearoff_window);
2312 priv->tearoff_window = NULL;
2313 priv->tearoff_hbox = NULL;
2314 priv->tearoff_scrollbar = NULL;
2315 priv->tearoff_adjustment = NULL;
2318 g_object_notify (G_OBJECT (menu), "tearoff-state");
2323 * gtk_menu_get_tearoff_state:
2326 * Returns whether the menu is torn off.
2327 * See gtk_menu_set_tearoff_state().
2329 * Return value: %TRUE if the menu is currently torn off.
2332 gtk_menu_get_tearoff_state (GtkMenu *menu)
2334 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2336 return menu->priv->torn_off;
2340 * gtk_menu_set_title:
2342 * @title: a string containing the title for the menu
2344 * Sets the title string for the menu.
2346 * The title is displayed when the menu is shown as a tearoff
2347 * menu. If @title is %NULL, the menu will see if it is attached
2348 * to a parent menu item, and if so it will try to use the same
2349 * text as that menu item's label.
2352 gtk_menu_set_title (GtkMenu *menu,
2355 GtkMenuPrivate *priv = menu->priv;
2358 g_return_if_fail (GTK_IS_MENU (menu));
2360 old_title = priv->title;
2361 priv->title = g_strdup (title);
2364 gtk_menu_update_title (menu);
2365 g_object_notify (G_OBJECT (menu), "tearoff-title");
2369 * gtk_menu_get_title:
2372 * Returns the title of the menu. See gtk_menu_set_title().
2374 * Return value: the title of the menu, or %NULL if the menu
2375 * has no title set on it. This string is owned by GTK+
2376 * and should not be modified or freed.
2379 gtk_menu_get_title (GtkMenu *menu)
2381 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2383 return menu->priv->title;
2387 * gtk_menu_reorder_child:
2389 * @child: the #GtkMenuItem to move
2390 * @position: the new position to place @child.
2391 * Positions are numbered from 0 to n - 1
2393 * Moves @child to a new @position in the list of @menu
2397 gtk_menu_reorder_child (GtkMenu *menu,
2401 GtkMenuShell *menu_shell;
2403 g_return_if_fail (GTK_IS_MENU (menu));
2404 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2406 menu_shell = GTK_MENU_SHELL (menu);
2408 if (g_list_find (menu_shell->priv->children, child))
2410 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2411 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2413 menu_queue_resize (menu);
2418 gtk_menu_style_updated (GtkWidget *widget)
2420 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2422 if (gtk_widget_get_realized (widget))
2424 GtkMenu *menu = GTK_MENU (widget);
2425 GtkMenuPrivate *priv = menu->priv;
2426 GtkStyleContext *context;
2428 context = gtk_widget_get_style_context (widget);
2430 gtk_style_context_set_background (context, priv->bin_window);
2431 gtk_style_context_set_background (context, priv->view_window);
2432 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2437 get_arrows_border (GtkMenu *menu,
2440 GtkMenuPrivate *priv = menu->priv;
2441 guint scroll_arrow_height;
2442 GtkArrowPlacement arrow_placement;
2444 gtk_widget_style_get (GTK_WIDGET (menu),
2445 "scroll-arrow-vlength", &scroll_arrow_height,
2446 "arrow_placement", &arrow_placement,
2449 switch (arrow_placement)
2451 case GTK_ARROWS_BOTH:
2452 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2453 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2456 case GTK_ARROWS_START:
2457 border->top = (priv->upper_arrow_visible ||
2458 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2462 case GTK_ARROWS_END:
2464 border->bottom = (priv->upper_arrow_visible ||
2465 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2469 border->left = border->right = 0;
2473 get_menu_padding (GtkWidget *widget,
2476 GtkStyleContext *context;
2477 GtkStateFlags state;
2479 context = gtk_widget_get_style_context (widget);
2480 state = gtk_widget_get_state_flags (widget);
2482 gtk_style_context_get_padding (context, state, padding);
2486 gtk_menu_realize (GtkWidget *widget)
2488 GtkMenu *menu = GTK_MENU (widget);
2489 GtkMenuPrivate *priv = menu->priv;
2490 GtkAllocation allocation;
2491 GtkStyleContext *context;
2493 GdkWindowAttr attributes;
2494 gint attributes_mask;
2498 guint vertical_padding;
2499 guint horizontal_padding;
2500 GtkBorder arrow_border, padding;
2502 g_return_if_fail (GTK_IS_MENU (widget));
2504 gtk_widget_set_realized (widget, TRUE);
2506 gtk_widget_get_allocation (widget, &allocation);
2508 attributes.window_type = GDK_WINDOW_CHILD;
2509 attributes.x = allocation.x;
2510 attributes.y = allocation.y;
2511 attributes.width = allocation.width;
2512 attributes.height = allocation.height;
2513 attributes.wclass = GDK_INPUT_OUTPUT;
2514 attributes.visual = gtk_widget_get_visual (widget);
2515 attributes.event_mask = gtk_widget_get_events (widget);
2516 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2517 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2519 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2521 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2522 &attributes, attributes_mask);
2523 gtk_widget_set_window (widget, window);
2524 gdk_window_set_user_data (window, widget);
2526 get_menu_padding (widget, &padding);
2527 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2528 context = gtk_widget_get_style_context (widget);
2530 gtk_widget_style_get (GTK_WIDGET (menu),
2531 "vertical-padding", &vertical_padding,
2532 "horizontal-padding", &horizontal_padding,
2535 gtk_widget_get_allocation (widget, &allocation);
2537 attributes.x = border_width + padding.left + horizontal_padding;
2538 attributes.y = border_width + padding.top + vertical_padding;
2539 attributes.width = allocation.width -
2540 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2541 attributes.height = allocation.height -
2542 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2544 get_arrows_border (menu, &arrow_border);
2545 attributes.y += arrow_border.top;
2546 attributes.height -= arrow_border.top;
2547 attributes.height -= arrow_border.bottom;
2549 attributes.width = MAX (1, attributes.width);
2550 attributes.height = MAX (1, attributes.height);
2552 priv->view_window = gdk_window_new (window,
2553 &attributes, attributes_mask);
2554 gdk_window_set_user_data (priv->view_window, menu);
2556 gtk_widget_get_allocation (widget, &allocation);
2560 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2561 padding.left + padding.right;
2562 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2563 padding.top + padding.bottom;
2565 attributes.width = MAX (1, attributes.width);
2566 attributes.height = MAX (1, attributes.height);
2568 priv->bin_window = gdk_window_new (priv->view_window,
2569 &attributes, attributes_mask);
2570 gdk_window_set_user_data (priv->bin_window, menu);
2572 children = GTK_MENU_SHELL (menu)->priv->children;
2575 child = children->data;
2576 children = children->next;
2578 gtk_widget_set_parent_window (child, priv->bin_window);
2581 gtk_style_context_set_background (context, priv->bin_window);
2582 gtk_style_context_set_background (context, priv->view_window);
2583 gtk_style_context_set_background (context, window);
2585 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2586 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2587 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2589 gdk_window_show (priv->bin_window);
2590 gdk_window_show (priv->view_window);
2594 gtk_menu_focus (GtkWidget *widget,
2595 GtkDirectionType direction)
2597 /* A menu or its menu items cannot have focus */
2601 /* See notes in gtk_menu_popup() for information
2602 * about the "grab transfer window"
2605 menu_grab_transfer_window_get (GtkMenu *menu)
2607 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2610 GdkWindowAttr attributes;
2611 gint attributes_mask;
2613 attributes.x = -100;
2614 attributes.y = -100;
2615 attributes.width = 10;
2616 attributes.height = 10;
2617 attributes.window_type = GDK_WINDOW_TEMP;
2618 attributes.wclass = GDK_INPUT_ONLY;
2619 attributes.override_redirect = TRUE;
2620 attributes.event_mask = 0;
2622 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2624 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2625 &attributes, attributes_mask);
2626 gdk_window_set_user_data (window, menu);
2628 gdk_window_show (window);
2630 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2637 menu_grab_transfer_window_destroy (GtkMenu *menu)
2639 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2642 gdk_window_set_user_data (window, NULL);
2643 gdk_window_destroy (window);
2644 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2649 gtk_menu_unrealize (GtkWidget *widget)
2651 GtkMenu *menu = GTK_MENU (widget);
2652 GtkMenuPrivate *priv = menu->priv;
2654 menu_grab_transfer_window_destroy (menu);
2656 gdk_window_set_user_data (priv->view_window, NULL);
2657 gdk_window_destroy (priv->view_window);
2658 priv->view_window = NULL;
2660 gdk_window_set_user_data (priv->bin_window, NULL);
2661 gdk_window_destroy (priv->bin_window);
2662 priv->bin_window = NULL;
2664 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2668 calculate_line_heights (GtkMenu *menu,
2670 guint **ret_min_heights,
2671 guint **ret_nat_heights)
2674 GtkMenuPrivate *priv;
2675 GtkMenuShell *menu_shell;
2676 GtkWidget *child, *widget;
2678 guint horizontal_padding;
2687 widget = GTK_WIDGET (menu);
2688 menu_shell = GTK_MENU_SHELL (widget);
2690 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2691 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2692 n_heights = gtk_menu_get_n_rows (menu);
2693 n_columns = gtk_menu_get_n_columns (menu);
2694 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2696 gtk_widget_style_get (GTK_WIDGET (menu),
2697 "horizontal-padding", &horizontal_padding,
2699 get_menu_padding (widget, &padding);
2701 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2702 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2704 for (children = menu_shell->priv->children; children; children = children->next)
2709 gint child_min, child_nat;
2711 child = children->data;
2713 if (!gtk_widget_get_visible (child))
2716 get_effective_child_attach (child, &l, &r, &t, &b);
2718 part = avail_width / (r - l);
2720 gtk_widget_get_preferred_height_for_width (child, part,
2721 &child_min, &child_nat);
2723 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2725 part = MAX (child_min, toggle_size) / (b - t);
2726 min_heights[t] = MAX (min_heights[t], part);
2728 part = MAX (child_nat, toggle_size) / (b - t);
2729 nat_heights[t] = MAX (nat_heights[t], part);
2732 if (ret_min_heights)
2733 *ret_min_heights = min_heights;
2735 g_free (min_heights);
2737 if (ret_nat_heights)
2738 *ret_nat_heights = nat_heights;
2740 g_free (nat_heights);
2746 gtk_menu_size_allocate (GtkWidget *widget,
2747 GtkAllocation *allocation)
2750 GtkMenuPrivate *priv;
2751 GtkMenuShell *menu_shell;
2753 GtkAllocation child_allocation;
2758 guint vertical_padding;
2759 guint horizontal_padding;
2762 g_return_if_fail (GTK_IS_MENU (widget));
2763 g_return_if_fail (allocation != NULL);
2765 menu = GTK_MENU (widget);
2766 menu_shell = GTK_MENU_SHELL (widget);
2769 gtk_widget_set_allocation (widget, allocation);
2771 gtk_widget_style_get (GTK_WIDGET (menu),
2772 "vertical-padding", &vertical_padding,
2773 "horizontal-padding", &horizontal_padding,
2776 get_menu_padding (widget, &padding);
2777 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2779 g_free (priv->heights);
2780 priv->heights_length = calculate_line_heights (menu,
2785 /* refresh our cached height request */
2786 priv->requested_height = (2 * (border_width + vertical_padding)) +
2787 padding.top + padding.bottom;
2788 for (i = 0; i < priv->heights_length; i++)
2789 priv->requested_height += priv->heights[i];
2791 x = border_width + padding.left + horizontal_padding;
2792 y = border_width + padding.top + vertical_padding;
2793 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2794 padding.left - padding.right;
2795 height = allocation->height - (2 * (border_width + vertical_padding)) -
2796 padding.top - padding.bottom;
2798 if (menu_shell->priv->active)
2799 gtk_menu_scroll_to (menu, priv->scroll_offset);
2801 if (!priv->tearoff_active)
2803 GtkBorder arrow_border;
2805 get_arrows_border (menu, &arrow_border);
2806 y += arrow_border.top;
2807 height -= arrow_border.top;
2808 height -= arrow_border.bottom;
2811 width = MAX (1, width);
2812 height = MAX (1, height);
2814 if (gtk_widget_get_realized (widget))
2816 gdk_window_move_resize (gtk_widget_get_window (widget),
2817 allocation->x, allocation->y,
2818 allocation->width, allocation->height);
2820 gdk_window_move_resize (priv->view_window, x, y, width, height);
2823 if (menu_shell->priv->children)
2825 gint base_width = width / gtk_menu_get_n_columns (menu);
2827 children = menu_shell->priv->children;
2830 child = children->data;
2831 children = children->next;
2833 if (gtk_widget_get_visible (child))
2838 get_effective_child_attach (child, &l, &r, &t, &b);
2840 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2843 tmp = gtk_menu_get_n_columns (menu) - l;
2844 l = gtk_menu_get_n_columns (menu) - r;
2848 child_allocation.width = (r - l) * base_width;
2849 child_allocation.height = 0;
2850 child_allocation.x = l * base_width;
2851 child_allocation.y = 0;
2853 for (i = 0; i < b; i++)
2856 child_allocation.y += priv->heights[i];
2858 child_allocation.height += priv->heights[i];
2861 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2864 gtk_widget_size_allocate (child, &child_allocation);
2865 gtk_widget_queue_draw (child);
2869 /* Resize the item window */
2870 if (gtk_widget_get_realized (widget))
2876 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2877 height += priv->heights[i];
2879 width = gtk_menu_get_n_columns (menu) * base_width;
2880 gdk_window_resize (priv->bin_window, width, height);
2883 if (priv->tearoff_active)
2885 if (height >= priv->requested_height)
2887 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2889 gtk_widget_hide (priv->tearoff_scrollbar);
2890 gtk_menu_set_tearoff_hints (menu, allocation->width);
2892 gtk_menu_scroll_to (menu, 0);
2897 gtk_adjustment_configure (priv->tearoff_adjustment,
2898 gtk_adjustment_get_value (priv->tearoff_adjustment),
2900 priv->requested_height,
2901 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2902 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2903 allocation->height);
2905 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2907 gtk_widget_show (priv->tearoff_scrollbar);
2908 gtk_menu_set_tearoff_hints (menu, allocation->width);
2916 get_arrows_visible_area (GtkMenu *menu,
2917 GdkRectangle *border,
2918 GdkRectangle *upper,
2919 GdkRectangle *lower,
2922 GtkArrowPlacement arrow_placement;
2923 GtkWidget *widget = GTK_WIDGET (menu);
2925 guint vertical_padding;
2926 guint horizontal_padding;
2927 gint scroll_arrow_height;
2928 GtkBorder menu_padding;
2930 gtk_widget_style_get (widget,
2931 "vertical-padding", &vertical_padding,
2932 "horizontal-padding", &horizontal_padding,
2933 "scroll-arrow-vlength", &scroll_arrow_height,
2934 "arrow-placement", &arrow_placement,
2937 get_menu_padding (widget, &menu_padding);
2938 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2939 border->x = border_width + menu_padding.left + horizontal_padding;
2940 border->y = border_width + menu_padding.top + vertical_padding;
2941 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2942 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2944 switch (arrow_placement)
2946 case GTK_ARROWS_BOTH:
2947 upper->x = border->x;
2948 upper->y = border->y;
2949 upper->width = border->width - 2 * border->x;
2950 upper->height = scroll_arrow_height;
2952 lower->x = border->x;
2953 lower->y = border->height - border->y - scroll_arrow_height;
2954 lower->width = border->width - 2 * border->x;
2955 lower->height = scroll_arrow_height;
2958 case GTK_ARROWS_START:
2959 upper->x = border->x;
2960 upper->y = border->y;
2961 upper->width = (border->width - 2 * border->x) / 2;
2962 upper->height = scroll_arrow_height;
2964 lower->x = border->x + upper->width;
2965 lower->y = border->y;
2966 lower->width = (border->width - 2 * border->x) / 2;
2967 lower->height = scroll_arrow_height;
2970 case GTK_ARROWS_END:
2971 upper->x = border->x;
2972 upper->y = border->height - border->y - scroll_arrow_height;
2973 upper->width = (border->width - 2 * border->x) / 2;
2974 upper->height = scroll_arrow_height;
2976 lower->x = border->x + upper->width;
2977 lower->y = border->height - border->y - scroll_arrow_height;
2978 lower->width = (border->width - 2 * border->x) / 2;
2979 lower->height = scroll_arrow_height;
2983 g_assert_not_reached();
2984 upper->x = upper->y = upper->width = upper->height = 0;
2985 lower->x = lower->y = lower->width = lower->height = 0;
2988 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
2992 gtk_menu_draw (GtkWidget *widget,
2996 GtkMenuPrivate *priv;
2997 GtkStyleContext *context;
2998 GdkRectangle border;
3002 GtkBorder menu_padding;
3004 menu = GTK_MENU (widget);
3006 context = gtk_widget_get_style_context (widget);
3008 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3009 get_menu_padding (widget, &menu_padding);
3011 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3013 gfloat arrow_scaling;
3016 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3017 arrow_size = arrow_scaling * arrow_space;
3019 gtk_render_background (context, cr, 0, 0,
3020 gtk_widget_get_allocated_width (widget),
3021 gtk_widget_get_allocated_height (widget));
3022 gtk_render_frame (context, cr, 0, 0,
3023 gtk_widget_get_allocated_width (widget),
3024 gtk_widget_get_allocated_height (widget));
3026 gtk_style_context_save (context);
3027 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3029 if (priv->upper_arrow_visible && !priv->tearoff_active)
3031 gtk_style_context_save (context);
3032 gtk_style_context_set_state (context, priv->upper_arrow_state);
3034 gtk_render_background (context, cr,
3036 upper.width, upper.height);
3037 gtk_render_frame (context, cr,
3039 upper.width, upper.height);
3041 gtk_render_arrow (context, cr, 0,
3042 upper.x + (upper.width - arrow_size) / 2,
3043 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3046 gtk_style_context_restore (context);
3049 if (priv->lower_arrow_visible && !priv->tearoff_active)
3051 gtk_style_context_save (context);
3052 gtk_style_context_set_state (context, priv->lower_arrow_state);
3054 gtk_render_background (context, cr,
3056 lower.width, lower.height);
3057 gtk_render_frame (context, cr,
3059 lower.width, lower.height);
3061 gtk_render_arrow (context, cr, G_PI,
3062 lower.x + (lower.width - arrow_size) / 2,
3063 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3066 gtk_style_context_restore (context);
3069 gtk_style_context_restore (context);
3072 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3074 gint y = -border.y + priv->scroll_offset;
3077 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3079 if (!priv->tearoff_active)
3081 GtkBorder arrow_border;
3083 get_arrows_border (menu, &arrow_border);
3084 y -= arrow_border.top;
3087 gtk_render_background (context, cr,
3089 border.width, border.height);
3090 gtk_render_frame (context, cr,
3092 border.width, border.height);
3097 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3103 gtk_menu_show (GtkWidget *widget)
3105 GtkMenu *menu = GTK_MENU (widget);
3107 _gtk_menu_refresh_accel_paths (menu, FALSE);
3109 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3114 gtk_menu_get_preferred_width (GtkWidget *widget,
3119 GtkMenuShell *menu_shell;
3120 GtkMenuPrivate *priv;
3123 guint max_toggle_size;
3124 guint max_accel_width;
3125 guint horizontal_padding;
3127 gint child_min, child_nat;
3128 gint min_width, nat_width;
3131 menu = GTK_MENU (widget);
3132 menu_shell = GTK_MENU_SHELL (widget);
3135 min_width = nat_width = 0;
3137 max_toggle_size = 0;
3138 max_accel_width = 0;
3140 children = menu_shell->priv->children;
3147 child = children->data;
3148 children = children->next;
3150 if (! gtk_widget_get_visible (child))
3153 get_effective_child_attach (child, &l, &r, &t, &b);
3155 /* It's important to size_request the child
3156 * before doing the toggle size request, in
3157 * case the toggle size request depends on the size
3158 * request of a child of the child (e.g. for ImageMenuItem)
3160 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3162 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3163 max_toggle_size = MAX (max_toggle_size, toggle_size);
3164 max_accel_width = MAX (max_accel_width,
3165 GTK_MENU_ITEM (child)->priv->accelerator_width);
3167 part = child_min / (r - l);
3168 min_width = MAX (min_width, part);
3170 part = child_nat / (r - l);
3171 nat_width = MAX (nat_width, part);
3174 /* If the menu doesn't include any images or check items
3175 * reserve the space so that all menus are consistent.
3176 * We only do this for 'ordinary' menus, not for combobox
3177 * menus or multi-column menus
3179 if (max_toggle_size == 0 &&
3180 gtk_menu_get_n_columns (menu) == 1 &&
3181 !priv->no_toggle_size)
3183 GtkStyleContext *context;
3184 GtkWidgetPath *menu_path, *check_path;
3185 guint toggle_spacing;
3186 guint indicator_size;
3188 context = gtk_widget_get_style_context (widget);
3189 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3191 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3192 check_path = gtk_widget_path_copy (menu_path);
3193 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3195 gtk_style_context_set_path (context, check_path);
3196 gtk_widget_path_free (check_path);
3198 gtk_style_context_get_style (context,
3199 "toggle-spacing", &toggle_spacing,
3200 "indicator-size", &indicator_size,
3203 max_toggle_size = indicator_size + toggle_spacing;
3205 /* Restore real widget path */
3206 gtk_style_context_set_path (context, menu_path);
3207 gtk_widget_path_free (menu_path);
3210 min_width += 2 * max_toggle_size + max_accel_width;
3211 min_width *= gtk_menu_get_n_columns (menu);
3213 nat_width += 2 * max_toggle_size + max_accel_width;
3214 nat_width *= gtk_menu_get_n_columns (menu);
3216 gtk_widget_style_get (GTK_WIDGET (menu),
3217 "horizontal-padding", &horizontal_padding,
3220 get_menu_padding (widget, &padding);
3221 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3222 min_width += (2 * (border_width + horizontal_padding)) +
3223 padding.left + padding.right;
3224 nat_width += (2 * (border_width + horizontal_padding)) +
3225 padding.left + padding.right;
3227 priv->toggle_size = max_toggle_size;
3228 priv->accel_size = max_accel_width;
3231 *minimum_size = min_width;
3234 *natural_size = nat_width;
3236 /* Don't resize the tearoff if it is not active,
3237 * because it won't redraw (it is only a background pixmap).
3239 if (priv->tearoff_active)
3240 gtk_menu_set_tearoff_hints (menu, min_width);
3244 gtk_menu_get_preferred_height (GtkWidget *widget,
3250 /* Menus are height-for-width only, just return the height
3251 * for the minimum width
3253 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3254 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3258 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3264 GtkMenu *menu = GTK_MENU (widget);
3265 GtkMenuPrivate *priv = menu->priv;
3266 guint *min_heights, *nat_heights;
3267 guint vertical_padding, border_width;
3269 gint min_height, nat_height;
3271 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3272 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3273 get_menu_padding (widget, &padding);
3275 min_height = nat_height = (border_width + vertical_padding) * 2 +
3276 padding.top + padding.bottom;
3279 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3281 for (i = 0; i < n_heights; i++)
3283 min_height += min_heights[i];
3284 nat_height += nat_heights[i];
3287 if (priv->have_position)
3289 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3290 GdkRectangle monitor;
3292 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
3294 if (priv->position_y + min_height > monitor.y + monitor.height)
3295 min_height = monitor.y + monitor.height - priv->position_y;
3297 if (priv->position_y + nat_height > monitor.y + monitor.height)
3298 nat_height = monitor.y + monitor.height - priv->position_y;
3300 if (priv->position_y < monitor.y)
3302 min_height -= monitor.y - priv->position_y;
3303 nat_height -= monitor.y - priv->position_y;
3308 *minimum_size = min_height;
3311 *natural_size = nat_height;
3313 g_free (min_heights);
3314 g_free (nat_heights);
3320 gtk_menu_button_scroll (GtkMenu *menu,
3321 GdkEventButton *event)
3323 GtkMenuPrivate *priv = menu->priv;
3325 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3327 gboolean touchscreen_mode;
3329 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3330 "gtk-touchscreen-mode", &touchscreen_mode,
3333 if (touchscreen_mode)
3334 gtk_menu_handle_scrolling (menu,
3335 event->x_root, event->y_root,
3336 event->type == GDK_BUTTON_PRESS,
3346 pointer_in_menu_window (GtkWidget *widget,
3350 GtkMenu *menu = GTK_MENU (widget);
3351 GtkMenuPrivate *priv = menu->priv;
3352 GtkAllocation allocation;
3354 if (gtk_widget_get_mapped (priv->toplevel))
3356 GtkMenuShell *menu_shell;
3357 gint window_x, window_y;
3359 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3360 &window_x, &window_y);
3362 gtk_widget_get_allocation (widget, &allocation);
3363 if (x_root >= window_x && x_root < window_x + allocation.width &&
3364 y_root >= window_y && y_root < window_y + allocation.height)
3367 menu_shell = GTK_MENU_SHELL (widget);
3369 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3370 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3378 gtk_menu_button_press (GtkWidget *widget,
3379 GdkEventButton *event)
3381 if (event->type != GDK_BUTTON_PRESS)
3384 /* Don't pass down to menu shell for presses over scroll arrows
3386 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3389 /* Don't pass down to menu shell if a non-menuitem part of the menu
3390 * was clicked. The check for the event_widget being a GtkMenuShell
3391 * works because we have the pointer grabbed on menu_shell->window
3392 * with owner_events=TRUE, so all events that are either outside
3393 * the menu or on its border are delivered relative to
3394 * menu_shell->window.
3396 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3397 pointer_in_menu_window (widget, event->x_root, event->y_root))
3400 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3404 gtk_menu_button_release (GtkWidget *widget,
3405 GdkEventButton *event)
3407 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3409 if (priv->ignore_button_release)
3411 priv->ignore_button_release = FALSE;
3415 if (event->type != GDK_BUTTON_RELEASE)
3418 /* Don't pass down to menu shell for releases over scroll arrows
3420 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3423 /* Don't pass down to menu shell if a non-menuitem part of the menu
3424 * was clicked (see comment in button_press()).
3426 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3427 pointer_in_menu_window (widget, event->x_root, event->y_root))
3429 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3430 * bail out early here so it is in a consistent state for the
3431 * next button_press/button_release in GtkMenuShell.
3434 if (GTK_MENU_SHELL (widget)->priv->active)
3435 GTK_MENU_SHELL (widget)->priv->button = 0;
3440 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3443 static const gchar *
3444 get_accel_path (GtkWidget *menu_item,
3449 GClosure *accel_closure;
3450 GtkAccelGroup *accel_group;
3452 path = _gtk_widget_get_accel_path (menu_item, locked);
3455 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3461 label = gtk_bin_get_child (GTK_BIN (menu_item));
3463 if (GTK_IS_ACCEL_LABEL (label))
3465 g_object_get (label,
3466 "accel-closure", &accel_closure,
3470 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3472 *locked = gtk_accel_group_get_is_locked (accel_group);
3482 gtk_menu_key_press (GtkWidget *widget,
3485 GtkMenuShell *menu_shell;
3487 gboolean delete = FALSE;
3488 gboolean can_change_accels;
3489 gchar *accel = NULL;
3490 guint accel_key, accel_mods;
3491 GdkModifierType consumed_modifiers;
3492 GdkDisplay *display;
3494 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3495 g_return_val_if_fail (event != NULL, FALSE);
3497 menu_shell = GTK_MENU_SHELL (widget);
3498 menu = GTK_MENU (widget);
3500 gtk_menu_stop_navigating_submenu (menu);
3502 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3505 display = gtk_widget_get_display (widget);
3507 g_object_get (gtk_widget_get_settings (widget),
3508 "gtk-menu-bar-accel", &accel,
3509 "gtk-can-change-accels", &can_change_accels,
3512 if (accel && *accel)
3515 GdkModifierType mods = 0;
3517 gtk_accelerator_parse (accel, &keyval, &mods);
3520 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3522 /* FIXME this is wrong, needs to be in the global accel resolution
3523 * thing, to properly consider i18n etc., but that probably requires
3524 * AccelGroup changes etc.
3526 if (event->keyval == keyval && (mods & event->state) == mods)
3528 gtk_menu_shell_cancel (menu_shell);
3536 switch (event->keyval)
3538 case GDK_KEY_Delete:
3539 case GDK_KEY_KP_Delete:
3540 case GDK_KEY_BackSpace:
3547 /* Figure out what modifiers went into determining the key symbol */
3548 _gtk_translate_keyboard_accel_state (gdk_keymap_get_for_display (display),
3549 event->hardware_keycode,
3551 gtk_accelerator_get_default_mod_mask (),
3553 &accel_key, NULL, NULL, &consumed_modifiers);
3555 accel_key = gdk_keyval_to_lower (accel_key);
3556 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3558 /* If lowercasing affects the keysym, then we need to include SHIFT
3559 * in the modifiers, We re-upper case when we match against the
3560 * keyval, but display and save in caseless form.
3562 if (accel_key != event->keyval)
3563 accel_mods |= GDK_SHIFT_MASK;
3565 /* Modify the accelerators */
3566 if (can_change_accels &&
3567 menu_shell->priv->active_menu_item &&
3568 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3569 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3570 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3572 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3573 gboolean locked, replace_accels = TRUE;
3576 path = get_accel_path (menu_item, &locked);
3577 if (!path || locked)
3579 /* Can't change accelerators on menu_items without paths
3580 * (basically, those items are accelerator-locked).
3582 gtk_widget_error_bell (widget);
3588 /* For the keys that act to delete the current setting,
3589 * we delete the current setting if there is one, otherwise,
3590 * we set the key as the accelerator.
3596 if (gtk_accel_map_lookup_entry (path, &key) &&
3597 (key.accel_key || key.accel_mods))
3603 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3607 /* We failed, probably because this key is in use
3608 * and locked already.
3610 gtk_widget_error_bell (widget);
3619 check_threshold (GtkWidget *widget,
3628 ABS (start_x - x) > THRESHOLD ||
3629 ABS (start_y - y) > THRESHOLD;
3633 definitely_within_item (GtkWidget *widget,
3637 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3640 w = gdk_window_get_width (window);
3641 h = gdk_window_get_height (window);
3644 check_threshold (widget, 0, 0, x, y) &&
3645 check_threshold (widget, w - 1, 0, x, y) &&
3646 check_threshold (widget, w - 1, h - 1, x, y) &&
3647 check_threshold (widget, 0, h - 1, x, y);
3651 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3653 GtkMenuPrivate *priv = menu->priv;
3655 return priv->navigation_height && priv->navigation_width;
3659 gtk_menu_motion_notify (GtkWidget *widget,
3660 GdkEventMotion *event)
3662 GtkWidget *menu_item;
3664 GtkMenuShell *menu_shell;
3667 gboolean need_enter;
3669 if (GTK_IS_MENU (widget))
3671 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3673 if (priv->ignore_button_release)
3674 priv->ignore_button_release = FALSE;
3676 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3680 /* We received the event for one of two reasons:
3682 * a) We are the active menu, and did gtk_grab_add()
3683 * b) The widget is a child of ours, and the event was propagated
3685 * Since for computation of navigation regions, we want the menu which
3686 * is the parent of the menu item, for a), we need to find that menu,
3687 * which may be different from 'widget'.
3689 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3690 parent = gtk_widget_get_parent (menu_item);
3691 if (!GTK_IS_MENU_ITEM (menu_item) ||
3692 !GTK_IS_MENU (parent))
3695 menu_shell = GTK_MENU_SHELL (parent);
3696 menu = GTK_MENU (menu_shell);
3698 if (definitely_within_item (menu_item, event->x, event->y))
3699 menu_shell->priv->activate_time = 0;
3701 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3703 /* Check to see if we are within an active submenu's navigation region
3705 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3708 /* Make sure we pop down if we enter a non-selectable menu item, so we
3709 * don't show a submenu when the cursor is outside the stay-up triangle.
3711 if (!_gtk_menu_item_is_selectable (menu_item))
3713 /* We really want to deselect, but this gives the menushell code
3714 * a chance to do some bookkeeping about the menuitem.
3716 gtk_menu_shell_select_item (menu_shell, menu_item);
3722 /* The menu is now sensitive to enter events on its items, but
3723 * was previously sensitive. So we fake an enter event.
3725 menu_shell->priv->ignore_enter = FALSE;
3727 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3728 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3730 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3733 send_event->crossing.window = g_object_ref (event->window);
3734 send_event->crossing.time = event->time;
3735 send_event->crossing.send_event = TRUE;
3736 send_event->crossing.x_root = event->x_root;
3737 send_event->crossing.y_root = event->y_root;
3738 send_event->crossing.x = event->x;
3739 send_event->crossing.y = event->y;
3740 send_event->crossing.state = event->state;
3741 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3743 /* We send the event to 'widget', the currently active menu,
3744 * instead of 'menu', the menu that the pointer is in. This
3745 * will ensure that the event will be ignored unless the
3746 * menuitem is a child of the active menu or some parent
3747 * menu of the active menu.
3749 result = gtk_widget_event (widget, send_event);
3750 gdk_event_free (send_event);
3760 get_double_arrows (GtkMenu *menu)
3762 GtkMenuPrivate *priv = menu->priv;
3763 gboolean double_arrows;
3764 GtkArrowPlacement arrow_placement;
3766 gtk_widget_style_get (GTK_WIDGET (menu),
3767 "double-arrows", &double_arrows,
3768 "arrow-placement", &arrow_placement,
3771 if (arrow_placement != GTK_ARROWS_BOTH)
3774 return double_arrows || (priv->initially_pushed_in &&
3775 priv->scroll_offset != 0);
3779 gtk_menu_scroll_by (GtkMenu *menu,
3782 GtkMenuPrivate *priv = menu->priv;
3783 GtkBorder arrow_border;
3787 gboolean double_arrows;
3789 widget = GTK_WIDGET (menu);
3790 offset = priv->scroll_offset + step;
3792 get_arrows_border (menu, &arrow_border);
3794 double_arrows = get_double_arrows (menu);
3796 /* If we scroll upward and the non-visible top part
3797 * is smaller than the scroll arrow it would be
3798 * pretty stupid to show the arrow and taking more
3799 * screen space than just scrolling to the top.
3802 if ((step < 0) && (offset < arrow_border.top))
3805 /* Don't scroll over the top if we weren't before: */
3806 if ((priv->scroll_offset >= 0) && (offset < 0))
3809 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3811 if (priv->scroll_offset == 0 &&
3812 view_height >= priv->requested_height)
3815 /* Don't scroll past the bottom if we weren't before: */
3816 if (priv->scroll_offset > 0)
3817 view_height -= arrow_border.top;
3819 /* When both arrows are always shown,
3820 * reduce view height even more.
3823 view_height -= arrow_border.bottom;
3825 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3826 (offset + view_height > priv->requested_height))
3827 offset = priv->requested_height - view_height;
3829 if (offset != priv->scroll_offset)
3830 gtk_menu_scroll_to (menu, offset);
3834 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3835 gboolean touchscreen_mode)
3837 GtkMenuPrivate *priv = menu->priv;
3838 gboolean upper_visible;
3839 gboolean lower_visible;
3841 upper_visible = priv->upper_arrow_visible;
3842 lower_visible = priv->lower_arrow_visible;
3844 gtk_menu_scroll_by (menu, priv->scroll_step);
3846 if (touchscreen_mode &&
3847 (upper_visible != priv->upper_arrow_visible ||
3848 lower_visible != priv->lower_arrow_visible))
3850 /* We are about to hide a scroll arrow while the mouse is pressed,
3851 * this would cause the uncovered menu item to be activated on button
3852 * release. Therefore we need to ignore button release here
3854 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3855 priv->ignore_button_release = TRUE;
3860 gtk_menu_scroll_timeout (gpointer data)
3863 gboolean touchscreen_mode;
3865 menu = GTK_MENU (data);
3867 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3868 "gtk-touchscreen-mode", &touchscreen_mode,
3871 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3877 gtk_menu_scroll_timeout_initial (gpointer data)
3881 gboolean touchscreen_mode;
3883 menu = GTK_MENU (data);
3885 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3886 "gtk-timeout-repeat", &timeout,
3887 "gtk-touchscreen-mode", &touchscreen_mode,
3890 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3892 gtk_menu_remove_scroll_timeout (menu);
3894 menu->priv->scroll_timeout =
3895 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3901 gtk_menu_start_scrolling (GtkMenu *menu)
3904 gboolean touchscreen_mode;
3906 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3907 "gtk-timeout-repeat", &timeout,
3908 "gtk-touchscreen-mode", &touchscreen_mode,
3911 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3913 menu->priv->scroll_timeout =
3914 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3918 gtk_menu_scroll (GtkWidget *widget,
3919 GdkEventScroll *event)
3921 GtkMenu *menu = GTK_MENU (widget);
3923 switch (event->direction)
3925 case GDK_SCROLL_RIGHT:
3926 case GDK_SCROLL_DOWN:
3927 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3929 case GDK_SCROLL_LEFT:
3931 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3939 get_arrows_sensitive_area (GtkMenu *menu,
3940 GdkRectangle *upper,
3941 GdkRectangle *lower)
3943 GtkArrowPlacement arrow_placement;
3944 GtkWidget *widget = GTK_WIDGET (menu);
3948 guint vertical_padding;
3950 gint scroll_arrow_height;
3953 window = gtk_widget_get_window (widget);
3954 width = gdk_window_get_width (window);
3955 height = gdk_window_get_height (window);
3957 gtk_widget_style_get (widget,
3958 "vertical-padding", &vertical_padding,
3959 "scroll-arrow-vlength", &scroll_arrow_height,
3960 "arrow-placement", &arrow_placement,
3963 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3964 get_menu_padding (widget, &padding);
3966 gdk_window_get_position (window, &win_x, &win_y);
3968 switch (arrow_placement)
3970 case GTK_ARROWS_BOTH:
3975 upper->width = width;
3976 upper->height = scroll_arrow_height + border + padding.top;
3982 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3983 lower->width = width;
3984 lower->height = scroll_arrow_height + border + padding.bottom;
3988 case GTK_ARROWS_START:
3993 upper->width = width / 2;
3994 upper->height = scroll_arrow_height + border + padding.top;
3999 lower->x = win_x + width / 2;
4001 lower->width = width / 2;
4002 lower->height = scroll_arrow_height + border + padding.bottom;
4006 case GTK_ARROWS_END:
4010 upper->y = win_y + height - border - scroll_arrow_height;
4011 upper->width = width / 2;
4012 upper->height = scroll_arrow_height + border + padding.top;
4017 lower->x = win_x + width / 2;
4018 lower->y = win_y + height - border - scroll_arrow_height;
4019 lower->width = width / 2;
4020 lower->height = scroll_arrow_height + border + padding.bottom;
4028 gtk_menu_handle_scrolling (GtkMenu *menu,
4034 GtkMenuPrivate *priv = menu->priv;
4035 GtkMenuShell *menu_shell;
4038 gboolean scroll_fast = FALSE;
4040 gboolean touchscreen_mode;
4042 menu_shell = GTK_MENU_SHELL (menu);
4044 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4045 "gtk-touchscreen-mode", &touchscreen_mode,
4048 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4053 /* upper arrow handling */
4055 get_arrows_sensitive_area (menu, &rect, NULL);
4058 if (priv->upper_arrow_visible && !priv->tearoff_active &&
4059 (x >= rect.x) && (x < rect.x + rect.width) &&
4060 (y >= rect.y) && (y < rect.y + rect.height))
4065 if (touchscreen_mode)
4066 priv->upper_arrow_prelight = in_arrow;
4068 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4070 gboolean arrow_pressed = FALSE;
4072 if (priv->upper_arrow_visible && !priv->tearoff_active)
4074 if (touchscreen_mode)
4076 if (enter && priv->upper_arrow_prelight)
4078 if (priv->scroll_timeout == 0)
4080 /* Deselect the active item so that
4081 * any submenus are popped down
4083 gtk_menu_shell_deselect (menu_shell);
4085 gtk_menu_remove_scroll_timeout (menu);
4086 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4090 /* Only do stuff on click. */
4091 gtk_menu_start_scrolling (menu);
4092 arrow_pressed = TRUE;
4097 arrow_pressed = TRUE;
4102 gtk_menu_stop_scrolling (menu);
4105 else /* !touchscreen_mode */
4107 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4109 if (enter && in_arrow &&
4110 (!priv->upper_arrow_prelight ||
4111 priv->scroll_fast != scroll_fast))
4113 priv->upper_arrow_prelight = TRUE;
4114 priv->scroll_fast = scroll_fast;
4116 /* Deselect the active item so that
4117 * any submenus are popped down
4119 gtk_menu_shell_deselect (menu_shell);
4121 gtk_menu_remove_scroll_timeout (menu);
4122 priv->scroll_step = scroll_fast
4123 ? -MENU_SCROLL_STEP2
4124 : -MENU_SCROLL_STEP1;
4126 priv->scroll_timeout =
4127 gdk_threads_add_timeout (scroll_fast
4128 ? MENU_SCROLL_TIMEOUT2
4129 : MENU_SCROLL_TIMEOUT1,
4130 gtk_menu_scroll_timeout, menu);
4132 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4134 gtk_menu_stop_scrolling (menu);
4139 /* gtk_menu_start_scrolling() might have hit the top of the
4140 * menu, so check if the button isn't insensitive before
4141 * changing it to something else.
4143 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4145 GtkStateFlags arrow_state = 0;
4148 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4150 if (priv->upper_arrow_prelight)
4151 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4153 if (arrow_state != priv->upper_arrow_state)
4155 priv->upper_arrow_state = arrow_state;
4157 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4163 /* lower arrow handling */
4165 get_arrows_sensitive_area (menu, NULL, &rect);
4168 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4169 (x >= rect.x) && (x < rect.x + rect.width) &&
4170 (y >= rect.y) && (y < rect.y + rect.height))
4175 if (touchscreen_mode)
4176 priv->lower_arrow_prelight = in_arrow;
4178 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4180 gboolean arrow_pressed = FALSE;
4182 if (priv->lower_arrow_visible && !priv->tearoff_active)
4184 if (touchscreen_mode)
4186 if (enter && priv->lower_arrow_prelight)
4188 if (priv->scroll_timeout == 0)
4190 /* Deselect the active item so that
4191 * any submenus are popped down
4193 gtk_menu_shell_deselect (menu_shell);
4195 gtk_menu_remove_scroll_timeout (menu);
4196 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4200 /* Only do stuff on click. */
4201 gtk_menu_start_scrolling (menu);
4202 arrow_pressed = TRUE;
4207 arrow_pressed = TRUE;
4212 gtk_menu_stop_scrolling (menu);
4215 else /* !touchscreen_mode */
4217 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4219 if (enter && in_arrow &&
4220 (!priv->lower_arrow_prelight ||
4221 priv->scroll_fast != scroll_fast))
4223 priv->lower_arrow_prelight = TRUE;
4224 priv->scroll_fast = scroll_fast;
4226 /* Deselect the active item so that
4227 * any submenus are popped down
4229 gtk_menu_shell_deselect (menu_shell);
4231 gtk_menu_remove_scroll_timeout (menu);
4232 priv->scroll_step = scroll_fast
4234 : MENU_SCROLL_STEP1;
4236 priv->scroll_timeout =
4237 gdk_threads_add_timeout (scroll_fast
4238 ? MENU_SCROLL_TIMEOUT2
4239 : MENU_SCROLL_TIMEOUT1,
4240 gtk_menu_scroll_timeout, menu);
4242 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4244 gtk_menu_stop_scrolling (menu);
4249 /* gtk_menu_start_scrolling() might have hit the bottom of the
4250 * menu, so check if the button isn't insensitive before
4251 * changing it to something else.
4253 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4255 GtkStateFlags arrow_state = 0;
4258 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4260 if (priv->lower_arrow_prelight)
4261 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4263 if (arrow_state != priv->lower_arrow_state)
4265 priv->lower_arrow_state = arrow_state;
4267 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4275 gtk_menu_enter_notify (GtkWidget *widget,
4276 GdkEventCrossing *event)
4278 GtkWidget *menu_item;
4280 gboolean touchscreen_mode;
4282 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4283 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4284 event->mode == GDK_CROSSING_STATE_CHANGED)
4287 g_object_get (gtk_widget_get_settings (widget),
4288 "gtk-touchscreen-mode", &touchscreen_mode,
4291 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4292 if (GTK_IS_MENU (widget))
4294 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4296 if (!menu_shell->priv->ignore_enter)
4297 gtk_menu_handle_scrolling (GTK_MENU (widget),
4298 event->x_root, event->y_root, TRUE, TRUE);
4301 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4303 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4305 if (GTK_IS_MENU (menu))
4307 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4308 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4310 if (priv->seen_item_enter)
4312 /* This is the second enter we see for an item
4313 * on this menu. This means a release should always
4316 menu_shell->priv->activate_time = 0;
4318 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4319 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4321 if (definitely_within_item (menu_item, event->x, event->y))
4323 /* This is an actual user-enter (ie. not a pop-under)
4324 * In this case, the user must either have entered
4325 * sufficiently far enough into the item, or he must move
4326 * far enough away from the enter point. (see
4327 * gtk_menu_motion_notify())
4329 menu_shell->priv->activate_time = 0;
4333 priv->seen_item_enter = TRUE;
4337 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4338 * will not correspond to the event widget's parent. Check to see
4339 * if we are in the parent's navigation region.
4341 parent = gtk_widget_get_parent (menu_item);
4342 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4343 gtk_menu_navigating_submenu (GTK_MENU (parent),
4344 event->x_root, event->y_root))
4347 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4351 gtk_menu_leave_notify (GtkWidget *widget,
4352 GdkEventCrossing *event)
4354 GtkMenuShell *menu_shell;
4356 GtkMenuItem *menu_item;
4357 GtkWidget *event_widget;
4359 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4360 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4361 event->mode == GDK_CROSSING_STATE_CHANGED)
4364 menu = GTK_MENU (widget);
4365 menu_shell = GTK_MENU_SHELL (widget);
4367 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4370 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4372 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4374 if (!GTK_IS_MENU_ITEM (event_widget))
4377 menu_item = GTK_MENU_ITEM (event_widget);
4379 /* Here we check to see if we're leaving an active menu item
4380 * with a submenu, in which case we enter submenu navigation mode.
4382 if (menu_shell->priv->active_menu_item != NULL
4383 && menu_item->priv->submenu != NULL
4384 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4386 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4388 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4391 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4393 /* We are leaving an active menu item with nonactive submenu.
4394 * Deselect it so we don't surprise the user with by popping
4395 * up a submenu _after_ he left the item.
4397 gtk_menu_shell_deselect (menu_shell);
4402 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4406 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4408 GtkMenuPrivate *priv = menu->priv;
4410 priv->navigation_x = 0;
4411 priv->navigation_y = 0;
4412 priv->navigation_width = 0;
4413 priv->navigation_height = 0;
4415 if (priv->navigation_timeout)
4417 g_source_remove (priv->navigation_timeout);
4418 priv->navigation_timeout = 0;
4422 /* When the timeout is elapsed, the navigation region is destroyed
4423 * and the menuitem under the pointer (if any) is selected.
4426 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4428 GtkMenuPopdownData *popdown_data = user_data;
4429 GtkMenu *menu = popdown_data->menu;
4430 GtkMenuPrivate *priv = menu->priv;
4431 GdkWindow *child_window;
4433 gtk_menu_stop_navigating_submenu (menu);
4435 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4437 child_window = gdk_window_get_device_position (priv->bin_window,
4438 popdown_data->device,
4443 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4445 send_event->crossing.window = g_object_ref (child_window);
4446 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4447 send_event->crossing.send_event = TRUE;
4448 gdk_event_set_device (send_event, popdown_data->device);
4450 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4452 gdk_event_free (send_event);
4460 gtk_menu_navigating_submenu (GtkMenu *menu,
4464 GtkMenuPrivate *priv = menu->priv;
4467 if (!gtk_menu_has_navigation_triangle (menu))
4470 width = priv->navigation_width;
4471 height = priv->navigation_height;
4473 /* Check if x/y are in the triangle spanned by the navigation parameters */
4475 /* 1) Move the coordinates so the triangle starts at 0,0 */
4476 event_x -= priv->navigation_x;
4477 event_y -= priv->navigation_y;
4479 /* 2) Ensure both legs move along the positive axis */
4491 /* 3) Check that the given coordinate is inside the triangle. The formula
4492 * is a transformed form of this formula: x/w + y/h <= 1
4494 if (event_x >= 0 && event_y >= 0 &&
4495 event_x * height + event_y * width <= width * height)
4501 gtk_menu_stop_navigating_submenu (menu);
4507 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4508 GtkMenuItem *menu_item,
4509 GdkEventCrossing *event)
4511 GtkMenuPrivate *priv = menu->priv;
4512 gint submenu_left = 0;
4513 gint submenu_right = 0;
4514 gint submenu_top = 0;
4515 gint submenu_bottom = 0;
4517 GtkWidget *event_widget;
4518 GtkMenuPopdownData *popdown_data;
4521 g_return_if_fail (menu_item->priv->submenu != NULL);
4522 g_return_if_fail (event != NULL);
4524 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4526 window = gtk_widget_get_window (menu_item->priv->submenu);
4527 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4529 submenu_right = submenu_left + gdk_window_get_width (window);
4530 submenu_bottom = submenu_top + gdk_window_get_height (window);
4532 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4534 if (event->x >= 0 && event->x < width)
4538 gtk_menu_stop_navigating_submenu (menu);
4540 /* The navigation region is the triangle closest to the x/y
4541 * location of the rectangle. This is why the width or height
4544 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4547 priv->navigation_x = submenu_left;
4548 priv->navigation_width = event->x_root - submenu_left;
4553 priv->navigation_x = submenu_right;
4554 priv->navigation_width = event->x_root - submenu_right;
4560 priv->navigation_y = event->y_root;
4561 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4563 if (priv->navigation_height >= 0)
4569 priv->navigation_y = event->y_root;
4570 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4572 if (priv->navigation_height <= 0)
4576 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4577 "gtk-menu-popdown-delay", &popdown_delay,
4580 popdown_data = g_new (GtkMenuPopdownData, 1);
4581 popdown_data->menu = menu;
4582 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4584 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4586 gtk_menu_stop_navigating_submenu_cb,
4588 (GDestroyNotify) g_free);
4593 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4597 g_return_if_fail (GTK_IS_MENU (menu_shell));
4599 parent = menu_shell->priv->parent_menu_shell;
4601 menu_shell->priv->activate_time = 0;
4602 gtk_menu_popdown (GTK_MENU (menu_shell));
4605 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4609 gtk_menu_position (GtkMenu *menu,
4610 gboolean set_scroll_offset)
4612 GtkMenuPrivate *priv = menu->priv;
4614 GtkRequisition requisition;
4618 GdkScreen *pointer_screen;
4619 GdkRectangle monitor;
4622 widget = GTK_WIDGET (menu);
4624 screen = gtk_widget_get_screen (widget);
4625 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4626 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4628 /* Realize so we have the proper width and heigh to figure out
4629 * the right place to popup the menu.
4631 gtk_widget_realize (priv->toplevel);
4632 requisition.width = gtk_widget_get_allocated_width (widget);
4633 requisition.height = gtk_widget_get_allocated_height (widget);
4635 if (pointer_screen != screen)
4637 /* Pointer is on a different screen; roughly center the
4638 * menu on the screen. If someone was using multiscreen
4639 * + Xinerama together they'd probably want something
4640 * fancier; but that is likely to be vanishingly rare.
4642 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4643 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4646 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4647 priv->initially_pushed_in = FALSE;
4649 /* Set the type hint here to allow custom position functions
4650 * to set a different hint
4652 if (!gtk_widget_get_visible (priv->toplevel))
4653 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4655 if (priv->position_func)
4657 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4658 priv->position_func_data);
4660 if (priv->monitor_num < 0)
4661 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4663 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4667 gint space_left, space_right, space_above, space_below;
4671 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4673 get_menu_padding (widget, &padding);
4675 /* The placement of popup menus horizontally works like this (with
4676 * RTL in parentheses)
4678 * - If there is enough room to the right (left) of the mouse cursor,
4679 * position the menu there.
4681 * - Otherwise, if if there is enough room to the left (right) of the
4682 * mouse cursor, position the menu there.
4684 * - Otherwise if the menu is smaller than the monitor, position it
4685 * on the side of the mouse cursor that has the most space available
4687 * - Otherwise (if there is simply not enough room for the menu on the
4688 * monitor), position it as far left (right) as possible.
4690 * Positioning in the vertical direction is similar: first try below
4691 * mouse cursor, then above.
4693 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4695 space_left = x - monitor.x;
4696 space_right = monitor.x + monitor.width - x - 1;
4697 space_above = y - monitor.y;
4698 space_below = monitor.y + monitor.height - y - 1;
4700 /* Position horizontally. */
4702 /* the amount of space we need to position the menu.
4703 * Note the menu is offset "thickness" pixels
4705 needed_width = requisition.width - padding.left;
4707 if (needed_width <= space_left ||
4708 needed_width <= space_right)
4710 if ((rtl && needed_width <= space_left) ||
4711 (!rtl && needed_width > space_right))
4714 x = x + padding.left - requisition.width + 1;
4718 /* position right */
4719 x = x - padding.right;
4722 /* x is clamped on-screen further down */
4724 else if (requisition.width <= monitor.width)
4726 /* the menu is too big to fit on either side of the mouse
4727 * cursor, but smaller than the monitor. Position it on
4728 * the side that has the most space
4730 if (space_left > space_right)
4738 x = monitor.x + monitor.width - requisition.width;
4741 else /* menu is simply too big for the monitor */
4746 x = monitor.x + monitor.width - requisition.width;
4755 /* Position vertically.
4756 * The algorithm is the same as above, but simpler
4757 * because we don't have to take RTL into account.
4759 needed_height = requisition.height - padding.top;
4761 if (needed_height <= space_above ||
4762 needed_height <= space_below)
4764 if (needed_height <= space_below)
4765 y = y - padding.top;
4767 y = y + padding.bottom - requisition.height + 1;
4769 y = CLAMP (y, monitor.y,
4770 monitor.y + monitor.height - requisition.height);
4772 else if (needed_height > space_below && needed_height > space_above)
4774 if (space_below >= space_above)
4775 y = monitor.y + monitor.height - requisition.height;
4787 if (y + requisition.height > monitor.y + monitor.height)
4789 if (priv->initially_pushed_in)
4790 scroll_offset += (monitor.y + monitor.height) - requisition.height - y;
4791 y = (monitor.y + monitor.height) - requisition.height;
4796 if (priv->initially_pushed_in)
4797 scroll_offset += monitor.y - y;
4801 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4803 if (GTK_MENU_SHELL (menu)->priv->active)
4805 priv->have_position = TRUE;
4806 priv->position_x = x;
4807 priv->position_y = y;
4810 if (scroll_offset != 0)
4812 GtkBorder arrow_border;
4814 get_arrows_border (menu, &arrow_border);
4815 scroll_offset += arrow_border.top;
4818 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4821 if (!GTK_MENU_SHELL (menu)->priv->active)
4823 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4824 requisition.width, requisition.height);
4827 if (set_scroll_offset)
4828 priv->scroll_offset = scroll_offset;
4832 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4834 GtkMenuPrivate *priv = menu->priv;
4836 if (priv->scroll_timeout)
4838 g_source_remove (priv->scroll_timeout);
4839 priv->scroll_timeout = 0;
4844 gtk_menu_stop_scrolling (GtkMenu *menu)
4846 GtkMenuPrivate *priv = menu->priv;
4847 gboolean touchscreen_mode;
4849 gtk_menu_remove_scroll_timeout (menu);
4851 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4852 "gtk-touchscreen-mode", &touchscreen_mode,
4855 if (!touchscreen_mode)
4857 priv->upper_arrow_prelight = FALSE;
4858 priv->lower_arrow_prelight = FALSE;
4863 gtk_menu_scroll_to (GtkMenu *menu,
4866 GtkMenuPrivate *priv = menu->priv;
4867 GtkBorder arrow_border, padding;
4870 gint view_width, view_height;
4873 guint vertical_padding;
4874 guint horizontal_padding;
4875 gboolean double_arrows;
4877 widget = GTK_WIDGET (menu);
4879 if (priv->tearoff_active && priv->tearoff_adjustment)
4880 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4882 /* Move/resize the viewport according to arrows: */
4883 view_width = gtk_widget_get_allocated_width (widget);
4884 view_height = gtk_widget_get_allocated_height (widget);
4886 gtk_widget_style_get (GTK_WIDGET (menu),
4887 "vertical-padding", &vertical_padding,
4888 "horizontal-padding", &horizontal_padding,
4891 get_menu_padding (widget, &padding);
4892 double_arrows = get_double_arrows (menu);
4894 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4896 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4897 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4898 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4899 padding.top - padding.bottom;
4901 x = border_width + padding.left + horizontal_padding;
4902 y = border_width + padding.top + vertical_padding;
4904 if (double_arrows && !priv->tearoff_active)
4906 if (view_height < menu_height ||
4907 (offset > 0 && priv->scroll_offset > 0) ||
4908 (offset < 0 && priv->scroll_offset < 0))
4910 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4911 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4913 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4914 gtk_widget_queue_draw (GTK_WIDGET (menu));
4916 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4918 get_arrows_border (menu, &arrow_border);
4919 y += arrow_border.top;
4920 view_height -= arrow_border.top;
4921 view_height -= arrow_border.bottom;
4924 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4927 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4929 if (priv->upper_arrow_prelight)
4930 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4932 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4935 if (offset >= menu_height - view_height)
4936 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4939 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4941 if (priv->lower_arrow_prelight)
4942 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4944 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4947 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4948 (priv->lower_arrow_state != lower_arrow_previous_state))
4949 gtk_widget_queue_draw (GTK_WIDGET (menu));
4951 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4952 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4954 /* At the upper border, possibly remove timeout */
4955 if (priv->scroll_step < 0)
4957 gtk_menu_stop_scrolling (menu);
4958 gtk_widget_queue_draw (GTK_WIDGET (menu));
4962 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4963 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4965 /* At the lower border, possibly remove timeout */
4966 if (priv->scroll_step > 0)
4968 gtk_menu_stop_scrolling (menu);
4969 gtk_widget_queue_draw (GTK_WIDGET (menu));
4973 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4977 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4978 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4980 gtk_menu_stop_scrolling (menu);
4981 gtk_widget_queue_draw (GTK_WIDGET (menu));
4984 else if (!priv->tearoff_active)
4986 gboolean last_visible;
4988 last_visible = priv->upper_arrow_visible;
4989 priv->upper_arrow_visible = offset > 0;
4991 /* upper_arrow_visible may have changed, so requery the border */
4992 get_arrows_border (menu, &arrow_border);
4993 view_height -= arrow_border.top;
4995 if ((last_visible != priv->upper_arrow_visible) &&
4996 !priv->upper_arrow_visible)
4998 priv->upper_arrow_prelight = FALSE;
5000 /* If we hide the upper arrow, possibly remove timeout */
5001 if (priv->scroll_step < 0)
5003 gtk_menu_stop_scrolling (menu);
5004 gtk_widget_queue_draw (GTK_WIDGET (menu));
5008 last_visible = priv->lower_arrow_visible;
5009 priv->lower_arrow_visible = offset < menu_height - view_height;
5011 /* lower_arrow_visible may have changed, so requery the border */
5012 get_arrows_border (menu, &arrow_border);
5013 view_height -= arrow_border.bottom;
5015 if ((last_visible != priv->lower_arrow_visible) &&
5016 !priv->lower_arrow_visible)
5018 priv->lower_arrow_prelight = FALSE;
5020 /* If we hide the lower arrow, possibly remove timeout */
5021 if (priv->scroll_step > 0)
5023 gtk_menu_stop_scrolling (menu);
5024 gtk_widget_queue_draw (GTK_WIDGET (menu));
5028 y += arrow_border.top;
5031 /* Scroll the menu: */
5032 if (gtk_widget_get_realized (widget))
5033 gdk_window_move (priv->bin_window, 0, -offset);
5035 if (gtk_widget_get_realized (widget))
5036 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5038 priv->scroll_offset = offset;
5042 compute_child_offset (GtkMenu *menu,
5043 GtkWidget *menu_item,
5046 gboolean *is_last_child)
5048 GtkMenuPrivate *priv = menu->priv;
5049 gint item_top_attach;
5050 gint item_bottom_attach;
5051 gint child_offset = 0;
5054 get_effective_child_attach (menu_item, NULL, NULL,
5055 &item_top_attach, &item_bottom_attach);
5057 /* there is a possibility that we get called before _size_request,
5058 * so check the height table for safety.
5060 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5063 /* when we have a row with only invisible children, its height will
5064 * be zero, so there's no need to check WIDGET_VISIBLE here
5066 for (i = 0; i < item_top_attach; i++)
5067 child_offset += priv->heights[i];
5070 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5072 *offset = child_offset;
5074 *height = priv->heights[item_top_attach];
5080 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5081 GtkWidget *menu_item)
5083 GtkMenu *menu = GTK_MENU (menu_shell);
5084 GtkMenuPrivate *priv = menu->priv;
5085 GtkWidget *widget = GTK_WIDGET (menu_shell);
5086 gint child_offset, child_height;
5090 gboolean last_child = 0;
5092 /* We need to check if the selected item fully visible.
5093 * If not we need to scroll the menu so that it becomes fully
5096 if (compute_child_offset (menu, menu_item,
5097 &child_offset, &child_height, &last_child))
5099 guint vertical_padding;
5100 gboolean double_arrows;
5103 y = priv->scroll_offset;
5104 height = gdk_window_get_height (gtk_widget_get_window (widget));
5106 gtk_widget_style_get (widget,
5107 "vertical-padding", &vertical_padding,
5110 double_arrows = get_double_arrows (menu);
5111 get_menu_padding (widget, &padding);
5113 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5114 padding.top + padding.bottom +
5115 2 * vertical_padding;
5116 if (child_offset < y)
5118 /* Ignore the enter event we might get if the pointer
5121 menu_shell->priv->ignore_enter = TRUE;
5122 gtk_menu_scroll_to (menu, child_offset);
5126 GtkBorder arrow_border;
5130 get_arrows_border (menu, &arrow_border);
5131 if (!priv->tearoff_active)
5132 arrow_height = arrow_border.top + arrow_border.bottom;
5134 if (child_offset + child_height > y + height - arrow_height)
5137 if ((!last_child && !priv->tearoff_active) || double_arrows)
5138 arrow_height += arrow_border.bottom;
5140 y = child_offset + child_height - height + arrow_height;
5141 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5143 /* Need upper arrow */
5144 arrow_height += arrow_border.top;
5145 y = child_offset + child_height - height + arrow_height;
5147 /* Ignore the enter event we might get if the pointer
5150 menu_shell->priv->ignore_enter = TRUE;
5151 gtk_menu_scroll_to (menu, y);
5158 gtk_menu_select_item (GtkMenuShell *menu_shell,
5159 GtkWidget *menu_item)
5161 GtkMenu *menu = GTK_MENU (menu_shell);
5163 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5164 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5166 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5170 /* Reparent the menu, taking care of the refcounting
5172 * If unrealize is true we force a unrealize while reparenting the parent.
5173 * This can help eliminate flicker in some cases.
5175 * What happens is that when the menu is unrealized and then re-realized,
5176 * the allocations are as follows:
5178 * parent - 1x1 at (0,0)
5179 * child1 - 100x20 at (0,0)
5180 * child2 - 100x20 at (0,20)
5181 * child3 - 100x20 at (0,40)
5183 * That is, the parent is small but the children are full sized. Then,
5184 * when the queued_resize gets processed, the parent gets resized to
5187 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5188 * contains the following logic:
5190 * - if a move or resize operation on a window would change the clip
5191 * region on the children, then before the window is resized
5192 * the background for children is temporarily set to None, the
5193 * move/resize done, and the background for the children restored.
5195 * So, at the point where the parent is resized to final size, the
5196 * background for the children is temporarily None, and thus they
5197 * are not cleared to the background color and the previous background
5198 * (the image of the menu) is left in place.
5201 gtk_menu_reparent (GtkMenu *menu,
5202 GtkWidget *new_parent,
5205 GObject *object = G_OBJECT (menu);
5206 GtkWidget *widget = GTK_WIDGET (menu);
5207 gboolean was_floating = g_object_is_floating (object);
5209 g_object_ref_sink (object);
5213 g_object_ref (object);
5214 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5215 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5216 g_object_unref (object);
5219 gtk_widget_reparent (widget, new_parent);
5222 g_object_force_floating (object);
5224 g_object_unref (object);
5228 gtk_menu_show_all (GtkWidget *widget)
5230 /* Show children, but not self. */
5231 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5235 * gtk_menu_set_screen:
5237 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5238 * determined by the widget the menu is attached to
5240 * Sets the #GdkScreen on which the menu will be displayed.
5245 gtk_menu_set_screen (GtkMenu *menu,
5248 g_return_if_fail (GTK_IS_MENU (menu));
5249 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5251 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5255 menu_change_screen (menu, screen);
5259 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5261 attach_widget_screen_changed (attach_widget, NULL, menu);
5268 * @child: a #GtkMenuItem
5269 * @left_attach: The column number to attach the left side of the item to
5270 * @right_attach: The column number to attach the right side of the item to
5271 * @top_attach: The row number to attach the top of the item to
5272 * @bottom_attach: The row number to attach the bottom of the item to
5274 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5275 * an item will occupy is specified by @left_attach, @right_attach,
5276 * @top_attach and @bottom_attach. These each represent the leftmost,
5277 * rightmost, uppermost and lower column and row numbers of the table.
5278 * (Columns and rows are indexed from zero).
5280 * Note that this function is not related to gtk_menu_detach().
5285 gtk_menu_attach (GtkMenu *menu,
5290 guint bottom_attach)
5292 GtkMenuShell *menu_shell;
5295 g_return_if_fail (GTK_IS_MENU (menu));
5296 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5297 parent = gtk_widget_get_parent (child);
5298 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5299 g_return_if_fail (left_attach < right_attach);
5300 g_return_if_fail (top_attach < bottom_attach);
5302 menu_shell = GTK_MENU_SHELL (menu);
5306 AttachInfo *ai = get_attach_info (child);
5308 ai->left_attach = left_attach;
5309 ai->right_attach = right_attach;
5310 ai->top_attach = top_attach;
5311 ai->bottom_attach = bottom_attach;
5313 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5315 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5317 menu_queue_resize (menu);
5321 gtk_container_child_set (GTK_CONTAINER (parent), child,
5322 "left-attach", left_attach,
5323 "right-attach", right_attach,
5324 "top-attach", top_attach,
5325 "bottom-attach", bottom_attach,
5331 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5335 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5336 "gtk-menu-popup-delay", &popup_delay,
5343 find_child_containing (GtkMenuShell *menu_shell,
5351 /* find a child which includes the area given by
5352 * left, right, top, bottom.
5354 for (list = menu_shell->priv->children; list; list = list->next)
5358 if (!_gtk_menu_item_is_selectable (list->data))
5361 get_effective_child_attach (list->data, &l, &r, &t, &b);
5363 if (l <= left && right <= r && t <= top && bottom <= b)
5364 return GTK_WIDGET (list->data);
5371 gtk_menu_move_current (GtkMenuShell *menu_shell,
5372 GtkMenuDirectionType direction)
5374 GtkMenu *menu = GTK_MENU (menu_shell);
5377 GtkWidget *match = NULL;
5379 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5383 case GTK_MENU_DIR_CHILD:
5384 direction = GTK_MENU_DIR_PARENT;
5386 case GTK_MENU_DIR_PARENT:
5387 direction = GTK_MENU_DIR_CHILD;
5393 /* use special table menu key bindings */
5394 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5396 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5398 if (direction == GTK_MENU_DIR_NEXT)
5400 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5402 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5410 for (i = 0; i < t; i++)
5412 match = find_child_containing (menu_shell,
5413 l, l + 1, i, i + 1);
5419 else if (direction == GTK_MENU_DIR_PREV)
5421 for (i = t; i > 0; i--)
5423 match = find_child_containing (menu_shell,
5424 l, l + 1, i - 1, i);
5432 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5434 match = find_child_containing (menu_shell,
5435 l, l + 1, i - 1, i);
5441 else if (direction == GTK_MENU_DIR_PARENT)
5443 /* we go one left if possible */
5445 match = find_child_containing (menu_shell,
5446 l - 1, l, t, t + 1);
5450 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5453 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5454 match = menu_shell->priv->active_menu_item;
5457 else if (direction == GTK_MENU_DIR_CHILD)
5459 /* we go one right if possible */
5460 if (r < gtk_menu_get_n_columns (menu))
5461 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5465 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5467 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5469 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5470 match = menu_shell->priv->active_menu_item;
5476 gtk_menu_shell_select_item (menu_shell, match);
5481 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5485 get_visible_size (GtkMenu *menu)
5487 GtkMenuPrivate *priv = menu->priv;
5488 GtkAllocation allocation;
5489 GtkWidget *widget = GTK_WIDGET (menu);
5490 GtkContainer *container = GTK_CONTAINER (menu);
5494 gtk_widget_get_allocation (widget, &allocation);
5495 get_menu_padding (widget, &padding);
5497 menu_height = (allocation.height -
5498 (2 * gtk_container_get_border_width (container)) -
5499 padding.top - padding.bottom);
5501 if (!priv->tearoff_active)
5503 GtkBorder arrow_border;
5505 get_arrows_border (menu, &arrow_border);
5506 menu_height -= arrow_border.top;
5507 menu_height -= arrow_border.bottom;
5513 /* Find the sensitive on-screen child containing @y, or if none,
5514 * the nearest selectable onscreen child. (%NULL if none)
5517 child_at (GtkMenu *menu,
5520 GtkMenuPrivate *priv = menu->priv;
5521 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5522 GtkWidget *child = NULL;
5523 gint child_offset = 0;
5526 gint lower, upper; /* Onscreen bounds */
5528 menu_height = get_visible_size (menu);
5529 lower = priv->scroll_offset;
5530 upper = priv->scroll_offset + menu_height;
5532 for (children = menu_shell->priv->children; children; children = children->next)
5534 if (gtk_widget_get_visible (children->data))
5536 GtkRequisition child_requisition;
5538 gtk_widget_get_preferred_size (children->data,
5539 &child_requisition, NULL);
5541 if (_gtk_menu_item_is_selectable (children->data) &&
5542 child_offset >= lower &&
5543 child_offset + child_requisition.height <= upper)
5545 child = children->data;
5547 if (child_offset + child_requisition.height > y &&
5548 !GTK_IS_TEAROFF_MENU_ITEM (child))
5552 child_offset += child_requisition.height;
5560 get_menu_height (GtkMenu *menu)
5562 GtkMenuPrivate *priv = menu->priv;
5563 GtkAllocation allocation;
5564 GtkWidget *widget = GTK_WIDGET (menu);
5568 gtk_widget_get_allocation (widget, &allocation);
5569 get_menu_padding (widget, &padding);
5571 height = allocation.height;
5572 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5573 padding.top + padding.bottom;
5575 if (!priv->tearoff_active)
5577 GtkBorder arrow_border;
5579 get_arrows_border (menu, &arrow_border);
5580 height -= arrow_border.top;
5581 height -= arrow_border.bottom;
5588 gtk_menu_real_move_scroll (GtkMenu *menu,
5591 GtkMenuPrivate *priv = menu->priv;
5592 gint page_size = get_visible_size (menu);
5593 gint end_position = get_menu_height (menu);
5594 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5598 case GTK_SCROLL_PAGE_UP:
5599 case GTK_SCROLL_PAGE_DOWN:
5603 gint child_offset = 0;
5604 gboolean old_upper_arrow_visible;
5607 if (type == GTK_SCROLL_PAGE_UP)
5612 if (menu_shell->priv->active_menu_item)
5616 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5617 &child_offset, &child_height, NULL);
5618 child_offset += child_height / 2;
5621 menu_shell->priv->ignore_enter = TRUE;
5622 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5623 old_offset = priv->scroll_offset;
5625 new_offset = priv->scroll_offset + step;
5626 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5628 gtk_menu_scroll_to (menu, new_offset);
5630 if (menu_shell->priv->active_menu_item)
5632 GtkWidget *new_child;
5633 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5634 GtkBorder arrow_border;
5635 get_arrows_border (menu, &arrow_border);
5637 if (priv->scroll_offset != old_offset)
5638 step = priv->scroll_offset - old_offset;
5640 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5642 new_child = child_at (menu, child_offset + step);
5644 gtk_menu_shell_select_item (menu_shell, new_child);
5648 case GTK_SCROLL_START:
5649 /* Ignore the enter event we might get if the pointer is on the menu */
5650 menu_shell->priv->ignore_enter = TRUE;
5651 gtk_menu_scroll_to (menu, 0);
5652 gtk_menu_shell_select_first (menu_shell, TRUE);
5654 case GTK_SCROLL_END:
5655 /* Ignore the enter event we might get if the pointer is on the menu */
5656 menu_shell->priv->ignore_enter = TRUE;
5657 gtk_menu_scroll_to (menu, end_position - page_size);
5658 _gtk_menu_shell_select_last (menu_shell, TRUE);
5667 * gtk_menu_set_monitor:
5669 * @monitor_num: the number of the monitor on which the menu should
5672 * Informs GTK+ on which monitor a menu should be popped up.
5673 * See gdk_screen_get_monitor_geometry().
5675 * This function should be called from a #GtkMenuPositionFunc
5676 * if the menu should not appear on the same monitor as the pointer.
5677 * This information can't be reliably inferred from the coordinates
5678 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5679 * these coordinates may extend beyond the monitor boundaries or even
5680 * the screen boundaries.
5685 gtk_menu_set_monitor (GtkMenu *menu,
5688 GtkMenuPrivate *priv = menu->priv;
5690 g_return_if_fail (GTK_IS_MENU (menu));
5692 priv->monitor_num = monitor_num;
5696 * gtk_menu_get_monitor:
5699 * Retrieves the number of the monitor on which to show the menu.
5701 * Returns: the number of the monitor on which the menu should
5702 * be popped up or -1, if no monitor has been set
5707 gtk_menu_get_monitor (GtkMenu *menu)
5709 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5711 return menu->priv->monitor_num;
5715 * gtk_menu_get_for_attach_widget:
5716 * @widget: a #GtkWidget
5718 * Returns a list of the menus which are attached to this widget.
5719 * This list is owned by GTK+ and must not be modified.
5721 * Return value: (element-type GtkWidget) (transfer none): the list
5722 * of menus attached to his widget.
5727 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5731 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5733 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5739 gtk_menu_grab_notify (GtkWidget *widget,
5740 gboolean was_grabbed)
5742 GtkWidget *toplevel;
5743 GtkWindowGroup *group;
5747 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5750 !gtk_widget_device_is_shadowed (widget, pointer))
5753 toplevel = gtk_widget_get_toplevel (widget);
5755 if (!GTK_IS_WINDOW (toplevel))
5758 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5759 grab = gtk_window_group_get_current_device_grab (group, pointer);
5761 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5762 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5766 * gtk_menu_set_reserve_toggle_size:
5768 * @reserve_toggle_size: whether to reserve size for toggles
5770 * Sets whether the menu should reserve space for drawing toggles
5771 * or icons, regardless of their actual presence.
5776 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5777 gboolean reserve_toggle_size)
5779 GtkMenuPrivate *priv = menu->priv;
5780 gboolean no_toggle_size;
5782 g_return_if_fail (GTK_IS_MENU (menu));
5784 no_toggle_size = !reserve_toggle_size;
5786 if (priv->no_toggle_size != no_toggle_size)
5788 priv->no_toggle_size = no_toggle_size;
5790 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5795 * gtk_menu_get_reserve_toggle_size:
5798 * Returns whether the menu reserves space for toggles and
5799 * icons, regardless of their actual presence.
5801 * Returns: Whether the menu reserves toggle space
5806 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5808 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5810 return !menu->priv->no_toggle_size;