1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 * @Short_description: A menu widget
32 * A #GtkMenu is a #GtkMenuShell that implements a drop down menu
33 * consisting of a list of #GtkMenuItem objects which can be navigated
34 * and activated by the user to perform application functions.
36 * A #GtkMenu is most commonly dropped down by activating a
37 * #GtkMenuItem in a #GtkMenuBar or popped up by activating a
38 * #GtkMenuItem in another #GtkMenu.
40 * A #GtkMenu can also be popped up by activating a #GtkOptionMenu.
41 * Other composite widgets such as the #GtkNotebook can pop up a
44 * Applications can display a #GtkMenu as a popup menu by calling the
45 * gtk_menu_popup() function. The example below shows how an application
46 * can pop up a menu when the 3rd mouse button is pressed.
49 * <title>Connecting the popup signal handler.</title>
51 * /<!---->* connect our handler which will popup the menu *<!---->/
52 * g_signal_connect_swapped (window, "button_press_event",
53 * G_CALLBACK (my_popup_handler), menu);
58 * <title>Signal handler which displays a popup menu.</title>
61 * my_popup_handler (GtkWidget *widget, GdkEvent *event)
64 * GdkEventButton *event_button;
66 * g_return_val_if_fail (widget != NULL, FALSE);
67 * g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
68 * g_return_val_if_fail (event != NULL, FALSE);
70 * /<!---->* The "widget" is the menu that was supplied when
71 * * g_signal_connect_swapped() was called.
73 * menu = GTK_MENU (widget);
75 * if (event->type == GDK_BUTTON_PRESS)
77 * event_button = (GdkEventButton *) event;
78 * if (event_button->button == GDK_BUTTON_SECONDARY)
80 * gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
81 * event_button->button, event_button->time);
96 #include <gobject/gvaluecollector.h>
98 #include "gtkaccellabel.h"
99 #include "gtkaccelmap.h"
100 #include "gtkbindings.h"
101 #include "gtkcheckmenuitem.h"
103 #include "gtkmarshalers.h"
104 #include "gtkmenuprivate.h"
105 #include "gtkmenuitemprivate.h"
106 #include "gtkmenushellprivate.h"
107 #include "gtkwindow.h"
109 #include "gtkscrollbar.h"
110 #include "gtksettings.h"
111 #include "gtkprivate.h"
112 #include "gtkwidgetprivate.h"
114 #include "gtktypebuiltins.h"
116 #include "deprecated/gtktearoffmenuitem.h"
119 #include "a11y/gtkmenuaccessible.h"
121 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
122 * extends below the submenu
125 #define MENU_SCROLL_STEP1 8
126 #define MENU_SCROLL_STEP2 15
127 #define MENU_SCROLL_FAST_ZONE 8
128 #define MENU_SCROLL_TIMEOUT1 50
129 #define MENU_SCROLL_TIMEOUT2 20
131 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
132 #define ATTACHED_MENUS "gtk-attached-menus"
134 typedef struct _GtkMenuAttachData GtkMenuAttachData;
135 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
137 struct _GtkMenuAttachData
139 GtkWidget *attach_widget;
140 GtkMenuDetachFunc detacher;
143 struct _GtkMenuPopdownData
155 gint effective_left_attach;
156 gint effective_right_attach;
157 gint effective_top_attach;
158 gint effective_bottom_attach;
175 PROP_RESERVE_TOGGLE_SIZE
180 CHILD_PROP_LEFT_ATTACH,
181 CHILD_PROP_RIGHT_ATTACH,
182 CHILD_PROP_TOP_ATTACH,
183 CHILD_PROP_BOTTOM_ATTACH
186 static void gtk_menu_set_property (GObject *object,
190 static void gtk_menu_get_property (GObject *object,
194 static void gtk_menu_set_child_property(GtkContainer *container,
199 static void gtk_menu_get_child_property(GtkContainer *container,
204 static void gtk_menu_destroy (GtkWidget *widget);
205 static void gtk_menu_realize (GtkWidget *widget);
206 static void gtk_menu_unrealize (GtkWidget *widget);
207 static void gtk_menu_size_allocate (GtkWidget *widget,
208 GtkAllocation *allocation);
209 static void gtk_menu_show (GtkWidget *widget);
210 static gboolean gtk_menu_draw (GtkWidget *widget,
212 static gboolean gtk_menu_key_press (GtkWidget *widget,
214 static gboolean gtk_menu_scroll (GtkWidget *widget,
215 GdkEventScroll *event);
216 static gboolean gtk_menu_button_press (GtkWidget *widget,
217 GdkEventButton *event);
218 static gboolean gtk_menu_button_release (GtkWidget *widget,
219 GdkEventButton *event);
220 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
221 GdkEventMotion *event);
222 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
223 GdkEventCrossing *event);
224 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
225 GdkEventCrossing *event);
226 static void gtk_menu_scroll_to (GtkMenu *menu,
228 static void gtk_menu_grab_notify (GtkWidget *widget,
229 gboolean was_grabbed);
231 static void gtk_menu_stop_scrolling (GtkMenu *menu);
232 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
233 static gboolean gtk_menu_scroll_timeout (gpointer data);
234 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
235 static void gtk_menu_start_scrolling (GtkMenu *menu);
237 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
238 GtkWidget *menu_item);
239 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
240 GtkWidget *menu_item);
241 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
244 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
246 static void gtk_menu_handle_scrolling (GtkMenu *menu,
251 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
253 static void gtk_menu_style_updated (GtkWidget *widget);
254 static gboolean gtk_menu_focus (GtkWidget *widget,
255 GtkDirectionType direction);
256 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
257 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
258 GtkMenuDirectionType direction);
259 static void gtk_menu_real_move_scroll (GtkMenu *menu,
262 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
263 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
264 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
267 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
268 GtkMenuItem *menu_item,
269 GdkEventCrossing *event);
271 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
272 static void gtk_menu_show_all (GtkWidget *widget);
273 static void gtk_menu_position (GtkMenu *menu,
274 gboolean set_scroll_offset);
275 static void gtk_menu_reparent (GtkMenu *menu,
276 GtkWidget *new_parent,
278 static void gtk_menu_remove (GtkContainer *menu,
281 static void gtk_menu_update_title (GtkMenu *menu);
283 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
284 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
286 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
288 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
289 gboolean group_changed);
291 static void gtk_menu_get_preferred_width (GtkWidget *widget,
294 static void gtk_menu_get_preferred_height (GtkWidget *widget,
297 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
303 static const gchar attach_data_key[] = "gtk-menu-attach-data";
305 static guint menu_signals[LAST_SIGNAL] = { 0 };
307 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
310 menu_queue_resize (GtkMenu *menu)
312 GtkMenuPrivate *priv = menu->priv;
314 priv->have_layout = FALSE;
315 gtk_widget_queue_resize (GTK_WIDGET (menu));
319 attach_info_free (AttachInfo *info)
321 g_slice_free (AttachInfo, info);
325 get_attach_info (GtkWidget *child)
327 GObject *object = G_OBJECT (child);
328 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
332 ai = g_slice_new0 (AttachInfo);
333 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
334 (GDestroyNotify) attach_info_free);
341 is_grid_attached (AttachInfo *ai)
343 return (ai->left_attach >= 0 &&
344 ai->right_attach >= 0 &&
345 ai->top_attach >= 0 &&
346 ai->bottom_attach >= 0);
350 menu_ensure_layout (GtkMenu *menu)
352 GtkMenuPrivate *priv = menu->priv;
354 if (!priv->have_layout)
356 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
360 gint max_right_attach;
361 gint max_bottom_attach;
363 /* Find extents of gridded portion
365 max_right_attach = 1;
366 max_bottom_attach = 0;
368 for (l = menu_shell->priv->children; l; l = l->next)
370 GtkWidget *child = l->data;
371 AttachInfo *ai = get_attach_info (child);
373 if (is_grid_attached (ai))
375 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
376 max_right_attach = MAX (max_right_attach, ai->right_attach);
380 /* Find empty rows */
381 row_occupied = g_malloc0 (max_bottom_attach);
383 for (l = menu_shell->priv->children; l; l = l->next)
385 GtkWidget *child = l->data;
386 AttachInfo *ai = get_attach_info (child);
388 if (is_grid_attached (ai))
392 for (i = ai->top_attach; i < ai->bottom_attach; i++)
393 row_occupied[i] = TRUE;
397 /* Lay non-grid-items out in those rows
400 for (l = menu_shell->priv->children; l; l = l->next)
402 GtkWidget *child = l->data;
403 AttachInfo *ai = get_attach_info (child);
405 if (!is_grid_attached (ai))
407 while (current_row < max_bottom_attach && row_occupied[current_row])
410 ai->effective_left_attach = 0;
411 ai->effective_right_attach = max_right_attach;
412 ai->effective_top_attach = current_row;
413 ai->effective_bottom_attach = current_row + 1;
419 ai->effective_left_attach = ai->left_attach;
420 ai->effective_right_attach = ai->right_attach;
421 ai->effective_top_attach = ai->top_attach;
422 ai->effective_bottom_attach = ai->bottom_attach;
426 g_free (row_occupied);
428 priv->n_rows = MAX (current_row, max_bottom_attach);
429 priv->n_columns = max_right_attach;
430 priv->have_layout = TRUE;
436 gtk_menu_get_n_columns (GtkMenu *menu)
438 GtkMenuPrivate *priv = menu->priv;
440 menu_ensure_layout (menu);
442 return priv->n_columns;
446 gtk_menu_get_n_rows (GtkMenu *menu)
448 GtkMenuPrivate *priv = menu->priv;
450 menu_ensure_layout (menu);
456 get_effective_child_attach (GtkWidget *child,
462 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
465 menu_ensure_layout (menu);
467 ai = get_attach_info (child);
470 *l = ai->effective_left_attach;
472 *r = ai->effective_right_attach;
474 *t = ai->effective_top_attach;
476 *b = ai->effective_bottom_attach;
481 gtk_menu_class_init (GtkMenuClass *class)
483 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
484 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
485 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
486 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
487 GtkBindingSet *binding_set;
489 gobject_class->set_property = gtk_menu_set_property;
490 gobject_class->get_property = gtk_menu_get_property;
492 widget_class->destroy = gtk_menu_destroy;
493 widget_class->realize = gtk_menu_realize;
494 widget_class->unrealize = gtk_menu_unrealize;
495 widget_class->size_allocate = gtk_menu_size_allocate;
496 widget_class->show = gtk_menu_show;
497 widget_class->draw = gtk_menu_draw;
498 widget_class->scroll_event = gtk_menu_scroll;
499 widget_class->key_press_event = gtk_menu_key_press;
500 widget_class->button_press_event = gtk_menu_button_press;
501 widget_class->button_release_event = gtk_menu_button_release;
502 widget_class->motion_notify_event = gtk_menu_motion_notify;
503 widget_class->show_all = gtk_menu_show_all;
504 widget_class->enter_notify_event = gtk_menu_enter_notify;
505 widget_class->leave_notify_event = gtk_menu_leave_notify;
506 widget_class->style_updated = gtk_menu_style_updated;
507 widget_class->focus = gtk_menu_focus;
508 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
509 widget_class->grab_notify = gtk_menu_grab_notify;
510 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
511 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
512 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
514 container_class->remove = gtk_menu_remove;
515 container_class->get_child_property = gtk_menu_get_child_property;
516 container_class->set_child_property = gtk_menu_set_child_property;
518 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
519 menu_shell_class->deactivate = gtk_menu_deactivate;
520 menu_shell_class->select_item = gtk_menu_select_item;
521 menu_shell_class->insert = gtk_menu_real_insert;
522 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
523 menu_shell_class->move_current = gtk_menu_move_current;
526 * GtkMenu::move-scroll:
528 * @scroll_type: a #GtkScrollType
530 menu_signals[MOVE_SCROLL] =
531 g_signal_new_class_handler (I_("move-scroll"),
532 G_OBJECT_CLASS_TYPE (gobject_class),
533 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
534 G_CALLBACK (gtk_menu_real_move_scroll),
536 _gtk_marshal_VOID__ENUM,
538 GTK_TYPE_SCROLL_TYPE);
543 * The index of the currently selected menu item, or -1 if no
544 * menu item is selected.
548 g_object_class_install_property (gobject_class,
550 g_param_spec_int ("active",
552 P_("The currently selected menu item"),
554 GTK_PARAM_READWRITE));
557 * GtkMenu:accel-group:
559 * The accel group holding accelerators for the menu.
563 g_object_class_install_property (gobject_class,
565 g_param_spec_object ("accel-group",
567 P_("The accel group holding accelerators for the menu"),
568 GTK_TYPE_ACCEL_GROUP,
569 GTK_PARAM_READWRITE));
572 * GtkMenu:accel-path:
574 * An accel path used to conveniently construct accel paths of child items.
578 g_object_class_install_property (gobject_class,
580 g_param_spec_string ("accel-path",
582 P_("An accel path used to conveniently construct accel paths of child items"),
584 GTK_PARAM_READWRITE));
587 * GtkMenu:attach-widget:
589 * The widget the menu is attached to. Setting this property attaches
590 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
591 * use gtk_menu_attach_to_widget() directly.
595 g_object_class_install_property (gobject_class,
597 g_param_spec_object ("attach-widget",
599 P_("The widget the menu is attached to"),
601 GTK_PARAM_READWRITE));
603 g_object_class_install_property (gobject_class,
605 g_param_spec_string ("tearoff-title",
607 P_("A title that may be displayed by the window manager when this menu is torn-off"),
609 GTK_PARAM_READWRITE));
612 * GtkMenu:tearoff-state:
614 * A boolean that indicates whether the menu is torn-off.
618 g_object_class_install_property (gobject_class,
620 g_param_spec_boolean ("tearoff-state",
622 P_("A boolean that indicates whether the menu is torn-off"),
624 GTK_PARAM_READWRITE));
629 * The monitor the menu will be popped up on.
633 g_object_class_install_property (gobject_class,
635 g_param_spec_int ("monitor",
637 P_("The monitor the menu will be popped up on"),
639 GTK_PARAM_READWRITE));
641 gtk_widget_class_install_style_property (widget_class,
642 g_param_spec_int ("vertical-padding",
643 P_("Vertical Padding"),
644 P_("Extra space at the top and bottom of the menu"),
648 GTK_PARAM_READABLE));
651 * GtkMenu:reserve-toggle-size:
653 * A boolean that indicates whether the menu reserves space for
654 * toggles and icons, regardless of their actual presence.
656 * This property should only be changed from its default value
657 * for special-purposes such as tabular menus. Regular menus that
658 * are connected to a menu bar or context menus should reserve
659 * toggle space for consistency.
663 g_object_class_install_property (gobject_class,
664 PROP_RESERVE_TOGGLE_SIZE,
665 g_param_spec_boolean ("reserve-toggle-size",
666 P_("Reserve Toggle Size"),
667 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
669 GTK_PARAM_READWRITE));
671 gtk_widget_class_install_style_property (widget_class,
672 g_param_spec_int ("horizontal-padding",
673 P_("Horizontal Padding"),
674 P_("Extra space at the left and right edges of the menu"),
678 GTK_PARAM_READABLE));
680 gtk_widget_class_install_style_property (widget_class,
681 g_param_spec_int ("vertical-offset",
682 P_("Vertical Offset"),
683 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
687 GTK_PARAM_READABLE));
689 gtk_widget_class_install_style_property (widget_class,
690 g_param_spec_int ("horizontal-offset",
691 P_("Horizontal Offset"),
692 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
696 GTK_PARAM_READABLE));
698 gtk_widget_class_install_style_property (widget_class,
699 g_param_spec_boolean ("double-arrows",
701 P_("When scrolling, always show both arrows."),
703 GTK_PARAM_READABLE));
706 * GtkMenu:arrow-placement:
708 * Indicates where scroll arrows should be placed.
712 gtk_widget_class_install_style_property (widget_class,
713 g_param_spec_enum ("arrow-placement",
714 P_("Arrow Placement"),
715 P_("Indicates where scroll arrows should be placed"),
716 GTK_TYPE_ARROW_PLACEMENT,
718 GTK_PARAM_READABLE));
720 gtk_container_class_install_child_property (container_class,
721 CHILD_PROP_LEFT_ATTACH,
722 g_param_spec_int ("left-attach",
724 P_("The column number to attach the left side of the child to"),
726 GTK_PARAM_READWRITE));
728 gtk_container_class_install_child_property (container_class,
729 CHILD_PROP_RIGHT_ATTACH,
730 g_param_spec_int ("right-attach",
732 P_("The column number to attach the right side of the child to"),
734 GTK_PARAM_READWRITE));
736 gtk_container_class_install_child_property (container_class,
737 CHILD_PROP_TOP_ATTACH,
738 g_param_spec_int ("top-attach",
740 P_("The row number to attach the top of the child to"),
742 GTK_PARAM_READWRITE));
744 gtk_container_class_install_child_property (container_class,
745 CHILD_PROP_BOTTOM_ATTACH,
746 g_param_spec_int ("bottom-attach",
748 P_("The row number to attach the bottom of the child to"),
750 GTK_PARAM_READWRITE));
753 * GtkMenu:arrow-scaling
755 * Arbitrary constant to scale down the size of the scroll arrow.
759 gtk_widget_class_install_style_property (widget_class,
760 g_param_spec_float ("arrow-scaling",
762 P_("Arbitrary constant to scale down the size of the scroll arrow"),
764 GTK_PARAM_READABLE));
766 binding_set = gtk_binding_set_by_class (class);
767 gtk_binding_entry_add_signal (binding_set,
769 I_("move-current"), 1,
770 GTK_TYPE_MENU_DIRECTION_TYPE,
772 gtk_binding_entry_add_signal (binding_set,
775 GTK_TYPE_MENU_DIRECTION_TYPE,
777 gtk_binding_entry_add_signal (binding_set,
780 GTK_TYPE_MENU_DIRECTION_TYPE,
782 gtk_binding_entry_add_signal (binding_set,
785 GTK_TYPE_MENU_DIRECTION_TYPE,
787 gtk_binding_entry_add_signal (binding_set,
790 GTK_TYPE_MENU_DIRECTION_TYPE,
791 GTK_MENU_DIR_PARENT);
792 gtk_binding_entry_add_signal (binding_set,
795 GTK_TYPE_MENU_DIRECTION_TYPE,
796 GTK_MENU_DIR_PARENT);
797 gtk_binding_entry_add_signal (binding_set,
800 GTK_TYPE_MENU_DIRECTION_TYPE,
802 gtk_binding_entry_add_signal (binding_set,
805 GTK_TYPE_MENU_DIRECTION_TYPE,
807 gtk_binding_entry_add_signal (binding_set,
810 GTK_TYPE_SCROLL_TYPE,
812 gtk_binding_entry_add_signal (binding_set,
815 GTK_TYPE_SCROLL_TYPE,
817 gtk_binding_entry_add_signal (binding_set,
820 GTK_TYPE_SCROLL_TYPE,
822 gtk_binding_entry_add_signal (binding_set,
825 GTK_TYPE_SCROLL_TYPE,
827 gtk_binding_entry_add_signal (binding_set,
830 GTK_TYPE_SCROLL_TYPE,
832 gtk_binding_entry_add_signal (binding_set,
833 GDK_KEY_KP_Page_Up, 0,
835 GTK_TYPE_SCROLL_TYPE,
837 gtk_binding_entry_add_signal (binding_set,
838 GDK_KEY_Page_Down, 0,
840 GTK_TYPE_SCROLL_TYPE,
841 GTK_SCROLL_PAGE_DOWN);
842 gtk_binding_entry_add_signal (binding_set,
843 GDK_KEY_KP_Page_Down, 0,
845 GTK_TYPE_SCROLL_TYPE,
846 GTK_SCROLL_PAGE_DOWN);
848 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
850 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
855 gtk_menu_set_property (GObject *object,
860 GtkMenu *menu = GTK_MENU (object);
865 gtk_menu_set_active (menu, g_value_get_int (value));
867 case PROP_ACCEL_GROUP:
868 gtk_menu_set_accel_group (menu, g_value_get_object (value));
870 case PROP_ACCEL_PATH:
871 gtk_menu_set_accel_path (menu, g_value_get_string (value));
873 case PROP_ATTACH_WIDGET:
877 widget = gtk_menu_get_attach_widget (menu);
879 gtk_menu_detach (menu);
881 widget = (GtkWidget*) g_value_get_object (value);
883 gtk_menu_attach_to_widget (menu, widget, NULL);
886 case PROP_TEAROFF_STATE:
887 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
889 case PROP_TEAROFF_TITLE:
890 gtk_menu_set_title (menu, g_value_get_string (value));
893 gtk_menu_set_monitor (menu, g_value_get_int (value));
895 case PROP_RESERVE_TOGGLE_SIZE:
896 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
899 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
905 gtk_menu_get_property (GObject *object,
910 GtkMenu *menu = GTK_MENU (object);
915 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
917 case PROP_ACCEL_GROUP:
918 g_value_set_object (value, gtk_menu_get_accel_group (menu));
920 case PROP_ACCEL_PATH:
921 g_value_set_string (value, gtk_menu_get_accel_path (menu));
923 case PROP_ATTACH_WIDGET:
924 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
926 case PROP_TEAROFF_STATE:
927 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
929 case PROP_TEAROFF_TITLE:
930 g_value_set_string (value, gtk_menu_get_title (menu));
933 g_value_set_int (value, gtk_menu_get_monitor (menu));
935 case PROP_RESERVE_TOGGLE_SIZE:
936 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
939 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
945 gtk_menu_set_child_property (GtkContainer *container,
951 GtkMenu *menu = GTK_MENU (container);
952 AttachInfo *ai = get_attach_info (child);
956 case CHILD_PROP_LEFT_ATTACH:
957 ai->left_attach = g_value_get_int (value);
959 case CHILD_PROP_RIGHT_ATTACH:
960 ai->right_attach = g_value_get_int (value);
962 case CHILD_PROP_TOP_ATTACH:
963 ai->top_attach = g_value_get_int (value);
965 case CHILD_PROP_BOTTOM_ATTACH:
966 ai->bottom_attach = g_value_get_int (value);
970 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
974 menu_queue_resize (menu);
978 gtk_menu_get_child_property (GtkContainer *container,
984 AttachInfo *ai = get_attach_info (child);
988 case CHILD_PROP_LEFT_ATTACH:
989 g_value_set_int (value, ai->left_attach);
991 case CHILD_PROP_RIGHT_ATTACH:
992 g_value_set_int (value, ai->right_attach);
994 case CHILD_PROP_TOP_ATTACH:
995 g_value_set_int (value, ai->top_attach);
997 case CHILD_PROP_BOTTOM_ATTACH:
998 g_value_set_int (value, ai->bottom_attach);
1002 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1008 gtk_menu_window_event (GtkWidget *window,
1012 gboolean handled = FALSE;
1014 g_object_ref (window);
1015 g_object_ref (menu);
1017 switch (event->type)
1020 case GDK_KEY_RELEASE:
1021 handled = gtk_widget_event (menu, event);
1027 g_object_unref (window);
1028 g_object_unref (menu);
1034 gtk_menu_init (GtkMenu *menu)
1036 GtkMenuPrivate *priv;
1037 GtkStyleContext *context;
1039 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1043 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1044 "type", GTK_WINDOW_POPUP,
1047 "signal::event", gtk_menu_window_event, menu,
1048 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1050 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1051 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1053 /* Refloat the menu, so that reference counting for the menu isn't
1054 * affected by it being a child of the toplevel
1056 g_object_force_floating (G_OBJECT (menu));
1057 priv->needs_destruction_ref = TRUE;
1059 priv->monitor_num = -1;
1061 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1062 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1066 gtk_menu_destroy (GtkWidget *widget)
1068 GtkMenu *menu = GTK_MENU (widget);
1069 GtkMenuPrivate *priv = menu->priv;
1070 GtkMenuAttachData *data;
1072 gtk_menu_remove_scroll_timeout (menu);
1074 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1076 gtk_menu_detach (menu);
1078 gtk_menu_stop_navigating_submenu (menu);
1080 if (priv->old_active_menu_item)
1081 g_clear_object (&priv->old_active_menu_item);
1083 /* Add back the reference count for being a child */
1084 if (priv->needs_destruction_ref)
1086 priv->needs_destruction_ref = FALSE;
1087 g_object_ref (widget);
1090 if (priv->accel_group)
1091 g_clear_object (&priv->accel_group);
1094 gtk_widget_destroy (priv->toplevel);
1096 if (priv->tearoff_window)
1097 gtk_widget_destroy (priv->tearoff_window);
1101 g_free (priv->heights);
1102 priv->heights = NULL;
1107 g_free (priv->title);
1111 if (priv->position_func_data_destroy)
1113 priv->position_func_data_destroy (priv->position_func_data);
1114 priv->position_func_data = NULL;
1115 priv->position_func_data_destroy = NULL;
1118 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1122 menu_change_screen (GtkMenu *menu,
1123 GdkScreen *new_screen)
1125 GtkMenuPrivate *priv = menu->priv;
1127 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1129 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1135 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1136 gtk_menu_position (menu, TRUE);
1139 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1140 priv->monitor_num = -1;
1144 attach_widget_screen_changed (GtkWidget *attach_widget,
1145 GdkScreen *previous_screen,
1148 if (gtk_widget_has_screen (attach_widget) &&
1149 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1150 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1154 * gtk_menu_attach_to_widget:
1156 * @attach_widget: the #GtkWidget that the menu will be attached to
1157 * @detacher: (scope async)(allow-none): the user supplied callback function
1158 * that will be called when the menu calls gtk_menu_detach()
1160 * Attaches the menu to the widget and provides a callback function
1161 * that will be invoked when the menu calls gtk_menu_detach() during
1165 gtk_menu_attach_to_widget (GtkMenu *menu,
1166 GtkWidget *attach_widget,
1167 GtkMenuDetachFunc detacher)
1169 GtkMenuAttachData *data;
1172 g_return_if_fail (GTK_IS_MENU (menu));
1173 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1175 /* keep this function in sync with gtk_widget_set_parent() */
1176 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1179 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1180 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1184 g_object_ref_sink (menu);
1186 data = g_slice_new (GtkMenuAttachData);
1187 data->attach_widget = attach_widget;
1189 g_signal_connect (attach_widget, "screen-changed",
1190 G_CALLBACK (attach_widget_screen_changed), menu);
1191 attach_widget_screen_changed (attach_widget, NULL, menu);
1193 data->detacher = detacher;
1194 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1195 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1196 if (!g_list_find (list, menu))
1197 list = g_list_prepend (list, menu);
1199 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1200 (GDestroyNotify) g_list_free);
1202 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1203 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1205 /* Attach the widget to the toplevel window. */
1206 gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), attach_widget);
1208 /* Fallback title for menu comes from attach widget */
1209 gtk_menu_update_title (menu);
1211 g_object_notify (G_OBJECT (menu), "attach-widget");
1215 * gtk_menu_get_attach_widget:
1218 * Returns the #GtkWidget that the menu is attached to.
1220 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1223 gtk_menu_get_attach_widget (GtkMenu *menu)
1225 GtkMenuAttachData *data;
1227 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1229 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1231 return data->attach_widget;
1239 * Detaches the menu from the widget to which it had been attached.
1240 * This function will call the callback function, @detacher, provided
1241 * when the gtk_menu_attach_to_widget() function was called.
1244 gtk_menu_detach (GtkMenu *menu)
1246 GtkMenuAttachData *data;
1249 g_return_if_fail (GTK_IS_MENU (menu));
1251 /* keep this function in sync with gtk_widget_unparent() */
1252 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1255 g_warning ("gtk_menu_detach(): menu is not attached");
1258 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1260 /* Detach the toplevel window. */
1261 gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), NULL);
1263 g_signal_handlers_disconnect_by_func (data->attach_widget,
1264 (gpointer) attach_widget_screen_changed,
1268 data->detacher (data->attach_widget, menu);
1269 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1270 list = g_list_remove (list, menu);
1272 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1273 (GDestroyNotify) g_list_free);
1275 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1277 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1278 gtk_widget_unrealize (GTK_WIDGET (menu));
1280 g_slice_free (GtkMenuAttachData, data);
1282 /* Fallback title for menu comes from attach widget */
1283 gtk_menu_update_title (menu);
1285 g_object_unref (menu);
1289 gtk_menu_remove (GtkContainer *container,
1292 GtkMenu *menu = GTK_MENU (container);
1293 GtkMenuPrivate *priv = menu->priv;
1295 /* Clear out old_active_menu_item if it matches the item we are removing */
1296 if (priv->old_active_menu_item == widget)
1297 g_clear_object (&priv->old_active_menu_item);
1299 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1301 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1303 menu_queue_resize (menu);
1309 * Creates a new #GtkMenu
1311 * Returns: a new #GtkMenu
1316 return g_object_new (GTK_TYPE_MENU, NULL);
1320 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1324 GtkMenu *menu = GTK_MENU (menu_shell);
1325 GtkMenuPrivate *priv = menu->priv;
1326 AttachInfo *ai = get_attach_info (child);
1328 ai->left_attach = -1;
1329 ai->right_attach = -1;
1330 ai->top_attach = -1;
1331 ai->bottom_attach = -1;
1333 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1334 gtk_widget_set_parent_window (child, priv->bin_window);
1336 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1338 menu_queue_resize (menu);
1342 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1344 GtkMenuPrivate *priv = menu->priv;
1350 cairo_surface_t *surface;
1351 cairo_pattern_t *pattern;
1354 priv->tearoff_active = FALSE;
1355 priv->saved_scroll_offset = priv->scroll_offset;
1357 window = gtk_widget_get_window (priv->tearoff_window);
1358 width = gdk_window_get_width (window);
1359 height = gdk_window_get_height (window);
1361 surface = gdk_window_create_similar_surface (window,
1362 CAIRO_CONTENT_COLOR,
1366 cr = cairo_create (surface);
1367 gdk_cairo_set_source_window (cr, window, 0, 0);
1371 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1373 pattern = cairo_pattern_create_for_surface (surface);
1374 gdk_window_set_background_pattern (window, pattern);
1376 cairo_pattern_destroy (pattern);
1377 cairo_surface_destroy (surface);
1382 popup_grab_on_window (GdkWindow *window,
1383 GdkDevice *keyboard,
1385 guint32 activate_time)
1388 gdk_device_grab (keyboard, window,
1389 GDK_OWNERSHIP_WINDOW, TRUE,
1390 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1391 NULL, activate_time) != GDK_GRAB_SUCCESS)
1395 gdk_device_grab (pointer, window,
1396 GDK_OWNERSHIP_WINDOW, TRUE,
1397 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1398 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1399 GDK_POINTER_MOTION_MASK,
1400 NULL, activate_time) != GDK_GRAB_SUCCESS)
1403 gdk_device_ungrab (keyboard, activate_time);
1412 * gtk_menu_popup_for_device:
1414 * @device: (allow-none): a #GdkDevice
1415 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1416 * menu item, or %NULL
1417 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1418 * the popup, or %NULL
1419 * @func: (allow-none): a user supplied function used to position the menu,
1421 * @data: (allow-none): user supplied data to be passed to @func
1422 * @destroy: (allow-none): destroy notify for @data
1423 * @button: the mouse button which was pressed to initiate the event
1424 * @activate_time: the time at which the activation event occurred
1426 * Displays a menu and makes it available for selection.
1428 * Applications can use this function to display context-sensitive menus,
1429 * and will typically supply %NULL for the @parent_menu_shell,
1430 * @parent_menu_item, @func, @data and @destroy parameters. The default
1431 * menu positioning function will position the menu at the current position
1432 * of @device (or its corresponding pointer).
1434 * The @button parameter should be the mouse button pressed to initiate
1435 * the menu popup. If the menu popup was initiated by something other than
1436 * a mouse button press, such as a mouse button release or a keypress,
1437 * @button should be 0.
1439 * The @activate_time parameter is used to conflict-resolve initiation of
1440 * concurrent requests for mouse/keyboard grab requests. To function
1441 * properly, this needs to be the time stamp of the user event (such as
1442 * a mouse click or key press) that caused the initiation of the popup.
1443 * Only if no such event is available, gtk_get_current_event_time() can
1447 * Rename to: gtk_menu_popup
1450 gtk_menu_popup_for_device (GtkMenu *menu,
1452 GtkWidget *parent_menu_shell,
1453 GtkWidget *parent_menu_item,
1454 GtkMenuPositionFunc func,
1456 GDestroyNotify destroy,
1458 guint32 activate_time)
1460 GtkMenuPrivate *priv = menu->priv;
1462 GtkWidget *xgrab_shell;
1464 GdkEvent *current_event;
1465 GtkMenuShell *menu_shell;
1466 gboolean grab_keyboard;
1467 GtkWidget *parent_toplevel;
1468 GdkDevice *keyboard, *pointer;
1470 g_return_if_fail (GTK_IS_MENU (menu));
1471 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1474 device = gtk_get_current_event_device ();
1478 GdkDisplay *display;
1479 GdkDeviceManager *device_manager;
1482 display = gtk_widget_get_display (GTK_WIDGET (menu));
1483 device_manager = gdk_display_get_device_manager (display);
1484 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1486 device = devices->data;
1488 g_list_free (devices);
1491 widget = GTK_WIDGET (menu);
1492 menu_shell = GTK_MENU_SHELL (menu);
1494 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1497 pointer = gdk_device_get_associated_device (device);
1502 keyboard = gdk_device_get_associated_device (device);
1505 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1507 priv->seen_item_enter = FALSE;
1509 /* Find the last viewable ancestor, and make an X grab on it
1511 parent = GTK_WIDGET (menu);
1515 gboolean viewable = TRUE;
1516 GtkWidget *tmp = parent;
1520 if (!gtk_widget_get_mapped (tmp))
1525 tmp = gtk_widget_get_parent (tmp);
1529 xgrab_shell = parent;
1531 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1534 /* We want to receive events generated when we map the menu;
1535 * unfortunately, since there is probably already an implicit
1536 * grab in place from the button that the user used to pop up
1537 * the menu, we won't receive then -- in particular, the EnterNotify
1538 * when the menu pops up under the pointer.
1540 * If we are grabbing on a parent menu shell, no problem; just grab
1541 * on that menu shell first before popping up the window with
1542 * owner_events = TRUE.
1544 * When grabbing on the menu itself, things get more convoluted --
1545 * we do an explicit grab on a specially created window with
1546 * owner_events = TRUE, which we override further down with a
1547 * grab on the menu. (We can't grab on the menu until it is mapped;
1548 * we probably could just leave the grab on the other window,
1549 * with a little reorganization of the code in gtkmenu*).
1551 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1552 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1557 if (xgrab_shell && xgrab_shell != widget)
1559 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1561 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1562 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1567 GdkWindow *transfer_window;
1569 xgrab_shell = widget;
1570 transfer_window = menu_grab_transfer_window_get (menu);
1571 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1573 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1574 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1578 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1580 /* We failed to make our pointer/keyboard grab.
1581 * Rather than leaving the user with a stuck up window,
1582 * we just abort here. Presumably the user will try again.
1584 menu_shell->priv->parent_menu_shell = NULL;
1585 menu_grab_transfer_window_destroy (menu);
1589 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1590 menu_shell->priv->active = TRUE;
1591 menu_shell->priv->button = button;
1593 /* If we are popping up the menu from something other than, a button
1594 * press then, as a heuristic, we ignore enter events for the menu
1595 * until we get a MOTION_NOTIFY.
1598 current_event = gtk_get_current_event ();
1601 if ((current_event->type != GDK_BUTTON_PRESS) &&
1602 (current_event->type != GDK_ENTER_NOTIFY))
1603 menu_shell->priv->ignore_enter = TRUE;
1605 gdk_event_free (current_event);
1608 menu_shell->priv->ignore_enter = TRUE;
1612 gtk_menu_tearoff_bg_copy (menu);
1614 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1617 parent_toplevel = NULL;
1618 if (parent_menu_shell)
1619 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1620 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1622 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1624 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1627 /* Set transient for to get the right window group and parent */
1628 if (GTK_IS_WINDOW (parent_toplevel))
1629 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1630 GTK_WINDOW (parent_toplevel));
1632 priv->parent_menu_item = parent_menu_item;
1633 priv->position_func = func;
1634 priv->position_func_data = data;
1635 priv->position_func_data_destroy = destroy;
1636 menu_shell->priv->activate_time = activate_time;
1638 /* We need to show the menu here rather in the init function
1639 * because code expects to be able to tell if the menu is onscreen
1640 * by looking at gtk_widget_get_visible (menu)
1642 gtk_widget_show (GTK_WIDGET (menu));
1644 /* Position the menu, possibly changing the size request
1646 gtk_menu_position (menu, TRUE);
1648 /* Compute the size of the toplevel and realize it so we
1649 * can scroll correctly.
1651 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1653 GtkRequisition tmp_request;
1654 GtkAllocation tmp_allocation = { 0, };
1656 /* Instead of trusting the menu position function to queue a
1657 * resize when the menu goes out of bounds, invalidate the cached
1660 gtk_widget_queue_resize (GTK_WIDGET (menu));
1661 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1663 tmp_allocation.width = tmp_request.width;
1664 tmp_allocation.height = tmp_request.height;
1666 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1668 gtk_widget_realize (priv->toplevel);
1671 gtk_menu_scroll_to (menu, priv->scroll_offset);
1673 /* if no item is selected, select the first one */
1674 if (!menu_shell->priv->active_menu_item)
1676 gboolean touchscreen_mode;
1678 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1679 "gtk-touchscreen-mode", &touchscreen_mode,
1682 if (touchscreen_mode)
1683 gtk_menu_shell_select_first (menu_shell, TRUE);
1686 /* Once everything is set up correctly, map the toplevel */
1687 gtk_widget_show (priv->toplevel);
1689 if (xgrab_shell == widget)
1690 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1692 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1694 if (parent_menu_shell)
1696 gboolean keyboard_mode;
1698 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1699 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1701 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1702 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1704 _gtk_menu_shell_update_mnemonics (menu_shell);
1708 * gtk_menu_popup: (skip)
1710 * @parent_menu_shell: (allow-none): the menu shell containing the
1711 * triggering menu item, or %NULL
1712 * @parent_menu_item: (allow-none): the menu item whose activation
1713 * triggered the popup, or %NULL
1714 * @func: (allow-none): a user supplied function used to position
1715 * the menu, or %NULL
1716 * @data: user supplied data to be passed to @func.
1717 * @button: the mouse button which was pressed to initiate the event.
1718 * @activate_time: the time at which the activation event occurred.
1720 * Displays a menu and makes it available for selection.
1722 * Applications can use this function to display context-sensitive
1723 * menus, and will typically supply %NULL for the @parent_menu_shell,
1724 * @parent_menu_item, @func and @data parameters. The default menu
1725 * positioning function will position the menu at the current mouse
1728 * The @button parameter should be the mouse button pressed to initiate
1729 * the menu popup. If the menu popup was initiated by something other
1730 * than a mouse button press, such as a mouse button release or a keypress,
1731 * @button should be 0.
1733 * The @activate_time parameter is used to conflict-resolve initiation
1734 * of concurrent requests for mouse/keyboard grab requests. To function
1735 * properly, this needs to be the timestamp of the user event (such as
1736 * a mouse click or key press) that caused the initiation of the popup.
1737 * Only if no such event is available, gtk_get_current_event_time() can
1741 gtk_menu_popup (GtkMenu *menu,
1742 GtkWidget *parent_menu_shell,
1743 GtkWidget *parent_menu_item,
1744 GtkMenuPositionFunc func,
1747 guint32 activate_time)
1749 g_return_if_fail (GTK_IS_MENU (menu));
1751 gtk_menu_popup_for_device (menu,
1756 button, activate_time);
1763 * Removes the menu from the screen.
1766 gtk_menu_popdown (GtkMenu *menu)
1768 GtkMenuPrivate *priv;
1769 GtkMenuShell *menu_shell;
1772 g_return_if_fail (GTK_IS_MENU (menu));
1774 menu_shell = GTK_MENU_SHELL (menu);
1777 menu_shell->priv->parent_menu_shell = NULL;
1778 menu_shell->priv->active = FALSE;
1779 menu_shell->priv->ignore_enter = FALSE;
1781 priv->have_position = FALSE;
1783 gtk_menu_stop_scrolling (menu);
1784 gtk_menu_stop_navigating_submenu (menu);
1786 if (menu_shell->priv->active_menu_item)
1788 if (priv->old_active_menu_item)
1789 g_object_unref (priv->old_active_menu_item);
1790 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1791 g_object_ref (priv->old_active_menu_item);
1794 gtk_menu_shell_deselect (menu_shell);
1796 /* The X Grab, if present, will automatically be removed
1797 * when we hide the window
1801 gtk_widget_hide (priv->toplevel);
1802 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1805 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1809 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1811 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1813 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1817 /* We popped up the menu from the tearoff, so we need to
1818 * release the grab - we aren't actually hiding the menu.
1820 if (menu_shell->priv->have_xgrab && pointer)
1822 GdkDevice *keyboard;
1824 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1825 keyboard = gdk_device_get_associated_device (pointer);
1828 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1832 /* gtk_menu_popdown is called each time a menu item is selected from
1833 * a torn off menu. Only scroll back to the saved position if the
1834 * non-tearoff menu was popped down.
1836 if (!priv->tearoff_active)
1837 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1838 priv->tearoff_active = TRUE;
1841 gtk_widget_hide (GTK_WIDGET (menu));
1843 menu_shell->priv->have_xgrab = FALSE;
1846 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1848 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1850 menu_grab_transfer_window_destroy (menu);
1854 * gtk_menu_get_active:
1857 * Returns the selected menu item from the menu. This is used by the
1860 * Returns: (transfer none): the #GtkMenuItem that was last selected
1861 * in the menu. If a selection has not yet been made, the
1862 * first menu item is selected.
1865 gtk_menu_get_active (GtkMenu *menu)
1867 GtkMenuPrivate *priv = menu->priv;
1871 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1873 if (!priv->old_active_menu_item)
1876 children = GTK_MENU_SHELL (menu)->priv->children;
1880 child = children->data;
1881 children = children->next;
1883 if (gtk_bin_get_child (GTK_BIN (child)))
1888 priv->old_active_menu_item = child;
1889 if (priv->old_active_menu_item)
1890 g_object_ref (priv->old_active_menu_item);
1893 return priv->old_active_menu_item;
1897 * gtk_menu_set_active:
1899 * @index: the index of the menu item to select. Iindex values are
1902 * Selects the specified menu item within the menu. This is used by
1903 * the #GtkOptionMenu and should not be used by anyone else.
1906 gtk_menu_set_active (GtkMenu *menu,
1909 GtkMenuPrivate *priv = menu->priv;
1913 g_return_if_fail (GTK_IS_MENU (menu));
1915 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1918 child = tmp_list->data;
1919 if (gtk_bin_get_child (GTK_BIN (child)))
1921 if (priv->old_active_menu_item)
1922 g_object_unref (priv->old_active_menu_item);
1923 priv->old_active_menu_item = child;
1924 g_object_ref (priv->old_active_menu_item);
1930 * gtk_menu_set_accel_group:
1932 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1935 * Set the #GtkAccelGroup which holds global accelerators for the
1936 * menu. This accelerator group needs to also be added to all windows
1937 * that this menu is being used in with gtk_window_add_accel_group(),
1938 * in order for those windows to support all the accelerators
1939 * contained in this group.
1942 gtk_menu_set_accel_group (GtkMenu *menu,
1943 GtkAccelGroup *accel_group)
1945 GtkMenuPrivate *priv = menu->priv;
1946 g_return_if_fail (GTK_IS_MENU (menu));
1948 if (priv->accel_group != accel_group)
1950 if (priv->accel_group)
1951 g_object_unref (priv->accel_group);
1952 priv->accel_group = accel_group;
1953 if (priv->accel_group)
1954 g_object_ref (priv->accel_group);
1955 _gtk_menu_refresh_accel_paths (menu, TRUE);
1960 * gtk_menu_get_accel_group:
1963 * Gets the #GtkAccelGroup which holds global accelerators for the
1964 * menu. See gtk_menu_set_accel_group().
1966 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
1969 gtk_menu_get_accel_group (GtkMenu *menu)
1971 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1973 return menu->priv->accel_group;
1977 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1980 /* Menu items chain here to figure whether they can activate their
1981 * accelerators. Unlike ordinary widgets, menus allow accel
1982 * activation even if invisible since that's the usual case for
1983 * submenus/popup-menus. however, the state of the attach widget
1984 * affects the "activeness" of the menu.
1986 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1989 return gtk_widget_can_activate_accel (awidget, signal_id);
1991 return gtk_widget_is_sensitive (widget);
1995 * gtk_menu_set_accel_path
1996 * @menu: a valid #GtkMenu
1997 * @accel_path: (allow-none): a valid accelerator path
1999 * Sets an accelerator path for this menu from which accelerator paths
2000 * for its immediate children, its menu items, can be constructed.
2001 * The main purpose of this function is to spare the programmer the
2002 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2003 * each menu item that should support runtime user changable accelerators.
2004 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2005 * each menu item of this menu, that contains a label describing its
2006 * purpose, automatically gets an accel path assigned.
2008 * For example, a menu containing menu items "New" and "Exit", will, after
2009 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2010 * has been called, assign its items the accel paths:
2011 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2013 * Assigning accel paths to menu items then enables the user to change
2014 * their accelerators at runtime. More details about accelerator paths
2015 * and their default setups can be found at gtk_accel_map_add_entry().
2017 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2018 * if you pass a static string, you can save some memory by interning
2019 * it first with g_intern_static_string().
2022 gtk_menu_set_accel_path (GtkMenu *menu,
2023 const gchar *accel_path)
2025 GtkMenuPrivate *priv = menu->priv;
2026 g_return_if_fail (GTK_IS_MENU (menu));
2029 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2031 /* FIXME: accel_path should be defined as const gchar* */
2032 priv->accel_path = (gchar*)g_intern_string (accel_path);
2033 if (priv->accel_path)
2034 _gtk_menu_refresh_accel_paths (menu, FALSE);
2038 * gtk_menu_get_accel_path
2039 * @menu: a valid #GtkMenu
2041 * Retrieves the accelerator path set on the menu.
2043 * Returns: the accelerator path set on the menu.
2048 gtk_menu_get_accel_path (GtkMenu *menu)
2050 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2052 return menu->priv->accel_path;
2057 gboolean group_changed;
2061 refresh_accel_paths_foreach (GtkWidget *widget,
2064 GtkMenuPrivate *priv;
2065 AccelPropagation *prop = data;
2067 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2069 priv = prop->menu->priv;
2070 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2073 prop->group_changed);
2078 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2079 gboolean group_changed)
2081 GtkMenuPrivate *priv = menu->priv;
2082 g_return_if_fail (GTK_IS_MENU (menu));
2084 if (priv->accel_path && priv->accel_group)
2086 AccelPropagation prop;
2089 prop.group_changed = group_changed;
2090 gtk_container_foreach (GTK_CONTAINER (menu),
2091 refresh_accel_paths_foreach,
2097 * gtk_menu_reposition:
2100 * Repositions the menu according to its position function.
2103 gtk_menu_reposition (GtkMenu *menu)
2105 g_return_if_fail (GTK_IS_MENU (menu));
2107 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2108 gtk_menu_position (menu, FALSE);
2112 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2117 value = gtk_adjustment_get_value (adjustment);
2118 if (menu->priv->scroll_offset != value)
2119 gtk_menu_scroll_to (menu, value);
2123 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2126 GtkMenuPrivate *priv = menu->priv;
2127 GdkGeometry geometry_hints;
2129 if (!priv->tearoff_window)
2132 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2134 GtkRequisition requisition;
2136 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2137 &requisition, NULL);
2138 width += requisition.width;
2141 geometry_hints.min_width = width;
2142 geometry_hints.max_width = width;
2144 geometry_hints.min_height = 0;
2145 geometry_hints.max_height = priv->requested_height;
2147 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2150 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2154 gtk_menu_update_title (GtkMenu *menu)
2156 GtkMenuPrivate *priv = menu->priv;
2158 if (priv->tearoff_window)
2161 GtkWidget *attach_widget;
2163 title = gtk_menu_get_title (menu);
2166 attach_widget = gtk_menu_get_attach_widget (menu);
2167 if (GTK_IS_MENU_ITEM (attach_widget))
2169 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2170 if (GTK_IS_LABEL (child))
2171 title = gtk_label_get_text (GTK_LABEL (child));
2176 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2181 gtk_menu_get_toplevel (GtkWidget *menu)
2183 GtkWidget *attach, *toplevel;
2185 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2187 if (GTK_IS_MENU_ITEM (attach))
2188 attach = gtk_widget_get_parent (attach);
2190 if (GTK_IS_MENU (attach))
2191 return gtk_menu_get_toplevel (attach);
2192 else if (GTK_IS_WIDGET (attach))
2194 toplevel = gtk_widget_get_toplevel (attach);
2195 if (gtk_widget_is_toplevel (toplevel))
2203 tearoff_window_destroyed (GtkWidget *widget,
2206 gtk_menu_set_tearoff_state (menu, FALSE);
2210 * gtk_menu_set_tearoff_state:
2212 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2214 * Changes the tearoff state of the menu. A menu is normally
2215 * displayed as drop down menu which persists as long as the menu is
2216 * active. It can also be displayed as a tearoff menu which persists
2217 * until it is closed or reattached.
2220 gtk_menu_set_tearoff_state (GtkMenu *menu,
2223 GtkMenuPrivate *priv = menu->priv;
2226 g_return_if_fail (GTK_IS_MENU (menu));
2228 if (priv->torn_off != torn_off)
2230 priv->torn_off = torn_off;
2231 priv->tearoff_active = torn_off;
2235 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2236 gtk_menu_popdown (menu);
2238 if (!priv->tearoff_window)
2240 GtkWidget *toplevel;
2242 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2243 "type", GTK_WINDOW_TOPLEVEL,
2244 "screen", gtk_widget_get_screen (priv->toplevel),
2245 "app-paintable", TRUE,
2248 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2249 GDK_WINDOW_TYPE_HINT_MENU);
2250 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2251 g_signal_connect (priv->tearoff_window, "destroy",
2252 G_CALLBACK (tearoff_window_destroyed), menu);
2253 g_signal_connect (priv->tearoff_window, "event",
2254 G_CALLBACK (gtk_menu_window_event), menu);
2256 gtk_menu_update_title (menu);
2258 gtk_widget_realize (priv->tearoff_window);
2260 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2261 if (toplevel != NULL)
2262 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2263 GTK_WINDOW (toplevel));
2265 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2266 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2267 priv->tearoff_hbox);
2269 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2270 priv->tearoff_adjustment = gtk_adjustment_new (0,
2271 0, priv->requested_height,
2275 g_object_connect (priv->tearoff_adjustment,
2276 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2278 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2280 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2281 priv->tearoff_scrollbar,
2284 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2285 gtk_widget_show (priv->tearoff_scrollbar);
2287 gtk_widget_show (priv->tearoff_hbox);
2290 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2292 /* Update menu->requisition */
2293 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2295 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2297 gtk_widget_realize (priv->tearoff_window);
2298 gtk_menu_position (menu, TRUE);
2300 gtk_widget_show (GTK_WIDGET (menu));
2301 gtk_widget_show (priv->tearoff_window);
2303 gtk_menu_scroll_to (menu, 0);
2308 gtk_widget_hide (GTK_WIDGET (menu));
2309 gtk_widget_hide (priv->tearoff_window);
2310 if (GTK_IS_CONTAINER (priv->toplevel))
2311 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2312 gtk_widget_destroy (priv->tearoff_window);
2314 priv->tearoff_window = NULL;
2315 priv->tearoff_hbox = NULL;
2316 priv->tearoff_scrollbar = NULL;
2317 priv->tearoff_adjustment = NULL;
2320 g_object_notify (G_OBJECT (menu), "tearoff-state");
2325 * gtk_menu_get_tearoff_state:
2328 * Returns whether the menu is torn off.
2329 * See gtk_menu_set_tearoff_state().
2331 * Return value: %TRUE if the menu is currently torn off.
2334 gtk_menu_get_tearoff_state (GtkMenu *menu)
2336 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2338 return menu->priv->torn_off;
2342 * gtk_menu_set_title:
2344 * @title: a string containing the title for the menu
2346 * Sets the title string for the menu.
2348 * The title is displayed when the menu is shown as a tearoff
2349 * menu. If @title is %NULL, the menu will see if it is attached
2350 * to a parent menu item, and if so it will try to use the same
2351 * text as that menu item's label.
2354 gtk_menu_set_title (GtkMenu *menu,
2357 GtkMenuPrivate *priv = menu->priv;
2360 g_return_if_fail (GTK_IS_MENU (menu));
2362 old_title = priv->title;
2363 priv->title = g_strdup (title);
2366 gtk_menu_update_title (menu);
2367 g_object_notify (G_OBJECT (menu), "tearoff-title");
2371 * gtk_menu_get_title:
2374 * Returns the title of the menu. See gtk_menu_set_title().
2376 * Return value: the title of the menu, or %NULL if the menu
2377 * has no title set on it. This string is owned by GTK+
2378 * and should not be modified or freed.
2381 gtk_menu_get_title (GtkMenu *menu)
2383 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2385 return menu->priv->title;
2389 * gtk_menu_reorder_child:
2391 * @child: the #GtkMenuItem to move
2392 * @position: the new position to place @child.
2393 * Positions are numbered from 0 to n - 1
2395 * Moves @child to a new @position in the list of @menu
2399 gtk_menu_reorder_child (GtkMenu *menu,
2403 GtkMenuShell *menu_shell;
2405 g_return_if_fail (GTK_IS_MENU (menu));
2406 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2408 menu_shell = GTK_MENU_SHELL (menu);
2410 if (g_list_find (menu_shell->priv->children, child))
2412 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2413 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2415 menu_queue_resize (menu);
2420 gtk_menu_style_updated (GtkWidget *widget)
2422 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2424 if (gtk_widget_get_realized (widget))
2426 GtkMenu *menu = GTK_MENU (widget);
2427 GtkMenuPrivate *priv = menu->priv;
2428 GtkStyleContext *context;
2430 context = gtk_widget_get_style_context (widget);
2432 gtk_style_context_set_background (context, priv->bin_window);
2433 gtk_style_context_set_background (context, priv->view_window);
2434 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2439 get_arrows_border (GtkMenu *menu,
2442 GtkMenuPrivate *priv = menu->priv;
2443 guint scroll_arrow_height;
2444 GtkArrowPlacement arrow_placement;
2446 gtk_widget_style_get (GTK_WIDGET (menu),
2447 "scroll-arrow-vlength", &scroll_arrow_height,
2448 "arrow_placement", &arrow_placement,
2451 switch (arrow_placement)
2453 case GTK_ARROWS_BOTH:
2454 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2455 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2458 case GTK_ARROWS_START:
2459 border->top = (priv->upper_arrow_visible ||
2460 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2464 case GTK_ARROWS_END:
2466 border->bottom = (priv->upper_arrow_visible ||
2467 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2471 border->left = border->right = 0;
2475 get_menu_padding (GtkWidget *widget,
2478 GtkStyleContext *context;
2479 GtkStateFlags state;
2481 context = gtk_widget_get_style_context (widget);
2482 state = gtk_widget_get_state_flags (widget);
2484 gtk_style_context_get_padding (context, state, padding);
2488 gtk_menu_realize (GtkWidget *widget)
2490 GtkMenu *menu = GTK_MENU (widget);
2491 GtkMenuPrivate *priv = menu->priv;
2492 GtkAllocation allocation;
2493 GtkStyleContext *context;
2495 GdkWindowAttr attributes;
2496 gint attributes_mask;
2500 guint vertical_padding;
2501 guint horizontal_padding;
2502 GtkBorder arrow_border, padding;
2504 g_return_if_fail (GTK_IS_MENU (widget));
2506 gtk_widget_set_realized (widget, TRUE);
2508 gtk_widget_get_allocation (widget, &allocation);
2510 attributes.window_type = GDK_WINDOW_CHILD;
2511 attributes.x = allocation.x;
2512 attributes.y = allocation.y;
2513 attributes.width = allocation.width;
2514 attributes.height = allocation.height;
2515 attributes.wclass = GDK_INPUT_OUTPUT;
2516 attributes.visual = gtk_widget_get_visual (widget);
2517 attributes.event_mask = gtk_widget_get_events (widget);
2518 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2519 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2521 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2523 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2524 &attributes, attributes_mask);
2525 gtk_widget_set_window (widget, window);
2526 gdk_window_set_user_data (window, widget);
2528 get_menu_padding (widget, &padding);
2529 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2530 context = gtk_widget_get_style_context (widget);
2532 gtk_widget_style_get (GTK_WIDGET (menu),
2533 "vertical-padding", &vertical_padding,
2534 "horizontal-padding", &horizontal_padding,
2537 gtk_widget_get_allocation (widget, &allocation);
2539 attributes.x = border_width + padding.left + horizontal_padding;
2540 attributes.y = border_width + padding.top + vertical_padding;
2541 attributes.width = allocation.width -
2542 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2543 attributes.height = allocation.height -
2544 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2546 get_arrows_border (menu, &arrow_border);
2547 attributes.y += arrow_border.top;
2548 attributes.height -= arrow_border.top;
2549 attributes.height -= arrow_border.bottom;
2551 attributes.width = MAX (1, attributes.width);
2552 attributes.height = MAX (1, attributes.height);
2554 priv->view_window = gdk_window_new (window,
2555 &attributes, attributes_mask);
2556 gdk_window_set_user_data (priv->view_window, menu);
2558 gtk_widget_get_allocation (widget, &allocation);
2562 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2563 padding.left + padding.right;
2564 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2565 padding.top + padding.bottom;
2567 attributes.width = MAX (1, attributes.width);
2568 attributes.height = MAX (1, attributes.height);
2570 priv->bin_window = gdk_window_new (priv->view_window,
2571 &attributes, attributes_mask);
2572 gdk_window_set_user_data (priv->bin_window, menu);
2574 children = GTK_MENU_SHELL (menu)->priv->children;
2577 child = children->data;
2578 children = children->next;
2580 gtk_widget_set_parent_window (child, priv->bin_window);
2583 gtk_style_context_set_background (context, priv->bin_window);
2584 gtk_style_context_set_background (context, priv->view_window);
2585 gtk_style_context_set_background (context, window);
2587 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2588 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2589 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2591 gdk_window_show (priv->bin_window);
2592 gdk_window_show (priv->view_window);
2596 gtk_menu_focus (GtkWidget *widget,
2597 GtkDirectionType direction)
2599 /* A menu or its menu items cannot have focus */
2603 /* See notes in gtk_menu_popup() for information
2604 * about the "grab transfer window"
2607 menu_grab_transfer_window_get (GtkMenu *menu)
2609 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2612 GdkWindowAttr attributes;
2613 gint attributes_mask;
2615 attributes.x = -100;
2616 attributes.y = -100;
2617 attributes.width = 10;
2618 attributes.height = 10;
2619 attributes.window_type = GDK_WINDOW_TEMP;
2620 attributes.wclass = GDK_INPUT_ONLY;
2621 attributes.override_redirect = TRUE;
2622 attributes.event_mask = 0;
2624 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2626 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2627 &attributes, attributes_mask);
2628 gdk_window_set_user_data (window, menu);
2630 gdk_window_show (window);
2632 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2639 menu_grab_transfer_window_destroy (GtkMenu *menu)
2641 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2644 gdk_window_set_user_data (window, NULL);
2645 gdk_window_destroy (window);
2646 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2651 gtk_menu_unrealize (GtkWidget *widget)
2653 GtkMenu *menu = GTK_MENU (widget);
2654 GtkMenuPrivate *priv = menu->priv;
2656 menu_grab_transfer_window_destroy (menu);
2658 gdk_window_set_user_data (priv->view_window, NULL);
2659 gdk_window_destroy (priv->view_window);
2660 priv->view_window = NULL;
2662 gdk_window_set_user_data (priv->bin_window, NULL);
2663 gdk_window_destroy (priv->bin_window);
2664 priv->bin_window = NULL;
2666 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2670 calculate_line_heights (GtkMenu *menu,
2672 guint **ret_min_heights,
2673 guint **ret_nat_heights)
2676 GtkMenuPrivate *priv;
2677 GtkMenuShell *menu_shell;
2678 GtkWidget *child, *widget;
2680 guint horizontal_padding;
2689 widget = GTK_WIDGET (menu);
2690 menu_shell = GTK_MENU_SHELL (widget);
2692 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2693 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2694 n_heights = gtk_menu_get_n_rows (menu);
2695 n_columns = gtk_menu_get_n_columns (menu);
2696 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2698 gtk_widget_style_get (GTK_WIDGET (menu),
2699 "horizontal-padding", &horizontal_padding,
2701 get_menu_padding (widget, &padding);
2703 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2704 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2706 for (children = menu_shell->priv->children; children; children = children->next)
2711 gint child_min, child_nat;
2713 child = children->data;
2715 if (!gtk_widget_get_visible (child))
2718 get_effective_child_attach (child, &l, &r, &t, &b);
2720 part = avail_width / (r - l);
2722 gtk_widget_get_preferred_height_for_width (child, part,
2723 &child_min, &child_nat);
2725 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2727 part = MAX (child_min, toggle_size) / (b - t);
2728 min_heights[t] = MAX (min_heights[t], part);
2730 part = MAX (child_nat, toggle_size) / (b - t);
2731 nat_heights[t] = MAX (nat_heights[t], part);
2734 if (ret_min_heights)
2735 *ret_min_heights = min_heights;
2737 g_free (min_heights);
2739 if (ret_nat_heights)
2740 *ret_nat_heights = nat_heights;
2742 g_free (nat_heights);
2748 gtk_menu_size_allocate (GtkWidget *widget,
2749 GtkAllocation *allocation)
2752 GtkMenuPrivate *priv;
2753 GtkMenuShell *menu_shell;
2755 GtkAllocation child_allocation;
2760 guint vertical_padding;
2761 guint horizontal_padding;
2764 g_return_if_fail (GTK_IS_MENU (widget));
2765 g_return_if_fail (allocation != NULL);
2767 menu = GTK_MENU (widget);
2768 menu_shell = GTK_MENU_SHELL (widget);
2771 gtk_widget_set_allocation (widget, allocation);
2773 gtk_widget_style_get (GTK_WIDGET (menu),
2774 "vertical-padding", &vertical_padding,
2775 "horizontal-padding", &horizontal_padding,
2778 get_menu_padding (widget, &padding);
2779 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2781 g_free (priv->heights);
2782 priv->heights_length = calculate_line_heights (menu,
2787 /* refresh our cached height request */
2788 priv->requested_height = (2 * (border_width + vertical_padding)) +
2789 padding.top + padding.bottom;
2790 for (i = 0; i < priv->heights_length; i++)
2791 priv->requested_height += priv->heights[i];
2793 x = border_width + padding.left + horizontal_padding;
2794 y = border_width + padding.top + vertical_padding;
2795 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2796 padding.left - padding.right;
2797 height = allocation->height - (2 * (border_width + vertical_padding)) -
2798 padding.top - padding.bottom;
2800 if (menu_shell->priv->active)
2801 gtk_menu_scroll_to (menu, priv->scroll_offset);
2803 if (!priv->tearoff_active)
2805 GtkBorder arrow_border;
2807 get_arrows_border (menu, &arrow_border);
2808 y += arrow_border.top;
2809 height -= arrow_border.top;
2810 height -= arrow_border.bottom;
2813 width = MAX (1, width);
2814 height = MAX (1, height);
2816 if (gtk_widget_get_realized (widget))
2818 gdk_window_move_resize (gtk_widget_get_window (widget),
2819 allocation->x, allocation->y,
2820 allocation->width, allocation->height);
2822 gdk_window_move_resize (priv->view_window, x, y, width, height);
2825 if (menu_shell->priv->children)
2827 gint base_width = width / gtk_menu_get_n_columns (menu);
2829 children = menu_shell->priv->children;
2832 child = children->data;
2833 children = children->next;
2835 if (gtk_widget_get_visible (child))
2840 get_effective_child_attach (child, &l, &r, &t, &b);
2842 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2845 tmp = gtk_menu_get_n_columns (menu) - l;
2846 l = gtk_menu_get_n_columns (menu) - r;
2850 child_allocation.width = (r - l) * base_width;
2851 child_allocation.height = 0;
2852 child_allocation.x = l * base_width;
2853 child_allocation.y = 0;
2855 for (i = 0; i < b; i++)
2858 child_allocation.y += priv->heights[i];
2860 child_allocation.height += priv->heights[i];
2863 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2866 gtk_widget_size_allocate (child, &child_allocation);
2867 gtk_widget_queue_draw (child);
2871 /* Resize the item window */
2872 if (gtk_widget_get_realized (widget))
2878 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2879 height += priv->heights[i];
2881 width = gtk_menu_get_n_columns (menu) * base_width;
2882 gdk_window_resize (priv->bin_window, width, height);
2885 if (priv->tearoff_active)
2887 if (height >= priv->requested_height)
2889 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2891 gtk_widget_hide (priv->tearoff_scrollbar);
2892 gtk_menu_set_tearoff_hints (menu, allocation->width);
2894 gtk_menu_scroll_to (menu, 0);
2899 gtk_adjustment_configure (priv->tearoff_adjustment,
2900 gtk_adjustment_get_value (priv->tearoff_adjustment),
2902 priv->requested_height,
2903 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2904 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2905 allocation->height);
2907 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2909 gtk_widget_show (priv->tearoff_scrollbar);
2910 gtk_menu_set_tearoff_hints (menu, allocation->width);
2918 get_arrows_visible_area (GtkMenu *menu,
2919 GdkRectangle *border,
2920 GdkRectangle *upper,
2921 GdkRectangle *lower,
2924 GtkArrowPlacement arrow_placement;
2925 GtkWidget *widget = GTK_WIDGET (menu);
2927 guint vertical_padding;
2928 guint horizontal_padding;
2929 gint scroll_arrow_height;
2930 GtkBorder menu_padding;
2932 gtk_widget_style_get (widget,
2933 "vertical-padding", &vertical_padding,
2934 "horizontal-padding", &horizontal_padding,
2935 "scroll-arrow-vlength", &scroll_arrow_height,
2936 "arrow-placement", &arrow_placement,
2939 get_menu_padding (widget, &menu_padding);
2940 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2941 border->x = border_width + menu_padding.left + horizontal_padding;
2942 border->y = border_width + menu_padding.top + vertical_padding;
2943 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2944 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2946 switch (arrow_placement)
2948 case GTK_ARROWS_BOTH:
2949 upper->x = border->x;
2950 upper->y = border->y;
2951 upper->width = border->width - 2 * border->x;
2952 upper->height = scroll_arrow_height;
2954 lower->x = border->x;
2955 lower->y = border->height - border->y - scroll_arrow_height;
2956 lower->width = border->width - 2 * border->x;
2957 lower->height = scroll_arrow_height;
2960 case GTK_ARROWS_START:
2961 upper->x = border->x;
2962 upper->y = border->y;
2963 upper->width = (border->width - 2 * border->x) / 2;
2964 upper->height = scroll_arrow_height;
2966 lower->x = border->x + upper->width;
2967 lower->y = border->y;
2968 lower->width = (border->width - 2 * border->x) / 2;
2969 lower->height = scroll_arrow_height;
2972 case GTK_ARROWS_END:
2973 upper->x = border->x;
2974 upper->y = border->height - border->y - scroll_arrow_height;
2975 upper->width = (border->width - 2 * border->x) / 2;
2976 upper->height = scroll_arrow_height;
2978 lower->x = border->x + upper->width;
2979 lower->y = border->height - border->y - scroll_arrow_height;
2980 lower->width = (border->width - 2 * border->x) / 2;
2981 lower->height = scroll_arrow_height;
2985 g_assert_not_reached();
2986 upper->x = upper->y = upper->width = upper->height = 0;
2987 lower->x = lower->y = lower->width = lower->height = 0;
2990 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
2994 gtk_menu_draw (GtkWidget *widget,
2998 GtkMenuPrivate *priv;
2999 GtkStyleContext *context;
3000 GdkRectangle border;
3004 GtkBorder menu_padding;
3006 menu = GTK_MENU (widget);
3008 context = gtk_widget_get_style_context (widget);
3010 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3011 get_menu_padding (widget, &menu_padding);
3013 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3015 gfloat arrow_scaling;
3018 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3019 arrow_size = arrow_scaling * arrow_space;
3021 gtk_render_background (context, cr, 0, 0,
3022 gtk_widget_get_allocated_width (widget),
3023 gtk_widget_get_allocated_height (widget));
3024 gtk_render_frame (context, cr, 0, 0,
3025 gtk_widget_get_allocated_width (widget),
3026 gtk_widget_get_allocated_height (widget));
3028 gtk_style_context_save (context);
3029 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3031 if (priv->upper_arrow_visible && !priv->tearoff_active)
3033 gtk_style_context_save (context);
3034 gtk_style_context_set_state (context, priv->upper_arrow_state);
3036 gtk_render_background (context, cr,
3038 upper.width, upper.height);
3039 gtk_render_frame (context, cr,
3041 upper.width, upper.height);
3043 gtk_render_arrow (context, cr, 0,
3044 upper.x + (upper.width - arrow_size) / 2,
3045 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3048 gtk_style_context_restore (context);
3051 if (priv->lower_arrow_visible && !priv->tearoff_active)
3053 gtk_style_context_save (context);
3054 gtk_style_context_set_state (context, priv->lower_arrow_state);
3056 gtk_render_background (context, cr,
3058 lower.width, lower.height);
3059 gtk_render_frame (context, cr,
3061 lower.width, lower.height);
3063 gtk_render_arrow (context, cr, G_PI,
3064 lower.x + (lower.width - arrow_size) / 2,
3065 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3068 gtk_style_context_restore (context);
3071 gtk_style_context_restore (context);
3074 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3076 gint y = -border.y + priv->scroll_offset;
3079 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3081 if (!priv->tearoff_active)
3083 GtkBorder arrow_border;
3085 get_arrows_border (menu, &arrow_border);
3086 y -= arrow_border.top;
3089 gtk_render_background (context, cr,
3091 border.width, border.height);
3092 gtk_render_frame (context, cr,
3094 border.width, border.height);
3099 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3105 gtk_menu_show (GtkWidget *widget)
3107 GtkMenu *menu = GTK_MENU (widget);
3109 _gtk_menu_refresh_accel_paths (menu, FALSE);
3111 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3116 gtk_menu_get_preferred_width (GtkWidget *widget,
3121 GtkMenuShell *menu_shell;
3122 GtkMenuPrivate *priv;
3125 guint max_toggle_size;
3126 guint max_accel_width;
3127 guint horizontal_padding;
3129 gint child_min, child_nat;
3130 gint min_width, nat_width;
3133 menu = GTK_MENU (widget);
3134 menu_shell = GTK_MENU_SHELL (widget);
3137 min_width = nat_width = 0;
3139 max_toggle_size = 0;
3140 max_accel_width = 0;
3142 children = menu_shell->priv->children;
3149 child = children->data;
3150 children = children->next;
3152 if (! gtk_widget_get_visible (child))
3155 get_effective_child_attach (child, &l, &r, &t, &b);
3157 /* It's important to size_request the child
3158 * before doing the toggle size request, in
3159 * case the toggle size request depends on the size
3160 * request of a child of the child (e.g. for ImageMenuItem)
3162 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3164 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3165 max_toggle_size = MAX (max_toggle_size, toggle_size);
3166 max_accel_width = MAX (max_accel_width,
3167 GTK_MENU_ITEM (child)->priv->accelerator_width);
3169 part = child_min / (r - l);
3170 min_width = MAX (min_width, part);
3172 part = child_nat / (r - l);
3173 nat_width = MAX (nat_width, part);
3176 /* If the menu doesn't include any images or check items
3177 * reserve the space so that all menus are consistent.
3178 * We only do this for 'ordinary' menus, not for combobox
3179 * menus or multi-column menus
3181 if (max_toggle_size == 0 &&
3182 gtk_menu_get_n_columns (menu) == 1 &&
3183 !priv->no_toggle_size)
3185 GtkStyleContext *context;
3186 GtkWidgetPath *menu_path, *check_path;
3187 guint toggle_spacing;
3188 guint indicator_size;
3190 context = gtk_widget_get_style_context (widget);
3191 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3193 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3194 check_path = gtk_widget_path_copy (menu_path);
3195 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3197 gtk_style_context_set_path (context, check_path);
3198 gtk_widget_path_free (check_path);
3200 gtk_style_context_get_style (context,
3201 "toggle-spacing", &toggle_spacing,
3202 "indicator-size", &indicator_size,
3205 max_toggle_size = indicator_size + toggle_spacing;
3207 /* Restore real widget path */
3208 gtk_style_context_set_path (context, menu_path);
3209 gtk_widget_path_free (menu_path);
3212 min_width += 2 * max_toggle_size + max_accel_width;
3213 min_width *= gtk_menu_get_n_columns (menu);
3215 nat_width += 2 * max_toggle_size + max_accel_width;
3216 nat_width *= gtk_menu_get_n_columns (menu);
3218 gtk_widget_style_get (GTK_WIDGET (menu),
3219 "horizontal-padding", &horizontal_padding,
3222 get_menu_padding (widget, &padding);
3223 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3224 min_width += (2 * (border_width + horizontal_padding)) +
3225 padding.left + padding.right;
3226 nat_width += (2 * (border_width + horizontal_padding)) +
3227 padding.left + padding.right;
3229 priv->toggle_size = max_toggle_size;
3230 priv->accel_size = max_accel_width;
3233 *minimum_size = min_width;
3236 *natural_size = nat_width;
3238 /* Don't resize the tearoff if it is not active,
3239 * because it won't redraw (it is only a background pixmap).
3241 if (priv->tearoff_active)
3242 gtk_menu_set_tearoff_hints (menu, min_width);
3246 gtk_menu_get_preferred_height (GtkWidget *widget,
3252 /* Menus are height-for-width only, just return the height
3253 * for the minimum width
3255 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3256 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3260 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3266 GtkMenu *menu = GTK_MENU (widget);
3267 GtkMenuPrivate *priv = menu->priv;
3268 guint *min_heights, *nat_heights;
3269 guint vertical_padding, border_width;
3271 gint min_height, nat_height;
3273 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3274 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3275 get_menu_padding (widget, &padding);
3277 min_height = nat_height = (border_width + vertical_padding) * 2 +
3278 padding.top + padding.bottom;
3281 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3283 for (i = 0; i < n_heights; i++)
3285 min_height += min_heights[i];
3286 nat_height += nat_heights[i];
3289 if (priv->have_position)
3291 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3292 GdkRectangle monitor;
3294 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
3296 if (priv->position_y + min_height > monitor.y + monitor.height)
3297 min_height = monitor.y + monitor.height - priv->position_y;
3299 if (priv->position_y + nat_height > monitor.y + monitor.height)
3300 nat_height = monitor.y + monitor.height - priv->position_y;
3302 if (priv->position_y < monitor.y)
3304 min_height -= monitor.y - priv->position_y;
3305 nat_height -= monitor.y - priv->position_y;
3310 *minimum_size = min_height;
3313 *natural_size = nat_height;
3315 g_free (min_heights);
3316 g_free (nat_heights);
3322 gtk_menu_button_scroll (GtkMenu *menu,
3323 GdkEventButton *event)
3325 GtkMenuPrivate *priv = menu->priv;
3327 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3329 gboolean touchscreen_mode;
3331 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3332 "gtk-touchscreen-mode", &touchscreen_mode,
3335 if (touchscreen_mode)
3336 gtk_menu_handle_scrolling (menu,
3337 event->x_root, event->y_root,
3338 event->type == GDK_BUTTON_PRESS,
3348 pointer_in_menu_window (GtkWidget *widget,
3352 GtkMenu *menu = GTK_MENU (widget);
3353 GtkMenuPrivate *priv = menu->priv;
3354 GtkAllocation allocation;
3356 if (gtk_widget_get_mapped (priv->toplevel))
3358 GtkMenuShell *menu_shell;
3359 gint window_x, window_y;
3361 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3362 &window_x, &window_y);
3364 gtk_widget_get_allocation (widget, &allocation);
3365 if (x_root >= window_x && x_root < window_x + allocation.width &&
3366 y_root >= window_y && y_root < window_y + allocation.height)
3369 menu_shell = GTK_MENU_SHELL (widget);
3371 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3372 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3380 gtk_menu_button_press (GtkWidget *widget,
3381 GdkEventButton *event)
3383 if (event->type != GDK_BUTTON_PRESS)
3386 /* Don't pass down to menu shell for presses over scroll arrows
3388 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3391 /* Don't pass down to menu shell if a non-menuitem part of the menu
3392 * was clicked. The check for the event_widget being a GtkMenuShell
3393 * works because we have the pointer grabbed on menu_shell->window
3394 * with owner_events=TRUE, so all events that are either outside
3395 * the menu or on its border are delivered relative to
3396 * menu_shell->window.
3398 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3399 pointer_in_menu_window (widget, event->x_root, event->y_root))
3402 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3406 gtk_menu_button_release (GtkWidget *widget,
3407 GdkEventButton *event)
3409 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3411 if (priv->ignore_button_release)
3413 priv->ignore_button_release = FALSE;
3417 if (event->type != GDK_BUTTON_RELEASE)
3420 /* Don't pass down to menu shell for releases over scroll arrows
3422 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3425 /* Don't pass down to menu shell if a non-menuitem part of the menu
3426 * was clicked (see comment in button_press()).
3428 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3429 pointer_in_menu_window (widget, event->x_root, event->y_root))
3431 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3432 * bail out early here so it is in a consistent state for the
3433 * next button_press/button_release in GtkMenuShell.
3436 if (GTK_MENU_SHELL (widget)->priv->active)
3437 GTK_MENU_SHELL (widget)->priv->button = 0;
3442 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3445 static const gchar *
3446 get_accel_path (GtkWidget *menu_item,
3451 GClosure *accel_closure;
3452 GtkAccelGroup *accel_group;
3454 path = _gtk_widget_get_accel_path (menu_item, locked);
3457 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3463 label = gtk_bin_get_child (GTK_BIN (menu_item));
3465 if (GTK_IS_ACCEL_LABEL (label))
3467 g_object_get (label,
3468 "accel-closure", &accel_closure,
3472 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3474 *locked = gtk_accel_group_get_is_locked (accel_group);
3484 gtk_menu_key_press (GtkWidget *widget,
3487 GtkMenuShell *menu_shell;
3489 gboolean delete = FALSE;
3490 gboolean can_change_accels;
3491 gchar *accel = NULL;
3492 guint accel_key, accel_mods;
3493 GdkModifierType consumed_modifiers;
3494 GdkDisplay *display;
3496 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3497 g_return_val_if_fail (event != NULL, FALSE);
3499 menu_shell = GTK_MENU_SHELL (widget);
3500 menu = GTK_MENU (widget);
3502 gtk_menu_stop_navigating_submenu (menu);
3504 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3507 display = gtk_widget_get_display (widget);
3509 g_object_get (gtk_widget_get_settings (widget),
3510 "gtk-menu-bar-accel", &accel,
3511 "gtk-can-change-accels", &can_change_accels,
3514 if (accel && *accel)
3517 GdkModifierType mods = 0;
3519 gtk_accelerator_parse (accel, &keyval, &mods);
3522 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3524 /* FIXME this is wrong, needs to be in the global accel resolution
3525 * thing, to properly consider i18n etc., but that probably requires
3526 * AccelGroup changes etc.
3528 if (event->keyval == keyval && (mods & event->state) == mods)
3530 gtk_menu_shell_cancel (menu_shell);
3538 switch (event->keyval)
3540 case GDK_KEY_Delete:
3541 case GDK_KEY_KP_Delete:
3542 case GDK_KEY_BackSpace:
3549 /* Figure out what modifiers went into determining the key symbol */
3550 _gtk_translate_keyboard_accel_state (gdk_keymap_get_for_display (display),
3551 event->hardware_keycode,
3553 gtk_accelerator_get_default_mod_mask (),
3555 &accel_key, NULL, NULL, &consumed_modifiers);
3557 accel_key = gdk_keyval_to_lower (accel_key);
3558 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3560 /* If lowercasing affects the keysym, then we need to include SHIFT
3561 * in the modifiers, We re-upper case when we match against the
3562 * keyval, but display and save in caseless form.
3564 if (accel_key != event->keyval)
3565 accel_mods |= GDK_SHIFT_MASK;
3567 /* Modify the accelerators */
3568 if (can_change_accels &&
3569 menu_shell->priv->active_menu_item &&
3570 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3571 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3572 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3574 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3575 gboolean locked, replace_accels = TRUE;
3578 path = get_accel_path (menu_item, &locked);
3579 if (!path || locked)
3581 /* Can't change accelerators on menu_items without paths
3582 * (basically, those items are accelerator-locked).
3584 gtk_widget_error_bell (widget);
3590 /* For the keys that act to delete the current setting,
3591 * we delete the current setting if there is one, otherwise,
3592 * we set the key as the accelerator.
3598 if (gtk_accel_map_lookup_entry (path, &key) &&
3599 (key.accel_key || key.accel_mods))
3605 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3609 /* We failed, probably because this key is in use
3610 * and locked already.
3612 gtk_widget_error_bell (widget);
3621 check_threshold (GtkWidget *widget,
3630 ABS (start_x - x) > THRESHOLD ||
3631 ABS (start_y - y) > THRESHOLD;
3635 definitely_within_item (GtkWidget *widget,
3639 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3642 w = gdk_window_get_width (window);
3643 h = gdk_window_get_height (window);
3646 check_threshold (widget, 0, 0, x, y) &&
3647 check_threshold (widget, w - 1, 0, x, y) &&
3648 check_threshold (widget, w - 1, h - 1, x, y) &&
3649 check_threshold (widget, 0, h - 1, x, y);
3653 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3655 GtkMenuPrivate *priv = menu->priv;
3657 return priv->navigation_height && priv->navigation_width;
3661 gtk_menu_motion_notify (GtkWidget *widget,
3662 GdkEventMotion *event)
3664 GtkWidget *menu_item;
3666 GtkMenuShell *menu_shell;
3669 gboolean need_enter;
3671 if (GTK_IS_MENU (widget))
3673 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3675 if (priv->ignore_button_release)
3676 priv->ignore_button_release = FALSE;
3678 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3682 /* We received the event for one of two reasons:
3684 * a) We are the active menu, and did gtk_grab_add()
3685 * b) The widget is a child of ours, and the event was propagated
3687 * Since for computation of navigation regions, we want the menu which
3688 * is the parent of the menu item, for a), we need to find that menu,
3689 * which may be different from 'widget'.
3691 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3692 parent = gtk_widget_get_parent (menu_item);
3693 if (!GTK_IS_MENU_ITEM (menu_item) ||
3694 !GTK_IS_MENU (parent))
3697 menu_shell = GTK_MENU_SHELL (parent);
3698 menu = GTK_MENU (menu_shell);
3700 if (definitely_within_item (menu_item, event->x, event->y))
3701 menu_shell->priv->activate_time = 0;
3703 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3705 /* Check to see if we are within an active submenu's navigation region
3707 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3710 /* Make sure we pop down if we enter a non-selectable menu item, so we
3711 * don't show a submenu when the cursor is outside the stay-up triangle.
3713 if (!_gtk_menu_item_is_selectable (menu_item))
3715 /* We really want to deselect, but this gives the menushell code
3716 * a chance to do some bookkeeping about the menuitem.
3718 gtk_menu_shell_select_item (menu_shell, menu_item);
3724 /* The menu is now sensitive to enter events on its items, but
3725 * was previously sensitive. So we fake an enter event.
3727 menu_shell->priv->ignore_enter = FALSE;
3729 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3730 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3732 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3735 send_event->crossing.window = g_object_ref (event->window);
3736 send_event->crossing.time = event->time;
3737 send_event->crossing.send_event = TRUE;
3738 send_event->crossing.x_root = event->x_root;
3739 send_event->crossing.y_root = event->y_root;
3740 send_event->crossing.x = event->x;
3741 send_event->crossing.y = event->y;
3742 send_event->crossing.state = event->state;
3743 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3745 /* We send the event to 'widget', the currently active menu,
3746 * instead of 'menu', the menu that the pointer is in. This
3747 * will ensure that the event will be ignored unless the
3748 * menuitem is a child of the active menu or some parent
3749 * menu of the active menu.
3751 result = gtk_widget_event (widget, send_event);
3752 gdk_event_free (send_event);
3762 get_double_arrows (GtkMenu *menu)
3764 GtkMenuPrivate *priv = menu->priv;
3765 gboolean double_arrows;
3766 GtkArrowPlacement arrow_placement;
3768 gtk_widget_style_get (GTK_WIDGET (menu),
3769 "double-arrows", &double_arrows,
3770 "arrow-placement", &arrow_placement,
3773 if (arrow_placement != GTK_ARROWS_BOTH)
3776 return double_arrows || (priv->initially_pushed_in &&
3777 priv->scroll_offset != 0);
3781 gtk_menu_scroll_by (GtkMenu *menu,
3784 GtkMenuPrivate *priv = menu->priv;
3785 GtkBorder arrow_border;
3789 gboolean double_arrows;
3791 widget = GTK_WIDGET (menu);
3792 offset = priv->scroll_offset + step;
3794 get_arrows_border (menu, &arrow_border);
3796 double_arrows = get_double_arrows (menu);
3798 /* If we scroll upward and the non-visible top part
3799 * is smaller than the scroll arrow it would be
3800 * pretty stupid to show the arrow and taking more
3801 * screen space than just scrolling to the top.
3804 if ((step < 0) && (offset < arrow_border.top))
3807 /* Don't scroll over the top if we weren't before: */
3808 if ((priv->scroll_offset >= 0) && (offset < 0))
3811 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3813 if (priv->scroll_offset == 0 &&
3814 view_height >= priv->requested_height)
3817 /* Don't scroll past the bottom if we weren't before: */
3818 if (priv->scroll_offset > 0)
3819 view_height -= arrow_border.top;
3821 /* When both arrows are always shown,
3822 * reduce view height even more.
3825 view_height -= arrow_border.bottom;
3827 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3828 (offset + view_height > priv->requested_height))
3829 offset = priv->requested_height - view_height;
3831 if (offset != priv->scroll_offset)
3832 gtk_menu_scroll_to (menu, offset);
3836 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3837 gboolean touchscreen_mode)
3839 GtkMenuPrivate *priv = menu->priv;
3840 gboolean upper_visible;
3841 gboolean lower_visible;
3843 upper_visible = priv->upper_arrow_visible;
3844 lower_visible = priv->lower_arrow_visible;
3846 gtk_menu_scroll_by (menu, priv->scroll_step);
3848 if (touchscreen_mode &&
3849 (upper_visible != priv->upper_arrow_visible ||
3850 lower_visible != priv->lower_arrow_visible))
3852 /* We are about to hide a scroll arrow while the mouse is pressed,
3853 * this would cause the uncovered menu item to be activated on button
3854 * release. Therefore we need to ignore button release here
3856 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3857 priv->ignore_button_release = TRUE;
3862 gtk_menu_scroll_timeout (gpointer data)
3865 gboolean touchscreen_mode;
3867 menu = GTK_MENU (data);
3869 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3870 "gtk-touchscreen-mode", &touchscreen_mode,
3873 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3879 gtk_menu_scroll_timeout_initial (gpointer data)
3883 gboolean touchscreen_mode;
3885 menu = GTK_MENU (data);
3887 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3888 "gtk-timeout-repeat", &timeout,
3889 "gtk-touchscreen-mode", &touchscreen_mode,
3892 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3894 gtk_menu_remove_scroll_timeout (menu);
3896 menu->priv->scroll_timeout =
3897 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3903 gtk_menu_start_scrolling (GtkMenu *menu)
3906 gboolean touchscreen_mode;
3908 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3909 "gtk-timeout-repeat", &timeout,
3910 "gtk-touchscreen-mode", &touchscreen_mode,
3913 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3915 menu->priv->scroll_timeout =
3916 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3920 gtk_menu_scroll (GtkWidget *widget,
3921 GdkEventScroll *event)
3923 GtkMenu *menu = GTK_MENU (widget);
3925 switch (event->direction)
3927 case GDK_SCROLL_RIGHT:
3928 case GDK_SCROLL_DOWN:
3929 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3931 case GDK_SCROLL_LEFT:
3933 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3941 get_arrows_sensitive_area (GtkMenu *menu,
3942 GdkRectangle *upper,
3943 GdkRectangle *lower)
3945 GtkArrowPlacement arrow_placement;
3946 GtkWidget *widget = GTK_WIDGET (menu);
3950 guint vertical_padding;
3952 gint scroll_arrow_height;
3955 window = gtk_widget_get_window (widget);
3956 width = gdk_window_get_width (window);
3957 height = gdk_window_get_height (window);
3959 gtk_widget_style_get (widget,
3960 "vertical-padding", &vertical_padding,
3961 "scroll-arrow-vlength", &scroll_arrow_height,
3962 "arrow-placement", &arrow_placement,
3965 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3966 get_menu_padding (widget, &padding);
3968 gdk_window_get_position (window, &win_x, &win_y);
3970 switch (arrow_placement)
3972 case GTK_ARROWS_BOTH:
3977 upper->width = width;
3978 upper->height = scroll_arrow_height + border + padding.top;
3984 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3985 lower->width = width;
3986 lower->height = scroll_arrow_height + border + padding.bottom;
3990 case GTK_ARROWS_START:
3995 upper->width = width / 2;
3996 upper->height = scroll_arrow_height + border + padding.top;
4001 lower->x = win_x + width / 2;
4003 lower->width = width / 2;
4004 lower->height = scroll_arrow_height + border + padding.bottom;
4008 case GTK_ARROWS_END:
4012 upper->y = win_y + height - border - scroll_arrow_height;
4013 upper->width = width / 2;
4014 upper->height = scroll_arrow_height + border + padding.top;
4019 lower->x = win_x + width / 2;
4020 lower->y = win_y + height - border - scroll_arrow_height;
4021 lower->width = width / 2;
4022 lower->height = scroll_arrow_height + border + padding.bottom;
4030 gtk_menu_handle_scrolling (GtkMenu *menu,
4036 GtkMenuPrivate *priv = menu->priv;
4037 GtkMenuShell *menu_shell;
4040 gboolean scroll_fast = FALSE;
4042 gboolean touchscreen_mode;
4044 menu_shell = GTK_MENU_SHELL (menu);
4046 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4047 "gtk-touchscreen-mode", &touchscreen_mode,
4050 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4055 /* upper arrow handling */
4057 get_arrows_sensitive_area (menu, &rect, NULL);
4060 if (priv->upper_arrow_visible && !priv->tearoff_active &&
4061 (x >= rect.x) && (x < rect.x + rect.width) &&
4062 (y >= rect.y) && (y < rect.y + rect.height))
4067 if (touchscreen_mode)
4068 priv->upper_arrow_prelight = in_arrow;
4070 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4072 gboolean arrow_pressed = FALSE;
4074 if (priv->upper_arrow_visible && !priv->tearoff_active)
4076 if (touchscreen_mode)
4078 if (enter && priv->upper_arrow_prelight)
4080 if (priv->scroll_timeout == 0)
4082 /* Deselect the active item so that
4083 * any submenus are popped down
4085 gtk_menu_shell_deselect (menu_shell);
4087 gtk_menu_remove_scroll_timeout (menu);
4088 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4092 /* Only do stuff on click. */
4093 gtk_menu_start_scrolling (menu);
4094 arrow_pressed = TRUE;
4099 arrow_pressed = TRUE;
4104 gtk_menu_stop_scrolling (menu);
4107 else /* !touchscreen_mode */
4109 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4111 if (enter && in_arrow &&
4112 (!priv->upper_arrow_prelight ||
4113 priv->scroll_fast != scroll_fast))
4115 priv->upper_arrow_prelight = TRUE;
4116 priv->scroll_fast = scroll_fast;
4118 /* Deselect the active item so that
4119 * any submenus are popped down
4121 gtk_menu_shell_deselect (menu_shell);
4123 gtk_menu_remove_scroll_timeout (menu);
4124 priv->scroll_step = scroll_fast
4125 ? -MENU_SCROLL_STEP2
4126 : -MENU_SCROLL_STEP1;
4128 priv->scroll_timeout =
4129 gdk_threads_add_timeout (scroll_fast
4130 ? MENU_SCROLL_TIMEOUT2
4131 : MENU_SCROLL_TIMEOUT1,
4132 gtk_menu_scroll_timeout, menu);
4134 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4136 gtk_menu_stop_scrolling (menu);
4141 /* gtk_menu_start_scrolling() might have hit the top of the
4142 * menu, so check if the button isn't insensitive before
4143 * changing it to something else.
4145 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4147 GtkStateFlags arrow_state = 0;
4150 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4152 if (priv->upper_arrow_prelight)
4153 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4155 if (arrow_state != priv->upper_arrow_state)
4157 priv->upper_arrow_state = arrow_state;
4159 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4165 /* lower arrow handling */
4167 get_arrows_sensitive_area (menu, NULL, &rect);
4170 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4171 (x >= rect.x) && (x < rect.x + rect.width) &&
4172 (y >= rect.y) && (y < rect.y + rect.height))
4177 if (touchscreen_mode)
4178 priv->lower_arrow_prelight = in_arrow;
4180 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4182 gboolean arrow_pressed = FALSE;
4184 if (priv->lower_arrow_visible && !priv->tearoff_active)
4186 if (touchscreen_mode)
4188 if (enter && priv->lower_arrow_prelight)
4190 if (priv->scroll_timeout == 0)
4192 /* Deselect the active item so that
4193 * any submenus are popped down
4195 gtk_menu_shell_deselect (menu_shell);
4197 gtk_menu_remove_scroll_timeout (menu);
4198 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4202 /* Only do stuff on click. */
4203 gtk_menu_start_scrolling (menu);
4204 arrow_pressed = TRUE;
4209 arrow_pressed = TRUE;
4214 gtk_menu_stop_scrolling (menu);
4217 else /* !touchscreen_mode */
4219 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4221 if (enter && in_arrow &&
4222 (!priv->lower_arrow_prelight ||
4223 priv->scroll_fast != scroll_fast))
4225 priv->lower_arrow_prelight = TRUE;
4226 priv->scroll_fast = scroll_fast;
4228 /* Deselect the active item so that
4229 * any submenus are popped down
4231 gtk_menu_shell_deselect (menu_shell);
4233 gtk_menu_remove_scroll_timeout (menu);
4234 priv->scroll_step = scroll_fast
4236 : MENU_SCROLL_STEP1;
4238 priv->scroll_timeout =
4239 gdk_threads_add_timeout (scroll_fast
4240 ? MENU_SCROLL_TIMEOUT2
4241 : MENU_SCROLL_TIMEOUT1,
4242 gtk_menu_scroll_timeout, menu);
4244 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4246 gtk_menu_stop_scrolling (menu);
4251 /* gtk_menu_start_scrolling() might have hit the bottom of the
4252 * menu, so check if the button isn't insensitive before
4253 * changing it to something else.
4255 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4257 GtkStateFlags arrow_state = 0;
4260 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4262 if (priv->lower_arrow_prelight)
4263 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4265 if (arrow_state != priv->lower_arrow_state)
4267 priv->lower_arrow_state = arrow_state;
4269 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4277 gtk_menu_enter_notify (GtkWidget *widget,
4278 GdkEventCrossing *event)
4280 GtkWidget *menu_item;
4282 gboolean touchscreen_mode;
4284 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4285 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4286 event->mode == GDK_CROSSING_STATE_CHANGED)
4289 g_object_get (gtk_widget_get_settings (widget),
4290 "gtk-touchscreen-mode", &touchscreen_mode,
4293 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4294 if (GTK_IS_MENU (widget))
4296 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4298 if (!menu_shell->priv->ignore_enter)
4299 gtk_menu_handle_scrolling (GTK_MENU (widget),
4300 event->x_root, event->y_root, TRUE, TRUE);
4303 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4305 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4307 if (GTK_IS_MENU (menu))
4309 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4310 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4312 if (priv->seen_item_enter)
4314 /* This is the second enter we see for an item
4315 * on this menu. This means a release should always
4318 menu_shell->priv->activate_time = 0;
4320 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4321 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4323 if (definitely_within_item (menu_item, event->x, event->y))
4325 /* This is an actual user-enter (ie. not a pop-under)
4326 * In this case, the user must either have entered
4327 * sufficiently far enough into the item, or he must move
4328 * far enough away from the enter point. (see
4329 * gtk_menu_motion_notify())
4331 menu_shell->priv->activate_time = 0;
4335 priv->seen_item_enter = TRUE;
4339 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4340 * will not correspond to the event widget's parent. Check to see
4341 * if we are in the parent's navigation region.
4343 parent = gtk_widget_get_parent (menu_item);
4344 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4345 gtk_menu_navigating_submenu (GTK_MENU (parent),
4346 event->x_root, event->y_root))
4349 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4353 gtk_menu_leave_notify (GtkWidget *widget,
4354 GdkEventCrossing *event)
4356 GtkMenuShell *menu_shell;
4358 GtkMenuItem *menu_item;
4359 GtkWidget *event_widget;
4361 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4362 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4363 event->mode == GDK_CROSSING_STATE_CHANGED)
4366 menu = GTK_MENU (widget);
4367 menu_shell = GTK_MENU_SHELL (widget);
4369 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4372 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4374 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4376 if (!GTK_IS_MENU_ITEM (event_widget))
4379 menu_item = GTK_MENU_ITEM (event_widget);
4381 /* Here we check to see if we're leaving an active menu item
4382 * with a submenu, in which case we enter submenu navigation mode.
4384 if (menu_shell->priv->active_menu_item != NULL
4385 && menu_item->priv->submenu != NULL
4386 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4388 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4390 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4393 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4395 /* We are leaving an active menu item with nonactive submenu.
4396 * Deselect it so we don't surprise the user with by popping
4397 * up a submenu _after_ he left the item.
4399 gtk_menu_shell_deselect (menu_shell);
4404 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4408 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4410 GtkMenuPrivate *priv = menu->priv;
4412 priv->navigation_x = 0;
4413 priv->navigation_y = 0;
4414 priv->navigation_width = 0;
4415 priv->navigation_height = 0;
4417 if (priv->navigation_timeout)
4419 g_source_remove (priv->navigation_timeout);
4420 priv->navigation_timeout = 0;
4424 /* When the timeout is elapsed, the navigation region is destroyed
4425 * and the menuitem under the pointer (if any) is selected.
4428 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4430 GtkMenuPopdownData *popdown_data = user_data;
4431 GtkMenu *menu = popdown_data->menu;
4432 GtkMenuPrivate *priv = menu->priv;
4433 GdkWindow *child_window;
4435 gtk_menu_stop_navigating_submenu (menu);
4437 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4439 child_window = gdk_window_get_device_position (priv->bin_window,
4440 popdown_data->device,
4445 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4447 send_event->crossing.window = g_object_ref (child_window);
4448 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4449 send_event->crossing.send_event = TRUE;
4450 gdk_event_set_device (send_event, popdown_data->device);
4452 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4454 gdk_event_free (send_event);
4462 gtk_menu_navigating_submenu (GtkMenu *menu,
4466 GtkMenuPrivate *priv = menu->priv;
4469 if (!gtk_menu_has_navigation_triangle (menu))
4472 width = priv->navigation_width;
4473 height = priv->navigation_height;
4475 /* Check if x/y are in the triangle spanned by the navigation parameters */
4477 /* 1) Move the coordinates so the triangle starts at 0,0 */
4478 event_x -= priv->navigation_x;
4479 event_y -= priv->navigation_y;
4481 /* 2) Ensure both legs move along the positive axis */
4493 /* 3) Check that the given coordinate is inside the triangle. The formula
4494 * is a transformed form of this formula: x/w + y/h <= 1
4496 if (event_x >= 0 && event_y >= 0 &&
4497 event_x * height + event_y * width <= width * height)
4503 gtk_menu_stop_navigating_submenu (menu);
4509 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4510 GtkMenuItem *menu_item,
4511 GdkEventCrossing *event)
4513 GtkMenuPrivate *priv = menu->priv;
4514 gint submenu_left = 0;
4515 gint submenu_right = 0;
4516 gint submenu_top = 0;
4517 gint submenu_bottom = 0;
4519 GtkWidget *event_widget;
4520 GtkMenuPopdownData *popdown_data;
4523 g_return_if_fail (menu_item->priv->submenu != NULL);
4524 g_return_if_fail (event != NULL);
4526 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4528 window = gtk_widget_get_window (menu_item->priv->submenu);
4529 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4531 submenu_right = submenu_left + gdk_window_get_width (window);
4532 submenu_bottom = submenu_top + gdk_window_get_height (window);
4534 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4536 if (event->x >= 0 && event->x < width)
4540 gtk_menu_stop_navigating_submenu (menu);
4542 /* The navigation region is the triangle closest to the x/y
4543 * location of the rectangle. This is why the width or height
4546 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4549 priv->navigation_x = submenu_left;
4550 priv->navigation_width = event->x_root - submenu_left;
4555 priv->navigation_x = submenu_right;
4556 priv->navigation_width = event->x_root - submenu_right;
4562 priv->navigation_y = event->y_root;
4563 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4565 if (priv->navigation_height >= 0)
4571 priv->navigation_y = event->y_root;
4572 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4574 if (priv->navigation_height <= 0)
4578 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4579 "gtk-menu-popdown-delay", &popdown_delay,
4582 popdown_data = g_new (GtkMenuPopdownData, 1);
4583 popdown_data->menu = menu;
4584 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4586 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4588 gtk_menu_stop_navigating_submenu_cb,
4590 (GDestroyNotify) g_free);
4595 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4599 g_return_if_fail (GTK_IS_MENU (menu_shell));
4601 parent = menu_shell->priv->parent_menu_shell;
4603 menu_shell->priv->activate_time = 0;
4604 gtk_menu_popdown (GTK_MENU (menu_shell));
4607 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4611 gtk_menu_position (GtkMenu *menu,
4612 gboolean set_scroll_offset)
4614 GtkMenuPrivate *priv = menu->priv;
4616 GtkRequisition requisition;
4620 GdkScreen *pointer_screen;
4621 GdkRectangle monitor;
4624 widget = GTK_WIDGET (menu);
4626 screen = gtk_widget_get_screen (widget);
4627 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4628 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4630 /* Realize so we have the proper width and heigh to figure out
4631 * the right place to popup the menu.
4633 gtk_widget_realize (priv->toplevel);
4634 requisition.width = gtk_widget_get_allocated_width (widget);
4635 requisition.height = gtk_widget_get_allocated_height (widget);
4637 if (pointer_screen != screen)
4639 /* Pointer is on a different screen; roughly center the
4640 * menu on the screen. If someone was using multiscreen
4641 * + Xinerama together they'd probably want something
4642 * fancier; but that is likely to be vanishingly rare.
4644 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4645 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4648 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4649 priv->initially_pushed_in = FALSE;
4651 /* Set the type hint here to allow custom position functions
4652 * to set a different hint
4654 if (!gtk_widget_get_visible (priv->toplevel))
4655 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4657 if (priv->position_func)
4659 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4660 priv->position_func_data);
4662 if (priv->monitor_num < 0)
4663 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4665 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4669 gint space_left, space_right, space_above, space_below;
4673 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4675 get_menu_padding (widget, &padding);
4677 /* The placement of popup menus horizontally works like this (with
4678 * RTL in parentheses)
4680 * - If there is enough room to the right (left) of the mouse cursor,
4681 * position the menu there.
4683 * - Otherwise, if if there is enough room to the left (right) of the
4684 * mouse cursor, position the menu there.
4686 * - Otherwise if the menu is smaller than the monitor, position it
4687 * on the side of the mouse cursor that has the most space available
4689 * - Otherwise (if there is simply not enough room for the menu on the
4690 * monitor), position it as far left (right) as possible.
4692 * Positioning in the vertical direction is similar: first try below
4693 * mouse cursor, then above.
4695 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4697 space_left = x - monitor.x;
4698 space_right = monitor.x + monitor.width - x - 1;
4699 space_above = y - monitor.y;
4700 space_below = monitor.y + monitor.height - y - 1;
4702 /* Position horizontally. */
4704 /* the amount of space we need to position the menu.
4705 * Note the menu is offset "thickness" pixels
4707 needed_width = requisition.width - padding.left;
4709 if (needed_width <= space_left ||
4710 needed_width <= space_right)
4712 if ((rtl && needed_width <= space_left) ||
4713 (!rtl && needed_width > space_right))
4716 x = x + padding.left - requisition.width + 1;
4720 /* position right */
4721 x = x - padding.right;
4724 /* x is clamped on-screen further down */
4726 else if (requisition.width <= monitor.width)
4728 /* the menu is too big to fit on either side of the mouse
4729 * cursor, but smaller than the monitor. Position it on
4730 * the side that has the most space
4732 if (space_left > space_right)
4740 x = monitor.x + monitor.width - requisition.width;
4743 else /* menu is simply too big for the monitor */
4748 x = monitor.x + monitor.width - requisition.width;
4757 /* Position vertically.
4758 * The algorithm is the same as above, but simpler
4759 * because we don't have to take RTL into account.
4761 needed_height = requisition.height - padding.top;
4763 if (needed_height <= space_above ||
4764 needed_height <= space_below)
4766 if (needed_height <= space_below)
4767 y = y - padding.top;
4769 y = y + padding.bottom - requisition.height + 1;
4771 y = CLAMP (y, monitor.y,
4772 monitor.y + monitor.height - requisition.height);
4774 else if (needed_height > space_below && needed_height > space_above)
4776 if (space_below >= space_above)
4777 y = monitor.y + monitor.height - requisition.height;
4789 if (y + requisition.height > monitor.y + monitor.height)
4791 if (priv->initially_pushed_in)
4792 scroll_offset += (monitor.y + monitor.height) - requisition.height - y;
4793 y = (monitor.y + monitor.height) - requisition.height;
4798 if (priv->initially_pushed_in)
4799 scroll_offset += monitor.y - y;
4803 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4805 if (GTK_MENU_SHELL (menu)->priv->active)
4807 priv->have_position = TRUE;
4808 priv->position_x = x;
4809 priv->position_y = y;
4812 if (scroll_offset != 0)
4814 GtkBorder arrow_border;
4816 get_arrows_border (menu, &arrow_border);
4817 scroll_offset += arrow_border.top;
4820 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4823 if (!GTK_MENU_SHELL (menu)->priv->active)
4825 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4826 requisition.width, requisition.height);
4829 if (set_scroll_offset)
4830 priv->scroll_offset = scroll_offset;
4834 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4836 GtkMenuPrivate *priv = menu->priv;
4838 if (priv->scroll_timeout)
4840 g_source_remove (priv->scroll_timeout);
4841 priv->scroll_timeout = 0;
4846 gtk_menu_stop_scrolling (GtkMenu *menu)
4848 GtkMenuPrivate *priv = menu->priv;
4849 gboolean touchscreen_mode;
4851 gtk_menu_remove_scroll_timeout (menu);
4853 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4854 "gtk-touchscreen-mode", &touchscreen_mode,
4857 if (!touchscreen_mode)
4859 priv->upper_arrow_prelight = FALSE;
4860 priv->lower_arrow_prelight = FALSE;
4865 gtk_menu_scroll_to (GtkMenu *menu,
4868 GtkMenuPrivate *priv = menu->priv;
4869 GtkBorder arrow_border, padding;
4872 gint view_width, view_height;
4875 guint vertical_padding;
4876 guint horizontal_padding;
4877 gboolean double_arrows;
4879 widget = GTK_WIDGET (menu);
4881 if (priv->tearoff_active && priv->tearoff_adjustment)
4882 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4884 /* Move/resize the viewport according to arrows: */
4885 view_width = gtk_widget_get_allocated_width (widget);
4886 view_height = gtk_widget_get_allocated_height (widget);
4888 gtk_widget_style_get (GTK_WIDGET (menu),
4889 "vertical-padding", &vertical_padding,
4890 "horizontal-padding", &horizontal_padding,
4893 get_menu_padding (widget, &padding);
4894 double_arrows = get_double_arrows (menu);
4896 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4898 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4899 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4900 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4901 padding.top - padding.bottom;
4903 x = border_width + padding.left + horizontal_padding;
4904 y = border_width + padding.top + vertical_padding;
4906 if (double_arrows && !priv->tearoff_active)
4908 if (view_height < menu_height ||
4909 (offset > 0 && priv->scroll_offset > 0) ||
4910 (offset < 0 && priv->scroll_offset < 0))
4912 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4913 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4915 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4916 gtk_widget_queue_draw (GTK_WIDGET (menu));
4918 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4920 get_arrows_border (menu, &arrow_border);
4921 y += arrow_border.top;
4922 view_height -= arrow_border.top;
4923 view_height -= arrow_border.bottom;
4926 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4929 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4931 if (priv->upper_arrow_prelight)
4932 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4934 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4937 if (offset >= menu_height - view_height)
4938 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4941 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4943 if (priv->lower_arrow_prelight)
4944 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4946 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4949 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4950 (priv->lower_arrow_state != lower_arrow_previous_state))
4951 gtk_widget_queue_draw (GTK_WIDGET (menu));
4953 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4954 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4956 /* At the upper border, possibly remove timeout */
4957 if (priv->scroll_step < 0)
4959 gtk_menu_stop_scrolling (menu);
4960 gtk_widget_queue_draw (GTK_WIDGET (menu));
4964 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4965 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4967 /* At the lower border, possibly remove timeout */
4968 if (priv->scroll_step > 0)
4970 gtk_menu_stop_scrolling (menu);
4971 gtk_widget_queue_draw (GTK_WIDGET (menu));
4975 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4979 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4980 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4982 gtk_menu_stop_scrolling (menu);
4983 gtk_widget_queue_draw (GTK_WIDGET (menu));
4986 else if (!priv->tearoff_active)
4988 gboolean last_visible;
4990 last_visible = priv->upper_arrow_visible;
4991 priv->upper_arrow_visible = offset > 0;
4993 /* upper_arrow_visible may have changed, so requery the border */
4994 get_arrows_border (menu, &arrow_border);
4995 view_height -= arrow_border.top;
4997 if ((last_visible != priv->upper_arrow_visible) &&
4998 !priv->upper_arrow_visible)
5000 priv->upper_arrow_prelight = FALSE;
5002 /* If we hide the upper arrow, possibly remove timeout */
5003 if (priv->scroll_step < 0)
5005 gtk_menu_stop_scrolling (menu);
5006 gtk_widget_queue_draw (GTK_WIDGET (menu));
5010 last_visible = priv->lower_arrow_visible;
5011 priv->lower_arrow_visible = offset < menu_height - view_height;
5013 /* lower_arrow_visible may have changed, so requery the border */
5014 get_arrows_border (menu, &arrow_border);
5015 view_height -= arrow_border.bottom;
5017 if ((last_visible != priv->lower_arrow_visible) &&
5018 !priv->lower_arrow_visible)
5020 priv->lower_arrow_prelight = FALSE;
5022 /* If we hide the lower arrow, possibly remove timeout */
5023 if (priv->scroll_step > 0)
5025 gtk_menu_stop_scrolling (menu);
5026 gtk_widget_queue_draw (GTK_WIDGET (menu));
5030 y += arrow_border.top;
5033 /* Scroll the menu: */
5034 if (gtk_widget_get_realized (widget))
5035 gdk_window_move (priv->bin_window, 0, -offset);
5037 if (gtk_widget_get_realized (widget))
5038 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5040 priv->scroll_offset = offset;
5044 compute_child_offset (GtkMenu *menu,
5045 GtkWidget *menu_item,
5048 gboolean *is_last_child)
5050 GtkMenuPrivate *priv = menu->priv;
5051 gint item_top_attach;
5052 gint item_bottom_attach;
5053 gint child_offset = 0;
5056 get_effective_child_attach (menu_item, NULL, NULL,
5057 &item_top_attach, &item_bottom_attach);
5059 /* there is a possibility that we get called before _size_request,
5060 * so check the height table for safety.
5062 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5065 /* when we have a row with only invisible children, its height will
5066 * be zero, so there's no need to check WIDGET_VISIBLE here
5068 for (i = 0; i < item_top_attach; i++)
5069 child_offset += priv->heights[i];
5072 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5074 *offset = child_offset;
5076 *height = priv->heights[item_top_attach];
5082 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5083 GtkWidget *menu_item)
5085 GtkMenu *menu = GTK_MENU (menu_shell);
5086 GtkMenuPrivate *priv = menu->priv;
5087 GtkWidget *widget = GTK_WIDGET (menu_shell);
5088 gint child_offset, child_height;
5092 gboolean last_child = 0;
5094 /* We need to check if the selected item fully visible.
5095 * If not we need to scroll the menu so that it becomes fully
5098 if (compute_child_offset (menu, menu_item,
5099 &child_offset, &child_height, &last_child))
5101 guint vertical_padding;
5102 gboolean double_arrows;
5105 y = priv->scroll_offset;
5106 height = gdk_window_get_height (gtk_widget_get_window (widget));
5108 gtk_widget_style_get (widget,
5109 "vertical-padding", &vertical_padding,
5112 double_arrows = get_double_arrows (menu);
5113 get_menu_padding (widget, &padding);
5115 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5116 padding.top + padding.bottom +
5117 2 * vertical_padding;
5118 if (child_offset < y)
5120 /* Ignore the enter event we might get if the pointer
5123 menu_shell->priv->ignore_enter = TRUE;
5124 gtk_menu_scroll_to (menu, child_offset);
5128 GtkBorder arrow_border;
5132 get_arrows_border (menu, &arrow_border);
5133 if (!priv->tearoff_active)
5134 arrow_height = arrow_border.top + arrow_border.bottom;
5136 if (child_offset + child_height > y + height - arrow_height)
5139 if ((!last_child && !priv->tearoff_active) || double_arrows)
5140 arrow_height += arrow_border.bottom;
5142 y = child_offset + child_height - height + arrow_height;
5143 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5145 /* Need upper arrow */
5146 arrow_height += arrow_border.top;
5147 y = child_offset + child_height - height + arrow_height;
5149 /* Ignore the enter event we might get if the pointer
5152 menu_shell->priv->ignore_enter = TRUE;
5153 gtk_menu_scroll_to (menu, y);
5160 gtk_menu_select_item (GtkMenuShell *menu_shell,
5161 GtkWidget *menu_item)
5163 GtkMenu *menu = GTK_MENU (menu_shell);
5165 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5166 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5168 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5172 /* Reparent the menu, taking care of the refcounting
5174 * If unrealize is true we force a unrealize while reparenting the parent.
5175 * This can help eliminate flicker in some cases.
5177 * What happens is that when the menu is unrealized and then re-realized,
5178 * the allocations are as follows:
5180 * parent - 1x1 at (0,0)
5181 * child1 - 100x20 at (0,0)
5182 * child2 - 100x20 at (0,20)
5183 * child3 - 100x20 at (0,40)
5185 * That is, the parent is small but the children are full sized. Then,
5186 * when the queued_resize gets processed, the parent gets resized to
5189 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5190 * contains the following logic:
5192 * - if a move or resize operation on a window would change the clip
5193 * region on the children, then before the window is resized
5194 * the background for children is temporarily set to None, the
5195 * move/resize done, and the background for the children restored.
5197 * So, at the point where the parent is resized to final size, the
5198 * background for the children is temporarily None, and thus they
5199 * are not cleared to the background color and the previous background
5200 * (the image of the menu) is left in place.
5203 gtk_menu_reparent (GtkMenu *menu,
5204 GtkWidget *new_parent,
5207 GObject *object = G_OBJECT (menu);
5208 GtkWidget *widget = GTK_WIDGET (menu);
5209 gboolean was_floating = g_object_is_floating (object);
5211 g_object_ref_sink (object);
5215 g_object_ref (object);
5216 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5217 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5218 g_object_unref (object);
5221 gtk_widget_reparent (widget, new_parent);
5224 g_object_force_floating (object);
5226 g_object_unref (object);
5230 gtk_menu_show_all (GtkWidget *widget)
5232 /* Show children, but not self. */
5233 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5237 * gtk_menu_set_screen:
5239 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5240 * determined by the widget the menu is attached to
5242 * Sets the #GdkScreen on which the menu will be displayed.
5247 gtk_menu_set_screen (GtkMenu *menu,
5250 g_return_if_fail (GTK_IS_MENU (menu));
5251 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5253 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5257 menu_change_screen (menu, screen);
5261 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5263 attach_widget_screen_changed (attach_widget, NULL, menu);
5270 * @child: a #GtkMenuItem
5271 * @left_attach: The column number to attach the left side of the item to
5272 * @right_attach: The column number to attach the right side of the item to
5273 * @top_attach: The row number to attach the top of the item to
5274 * @bottom_attach: The row number to attach the bottom of the item to
5276 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5277 * an item will occupy is specified by @left_attach, @right_attach,
5278 * @top_attach and @bottom_attach. These each represent the leftmost,
5279 * rightmost, uppermost and lower column and row numbers of the table.
5280 * (Columns and rows are indexed from zero).
5282 * Note that this function is not related to gtk_menu_detach().
5287 gtk_menu_attach (GtkMenu *menu,
5292 guint bottom_attach)
5294 GtkMenuShell *menu_shell;
5297 g_return_if_fail (GTK_IS_MENU (menu));
5298 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5299 parent = gtk_widget_get_parent (child);
5300 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5301 g_return_if_fail (left_attach < right_attach);
5302 g_return_if_fail (top_attach < bottom_attach);
5304 menu_shell = GTK_MENU_SHELL (menu);
5308 AttachInfo *ai = get_attach_info (child);
5310 ai->left_attach = left_attach;
5311 ai->right_attach = right_attach;
5312 ai->top_attach = top_attach;
5313 ai->bottom_attach = bottom_attach;
5315 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5317 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5319 menu_queue_resize (menu);
5323 gtk_container_child_set (GTK_CONTAINER (parent), child,
5324 "left-attach", left_attach,
5325 "right-attach", right_attach,
5326 "top-attach", top_attach,
5327 "bottom-attach", bottom_attach,
5333 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5337 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5338 "gtk-menu-popup-delay", &popup_delay,
5345 find_child_containing (GtkMenuShell *menu_shell,
5353 /* find a child which includes the area given by
5354 * left, right, top, bottom.
5356 for (list = menu_shell->priv->children; list; list = list->next)
5360 if (!_gtk_menu_item_is_selectable (list->data))
5363 get_effective_child_attach (list->data, &l, &r, &t, &b);
5365 if (l <= left && right <= r && t <= top && bottom <= b)
5366 return GTK_WIDGET (list->data);
5373 gtk_menu_move_current (GtkMenuShell *menu_shell,
5374 GtkMenuDirectionType direction)
5376 GtkMenu *menu = GTK_MENU (menu_shell);
5379 GtkWidget *match = NULL;
5381 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5385 case GTK_MENU_DIR_CHILD:
5386 direction = GTK_MENU_DIR_PARENT;
5388 case GTK_MENU_DIR_PARENT:
5389 direction = GTK_MENU_DIR_CHILD;
5395 /* use special table menu key bindings */
5396 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5398 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5400 if (direction == GTK_MENU_DIR_NEXT)
5402 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5404 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5412 for (i = 0; i < t; i++)
5414 match = find_child_containing (menu_shell,
5415 l, l + 1, i, i + 1);
5421 else if (direction == GTK_MENU_DIR_PREV)
5423 for (i = t; i > 0; i--)
5425 match = find_child_containing (menu_shell,
5426 l, l + 1, i - 1, i);
5434 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5436 match = find_child_containing (menu_shell,
5437 l, l + 1, i - 1, i);
5443 else if (direction == GTK_MENU_DIR_PARENT)
5445 /* we go one left if possible */
5447 match = find_child_containing (menu_shell,
5448 l - 1, l, t, t + 1);
5452 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5455 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5456 match = menu_shell->priv->active_menu_item;
5459 else if (direction == GTK_MENU_DIR_CHILD)
5461 /* we go one right if possible */
5462 if (r < gtk_menu_get_n_columns (menu))
5463 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5467 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5469 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5471 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5472 match = menu_shell->priv->active_menu_item;
5478 gtk_menu_shell_select_item (menu_shell, match);
5483 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5487 get_visible_size (GtkMenu *menu)
5489 GtkMenuPrivate *priv = menu->priv;
5490 GtkAllocation allocation;
5491 GtkWidget *widget = GTK_WIDGET (menu);
5492 GtkContainer *container = GTK_CONTAINER (menu);
5496 gtk_widget_get_allocation (widget, &allocation);
5497 get_menu_padding (widget, &padding);
5499 menu_height = (allocation.height -
5500 (2 * gtk_container_get_border_width (container)) -
5501 padding.top - padding.bottom);
5503 if (!priv->tearoff_active)
5505 GtkBorder arrow_border;
5507 get_arrows_border (menu, &arrow_border);
5508 menu_height -= arrow_border.top;
5509 menu_height -= arrow_border.bottom;
5515 /* Find the sensitive on-screen child containing @y, or if none,
5516 * the nearest selectable onscreen child. (%NULL if none)
5519 child_at (GtkMenu *menu,
5522 GtkMenuPrivate *priv = menu->priv;
5523 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5524 GtkWidget *child = NULL;
5525 gint child_offset = 0;
5528 gint lower, upper; /* Onscreen bounds */
5530 menu_height = get_visible_size (menu);
5531 lower = priv->scroll_offset;
5532 upper = priv->scroll_offset + menu_height;
5534 for (children = menu_shell->priv->children; children; children = children->next)
5536 if (gtk_widget_get_visible (children->data))
5538 GtkRequisition child_requisition;
5540 gtk_widget_get_preferred_size (children->data,
5541 &child_requisition, NULL);
5543 if (_gtk_menu_item_is_selectable (children->data) &&
5544 child_offset >= lower &&
5545 child_offset + child_requisition.height <= upper)
5547 child = children->data;
5549 if (child_offset + child_requisition.height > y &&
5550 !GTK_IS_TEAROFF_MENU_ITEM (child))
5554 child_offset += child_requisition.height;
5562 get_menu_height (GtkMenu *menu)
5564 GtkMenuPrivate *priv = menu->priv;
5565 GtkAllocation allocation;
5566 GtkWidget *widget = GTK_WIDGET (menu);
5570 gtk_widget_get_allocation (widget, &allocation);
5571 get_menu_padding (widget, &padding);
5573 height = allocation.height;
5574 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5575 padding.top + padding.bottom;
5577 if (!priv->tearoff_active)
5579 GtkBorder arrow_border;
5581 get_arrows_border (menu, &arrow_border);
5582 height -= arrow_border.top;
5583 height -= arrow_border.bottom;
5590 gtk_menu_real_move_scroll (GtkMenu *menu,
5593 GtkMenuPrivate *priv = menu->priv;
5594 gint page_size = get_visible_size (menu);
5595 gint end_position = get_menu_height (menu);
5596 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5600 case GTK_SCROLL_PAGE_UP:
5601 case GTK_SCROLL_PAGE_DOWN:
5605 gint child_offset = 0;
5606 gboolean old_upper_arrow_visible;
5609 if (type == GTK_SCROLL_PAGE_UP)
5614 if (menu_shell->priv->active_menu_item)
5618 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5619 &child_offset, &child_height, NULL);
5620 child_offset += child_height / 2;
5623 menu_shell->priv->ignore_enter = TRUE;
5624 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5625 old_offset = priv->scroll_offset;
5627 new_offset = priv->scroll_offset + step;
5628 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5630 gtk_menu_scroll_to (menu, new_offset);
5632 if (menu_shell->priv->active_menu_item)
5634 GtkWidget *new_child;
5635 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5636 GtkBorder arrow_border;
5637 get_arrows_border (menu, &arrow_border);
5639 if (priv->scroll_offset != old_offset)
5640 step = priv->scroll_offset - old_offset;
5642 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5644 new_child = child_at (menu, child_offset + step);
5646 gtk_menu_shell_select_item (menu_shell, new_child);
5650 case GTK_SCROLL_START:
5651 /* Ignore the enter event we might get if the pointer is on the menu */
5652 menu_shell->priv->ignore_enter = TRUE;
5653 gtk_menu_scroll_to (menu, 0);
5654 gtk_menu_shell_select_first (menu_shell, TRUE);
5656 case GTK_SCROLL_END:
5657 /* Ignore the enter event we might get if the pointer is on the menu */
5658 menu_shell->priv->ignore_enter = TRUE;
5659 gtk_menu_scroll_to (menu, end_position - page_size);
5660 _gtk_menu_shell_select_last (menu_shell, TRUE);
5669 * gtk_menu_set_monitor:
5671 * @monitor_num: the number of the monitor on which the menu should
5674 * Informs GTK+ on which monitor a menu should be popped up.
5675 * See gdk_screen_get_monitor_geometry().
5677 * This function should be called from a #GtkMenuPositionFunc
5678 * if the menu should not appear on the same monitor as the pointer.
5679 * This information can't be reliably inferred from the coordinates
5680 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5681 * these coordinates may extend beyond the monitor boundaries or even
5682 * the screen boundaries.
5687 gtk_menu_set_monitor (GtkMenu *menu,
5690 GtkMenuPrivate *priv = menu->priv;
5692 g_return_if_fail (GTK_IS_MENU (menu));
5694 priv->monitor_num = monitor_num;
5698 * gtk_menu_get_monitor:
5701 * Retrieves the number of the monitor on which to show the menu.
5703 * Returns: the number of the monitor on which the menu should
5704 * be popped up or -1, if no monitor has been set
5709 gtk_menu_get_monitor (GtkMenu *menu)
5711 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5713 return menu->priv->monitor_num;
5717 * gtk_menu_get_for_attach_widget:
5718 * @widget: a #GtkWidget
5720 * Returns a list of the menus which are attached to this widget.
5721 * This list is owned by GTK+ and must not be modified.
5723 * Return value: (element-type GtkWidget) (transfer none): the list
5724 * of menus attached to his widget.
5729 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5733 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5735 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5741 gtk_menu_grab_notify (GtkWidget *widget,
5742 gboolean was_grabbed)
5744 GtkWidget *toplevel;
5745 GtkWindowGroup *group;
5749 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5752 !gtk_widget_device_is_shadowed (widget, pointer))
5755 toplevel = gtk_widget_get_toplevel (widget);
5757 if (!GTK_IS_WINDOW (toplevel))
5760 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5761 grab = gtk_window_group_get_current_device_grab (group, pointer);
5763 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5764 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5768 * gtk_menu_set_reserve_toggle_size:
5770 * @reserve_toggle_size: whether to reserve size for toggles
5772 * Sets whether the menu should reserve space for drawing toggles
5773 * or icons, regardless of their actual presence.
5778 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5779 gboolean reserve_toggle_size)
5781 GtkMenuPrivate *priv = menu->priv;
5782 gboolean no_toggle_size;
5784 g_return_if_fail (GTK_IS_MENU (menu));
5786 no_toggle_size = !reserve_toggle_size;
5788 if (priv->no_toggle_size != no_toggle_size)
5790 priv->no_toggle_size = no_toggle_size;
5792 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5797 * gtk_menu_get_reserve_toggle_size:
5800 * Returns whether the menu reserves space for toggles and
5801 * icons, regardless of their actual presence.
5803 * Returns: Whether the menu reserves toggle space
5808 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5810 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5812 return !menu->priv->no_toggle_size;