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 #include "gdk/gdkkeysyms.h"
30 #include "gtkaccellabel.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtkcheckmenuitem.h"
34 #include <gobject/gvaluecollector.h>
36 #include "gtkmarshalers.h"
38 #include "gtkmenuprivate.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtksizerequest.h"
45 #include "gtkprivate.h"
48 #define DEFAULT_POPUP_DELAY 225
49 #define DEFAULT_POPDOWN_DELAY 1000
51 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
52 * extends below the submenu
55 #define MENU_SCROLL_STEP1 8
56 #define MENU_SCROLL_STEP2 15
57 #define MENU_SCROLL_FAST_ZONE 8
58 #define MENU_SCROLL_TIMEOUT1 50
59 #define MENU_SCROLL_TIMEOUT2 20
61 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
62 #define ATTACHED_MENUS "gtk-attached-menus"
64 typedef struct _GtkMenuAttachData GtkMenuAttachData;
65 typedef struct _GtkMenuPrivate GtkMenuPrivate;
66 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
68 struct _GtkMenuAttachData
70 GtkWidget *attach_widget;
71 GtkMenuDetachFunc detacher;
74 struct _GtkMenuPrivate
78 gboolean initially_pushed_in;
80 /* info used for the table */
83 gint requested_height;
87 /* Cached layout information */
96 GtkStateType lower_arrow_state;
97 GtkStateType upper_arrow_state;
99 /* navigation region */
102 int navigation_width;
103 int navigation_height;
105 guint have_layout : 1;
106 guint seen_item_enter : 1;
107 guint have_position : 1;
108 guint ignore_button_release : 1;
109 guint no_toggle_size : 1;
112 struct _GtkMenuPopdownData
124 gint effective_left_attach;
125 gint effective_right_attach;
126 gint effective_top_attach;
127 gint effective_bottom_attach;
144 PROP_RESERVE_TOGGLE_SIZE
149 CHILD_PROP_LEFT_ATTACH,
150 CHILD_PROP_RIGHT_ATTACH,
151 CHILD_PROP_TOP_ATTACH,
152 CHILD_PROP_BOTTOM_ATTACH
155 static void gtk_menu_set_property (GObject *object,
159 static void gtk_menu_get_property (GObject *object,
163 static void gtk_menu_set_child_property(GtkContainer *container,
168 static void gtk_menu_get_child_property(GtkContainer *container,
173 static void gtk_menu_destroy (GtkObject *object);
174 static void gtk_menu_realize (GtkWidget *widget);
175 static void gtk_menu_unrealize (GtkWidget *widget);
176 static void gtk_menu_size_allocate (GtkWidget *widget,
177 GtkAllocation *allocation);
178 static void gtk_menu_paint (GtkWidget *widget,
179 GdkEventExpose *expose);
180 static void gtk_menu_show (GtkWidget *widget);
181 static gboolean gtk_menu_expose (GtkWidget *widget,
182 GdkEventExpose *event);
183 static gboolean gtk_menu_key_press (GtkWidget *widget,
185 static gboolean gtk_menu_scroll (GtkWidget *widget,
186 GdkEventScroll *event);
187 static gboolean gtk_menu_button_press (GtkWidget *widget,
188 GdkEventButton *event);
189 static gboolean gtk_menu_button_release (GtkWidget *widget,
190 GdkEventButton *event);
191 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
192 GdkEventMotion *event);
193 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
194 GdkEventCrossing *event);
195 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
196 GdkEventCrossing *event);
197 static void gtk_menu_scroll_to (GtkMenu *menu,
199 static void gtk_menu_grab_notify (GtkWidget *widget,
200 gboolean was_grabbed);
202 static void gtk_menu_stop_scrolling (GtkMenu *menu);
203 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
204 static gboolean gtk_menu_scroll_timeout (gpointer data);
205 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
206 static void gtk_menu_start_scrolling (GtkMenu *menu);
208 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
209 GtkWidget *menu_item);
210 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
211 GtkWidget *menu_item);
212 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
215 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
217 static void gtk_menu_handle_scrolling (GtkMenu *menu,
222 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
224 static void gtk_menu_style_set (GtkWidget *widget,
225 GtkStyle *previous_style);
226 static gboolean gtk_menu_focus (GtkWidget *widget,
227 GtkDirectionType direction);
228 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
229 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
230 GtkMenuDirectionType direction);
231 static void gtk_menu_real_move_scroll (GtkMenu *menu,
234 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
235 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
236 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
239 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
240 GtkMenuItem *menu_item,
241 GdkEventCrossing *event);
243 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
244 static void gtk_menu_show_all (GtkWidget *widget);
245 static void gtk_menu_hide_all (GtkWidget *widget);
246 static void gtk_menu_position (GtkMenu *menu);
247 static void gtk_menu_reparent (GtkMenu *menu,
248 GtkWidget *new_parent,
250 static void gtk_menu_remove (GtkContainer *menu,
253 static void gtk_menu_update_title (GtkMenu *menu);
255 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
256 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
258 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
260 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
261 gboolean group_changed);
263 static void gtk_menu_size_request_init (GtkSizeRequestIface *iface);
264 static void gtk_menu_get_width (GtkSizeRequest *widget,
267 static void gtk_menu_get_height (GtkSizeRequest *widget,
270 static void gtk_menu_get_height_for_width (GtkSizeRequest *widget,
276 static const gchar attach_data_key[] = "gtk-menu-attach-data";
278 static guint menu_signals[LAST_SIGNAL] = { 0 };
280 static GtkMenuPrivate *
281 gtk_menu_get_private (GtkMenu *menu)
283 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
286 G_DEFINE_TYPE_WITH_CODE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL,
287 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
288 gtk_menu_size_request_init))
291 menu_queue_resize (GtkMenu *menu)
293 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
295 priv->have_layout = FALSE;
296 gtk_widget_queue_resize (GTK_WIDGET (menu));
300 attach_info_free (AttachInfo *info)
302 g_slice_free (AttachInfo, info);
306 get_attach_info (GtkWidget *child)
308 GObject *object = G_OBJECT (child);
309 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
313 ai = g_slice_new0 (AttachInfo);
314 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
315 (GDestroyNotify) attach_info_free);
322 is_grid_attached (AttachInfo *ai)
324 return (ai->left_attach >= 0 &&
325 ai->right_attach >= 0 &&
326 ai->top_attach >= 0 &&
327 ai->bottom_attach >= 0);
331 menu_ensure_layout (GtkMenu *menu)
333 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
335 if (!priv->have_layout)
337 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
341 gint max_right_attach;
342 gint max_bottom_attach;
344 /* Find extents of gridded portion
346 max_right_attach = 1;
347 max_bottom_attach = 0;
349 for (l = menu_shell->children; l; l = l->next)
351 GtkWidget *child = l->data;
352 AttachInfo *ai = get_attach_info (child);
354 if (is_grid_attached (ai))
356 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
357 max_right_attach = MAX (max_right_attach, ai->right_attach);
363 row_occupied = g_malloc0 (max_bottom_attach);
365 for (l = menu_shell->children; l; l = l->next)
367 GtkWidget *child = l->data;
368 AttachInfo *ai = get_attach_info (child);
370 if (is_grid_attached (ai))
374 for (i = ai->top_attach; i < ai->bottom_attach; i++)
375 row_occupied[i] = TRUE;
379 /* Lay non-grid-items out in those rows
382 for (l = menu_shell->children; l; l = l->next)
384 GtkWidget *child = l->data;
385 AttachInfo *ai = get_attach_info (child);
387 if (!is_grid_attached (ai))
389 while (current_row < max_bottom_attach && row_occupied[current_row])
392 ai->effective_left_attach = 0;
393 ai->effective_right_attach = max_right_attach;
394 ai->effective_top_attach = current_row;
395 ai->effective_bottom_attach = current_row + 1;
401 ai->effective_left_attach = ai->left_attach;
402 ai->effective_right_attach = ai->right_attach;
403 ai->effective_top_attach = ai->top_attach;
404 ai->effective_bottom_attach = ai->bottom_attach;
408 g_free (row_occupied);
410 priv->n_rows = MAX (current_row, max_bottom_attach);
411 priv->n_columns = max_right_attach;
412 priv->have_layout = TRUE;
418 gtk_menu_get_n_columns (GtkMenu *menu)
420 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
422 menu_ensure_layout (menu);
424 return priv->n_columns;
428 gtk_menu_get_n_rows (GtkMenu *menu)
430 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
432 menu_ensure_layout (menu);
438 get_effective_child_attach (GtkWidget *child,
444 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
447 menu_ensure_layout (menu);
449 ai = get_attach_info (child);
452 *l = ai->effective_left_attach;
454 *r = ai->effective_right_attach;
456 *t = ai->effective_top_attach;
458 *b = ai->effective_bottom_attach;
463 gtk_menu_class_init (GtkMenuClass *class)
465 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
466 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
467 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
468 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
469 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
470 GtkBindingSet *binding_set;
472 gobject_class->set_property = gtk_menu_set_property;
473 gobject_class->get_property = gtk_menu_get_property;
475 object_class->destroy = gtk_menu_destroy;
477 widget_class->realize = gtk_menu_realize;
478 widget_class->unrealize = gtk_menu_unrealize;
479 widget_class->size_allocate = gtk_menu_size_allocate;
480 widget_class->show = gtk_menu_show;
481 widget_class->expose_event = gtk_menu_expose;
482 widget_class->scroll_event = gtk_menu_scroll;
483 widget_class->key_press_event = gtk_menu_key_press;
484 widget_class->button_press_event = gtk_menu_button_press;
485 widget_class->button_release_event = gtk_menu_button_release;
486 widget_class->motion_notify_event = gtk_menu_motion_notify;
487 widget_class->show_all = gtk_menu_show_all;
488 widget_class->hide_all = gtk_menu_hide_all;
489 widget_class->enter_notify_event = gtk_menu_enter_notify;
490 widget_class->leave_notify_event = gtk_menu_leave_notify;
491 widget_class->style_set = gtk_menu_style_set;
492 widget_class->focus = gtk_menu_focus;
493 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
494 widget_class->grab_notify = gtk_menu_grab_notify;
496 container_class->remove = gtk_menu_remove;
497 container_class->get_child_property = gtk_menu_get_child_property;
498 container_class->set_child_property = gtk_menu_set_child_property;
500 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
501 menu_shell_class->deactivate = gtk_menu_deactivate;
502 menu_shell_class->select_item = gtk_menu_select_item;
503 menu_shell_class->insert = gtk_menu_real_insert;
504 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
505 menu_shell_class->move_current = gtk_menu_move_current;
507 menu_signals[MOVE_SCROLL] =
508 g_signal_new_class_handler (I_("move-scroll"),
509 G_OBJECT_CLASS_TYPE (object_class),
510 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
511 G_CALLBACK (gtk_menu_real_move_scroll),
513 _gtk_marshal_VOID__ENUM,
515 GTK_TYPE_SCROLL_TYPE);
520 * The index of the currently selected menu item, or -1 if no
521 * menu item is selected.
525 g_object_class_install_property (gobject_class,
527 g_param_spec_int ("active",
529 P_("The currently selected menu item"),
531 GTK_PARAM_READWRITE));
534 * GtkMenu:accel-group:
536 * The accel group holding accelerators for the menu.
540 g_object_class_install_property (gobject_class,
542 g_param_spec_object ("accel-group",
544 P_("The accel group holding accelerators for the menu"),
545 GTK_TYPE_ACCEL_GROUP,
546 GTK_PARAM_READWRITE));
549 * GtkMenu:accel-path:
551 * An accel path used to conveniently construct accel paths of child items.
555 g_object_class_install_property (gobject_class,
557 g_param_spec_string ("accel-path",
559 P_("An accel path used to conveniently construct accel paths of child items"),
561 GTK_PARAM_READWRITE));
564 * GtkMenu:attach-widget:
566 * The widget the menu is attached to. Setting this property attaches
567 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
568 * use gtk_menu_attach_to_widget() directly.
572 g_object_class_install_property (gobject_class,
574 g_param_spec_object ("attach-widget",
576 P_("The widget the menu is attached to"),
578 GTK_PARAM_READWRITE));
580 g_object_class_install_property (gobject_class,
582 g_param_spec_string ("tearoff-title",
584 P_("A title that may be displayed by the window manager when this menu is torn-off"),
586 GTK_PARAM_READWRITE));
589 * GtkMenu:tearoff-state:
591 * A boolean that indicates whether the menu is torn-off.
595 g_object_class_install_property (gobject_class,
597 g_param_spec_boolean ("tearoff-state",
599 P_("A boolean that indicates whether the menu is torn-off"),
601 GTK_PARAM_READWRITE));
606 * The monitor the menu will be popped up on.
610 g_object_class_install_property (gobject_class,
612 g_param_spec_int ("monitor",
614 P_("The monitor the menu will be popped up on"),
616 GTK_PARAM_READWRITE));
618 gtk_widget_class_install_style_property (widget_class,
619 g_param_spec_int ("vertical-padding",
620 P_("Vertical Padding"),
621 P_("Extra space at the top and bottom of the menu"),
625 GTK_PARAM_READABLE));
628 * GtkMenu:reserve-toggle-size:
630 * A boolean that indicates whether the menu reserves space for
631 * toggles and icons, regardless of their actual presence.
633 * This property should only be changed from its default value
634 * for special-purposes such as tabular menus. Regular menus that
635 * are connected to a menu bar or context menus should reserve
636 * toggle space for consistency.
640 g_object_class_install_property (gobject_class,
641 PROP_RESERVE_TOGGLE_SIZE,
642 g_param_spec_boolean ("reserve-toggle-size",
643 P_("Reserve Toggle Size"),
644 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
646 GTK_PARAM_READWRITE));
648 gtk_widget_class_install_style_property (widget_class,
649 g_param_spec_int ("horizontal-padding",
650 P_("Horizontal Padding"),
651 P_("Extra space at the left and right edges of the menu"),
655 GTK_PARAM_READABLE));
657 gtk_widget_class_install_style_property (widget_class,
658 g_param_spec_int ("vertical-offset",
659 P_("Vertical Offset"),
660 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
664 GTK_PARAM_READABLE));
666 gtk_widget_class_install_style_property (widget_class,
667 g_param_spec_int ("horizontal-offset",
668 P_("Horizontal Offset"),
669 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
673 GTK_PARAM_READABLE));
675 gtk_widget_class_install_style_property (widget_class,
676 g_param_spec_boolean ("double-arrows",
678 P_("When scrolling, always show both arrows."),
680 GTK_PARAM_READABLE));
683 * GtkMenu:arrow-placement:
685 * Indicates where scroll arrows should be placed.
689 gtk_widget_class_install_style_property (widget_class,
690 g_param_spec_enum ("arrow-placement",
691 P_("Arrow Placement"),
692 P_("Indicates where scroll arrows should be placed"),
693 GTK_TYPE_ARROW_PLACEMENT,
695 GTK_PARAM_READABLE));
697 gtk_container_class_install_child_property (container_class,
698 CHILD_PROP_LEFT_ATTACH,
699 g_param_spec_int ("left-attach",
701 P_("The column number to attach the left side of the child to"),
703 GTK_PARAM_READWRITE));
705 gtk_container_class_install_child_property (container_class,
706 CHILD_PROP_RIGHT_ATTACH,
707 g_param_spec_int ("right-attach",
709 P_("The column number to attach the right side of the child to"),
711 GTK_PARAM_READWRITE));
713 gtk_container_class_install_child_property (container_class,
714 CHILD_PROP_TOP_ATTACH,
715 g_param_spec_int ("top-attach",
717 P_("The row number to attach the top of the child to"),
719 GTK_PARAM_READWRITE));
721 gtk_container_class_install_child_property (container_class,
722 CHILD_PROP_BOTTOM_ATTACH,
723 g_param_spec_int ("bottom-attach",
725 P_("The row number to attach the bottom of the child to"),
727 GTK_PARAM_READWRITE));
730 * GtkMenu::arrow-scaling
732 * Arbitrary constant to scale down the size of the scroll arrow.
736 gtk_widget_class_install_style_property (widget_class,
737 g_param_spec_float ("arrow-scaling",
739 P_("Arbitrary constant to scale down the size of the scroll arrow"),
741 GTK_PARAM_READABLE));
743 binding_set = gtk_binding_set_by_class (class);
744 gtk_binding_entry_add_signal (binding_set,
746 I_("move-current"), 1,
747 GTK_TYPE_MENU_DIRECTION_TYPE,
749 gtk_binding_entry_add_signal (binding_set,
752 GTK_TYPE_MENU_DIRECTION_TYPE,
754 gtk_binding_entry_add_signal (binding_set,
757 GTK_TYPE_MENU_DIRECTION_TYPE,
759 gtk_binding_entry_add_signal (binding_set,
762 GTK_TYPE_MENU_DIRECTION_TYPE,
764 gtk_binding_entry_add_signal (binding_set,
767 GTK_TYPE_MENU_DIRECTION_TYPE,
768 GTK_MENU_DIR_PARENT);
769 gtk_binding_entry_add_signal (binding_set,
772 GTK_TYPE_MENU_DIRECTION_TYPE,
773 GTK_MENU_DIR_PARENT);
774 gtk_binding_entry_add_signal (binding_set,
777 GTK_TYPE_MENU_DIRECTION_TYPE,
779 gtk_binding_entry_add_signal (binding_set,
782 GTK_TYPE_MENU_DIRECTION_TYPE,
784 gtk_binding_entry_add_signal (binding_set,
787 GTK_TYPE_SCROLL_TYPE,
789 gtk_binding_entry_add_signal (binding_set,
792 GTK_TYPE_SCROLL_TYPE,
794 gtk_binding_entry_add_signal (binding_set,
797 GTK_TYPE_SCROLL_TYPE,
799 gtk_binding_entry_add_signal (binding_set,
802 GTK_TYPE_SCROLL_TYPE,
804 gtk_binding_entry_add_signal (binding_set,
807 GTK_TYPE_SCROLL_TYPE,
809 gtk_binding_entry_add_signal (binding_set,
810 GDK_KEY_KP_Page_Up, 0,
812 GTK_TYPE_SCROLL_TYPE,
814 gtk_binding_entry_add_signal (binding_set,
815 GDK_KEY_Page_Down, 0,
817 GTK_TYPE_SCROLL_TYPE,
818 GTK_SCROLL_PAGE_DOWN);
819 gtk_binding_entry_add_signal (binding_set,
820 GDK_KEY_KP_Page_Down, 0,
822 GTK_TYPE_SCROLL_TYPE,
823 GTK_SCROLL_PAGE_DOWN);
825 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
826 P_("Can change accelerators"),
827 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
829 GTK_PARAM_READWRITE));
831 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
832 P_("Delay before submenus appear"),
833 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
837 GTK_PARAM_READWRITE));
839 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
840 P_("Delay before hiding a submenu"),
841 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
844 DEFAULT_POPDOWN_DELAY,
845 GTK_PARAM_READWRITE));
847 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
852 gtk_menu_set_property (GObject *object,
857 GtkMenu *menu = GTK_MENU (object);
862 gtk_menu_set_active (menu, g_value_get_int (value));
864 case PROP_ACCEL_GROUP:
865 gtk_menu_set_accel_group (menu, g_value_get_object (value));
867 case PROP_ACCEL_PATH:
868 gtk_menu_set_accel_path (menu, g_value_get_string (value));
870 case PROP_ATTACH_WIDGET:
874 widget = gtk_menu_get_attach_widget (menu);
876 gtk_menu_detach (menu);
878 widget = (GtkWidget*) g_value_get_object (value);
880 gtk_menu_attach_to_widget (menu, widget, NULL);
883 case PROP_TEAROFF_STATE:
884 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
886 case PROP_TEAROFF_TITLE:
887 gtk_menu_set_title (menu, g_value_get_string (value));
890 gtk_menu_set_monitor (menu, g_value_get_int (value));
892 case PROP_RESERVE_TOGGLE_SIZE:
893 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
896 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
902 gtk_menu_get_property (GObject *object,
907 GtkMenu *menu = GTK_MENU (object);
912 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
914 case PROP_ACCEL_GROUP:
915 g_value_set_object (value, gtk_menu_get_accel_group (menu));
917 case PROP_ACCEL_PATH:
918 g_value_set_string (value, gtk_menu_get_accel_path (menu));
920 case PROP_ATTACH_WIDGET:
921 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
923 case PROP_TEAROFF_STATE:
924 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
926 case PROP_TEAROFF_TITLE:
927 g_value_set_string (value, gtk_menu_get_title (menu));
930 g_value_set_int (value, gtk_menu_get_monitor (menu));
932 case PROP_RESERVE_TOGGLE_SIZE:
933 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
936 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
942 gtk_menu_set_child_property (GtkContainer *container,
948 GtkMenu *menu = GTK_MENU (container);
949 AttachInfo *ai = get_attach_info (child);
953 case CHILD_PROP_LEFT_ATTACH:
954 ai->left_attach = g_value_get_int (value);
956 case CHILD_PROP_RIGHT_ATTACH:
957 ai->right_attach = g_value_get_int (value);
959 case CHILD_PROP_TOP_ATTACH:
960 ai->top_attach = g_value_get_int (value);
962 case CHILD_PROP_BOTTOM_ATTACH:
963 ai->bottom_attach = g_value_get_int (value);
967 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
971 menu_queue_resize (menu);
975 gtk_menu_get_child_property (GtkContainer *container,
981 AttachInfo *ai = get_attach_info (child);
985 case CHILD_PROP_LEFT_ATTACH:
986 g_value_set_int (value, ai->left_attach);
988 case CHILD_PROP_RIGHT_ATTACH:
989 g_value_set_int (value, ai->right_attach);
991 case CHILD_PROP_TOP_ATTACH:
992 g_value_set_int (value, ai->top_attach);
994 case CHILD_PROP_BOTTOM_ATTACH:
995 g_value_set_int (value, ai->bottom_attach);
999 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1005 gtk_menu_window_event (GtkWidget *window,
1009 gboolean handled = FALSE;
1011 g_object_ref (window);
1012 g_object_ref (menu);
1014 switch (event->type)
1017 case GDK_KEY_RELEASE:
1018 handled = gtk_widget_event (menu, event);
1024 g_object_unref (window);
1025 g_object_unref (menu);
1031 gtk_menu_init (GtkMenu *menu)
1033 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1035 menu->parent_menu_item = NULL;
1036 menu->old_active_menu_item = NULL;
1037 menu->accel_group = NULL;
1038 menu->position_func = NULL;
1039 menu->position_func_data = NULL;
1040 menu->toggle_size = 0;
1042 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1043 "type", GTK_WINDOW_POPUP,
1046 "signal::event", gtk_menu_window_event, menu,
1047 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1049 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1050 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1052 /* Refloat the menu, so that reference counting for the menu isn't
1053 * affected by it being a child of the toplevel
1055 g_object_force_floating (G_OBJECT (menu));
1056 menu->needs_destruction_ref_count = TRUE;
1058 menu->view_window = NULL;
1059 menu->bin_window = NULL;
1061 menu->scroll_offset = 0;
1062 menu->scroll_step = 0;
1063 menu->timeout_id = 0;
1064 menu->scroll_fast = FALSE;
1066 menu->tearoff_window = NULL;
1067 menu->tearoff_hbox = NULL;
1068 menu->torn_off = FALSE;
1069 menu->tearoff_active = FALSE;
1070 menu->tearoff_adjustment = NULL;
1071 menu->tearoff_scrollbar = NULL;
1073 menu->upper_arrow_visible = FALSE;
1074 menu->lower_arrow_visible = FALSE;
1075 menu->upper_arrow_prelight = FALSE;
1076 menu->lower_arrow_prelight = FALSE;
1078 priv->upper_arrow_state = GTK_STATE_NORMAL;
1079 priv->lower_arrow_state = GTK_STATE_NORMAL;
1081 priv->have_layout = FALSE;
1082 priv->monitor_num = -1;
1086 gtk_menu_destroy (GtkObject *object)
1088 GtkMenu *menu = GTK_MENU (object);
1089 GtkMenuAttachData *data;
1090 GtkMenuPrivate *priv;
1092 gtk_menu_remove_scroll_timeout (menu);
1094 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1096 gtk_menu_detach (menu);
1098 gtk_menu_stop_navigating_submenu (menu);
1100 if (menu->old_active_menu_item)
1102 g_object_unref (menu->old_active_menu_item);
1103 menu->old_active_menu_item = NULL;
1106 /* Add back the reference count for being a child */
1107 if (menu->needs_destruction_ref_count)
1109 menu->needs_destruction_ref_count = FALSE;
1110 g_object_ref (object);
1113 if (menu->accel_group)
1115 g_object_unref (menu->accel_group);
1116 menu->accel_group = NULL;
1120 gtk_widget_destroy (menu->toplevel);
1122 if (menu->tearoff_window)
1123 gtk_widget_destroy (menu->tearoff_window);
1125 priv = gtk_menu_get_private (menu);
1129 g_free (priv->heights);
1130 priv->heights = NULL;
1135 g_free (priv->title);
1139 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1143 menu_change_screen (GtkMenu *menu,
1144 GdkScreen *new_screen)
1146 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1148 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1150 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1156 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1157 gtk_menu_position (menu);
1160 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1161 private->monitor_num = -1;
1165 attach_widget_screen_changed (GtkWidget *attach_widget,
1166 GdkScreen *previous_screen,
1169 if (gtk_widget_has_screen (attach_widget) &&
1170 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1172 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1177 gtk_menu_attach_to_widget (GtkMenu *menu,
1178 GtkWidget *attach_widget,
1179 GtkMenuDetachFunc detacher)
1181 GtkMenuAttachData *data;
1184 g_return_if_fail (GTK_IS_MENU (menu));
1185 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1187 /* keep this function in sync with gtk_widget_set_parent()
1190 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1193 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1194 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1198 g_object_ref_sink (menu);
1200 data = g_slice_new (GtkMenuAttachData);
1201 data->attach_widget = attach_widget;
1203 g_signal_connect (attach_widget, "screen-changed",
1204 G_CALLBACK (attach_widget_screen_changed), menu);
1205 attach_widget_screen_changed (attach_widget, NULL, menu);
1207 data->detacher = detacher;
1208 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1209 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1210 if (!g_list_find (list, menu))
1212 list = g_list_prepend (list, menu);
1214 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1215 (GDestroyNotify) g_list_free);
1217 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1218 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1220 /* we don't need to set the style here, since
1221 * we are a toplevel widget.
1224 /* Fallback title for menu comes from attach widget */
1225 gtk_menu_update_title (menu);
1227 g_object_notify (G_OBJECT (menu), "attach-widget");
1231 gtk_menu_get_attach_widget (GtkMenu *menu)
1233 GtkMenuAttachData *data;
1235 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1237 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1239 return data->attach_widget;
1244 gtk_menu_detach (GtkMenu *menu)
1246 GtkMenuAttachData *data;
1249 g_return_if_fail (GTK_IS_MENU (menu));
1251 /* keep this function in sync with gtk_widget_unparent()
1253 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1256 g_warning ("gtk_menu_detach(): menu is not attached");
1259 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1261 g_signal_handlers_disconnect_by_func (data->attach_widget,
1262 (gpointer) attach_widget_screen_changed,
1266 data->detacher (data->attach_widget, menu);
1267 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1268 list = g_list_remove (list, menu);
1270 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1271 (GDestroyNotify) g_list_free);
1273 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1275 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1276 gtk_widget_unrealize (GTK_WIDGET (menu));
1278 g_slice_free (GtkMenuAttachData, data);
1280 /* Fallback title for menu comes from attach widget */
1281 gtk_menu_update_title (menu);
1283 g_object_unref (menu);
1287 gtk_menu_remove (GtkContainer *container,
1290 GtkMenu *menu = GTK_MENU (container);
1292 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1294 /* Clear out old_active_menu_item if it matches the item we are removing
1296 if (menu->old_active_menu_item == widget)
1298 g_object_unref (menu->old_active_menu_item);
1299 menu->old_active_menu_item = NULL;
1302 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1303 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1305 menu_queue_resize (menu);
1311 return g_object_new (GTK_TYPE_MENU, NULL);
1315 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1319 GtkMenu *menu = GTK_MENU (menu_shell);
1320 AttachInfo *ai = get_attach_info (child);
1322 ai->left_attach = -1;
1323 ai->right_attach = -1;
1324 ai->top_attach = -1;
1325 ai->bottom_attach = -1;
1327 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1328 gtk_widget_set_parent_window (child, menu->bin_window);
1330 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1332 menu_queue_resize (menu);
1336 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1341 widget = GTK_WIDGET (menu);
1349 menu->tearoff_active = FALSE;
1350 menu->saved_scroll_offset = menu->scroll_offset;
1352 window = gtk_widget_get_window (menu->tearoff_window);
1354 gdk_drawable_get_size (window, &width, &height);
1356 pixmap = gdk_pixmap_new (window,
1361 cr = gdk_cairo_create (pixmap);
1362 /* Let's hope that function never notices we're not passing it a pixmap */
1363 gdk_cairo_set_source_pixmap (cr,
1369 gtk_widget_set_size_request (menu->tearoff_window,
1373 gdk_window_set_back_pixmap (window, pixmap, FALSE);
1374 g_object_unref (pixmap);
1379 popup_grab_on_window (GdkWindow *window,
1380 GdkDevice *keyboard,
1382 guint32 activate_time)
1385 gdk_device_grab (keyboard, window,
1386 GDK_OWNERSHIP_WINDOW, TRUE,
1387 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1388 NULL, activate_time) != GDK_GRAB_SUCCESS)
1392 gdk_device_grab (pointer, window,
1393 GDK_OWNERSHIP_WINDOW, TRUE,
1394 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1395 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1396 GDK_POINTER_MOTION_MASK,
1397 NULL, activate_time) != GDK_GRAB_SUCCESS)
1400 gdk_device_ungrab (keyboard, activate_time);
1409 * gtk_menu_popup_for_device:
1410 * @menu: a #GtkMenu.
1411 * @device: (allow-none): a #GdkDevice
1412 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1413 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1414 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1415 * @data: (allow-none): user supplied data to be passed to @func.
1416 * @button: the mouse button which was pressed to initiate the event.
1417 * @activate_time: the time at which the activation event occurred.
1419 * Displays a menu and makes it available for selection. Applications can use
1420 * this function to display context-sensitive menus, and will typically supply
1421 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1422 * parameters. The default menu positioning function will position the menu
1423 * at the current position of @device (or its corresponding pointer).
1425 * The @button parameter should be the mouse button pressed to initiate
1426 * the menu popup. If the menu popup was initiated by something other than
1427 * a mouse button press, such as a mouse button release or a keypress,
1428 * @button should be 0.
1430 * The @activate_time parameter is used to conflict-resolve initiation of
1431 * concurrent requests for mouse/keyboard grab requests. To function
1432 * properly, this needs to be the time stamp of the user event (such as
1433 * a mouse click or key press) that caused the initiation of the popup.
1434 * Only if no such event is available, gtk_get_current_event_time() can
1440 gtk_menu_popup_for_device (GtkMenu *menu,
1442 GtkWidget *parent_menu_shell,
1443 GtkWidget *parent_menu_item,
1444 GtkMenuPositionFunc func,
1447 guint32 activate_time)
1450 GtkWidget *xgrab_shell;
1452 GdkEvent *current_event;
1453 GtkMenuShell *menu_shell;
1454 gboolean grab_keyboard;
1455 GtkMenuPrivate *priv;
1456 GtkWidget *parent_toplevel;
1457 GdkDevice *keyboard, *pointer;
1459 g_return_if_fail (GTK_IS_MENU (menu));
1460 g_return_if_fail (GDK_IS_DEVICE (device));
1462 widget = GTK_WIDGET (menu);
1463 menu_shell = GTK_MENU_SHELL (menu);
1464 priv = gtk_menu_get_private (menu);
1466 if (device->source == GDK_SOURCE_KEYBOARD)
1469 pointer = gdk_device_get_associated_device (device);
1474 keyboard = gdk_device_get_associated_device (device);
1477 menu_shell->parent_menu_shell = parent_menu_shell;
1479 priv->seen_item_enter = FALSE;
1481 /* Find the last viewable ancestor, and make an X grab on it
1483 parent = GTK_WIDGET (menu);
1487 gboolean viewable = TRUE;
1488 GtkWidget *tmp = parent;
1492 if (!gtk_widget_get_mapped (tmp))
1497 tmp = gtk_widget_get_parent (tmp);
1501 xgrab_shell = parent;
1503 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1506 /* We want to receive events generated when we map the menu; unfortunately,
1507 * since there is probably already an implicit grab in place from the
1508 * button that the user used to pop up the menu, we won't receive then --
1509 * in particular, the EnterNotify when the menu pops up under the pointer.
1511 * If we are grabbing on a parent menu shell, no problem; just grab on
1512 * that menu shell first before popping up the window with owner_events = TRUE.
1514 * When grabbing on the menu itself, things get more convuluted - we
1515 * we do an explicit grab on a specially created window with
1516 * owner_events = TRUE, which we override further down with a grab
1517 * on the menu. (We can't grab on the menu until it is mapped; we
1518 * probably could just leave the grab on the other window, with a
1519 * little reorganization of the code in gtkmenu*).
1521 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1522 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1527 if (xgrab_shell && xgrab_shell != widget)
1529 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1531 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1532 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1537 GdkWindow *transfer_window;
1539 xgrab_shell = widget;
1540 transfer_window = menu_grab_transfer_window_get (menu);
1541 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1543 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1544 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1548 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1550 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1551 * with a stuck up window, we just abort here. Presumably the user will
1554 menu_shell->parent_menu_shell = NULL;
1555 menu_grab_transfer_window_destroy (menu);
1559 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1560 menu_shell->active = TRUE;
1561 menu_shell->button = button;
1563 /* If we are popping up the menu from something other than, a button
1564 * press then, as a heuristic, we ignore enter events for the menu
1565 * until we get a MOTION_NOTIFY.
1568 current_event = gtk_get_current_event ();
1571 if ((current_event->type != GDK_BUTTON_PRESS) &&
1572 (current_event->type != GDK_ENTER_NOTIFY))
1573 menu_shell->ignore_enter = TRUE;
1575 gdk_event_free (current_event);
1578 menu_shell->ignore_enter = TRUE;
1582 gtk_menu_tearoff_bg_copy (menu);
1584 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1587 parent_toplevel = NULL;
1588 if (parent_menu_shell)
1589 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1590 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1592 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1594 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1597 /* Set transient for to get the right window group and parent relationship */
1598 if (GTK_IS_WINDOW (parent_toplevel))
1599 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1600 GTK_WINDOW (parent_toplevel));
1602 menu->parent_menu_item = parent_menu_item;
1603 menu->position_func = func;
1604 menu->position_func_data = data;
1605 menu_shell->activate_time = activate_time;
1607 /* We need to show the menu here rather in the init function because
1608 * code expects to be able to tell if the menu is onscreen by
1609 * looking at the gtk_widget_get_visible (menu)
1611 gtk_widget_show (GTK_WIDGET (menu));
1613 /* Position the menu, possibly changing the size request
1615 gtk_menu_position (menu);
1617 /* Compute the size of the toplevel and realize it so we
1618 * can scroll correctly.
1621 GtkRequisition tmp_request;
1622 GtkAllocation tmp_allocation = { 0, };
1624 /* Instead of trusting the menu position function to queue a resize when the
1625 * menu goes out of bounds, invalidate the cached size here. */
1626 gtk_widget_queue_resize (GTK_WIDGET (menu));
1627 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->toplevel), &tmp_request, NULL);
1629 tmp_allocation.width = tmp_request.width;
1630 tmp_allocation.height = tmp_request.height;
1632 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1634 gtk_widget_realize (GTK_WIDGET (menu));
1637 gtk_menu_scroll_to (menu, menu->scroll_offset);
1639 /* if no item is selected, select the first one */
1640 if (!menu_shell->active_menu_item)
1642 gboolean touchscreen_mode;
1644 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1645 "gtk-touchscreen-mode", &touchscreen_mode,
1648 if (touchscreen_mode)
1649 gtk_menu_shell_select_first (menu_shell, TRUE);
1652 /* Once everything is set up correctly, map the toplevel window on
1655 gtk_widget_show (menu->toplevel);
1657 if (xgrab_shell == widget)
1658 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1660 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1662 if (parent_menu_shell)
1664 gboolean keyboard_mode;
1666 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1667 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1669 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1670 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1672 _gtk_menu_shell_update_mnemonics (menu_shell);
1677 * @menu: a #GtkMenu.
1678 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1679 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1680 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1681 * @data: (allow-none): user supplied data to be passed to @func.
1682 * @button: the mouse button which was pressed to initiate the event.
1683 * @activate_time: the time at which the activation event occurred.
1685 * Displays a menu and makes it available for selection. Applications can use
1686 * this function to display context-sensitive menus, and will typically supply
1687 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1688 * parameters. The default menu positioning function will position the menu
1689 * at the current mouse cursor position.
1691 * The @button parameter should be the mouse button pressed to initiate
1692 * the menu popup. If the menu popup was initiated by something other than
1693 * a mouse button press, such as a mouse button release or a keypress,
1694 * @button should be 0.
1696 * The @activate_time parameter is used to conflict-resolve initiation of
1697 * concurrent requests for mouse/keyboard grab requests. To function
1698 * properly, this needs to be the time stamp of the user event (such as
1699 * a mouse click or key press) that caused the initiation of the popup.
1700 * Only if no such event is available, gtk_get_current_event_time() can
1704 gtk_menu_popup (GtkMenu *menu,
1705 GtkWidget *parent_menu_shell,
1706 GtkWidget *parent_menu_item,
1707 GtkMenuPositionFunc func,
1710 guint32 activate_time)
1714 g_return_if_fail (GTK_IS_MENU (menu));
1716 device = gtk_get_current_event_device ();
1720 GdkDisplay *display;
1721 GdkDeviceManager *device_manager;
1724 display = gtk_widget_get_display (GTK_WIDGET (menu));
1725 device_manager = gdk_display_get_device_manager (display);
1726 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1728 device = devices->data;
1730 g_list_free (devices);
1733 gtk_menu_popup_for_device (menu, device,
1737 button, activate_time);
1741 gtk_menu_popdown (GtkMenu *menu)
1743 GtkMenuPrivate *private;
1744 GtkMenuShell *menu_shell;
1747 g_return_if_fail (GTK_IS_MENU (menu));
1749 menu_shell = GTK_MENU_SHELL (menu);
1750 private = gtk_menu_get_private (menu);
1752 menu_shell->parent_menu_shell = NULL;
1753 menu_shell->active = FALSE;
1754 menu_shell->ignore_enter = FALSE;
1756 private->have_position = FALSE;
1758 gtk_menu_stop_scrolling (menu);
1760 gtk_menu_stop_navigating_submenu (menu);
1762 if (menu_shell->active_menu_item)
1764 if (menu->old_active_menu_item)
1765 g_object_unref (menu->old_active_menu_item);
1766 menu->old_active_menu_item = menu_shell->active_menu_item;
1767 g_object_ref (menu->old_active_menu_item);
1770 gtk_menu_shell_deselect (menu_shell);
1772 /* The X Grab, if present, will automatically be removed when we hide
1774 gtk_widget_hide (menu->toplevel);
1775 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1777 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1781 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1783 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1785 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1789 /* We popped up the menu from the tearoff, so we need to
1790 * release the grab - we aren't actually hiding the menu.
1792 if (menu_shell->have_xgrab && pointer)
1794 GdkDevice *keyboard;
1796 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1797 keyboard = gdk_device_get_associated_device (pointer);
1800 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1804 /* gtk_menu_popdown is called each time a menu item is selected from
1805 * a torn off menu. Only scroll back to the saved position if the
1806 * non-tearoff menu was popped down.
1808 if (!menu->tearoff_active)
1809 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1810 menu->tearoff_active = TRUE;
1813 gtk_widget_hide (GTK_WIDGET (menu));
1815 menu_shell->have_xgrab = FALSE;
1818 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1820 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1822 menu_grab_transfer_window_destroy (menu);
1826 gtk_menu_get_active (GtkMenu *menu)
1831 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1833 if (!menu->old_active_menu_item)
1836 children = GTK_MENU_SHELL (menu)->children;
1840 child = children->data;
1841 children = children->next;
1843 if (gtk_bin_get_child (GTK_BIN (child)))
1848 menu->old_active_menu_item = child;
1849 if (menu->old_active_menu_item)
1850 g_object_ref (menu->old_active_menu_item);
1853 return menu->old_active_menu_item;
1857 gtk_menu_set_active (GtkMenu *menu,
1863 g_return_if_fail (GTK_IS_MENU (menu));
1865 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1868 child = tmp_list->data;
1869 if (gtk_bin_get_child (GTK_BIN (child)))
1871 if (menu->old_active_menu_item)
1872 g_object_unref (menu->old_active_menu_item);
1873 menu->old_active_menu_item = child;
1874 g_object_ref (menu->old_active_menu_item);
1881 * gtk_menu_set_accel_group:
1882 * @accel_group: (allow-none):
1885 gtk_menu_set_accel_group (GtkMenu *menu,
1886 GtkAccelGroup *accel_group)
1888 g_return_if_fail (GTK_IS_MENU (menu));
1890 if (menu->accel_group != accel_group)
1892 if (menu->accel_group)
1893 g_object_unref (menu->accel_group);
1894 menu->accel_group = accel_group;
1895 if (menu->accel_group)
1896 g_object_ref (menu->accel_group);
1897 _gtk_menu_refresh_accel_paths (menu, TRUE);
1902 gtk_menu_get_accel_group (GtkMenu *menu)
1904 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1906 return menu->accel_group;
1910 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1913 /* Menu items chain here to figure whether they can activate their
1914 * accelerators. Unlike ordinary widgets, menus allow accel
1915 * activation even if invisible since that's the usual case for
1916 * submenus/popup-menus. however, the state of the attach widget
1917 * affects the "activeness" of the menu.
1919 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1922 return gtk_widget_can_activate_accel (awidget, signal_id);
1924 return gtk_widget_is_sensitive (widget);
1928 * gtk_menu_set_accel_path
1929 * @menu: a valid #GtkMenu
1930 * @accel_path: (allow-none): a valid accelerator path
1932 * Sets an accelerator path for this menu from which accelerator paths
1933 * for its immediate children, its menu items, can be constructed.
1934 * The main purpose of this function is to spare the programmer the
1935 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1936 * each menu item that should support runtime user changable accelerators.
1937 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1938 * each menu item of this menu, that contains a label describing its purpose,
1939 * automatically gets an accel path assigned. For example, a menu containing
1940 * menu items "New" and "Exit", will, after
1941 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1942 * has been called, assign its items the accel paths:
1943 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1944 * Assigning accel paths to menu items then enables the user to change
1945 * their accelerators at runtime. More details about accelerator paths
1946 * and their default setups can be found at gtk_accel_map_add_entry().
1948 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1949 * pass a static string, you can save some memory by interning it first with
1950 * g_intern_static_string().
1953 gtk_menu_set_accel_path (GtkMenu *menu,
1954 const gchar *accel_path)
1956 g_return_if_fail (GTK_IS_MENU (menu));
1958 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1960 /* FIXME: accel_path should be defined as const gchar* */
1961 menu->accel_path = (gchar*)g_intern_string (accel_path);
1962 if (menu->accel_path)
1963 _gtk_menu_refresh_accel_paths (menu, FALSE);
1967 * gtk_menu_get_accel_path
1968 * @menu: a valid #GtkMenu
1970 * Retrieves the accelerator path set on the menu.
1972 * Returns: the accelerator path set on the menu.
1977 gtk_menu_get_accel_path (GtkMenu *menu)
1979 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1981 return menu->accel_path;
1986 gboolean group_changed;
1990 refresh_accel_paths_foreach (GtkWidget *widget,
1993 AccelPropagation *prop = data;
1995 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1996 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1997 prop->menu->accel_path,
1998 prop->menu->accel_group,
1999 prop->group_changed);
2003 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2004 gboolean group_changed)
2006 g_return_if_fail (GTK_IS_MENU (menu));
2008 if (menu->accel_path && menu->accel_group)
2010 AccelPropagation prop;
2013 prop.group_changed = group_changed;
2014 gtk_container_foreach (GTK_CONTAINER (menu),
2015 refresh_accel_paths_foreach,
2021 gtk_menu_reposition (GtkMenu *menu)
2023 g_return_if_fail (GTK_IS_MENU (menu));
2025 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2026 gtk_menu_position (menu);
2030 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2033 g_return_if_fail (GTK_IS_MENU (menu));
2035 if (adjustment->value != menu->scroll_offset)
2036 gtk_menu_scroll_to (menu, adjustment->value);
2040 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2043 GdkGeometry geometry_hints;
2044 GtkMenuPrivate *priv;
2046 if (!menu->tearoff_window)
2049 priv = gtk_menu_get_private (menu);
2051 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2053 GtkRequisition requisition;
2054 gtk_widget_size_request (menu->tearoff_scrollbar, &requisition);
2055 width += requisition.width;
2058 geometry_hints.min_width = width;
2059 geometry_hints.max_width = width;
2061 geometry_hints.min_height = 0;
2062 geometry_hints.max_height = priv->requested_height;
2064 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2067 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2071 gtk_menu_update_title (GtkMenu *menu)
2073 if (menu->tearoff_window)
2076 GtkWidget *attach_widget;
2078 title = gtk_menu_get_title (menu);
2081 attach_widget = gtk_menu_get_attach_widget (menu);
2082 if (GTK_IS_MENU_ITEM (attach_widget))
2084 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2085 if (GTK_IS_LABEL (child))
2086 title = gtk_label_get_text (GTK_LABEL (child));
2091 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2096 gtk_menu_get_toplevel (GtkWidget *menu)
2098 GtkWidget *attach, *toplevel;
2100 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2102 if (GTK_IS_MENU_ITEM (attach))
2103 attach = gtk_widget_get_parent (attach);
2105 if (GTK_IS_MENU (attach))
2106 return gtk_menu_get_toplevel (attach);
2107 else if (GTK_IS_WIDGET (attach))
2109 toplevel = gtk_widget_get_toplevel (attach);
2110 if (gtk_widget_is_toplevel (toplevel))
2118 tearoff_window_destroyed (GtkWidget *widget,
2121 gtk_menu_set_tearoff_state (menu, FALSE);
2125 gtk_menu_set_tearoff_state (GtkMenu *menu,
2129 GtkMenuPrivate *priv;
2131 g_return_if_fail (GTK_IS_MENU (menu));
2133 priv = gtk_menu_get_private (menu);
2135 if (menu->torn_off != torn_off)
2137 menu->torn_off = torn_off;
2138 menu->tearoff_active = torn_off;
2142 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2143 gtk_menu_popdown (menu);
2145 if (!menu->tearoff_window)
2147 GtkWidget *toplevel;
2149 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2150 "type", GTK_WINDOW_TOPLEVEL,
2151 "screen", gtk_widget_get_screen (menu->toplevel),
2152 "app-paintable", TRUE,
2155 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2156 GDK_WINDOW_TYPE_HINT_MENU);
2157 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2158 g_signal_connect (menu->tearoff_window, "destroy",
2159 G_CALLBACK (tearoff_window_destroyed), menu);
2160 g_signal_connect (menu->tearoff_window, "event",
2161 G_CALLBACK (gtk_menu_window_event), menu);
2163 gtk_menu_update_title (menu);
2165 gtk_widget_realize (menu->tearoff_window);
2167 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2168 if (toplevel != NULL)
2169 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2170 GTK_WINDOW (toplevel));
2172 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2173 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2175 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2177 menu->tearoff_adjustment =
2178 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2180 priv->requested_height,
2184 g_object_connect (menu->tearoff_adjustment,
2185 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2187 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2189 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2190 menu->tearoff_scrollbar,
2193 if (menu->tearoff_adjustment->upper > height)
2194 gtk_widget_show (menu->tearoff_scrollbar);
2196 gtk_widget_show (menu->tearoff_hbox);
2199 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2201 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2204 /* Update menu->requisition
2206 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2208 gtk_menu_set_tearoff_hints (menu, width);
2210 gtk_widget_realize (menu->tearoff_window);
2211 gtk_menu_position (menu);
2213 gtk_widget_show (GTK_WIDGET (menu));
2214 gtk_widget_show (menu->tearoff_window);
2216 gtk_menu_scroll_to (menu, 0);
2221 gtk_widget_hide (GTK_WIDGET (menu));
2222 gtk_widget_hide (menu->tearoff_window);
2223 if (GTK_IS_CONTAINER (menu->toplevel))
2224 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2225 gtk_widget_destroy (menu->tearoff_window);
2227 menu->tearoff_window = NULL;
2228 menu->tearoff_hbox = NULL;
2229 menu->tearoff_scrollbar = NULL;
2230 menu->tearoff_adjustment = NULL;
2233 g_object_notify (G_OBJECT (menu), "tearoff-state");
2238 * gtk_menu_get_tearoff_state:
2241 * Returns whether the menu is torn off. See
2242 * gtk_menu_set_tearoff_state ().
2244 * Return value: %TRUE if the menu is currently torn off.
2247 gtk_menu_get_tearoff_state (GtkMenu *menu)
2249 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2251 return menu->torn_off;
2255 * gtk_menu_set_title:
2257 * @title: a string containing the title for the menu.
2259 * Sets the title string for the menu. The title is displayed when the menu
2260 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2261 * attached to a parent menu item, and if so it will try to use the same text as
2262 * that menu item's label.
2265 gtk_menu_set_title (GtkMenu *menu,
2268 GtkMenuPrivate *priv;
2271 g_return_if_fail (GTK_IS_MENU (menu));
2273 priv = gtk_menu_get_private (menu);
2275 old_title = priv->title;
2276 priv->title = g_strdup (title);
2279 gtk_menu_update_title (menu);
2280 g_object_notify (G_OBJECT (menu), "tearoff-title");
2284 * gtk_menu_get_title:
2287 * Returns the title of the menu. See gtk_menu_set_title().
2289 * Return value: the title of the menu, or %NULL if the menu has no
2290 * title set on it. This string is owned by the widget and should
2291 * not be modified or freed.
2293 G_CONST_RETURN gchar *
2294 gtk_menu_get_title (GtkMenu *menu)
2296 GtkMenuPrivate *priv;
2298 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2300 priv = gtk_menu_get_private (menu);
2306 gtk_menu_reorder_child (GtkMenu *menu,
2310 GtkMenuShell *menu_shell;
2312 g_return_if_fail (GTK_IS_MENU (menu));
2313 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2315 menu_shell = GTK_MENU_SHELL (menu);
2317 if (g_list_find (menu_shell->children, child))
2319 menu_shell->children = g_list_remove (menu_shell->children, child);
2320 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2322 menu_queue_resize (menu);
2327 gtk_menu_style_set (GtkWidget *widget,
2328 GtkStyle *previous_style)
2330 if (gtk_widget_get_realized (widget))
2332 GtkMenu *menu = GTK_MENU (widget);
2335 style = gtk_widget_get_style (widget);
2337 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2338 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2339 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2344 get_arrows_border (GtkMenu *menu,
2347 guint scroll_arrow_height;
2348 GtkArrowPlacement arrow_placement;
2350 gtk_widget_style_get (GTK_WIDGET (menu),
2351 "scroll-arrow-vlength", &scroll_arrow_height,
2352 "arrow_placement", &arrow_placement,
2355 switch (arrow_placement)
2357 case GTK_ARROWS_BOTH:
2358 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2359 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2362 case GTK_ARROWS_START:
2363 border->top = (menu->upper_arrow_visible ||
2364 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2368 case GTK_ARROWS_END:
2370 border->bottom = (menu->upper_arrow_visible ||
2371 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2375 border->left = border->right = 0;
2379 gtk_menu_realize (GtkWidget *widget)
2381 GtkAllocation allocation;
2384 GdkWindowAttr attributes;
2385 gint attributes_mask;
2388 GtkMenuPrivate *priv;
2391 guint vertical_padding;
2392 guint horizontal_padding;
2393 GtkBorder arrow_border;
2395 g_return_if_fail (GTK_IS_MENU (widget));
2397 menu = GTK_MENU (widget);
2398 priv = gtk_menu_get_private (menu);
2400 gtk_widget_set_realized (widget, TRUE);
2402 gtk_widget_get_allocation (widget, &allocation);
2404 attributes.window_type = GDK_WINDOW_CHILD;
2405 attributes.x = allocation.x;
2406 attributes.y = allocation.y;
2407 attributes.width = allocation.width;
2408 attributes.height = allocation.height;
2409 attributes.wclass = GDK_INPUT_OUTPUT;
2410 attributes.visual = gtk_widget_get_visual (widget);
2411 attributes.colormap = gtk_widget_get_colormap (widget);
2412 attributes.event_mask = gtk_widget_get_events (widget);
2413 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2414 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2416 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2418 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2419 &attributes, attributes_mask);
2420 gtk_widget_set_window (widget, window);
2421 gdk_window_set_user_data (window, widget);
2423 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2425 style = gtk_widget_get_style (widget);
2426 gtk_widget_style_get (GTK_WIDGET (menu),
2427 "vertical-padding", &vertical_padding,
2428 "horizontal-padding", &horizontal_padding,
2431 gtk_widget_get_allocation (widget, &allocation);
2433 attributes.x = border_width + style->xthickness + horizontal_padding;
2434 attributes.y = border_width + style->ythickness + vertical_padding;
2435 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2436 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2438 get_arrows_border (menu, &arrow_border);
2439 attributes.y += arrow_border.top;
2440 attributes.height -= arrow_border.top;
2441 attributes.height -= arrow_border.bottom;
2443 menu->view_window = gdk_window_new (window,
2444 &attributes, attributes_mask);
2445 gdk_window_set_user_data (menu->view_window, menu);
2447 gtk_widget_get_allocation (widget, &allocation);
2451 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2452 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2454 menu->bin_window = gdk_window_new (menu->view_window,
2455 &attributes, attributes_mask);
2456 gdk_window_set_user_data (menu->bin_window, menu);
2458 children = GTK_MENU_SHELL (menu)->children;
2461 child = children->data;
2462 children = children->next;
2464 gtk_widget_set_parent_window (child, menu->bin_window);
2467 gtk_widget_style_attach (widget);
2468 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2469 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2470 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2472 if (GTK_MENU_SHELL (widget)->active_menu_item)
2473 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2474 GTK_MENU_SHELL (widget)->active_menu_item);
2476 gdk_window_show (menu->bin_window);
2477 gdk_window_show (menu->view_window);
2481 gtk_menu_focus (GtkWidget *widget,
2482 GtkDirectionType direction)
2485 * A menu or its menu items cannot have focus
2490 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2493 menu_grab_transfer_window_get (GtkMenu *menu)
2495 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2498 GdkWindowAttr attributes;
2499 gint attributes_mask;
2501 attributes.x = -100;
2502 attributes.y = -100;
2503 attributes.width = 10;
2504 attributes.height = 10;
2505 attributes.window_type = GDK_WINDOW_TEMP;
2506 attributes.wclass = GDK_INPUT_ONLY;
2507 attributes.override_redirect = TRUE;
2508 attributes.event_mask = 0;
2510 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2512 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2513 &attributes, attributes_mask);
2514 gdk_window_set_user_data (window, menu);
2516 gdk_window_show (window);
2518 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2525 menu_grab_transfer_window_destroy (GtkMenu *menu)
2527 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2530 gdk_window_set_user_data (window, NULL);
2531 gdk_window_destroy (window);
2532 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2537 gtk_menu_unrealize (GtkWidget *widget)
2539 GtkMenu *menu = GTK_MENU (widget);
2541 menu_grab_transfer_window_destroy (menu);
2543 gdk_window_set_user_data (menu->view_window, NULL);
2544 gdk_window_destroy (menu->view_window);
2545 menu->view_window = NULL;
2547 gdk_window_set_user_data (menu->bin_window, NULL);
2548 gdk_window_destroy (menu->bin_window);
2549 menu->bin_window = NULL;
2551 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2556 calculate_line_heights (GtkMenu *menu,
2558 guint **ret_min_heights,
2559 guint **ret_nat_heights)
2561 GtkMenuShell *menu_shell;
2562 GtkMenuPrivate *priv;
2563 GtkWidget *child, *widget;
2565 guint horizontal_padding;
2573 widget = GTK_WIDGET (menu);
2574 menu_shell = GTK_MENU_SHELL (widget);
2575 priv = gtk_menu_get_private (menu);
2577 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2578 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2579 n_heights = gtk_menu_get_n_rows (menu);
2580 n_columns = gtk_menu_get_n_columns (menu);
2581 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2583 gtk_widget_style_get (GTK_WIDGET (menu),
2584 "horizontal-padding", &horizontal_padding,
2587 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2588 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2590 for (children = menu_shell->children; children; children = children->next)
2595 gint child_min, child_nat;
2597 child = children->data;
2599 if (! gtk_widget_get_visible (child))
2602 get_effective_child_attach (child, &l, &r, &t, &b);
2604 part = avail_width / (r - l);
2606 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child), part,
2607 &child_min, &child_nat);
2609 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2611 part = MAX (child_min, toggle_size) / (b - t);
2612 min_heights[t] = MAX (min_heights[t], part);
2614 part = MAX (child_nat, toggle_size) / (b - t);
2615 nat_heights[t] = MAX (nat_heights[t], part);
2618 if (ret_min_heights)
2619 *ret_min_heights = min_heights;
2621 g_free (min_heights);
2623 if (ret_nat_heights)
2624 *ret_nat_heights = nat_heights;
2626 g_free (nat_heights);
2632 gtk_menu_size_allocate (GtkWidget *widget,
2633 GtkAllocation *allocation)
2636 GtkMenuShell *menu_shell;
2638 GtkAllocation child_allocation;
2639 GtkMenuPrivate *priv;
2645 guint vertical_padding;
2646 guint horizontal_padding;
2648 g_return_if_fail (GTK_IS_MENU (widget));
2649 g_return_if_fail (allocation != NULL);
2651 menu = GTK_MENU (widget);
2652 menu_shell = GTK_MENU_SHELL (widget);
2653 priv = gtk_menu_get_private (menu);
2655 gtk_widget_set_allocation (widget, allocation);
2657 style = gtk_widget_get_style (widget);
2659 gtk_widget_style_get (GTK_WIDGET (menu),
2660 "vertical-padding", &vertical_padding,
2661 "horizontal-padding", &horizontal_padding,
2663 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2665 g_free (priv->heights);
2666 priv->heights_length =
2667 calculate_line_heights (menu,
2672 /* refresh our cached height request */
2673 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2674 for (i = 0; i < priv->heights_length; i++)
2675 priv->requested_height += priv->heights[i];
2677 x = border_width + style->xthickness + horizontal_padding;
2678 y = border_width + style->ythickness + vertical_padding;
2680 width = MAX (1, allocation->width - x * 2);
2681 height = MAX (1, allocation->height - y * 2);
2683 if (menu_shell->active)
2684 gtk_menu_scroll_to (menu, menu->scroll_offset);
2686 if (!menu->tearoff_active)
2688 GtkBorder arrow_border;
2690 get_arrows_border (menu, &arrow_border);
2691 y += arrow_border.top;
2692 height -= arrow_border.top;
2693 height -= arrow_border.bottom;
2696 if (gtk_widget_get_realized (widget))
2698 gdk_window_move_resize (gtk_widget_get_window (widget),
2699 allocation->x, allocation->y,
2700 allocation->width, allocation->height);
2702 gdk_window_move_resize (menu->view_window,
2709 if (menu_shell->children)
2711 gint base_width = width / gtk_menu_get_n_columns (menu);
2713 children = menu_shell->children;
2716 child = children->data;
2717 children = children->next;
2719 if (gtk_widget_get_visible (child))
2724 get_effective_child_attach (child, &l, &r, &t, &b);
2726 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2729 tmp = gtk_menu_get_n_columns (menu) - l;
2730 l = gtk_menu_get_n_columns (menu) - r;
2734 child_allocation.width = (r - l) * base_width;
2735 child_allocation.height = 0;
2736 child_allocation.x = l * base_width;
2737 child_allocation.y = 0;
2739 for (i = 0; i < b; i++)
2742 child_allocation.y += priv->heights[i];
2744 child_allocation.height += priv->heights[i];
2747 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2750 gtk_widget_size_allocate (child, &child_allocation);
2751 gtk_widget_queue_draw (child);
2755 /* Resize the item window */
2756 if (gtk_widget_get_realized (widget))
2762 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2763 height += priv->heights[i];
2765 width = gtk_menu_get_n_columns (menu) * base_width;
2766 gdk_window_resize (menu->bin_window, width, height);
2769 if (menu->tearoff_active)
2771 if (height >= priv->requested_height)
2773 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2775 gtk_widget_hide (menu->tearoff_scrollbar);
2776 gtk_menu_set_tearoff_hints (menu, allocation->width);
2778 gtk_menu_scroll_to (menu, 0);
2783 menu->tearoff_adjustment->upper = priv->requested_height;
2784 menu->tearoff_adjustment->page_size = allocation->height;
2786 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2787 menu->tearoff_adjustment->upper)
2790 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2793 gtk_menu_scroll_to (menu, value);
2796 gtk_adjustment_changed (menu->tearoff_adjustment);
2798 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2800 gtk_widget_show (menu->tearoff_scrollbar);
2801 gtk_menu_set_tearoff_hints (menu, allocation->width);
2809 get_arrows_visible_area (GtkMenu *menu,
2810 GdkRectangle *border,
2811 GdkRectangle *upper,
2812 GdkRectangle *lower,
2815 GtkArrowPlacement arrow_placement;
2817 GtkWidget *widget = GTK_WIDGET (menu);
2819 guint vertical_padding;
2820 guint horizontal_padding;
2821 gint scroll_arrow_height;
2823 style = gtk_widget_get_style (widget);
2825 gtk_widget_style_get (widget,
2826 "vertical-padding", &vertical_padding,
2827 "horizontal-padding", &horizontal_padding,
2828 "scroll-arrow-vlength", &scroll_arrow_height,
2829 "arrow-placement", &arrow_placement,
2832 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2833 border->x = border_width + style->xthickness + horizontal_padding;
2834 border->y = border_width + style->ythickness + vertical_padding;
2835 gdk_drawable_get_size (gtk_widget_get_window (widget),
2836 &border->width, &border->height);
2838 switch (arrow_placement)
2840 case GTK_ARROWS_BOTH:
2841 upper->x = border->x;
2842 upper->y = border->y;
2843 upper->width = border->width - 2 * border->x;
2844 upper->height = scroll_arrow_height;
2846 lower->x = border->x;
2847 lower->y = border->height - border->y - scroll_arrow_height;
2848 lower->width = border->width - 2 * border->x;
2849 lower->height = scroll_arrow_height;
2852 case GTK_ARROWS_START:
2853 upper->x = border->x;
2854 upper->y = border->y;
2855 upper->width = (border->width - 2 * border->x) / 2;
2856 upper->height = scroll_arrow_height;
2858 lower->x = border->x + upper->width;
2859 lower->y = border->y;
2860 lower->width = (border->width - 2 * border->x) / 2;
2861 lower->height = scroll_arrow_height;
2864 case GTK_ARROWS_END:
2865 upper->x = border->x;
2866 upper->y = border->height - border->y - scroll_arrow_height;
2867 upper->width = (border->width - 2 * border->x) / 2;
2868 upper->height = scroll_arrow_height;
2870 lower->x = border->x + upper->width;
2871 lower->y = border->height - border->y - scroll_arrow_height;
2872 lower->width = (border->width - 2 * border->x) / 2;
2873 lower->height = scroll_arrow_height;
2877 g_assert_not_reached();
2878 upper->x = upper->y = upper->width = upper->height = 0;
2879 lower->x = lower->y = lower->width = lower->height = 0;
2882 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2886 gtk_menu_paint (GtkWidget *widget,
2887 GdkEventExpose *event)
2890 GtkMenuPrivate *priv;
2892 GdkRectangle border;
2898 g_return_if_fail (GTK_IS_MENU (widget));
2900 menu = GTK_MENU (widget);
2901 priv = gtk_menu_get_private (menu);
2903 style = gtk_widget_get_style (widget);
2904 window = gtk_widget_get_window (widget);
2906 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2908 if (event->window == window)
2910 gfloat arrow_scaling;
2913 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2914 arrow_size = arrow_scaling * arrow_space;
2916 gtk_paint_box (style,
2920 &event->area, widget, "menu",
2923 if (menu->upper_arrow_visible && !menu->tearoff_active)
2925 gtk_paint_box (style,
2927 priv->upper_arrow_state,
2929 &event->area, widget, "menu_scroll_arrow_up",
2935 gtk_paint_arrow (style,
2937 priv->upper_arrow_state,
2939 &event->area, widget, "menu_scroll_arrow_up",
2942 upper.x + (upper.width - arrow_size) / 2,
2943 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2944 arrow_size, arrow_size);
2947 if (menu->lower_arrow_visible && !menu->tearoff_active)
2949 gtk_paint_box (style,
2951 priv->lower_arrow_state,
2953 &event->area, widget, "menu_scroll_arrow_down",
2959 gtk_paint_arrow (style,
2961 priv->lower_arrow_state,
2963 &event->area, widget, "menu_scroll_arrow_down",
2966 lower.x + (lower.width - arrow_size) / 2,
2967 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2968 arrow_size, arrow_size);
2971 else if (event->window == menu->bin_window)
2973 gint y = -border.y + menu->scroll_offset;
2975 if (!menu->tearoff_active)
2977 GtkBorder arrow_border;
2979 get_arrows_border (menu, &arrow_border);
2980 y -= arrow_border.top;
2983 gtk_paint_box (style,
2987 &event->area, widget, "menu",
2989 border.width, border.height);
2994 gtk_menu_expose (GtkWidget *widget,
2995 GdkEventExpose *event)
2997 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2998 g_return_val_if_fail (event != NULL, FALSE);
3000 if (gtk_widget_is_drawable (widget))
3002 gtk_menu_paint (widget, event);
3004 GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
3011 gtk_menu_show (GtkWidget *widget)
3013 GtkMenu *menu = GTK_MENU (widget);
3015 _gtk_menu_refresh_accel_paths (menu, FALSE);
3017 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3023 gtk_menu_size_request_init (GtkSizeRequestIface *iface)
3025 iface->get_width = gtk_menu_get_width;
3026 iface->get_height = gtk_menu_get_height;
3027 iface->get_height_for_width = gtk_menu_get_height_for_width;
3031 gtk_menu_get_width (GtkSizeRequest *widget,
3036 GtkMenuShell *menu_shell;
3037 GtkMenuPrivate *priv;
3041 guint max_toggle_size;
3042 guint max_accel_width;
3043 guint horizontal_padding;
3045 gint child_min, child_nat;
3046 gint min_width, nat_width;
3048 menu = GTK_MENU (widget);
3049 menu_shell = GTK_MENU_SHELL (widget);
3050 priv = gtk_menu_get_private (menu);
3052 style = gtk_widget_get_style (GTK_WIDGET (widget));
3054 min_width = nat_width = 0;
3056 max_toggle_size = 0;
3057 max_accel_width = 0;
3059 children = menu_shell->children;
3066 child = children->data;
3067 children = children->next;
3069 if (! gtk_widget_get_visible (child))
3072 get_effective_child_attach (child, &l, &r, &t, &b);
3074 /* It's important to size_request the child
3075 * before doing the toggle size request, in
3076 * case the toggle size request depends on the size
3077 * request of a child of the child (e.g. for ImageMenuItem)
3080 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3081 gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
3083 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3084 max_toggle_size = MAX (max_toggle_size, toggle_size);
3085 max_accel_width = MAX (max_accel_width,
3086 GTK_MENU_ITEM (child)->accelerator_width);
3088 part = child_min / (r - l);
3089 min_width = MAX (min_width, part);
3091 part = child_nat / (r - l);
3092 nat_width = MAX (nat_width, part);
3095 /* If the menu doesn't include any images or check items
3096 * reserve the space so that all menus are consistent.
3097 * We only do this for 'ordinary' menus, not for combobox
3098 * menus or multi-column menus
3100 if (max_toggle_size == 0 &&
3101 gtk_menu_get_n_columns (menu) == 1 &&
3102 !priv->no_toggle_size)
3104 guint toggle_spacing;
3105 guint indicator_size;
3107 gtk_style_get (style,
3108 GTK_TYPE_CHECK_MENU_ITEM,
3109 "toggle-spacing", &toggle_spacing,
3110 "indicator-size", &indicator_size,
3113 max_toggle_size = indicator_size + toggle_spacing;
3116 min_width += 2 * max_toggle_size + max_accel_width;
3117 min_width *= gtk_menu_get_n_columns (menu);
3119 nat_width += 2 * max_toggle_size + max_accel_width;
3120 nat_width *= gtk_menu_get_n_columns (menu);
3122 gtk_widget_style_get (GTK_WIDGET (menu),
3123 "horizontal-padding", &horizontal_padding,
3126 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3127 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3128 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3130 menu->toggle_size = max_toggle_size;
3131 priv->accel_size = max_accel_width;
3134 *minimum_size = min_width;
3137 *natural_size = nat_width;
3139 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3141 if (menu->tearoff_active)
3142 gtk_menu_set_tearoff_hints (menu, min_width);
3146 gtk_menu_get_height (GtkSizeRequest *widget,
3152 /* Menus are height-for-width only, just return the height for the minimum width */
3153 gtk_size_request_get_width (widget, &min_width, NULL);
3154 gtk_size_request_get_height_for_width (widget, min_width, minimum_size, natural_size);
3158 gtk_menu_get_height_for_width (GtkSizeRequest *widget,
3163 GtkMenu *menu = GTK_MENU (widget);
3164 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3165 guint *min_heights, *nat_heights;
3166 guint vertical_padding, border_width;
3168 gint min_height, nat_height;
3170 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3171 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3173 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3176 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3178 for (i = 0; i < n_heights; i++)
3180 min_height += min_heights[i];
3181 nat_height += nat_heights[i];
3184 if (private->have_position)
3186 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3187 GdkRectangle monitor;
3189 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3191 if (private->y + min_height > monitor.y + monitor.height)
3192 min_height = monitor.y + monitor.height - private->y;
3194 if (private->y + nat_height > monitor.y + monitor.height)
3195 nat_height = monitor.y + monitor.height - private->y;
3197 if (private->y < monitor.y)
3199 min_height -= monitor.y - private->y;
3200 nat_height -= monitor.y - private->y;
3205 *minimum_size = min_height;
3208 *natural_size = nat_height;
3210 g_free (min_heights);
3211 g_free (nat_heights);
3217 gtk_menu_button_scroll (GtkMenu *menu,
3218 GdkEventButton *event)
3220 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3222 gboolean touchscreen_mode;
3224 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3225 "gtk-touchscreen-mode", &touchscreen_mode,
3228 if (touchscreen_mode)
3229 gtk_menu_handle_scrolling (menu,
3230 event->x_root, event->y_root,
3231 event->type == GDK_BUTTON_PRESS,
3241 pointer_in_menu_window (GtkWidget *widget,
3245 GtkAllocation allocation;
3246 GtkMenu *menu = GTK_MENU (widget);
3248 if (gtk_widget_get_mapped (menu->toplevel))
3250 GtkMenuShell *menu_shell;
3251 gint window_x, window_y;
3253 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3254 &window_x, &window_y);
3256 gtk_widget_get_allocation (widget, &allocation);
3257 if (x_root >= window_x && x_root < window_x + allocation.width &&
3258 y_root >= window_y && y_root < window_y + allocation.height)
3261 menu_shell = GTK_MENU_SHELL (widget);
3263 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3264 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3272 gtk_menu_button_press (GtkWidget *widget,
3273 GdkEventButton *event)
3275 if (event->type != GDK_BUTTON_PRESS)
3278 /* Don't pass down to menu shell for presses over scroll arrows
3280 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3283 /* Don't pass down to menu shell if a non-menuitem part of the menu
3284 * was clicked. The check for the event_widget being a GtkMenuShell
3285 * works because we have the pointer grabbed on menu_shell->window
3286 * with owner_events=TRUE, so all events that are either outside
3287 * the menu or on its border are delivered relative to
3288 * menu_shell->window.
3290 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3291 pointer_in_menu_window (widget, event->x_root, event->y_root))
3294 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3298 gtk_menu_button_release (GtkWidget *widget,
3299 GdkEventButton *event)
3301 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3303 if (priv->ignore_button_release)
3305 priv->ignore_button_release = FALSE;
3309 if (event->type != GDK_BUTTON_RELEASE)
3312 /* Don't pass down to menu shell for releases over scroll arrows
3314 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3317 /* Don't pass down to menu shell if a non-menuitem part of the menu
3318 * was clicked (see comment in button_press()).
3320 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3321 pointer_in_menu_window (widget, event->x_root, event->y_root))
3323 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3324 * bail out early here so it is in a consistent state for the
3325 * next button_press/button_release in GtkMenuShell.
3328 if (GTK_MENU_SHELL (widget)->active)
3329 GTK_MENU_SHELL (widget)->button = 0;
3334 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3337 static const gchar *
3338 get_accel_path (GtkWidget *menu_item,
3343 GClosure *accel_closure;
3344 GtkAccelGroup *accel_group;
3346 path = _gtk_widget_get_accel_path (menu_item, locked);
3349 path = GTK_MENU_ITEM (menu_item)->accel_path;
3355 label = gtk_bin_get_child (GTK_BIN (menu_item));
3357 if (GTK_IS_ACCEL_LABEL (label))
3359 g_object_get (label,
3360 "accel-closure", &accel_closure,
3364 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3366 *locked = gtk_accel_group_get_is_locked (accel_group);
3376 gtk_menu_key_press (GtkWidget *widget,
3379 GtkMenuShell *menu_shell;
3381 gboolean delete = FALSE;
3382 gboolean can_change_accels;
3383 gchar *accel = NULL;
3384 guint accel_key, accel_mods;
3385 GdkModifierType consumed_modifiers;
3386 GdkDisplay *display;
3388 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3389 g_return_val_if_fail (event != NULL, FALSE);
3391 menu_shell = GTK_MENU_SHELL (widget);
3392 menu = GTK_MENU (widget);
3394 gtk_menu_stop_navigating_submenu (menu);
3396 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3399 display = gtk_widget_get_display (widget);
3401 g_object_get (gtk_widget_get_settings (widget),
3402 "gtk-menu-bar-accel", &accel,
3403 "gtk-can-change-accels", &can_change_accels,
3406 if (accel && *accel)
3409 GdkModifierType mods = 0;
3411 gtk_accelerator_parse (accel, &keyval, &mods);
3414 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3416 /* FIXME this is wrong, needs to be in the global accel resolution
3417 * thing, to properly consider i18n etc., but that probably requires
3418 * AccelGroup changes etc.
3420 if (event->keyval == keyval && (mods & event->state) == mods)
3422 gtk_menu_shell_cancel (menu_shell);
3430 switch (event->keyval)
3432 case GDK_KEY_Delete:
3433 case GDK_KEY_KP_Delete:
3434 case GDK_KEY_BackSpace:
3441 /* Figure out what modifiers went into determining the key symbol */
3442 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3443 event->hardware_keycode, event->state, event->group,
3444 NULL, NULL, NULL, &consumed_modifiers);
3446 accel_key = gdk_keyval_to_lower (event->keyval);
3447 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3449 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3450 * We re-upper case when we match against the keyval, but display and save in caseless form.
3452 if (accel_key != event->keyval)
3453 accel_mods |= GDK_SHIFT_MASK;
3455 /* Modify the accelerators */
3456 if (can_change_accels &&
3457 menu_shell->active_menu_item &&
3458 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3459 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3460 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3462 GtkWidget *menu_item = menu_shell->active_menu_item;
3463 gboolean locked, replace_accels = TRUE;
3466 path = get_accel_path (menu_item, &locked);
3467 if (!path || locked)
3469 /* can't change accelerators on menu_items without paths
3470 * (basically, those items are accelerator-locked).
3472 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3473 gtk_widget_error_bell (widget);
3479 /* For the keys that act to delete the current setting, we delete
3480 * the current setting if there is one, otherwise, we set the
3481 * key as the accelerator.
3487 if (gtk_accel_map_lookup_entry (path, &key) &&
3488 (key.accel_key || key.accel_mods))
3494 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3498 /* we failed, probably because this key is in use and
3501 /* g_print("failed to change\n"); */
3502 gtk_widget_error_bell (widget);
3511 check_threshold (GtkWidget *widget,
3520 ABS (start_x - x) > THRESHOLD ||
3521 ABS (start_y - y) > THRESHOLD;
3525 definitely_within_item (GtkWidget *widget,
3529 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3532 gdk_drawable_get_size (window, &w, &h);
3535 check_threshold (widget, 0, 0, x, y) &&
3536 check_threshold (widget, w - 1, 0, x, y) &&
3537 check_threshold (widget, w - 1, h - 1, x, y) &&
3538 check_threshold (widget, 0, h - 1, x, y);
3542 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3544 GtkMenuPrivate *priv;
3546 priv = gtk_menu_get_private (menu);
3548 return priv->navigation_height && priv->navigation_width;
3552 gtk_menu_motion_notify (GtkWidget *widget,
3553 GdkEventMotion *event)
3555 GtkWidget *menu_item;
3557 GtkMenuShell *menu_shell;
3560 gboolean need_enter;
3562 if (GTK_IS_MENU (widget))
3564 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3566 if (priv->ignore_button_release)
3567 priv->ignore_button_release = FALSE;
3569 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3573 /* We received the event for one of two reasons:
3575 * a) We are the active menu, and did gtk_grab_add()
3576 * b) The widget is a child of ours, and the event was propagated
3578 * Since for computation of navigation regions, we want the menu which
3579 * is the parent of the menu item, for a), we need to find that menu,
3580 * which may be different from 'widget'.
3582 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3583 parent = gtk_widget_get_parent (menu_item);
3584 if (!GTK_IS_MENU_ITEM (menu_item) ||
3585 !GTK_IS_MENU (parent))
3588 menu_shell = GTK_MENU_SHELL (parent);
3589 menu = GTK_MENU (menu_shell);
3591 if (definitely_within_item (menu_item, event->x, event->y))
3592 menu_shell->activate_time = 0;
3594 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3596 /* Check to see if we are within an active submenu's navigation region
3598 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3601 /* Make sure we pop down if we enter a non-selectable menu item, so we
3602 * don't show a submenu when the cursor is outside the stay-up triangle.
3604 if (!_gtk_menu_item_is_selectable (menu_item))
3606 /* We really want to deselect, but this gives the menushell code
3607 * a chance to do some bookkeeping about the menuitem.
3609 gtk_menu_shell_select_item (menu_shell, menu_item);
3615 /* The menu is now sensitive to enter events on its items, but
3616 * was previously sensitive. So we fake an enter event.
3620 menu_shell->ignore_enter = FALSE;
3622 gdk_drawable_get_size (event->window, &width, &height);
3623 if (event->x >= 0 && event->x < width &&
3624 event->y >= 0 && event->y < height)
3626 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3629 send_event->crossing.window = g_object_ref (event->window);
3630 send_event->crossing.time = event->time;
3631 send_event->crossing.send_event = TRUE;
3632 send_event->crossing.x_root = event->x_root;
3633 send_event->crossing.y_root = event->y_root;
3634 send_event->crossing.x = event->x;
3635 send_event->crossing.y = event->y;
3636 send_event->crossing.state = event->state;
3637 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3639 /* We send the event to 'widget', the currently active menu,
3640 * instead of 'menu', the menu that the pointer is in. This
3641 * will ensure that the event will be ignored unless the
3642 * menuitem is a child of the active menu or some parent
3643 * menu of the active menu.
3645 result = gtk_widget_event (widget, send_event);
3646 gdk_event_free (send_event);
3656 get_double_arrows (GtkMenu *menu)
3658 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3659 gboolean double_arrows;
3660 GtkArrowPlacement arrow_placement;
3662 gtk_widget_style_get (GTK_WIDGET (menu),
3663 "double-arrows", &double_arrows,
3664 "arrow-placement", &arrow_placement,
3667 if (arrow_placement != GTK_ARROWS_BOTH)
3670 return double_arrows || (priv->initially_pushed_in &&
3671 menu->scroll_offset != 0);
3675 gtk_menu_scroll_by (GtkMenu *menu,
3678 GtkMenuPrivate *priv;
3679 GtkBorder arrow_border;
3682 gint view_width, view_height;
3683 gboolean double_arrows;
3685 widget = GTK_WIDGET (menu);
3686 offset = menu->scroll_offset + step;
3687 priv = gtk_menu_get_private (menu);
3689 get_arrows_border (menu, &arrow_border);
3691 double_arrows = get_double_arrows (menu);
3693 /* If we scroll upward and the non-visible top part
3694 * is smaller than the scroll arrow it would be
3695 * pretty stupid to show the arrow and taking more
3696 * screen space than just scrolling to the top.
3699 if ((step < 0) && (offset < arrow_border.top))
3702 /* Don't scroll over the top if we weren't before: */
3703 if ((menu->scroll_offset >= 0) && (offset < 0))
3706 gdk_drawable_get_size (gtk_widget_get_window (widget),
3707 &view_width, &view_height);
3709 if (menu->scroll_offset == 0 &&
3710 view_height >= priv->requested_height)
3713 /* Don't scroll past the bottom if we weren't before: */
3714 if (menu->scroll_offset > 0)
3715 view_height -= arrow_border.top;
3717 /* When both arrows are always shown, reduce
3718 * view height even more.
3721 view_height -= arrow_border.bottom;
3723 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3724 (offset + view_height > priv->requested_height))
3725 offset = priv->requested_height - view_height;
3727 if (offset != menu->scroll_offset)
3728 gtk_menu_scroll_to (menu, offset);
3732 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3733 gboolean touchscreen_mode)
3735 gboolean upper_visible;
3736 gboolean lower_visible;
3738 upper_visible = menu->upper_arrow_visible;
3739 lower_visible = menu->lower_arrow_visible;
3741 gtk_menu_scroll_by (menu, menu->scroll_step);
3743 if (touchscreen_mode &&
3744 (upper_visible != menu->upper_arrow_visible ||
3745 lower_visible != menu->lower_arrow_visible))
3747 /* We are about to hide a scroll arrow while the mouse is pressed,
3748 * this would cause the uncovered menu item to be activated on button
3749 * release. Therefore we need to ignore button release here
3751 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3752 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3757 gtk_menu_scroll_timeout (gpointer data)
3760 gboolean touchscreen_mode;
3762 menu = GTK_MENU (data);
3764 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3765 "gtk-touchscreen-mode", &touchscreen_mode,
3768 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3774 gtk_menu_scroll_timeout_initial (gpointer data)
3778 gboolean touchscreen_mode;
3780 menu = GTK_MENU (data);
3782 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3783 "gtk-timeout-repeat", &timeout,
3784 "gtk-touchscreen-mode", &touchscreen_mode,
3787 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3789 gtk_menu_remove_scroll_timeout (menu);
3791 menu->timeout_id = gdk_threads_add_timeout (timeout,
3792 gtk_menu_scroll_timeout,
3799 gtk_menu_start_scrolling (GtkMenu *menu)
3802 gboolean touchscreen_mode;
3804 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3805 "gtk-timeout-repeat", &timeout,
3806 "gtk-touchscreen-mode", &touchscreen_mode,
3809 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3811 menu->timeout_id = gdk_threads_add_timeout (timeout,
3812 gtk_menu_scroll_timeout_initial,
3817 gtk_menu_scroll (GtkWidget *widget,
3818 GdkEventScroll *event)
3820 GtkMenu *menu = GTK_MENU (widget);
3822 switch (event->direction)
3824 case GDK_SCROLL_RIGHT:
3825 case GDK_SCROLL_DOWN:
3826 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3828 case GDK_SCROLL_LEFT:
3830 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3838 get_arrows_sensitive_area (GtkMenu *menu,
3839 GdkRectangle *upper,
3840 GdkRectangle *lower)
3842 GtkArrowPlacement arrow_placement;
3843 GtkWidget *widget = GTK_WIDGET (menu);
3847 guint vertical_padding;
3849 gint scroll_arrow_height;
3851 window = gtk_widget_get_window (widget);
3853 gdk_drawable_get_size (window, &width, &height);
3855 gtk_widget_style_get (widget,
3856 "vertical-padding", &vertical_padding,
3857 "scroll-arrow-vlength", &scroll_arrow_height,
3858 "arrow-placement", &arrow_placement,
3861 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3862 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3864 gdk_window_get_position (window, &win_x, &win_y);
3866 switch (arrow_placement)
3868 case GTK_ARROWS_BOTH:
3873 upper->width = width;
3874 upper->height = scroll_arrow_height + border;
3880 lower->y = win_y + height - border - scroll_arrow_height;
3881 lower->width = width;
3882 lower->height = scroll_arrow_height + border;
3886 case GTK_ARROWS_START:
3891 upper->width = width / 2;
3892 upper->height = scroll_arrow_height + border;
3897 lower->x = win_x + width / 2;
3899 lower->width = width / 2;
3900 lower->height = scroll_arrow_height + border;
3904 case GTK_ARROWS_END:
3908 upper->y = win_y + height - border - scroll_arrow_height;
3909 upper->width = width / 2;
3910 upper->height = scroll_arrow_height + border;
3915 lower->x = win_x + width / 2;
3916 lower->y = win_y + height - border - scroll_arrow_height;
3917 lower->width = width / 2;
3918 lower->height = scroll_arrow_height + border;
3926 gtk_menu_handle_scrolling (GtkMenu *menu,
3932 GtkMenuShell *menu_shell;
3933 GtkMenuPrivate *priv;
3936 gboolean scroll_fast = FALSE;
3938 gboolean touchscreen_mode;
3940 priv = gtk_menu_get_private (menu);
3942 menu_shell = GTK_MENU_SHELL (menu);
3944 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3945 "gtk-touchscreen-mode", &touchscreen_mode,
3948 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3953 /* upper arrow handling */
3955 get_arrows_sensitive_area (menu, &rect, NULL);
3958 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3959 (x >= rect.x) && (x < rect.x + rect.width) &&
3960 (y >= rect.y) && (y < rect.y + rect.height))
3965 if (touchscreen_mode)
3966 menu->upper_arrow_prelight = in_arrow;
3968 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3970 gboolean arrow_pressed = FALSE;
3972 if (menu->upper_arrow_visible && !menu->tearoff_active)
3974 if (touchscreen_mode)
3976 if (enter && menu->upper_arrow_prelight)
3978 if (menu->timeout_id == 0)
3980 /* Deselect the active item so that
3981 * any submenus are popped down
3983 gtk_menu_shell_deselect (menu_shell);
3985 gtk_menu_remove_scroll_timeout (menu);
3986 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3990 /* Only do stuff on click. */
3991 gtk_menu_start_scrolling (menu);
3992 arrow_pressed = TRUE;
3997 arrow_pressed = TRUE;
4002 gtk_menu_stop_scrolling (menu);
4005 else /* !touchscreen_mode */
4007 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4009 if (enter && in_arrow &&
4010 (!menu->upper_arrow_prelight ||
4011 menu->scroll_fast != scroll_fast))
4013 menu->upper_arrow_prelight = TRUE;
4014 menu->scroll_fast = scroll_fast;
4016 /* Deselect the active item so that
4017 * any submenus are popped down
4019 gtk_menu_shell_deselect (menu_shell);
4021 gtk_menu_remove_scroll_timeout (menu);
4022 menu->scroll_step = scroll_fast ?
4023 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4026 gdk_threads_add_timeout (scroll_fast ?
4027 MENU_SCROLL_TIMEOUT2 :
4028 MENU_SCROLL_TIMEOUT1,
4029 gtk_menu_scroll_timeout, menu);
4031 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4033 gtk_menu_stop_scrolling (menu);
4038 /* gtk_menu_start_scrolling() might have hit the top of the
4039 * menu, so check if the button isn't insensitive before
4040 * changing it to something else.
4042 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4044 GtkStateType arrow_state = GTK_STATE_NORMAL;
4047 arrow_state = GTK_STATE_ACTIVE;
4048 else if (menu->upper_arrow_prelight)
4049 arrow_state = GTK_STATE_PRELIGHT;
4051 if (arrow_state != priv->upper_arrow_state)
4053 priv->upper_arrow_state = arrow_state;
4055 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4061 /* lower arrow handling */
4063 get_arrows_sensitive_area (menu, NULL, &rect);
4066 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4067 (x >= rect.x) && (x < rect.x + rect.width) &&
4068 (y >= rect.y) && (y < rect.y + rect.height))
4073 if (touchscreen_mode)
4074 menu->lower_arrow_prelight = in_arrow;
4076 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4078 gboolean arrow_pressed = FALSE;
4080 if (menu->lower_arrow_visible && !menu->tearoff_active)
4082 if (touchscreen_mode)
4084 if (enter && menu->lower_arrow_prelight)
4086 if (menu->timeout_id == 0)
4088 /* Deselect the active item so that
4089 * any submenus are popped down
4091 gtk_menu_shell_deselect (menu_shell);
4093 gtk_menu_remove_scroll_timeout (menu);
4094 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4098 /* Only do stuff on click. */
4099 gtk_menu_start_scrolling (menu);
4100 arrow_pressed = TRUE;
4105 arrow_pressed = TRUE;
4110 gtk_menu_stop_scrolling (menu);
4113 else /* !touchscreen_mode */
4115 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4117 if (enter && in_arrow &&
4118 (!menu->lower_arrow_prelight ||
4119 menu->scroll_fast != scroll_fast))
4121 menu->lower_arrow_prelight = TRUE;
4122 menu->scroll_fast = scroll_fast;
4124 /* Deselect the active item so that
4125 * any submenus are popped down
4127 gtk_menu_shell_deselect (menu_shell);
4129 gtk_menu_remove_scroll_timeout (menu);
4130 menu->scroll_step = scroll_fast ?
4131 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4134 gdk_threads_add_timeout (scroll_fast ?
4135 MENU_SCROLL_TIMEOUT2 :
4136 MENU_SCROLL_TIMEOUT1,
4137 gtk_menu_scroll_timeout, menu);
4139 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4141 gtk_menu_stop_scrolling (menu);
4146 /* gtk_menu_start_scrolling() might have hit the bottom of the
4147 * menu, so check if the button isn't insensitive before
4148 * changing it to something else.
4150 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4152 GtkStateType arrow_state = GTK_STATE_NORMAL;
4155 arrow_state = GTK_STATE_ACTIVE;
4156 else if (menu->lower_arrow_prelight)
4157 arrow_state = GTK_STATE_PRELIGHT;
4159 if (arrow_state != priv->lower_arrow_state)
4161 priv->lower_arrow_state = arrow_state;
4163 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4171 gtk_menu_enter_notify (GtkWidget *widget,
4172 GdkEventCrossing *event)
4174 GtkWidget *menu_item;
4176 gboolean touchscreen_mode;
4178 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4179 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4180 event->mode == GDK_CROSSING_STATE_CHANGED)
4183 g_object_get (gtk_widget_get_settings (widget),
4184 "gtk-touchscreen-mode", &touchscreen_mode,
4187 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4188 if (GTK_IS_MENU (widget))
4190 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4192 if (!menu_shell->ignore_enter)
4193 gtk_menu_handle_scrolling (GTK_MENU (widget),
4194 event->x_root, event->y_root, TRUE, TRUE);
4197 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4199 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4201 if (GTK_IS_MENU (menu))
4203 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4204 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4206 if (priv->seen_item_enter)
4208 /* This is the second enter we see for an item
4209 * on this menu. This means a release should always
4212 menu_shell->activate_time = 0;
4214 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4215 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4217 if (definitely_within_item (menu_item, event->x, event->y))
4219 /* This is an actual user-enter (ie. not a pop-under)
4220 * In this case, the user must either have entered
4221 * sufficiently far enough into the item, or he must move
4222 * far enough away from the enter point. (see
4223 * gtk_menu_motion_notify())
4225 menu_shell->activate_time = 0;
4229 priv->seen_item_enter = TRUE;
4233 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4234 * will not correspond to the event widget's parent. Check to see
4235 * if we are in the parent's navigation region.
4237 parent = gtk_widget_get_parent (menu_item);
4238 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4239 gtk_menu_navigating_submenu (GTK_MENU (parent),
4240 event->x_root, event->y_root))
4243 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4247 gtk_menu_leave_notify (GtkWidget *widget,
4248 GdkEventCrossing *event)
4250 GtkMenuShell *menu_shell;
4252 GtkMenuItem *menu_item;
4253 GtkWidget *event_widget;
4255 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4256 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4257 event->mode == GDK_CROSSING_STATE_CHANGED)
4260 menu = GTK_MENU (widget);
4261 menu_shell = GTK_MENU_SHELL (widget);
4263 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4266 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4268 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4270 if (!GTK_IS_MENU_ITEM (event_widget))
4273 menu_item = GTK_MENU_ITEM (event_widget);
4275 /* Here we check to see if we're leaving an active menu item with a submenu,
4276 * in which case we enter submenu navigation mode.
4278 if (menu_shell->active_menu_item != NULL
4279 && menu_item->submenu != NULL
4280 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4282 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4284 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4287 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4289 /* We are leaving an active menu item with nonactive submenu.
4290 * Deselect it so we don't surprise the user with by popping
4291 * up a submenu _after_ he left the item.
4293 gtk_menu_shell_deselect (menu_shell);
4298 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4302 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4304 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4306 priv->navigation_x = 0;
4307 priv->navigation_y = 0;
4308 priv->navigation_width = 0;
4309 priv->navigation_height = 0;
4311 if (menu->navigation_timeout)
4313 g_source_remove (menu->navigation_timeout);
4314 menu->navigation_timeout = 0;
4318 /* When the timeout is elapsed, the navigation region is destroyed
4319 * and the menuitem under the pointer (if any) is selected.
4322 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4324 GtkMenuPopdownData *popdown_data = user_data;
4325 GtkMenu *menu = popdown_data->menu;
4326 GdkWindow *child_window;
4328 gtk_menu_stop_navigating_submenu (menu);
4330 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4332 child_window = gdk_window_get_device_position (menu->bin_window,
4333 popdown_data->device,
4338 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4340 send_event->crossing.window = g_object_ref (child_window);
4341 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4342 send_event->crossing.send_event = TRUE;
4343 gdk_event_set_device (send_event, popdown_data->device);
4345 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4347 gdk_event_free (send_event);
4355 gtk_menu_navigating_submenu (GtkMenu *menu,
4359 GtkMenuPrivate *priv;
4362 if (!gtk_menu_has_navigation_triangle (menu))
4365 priv = gtk_menu_get_private (menu);
4366 width = priv->navigation_width;
4367 height = priv->navigation_height;
4369 /* check if x/y are in the triangle spanned by the navigation parameters */
4371 /* 1) Move the coordinates so the triangle starts at 0,0 */
4372 event_x -= priv->navigation_x;
4373 event_y -= priv->navigation_y;
4375 /* 2) Ensure both legs move along the positive axis */
4387 /* 3) Check that the given coordinate is inside the triangle. The formula
4388 * is a transformed form of this formula: x/w + y/h <= 1
4390 if (event_x >= 0 && event_y >= 0 &&
4391 event_x * height + event_y * width <= width * height)
4397 gtk_menu_stop_navigating_submenu (menu);
4403 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4404 GtkMenuItem *menu_item,
4405 GdkEventCrossing *event)
4407 gint submenu_left = 0;
4408 gint submenu_right = 0;
4409 gint submenu_top = 0;
4410 gint submenu_bottom = 0;
4413 GtkWidget *event_widget;
4414 GtkMenuPopdownData *popdown_data;
4415 GtkMenuPrivate *priv;
4418 g_return_if_fail (menu_item->submenu != NULL);
4419 g_return_if_fail (event != NULL);
4421 priv = gtk_menu_get_private (menu);
4423 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4425 window = gtk_widget_get_window (menu_item->submenu);
4426 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4427 gdk_drawable_get_size (window, &width, &height);
4429 submenu_right = submenu_left + width;
4430 submenu_bottom = submenu_top + height;
4432 gdk_drawable_get_size (gtk_widget_get_window (event_widget),
4435 if (event->x >= 0 && event->x < width)
4439 gtk_menu_stop_navigating_submenu (menu);
4441 /* The navigation region is the triangle closest to the x/y
4442 * location of the rectangle. This is why the width or height
4446 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4449 priv->navigation_x = submenu_left;
4450 priv->navigation_width = event->x_root - submenu_left;
4455 priv->navigation_x = submenu_right;
4456 priv->navigation_width = event->x_root - submenu_right;
4462 priv->navigation_y = event->y_root;
4463 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4465 if (priv->navigation_height >= 0)
4471 priv->navigation_y = event->y_root;
4472 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4474 if (priv->navigation_height <= 0)
4478 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4479 "gtk-menu-popdown-delay", &popdown_delay,
4482 popdown_data = g_new (GtkMenuPopdownData, 1);
4483 popdown_data->menu = menu;
4484 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4486 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4488 gtk_menu_stop_navigating_submenu_cb,
4490 (GDestroyNotify) g_free);
4495 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4499 g_return_if_fail (GTK_IS_MENU (menu_shell));
4501 parent = menu_shell->parent_menu_shell;
4503 menu_shell->activate_time = 0;
4504 gtk_menu_popdown (GTK_MENU (menu_shell));
4507 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4511 gtk_menu_position (GtkMenu *menu)
4514 GtkRequisition requisition;
4515 GtkMenuPrivate *private;
4520 GdkScreen *pointer_screen;
4521 GdkRectangle monitor;
4524 g_return_if_fail (GTK_IS_MENU (menu));
4526 widget = GTK_WIDGET (menu);
4528 screen = gtk_widget_get_screen (widget);
4529 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4530 gdk_display_get_device_state (gdk_screen_get_display (screen),
4531 pointer, &pointer_screen, &x, &y, NULL);
4533 /* Get the minimum height for minimum width to figure out
4534 * the right place to popup the menu.
4536 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
4538 if (pointer_screen != screen)
4540 /* Pointer is on a different screen; roughly center the
4541 * menu on the screen. If someone was using multiscreen
4542 * + Xinerama together they'd probably want something
4543 * fancier; but that is likely to be vanishingly rare.
4545 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4546 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4549 private = gtk_menu_get_private (menu);
4550 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4552 private->initially_pushed_in = FALSE;
4554 /* Set the type hint here to allow custom position functions to set a different hint */
4555 if (!gtk_widget_get_visible (menu->toplevel))
4556 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4558 if (menu->position_func)
4560 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4561 menu->position_func_data);
4563 if (private->monitor_num < 0)
4564 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4566 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4570 GtkStyle *style = gtk_widget_get_style (widget);
4571 gint space_left, space_right, space_above, space_below;
4574 gint xthickness = style->xthickness;
4575 gint ythickness = style->ythickness;
4576 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4578 /* The placement of popup menus horizontally works like this (with
4579 * RTL in parentheses)
4581 * - If there is enough room to the right (left) of the mouse cursor,
4582 * position the menu there.
4584 * - Otherwise, if if there is enough room to the left (right) of the
4585 * mouse cursor, position the menu there.
4587 * - Otherwise if the menu is smaller than the monitor, position it
4588 * on the side of the mouse cursor that has the most space available
4590 * - Otherwise (if there is simply not enough room for the menu on the
4591 * monitor), position it as far left (right) as possible.
4593 * Positioning in the vertical direction is similar: first try below
4594 * mouse cursor, then above.
4596 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4598 space_left = x - monitor.x;
4599 space_right = monitor.x + monitor.width - x - 1;
4600 space_above = y - monitor.y;
4601 space_below = monitor.y + monitor.height - y - 1;
4603 /* position horizontally */
4605 /* the amount of space we need to position the menu. Note the
4606 * menu is offset "xthickness" pixels
4608 needed_width = requisition.width - xthickness;
4610 if (needed_width <= space_left ||
4611 needed_width <= space_right)
4613 if ((rtl && needed_width <= space_left) ||
4614 (!rtl && needed_width > space_right))
4617 x = x + xthickness - requisition.width + 1;
4621 /* position right */
4625 /* x is clamped on-screen further down */
4627 else if (requisition.width <= monitor.width)
4629 /* the menu is too big to fit on either side of the mouse
4630 * cursor, but smaller than the monitor. Position it on
4631 * the side that has the most space
4633 if (space_left > space_right)
4641 x = monitor.x + monitor.width - requisition.width;
4644 else /* menu is simply too big for the monitor */
4649 x = monitor.x + monitor.width - requisition.width;
4658 /* Position vertically. The algorithm is the same as above, but
4659 * simpler because we don't have to take RTL into account.
4661 needed_height = requisition.height - ythickness;
4663 if (needed_height <= space_above ||
4664 needed_height <= space_below)
4666 if (needed_height <= space_below)
4669 y = y + ythickness - requisition.height + 1;
4671 y = CLAMP (y, monitor.y,
4672 monitor.y + monitor.height - requisition.height);
4674 else if (needed_height > space_below && needed_height > space_above)
4676 if (space_below >= space_above)
4677 y = monitor.y + monitor.height - requisition.height;
4689 if (private->initially_pushed_in)
4691 menu_height = requisition.height;
4693 if (y + menu_height > monitor.y + monitor.height)
4695 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4696 y = (monitor.y + monitor.height) - menu_height;
4701 scroll_offset += monitor.y - y;
4706 /* FIXME: should this be done in the various position_funcs ? */
4707 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4709 if (GTK_MENU_SHELL (menu)->active)
4711 private->have_position = TRUE;
4716 if (y + requisition.height > monitor.y + monitor.height)
4717 requisition.height = (monitor.y + monitor.height) - y;
4721 scroll_offset += monitor.y - y;
4722 requisition.height -= monitor.y - y;
4726 if (scroll_offset > 0)
4728 GtkBorder arrow_border;
4730 get_arrows_border (menu, &arrow_border);
4731 scroll_offset += arrow_border.top;
4734 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4737 if (!GTK_MENU_SHELL (menu)->active)
4739 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4740 requisition.width, requisition.height);
4743 menu->scroll_offset = scroll_offset;
4747 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4749 if (menu->timeout_id)
4751 g_source_remove (menu->timeout_id);
4752 menu->timeout_id = 0;
4757 gtk_menu_stop_scrolling (GtkMenu *menu)
4759 gboolean touchscreen_mode;
4761 gtk_menu_remove_scroll_timeout (menu);
4763 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4764 "gtk-touchscreen-mode", &touchscreen_mode,
4767 if (!touchscreen_mode)
4769 menu->upper_arrow_prelight = FALSE;
4770 menu->lower_arrow_prelight = FALSE;
4775 gtk_menu_scroll_to (GtkMenu *menu,
4778 GtkMenuPrivate *priv;
4779 GtkAllocation allocation;
4780 GtkBorder arrow_border;
4784 gint view_width, view_height;
4787 guint vertical_padding;
4788 guint horizontal_padding;
4789 gboolean double_arrows;
4791 widget = GTK_WIDGET (menu);
4792 priv = gtk_menu_get_private (menu);
4794 if (menu->tearoff_active &&
4795 menu->tearoff_adjustment &&
4796 (menu->tearoff_adjustment->value != offset))
4798 menu->tearoff_adjustment->value =
4800 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4801 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4804 /* Move/resize the viewport according to arrows: */
4805 gtk_widget_get_allocation (widget, &allocation);
4806 view_width = allocation.width;
4807 view_height = allocation.height;
4809 style = gtk_widget_get_style (widget);
4811 gtk_widget_style_get (GTK_WIDGET (menu),
4812 "vertical-padding", &vertical_padding,
4813 "horizontal-padding", &horizontal_padding,
4816 double_arrows = get_double_arrows (menu);
4818 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4820 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4821 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4822 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4824 x = border_width + style->xthickness + horizontal_padding;
4825 y = border_width + style->ythickness + vertical_padding;
4827 if (double_arrows && !menu->tearoff_active)
4829 if (view_height < menu_height ||
4830 (offset > 0 && menu->scroll_offset > 0) ||
4831 (offset < 0 && menu->scroll_offset < 0))
4833 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4834 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4835 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4837 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4838 gtk_widget_queue_draw (GTK_WIDGET (menu));
4840 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4842 get_arrows_border (menu, &arrow_border);
4843 y += arrow_border.top;
4844 view_height -= arrow_border.top;
4845 view_height -= arrow_border.bottom;
4848 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4849 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4850 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4851 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4853 if (offset >= menu_height - view_height)
4854 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4855 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4856 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4857 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4859 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4860 (priv->lower_arrow_state != lower_arrow_previous_state))
4861 gtk_widget_queue_draw (GTK_WIDGET (menu));
4863 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4864 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4866 /* At the upper border, possibly remove timeout */
4867 if (menu->scroll_step < 0)
4869 gtk_menu_stop_scrolling (menu);
4870 gtk_widget_queue_draw (GTK_WIDGET (menu));
4874 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4875 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4877 /* At the lower border, possibly remove timeout */
4878 if (menu->scroll_step > 0)
4880 gtk_menu_stop_scrolling (menu);
4881 gtk_widget_queue_draw (GTK_WIDGET (menu));
4885 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4889 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4890 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4892 gtk_menu_stop_scrolling (menu);
4893 gtk_widget_queue_draw (GTK_WIDGET (menu));
4896 else if (!menu->tearoff_active)
4898 gboolean last_visible;
4900 last_visible = menu->upper_arrow_visible;
4901 menu->upper_arrow_visible = offset > 0;
4903 /* upper_arrow_visible may have changed, so requery the border */
4904 get_arrows_border (menu, &arrow_border);
4905 view_height -= arrow_border.top;
4907 if ((last_visible != menu->upper_arrow_visible) &&
4908 !menu->upper_arrow_visible)
4910 menu->upper_arrow_prelight = FALSE;
4912 /* If we hid the upper arrow, possibly remove timeout */
4913 if (menu->scroll_step < 0)
4915 gtk_menu_stop_scrolling (menu);
4916 gtk_widget_queue_draw (GTK_WIDGET (menu));
4920 last_visible = menu->lower_arrow_visible;
4921 menu->lower_arrow_visible = offset < menu_height - view_height;
4923 /* lower_arrow_visible may have changed, so requery the border */
4924 get_arrows_border (menu, &arrow_border);
4925 view_height -= arrow_border.bottom;
4927 if ((last_visible != menu->lower_arrow_visible) &&
4928 !menu->lower_arrow_visible)
4930 menu->lower_arrow_prelight = FALSE;
4932 /* If we hid the lower arrow, possibly remove timeout */
4933 if (menu->scroll_step > 0)
4935 gtk_menu_stop_scrolling (menu);
4936 gtk_widget_queue_draw (GTK_WIDGET (menu));
4940 y += arrow_border.top;
4943 /* Scroll the menu: */
4944 if (gtk_widget_get_realized (widget))
4945 gdk_window_move (menu->bin_window, 0, -offset);
4947 if (gtk_widget_get_realized (widget))
4948 gdk_window_move_resize (menu->view_window,
4954 menu->scroll_offset = offset;
4958 compute_child_offset (GtkMenu *menu,
4959 GtkWidget *menu_item,
4962 gboolean *is_last_child)
4964 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4965 gint item_top_attach;
4966 gint item_bottom_attach;
4967 gint child_offset = 0;
4970 get_effective_child_attach (menu_item, NULL, NULL,
4971 &item_top_attach, &item_bottom_attach);
4973 /* there is a possibility that we get called before _size_request, so
4974 * check the height table for safety.
4976 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4979 /* when we have a row with only invisible children, it's height will
4980 * be zero, so there's no need to check WIDGET_VISIBLE here
4982 for (i = 0; i < item_top_attach; i++)
4983 child_offset += priv->heights[i];
4986 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4988 *offset = child_offset;
4990 *height = priv->heights[item_top_attach];
4996 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4997 GtkWidget *menu_item)
4999 GtkMenu *menu = GTK_MENU (menu_shell);
5000 GtkWidget *widget = GTK_WIDGET (menu_shell);
5001 gint child_offset, child_height;
5005 gboolean last_child = 0;
5007 /* We need to check if the selected item fully visible.
5008 * If not we need to scroll the menu so that it becomes fully
5012 if (compute_child_offset (menu, menu_item,
5013 &child_offset, &child_height, &last_child))
5015 guint vertical_padding;
5016 gboolean double_arrows;
5018 y = menu->scroll_offset;
5019 gdk_drawable_get_size (gtk_widget_get_window (widget),
5022 gtk_widget_style_get (widget,
5023 "vertical-padding", &vertical_padding,
5026 double_arrows = get_double_arrows (menu);
5028 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5029 2 * gtk_widget_get_style (widget)->ythickness +
5030 2 * vertical_padding;
5031 if (child_offset < y)
5033 /* Ignore the enter event we might get if the pointer is on the menu
5035 menu_shell->ignore_enter = TRUE;
5036 gtk_menu_scroll_to (menu, child_offset);
5040 GtkBorder arrow_border;
5044 get_arrows_border (menu, &arrow_border);
5045 if (!menu->tearoff_active)
5046 arrow_height = arrow_border.top + arrow_border.bottom;
5048 if (child_offset + child_height > y + height - arrow_height)
5051 if ((!last_child && !menu->tearoff_active) || double_arrows)
5052 arrow_height += arrow_border.bottom;
5054 y = child_offset + child_height - height + arrow_height;
5055 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5057 /* Need upper arrow */
5058 arrow_height += arrow_border.top;
5059 y = child_offset + child_height - height + arrow_height;
5061 /* Ignore the enter event we might get if the pointer is on the menu
5063 menu_shell->ignore_enter = TRUE;
5064 gtk_menu_scroll_to (menu, y);
5072 gtk_menu_select_item (GtkMenuShell *menu_shell,
5073 GtkWidget *menu_item)
5075 GtkMenu *menu = GTK_MENU (menu_shell);
5077 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5078 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5080 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5084 /* Reparent the menu, taking care of the refcounting
5086 * If unrealize is true we force a unrealize while reparenting the parent.
5087 * This can help eliminate flicker in some cases.
5089 * What happens is that when the menu is unrealized and then re-realized,
5090 * the allocations are as follows:
5092 * parent - 1x1 at (0,0)
5093 * child1 - 100x20 at (0,0)
5094 * child2 - 100x20 at (0,20)
5095 * child3 - 100x20 at (0,40)
5097 * That is, the parent is small but the children are full sized. Then,
5098 * when the queued_resize gets processed, the parent gets resized to
5101 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5102 * contains the following logic:
5104 * - if a move or resize operation on a window would change the clip
5105 * region on the children, then before the window is resized
5106 * the background for children is temporarily set to None, the
5107 * move/resize done, and the background for the children restored.
5109 * So, at the point where the parent is resized to final size, the
5110 * background for the children is temporarily None, and thus they
5111 * are not cleared to the background color and the previous background
5112 * (the image of the menu) is left in place.
5115 gtk_menu_reparent (GtkMenu *menu,
5116 GtkWidget *new_parent,
5119 GtkObject *object = GTK_OBJECT (menu);
5120 GtkWidget *widget = GTK_WIDGET (menu);
5121 gboolean was_floating = g_object_is_floating (object);
5123 g_object_ref_sink (object);
5127 g_object_ref (object);
5128 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5129 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5130 g_object_unref (object);
5133 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
5136 g_object_force_floating (G_OBJECT (object));
5138 g_object_unref (object);
5142 gtk_menu_show_all (GtkWidget *widget)
5144 /* Show children, but not self. */
5145 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5150 gtk_menu_hide_all (GtkWidget *widget)
5152 /* Hide children, but not self. */
5153 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5157 * gtk_menu_set_screen:
5158 * @menu: a #GtkMenu.
5159 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5160 * determined by the widget the menu is attached to.
5162 * Sets the #GdkScreen on which the menu will be displayed.
5167 gtk_menu_set_screen (GtkMenu *menu,
5170 g_return_if_fail (GTK_IS_MENU (menu));
5171 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5173 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5177 menu_change_screen (menu, screen);
5181 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5183 attach_widget_screen_changed (attach_widget, NULL, menu);
5189 * @menu: a #GtkMenu.
5190 * @child: a #GtkMenuItem.
5191 * @left_attach: The column number to attach the left side of the item to.
5192 * @right_attach: The column number to attach the right side of the item to.
5193 * @top_attach: The row number to attach the top of the item to.
5194 * @bottom_attach: The row number to attach the bottom of the item to.
5196 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5197 * an item will occupy is specified by @left_attach, @right_attach,
5198 * @top_attach and @bottom_attach. These each represent the leftmost,
5199 * rightmost, uppermost and lower column and row numbers of the table.
5200 * (Columns and rows are indexed from zero).
5202 * Note that this function is not related to gtk_menu_detach().
5207 gtk_menu_attach (GtkMenu *menu,
5212 guint bottom_attach)
5214 GtkMenuShell *menu_shell;
5217 g_return_if_fail (GTK_IS_MENU (menu));
5218 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5219 parent = gtk_widget_get_parent (child);
5220 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5221 g_return_if_fail (left_attach < right_attach);
5222 g_return_if_fail (top_attach < bottom_attach);
5224 menu_shell = GTK_MENU_SHELL (menu);
5228 AttachInfo *ai = get_attach_info (child);
5230 ai->left_attach = left_attach;
5231 ai->right_attach = right_attach;
5232 ai->top_attach = top_attach;
5233 ai->bottom_attach = bottom_attach;
5235 menu_shell->children = g_list_append (menu_shell->children, child);
5237 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5239 menu_queue_resize (menu);
5243 gtk_container_child_set (GTK_CONTAINER (parent), child,
5244 "left-attach", left_attach,
5245 "right-attach", right_attach,
5246 "top-attach", top_attach,
5247 "bottom-attach", bottom_attach,
5253 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5257 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5258 "gtk-menu-popup-delay", &popup_delay,
5265 find_child_containing (GtkMenuShell *menu_shell,
5273 /* find a child which includes the area given by
5274 * left, right, top, bottom.
5277 for (list = menu_shell->children; list; list = list->next)
5281 if (!_gtk_menu_item_is_selectable (list->data))
5284 get_effective_child_attach (list->data, &l, &r, &t, &b);
5286 if (l <= left && right <= r
5287 && t <= top && bottom <= b)
5288 return GTK_WIDGET (list->data);
5295 gtk_menu_move_current (GtkMenuShell *menu_shell,
5296 GtkMenuDirectionType direction)
5298 GtkMenu *menu = GTK_MENU (menu_shell);
5301 GtkWidget *match = NULL;
5303 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5307 case GTK_MENU_DIR_CHILD:
5308 direction = GTK_MENU_DIR_PARENT;
5310 case GTK_MENU_DIR_PARENT:
5311 direction = GTK_MENU_DIR_CHILD;
5317 /* use special table menu key bindings */
5318 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5320 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5322 if (direction == GTK_MENU_DIR_NEXT)
5324 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5326 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5334 for (i = 0; i < t; i++)
5336 match = find_child_containing (menu_shell,
5337 l, l + 1, i, i + 1);
5343 else if (direction == GTK_MENU_DIR_PREV)
5345 for (i = t; i > 0; i--)
5347 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5355 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5357 match = find_child_containing (menu_shell,
5358 l, l + 1, i - 1, i);
5364 else if (direction == GTK_MENU_DIR_PARENT)
5366 /* we go one left if possible */
5368 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5372 GtkWidget *parent = menu_shell->parent_menu_shell;
5375 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5376 match = menu_shell->active_menu_item;
5379 else if (direction == GTK_MENU_DIR_CHILD)
5381 /* we go one right if possible */
5382 if (r < gtk_menu_get_n_columns (menu))
5383 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5387 GtkWidget *parent = menu_shell->parent_menu_shell;
5389 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5391 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5392 match = menu_shell->active_menu_item;
5398 gtk_menu_shell_select_item (menu_shell, match);
5403 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5407 get_visible_size (GtkMenu *menu)
5409 GtkAllocation allocation;
5410 GtkWidget *widget = GTK_WIDGET (menu);
5411 GtkContainer *container = GTK_CONTAINER (menu);
5414 gtk_widget_get_allocation (widget, &allocation);
5415 menu_height = (allocation.height
5416 - 2 * (gtk_container_get_border_width (container)
5417 + gtk_widget_get_style (widget)->ythickness));
5419 if (!menu->tearoff_active)
5421 GtkBorder arrow_border;
5423 get_arrows_border (menu, &arrow_border);
5424 menu_height -= arrow_border.top;
5425 menu_height -= arrow_border.bottom;
5431 /* Find the sensitive on-screen child containing @y, or if none,
5432 * the nearest selectable onscreen child. (%NULL if none)
5435 child_at (GtkMenu *menu,
5438 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5439 GtkWidget *child = NULL;
5440 gint child_offset = 0;
5443 gint lower, upper; /* Onscreen bounds */
5445 menu_height = get_visible_size (menu);
5446 lower = menu->scroll_offset;
5447 upper = menu->scroll_offset + menu_height;
5449 for (children = menu_shell->children; children; children = children->next)
5451 if (gtk_widget_get_visible (children->data))
5453 GtkRequisition child_requisition;
5455 gtk_widget_size_request (children->data, &child_requisition);
5457 if (_gtk_menu_item_is_selectable (children->data) &&
5458 child_offset >= lower &&
5459 child_offset + child_requisition.height <= upper)
5461 child = children->data;
5463 if (child_offset + child_requisition.height > y &&
5464 !GTK_IS_TEAROFF_MENU_ITEM (child))
5468 child_offset += child_requisition.height;
5476 get_menu_height (GtkMenu *menu)
5478 GtkAllocation allocation;
5479 GtkWidget *widget = GTK_WIDGET (menu);
5482 gtk_widget_get_allocation (widget, &allocation);
5484 height = allocation.height;
5485 height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + gtk_widget_get_style (widget)->ythickness) * 2;
5487 if (!menu->tearoff_active)
5489 GtkBorder arrow_border;
5491 get_arrows_border (menu, &arrow_border);
5492 height -= arrow_border.top;
5493 height -= arrow_border.bottom;
5500 gtk_menu_real_move_scroll (GtkMenu *menu,
5503 gint page_size = get_visible_size (menu);
5504 gint end_position = get_menu_height (menu);
5505 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5509 case GTK_SCROLL_PAGE_UP:
5510 case GTK_SCROLL_PAGE_DOWN:
5514 gint child_offset = 0;
5515 gboolean old_upper_arrow_visible;
5518 if (type == GTK_SCROLL_PAGE_UP)
5523 if (menu_shell->active_menu_item)
5527 compute_child_offset (menu, menu_shell->active_menu_item,
5528 &child_offset, &child_height, NULL);
5529 child_offset += child_height / 2;
5532 menu_shell->ignore_enter = TRUE;
5533 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5534 old_offset = menu->scroll_offset;
5536 new_offset = menu->scroll_offset + step;
5537 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5539 gtk_menu_scroll_to (menu, new_offset);
5541 if (menu_shell->active_menu_item)
5543 GtkWidget *new_child;
5544 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5545 GtkBorder arrow_border;
5547 get_arrows_border (menu, &arrow_border);
5549 if (menu->scroll_offset != old_offset)
5550 step = menu->scroll_offset - old_offset;
5552 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5554 new_child = child_at (menu, child_offset + step);
5556 gtk_menu_shell_select_item (menu_shell, new_child);
5560 case GTK_SCROLL_START:
5561 /* Ignore the enter event we might get if the pointer is on the menu
5563 menu_shell->ignore_enter = TRUE;
5564 gtk_menu_scroll_to (menu, 0);
5565 gtk_menu_shell_select_first (menu_shell, TRUE);
5567 case GTK_SCROLL_END:
5568 /* Ignore the enter event we might get if the pointer is on the menu
5570 menu_shell->ignore_enter = TRUE;
5571 gtk_menu_scroll_to (menu, end_position - page_size);
5572 _gtk_menu_shell_select_last (menu_shell, TRUE);
5581 * gtk_menu_set_monitor:
5583 * @monitor_num: the number of the monitor on which the menu should
5586 * Informs GTK+ on which monitor a menu should be popped up.
5587 * See gdk_screen_get_monitor_geometry().
5589 * This function should be called from a #GtkMenuPositionFunc if the
5590 * menu should not appear on the same monitor as the pointer. This
5591 * information can't be reliably inferred from the coordinates returned
5592 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5593 * may extend beyond the monitor boundaries or even the screen boundaries.
5598 gtk_menu_set_monitor (GtkMenu *menu,
5601 GtkMenuPrivate *priv;
5602 g_return_if_fail (GTK_IS_MENU (menu));
5604 priv = gtk_menu_get_private (menu);
5606 priv->monitor_num = monitor_num;
5610 * gtk_menu_get_monitor:
5613 * Retrieves the number of the monitor on which to show the menu.
5615 * Returns: the number of the monitor on which the menu should
5616 * be popped up or -1, if no monitor has been set
5621 gtk_menu_get_monitor (GtkMenu *menu)
5623 GtkMenuPrivate *priv;
5624 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5626 priv = gtk_menu_get_private (menu);
5628 return priv->monitor_num;
5632 * gtk_menu_get_for_attach_widget:
5633 * @widget: a #GtkWidget
5635 * Returns a list of the menus which are attached to this widget.
5636 * This list is owned by GTK+ and must not be modified.
5638 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5643 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5647 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5649 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5654 gtk_menu_grab_notify (GtkWidget *widget,
5655 gboolean was_grabbed)
5657 GtkWidget *toplevel;
5658 GtkWindowGroup *group;
5662 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5665 !gtk_widget_device_is_shadowed (widget, pointer))
5668 toplevel = gtk_widget_get_toplevel (widget);
5670 if (!GTK_IS_WINDOW (toplevel))
5673 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5674 grab = gtk_window_group_get_current_device_grab (group, pointer);
5676 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5677 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5681 * gtk_menu_set_reserve_toggle_size:
5683 * @reserve_toggle_size: whether to reserve size for toggles
5685 * Sets whether the menu should reserve space for drawing toggles
5686 * or icons, regardless of their actual presence.
5691 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5692 gboolean reserve_toggle_size)
5694 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5695 gboolean no_toggle_size;
5697 no_toggle_size = !reserve_toggle_size;
5699 if (priv->no_toggle_size != no_toggle_size)
5701 priv->no_toggle_size = no_toggle_size;
5703 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5708 * gtk_menu_get_reserve_toggle_size:
5711 * Returns whether the menu reserves space for toggles and
5712 * icons, regardless of their actual presence.
5714 * Returns: Whether the menu reserves toggle space
5719 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5721 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5723 return !priv->no_toggle_size;