1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 * @Short_description: A menu widget
32 * A #GtkMenu is a #GtkMenuShell that implements a drop down menu
33 * consisting of a list of #GtkMenuItem objects which can be navigated
34 * and activated by the user to perform application functions.
36 * A #GtkMenu is most commonly dropped down by activating a
37 * #GtkMenuItem in a #GtkMenuBar or popped up by activating a
38 * #GtkMenuItem in another #GtkMenu.
40 * A #GtkMenu can also be popped up by activating a #GtkOptionMenu.
41 * Other composite widgets such as the #GtkNotebook can pop up a
44 * Applications can display a #GtkMenu as a popup menu by calling the
45 * gtk_menu_popup() function. The example below shows how an application
46 * can pop up a menu when the 3rd mouse button is pressed.
49 * <title>Connecting the popup signal handler.</title>
51 * /<!---->* connect our handler which will popup the menu *<!---->/
52 * g_signal_connect_swapped (window, "button_press_event",
53 * G_CALLBACK (my_popup_handler), menu);
58 * <title>Signal handler which displays a popup menu.</title>
61 * my_popup_handler (GtkWidget *widget, GdkEvent *event)
64 * GdkEventButton *event_button;
66 * g_return_val_if_fail (widget != NULL, FALSE);
67 * g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
68 * g_return_val_if_fail (event != NULL, FALSE);
70 * /<!---->* The "widget" is the menu that was supplied when
71 * * g_signal_connect_swapped() was called.
73 * menu = GTK_MENU (widget);
75 * if (event->type == GDK_BUTTON_PRESS)
77 * event_button = (GdkEventButton *) event;
78 * if (event_button->button == 3)
80 * gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
81 * event_button->button, event_button->time);
96 #include <gobject/gvaluecollector.h>
98 #include "gtkaccellabel.h"
99 #include "gtkaccelmap.h"
100 #include "gtkbindings.h"
101 #include "gtkcheckmenuitem.h"
103 #include "gtkmarshalers.h"
104 #include "gtkmenuprivate.h"
105 #include "gtkmenuitemprivate.h"
106 #include "gtkmenushellprivate.h"
107 #include "gtktearoffmenuitem.h"
108 #include "gtkwindow.h"
110 #include "gtkvscrollbar.h"
111 #include "gtksettings.h"
112 #include "gtkprivate.h"
114 #include "gtktypebuiltins.h"
116 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
117 * extends below the submenu
120 #define MENU_SCROLL_STEP1 8
121 #define MENU_SCROLL_STEP2 15
122 #define MENU_SCROLL_FAST_ZONE 8
123 #define MENU_SCROLL_TIMEOUT1 50
124 #define MENU_SCROLL_TIMEOUT2 20
126 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
127 #define ATTACHED_MENUS "gtk-attached-menus"
129 typedef struct _GtkMenuAttachData GtkMenuAttachData;
130 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
132 struct _GtkMenuAttachData
134 GtkWidget *attach_widget;
135 GtkMenuDetachFunc detacher;
138 struct _OldGtkMenuPrivate
142 gboolean initially_pushed_in;
144 GDestroyNotify position_func_data_destroy;
146 /* info used for the table */
149 gint requested_height;
153 /* Cached layout information */
162 GtkStateFlags lower_arrow_state;
163 GtkStateFlags upper_arrow_state;
165 /* navigation region */
168 int navigation_width;
169 int navigation_height;
171 guint have_layout : 1;
172 guint seen_item_enter : 1;
173 guint have_position : 1;
174 guint ignore_button_release : 1;
175 guint no_toggle_size : 1;
178 struct _GtkMenuPopdownData
190 gint effective_left_attach;
191 gint effective_right_attach;
192 gint effective_top_attach;
193 gint effective_bottom_attach;
210 PROP_RESERVE_TOGGLE_SIZE
215 CHILD_PROP_LEFT_ATTACH,
216 CHILD_PROP_RIGHT_ATTACH,
217 CHILD_PROP_TOP_ATTACH,
218 CHILD_PROP_BOTTOM_ATTACH
221 static void gtk_menu_set_property (GObject *object,
225 static void gtk_menu_get_property (GObject *object,
229 static void gtk_menu_set_child_property(GtkContainer *container,
234 static void gtk_menu_get_child_property(GtkContainer *container,
239 static void gtk_menu_destroy (GtkWidget *widget);
240 static void gtk_menu_realize (GtkWidget *widget);
241 static void gtk_menu_unrealize (GtkWidget *widget);
242 static void gtk_menu_size_allocate (GtkWidget *widget,
243 GtkAllocation *allocation);
244 static void gtk_menu_show (GtkWidget *widget);
245 static gboolean gtk_menu_draw (GtkWidget *widget,
247 static gboolean gtk_menu_key_press (GtkWidget *widget,
249 static gboolean gtk_menu_scroll (GtkWidget *widget,
250 GdkEventScroll *event);
251 static gboolean gtk_menu_button_press (GtkWidget *widget,
252 GdkEventButton *event);
253 static gboolean gtk_menu_button_release (GtkWidget *widget,
254 GdkEventButton *event);
255 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
256 GdkEventMotion *event);
257 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
258 GdkEventCrossing *event);
259 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
260 GdkEventCrossing *event);
261 static void gtk_menu_scroll_to (GtkMenu *menu,
263 static void gtk_menu_grab_notify (GtkWidget *widget,
264 gboolean was_grabbed);
266 static void gtk_menu_stop_scrolling (GtkMenu *menu);
267 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
268 static gboolean gtk_menu_scroll_timeout (gpointer data);
269 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
270 static void gtk_menu_start_scrolling (GtkMenu *menu);
272 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
273 GtkWidget *menu_item);
274 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
275 GtkWidget *menu_item);
276 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
279 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
281 static void gtk_menu_handle_scrolling (GtkMenu *menu,
286 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
288 static void gtk_menu_style_updated (GtkWidget *widget);
289 static gboolean gtk_menu_focus (GtkWidget *widget,
290 GtkDirectionType direction);
291 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
292 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
293 GtkMenuDirectionType direction);
294 static void gtk_menu_real_move_scroll (GtkMenu *menu,
297 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
298 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
299 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
302 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
303 GtkMenuItem *menu_item,
304 GdkEventCrossing *event);
306 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
307 static void gtk_menu_show_all (GtkWidget *widget);
308 static void gtk_menu_position (GtkMenu *menu,
309 gboolean set_scroll_offset);
310 static void gtk_menu_reparent (GtkMenu *menu,
311 GtkWidget *new_parent,
313 static void gtk_menu_remove (GtkContainer *menu,
316 static void gtk_menu_update_title (GtkMenu *menu);
318 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
319 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
321 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
323 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
324 gboolean group_changed);
326 static void gtk_menu_get_preferred_width (GtkWidget *widget,
329 static void gtk_menu_get_preferred_height (GtkWidget *widget,
332 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
338 static const gchar attach_data_key[] = "gtk-menu-attach-data";
340 static guint menu_signals[LAST_SIGNAL] = { 0 };
342 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
345 menu_queue_resize (GtkMenu *menu)
347 GtkMenuPrivate *priv = menu->priv;
349 priv->have_layout = FALSE;
350 gtk_widget_queue_resize (GTK_WIDGET (menu));
354 attach_info_free (AttachInfo *info)
356 g_slice_free (AttachInfo, info);
360 get_attach_info (GtkWidget *child)
362 GObject *object = G_OBJECT (child);
363 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
367 ai = g_slice_new0 (AttachInfo);
368 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
369 (GDestroyNotify) attach_info_free);
376 is_grid_attached (AttachInfo *ai)
378 return (ai->left_attach >= 0 &&
379 ai->right_attach >= 0 &&
380 ai->top_attach >= 0 &&
381 ai->bottom_attach >= 0);
385 menu_ensure_layout (GtkMenu *menu)
387 GtkMenuPrivate *priv = menu->priv;
389 if (!priv->have_layout)
391 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
395 gint max_right_attach;
396 gint max_bottom_attach;
398 /* Find extents of gridded portion
400 max_right_attach = 1;
401 max_bottom_attach = 0;
403 for (l = menu_shell->priv->children; l; l = l->next)
405 GtkWidget *child = l->data;
406 AttachInfo *ai = get_attach_info (child);
408 if (is_grid_attached (ai))
410 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
411 max_right_attach = MAX (max_right_attach, ai->right_attach);
415 /* Find empty rows */
416 row_occupied = g_malloc0 (max_bottom_attach);
418 for (l = menu_shell->priv->children; l; l = l->next)
420 GtkWidget *child = l->data;
421 AttachInfo *ai = get_attach_info (child);
423 if (is_grid_attached (ai))
427 for (i = ai->top_attach; i < ai->bottom_attach; i++)
428 row_occupied[i] = TRUE;
432 /* Lay non-grid-items out in those rows
435 for (l = menu_shell->priv->children; l; l = l->next)
437 GtkWidget *child = l->data;
438 AttachInfo *ai = get_attach_info (child);
440 if (!is_grid_attached (ai))
442 while (current_row < max_bottom_attach && row_occupied[current_row])
445 ai->effective_left_attach = 0;
446 ai->effective_right_attach = max_right_attach;
447 ai->effective_top_attach = current_row;
448 ai->effective_bottom_attach = current_row + 1;
454 ai->effective_left_attach = ai->left_attach;
455 ai->effective_right_attach = ai->right_attach;
456 ai->effective_top_attach = ai->top_attach;
457 ai->effective_bottom_attach = ai->bottom_attach;
461 g_free (row_occupied);
463 priv->n_rows = MAX (current_row, max_bottom_attach);
464 priv->n_columns = max_right_attach;
465 priv->have_layout = TRUE;
471 gtk_menu_get_n_columns (GtkMenu *menu)
473 GtkMenuPrivate *priv = menu->priv;
475 menu_ensure_layout (menu);
477 return priv->n_columns;
481 gtk_menu_get_n_rows (GtkMenu *menu)
483 GtkMenuPrivate *priv = menu->priv;
485 menu_ensure_layout (menu);
491 get_effective_child_attach (GtkWidget *child,
497 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
500 menu_ensure_layout (menu);
502 ai = get_attach_info (child);
505 *l = ai->effective_left_attach;
507 *r = ai->effective_right_attach;
509 *t = ai->effective_top_attach;
511 *b = ai->effective_bottom_attach;
516 gtk_menu_class_init (GtkMenuClass *class)
518 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
519 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
520 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
521 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
522 GtkBindingSet *binding_set;
524 gobject_class->set_property = gtk_menu_set_property;
525 gobject_class->get_property = gtk_menu_get_property;
527 widget_class->destroy = gtk_menu_destroy;
528 widget_class->realize = gtk_menu_realize;
529 widget_class->unrealize = gtk_menu_unrealize;
530 widget_class->size_allocate = gtk_menu_size_allocate;
531 widget_class->show = gtk_menu_show;
532 widget_class->draw = gtk_menu_draw;
533 widget_class->scroll_event = gtk_menu_scroll;
534 widget_class->key_press_event = gtk_menu_key_press;
535 widget_class->button_press_event = gtk_menu_button_press;
536 widget_class->button_release_event = gtk_menu_button_release;
537 widget_class->motion_notify_event = gtk_menu_motion_notify;
538 widget_class->show_all = gtk_menu_show_all;
539 widget_class->enter_notify_event = gtk_menu_enter_notify;
540 widget_class->leave_notify_event = gtk_menu_leave_notify;
541 widget_class->style_updated = gtk_menu_style_updated;
542 widget_class->focus = gtk_menu_focus;
543 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
544 widget_class->grab_notify = gtk_menu_grab_notify;
545 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
546 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
547 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
549 container_class->remove = gtk_menu_remove;
550 container_class->get_child_property = gtk_menu_get_child_property;
551 container_class->set_child_property = gtk_menu_set_child_property;
553 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
554 menu_shell_class->deactivate = gtk_menu_deactivate;
555 menu_shell_class->select_item = gtk_menu_select_item;
556 menu_shell_class->insert = gtk_menu_real_insert;
557 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
558 menu_shell_class->move_current = gtk_menu_move_current;
561 * GtkMenu::move-scroll:
563 * @scroll_type: a #GtkScrollType
565 menu_signals[MOVE_SCROLL] =
566 g_signal_new_class_handler (I_("move-scroll"),
567 G_OBJECT_CLASS_TYPE (gobject_class),
568 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
569 G_CALLBACK (gtk_menu_real_move_scroll),
571 _gtk_marshal_VOID__ENUM,
573 GTK_TYPE_SCROLL_TYPE);
578 * The index of the currently selected menu item, or -1 if no
579 * menu item is selected.
583 g_object_class_install_property (gobject_class,
585 g_param_spec_int ("active",
587 P_("The currently selected menu item"),
589 GTK_PARAM_READWRITE));
592 * GtkMenu:accel-group:
594 * The accel group holding accelerators for the menu.
598 g_object_class_install_property (gobject_class,
600 g_param_spec_object ("accel-group",
602 P_("The accel group holding accelerators for the menu"),
603 GTK_TYPE_ACCEL_GROUP,
604 GTK_PARAM_READWRITE));
607 * GtkMenu:accel-path:
609 * An accel path used to conveniently construct accel paths of child items.
613 g_object_class_install_property (gobject_class,
615 g_param_spec_string ("accel-path",
617 P_("An accel path used to conveniently construct accel paths of child items"),
619 GTK_PARAM_READWRITE));
622 * GtkMenu:attach-widget:
624 * The widget the menu is attached to. Setting this property attaches
625 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
626 * use gtk_menu_attach_to_widget() directly.
630 g_object_class_install_property (gobject_class,
632 g_param_spec_object ("attach-widget",
634 P_("The widget the menu is attached to"),
636 GTK_PARAM_READWRITE));
638 g_object_class_install_property (gobject_class,
640 g_param_spec_string ("tearoff-title",
642 P_("A title that may be displayed by the window manager when this menu is torn-off"),
644 GTK_PARAM_READWRITE));
647 * GtkMenu:tearoff-state:
649 * A boolean that indicates whether the menu is torn-off.
653 g_object_class_install_property (gobject_class,
655 g_param_spec_boolean ("tearoff-state",
657 P_("A boolean that indicates whether the menu is torn-off"),
659 GTK_PARAM_READWRITE));
664 * The monitor the menu will be popped up on.
668 g_object_class_install_property (gobject_class,
670 g_param_spec_int ("monitor",
672 P_("The monitor the menu will be popped up on"),
674 GTK_PARAM_READWRITE));
676 gtk_widget_class_install_style_property (widget_class,
677 g_param_spec_int ("vertical-padding",
678 P_("Vertical Padding"),
679 P_("Extra space at the top and bottom of the menu"),
683 GTK_PARAM_READABLE));
686 * GtkMenu:reserve-toggle-size:
688 * A boolean that indicates whether the menu reserves space for
689 * toggles and icons, regardless of their actual presence.
691 * This property should only be changed from its default value
692 * for special-purposes such as tabular menus. Regular menus that
693 * are connected to a menu bar or context menus should reserve
694 * toggle space for consistency.
698 g_object_class_install_property (gobject_class,
699 PROP_RESERVE_TOGGLE_SIZE,
700 g_param_spec_boolean ("reserve-toggle-size",
701 P_("Reserve Toggle Size"),
702 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
704 GTK_PARAM_READWRITE));
706 gtk_widget_class_install_style_property (widget_class,
707 g_param_spec_int ("horizontal-padding",
708 P_("Horizontal Padding"),
709 P_("Extra space at the left and right edges of the menu"),
713 GTK_PARAM_READABLE));
715 gtk_widget_class_install_style_property (widget_class,
716 g_param_spec_int ("vertical-offset",
717 P_("Vertical Offset"),
718 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
722 GTK_PARAM_READABLE));
724 gtk_widget_class_install_style_property (widget_class,
725 g_param_spec_int ("horizontal-offset",
726 P_("Horizontal Offset"),
727 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
731 GTK_PARAM_READABLE));
733 gtk_widget_class_install_style_property (widget_class,
734 g_param_spec_boolean ("double-arrows",
736 P_("When scrolling, always show both arrows."),
738 GTK_PARAM_READABLE));
741 * GtkMenu:arrow-placement:
743 * Indicates where scroll arrows should be placed.
747 gtk_widget_class_install_style_property (widget_class,
748 g_param_spec_enum ("arrow-placement",
749 P_("Arrow Placement"),
750 P_("Indicates where scroll arrows should be placed"),
751 GTK_TYPE_ARROW_PLACEMENT,
753 GTK_PARAM_READABLE));
755 gtk_container_class_install_child_property (container_class,
756 CHILD_PROP_LEFT_ATTACH,
757 g_param_spec_int ("left-attach",
759 P_("The column number to attach the left side of the child to"),
761 GTK_PARAM_READWRITE));
763 gtk_container_class_install_child_property (container_class,
764 CHILD_PROP_RIGHT_ATTACH,
765 g_param_spec_int ("right-attach",
767 P_("The column number to attach the right side of the child to"),
769 GTK_PARAM_READWRITE));
771 gtk_container_class_install_child_property (container_class,
772 CHILD_PROP_TOP_ATTACH,
773 g_param_spec_int ("top-attach",
775 P_("The row number to attach the top of the child to"),
777 GTK_PARAM_READWRITE));
779 gtk_container_class_install_child_property (container_class,
780 CHILD_PROP_BOTTOM_ATTACH,
781 g_param_spec_int ("bottom-attach",
783 P_("The row number to attach the bottom of the child to"),
785 GTK_PARAM_READWRITE));
788 * GtkMenu:arrow-scaling
790 * Arbitrary constant to scale down the size of the scroll arrow.
794 gtk_widget_class_install_style_property (widget_class,
795 g_param_spec_float ("arrow-scaling",
797 P_("Arbitrary constant to scale down the size of the scroll arrow"),
799 GTK_PARAM_READABLE));
801 binding_set = gtk_binding_set_by_class (class);
802 gtk_binding_entry_add_signal (binding_set,
804 I_("move-current"), 1,
805 GTK_TYPE_MENU_DIRECTION_TYPE,
807 gtk_binding_entry_add_signal (binding_set,
810 GTK_TYPE_MENU_DIRECTION_TYPE,
812 gtk_binding_entry_add_signal (binding_set,
815 GTK_TYPE_MENU_DIRECTION_TYPE,
817 gtk_binding_entry_add_signal (binding_set,
820 GTK_TYPE_MENU_DIRECTION_TYPE,
822 gtk_binding_entry_add_signal (binding_set,
825 GTK_TYPE_MENU_DIRECTION_TYPE,
826 GTK_MENU_DIR_PARENT);
827 gtk_binding_entry_add_signal (binding_set,
830 GTK_TYPE_MENU_DIRECTION_TYPE,
831 GTK_MENU_DIR_PARENT);
832 gtk_binding_entry_add_signal (binding_set,
835 GTK_TYPE_MENU_DIRECTION_TYPE,
837 gtk_binding_entry_add_signal (binding_set,
840 GTK_TYPE_MENU_DIRECTION_TYPE,
842 gtk_binding_entry_add_signal (binding_set,
845 GTK_TYPE_SCROLL_TYPE,
847 gtk_binding_entry_add_signal (binding_set,
850 GTK_TYPE_SCROLL_TYPE,
852 gtk_binding_entry_add_signal (binding_set,
855 GTK_TYPE_SCROLL_TYPE,
857 gtk_binding_entry_add_signal (binding_set,
860 GTK_TYPE_SCROLL_TYPE,
862 gtk_binding_entry_add_signal (binding_set,
865 GTK_TYPE_SCROLL_TYPE,
867 gtk_binding_entry_add_signal (binding_set,
868 GDK_KEY_KP_Page_Up, 0,
870 GTK_TYPE_SCROLL_TYPE,
872 gtk_binding_entry_add_signal (binding_set,
873 GDK_KEY_Page_Down, 0,
875 GTK_TYPE_SCROLL_TYPE,
876 GTK_SCROLL_PAGE_DOWN);
877 gtk_binding_entry_add_signal (binding_set,
878 GDK_KEY_KP_Page_Down, 0,
880 GTK_TYPE_SCROLL_TYPE,
881 GTK_SCROLL_PAGE_DOWN);
883 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
888 gtk_menu_set_property (GObject *object,
893 GtkMenu *menu = GTK_MENU (object);
898 gtk_menu_set_active (menu, g_value_get_int (value));
900 case PROP_ACCEL_GROUP:
901 gtk_menu_set_accel_group (menu, g_value_get_object (value));
903 case PROP_ACCEL_PATH:
904 gtk_menu_set_accel_path (menu, g_value_get_string (value));
906 case PROP_ATTACH_WIDGET:
910 widget = gtk_menu_get_attach_widget (menu);
912 gtk_menu_detach (menu);
914 widget = (GtkWidget*) g_value_get_object (value);
916 gtk_menu_attach_to_widget (menu, widget, NULL);
919 case PROP_TEAROFF_STATE:
920 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
922 case PROP_TEAROFF_TITLE:
923 gtk_menu_set_title (menu, g_value_get_string (value));
926 gtk_menu_set_monitor (menu, g_value_get_int (value));
928 case PROP_RESERVE_TOGGLE_SIZE:
929 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
932 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
938 gtk_menu_get_property (GObject *object,
943 GtkMenu *menu = GTK_MENU (object);
948 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
950 case PROP_ACCEL_GROUP:
951 g_value_set_object (value, gtk_menu_get_accel_group (menu));
953 case PROP_ACCEL_PATH:
954 g_value_set_string (value, gtk_menu_get_accel_path (menu));
956 case PROP_ATTACH_WIDGET:
957 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
959 case PROP_TEAROFF_STATE:
960 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
962 case PROP_TEAROFF_TITLE:
963 g_value_set_string (value, gtk_menu_get_title (menu));
966 g_value_set_int (value, gtk_menu_get_monitor (menu));
968 case PROP_RESERVE_TOGGLE_SIZE:
969 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
972 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
978 gtk_menu_set_child_property (GtkContainer *container,
984 GtkMenu *menu = GTK_MENU (container);
985 AttachInfo *ai = get_attach_info (child);
989 case CHILD_PROP_LEFT_ATTACH:
990 ai->left_attach = g_value_get_int (value);
992 case CHILD_PROP_RIGHT_ATTACH:
993 ai->right_attach = g_value_get_int (value);
995 case CHILD_PROP_TOP_ATTACH:
996 ai->top_attach = g_value_get_int (value);
998 case CHILD_PROP_BOTTOM_ATTACH:
999 ai->bottom_attach = g_value_get_int (value);
1003 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1007 menu_queue_resize (menu);
1011 gtk_menu_get_child_property (GtkContainer *container,
1017 AttachInfo *ai = get_attach_info (child);
1019 switch (property_id)
1021 case CHILD_PROP_LEFT_ATTACH:
1022 g_value_set_int (value, ai->left_attach);
1024 case CHILD_PROP_RIGHT_ATTACH:
1025 g_value_set_int (value, ai->right_attach);
1027 case CHILD_PROP_TOP_ATTACH:
1028 g_value_set_int (value, ai->top_attach);
1030 case CHILD_PROP_BOTTOM_ATTACH:
1031 g_value_set_int (value, ai->bottom_attach);
1035 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1041 gtk_menu_window_event (GtkWidget *window,
1045 gboolean handled = FALSE;
1047 g_object_ref (window);
1048 g_object_ref (menu);
1050 switch (event->type)
1053 case GDK_KEY_RELEASE:
1054 handled = gtk_widget_event (menu, event);
1060 g_object_unref (window);
1061 g_object_unref (menu);
1067 gtk_menu_init (GtkMenu *menu)
1069 GtkMenuPrivate *priv;
1070 GtkStyleContext *context;
1072 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1076 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1077 "type", GTK_WINDOW_POPUP,
1080 "signal::event", gtk_menu_window_event, menu,
1081 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1083 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1084 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1086 /* Refloat the menu, so that reference counting for the menu isn't
1087 * affected by it being a child of the toplevel
1089 g_object_force_floating (G_OBJECT (menu));
1090 priv->needs_destruction_ref = TRUE;
1092 priv->monitor_num = -1;
1094 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1095 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1099 gtk_menu_destroy (GtkWidget *widget)
1101 GtkMenu *menu = GTK_MENU (widget);
1102 GtkMenuPrivate *priv = menu->priv;
1103 GtkMenuAttachData *data;
1105 gtk_menu_remove_scroll_timeout (menu);
1107 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1109 gtk_menu_detach (menu);
1111 gtk_menu_stop_navigating_submenu (menu);
1113 if (priv->old_active_menu_item)
1114 g_clear_object (&priv->old_active_menu_item);
1116 /* Add back the reference count for being a child */
1117 if (priv->needs_destruction_ref)
1119 priv->needs_destruction_ref = FALSE;
1120 g_object_ref (widget);
1123 if (priv->accel_group)
1124 g_clear_object (&priv->accel_group);
1127 gtk_widget_destroy (priv->toplevel);
1129 if (priv->tearoff_window)
1130 gtk_widget_destroy (priv->tearoff_window);
1134 g_free (priv->heights);
1135 priv->heights = NULL;
1140 g_free (priv->title);
1144 if (priv->position_func_data_destroy)
1146 priv->position_func_data_destroy (priv->position_func_data);
1147 priv->position_func_data = NULL;
1148 priv->position_func_data_destroy = NULL;
1151 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1155 menu_change_screen (GtkMenu *menu,
1156 GdkScreen *new_screen)
1158 GtkMenuPrivate *priv = menu->priv;
1160 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1162 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1168 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1169 gtk_menu_position (menu, TRUE);
1172 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1173 priv->monitor_num = -1;
1177 attach_widget_screen_changed (GtkWidget *attach_widget,
1178 GdkScreen *previous_screen,
1181 if (gtk_widget_has_screen (attach_widget) &&
1182 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1183 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1187 * gtk_menu_attach_to_widget:
1189 * @attach_widget: the #GtkWidget that the menu will be attached to
1190 * @detacher: (scope async)(allow-none): the user supplied callback function
1191 * that will be called when the menu calls gtk_menu_detach()
1193 * Attaches the menu to the widget and provides a callback function
1194 * that will be invoked when the menu calls gtk_menu_detach() during
1198 gtk_menu_attach_to_widget (GtkMenu *menu,
1199 GtkWidget *attach_widget,
1200 GtkMenuDetachFunc detacher)
1202 GtkMenuAttachData *data;
1205 g_return_if_fail (GTK_IS_MENU (menu));
1206 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1208 /* keep this function in sync with gtk_widget_set_parent() */
1209 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1212 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1213 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1217 g_object_ref_sink (menu);
1219 data = g_slice_new (GtkMenuAttachData);
1220 data->attach_widget = attach_widget;
1222 g_signal_connect (attach_widget, "screen-changed",
1223 G_CALLBACK (attach_widget_screen_changed), menu);
1224 attach_widget_screen_changed (attach_widget, NULL, menu);
1226 data->detacher = detacher;
1227 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1228 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1229 if (!g_list_find (list, menu))
1230 list = g_list_prepend (list, menu);
1232 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1233 (GDestroyNotify) g_list_free);
1235 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1236 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1238 /* we don't need to set the style here, since
1239 * we are a toplevel widget.
1242 /* Fallback title for menu comes from attach widget */
1243 gtk_menu_update_title (menu);
1245 g_object_notify (G_OBJECT (menu), "attach-widget");
1249 * gtk_menu_get_attach_widget:
1252 * Returns the #GtkWidget that the menu is attached to.
1254 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1257 gtk_menu_get_attach_widget (GtkMenu *menu)
1259 GtkMenuAttachData *data;
1261 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1263 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1265 return data->attach_widget;
1273 * Detaches the menu from the widget to which it had been attached.
1274 * This function will call the callback function, @detacher, provided
1275 * when the gtk_menu_attach_to_widget() function was called.
1278 gtk_menu_detach (GtkMenu *menu)
1280 GtkMenuAttachData *data;
1283 g_return_if_fail (GTK_IS_MENU (menu));
1285 /* keep this function in sync with gtk_widget_unparent() */
1286 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1289 g_warning ("gtk_menu_detach(): menu is not attached");
1292 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1294 g_signal_handlers_disconnect_by_func (data->attach_widget,
1295 (gpointer) attach_widget_screen_changed,
1299 data->detacher (data->attach_widget, menu);
1300 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1301 list = g_list_remove (list, menu);
1303 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1304 (GDestroyNotify) g_list_free);
1306 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1308 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1309 gtk_widget_unrealize (GTK_WIDGET (menu));
1311 g_slice_free (GtkMenuAttachData, data);
1313 /* Fallback title for menu comes from attach widget */
1314 gtk_menu_update_title (menu);
1316 g_object_unref (menu);
1320 gtk_menu_remove (GtkContainer *container,
1323 GtkMenu *menu = GTK_MENU (container);
1324 GtkMenuPrivate *priv = menu->priv;
1326 /* Clear out old_active_menu_item if it matches the item we are removing */
1327 if (priv->old_active_menu_item == widget)
1328 g_clear_object (&priv->old_active_menu_item);
1330 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1332 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1334 menu_queue_resize (menu);
1340 * Creates a new #GtkMenu
1342 * Returns: a new #GtkMenu
1347 return g_object_new (GTK_TYPE_MENU, NULL);
1351 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1355 GtkMenu *menu = GTK_MENU (menu_shell);
1356 GtkMenuPrivate *priv = menu->priv;
1357 AttachInfo *ai = get_attach_info (child);
1359 ai->left_attach = -1;
1360 ai->right_attach = -1;
1361 ai->top_attach = -1;
1362 ai->bottom_attach = -1;
1364 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1365 gtk_widget_set_parent_window (child, priv->bin_window);
1367 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1369 menu_queue_resize (menu);
1373 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1375 GtkMenuPrivate *priv = menu->priv;
1381 cairo_surface_t *surface;
1382 cairo_pattern_t *pattern;
1385 priv->tearoff_active = FALSE;
1386 priv->saved_scroll_offset = priv->scroll_offset;
1388 window = gtk_widget_get_window (priv->tearoff_window);
1389 width = gdk_window_get_width (window);
1390 height = gdk_window_get_height (window);
1392 surface = gdk_window_create_similar_surface (window,
1393 CAIRO_CONTENT_COLOR,
1397 cr = cairo_create (surface);
1398 gdk_cairo_set_source_window (cr, window, 0, 0);
1402 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1404 pattern = cairo_pattern_create_for_surface (surface);
1405 gdk_window_set_background_pattern (window, pattern);
1407 cairo_pattern_destroy (pattern);
1408 cairo_surface_destroy (surface);
1413 popup_grab_on_window (GdkWindow *window,
1414 GdkDevice *keyboard,
1416 guint32 activate_time)
1419 gdk_device_grab (keyboard, window,
1420 GDK_OWNERSHIP_WINDOW, TRUE,
1421 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1422 NULL, activate_time) != GDK_GRAB_SUCCESS)
1426 gdk_device_grab (pointer, window,
1427 GDK_OWNERSHIP_WINDOW, TRUE,
1428 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1429 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1430 GDK_POINTER_MOTION_MASK,
1431 NULL, activate_time) != GDK_GRAB_SUCCESS)
1434 gdk_device_ungrab (keyboard, activate_time);
1443 * gtk_menu_popup_for_device:
1445 * @device: (allow-none): a #GdkDevice
1446 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1447 * menu item, or %NULL
1448 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1449 * the popup, or %NULL
1450 * @func: (allow-none): a user supplied function used to position the menu,
1452 * @data: (allow-none): user supplied data to be passed to @func
1453 * @destroy: (allow-none): destroy notify for @data
1454 * @button: the mouse button which was pressed to initiate the event
1455 * @activate_time: the time at which the activation event occurred
1457 * Displays a menu and makes it available for selection.
1459 * Applications can use this function to display context-sensitive menus,
1460 * and will typically supply %NULL for the @parent_menu_shell,
1461 * @parent_menu_item, @func, @data and @destroy parameters. The default
1462 * menu positioning function will position the menu at the current position
1463 * of @device (or its corresponding pointer).
1465 * The @button parameter should be the mouse button pressed to initiate
1466 * the menu popup. If the menu popup was initiated by something other than
1467 * a mouse button press, such as a mouse button release or a keypress,
1468 * @button should be 0.
1470 * The @activate_time parameter is used to conflict-resolve initiation of
1471 * concurrent requests for mouse/keyboard grab requests. To function
1472 * properly, this needs to be the time stamp of the user event (such as
1473 * a mouse click or key press) that caused the initiation of the popup.
1474 * Only if no such event is available, gtk_get_current_event_time() can
1480 gtk_menu_popup_for_device (GtkMenu *menu,
1482 GtkWidget *parent_menu_shell,
1483 GtkWidget *parent_menu_item,
1484 GtkMenuPositionFunc func,
1486 GDestroyNotify destroy,
1488 guint32 activate_time)
1490 GtkMenuPrivate *priv = menu->priv;
1492 GtkWidget *xgrab_shell;
1494 GdkEvent *current_event;
1495 GtkMenuShell *menu_shell;
1496 gboolean grab_keyboard;
1497 GtkWidget *parent_toplevel;
1498 GdkDevice *keyboard, *pointer;
1500 g_return_if_fail (GTK_IS_MENU (menu));
1501 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1504 device = gtk_get_current_event_device ();
1508 GdkDisplay *display;
1509 GdkDeviceManager *device_manager;
1512 display = gtk_widget_get_display (GTK_WIDGET (menu));
1513 device_manager = gdk_display_get_device_manager (display);
1514 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1516 device = devices->data;
1518 g_list_free (devices);
1521 widget = GTK_WIDGET (menu);
1522 menu_shell = GTK_MENU_SHELL (menu);
1524 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1527 pointer = gdk_device_get_associated_device (device);
1532 keyboard = gdk_device_get_associated_device (device);
1535 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1537 priv->seen_item_enter = FALSE;
1539 /* Find the last viewable ancestor, and make an X grab on it
1541 parent = GTK_WIDGET (menu);
1545 gboolean viewable = TRUE;
1546 GtkWidget *tmp = parent;
1550 if (!gtk_widget_get_mapped (tmp))
1555 tmp = gtk_widget_get_parent (tmp);
1559 xgrab_shell = parent;
1561 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1564 /* We want to receive events generated when we map the menu;
1565 * unfortunately, since there is probably already an implicit
1566 * grab in place from the button that the user used to pop up
1567 * the menu, we won't receive then -- in particular, the EnterNotify
1568 * when the menu pops up under the pointer.
1570 * If we are grabbing on a parent menu shell, no problem; just grab
1571 * on that menu shell first before popping up the window with
1572 * owner_events = TRUE.
1574 * When grabbing on the menu itself, things get more convoluted --
1575 * we do an explicit grab on a specially created window with
1576 * owner_events = TRUE, which we override further down with a
1577 * grab on the menu. (We can't grab on the menu until it is mapped;
1578 * we probably could just leave the grab on the other window,
1579 * with a little reorganization of the code in gtkmenu*).
1581 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1582 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1587 if (xgrab_shell && xgrab_shell != widget)
1589 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1591 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1592 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1597 GdkWindow *transfer_window;
1599 xgrab_shell = widget;
1600 transfer_window = menu_grab_transfer_window_get (menu);
1601 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1603 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1604 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1608 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1610 /* We failed to make our pointer/keyboard grab.
1611 * Rather than leaving the user with a stuck up window,
1612 * we just abort here. Presumably the user will try again.
1614 menu_shell->priv->parent_menu_shell = NULL;
1615 menu_grab_transfer_window_destroy (menu);
1619 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1620 menu_shell->priv->active = TRUE;
1621 menu_shell->priv->button = button;
1623 /* If we are popping up the menu from something other than, a button
1624 * press then, as a heuristic, we ignore enter events for the menu
1625 * until we get a MOTION_NOTIFY.
1628 current_event = gtk_get_current_event ();
1631 if ((current_event->type != GDK_BUTTON_PRESS) &&
1632 (current_event->type != GDK_ENTER_NOTIFY))
1633 menu_shell->priv->ignore_enter = TRUE;
1635 gdk_event_free (current_event);
1638 menu_shell->priv->ignore_enter = TRUE;
1642 gtk_menu_tearoff_bg_copy (menu);
1644 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1647 parent_toplevel = NULL;
1648 if (parent_menu_shell)
1649 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1650 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1652 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1654 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1657 /* Set transient for to get the right window group and parent */
1658 if (GTK_IS_WINDOW (parent_toplevel))
1659 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1660 GTK_WINDOW (parent_toplevel));
1662 priv->parent_menu_item = parent_menu_item;
1663 priv->position_func = func;
1664 priv->position_func_data = data;
1665 priv->position_func_data_destroy = destroy;
1666 menu_shell->priv->activate_time = activate_time;
1668 /* We need to show the menu here rather in the init function
1669 * because code expects to be able to tell if the menu is onscreen
1670 * by looking at gtk_widget_get_visible (menu)
1672 gtk_widget_show (GTK_WIDGET (menu));
1674 /* Position the menu, possibly changing the size request
1676 gtk_menu_position (menu, TRUE);
1678 /* Compute the size of the toplevel and realize it so we
1679 * can scroll correctly.
1682 GtkRequisition tmp_request;
1683 GtkAllocation tmp_allocation = { 0, };
1685 /* Instead of trusting the menu position function to queue a
1686 * resize when the menu goes out of bounds, invalidate the cached
1689 gtk_widget_queue_resize (GTK_WIDGET (menu));
1690 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1692 tmp_allocation.width = tmp_request.width;
1693 tmp_allocation.height = tmp_request.height;
1695 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1697 gtk_widget_realize (GTK_WIDGET (menu));
1700 gtk_menu_scroll_to (menu, priv->scroll_offset);
1702 /* if no item is selected, select the first one */
1703 if (!menu_shell->priv->active_menu_item)
1705 gboolean touchscreen_mode;
1707 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1708 "gtk-touchscreen-mode", &touchscreen_mode,
1711 if (touchscreen_mode)
1712 gtk_menu_shell_select_first (menu_shell, TRUE);
1715 /* Once everything is set up correctly, map the toplevel */
1716 gtk_widget_show (priv->toplevel);
1718 if (xgrab_shell == widget)
1719 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1721 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1723 if (parent_menu_shell)
1725 gboolean keyboard_mode;
1727 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1728 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1730 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1731 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1733 _gtk_menu_shell_update_mnemonics (menu_shell);
1737 * gtk_menu_popup: (skip)
1739 * @parent_menu_shell: (allow-none): the menu shell containing the
1740 * triggering menu item, or %NULL
1741 * @parent_menu_item: (allow-none): the menu item whose activation
1742 * triggered the popup, or %NULL
1743 * @func: (allow-none): a user supplied function used to position
1744 * the menu, or %NULL
1745 * @data: user supplied data to be passed to @func.
1746 * @button: the mouse button which was pressed to initiate the event.
1747 * @activate_time: the time at which the activation event occurred.
1749 * Displays a menu and makes it available for selection.
1751 * Applications can use this function to display context-sensitive
1752 * menus, and will typically supply %NULL for the @parent_menu_shell,
1753 * @parent_menu_item, @func and @data parameters. The default menu
1754 * positioning function will position the menu at the current mouse
1757 * The @button parameter should be the mouse button pressed to initiate
1758 * the menu popup. If the menu popup was initiated by something other
1759 * than a mouse button press, such as a mouse button release or a keypress,
1760 * @button should be 0.
1762 * The @activate_time parameter is used to conflict-resolve initiation
1763 * of concurrent requests for mouse/keyboard grab requests. To function
1764 * properly, this needs to be the timestamp of the user event (such as
1765 * a mouse click or key press) that caused the initiation of the popup.
1766 * Only if no such event is available, gtk_get_current_event_time() can
1770 gtk_menu_popup (GtkMenu *menu,
1771 GtkWidget *parent_menu_shell,
1772 GtkWidget *parent_menu_item,
1773 GtkMenuPositionFunc func,
1776 guint32 activate_time)
1778 g_return_if_fail (GTK_IS_MENU (menu));
1780 gtk_menu_popup_for_device (menu,
1785 button, activate_time);
1792 * Removes the menu from the screen.
1795 gtk_menu_popdown (GtkMenu *menu)
1797 GtkMenuPrivate *priv;
1798 GtkMenuShell *menu_shell;
1801 g_return_if_fail (GTK_IS_MENU (menu));
1803 menu_shell = GTK_MENU_SHELL (menu);
1806 menu_shell->priv->parent_menu_shell = NULL;
1807 menu_shell->priv->active = FALSE;
1808 menu_shell->priv->ignore_enter = FALSE;
1810 priv->have_position = FALSE;
1812 gtk_menu_stop_scrolling (menu);
1813 gtk_menu_stop_navigating_submenu (menu);
1815 if (menu_shell->priv->active_menu_item)
1817 if (priv->old_active_menu_item)
1818 g_object_unref (priv->old_active_menu_item);
1819 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1820 g_object_ref (priv->old_active_menu_item);
1823 gtk_menu_shell_deselect (menu_shell);
1825 /* The X Grab, if present, will automatically be removed
1826 * when we hide the window
1830 gtk_widget_hide (priv->toplevel);
1831 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1834 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1838 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1840 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1842 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1846 /* We popped up the menu from the tearoff, so we need to
1847 * release the grab - we aren't actually hiding the menu.
1849 if (menu_shell->priv->have_xgrab && pointer)
1851 GdkDevice *keyboard;
1853 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1854 keyboard = gdk_device_get_associated_device (pointer);
1857 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1861 /* gtk_menu_popdown is called each time a menu item is selected from
1862 * a torn off menu. Only scroll back to the saved position if the
1863 * non-tearoff menu was popped down.
1865 if (!priv->tearoff_active)
1866 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1867 priv->tearoff_active = TRUE;
1870 gtk_widget_hide (GTK_WIDGET (menu));
1872 menu_shell->priv->have_xgrab = FALSE;
1875 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1877 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1879 menu_grab_transfer_window_destroy (menu);
1883 * gtk_menu_get_active:
1886 * Returns the selected menu item from the menu. This is used by the
1889 * Returns: (transfer none): the #GtkMenuItem that was last selected
1890 * in the menu. If a selection has not yet been made, the
1891 * first menu item is selected.
1894 gtk_menu_get_active (GtkMenu *menu)
1896 GtkMenuPrivate *priv = menu->priv;
1900 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1902 if (!priv->old_active_menu_item)
1905 children = GTK_MENU_SHELL (menu)->priv->children;
1909 child = children->data;
1910 children = children->next;
1912 if (gtk_bin_get_child (GTK_BIN (child)))
1917 priv->old_active_menu_item = child;
1918 if (priv->old_active_menu_item)
1919 g_object_ref (priv->old_active_menu_item);
1922 return priv->old_active_menu_item;
1926 * gtk_menu_set_active:
1928 * @index: the index of the menu item to select. Iindex values are
1931 * Selects the specified menu item within the menu. This is used by
1932 * the #GtkOptionMenu and should not be used by anyone else.
1935 gtk_menu_set_active (GtkMenu *menu,
1938 GtkMenuPrivate *priv = menu->priv;
1942 g_return_if_fail (GTK_IS_MENU (menu));
1944 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1947 child = tmp_list->data;
1948 if (gtk_bin_get_child (GTK_BIN (child)))
1950 if (priv->old_active_menu_item)
1951 g_object_unref (priv->old_active_menu_item);
1952 priv->old_active_menu_item = child;
1953 g_object_ref (priv->old_active_menu_item);
1959 * gtk_menu_set_accel_group:
1961 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1964 * Set the #GtkAccelGroup which holds global accelerators for the
1965 * menu. This accelerator group needs to also be added to all windows
1966 * that this menu is being used in with gtk_window_add_accel_group(),
1967 * in order for those windows to support all the accelerators
1968 * contained in this group.
1971 gtk_menu_set_accel_group (GtkMenu *menu,
1972 GtkAccelGroup *accel_group)
1974 GtkMenuPrivate *priv = menu->priv;
1975 g_return_if_fail (GTK_IS_MENU (menu));
1977 if (priv->accel_group != accel_group)
1979 if (priv->accel_group)
1980 g_object_unref (priv->accel_group);
1981 priv->accel_group = accel_group;
1982 if (priv->accel_group)
1983 g_object_ref (priv->accel_group);
1984 _gtk_menu_refresh_accel_paths (menu, TRUE);
1989 * gtk_menu_get_accel_group:
1992 * Gets the #GtkAccelGroup which holds global accelerators for the
1993 * menu. See gtk_menu_set_accel_group().
1995 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
1998 gtk_menu_get_accel_group (GtkMenu *menu)
2000 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2002 return menu->priv->accel_group;
2006 gtk_menu_real_can_activate_accel (GtkWidget *widget,
2009 /* Menu items chain here to figure whether they can activate their
2010 * accelerators. Unlike ordinary widgets, menus allow accel
2011 * activation even if invisible since that's the usual case for
2012 * submenus/popup-menus. however, the state of the attach widget
2013 * affects the "activeness" of the menu.
2015 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
2018 return gtk_widget_can_activate_accel (awidget, signal_id);
2020 return gtk_widget_is_sensitive (widget);
2024 * gtk_menu_set_accel_path
2025 * @menu: a valid #GtkMenu
2026 * @accel_path: (allow-none): a valid accelerator path
2028 * Sets an accelerator path for this menu from which accelerator paths
2029 * for its immediate children, its menu items, can be constructed.
2030 * The main purpose of this function is to spare the programmer the
2031 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2032 * each menu item that should support runtime user changable accelerators.
2033 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2034 * each menu item of this menu, that contains a label describing its
2035 * purpose, automatically gets an accel path assigned.
2037 * For example, a menu containing menu items "New" and "Exit", will, after
2038 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2039 * has been called, assign its items the accel paths:
2040 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2042 * Assigning accel paths to menu items then enables the user to change
2043 * their accelerators at runtime. More details about accelerator paths
2044 * and their default setups can be found at gtk_accel_map_add_entry().
2046 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2047 * if you pass a static string, you can save some memory by interning
2048 * it first with g_intern_static_string().
2051 gtk_menu_set_accel_path (GtkMenu *menu,
2052 const gchar *accel_path)
2054 GtkMenuPrivate *priv = menu->priv;
2055 g_return_if_fail (GTK_IS_MENU (menu));
2058 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2060 /* FIXME: accel_path should be defined as const gchar* */
2061 priv->accel_path = (gchar*)g_intern_string (accel_path);
2062 if (priv->accel_path)
2063 _gtk_menu_refresh_accel_paths (menu, FALSE);
2067 * gtk_menu_get_accel_path
2068 * @menu: a valid #GtkMenu
2070 * Retrieves the accelerator path set on the menu.
2072 * Returns: the accelerator path set on the menu.
2077 gtk_menu_get_accel_path (GtkMenu *menu)
2079 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2081 return menu->priv->accel_path;
2086 gboolean group_changed;
2090 refresh_accel_paths_foreach (GtkWidget *widget,
2093 GtkMenuPrivate *priv;
2094 AccelPropagation *prop = data;
2096 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2098 priv = prop->menu->priv;
2099 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2102 prop->group_changed);
2107 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2108 gboolean group_changed)
2110 GtkMenuPrivate *priv = menu->priv;
2111 g_return_if_fail (GTK_IS_MENU (menu));
2113 if (priv->accel_path && priv->accel_group)
2115 AccelPropagation prop;
2118 prop.group_changed = group_changed;
2119 gtk_container_foreach (GTK_CONTAINER (menu),
2120 refresh_accel_paths_foreach,
2126 * gtk_menu_reposition:
2129 * Repositions the menu according to its position function.
2132 gtk_menu_reposition (GtkMenu *menu)
2134 g_return_if_fail (GTK_IS_MENU (menu));
2136 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2137 gtk_menu_position (menu, FALSE);
2141 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2146 value = gtk_adjustment_get_value (adjustment);
2147 if (menu->priv->scroll_offset != value)
2148 gtk_menu_scroll_to (menu, value);
2152 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2155 GtkMenuPrivate *priv = menu->priv;
2156 GdkGeometry geometry_hints;
2158 if (!priv->tearoff_window)
2161 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2163 GtkRequisition requisition;
2165 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2166 &requisition, NULL);
2167 width += requisition.width;
2170 geometry_hints.min_width = width;
2171 geometry_hints.max_width = width;
2173 geometry_hints.min_height = 0;
2174 geometry_hints.max_height = priv->requested_height;
2176 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2179 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2183 gtk_menu_update_title (GtkMenu *menu)
2185 GtkMenuPrivate *priv = menu->priv;
2187 if (priv->tearoff_window)
2190 GtkWidget *attach_widget;
2192 title = gtk_menu_get_title (menu);
2195 attach_widget = gtk_menu_get_attach_widget (menu);
2196 if (GTK_IS_MENU_ITEM (attach_widget))
2198 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2199 if (GTK_IS_LABEL (child))
2200 title = gtk_label_get_text (GTK_LABEL (child));
2205 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2210 gtk_menu_get_toplevel (GtkWidget *menu)
2212 GtkWidget *attach, *toplevel;
2214 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2216 if (GTK_IS_MENU_ITEM (attach))
2217 attach = gtk_widget_get_parent (attach);
2219 if (GTK_IS_MENU (attach))
2220 return gtk_menu_get_toplevel (attach);
2221 else if (GTK_IS_WIDGET (attach))
2223 toplevel = gtk_widget_get_toplevel (attach);
2224 if (gtk_widget_is_toplevel (toplevel))
2232 tearoff_window_destroyed (GtkWidget *widget,
2235 gtk_menu_set_tearoff_state (menu, FALSE);
2239 * gtk_menu_set_tearoff_state:
2241 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2243 * Changes the tearoff state of the menu. A menu is normally
2244 * displayed as drop down menu which persists as long as the menu is
2245 * active. It can also be displayed as a tearoff menu which persists
2246 * until it is closed or reattached.
2249 gtk_menu_set_tearoff_state (GtkMenu *menu,
2252 GtkMenuPrivate *priv = menu->priv;
2255 g_return_if_fail (GTK_IS_MENU (menu));
2257 if (priv->torn_off != torn_off)
2259 priv->torn_off = torn_off;
2260 priv->tearoff_active = torn_off;
2264 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2265 gtk_menu_popdown (menu);
2267 if (!priv->tearoff_window)
2269 GtkWidget *toplevel;
2271 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2272 "type", GTK_WINDOW_TOPLEVEL,
2273 "screen", gtk_widget_get_screen (priv->toplevel),
2274 "app-paintable", TRUE,
2277 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2278 GDK_WINDOW_TYPE_HINT_MENU);
2279 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2280 g_signal_connect (priv->tearoff_window, "destroy",
2281 G_CALLBACK (tearoff_window_destroyed), menu);
2282 g_signal_connect (priv->tearoff_window, "event",
2283 G_CALLBACK (gtk_menu_window_event), menu);
2285 gtk_menu_update_title (menu);
2287 gtk_widget_realize (priv->tearoff_window);
2289 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2290 if (toplevel != NULL)
2291 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2292 GTK_WINDOW (toplevel));
2294 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2295 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2296 priv->tearoff_hbox);
2298 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2299 priv->tearoff_adjustment = gtk_adjustment_new (0,
2300 0, priv->requested_height,
2304 g_object_connect (priv->tearoff_adjustment,
2305 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2307 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2309 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2310 priv->tearoff_scrollbar,
2313 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2314 gtk_widget_show (priv->tearoff_scrollbar);
2316 gtk_widget_show (priv->tearoff_hbox);
2319 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2321 /* Update menu->requisition */
2322 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2324 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2326 gtk_widget_realize (priv->tearoff_window);
2327 gtk_menu_position (menu, TRUE);
2329 gtk_widget_show (GTK_WIDGET (menu));
2330 gtk_widget_show (priv->tearoff_window);
2332 gtk_menu_scroll_to (menu, 0);
2337 gtk_widget_hide (GTK_WIDGET (menu));
2338 gtk_widget_hide (priv->tearoff_window);
2339 if (GTK_IS_CONTAINER (priv->toplevel))
2340 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2341 gtk_widget_destroy (priv->tearoff_window);
2343 priv->tearoff_window = NULL;
2344 priv->tearoff_hbox = NULL;
2345 priv->tearoff_scrollbar = NULL;
2346 priv->tearoff_adjustment = NULL;
2349 g_object_notify (G_OBJECT (menu), "tearoff-state");
2354 * gtk_menu_get_tearoff_state:
2357 * Returns whether the menu is torn off.
2358 * See gtk_menu_set_tearoff_state().
2360 * Return value: %TRUE if the menu is currently torn off.
2363 gtk_menu_get_tearoff_state (GtkMenu *menu)
2365 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2367 return menu->priv->torn_off;
2371 * gtk_menu_set_title:
2373 * @title: a string containing the title for the menu
2375 * Sets the title string for the menu.
2377 * The title is displayed when the menu is shown as a tearoff
2378 * menu. If @title is %NULL, the menu will see if it is attached
2379 * to a parent menu item, and if so it will try to use the same
2380 * text as that menu item's label.
2383 gtk_menu_set_title (GtkMenu *menu,
2386 GtkMenuPrivate *priv = menu->priv;
2389 g_return_if_fail (GTK_IS_MENU (menu));
2391 old_title = priv->title;
2392 priv->title = g_strdup (title);
2395 gtk_menu_update_title (menu);
2396 g_object_notify (G_OBJECT (menu), "tearoff-title");
2400 * gtk_menu_get_title:
2403 * Returns the title of the menu. See gtk_menu_set_title().
2405 * Return value: the title of the menu, or %NULL if the menu
2406 * has no title set on it. This string is owned by GTK+
2407 * and should not be modified or freed.
2409 G_CONST_RETURN gchar *
2410 gtk_menu_get_title (GtkMenu *menu)
2412 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2414 return menu->priv->title;
2418 * gtk_menu_reorder_child:
2420 * @child: the #GtkMenuItem to move
2421 * @position: the new position to place @child.
2422 * Positions are numbered from 0 to n - 1
2424 * Moves @child to a new @position in the list of @menu
2428 gtk_menu_reorder_child (GtkMenu *menu,
2432 GtkMenuShell *menu_shell;
2434 g_return_if_fail (GTK_IS_MENU (menu));
2435 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2437 menu_shell = GTK_MENU_SHELL (menu);
2439 if (g_list_find (menu_shell->priv->children, child))
2441 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2442 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2444 menu_queue_resize (menu);
2449 gtk_menu_style_updated (GtkWidget *widget)
2451 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2453 if (gtk_widget_get_realized (widget))
2455 GtkMenu *menu = GTK_MENU (widget);
2456 GtkMenuPrivate *priv = menu->priv;
2457 GtkStyleContext *context;
2459 context = gtk_widget_get_style_context (widget);
2461 gtk_style_context_set_background (context, priv->bin_window);
2462 gtk_style_context_set_background (context, priv->view_window);
2463 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2468 get_arrows_border (GtkMenu *menu,
2471 GtkMenuPrivate *priv = menu->priv;
2472 guint scroll_arrow_height;
2473 GtkArrowPlacement arrow_placement;
2475 gtk_widget_style_get (GTK_WIDGET (menu),
2476 "scroll-arrow-vlength", &scroll_arrow_height,
2477 "arrow_placement", &arrow_placement,
2480 switch (arrow_placement)
2482 case GTK_ARROWS_BOTH:
2483 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2484 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2487 case GTK_ARROWS_START:
2488 border->top = (priv->upper_arrow_visible ||
2489 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2493 case GTK_ARROWS_END:
2495 border->bottom = (priv->upper_arrow_visible ||
2496 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2500 border->left = border->right = 0;
2504 get_menu_padding (GtkWidget *widget,
2507 GtkStyleContext *context;
2508 GtkStateFlags state;
2510 context = gtk_widget_get_style_context (widget);
2511 state = gtk_widget_get_state_flags (widget);
2513 gtk_style_context_get_padding (context, state, padding);
2517 gtk_menu_realize (GtkWidget *widget)
2519 GtkMenu *menu = GTK_MENU (widget);
2520 GtkMenuPrivate *priv = menu->priv;
2521 GtkAllocation allocation;
2522 GtkStyleContext *context;
2524 GdkWindowAttr attributes;
2525 gint attributes_mask;
2529 guint vertical_padding;
2530 guint horizontal_padding;
2531 GtkBorder arrow_border, padding;
2533 g_return_if_fail (GTK_IS_MENU (widget));
2535 gtk_widget_set_realized (widget, TRUE);
2537 gtk_widget_get_allocation (widget, &allocation);
2539 attributes.window_type = GDK_WINDOW_CHILD;
2540 attributes.x = allocation.x;
2541 attributes.y = allocation.y;
2542 attributes.width = allocation.width;
2543 attributes.height = allocation.height;
2544 attributes.wclass = GDK_INPUT_OUTPUT;
2545 attributes.visual = gtk_widget_get_visual (widget);
2546 attributes.event_mask = gtk_widget_get_events (widget);
2547 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2548 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2550 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2552 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2553 &attributes, attributes_mask);
2554 gtk_widget_set_window (widget, window);
2555 gdk_window_set_user_data (window, widget);
2557 get_menu_padding (widget, &padding);
2558 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2559 context = gtk_widget_get_style_context (widget);
2561 gtk_widget_style_get (GTK_WIDGET (menu),
2562 "vertical-padding", &vertical_padding,
2563 "horizontal-padding", &horizontal_padding,
2566 gtk_widget_get_allocation (widget, &allocation);
2568 attributes.x = border_width + padding.left + horizontal_padding;
2569 attributes.y = border_width + padding.top + vertical_padding;
2570 attributes.width = allocation.width -
2571 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2572 attributes.height = allocation.height -
2573 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2575 get_arrows_border (menu, &arrow_border);
2576 attributes.y += arrow_border.top;
2577 attributes.height -= arrow_border.top;
2578 attributes.height -= arrow_border.bottom;
2580 attributes.width = MAX (1, attributes.width);
2581 attributes.height = MAX (1, attributes.height);
2583 priv->view_window = gdk_window_new (window,
2584 &attributes, attributes_mask);
2585 gdk_window_set_user_data (priv->view_window, menu);
2587 gtk_widget_get_allocation (widget, &allocation);
2591 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2592 padding.left + padding.right;
2593 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2594 padding.top + padding.bottom;
2596 attributes.width = MAX (1, attributes.width);
2597 attributes.height = MAX (1, attributes.height);
2599 priv->bin_window = gdk_window_new (priv->view_window,
2600 &attributes, attributes_mask);
2601 gdk_window_set_user_data (priv->bin_window, menu);
2603 children = GTK_MENU_SHELL (menu)->priv->children;
2606 child = children->data;
2607 children = children->next;
2609 gtk_widget_set_parent_window (child, priv->bin_window);
2612 gtk_style_context_set_background (context, priv->bin_window);
2613 gtk_style_context_set_background (context, priv->view_window);
2614 gtk_style_context_set_background (context, window);
2616 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2617 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2618 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2620 gdk_window_show (priv->bin_window);
2621 gdk_window_show (priv->view_window);
2625 gtk_menu_focus (GtkWidget *widget,
2626 GtkDirectionType direction)
2628 /* A menu or its menu items cannot have focus */
2632 /* See notes in gtk_menu_popup() for information
2633 * about the "grab transfer window"
2636 menu_grab_transfer_window_get (GtkMenu *menu)
2638 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2641 GdkWindowAttr attributes;
2642 gint attributes_mask;
2644 attributes.x = -100;
2645 attributes.y = -100;
2646 attributes.width = 10;
2647 attributes.height = 10;
2648 attributes.window_type = GDK_WINDOW_TEMP;
2649 attributes.wclass = GDK_INPUT_ONLY;
2650 attributes.override_redirect = TRUE;
2651 attributes.event_mask = 0;
2653 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2655 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2656 &attributes, attributes_mask);
2657 gdk_window_set_user_data (window, menu);
2659 gdk_window_show (window);
2661 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2668 menu_grab_transfer_window_destroy (GtkMenu *menu)
2670 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2673 gdk_window_set_user_data (window, NULL);
2674 gdk_window_destroy (window);
2675 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2680 gtk_menu_unrealize (GtkWidget *widget)
2682 GtkMenu *menu = GTK_MENU (widget);
2683 GtkMenuPrivate *priv = menu->priv;
2685 menu_grab_transfer_window_destroy (menu);
2687 gdk_window_set_user_data (priv->view_window, NULL);
2688 gdk_window_destroy (priv->view_window);
2689 priv->view_window = NULL;
2691 gdk_window_set_user_data (priv->bin_window, NULL);
2692 gdk_window_destroy (priv->bin_window);
2693 priv->bin_window = NULL;
2695 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2699 calculate_line_heights (GtkMenu *menu,
2701 guint **ret_min_heights,
2702 guint **ret_nat_heights)
2705 GtkMenuPrivate *priv;
2706 GtkMenuShell *menu_shell;
2707 GtkWidget *child, *widget;
2709 guint horizontal_padding;
2718 widget = GTK_WIDGET (menu);
2719 menu_shell = GTK_MENU_SHELL (widget);
2721 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2722 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2723 n_heights = gtk_menu_get_n_rows (menu);
2724 n_columns = gtk_menu_get_n_columns (menu);
2725 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2727 gtk_widget_style_get (GTK_WIDGET (menu),
2728 "horizontal-padding", &horizontal_padding,
2730 get_menu_padding (widget, &padding);
2732 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2733 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2735 for (children = menu_shell->priv->children; children; children = children->next)
2740 gint child_min, child_nat;
2742 child = children->data;
2744 if (!gtk_widget_get_visible (child))
2747 get_effective_child_attach (child, &l, &r, &t, &b);
2749 part = avail_width / (r - l);
2751 gtk_widget_get_preferred_height_for_width (child, part,
2752 &child_min, &child_nat);
2754 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2756 part = MAX (child_min, toggle_size) / (b - t);
2757 min_heights[t] = MAX (min_heights[t], part);
2759 part = MAX (child_nat, toggle_size) / (b - t);
2760 nat_heights[t] = MAX (nat_heights[t], part);
2763 if (ret_min_heights)
2764 *ret_min_heights = min_heights;
2766 g_free (min_heights);
2768 if (ret_nat_heights)
2769 *ret_nat_heights = nat_heights;
2771 g_free (nat_heights);
2777 gtk_menu_size_allocate (GtkWidget *widget,
2778 GtkAllocation *allocation)
2781 GtkMenuPrivate *priv;
2782 GtkMenuShell *menu_shell;
2784 GtkAllocation child_allocation;
2789 guint vertical_padding;
2790 guint horizontal_padding;
2793 g_return_if_fail (GTK_IS_MENU (widget));
2794 g_return_if_fail (allocation != NULL);
2796 menu = GTK_MENU (widget);
2797 menu_shell = GTK_MENU_SHELL (widget);
2800 gtk_widget_set_allocation (widget, allocation);
2802 gtk_widget_style_get (GTK_WIDGET (menu),
2803 "vertical-padding", &vertical_padding,
2804 "horizontal-padding", &horizontal_padding,
2807 get_menu_padding (widget, &padding);
2808 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2810 g_free (priv->heights);
2811 priv->heights_length = calculate_line_heights (menu,
2816 /* refresh our cached height request */
2817 priv->requested_height = (2 * (border_width + vertical_padding)) +
2818 padding.top + padding.bottom;
2819 for (i = 0; i < priv->heights_length; i++)
2820 priv->requested_height += priv->heights[i];
2822 x = border_width + padding.left + horizontal_padding;
2823 y = border_width + padding.top + vertical_padding;
2824 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2825 padding.left - padding.right;
2826 height = allocation->height - (2 * (border_width + vertical_padding)) -
2827 padding.top - padding.bottom;
2829 if (menu_shell->priv->active)
2830 gtk_menu_scroll_to (menu, priv->scroll_offset);
2832 if (!priv->tearoff_active)
2834 GtkBorder arrow_border;
2836 get_arrows_border (menu, &arrow_border);
2837 y += arrow_border.top;
2838 height -= arrow_border.top;
2839 height -= arrow_border.bottom;
2842 width = MAX (1, width);
2843 height = MAX (1, height);
2845 if (gtk_widget_get_realized (widget))
2847 gdk_window_move_resize (gtk_widget_get_window (widget),
2848 allocation->x, allocation->y,
2849 allocation->width, allocation->height);
2851 gdk_window_move_resize (priv->view_window, x, y, width, height);
2854 if (menu_shell->priv->children)
2856 gint base_width = width / gtk_menu_get_n_columns (menu);
2858 children = menu_shell->priv->children;
2861 child = children->data;
2862 children = children->next;
2864 if (gtk_widget_get_visible (child))
2869 get_effective_child_attach (child, &l, &r, &t, &b);
2871 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2874 tmp = gtk_menu_get_n_columns (menu) - l;
2875 l = gtk_menu_get_n_columns (menu) - r;
2879 child_allocation.width = (r - l) * base_width;
2880 child_allocation.height = 0;
2881 child_allocation.x = l * base_width;
2882 child_allocation.y = 0;
2884 for (i = 0; i < b; i++)
2887 child_allocation.y += priv->heights[i];
2889 child_allocation.height += priv->heights[i];
2892 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2895 gtk_widget_size_allocate (child, &child_allocation);
2896 gtk_widget_queue_draw (child);
2900 /* Resize the item window */
2901 if (gtk_widget_get_realized (widget))
2907 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2908 height += priv->heights[i];
2910 width = gtk_menu_get_n_columns (menu) * base_width;
2911 gdk_window_resize (priv->bin_window, width, height);
2914 if (priv->tearoff_active)
2916 if (height >= priv->requested_height)
2918 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2920 gtk_widget_hide (priv->tearoff_scrollbar);
2921 gtk_menu_set_tearoff_hints (menu, allocation->width);
2923 gtk_menu_scroll_to (menu, 0);
2928 gtk_adjustment_configure (priv->tearoff_adjustment,
2929 gtk_adjustment_get_value (priv->tearoff_adjustment),
2931 priv->requested_height,
2932 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2933 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2934 allocation->height);
2936 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2938 gtk_widget_show (priv->tearoff_scrollbar);
2939 gtk_menu_set_tearoff_hints (menu, allocation->width);
2947 get_arrows_visible_area (GtkMenu *menu,
2948 GdkRectangle *border,
2949 GdkRectangle *upper,
2950 GdkRectangle *lower,
2953 GtkArrowPlacement arrow_placement;
2954 GtkWidget *widget = GTK_WIDGET (menu);
2956 guint vertical_padding;
2957 guint horizontal_padding;
2958 gint scroll_arrow_height;
2959 GtkBorder menu_padding;
2961 gtk_widget_style_get (widget,
2962 "vertical-padding", &vertical_padding,
2963 "horizontal-padding", &horizontal_padding,
2964 "scroll-arrow-vlength", &scroll_arrow_height,
2965 "arrow-placement", &arrow_placement,
2968 get_menu_padding (widget, &menu_padding);
2969 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2970 border->x = border_width + menu_padding.left + horizontal_padding;
2971 border->y = border_width + menu_padding.top + vertical_padding;
2972 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2973 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2975 switch (arrow_placement)
2977 case GTK_ARROWS_BOTH:
2978 upper->x = border->x;
2979 upper->y = border->y;
2980 upper->width = border->width - 2 * border->x;
2981 upper->height = scroll_arrow_height;
2983 lower->x = border->x;
2984 lower->y = border->height - border->y - scroll_arrow_height;
2985 lower->width = border->width - 2 * border->x;
2986 lower->height = scroll_arrow_height;
2989 case GTK_ARROWS_START:
2990 upper->x = border->x;
2991 upper->y = border->y;
2992 upper->width = (border->width - 2 * border->x) / 2;
2993 upper->height = scroll_arrow_height;
2995 lower->x = border->x + upper->width;
2996 lower->y = border->y;
2997 lower->width = (border->width - 2 * border->x) / 2;
2998 lower->height = scroll_arrow_height;
3001 case GTK_ARROWS_END:
3002 upper->x = border->x;
3003 upper->y = border->height - border->y - scroll_arrow_height;
3004 upper->width = (border->width - 2 * border->x) / 2;
3005 upper->height = scroll_arrow_height;
3007 lower->x = border->x + upper->width;
3008 lower->y = border->height - border->y - scroll_arrow_height;
3009 lower->width = (border->width - 2 * border->x) / 2;
3010 lower->height = scroll_arrow_height;
3014 g_assert_not_reached();
3015 upper->x = upper->y = upper->width = upper->height = 0;
3016 lower->x = lower->y = lower->width = lower->height = 0;
3019 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
3023 gtk_menu_draw (GtkWidget *widget,
3027 GtkMenuPrivate *priv;
3028 GtkStyleContext *context;
3029 GdkRectangle border;
3033 GtkBorder menu_padding;
3035 menu = GTK_MENU (widget);
3037 context = gtk_widget_get_style_context (widget);
3039 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3040 get_menu_padding (widget, &menu_padding);
3042 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3044 gfloat arrow_scaling;
3047 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3048 arrow_size = arrow_scaling * arrow_space;
3050 gtk_render_background (context, cr, 0, 0,
3051 gtk_widget_get_allocated_width (widget),
3052 gtk_widget_get_allocated_height (widget));
3053 gtk_render_frame (context, cr, 0, 0,
3054 gtk_widget_get_allocated_width (widget),
3055 gtk_widget_get_allocated_height (widget));
3057 gtk_style_context_save (context);
3058 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3060 if (priv->upper_arrow_visible && !priv->tearoff_active)
3062 gtk_style_context_save (context);
3063 gtk_style_context_set_state (context, priv->upper_arrow_state);
3065 gtk_render_background (context, cr,
3067 upper.width, upper.height);
3068 gtk_render_frame (context, cr,
3070 upper.width, upper.height);
3072 gtk_render_arrow (context, cr, 0,
3073 upper.x + (upper.width - arrow_size) / 2,
3074 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3077 gtk_style_context_restore (context);
3080 if (priv->lower_arrow_visible && !priv->tearoff_active)
3082 gtk_style_context_save (context);
3083 gtk_style_context_set_state (context, priv->lower_arrow_state);
3085 gtk_render_background (context, cr,
3087 lower.width, lower.height);
3088 gtk_render_frame (context, cr,
3090 lower.width, lower.height);
3092 gtk_render_arrow (context, cr, G_PI,
3093 lower.x + (lower.width - arrow_size) / 2,
3094 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3097 gtk_style_context_restore (context);
3100 gtk_style_context_restore (context);
3103 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3105 gint y = -border.y + priv->scroll_offset;
3108 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3110 if (!priv->tearoff_active)
3112 GtkBorder arrow_border;
3114 get_arrows_border (menu, &arrow_border);
3115 y -= arrow_border.top;
3118 gtk_render_background (context, cr,
3120 border.width, border.height);
3121 gtk_render_frame (context, cr,
3123 border.width, border.height);
3128 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3134 gtk_menu_show (GtkWidget *widget)
3136 GtkMenu *menu = GTK_MENU (widget);
3138 _gtk_menu_refresh_accel_paths (menu, FALSE);
3140 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3145 gtk_menu_get_preferred_width (GtkWidget *widget,
3150 GtkMenuShell *menu_shell;
3151 GtkMenuPrivate *priv;
3154 guint max_toggle_size;
3155 guint max_accel_width;
3156 guint horizontal_padding;
3158 gint child_min, child_nat;
3159 gint min_width, nat_width;
3162 menu = GTK_MENU (widget);
3163 menu_shell = GTK_MENU_SHELL (widget);
3166 min_width = nat_width = 0;
3168 max_toggle_size = 0;
3169 max_accel_width = 0;
3171 children = menu_shell->priv->children;
3178 child = children->data;
3179 children = children->next;
3181 if (! gtk_widget_get_visible (child))
3184 get_effective_child_attach (child, &l, &r, &t, &b);
3186 /* It's important to size_request the child
3187 * before doing the toggle size request, in
3188 * case the toggle size request depends on the size
3189 * request of a child of the child (e.g. for ImageMenuItem)
3191 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3193 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3194 max_toggle_size = MAX (max_toggle_size, toggle_size);
3195 max_accel_width = MAX (max_accel_width,
3196 GTK_MENU_ITEM (child)->priv->accelerator_width);
3198 part = child_min / (r - l);
3199 min_width = MAX (min_width, part);
3201 part = child_nat / (r - l);
3202 nat_width = MAX (nat_width, part);
3205 /* If the menu doesn't include any images or check items
3206 * reserve the space so that all menus are consistent.
3207 * We only do this for 'ordinary' menus, not for combobox
3208 * menus or multi-column menus
3210 if (max_toggle_size == 0 &&
3211 gtk_menu_get_n_columns (menu) == 1 &&
3212 !priv->no_toggle_size)
3214 GtkStyleContext *context;
3215 GtkWidgetPath *menu_path, *check_path;
3216 guint toggle_spacing;
3217 guint indicator_size;
3219 context = gtk_widget_get_style_context (widget);
3220 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3222 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3223 check_path = gtk_widget_path_copy (menu_path);
3224 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3226 gtk_style_context_set_path (context, check_path);
3227 gtk_widget_path_free (check_path);
3229 gtk_style_context_get_style (context,
3230 "toggle-spacing", &toggle_spacing,
3231 "indicator-size", &indicator_size,
3234 max_toggle_size = indicator_size + toggle_spacing;
3236 /* Restore real widget path */
3237 gtk_style_context_set_path (context, menu_path);
3238 gtk_widget_path_free (menu_path);
3241 min_width += 2 * max_toggle_size + max_accel_width;
3242 min_width *= gtk_menu_get_n_columns (menu);
3244 nat_width += 2 * max_toggle_size + max_accel_width;
3245 nat_width *= gtk_menu_get_n_columns (menu);
3247 gtk_widget_style_get (GTK_WIDGET (menu),
3248 "horizontal-padding", &horizontal_padding,
3251 get_menu_padding (widget, &padding);
3252 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3253 min_width += (2 * (border_width + horizontal_padding)) +
3254 padding.left + padding.right;
3255 nat_width += (2 * (border_width + horizontal_padding)) +
3256 padding.left + padding.right;
3258 priv->toggle_size = max_toggle_size;
3259 priv->accel_size = max_accel_width;
3262 *minimum_size = min_width;
3265 *natural_size = nat_width;
3267 /* Don't resize the tearoff if it is not active,
3268 * because it won't redraw (it is only a background pixmap).
3270 if (priv->tearoff_active)
3271 gtk_menu_set_tearoff_hints (menu, min_width);
3275 gtk_menu_get_preferred_height (GtkWidget *widget,
3281 /* Menus are height-for-width only, just return the height
3282 * for the minimum width
3284 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3285 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3289 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3295 GtkMenu *menu = GTK_MENU (widget);
3296 GtkMenuPrivate *priv = menu->priv;
3297 guint *min_heights, *nat_heights;
3298 guint vertical_padding, border_width;
3300 gint min_height, nat_height;
3302 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3303 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3304 get_menu_padding (widget, &padding);
3306 min_height = nat_height = (border_width + vertical_padding) * 2 +
3307 padding.top + padding.bottom;
3310 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3312 for (i = 0; i < n_heights; i++)
3314 min_height += min_heights[i];
3315 nat_height += nat_heights[i];
3318 if (priv->have_position)
3320 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3321 GdkRectangle monitor;
3323 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
3325 if (priv->position_y + min_height > monitor.y + monitor.height)
3326 min_height = monitor.y + monitor.height - priv->position_y;
3328 if (priv->position_y + nat_height > monitor.y + monitor.height)
3329 nat_height = monitor.y + monitor.height - priv->position_y;
3331 if (priv->position_y < monitor.y)
3333 min_height -= monitor.y - priv->position_y;
3334 nat_height -= monitor.y - priv->position_y;
3339 *minimum_size = min_height;
3342 *natural_size = nat_height;
3344 g_free (min_heights);
3345 g_free (nat_heights);
3351 gtk_menu_button_scroll (GtkMenu *menu,
3352 GdkEventButton *event)
3354 GtkMenuPrivate *priv = menu->priv;
3356 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3358 gboolean touchscreen_mode;
3360 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3361 "gtk-touchscreen-mode", &touchscreen_mode,
3364 if (touchscreen_mode)
3365 gtk_menu_handle_scrolling (menu,
3366 event->x_root, event->y_root,
3367 event->type == GDK_BUTTON_PRESS,
3377 pointer_in_menu_window (GtkWidget *widget,
3381 GtkMenu *menu = GTK_MENU (widget);
3382 GtkMenuPrivate *priv = menu->priv;
3383 GtkAllocation allocation;
3385 if (gtk_widget_get_mapped (priv->toplevel))
3387 GtkMenuShell *menu_shell;
3388 gint window_x, window_y;
3390 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3391 &window_x, &window_y);
3393 gtk_widget_get_allocation (widget, &allocation);
3394 if (x_root >= window_x && x_root < window_x + allocation.width &&
3395 y_root >= window_y && y_root < window_y + allocation.height)
3398 menu_shell = GTK_MENU_SHELL (widget);
3400 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3401 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3409 gtk_menu_button_press (GtkWidget *widget,
3410 GdkEventButton *event)
3412 if (event->type != GDK_BUTTON_PRESS)
3415 /* Don't pass down to menu shell for presses over scroll arrows
3417 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3420 /* Don't pass down to menu shell if a non-menuitem part of the menu
3421 * was clicked. The check for the event_widget being a GtkMenuShell
3422 * works because we have the pointer grabbed on menu_shell->window
3423 * with owner_events=TRUE, so all events that are either outside
3424 * the menu or on its border are delivered relative to
3425 * menu_shell->window.
3427 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3428 pointer_in_menu_window (widget, event->x_root, event->y_root))
3431 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3435 gtk_menu_button_release (GtkWidget *widget,
3436 GdkEventButton *event)
3438 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3440 if (priv->ignore_button_release)
3442 priv->ignore_button_release = FALSE;
3446 if (event->type != GDK_BUTTON_RELEASE)
3449 /* Don't pass down to menu shell for releases over scroll arrows
3451 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3454 /* Don't pass down to menu shell if a non-menuitem part of the menu
3455 * was clicked (see comment in button_press()).
3457 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3458 pointer_in_menu_window (widget, event->x_root, event->y_root))
3460 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3461 * bail out early here so it is in a consistent state for the
3462 * next button_press/button_release in GtkMenuShell.
3465 if (GTK_MENU_SHELL (widget)->priv->active)
3466 GTK_MENU_SHELL (widget)->priv->button = 0;
3471 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3474 static const gchar *
3475 get_accel_path (GtkWidget *menu_item,
3480 GClosure *accel_closure;
3481 GtkAccelGroup *accel_group;
3483 path = _gtk_widget_get_accel_path (menu_item, locked);
3486 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3492 label = gtk_bin_get_child (GTK_BIN (menu_item));
3494 if (GTK_IS_ACCEL_LABEL (label))
3496 g_object_get (label,
3497 "accel-closure", &accel_closure,
3501 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3503 *locked = gtk_accel_group_get_is_locked (accel_group);
3513 gtk_menu_key_press (GtkWidget *widget,
3516 GtkMenuShell *menu_shell;
3518 gboolean delete = FALSE;
3519 gboolean can_change_accels;
3520 gchar *accel = NULL;
3521 guint accel_key, accel_mods;
3522 GdkModifierType consumed_modifiers;
3523 GdkDisplay *display;
3525 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3526 g_return_val_if_fail (event != NULL, FALSE);
3528 menu_shell = GTK_MENU_SHELL (widget);
3529 menu = GTK_MENU (widget);
3531 gtk_menu_stop_navigating_submenu (menu);
3533 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3536 display = gtk_widget_get_display (widget);
3538 g_object_get (gtk_widget_get_settings (widget),
3539 "gtk-menu-bar-accel", &accel,
3540 "gtk-can-change-accels", &can_change_accels,
3543 if (accel && *accel)
3546 GdkModifierType mods = 0;
3548 gtk_accelerator_parse (accel, &keyval, &mods);
3551 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3553 /* FIXME this is wrong, needs to be in the global accel resolution
3554 * thing, to properly consider i18n etc., but that probably requires
3555 * AccelGroup changes etc.
3557 if (event->keyval == keyval && (mods & event->state) == mods)
3559 gtk_menu_shell_cancel (menu_shell);
3567 switch (event->keyval)
3569 case GDK_KEY_Delete:
3570 case GDK_KEY_KP_Delete:
3571 case GDK_KEY_BackSpace:
3578 /* Figure out what modifiers went into determining the key symbol */
3579 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3580 event->hardware_keycode,
3581 event->state, event->group,
3582 NULL, NULL, NULL, &consumed_modifiers);
3584 accel_key = gdk_keyval_to_lower (event->keyval);
3585 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3587 /* If lowercasing affects the keysym, then we need to include SHIFT
3588 * in the modifiers, we re-uppercase when we match against the keyval,
3589 * but display and save in caseless form.
3591 if (accel_key != event->keyval)
3592 accel_mods |= GDK_SHIFT_MASK;
3594 /* Modify the accelerators */
3595 if (can_change_accels &&
3596 menu_shell->priv->active_menu_item &&
3597 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3598 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3599 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3601 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3602 gboolean locked, replace_accels = TRUE;
3605 path = get_accel_path (menu_item, &locked);
3606 if (!path || locked)
3608 /* Can't change accelerators on menu_items without paths
3609 * (basically, those items are accelerator-locked).
3611 gtk_widget_error_bell (widget);
3617 /* For the keys that act to delete the current setting,
3618 * we delete the current setting if there is one, otherwise,
3619 * we set the key as the accelerator.
3625 if (gtk_accel_map_lookup_entry (path, &key) &&
3626 (key.accel_key || key.accel_mods))
3632 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3636 /* We failed, probably because this key is in use
3637 * and locked already.
3639 gtk_widget_error_bell (widget);
3648 check_threshold (GtkWidget *widget,
3657 ABS (start_x - x) > THRESHOLD ||
3658 ABS (start_y - y) > THRESHOLD;
3662 definitely_within_item (GtkWidget *widget,
3666 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3669 w = gdk_window_get_width (window);
3670 h = gdk_window_get_height (window);
3673 check_threshold (widget, 0, 0, x, y) &&
3674 check_threshold (widget, w - 1, 0, x, y) &&
3675 check_threshold (widget, w - 1, h - 1, x, y) &&
3676 check_threshold (widget, 0, h - 1, x, y);
3680 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3682 GtkMenuPrivate *priv = menu->priv;
3684 return priv->navigation_height && priv->navigation_width;
3688 gtk_menu_motion_notify (GtkWidget *widget,
3689 GdkEventMotion *event)
3691 GtkWidget *menu_item;
3693 GtkMenuShell *menu_shell;
3696 gboolean need_enter;
3698 if (GTK_IS_MENU (widget))
3700 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3702 if (priv->ignore_button_release)
3703 priv->ignore_button_release = FALSE;
3705 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3709 /* We received the event for one of two reasons:
3711 * a) We are the active menu, and did gtk_grab_add()
3712 * b) The widget is a child of ours, and the event was propagated
3714 * Since for computation of navigation regions, we want the menu which
3715 * is the parent of the menu item, for a), we need to find that menu,
3716 * which may be different from 'widget'.
3718 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3719 parent = gtk_widget_get_parent (menu_item);
3720 if (!GTK_IS_MENU_ITEM (menu_item) ||
3721 !GTK_IS_MENU (parent))
3724 menu_shell = GTK_MENU_SHELL (parent);
3725 menu = GTK_MENU (menu_shell);
3727 if (definitely_within_item (menu_item, event->x, event->y))
3728 menu_shell->priv->activate_time = 0;
3730 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3732 /* Check to see if we are within an active submenu's navigation region
3734 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3737 /* Make sure we pop down if we enter a non-selectable menu item, so we
3738 * don't show a submenu when the cursor is outside the stay-up triangle.
3740 if (!_gtk_menu_item_is_selectable (menu_item))
3742 /* We really want to deselect, but this gives the menushell code
3743 * a chance to do some bookkeeping about the menuitem.
3745 gtk_menu_shell_select_item (menu_shell, menu_item);
3751 /* The menu is now sensitive to enter events on its items, but
3752 * was previously sensitive. So we fake an enter event.
3754 menu_shell->priv->ignore_enter = FALSE;
3756 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3757 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3759 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3762 send_event->crossing.window = g_object_ref (event->window);
3763 send_event->crossing.time = event->time;
3764 send_event->crossing.send_event = TRUE;
3765 send_event->crossing.x_root = event->x_root;
3766 send_event->crossing.y_root = event->y_root;
3767 send_event->crossing.x = event->x;
3768 send_event->crossing.y = event->y;
3769 send_event->crossing.state = event->state;
3770 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3772 /* We send the event to 'widget', the currently active menu,
3773 * instead of 'menu', the menu that the pointer is in. This
3774 * will ensure that the event will be ignored unless the
3775 * menuitem is a child of the active menu or some parent
3776 * menu of the active menu.
3778 result = gtk_widget_event (widget, send_event);
3779 gdk_event_free (send_event);
3789 get_double_arrows (GtkMenu *menu)
3791 GtkMenuPrivate *priv = menu->priv;
3792 gboolean double_arrows;
3793 GtkArrowPlacement arrow_placement;
3795 gtk_widget_style_get (GTK_WIDGET (menu),
3796 "double-arrows", &double_arrows,
3797 "arrow-placement", &arrow_placement,
3800 if (arrow_placement != GTK_ARROWS_BOTH)
3803 return double_arrows || (priv->initially_pushed_in &&
3804 priv->scroll_offset != 0);
3808 gtk_menu_scroll_by (GtkMenu *menu,
3811 GtkMenuPrivate *priv = menu->priv;
3812 GtkBorder arrow_border;
3816 gboolean double_arrows;
3818 widget = GTK_WIDGET (menu);
3819 offset = priv->scroll_offset + step;
3821 get_arrows_border (menu, &arrow_border);
3823 double_arrows = get_double_arrows (menu);
3825 /* If we scroll upward and the non-visible top part
3826 * is smaller than the scroll arrow it would be
3827 * pretty stupid to show the arrow and taking more
3828 * screen space than just scrolling to the top.
3831 if ((step < 0) && (offset < arrow_border.top))
3834 /* Don't scroll over the top if we weren't before: */
3835 if ((priv->scroll_offset >= 0) && (offset < 0))
3838 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3840 if (priv->scroll_offset == 0 &&
3841 view_height >= priv->requested_height)
3844 /* Don't scroll past the bottom if we weren't before: */
3845 if (priv->scroll_offset > 0)
3846 view_height -= arrow_border.top;
3848 /* When both arrows are always shown,
3849 * reduce view height even more.
3852 view_height -= arrow_border.bottom;
3854 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3855 (offset + view_height > priv->requested_height))
3856 offset = priv->requested_height - view_height;
3858 if (offset != priv->scroll_offset)
3859 gtk_menu_scroll_to (menu, offset);
3863 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3864 gboolean touchscreen_mode)
3866 GtkMenuPrivate *priv = menu->priv;
3867 gboolean upper_visible;
3868 gboolean lower_visible;
3870 upper_visible = priv->upper_arrow_visible;
3871 lower_visible = priv->lower_arrow_visible;
3873 gtk_menu_scroll_by (menu, priv->scroll_step);
3875 if (touchscreen_mode &&
3876 (upper_visible != priv->upper_arrow_visible ||
3877 lower_visible != priv->lower_arrow_visible))
3879 /* We are about to hide a scroll arrow while the mouse is pressed,
3880 * this would cause the uncovered menu item to be activated on button
3881 * release. Therefore we need to ignore button release here
3883 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3884 priv->ignore_button_release = TRUE;
3889 gtk_menu_scroll_timeout (gpointer data)
3892 gboolean touchscreen_mode;
3894 menu = GTK_MENU (data);
3896 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3897 "gtk-touchscreen-mode", &touchscreen_mode,
3900 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3906 gtk_menu_scroll_timeout_initial (gpointer data)
3910 gboolean touchscreen_mode;
3912 menu = GTK_MENU (data);
3914 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3915 "gtk-timeout-repeat", &timeout,
3916 "gtk-touchscreen-mode", &touchscreen_mode,
3919 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3921 gtk_menu_remove_scroll_timeout (menu);
3923 menu->priv->scroll_timeout =
3924 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3930 gtk_menu_start_scrolling (GtkMenu *menu)
3933 gboolean touchscreen_mode;
3935 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3936 "gtk-timeout-repeat", &timeout,
3937 "gtk-touchscreen-mode", &touchscreen_mode,
3940 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3942 menu->priv->scroll_timeout =
3943 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3947 gtk_menu_scroll (GtkWidget *widget,
3948 GdkEventScroll *event)
3950 GtkMenu *menu = GTK_MENU (widget);
3952 switch (event->direction)
3954 case GDK_SCROLL_RIGHT:
3955 case GDK_SCROLL_DOWN:
3956 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3958 case GDK_SCROLL_LEFT:
3960 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3968 get_arrows_sensitive_area (GtkMenu *menu,
3969 GdkRectangle *upper,
3970 GdkRectangle *lower)
3972 GtkArrowPlacement arrow_placement;
3973 GtkWidget *widget = GTK_WIDGET (menu);
3977 guint vertical_padding;
3979 gint scroll_arrow_height;
3982 window = gtk_widget_get_window (widget);
3983 width = gdk_window_get_width (window);
3984 height = gdk_window_get_height (window);
3986 gtk_widget_style_get (widget,
3987 "vertical-padding", &vertical_padding,
3988 "scroll-arrow-vlength", &scroll_arrow_height,
3989 "arrow-placement", &arrow_placement,
3992 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3993 get_menu_padding (widget, &padding);
3995 gdk_window_get_position (window, &win_x, &win_y);
3997 switch (arrow_placement)
3999 case GTK_ARROWS_BOTH:
4004 upper->width = width;
4005 upper->height = scroll_arrow_height + border + padding.top;
4011 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
4012 lower->width = width;
4013 lower->height = scroll_arrow_height + border + padding.bottom;
4017 case GTK_ARROWS_START:
4022 upper->width = width / 2;
4023 upper->height = scroll_arrow_height + border + padding.top;
4028 lower->x = win_x + width / 2;
4030 lower->width = width / 2;
4031 lower->height = scroll_arrow_height + border + padding.bottom;
4035 case GTK_ARROWS_END:
4039 upper->y = win_y + height - border - scroll_arrow_height;
4040 upper->width = width / 2;
4041 upper->height = scroll_arrow_height + border + padding.top;
4046 lower->x = win_x + width / 2;
4047 lower->y = win_y + height - border - scroll_arrow_height;
4048 lower->width = width / 2;
4049 lower->height = scroll_arrow_height + border + padding.bottom;
4057 gtk_menu_handle_scrolling (GtkMenu *menu,
4063 GtkMenuPrivate *priv = menu->priv;
4064 GtkMenuShell *menu_shell;
4067 gboolean scroll_fast = FALSE;
4069 gboolean touchscreen_mode;
4071 menu_shell = GTK_MENU_SHELL (menu);
4073 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4074 "gtk-touchscreen-mode", &touchscreen_mode,
4077 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4082 /* upper arrow handling */
4084 get_arrows_sensitive_area (menu, &rect, NULL);
4087 if (priv->upper_arrow_visible && !priv->tearoff_active &&
4088 (x >= rect.x) && (x < rect.x + rect.width) &&
4089 (y >= rect.y) && (y < rect.y + rect.height))
4094 if (touchscreen_mode)
4095 priv->upper_arrow_prelight = in_arrow;
4097 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4099 gboolean arrow_pressed = FALSE;
4101 if (priv->upper_arrow_visible && !priv->tearoff_active)
4103 if (touchscreen_mode)
4105 if (enter && priv->upper_arrow_prelight)
4107 if (priv->scroll_timeout == 0)
4109 /* Deselect the active item so that
4110 * any submenus are popped down
4112 gtk_menu_shell_deselect (menu_shell);
4114 gtk_menu_remove_scroll_timeout (menu);
4115 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4119 /* Only do stuff on click. */
4120 gtk_menu_start_scrolling (menu);
4121 arrow_pressed = TRUE;
4126 arrow_pressed = TRUE;
4131 gtk_menu_stop_scrolling (menu);
4134 else /* !touchscreen_mode */
4136 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4138 if (enter && in_arrow &&
4139 (!priv->upper_arrow_prelight ||
4140 priv->scroll_fast != scroll_fast))
4142 priv->upper_arrow_prelight = TRUE;
4143 priv->scroll_fast = scroll_fast;
4145 /* Deselect the active item so that
4146 * any submenus are popped down
4148 gtk_menu_shell_deselect (menu_shell);
4150 gtk_menu_remove_scroll_timeout (menu);
4151 priv->scroll_step = scroll_fast
4152 ? -MENU_SCROLL_STEP2
4153 : -MENU_SCROLL_STEP1;
4155 priv->scroll_timeout =
4156 gdk_threads_add_timeout (scroll_fast
4157 ? MENU_SCROLL_TIMEOUT2
4158 : MENU_SCROLL_TIMEOUT1,
4159 gtk_menu_scroll_timeout, menu);
4161 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4163 gtk_menu_stop_scrolling (menu);
4168 /* gtk_menu_start_scrolling() might have hit the top of the
4169 * menu, so check if the button isn't insensitive before
4170 * changing it to something else.
4172 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4174 GtkStateFlags arrow_state = 0;
4177 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4179 if (priv->upper_arrow_prelight)
4180 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4182 if (arrow_state != priv->upper_arrow_state)
4184 priv->upper_arrow_state = arrow_state;
4186 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4192 /* lower arrow handling */
4194 get_arrows_sensitive_area (menu, NULL, &rect);
4197 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4198 (x >= rect.x) && (x < rect.x + rect.width) &&
4199 (y >= rect.y) && (y < rect.y + rect.height))
4204 if (touchscreen_mode)
4205 priv->lower_arrow_prelight = in_arrow;
4207 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4209 gboolean arrow_pressed = FALSE;
4211 if (priv->lower_arrow_visible && !priv->tearoff_active)
4213 if (touchscreen_mode)
4215 if (enter && priv->lower_arrow_prelight)
4217 if (priv->scroll_timeout == 0)
4219 /* Deselect the active item so that
4220 * any submenus are popped down
4222 gtk_menu_shell_deselect (menu_shell);
4224 gtk_menu_remove_scroll_timeout (menu);
4225 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4229 /* Only do stuff on click. */
4230 gtk_menu_start_scrolling (menu);
4231 arrow_pressed = TRUE;
4236 arrow_pressed = TRUE;
4241 gtk_menu_stop_scrolling (menu);
4244 else /* !touchscreen_mode */
4246 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4248 if (enter && in_arrow &&
4249 (!priv->lower_arrow_prelight ||
4250 priv->scroll_fast != scroll_fast))
4252 priv->lower_arrow_prelight = TRUE;
4253 priv->scroll_fast = scroll_fast;
4255 /* Deselect the active item so that
4256 * any submenus are popped down
4258 gtk_menu_shell_deselect (menu_shell);
4260 gtk_menu_remove_scroll_timeout (menu);
4261 priv->scroll_step = scroll_fast
4263 : MENU_SCROLL_STEP1;
4265 priv->scroll_timeout =
4266 gdk_threads_add_timeout (scroll_fast
4267 ? MENU_SCROLL_TIMEOUT2
4268 : MENU_SCROLL_TIMEOUT1,
4269 gtk_menu_scroll_timeout, menu);
4271 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4273 gtk_menu_stop_scrolling (menu);
4278 /* gtk_menu_start_scrolling() might have hit the bottom of the
4279 * menu, so check if the button isn't insensitive before
4280 * changing it to something else.
4282 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4284 GtkStateFlags arrow_state = 0;
4287 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4289 if (priv->lower_arrow_prelight)
4290 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4292 if (arrow_state != priv->lower_arrow_state)
4294 priv->lower_arrow_state = arrow_state;
4296 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4304 gtk_menu_enter_notify (GtkWidget *widget,
4305 GdkEventCrossing *event)
4307 GtkWidget *menu_item;
4309 gboolean touchscreen_mode;
4311 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4312 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4313 event->mode == GDK_CROSSING_STATE_CHANGED)
4316 g_object_get (gtk_widget_get_settings (widget),
4317 "gtk-touchscreen-mode", &touchscreen_mode,
4320 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4321 if (GTK_IS_MENU (widget))
4323 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4325 if (!menu_shell->priv->ignore_enter)
4326 gtk_menu_handle_scrolling (GTK_MENU (widget),
4327 event->x_root, event->y_root, TRUE, TRUE);
4330 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4332 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4334 if (GTK_IS_MENU (menu))
4336 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4337 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4339 if (priv->seen_item_enter)
4341 /* This is the second enter we see for an item
4342 * on this menu. This means a release should always
4345 menu_shell->priv->activate_time = 0;
4347 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4348 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4350 if (definitely_within_item (menu_item, event->x, event->y))
4352 /* This is an actual user-enter (ie. not a pop-under)
4353 * In this case, the user must either have entered
4354 * sufficiently far enough into the item, or he must move
4355 * far enough away from the enter point. (see
4356 * gtk_menu_motion_notify())
4358 menu_shell->priv->activate_time = 0;
4362 priv->seen_item_enter = TRUE;
4366 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4367 * will not correspond to the event widget's parent. Check to see
4368 * if we are in the parent's navigation region.
4370 parent = gtk_widget_get_parent (menu_item);
4371 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4372 gtk_menu_navigating_submenu (GTK_MENU (parent),
4373 event->x_root, event->y_root))
4376 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4380 gtk_menu_leave_notify (GtkWidget *widget,
4381 GdkEventCrossing *event)
4383 GtkMenuShell *menu_shell;
4385 GtkMenuItem *menu_item;
4386 GtkWidget *event_widget;
4388 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4389 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4390 event->mode == GDK_CROSSING_STATE_CHANGED)
4393 menu = GTK_MENU (widget);
4394 menu_shell = GTK_MENU_SHELL (widget);
4396 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4399 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4401 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4403 if (!GTK_IS_MENU_ITEM (event_widget))
4406 menu_item = GTK_MENU_ITEM (event_widget);
4408 /* Here we check to see if we're leaving an active menu item
4409 * with a submenu, in which case we enter submenu navigation mode.
4411 if (menu_shell->priv->active_menu_item != NULL
4412 && menu_item->priv->submenu != NULL
4413 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4415 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4417 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4420 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4422 /* We are leaving an active menu item with nonactive submenu.
4423 * Deselect it so we don't surprise the user with by popping
4424 * up a submenu _after_ he left the item.
4426 gtk_menu_shell_deselect (menu_shell);
4431 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4435 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4437 GtkMenuPrivate *priv = menu->priv;
4439 priv->navigation_x = 0;
4440 priv->navigation_y = 0;
4441 priv->navigation_width = 0;
4442 priv->navigation_height = 0;
4444 if (priv->navigation_timeout)
4446 g_source_remove (priv->navigation_timeout);
4447 priv->navigation_timeout = 0;
4451 /* When the timeout is elapsed, the navigation region is destroyed
4452 * and the menuitem under the pointer (if any) is selected.
4455 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4457 GtkMenuPopdownData *popdown_data = user_data;
4458 GtkMenu *menu = popdown_data->menu;
4459 GtkMenuPrivate *priv = menu->priv;
4460 GdkWindow *child_window;
4462 gtk_menu_stop_navigating_submenu (menu);
4464 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4466 child_window = gdk_window_get_device_position (priv->bin_window,
4467 popdown_data->device,
4472 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4474 send_event->crossing.window = g_object_ref (child_window);
4475 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4476 send_event->crossing.send_event = TRUE;
4477 gdk_event_set_device (send_event, popdown_data->device);
4479 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4481 gdk_event_free (send_event);
4489 gtk_menu_navigating_submenu (GtkMenu *menu,
4493 GtkMenuPrivate *priv = menu->priv;
4496 if (!gtk_menu_has_navigation_triangle (menu))
4499 width = priv->navigation_width;
4500 height = priv->navigation_height;
4502 /* Check if x/y are in the triangle spanned by the navigation parameters */
4504 /* 1) Move the coordinates so the triangle starts at 0,0 */
4505 event_x -= priv->navigation_x;
4506 event_y -= priv->navigation_y;
4508 /* 2) Ensure both legs move along the positive axis */
4520 /* 3) Check that the given coordinate is inside the triangle. The formula
4521 * is a transformed form of this formula: x/w + y/h <= 1
4523 if (event_x >= 0 && event_y >= 0 &&
4524 event_x * height + event_y * width <= width * height)
4530 gtk_menu_stop_navigating_submenu (menu);
4536 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4537 GtkMenuItem *menu_item,
4538 GdkEventCrossing *event)
4540 GtkMenuPrivate *priv = menu->priv;
4541 gint submenu_left = 0;
4542 gint submenu_right = 0;
4543 gint submenu_top = 0;
4544 gint submenu_bottom = 0;
4546 GtkWidget *event_widget;
4547 GtkMenuPopdownData *popdown_data;
4550 g_return_if_fail (menu_item->priv->submenu != NULL);
4551 g_return_if_fail (event != NULL);
4553 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4555 window = gtk_widget_get_window (menu_item->priv->submenu);
4556 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4558 submenu_right = submenu_left + gdk_window_get_width (window);
4559 submenu_bottom = submenu_top + gdk_window_get_height (window);
4561 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4563 if (event->x >= 0 && event->x < width)
4567 gtk_menu_stop_navigating_submenu (menu);
4569 /* The navigation region is the triangle closest to the x/y
4570 * location of the rectangle. This is why the width or height
4573 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4576 priv->navigation_x = submenu_left;
4577 priv->navigation_width = event->x_root - submenu_left;
4582 priv->navigation_x = submenu_right;
4583 priv->navigation_width = event->x_root - submenu_right;
4589 priv->navigation_y = event->y_root;
4590 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4592 if (priv->navigation_height >= 0)
4598 priv->navigation_y = event->y_root;
4599 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4601 if (priv->navigation_height <= 0)
4605 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4606 "gtk-menu-popdown-delay", &popdown_delay,
4609 popdown_data = g_new (GtkMenuPopdownData, 1);
4610 popdown_data->menu = menu;
4611 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4613 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4615 gtk_menu_stop_navigating_submenu_cb,
4617 (GDestroyNotify) g_free);
4622 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4626 g_return_if_fail (GTK_IS_MENU (menu_shell));
4628 parent = menu_shell->priv->parent_menu_shell;
4630 menu_shell->priv->activate_time = 0;
4631 gtk_menu_popdown (GTK_MENU (menu_shell));
4634 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4638 gtk_menu_position (GtkMenu *menu,
4639 gboolean set_scroll_offset)
4641 GtkMenuPrivate *priv = menu->priv;
4643 GtkRequisition requisition;
4648 GdkScreen *pointer_screen;
4649 GdkRectangle monitor;
4652 widget = GTK_WIDGET (menu);
4654 screen = gtk_widget_get_screen (widget);
4655 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4656 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4658 /* Get the minimum height for minimum width to figure out
4659 * the right place to popup the menu.
4661 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4663 if (pointer_screen != screen)
4665 /* Pointer is on a different screen; roughly center the
4666 * menu on the screen. If someone was using multiscreen
4667 * + Xinerama together they'd probably want something
4668 * fancier; but that is likely to be vanishingly rare.
4670 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4671 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4674 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4675 priv->initially_pushed_in = FALSE;
4677 /* Set the type hint here to allow custom position functions
4678 * to set a different hint
4680 if (!gtk_widget_get_visible (priv->toplevel))
4681 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4683 if (priv->position_func)
4685 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4686 priv->position_func_data);
4688 if (priv->monitor_num < 0)
4689 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4691 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4695 gint space_left, space_right, space_above, space_below;
4699 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4701 get_menu_padding (widget, &padding);
4703 /* The placement of popup menus horizontally works like this (with
4704 * RTL in parentheses)
4706 * - If there is enough room to the right (left) of the mouse cursor,
4707 * position the menu there.
4709 * - Otherwise, if if there is enough room to the left (right) of the
4710 * mouse cursor, position the menu there.
4712 * - Otherwise if the menu is smaller than the monitor, position it
4713 * on the side of the mouse cursor that has the most space available
4715 * - Otherwise (if there is simply not enough room for the menu on the
4716 * monitor), position it as far left (right) as possible.
4718 * Positioning in the vertical direction is similar: first try below
4719 * mouse cursor, then above.
4721 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4723 space_left = x - monitor.x;
4724 space_right = monitor.x + monitor.width - x - 1;
4725 space_above = y - monitor.y;
4726 space_below = monitor.y + monitor.height - y - 1;
4728 /* Position horizontally. */
4730 /* the amount of space we need to position the menu.
4731 * Note the menu is offset "thickness" pixels
4733 needed_width = requisition.width - padding.left;
4735 if (needed_width <= space_left ||
4736 needed_width <= space_right)
4738 if ((rtl && needed_width <= space_left) ||
4739 (!rtl && needed_width > space_right))
4742 x = x + padding.left - requisition.width + 1;
4746 /* position right */
4747 x = x - padding.right;
4750 /* x is clamped on-screen further down */
4752 else if (requisition.width <= monitor.width)
4754 /* the menu is too big to fit on either side of the mouse
4755 * cursor, but smaller than the monitor. Position it on
4756 * the side that has the most space
4758 if (space_left > space_right)
4766 x = monitor.x + monitor.width - requisition.width;
4769 else /* menu is simply too big for the monitor */
4774 x = monitor.x + monitor.width - requisition.width;
4783 /* Position vertically.
4784 * The algorithm is the same as above, but simpler
4785 * because we don't have to take RTL into account.
4787 needed_height = requisition.height - padding.top;
4789 if (needed_height <= space_above ||
4790 needed_height <= space_below)
4792 if (needed_height <= space_below)
4793 y = y - padding.top;
4795 y = y + padding.bottom - requisition.height + 1;
4797 y = CLAMP (y, monitor.y,
4798 monitor.y + monitor.height - requisition.height);
4800 else if (needed_height > space_below && needed_height > space_above)
4802 if (space_below >= space_above)
4803 y = monitor.y + monitor.height - requisition.height;
4815 if (priv->initially_pushed_in)
4817 menu_height = requisition.height;
4819 if (y + menu_height > monitor.y + monitor.height)
4821 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4822 y = (monitor.y + monitor.height) - menu_height;
4827 scroll_offset += monitor.y - y;
4832 /* FIXME: should this be done in the various position_funcs ? */
4833 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4835 if (GTK_MENU_SHELL (menu)->priv->active)
4837 priv->have_position = TRUE;
4838 priv->position_x = x;
4839 priv->position_y = y;
4842 if (y + requisition.height > monitor.y + monitor.height)
4843 requisition.height = (monitor.y + monitor.height) - y;
4847 scroll_offset += monitor.y - y;
4848 requisition.height -= monitor.y - y;
4852 if (scroll_offset > 0)
4854 GtkBorder arrow_border;
4856 get_arrows_border (menu, &arrow_border);
4857 scroll_offset += arrow_border.top;
4860 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4863 if (!GTK_MENU_SHELL (menu)->priv->active)
4865 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4866 requisition.width, requisition.height);
4869 if (set_scroll_offset)
4870 priv->scroll_offset = scroll_offset;
4874 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4876 GtkMenuPrivate *priv = menu->priv;
4878 if (priv->scroll_timeout)
4880 g_source_remove (priv->scroll_timeout);
4881 priv->scroll_timeout = 0;
4886 gtk_menu_stop_scrolling (GtkMenu *menu)
4888 GtkMenuPrivate *priv = menu->priv;
4889 gboolean touchscreen_mode;
4891 gtk_menu_remove_scroll_timeout (menu);
4893 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4894 "gtk-touchscreen-mode", &touchscreen_mode,
4897 if (!touchscreen_mode)
4899 priv->upper_arrow_prelight = FALSE;
4900 priv->lower_arrow_prelight = FALSE;
4905 gtk_menu_scroll_to (GtkMenu *menu,
4908 GtkMenuPrivate *priv = menu->priv;
4909 GtkAllocation allocation;
4910 GtkBorder arrow_border, padding;
4913 gint view_width, view_height;
4916 guint vertical_padding;
4917 guint horizontal_padding;
4918 gboolean double_arrows;
4920 widget = GTK_WIDGET (menu);
4922 if (priv->tearoff_active && priv->tearoff_adjustment)
4923 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4925 /* Move/resize the viewport according to arrows: */
4926 gtk_widget_get_allocation (widget, &allocation);
4927 view_width = allocation.width;
4928 view_height = allocation.height;
4930 gtk_widget_style_get (GTK_WIDGET (menu),
4931 "vertical-padding", &vertical_padding,
4932 "horizontal-padding", &horizontal_padding,
4935 get_menu_padding (widget, &padding);
4936 double_arrows = get_double_arrows (menu);
4938 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4940 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4941 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4942 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4943 padding.top - padding.bottom;
4945 x = border_width + padding.left + horizontal_padding;
4946 y = border_width + padding.top + vertical_padding;
4948 if (double_arrows && !priv->tearoff_active)
4950 if (view_height < menu_height ||
4951 (offset > 0 && priv->scroll_offset > 0) ||
4952 (offset < 0 && priv->scroll_offset < 0))
4954 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4955 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4957 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4958 gtk_widget_queue_draw (GTK_WIDGET (menu));
4960 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4962 get_arrows_border (menu, &arrow_border);
4963 y += arrow_border.top;
4964 view_height -= arrow_border.top;
4965 view_height -= arrow_border.bottom;
4968 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4971 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4973 if (priv->upper_arrow_prelight)
4974 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4976 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4979 if (offset >= menu_height - view_height)
4980 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4983 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4985 if (priv->lower_arrow_prelight)
4986 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4988 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4991 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4992 (priv->lower_arrow_state != lower_arrow_previous_state))
4993 gtk_widget_queue_draw (GTK_WIDGET (menu));
4995 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4996 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4998 /* At the upper border, possibly remove timeout */
4999 if (priv->scroll_step < 0)
5001 gtk_menu_stop_scrolling (menu);
5002 gtk_widget_queue_draw (GTK_WIDGET (menu));
5006 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5007 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5009 /* At the lower border, possibly remove timeout */
5010 if (priv->scroll_step > 0)
5012 gtk_menu_stop_scrolling (menu);
5013 gtk_widget_queue_draw (GTK_WIDGET (menu));
5017 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
5021 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
5022 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
5024 gtk_menu_stop_scrolling (menu);
5025 gtk_widget_queue_draw (GTK_WIDGET (menu));
5028 else if (!priv->tearoff_active)
5030 gboolean last_visible;
5032 last_visible = priv->upper_arrow_visible;
5033 priv->upper_arrow_visible = offset > 0;
5035 /* upper_arrow_visible may have changed, so requery the border */
5036 get_arrows_border (menu, &arrow_border);
5037 view_height -= arrow_border.top;
5039 if ((last_visible != priv->upper_arrow_visible) &&
5040 !priv->upper_arrow_visible)
5042 priv->upper_arrow_prelight = FALSE;
5044 /* If we hide the upper arrow, possibly remove timeout */
5045 if (priv->scroll_step < 0)
5047 gtk_menu_stop_scrolling (menu);
5048 gtk_widget_queue_draw (GTK_WIDGET (menu));
5052 last_visible = priv->lower_arrow_visible;
5053 priv->lower_arrow_visible = offset < menu_height - view_height;
5055 /* lower_arrow_visible may have changed, so requery the border */
5056 get_arrows_border (menu, &arrow_border);
5057 view_height -= arrow_border.bottom;
5059 if ((last_visible != priv->lower_arrow_visible) &&
5060 !priv->lower_arrow_visible)
5062 priv->lower_arrow_prelight = FALSE;
5064 /* If we hide the lower arrow, possibly remove timeout */
5065 if (priv->scroll_step > 0)
5067 gtk_menu_stop_scrolling (menu);
5068 gtk_widget_queue_draw (GTK_WIDGET (menu));
5072 y += arrow_border.top;
5075 /* Scroll the menu: */
5076 if (gtk_widget_get_realized (widget))
5077 gdk_window_move (priv->bin_window, 0, -offset);
5079 if (gtk_widget_get_realized (widget))
5080 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5082 priv->scroll_offset = offset;
5086 compute_child_offset (GtkMenu *menu,
5087 GtkWidget *menu_item,
5090 gboolean *is_last_child)
5092 GtkMenuPrivate *priv = menu->priv;
5093 gint item_top_attach;
5094 gint item_bottom_attach;
5095 gint child_offset = 0;
5098 get_effective_child_attach (menu_item, NULL, NULL,
5099 &item_top_attach, &item_bottom_attach);
5101 /* there is a possibility that we get called before _size_request,
5102 * so check the height table for safety.
5104 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5107 /* when we have a row with only invisible children, its height will
5108 * be zero, so there's no need to check WIDGET_VISIBLE here
5110 for (i = 0; i < item_top_attach; i++)
5111 child_offset += priv->heights[i];
5114 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5116 *offset = child_offset;
5118 *height = priv->heights[item_top_attach];
5124 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5125 GtkWidget *menu_item)
5127 GtkMenu *menu = GTK_MENU (menu_shell);
5128 GtkMenuPrivate *priv = menu->priv;
5129 GtkWidget *widget = GTK_WIDGET (menu_shell);
5130 gint child_offset, child_height;
5134 gboolean last_child = 0;
5136 /* We need to check if the selected item fully visible.
5137 * If not we need to scroll the menu so that it becomes fully
5140 if (compute_child_offset (menu, menu_item,
5141 &child_offset, &child_height, &last_child))
5143 guint vertical_padding;
5144 gboolean double_arrows;
5147 y = priv->scroll_offset;
5148 height = gdk_window_get_height (gtk_widget_get_window (widget));
5150 gtk_widget_style_get (widget,
5151 "vertical-padding", &vertical_padding,
5154 double_arrows = get_double_arrows (menu);
5155 get_menu_padding (widget, &padding);
5157 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5158 padding.top + padding.bottom +
5159 2 * vertical_padding;
5160 if (child_offset < y)
5162 /* Ignore the enter event we might get if the pointer
5165 menu_shell->priv->ignore_enter = TRUE;
5166 gtk_menu_scroll_to (menu, child_offset);
5170 GtkBorder arrow_border;
5174 get_arrows_border (menu, &arrow_border);
5175 if (!priv->tearoff_active)
5176 arrow_height = arrow_border.top + arrow_border.bottom;
5178 if (child_offset + child_height > y + height - arrow_height)
5181 if ((!last_child && !priv->tearoff_active) || double_arrows)
5182 arrow_height += arrow_border.bottom;
5184 y = child_offset + child_height - height + arrow_height;
5185 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5187 /* Need upper arrow */
5188 arrow_height += arrow_border.top;
5189 y = child_offset + child_height - height + arrow_height;
5191 /* Ignore the enter event we might get if the pointer
5194 menu_shell->priv->ignore_enter = TRUE;
5195 gtk_menu_scroll_to (menu, y);
5202 gtk_menu_select_item (GtkMenuShell *menu_shell,
5203 GtkWidget *menu_item)
5205 GtkMenu *menu = GTK_MENU (menu_shell);
5207 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5208 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5210 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5214 /* Reparent the menu, taking care of the refcounting
5216 * If unrealize is true we force a unrealize while reparenting the parent.
5217 * This can help eliminate flicker in some cases.
5219 * What happens is that when the menu is unrealized and then re-realized,
5220 * the allocations are as follows:
5222 * parent - 1x1 at (0,0)
5223 * child1 - 100x20 at (0,0)
5224 * child2 - 100x20 at (0,20)
5225 * child3 - 100x20 at (0,40)
5227 * That is, the parent is small but the children are full sized. Then,
5228 * when the queued_resize gets processed, the parent gets resized to
5231 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5232 * contains the following logic:
5234 * - if a move or resize operation on a window would change the clip
5235 * region on the children, then before the window is resized
5236 * the background for children is temporarily set to None, the
5237 * move/resize done, and the background for the children restored.
5239 * So, at the point where the parent is resized to final size, the
5240 * background for the children is temporarily None, and thus they
5241 * are not cleared to the background color and the previous background
5242 * (the image of the menu) is left in place.
5245 gtk_menu_reparent (GtkMenu *menu,
5246 GtkWidget *new_parent,
5249 GObject *object = G_OBJECT (menu);
5250 GtkWidget *widget = GTK_WIDGET (menu);
5251 gboolean was_floating = g_object_is_floating (object);
5253 g_object_ref_sink (object);
5257 g_object_ref (object);
5258 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5259 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5260 g_object_unref (object);
5263 gtk_widget_reparent (widget, new_parent);
5266 g_object_force_floating (object);
5268 g_object_unref (object);
5272 gtk_menu_show_all (GtkWidget *widget)
5274 /* Show children, but not self. */
5275 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5279 * gtk_menu_set_screen:
5281 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5282 * determined by the widget the menu is attached to
5284 * Sets the #GdkScreen on which the menu will be displayed.
5289 gtk_menu_set_screen (GtkMenu *menu,
5292 g_return_if_fail (GTK_IS_MENU (menu));
5293 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5295 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5299 menu_change_screen (menu, screen);
5303 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5305 attach_widget_screen_changed (attach_widget, NULL, menu);
5312 * @child: a #GtkMenuItem
5313 * @left_attach: The column number to attach the left side of the item to
5314 * @right_attach: The column number to attach the right side of the item to
5315 * @top_attach: The row number to attach the top of the item to
5316 * @bottom_attach: The row number to attach the bottom of the item to
5318 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5319 * an item will occupy is specified by @left_attach, @right_attach,
5320 * @top_attach and @bottom_attach. These each represent the leftmost,
5321 * rightmost, uppermost and lower column and row numbers of the table.
5322 * (Columns and rows are indexed from zero).
5324 * Note that this function is not related to gtk_menu_detach().
5329 gtk_menu_attach (GtkMenu *menu,
5334 guint bottom_attach)
5336 GtkMenuShell *menu_shell;
5339 g_return_if_fail (GTK_IS_MENU (menu));
5340 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5341 parent = gtk_widget_get_parent (child);
5342 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5343 g_return_if_fail (left_attach < right_attach);
5344 g_return_if_fail (top_attach < bottom_attach);
5346 menu_shell = GTK_MENU_SHELL (menu);
5350 AttachInfo *ai = get_attach_info (child);
5352 ai->left_attach = left_attach;
5353 ai->right_attach = right_attach;
5354 ai->top_attach = top_attach;
5355 ai->bottom_attach = bottom_attach;
5357 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5359 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5361 menu_queue_resize (menu);
5365 gtk_container_child_set (GTK_CONTAINER (parent), child,
5366 "left-attach", left_attach,
5367 "right-attach", right_attach,
5368 "top-attach", top_attach,
5369 "bottom-attach", bottom_attach,
5375 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5379 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5380 "gtk-menu-popup-delay", &popup_delay,
5387 find_child_containing (GtkMenuShell *menu_shell,
5395 /* find a child which includes the area given by
5396 * left, right, top, bottom.
5398 for (list = menu_shell->priv->children; list; list = list->next)
5402 if (!_gtk_menu_item_is_selectable (list->data))
5405 get_effective_child_attach (list->data, &l, &r, &t, &b);
5407 if (l <= left && right <= r && t <= top && bottom <= b)
5408 return GTK_WIDGET (list->data);
5415 gtk_menu_move_current (GtkMenuShell *menu_shell,
5416 GtkMenuDirectionType direction)
5418 GtkMenu *menu = GTK_MENU (menu_shell);
5421 GtkWidget *match = NULL;
5423 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5427 case GTK_MENU_DIR_CHILD:
5428 direction = GTK_MENU_DIR_PARENT;
5430 case GTK_MENU_DIR_PARENT:
5431 direction = GTK_MENU_DIR_CHILD;
5437 /* use special table menu key bindings */
5438 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5440 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5442 if (direction == GTK_MENU_DIR_NEXT)
5444 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5446 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5454 for (i = 0; i < t; i++)
5456 match = find_child_containing (menu_shell,
5457 l, l + 1, i, i + 1);
5463 else if (direction == GTK_MENU_DIR_PREV)
5465 for (i = t; i > 0; i--)
5467 match = find_child_containing (menu_shell,
5468 l, l + 1, i - 1, i);
5476 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5478 match = find_child_containing (menu_shell,
5479 l, l + 1, i - 1, i);
5485 else if (direction == GTK_MENU_DIR_PARENT)
5487 /* we go one left if possible */
5489 match = find_child_containing (menu_shell,
5490 l - 1, l, t, t + 1);
5494 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5497 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5498 match = menu_shell->priv->active_menu_item;
5501 else if (direction == GTK_MENU_DIR_CHILD)
5503 /* we go one right if possible */
5504 if (r < gtk_menu_get_n_columns (menu))
5505 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5509 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5511 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5513 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5514 match = menu_shell->priv->active_menu_item;
5520 gtk_menu_shell_select_item (menu_shell, match);
5525 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5529 get_visible_size (GtkMenu *menu)
5531 GtkMenuPrivate *priv = menu->priv;
5532 GtkAllocation allocation;
5533 GtkWidget *widget = GTK_WIDGET (menu);
5534 GtkContainer *container = GTK_CONTAINER (menu);
5538 gtk_widget_get_allocation (widget, &allocation);
5539 get_menu_padding (widget, &padding);
5541 menu_height = (allocation.height -
5542 (2 * gtk_container_get_border_width (container)) -
5543 padding.top - padding.bottom);
5545 if (!priv->tearoff_active)
5547 GtkBorder arrow_border;
5549 get_arrows_border (menu, &arrow_border);
5550 menu_height -= arrow_border.top;
5551 menu_height -= arrow_border.bottom;
5557 /* Find the sensitive on-screen child containing @y, or if none,
5558 * the nearest selectable onscreen child. (%NULL if none)
5561 child_at (GtkMenu *menu,
5564 GtkMenuPrivate *priv = menu->priv;
5565 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5566 GtkWidget *child = NULL;
5567 gint child_offset = 0;
5570 gint lower, upper; /* Onscreen bounds */
5572 menu_height = get_visible_size (menu);
5573 lower = priv->scroll_offset;
5574 upper = priv->scroll_offset + menu_height;
5576 for (children = menu_shell->priv->children; children; children = children->next)
5578 if (gtk_widget_get_visible (children->data))
5580 GtkRequisition child_requisition;
5582 gtk_widget_get_preferred_size (children->data,
5583 &child_requisition, NULL);
5585 if (_gtk_menu_item_is_selectable (children->data) &&
5586 child_offset >= lower &&
5587 child_offset + child_requisition.height <= upper)
5589 child = children->data;
5591 if (child_offset + child_requisition.height > y &&
5592 !GTK_IS_TEAROFF_MENU_ITEM (child))
5596 child_offset += child_requisition.height;
5604 get_menu_height (GtkMenu *menu)
5606 GtkMenuPrivate *priv = menu->priv;
5607 GtkAllocation allocation;
5608 GtkWidget *widget = GTK_WIDGET (menu);
5612 gtk_widget_get_allocation (widget, &allocation);
5613 get_menu_padding (widget, &padding);
5615 height = allocation.height;
5616 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5617 padding.top + padding.bottom;
5619 if (!priv->tearoff_active)
5621 GtkBorder arrow_border;
5623 get_arrows_border (menu, &arrow_border);
5624 height -= arrow_border.top;
5625 height -= arrow_border.bottom;
5632 gtk_menu_real_move_scroll (GtkMenu *menu,
5635 GtkMenuPrivate *priv = menu->priv;
5636 gint page_size = get_visible_size (menu);
5637 gint end_position = get_menu_height (menu);
5638 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5642 case GTK_SCROLL_PAGE_UP:
5643 case GTK_SCROLL_PAGE_DOWN:
5647 gint child_offset = 0;
5648 gboolean old_upper_arrow_visible;
5651 if (type == GTK_SCROLL_PAGE_UP)
5656 if (menu_shell->priv->active_menu_item)
5660 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5661 &child_offset, &child_height, NULL);
5662 child_offset += child_height / 2;
5665 menu_shell->priv->ignore_enter = TRUE;
5666 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5667 old_offset = priv->scroll_offset;
5669 new_offset = priv->scroll_offset + step;
5670 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5672 gtk_menu_scroll_to (menu, new_offset);
5674 if (menu_shell->priv->active_menu_item)
5676 GtkWidget *new_child;
5677 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5678 GtkBorder arrow_border;
5679 get_arrows_border (menu, &arrow_border);
5681 if (priv->scroll_offset != old_offset)
5682 step = priv->scroll_offset - old_offset;
5684 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5686 new_child = child_at (menu, child_offset + step);
5688 gtk_menu_shell_select_item (menu_shell, new_child);
5692 case GTK_SCROLL_START:
5693 /* Ignore the enter event we might get if the pointer is on the menu */
5694 menu_shell->priv->ignore_enter = TRUE;
5695 gtk_menu_scroll_to (menu, 0);
5696 gtk_menu_shell_select_first (menu_shell, TRUE);
5698 case GTK_SCROLL_END:
5699 /* Ignore the enter event we might get if the pointer is on the menu */
5700 menu_shell->priv->ignore_enter = TRUE;
5701 gtk_menu_scroll_to (menu, end_position - page_size);
5702 _gtk_menu_shell_select_last (menu_shell, TRUE);
5711 * gtk_menu_set_monitor:
5713 * @monitor_num: the number of the monitor on which the menu should
5716 * Informs GTK+ on which monitor a menu should be popped up.
5717 * See gdk_screen_get_monitor_geometry().
5719 * This function should be called from a #GtkMenuPositionFunc
5720 * if the menu should not appear on the same monitor as the pointer.
5721 * This information can't be reliably inferred from the coordinates
5722 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5723 * these coordinates may extend beyond the monitor boundaries or even
5724 * the screen boundaries.
5729 gtk_menu_set_monitor (GtkMenu *menu,
5732 GtkMenuPrivate *priv = menu->priv;
5734 g_return_if_fail (GTK_IS_MENU (menu));
5736 priv->monitor_num = monitor_num;
5740 * gtk_menu_get_monitor:
5743 * Retrieves the number of the monitor on which to show the menu.
5745 * Returns: the number of the monitor on which the menu should
5746 * be popped up or -1, if no monitor has been set
5751 gtk_menu_get_monitor (GtkMenu *menu)
5753 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5755 return menu->priv->monitor_num;
5759 * gtk_menu_get_for_attach_widget:
5760 * @widget: a #GtkWidget
5762 * Returns a list of the menus which are attached to this widget.
5763 * This list is owned by GTK+ and must not be modified.
5765 * Return value: (element-type GtkWidget) (transfer none): the list
5766 * of menus attached to his widget.
5771 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5775 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5777 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5783 gtk_menu_grab_notify (GtkWidget *widget,
5784 gboolean was_grabbed)
5786 GtkWidget *toplevel;
5787 GtkWindowGroup *group;
5791 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5794 !gtk_widget_device_is_shadowed (widget, pointer))
5797 toplevel = gtk_widget_get_toplevel (widget);
5799 if (!GTK_IS_WINDOW (toplevel))
5802 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5803 grab = gtk_window_group_get_current_device_grab (group, pointer);
5805 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5806 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5810 * gtk_menu_set_reserve_toggle_size:
5812 * @reserve_toggle_size: whether to reserve size for toggles
5814 * Sets whether the menu should reserve space for drawing toggles
5815 * or icons, regardless of their actual presence.
5820 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5821 gboolean reserve_toggle_size)
5823 GtkMenuPrivate *priv = menu->priv;
5824 gboolean no_toggle_size;
5826 g_return_if_fail (GTK_IS_MENU (menu));
5828 no_toggle_size = !reserve_toggle_size;
5830 if (priv->no_toggle_size != no_toggle_size)
5832 priv->no_toggle_size = no_toggle_size;
5834 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5839 * gtk_menu_get_reserve_toggle_size:
5842 * Returns whether the menu reserves space for toggles and
5843 * icons, regardless of their actual presence.
5845 * Returns: Whether the menu reserves toggle space
5850 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5852 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5854 return !menu->priv->no_toggle_size;