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 "gtkprivate.h"
47 #define DEFAULT_POPUP_DELAY 225
48 #define DEFAULT_POPDOWN_DELAY 1000
50 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
51 * extends below the submenu
54 #define MENU_SCROLL_STEP1 8
55 #define MENU_SCROLL_STEP2 15
56 #define MENU_SCROLL_FAST_ZONE 8
57 #define MENU_SCROLL_TIMEOUT1 50
58 #define MENU_SCROLL_TIMEOUT2 20
60 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
61 #define ATTACHED_MENUS "gtk-attached-menus"
63 typedef struct _GtkMenuAttachData GtkMenuAttachData;
64 typedef struct _GtkMenuPrivate GtkMenuPrivate;
65 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
67 struct _GtkMenuAttachData
69 GtkWidget *attach_widget;
70 GtkMenuDetachFunc detacher;
73 struct _GtkMenuPrivate
77 gboolean initially_pushed_in;
79 GDestroyNotify position_func_data_destroy;
81 /* info used for the table */
84 gint requested_height;
88 /* Cached layout information */
97 GtkStateType lower_arrow_state;
98 GtkStateType upper_arrow_state;
100 /* navigation region */
103 int navigation_width;
104 int navigation_height;
106 guint have_layout : 1;
107 guint seen_item_enter : 1;
108 guint have_position : 1;
109 guint ignore_button_release : 1;
110 guint no_toggle_size : 1;
113 struct _GtkMenuPopdownData
125 gint effective_left_attach;
126 gint effective_right_attach;
127 gint effective_top_attach;
128 gint effective_bottom_attach;
145 PROP_RESERVE_TOGGLE_SIZE
150 CHILD_PROP_LEFT_ATTACH,
151 CHILD_PROP_RIGHT_ATTACH,
152 CHILD_PROP_TOP_ATTACH,
153 CHILD_PROP_BOTTOM_ATTACH
156 static void gtk_menu_set_property (GObject *object,
160 static void gtk_menu_get_property (GObject *object,
164 static void gtk_menu_set_child_property(GtkContainer *container,
169 static void gtk_menu_get_child_property(GtkContainer *container,
174 static void gtk_menu_destroy (GtkWidget *widget);
175 static void gtk_menu_realize (GtkWidget *widget);
176 static void gtk_menu_unrealize (GtkWidget *widget);
177 static void gtk_menu_size_allocate (GtkWidget *widget,
178 GtkAllocation *allocation);
179 static void gtk_menu_show (GtkWidget *widget);
180 static gboolean gtk_menu_draw (GtkWidget *widget,
182 static gboolean gtk_menu_key_press (GtkWidget *widget,
184 static gboolean gtk_menu_scroll (GtkWidget *widget,
185 GdkEventScroll *event);
186 static gboolean gtk_menu_button_press (GtkWidget *widget,
187 GdkEventButton *event);
188 static gboolean gtk_menu_button_release (GtkWidget *widget,
189 GdkEventButton *event);
190 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
191 GdkEventMotion *event);
192 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
193 GdkEventCrossing *event);
194 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
195 GdkEventCrossing *event);
196 static void gtk_menu_scroll_to (GtkMenu *menu,
198 static void gtk_menu_grab_notify (GtkWidget *widget,
199 gboolean was_grabbed);
201 static void gtk_menu_stop_scrolling (GtkMenu *menu);
202 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
203 static gboolean gtk_menu_scroll_timeout (gpointer data);
204 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
205 static void gtk_menu_start_scrolling (GtkMenu *menu);
207 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
208 GtkWidget *menu_item);
209 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
210 GtkWidget *menu_item);
211 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
214 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
216 static void gtk_menu_handle_scrolling (GtkMenu *menu,
221 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
223 static void gtk_menu_style_set (GtkWidget *widget,
224 GtkStyle *previous_style);
225 static gboolean gtk_menu_focus (GtkWidget *widget,
226 GtkDirectionType direction);
227 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
228 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
229 GtkMenuDirectionType direction);
230 static void gtk_menu_real_move_scroll (GtkMenu *menu,
233 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
234 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
235 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
238 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
239 GtkMenuItem *menu_item,
240 GdkEventCrossing *event);
242 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
243 static void gtk_menu_show_all (GtkWidget *widget);
244 static void gtk_menu_hide_all (GtkWidget *widget);
245 static void gtk_menu_position (GtkMenu *menu);
246 static void gtk_menu_reparent (GtkMenu *menu,
247 GtkWidget *new_parent,
249 static void gtk_menu_remove (GtkContainer *menu,
252 static void gtk_menu_update_title (GtkMenu *menu);
254 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
255 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
257 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
259 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
260 gboolean group_changed);
262 static void gtk_menu_get_preferred_width (GtkWidget *widget,
265 static void gtk_menu_get_preferred_height (GtkWidget *widget,
268 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
274 static const gchar attach_data_key[] = "gtk-menu-attach-data";
276 static guint menu_signals[LAST_SIGNAL] = { 0 };
278 static GtkMenuPrivate *
279 gtk_menu_get_private (GtkMenu *menu)
281 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
284 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
287 menu_queue_resize (GtkMenu *menu)
289 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
291 priv->have_layout = FALSE;
292 gtk_widget_queue_resize (GTK_WIDGET (menu));
296 attach_info_free (AttachInfo *info)
298 g_slice_free (AttachInfo, info);
302 get_attach_info (GtkWidget *child)
304 GObject *object = G_OBJECT (child);
305 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
309 ai = g_slice_new0 (AttachInfo);
310 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
311 (GDestroyNotify) attach_info_free);
318 is_grid_attached (AttachInfo *ai)
320 return (ai->left_attach >= 0 &&
321 ai->right_attach >= 0 &&
322 ai->top_attach >= 0 &&
323 ai->bottom_attach >= 0);
327 menu_ensure_layout (GtkMenu *menu)
329 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
331 if (!priv->have_layout)
333 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
337 gint max_right_attach;
338 gint max_bottom_attach;
340 /* Find extents of gridded portion
342 max_right_attach = 1;
343 max_bottom_attach = 0;
345 for (l = menu_shell->children; l; l = l->next)
347 GtkWidget *child = l->data;
348 AttachInfo *ai = get_attach_info (child);
350 if (is_grid_attached (ai))
352 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
353 max_right_attach = MAX (max_right_attach, ai->right_attach);
359 row_occupied = g_malloc0 (max_bottom_attach);
361 for (l = menu_shell->children; l; l = l->next)
363 GtkWidget *child = l->data;
364 AttachInfo *ai = get_attach_info (child);
366 if (is_grid_attached (ai))
370 for (i = ai->top_attach; i < ai->bottom_attach; i++)
371 row_occupied[i] = TRUE;
375 /* Lay non-grid-items out in those rows
378 for (l = menu_shell->children; l; l = l->next)
380 GtkWidget *child = l->data;
381 AttachInfo *ai = get_attach_info (child);
383 if (!is_grid_attached (ai))
385 while (current_row < max_bottom_attach && row_occupied[current_row])
388 ai->effective_left_attach = 0;
389 ai->effective_right_attach = max_right_attach;
390 ai->effective_top_attach = current_row;
391 ai->effective_bottom_attach = current_row + 1;
397 ai->effective_left_attach = ai->left_attach;
398 ai->effective_right_attach = ai->right_attach;
399 ai->effective_top_attach = ai->top_attach;
400 ai->effective_bottom_attach = ai->bottom_attach;
404 g_free (row_occupied);
406 priv->n_rows = MAX (current_row, max_bottom_attach);
407 priv->n_columns = max_right_attach;
408 priv->have_layout = TRUE;
414 gtk_menu_get_n_columns (GtkMenu *menu)
416 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
418 menu_ensure_layout (menu);
420 return priv->n_columns;
424 gtk_menu_get_n_rows (GtkMenu *menu)
426 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
428 menu_ensure_layout (menu);
434 get_effective_child_attach (GtkWidget *child,
440 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
443 menu_ensure_layout (menu);
445 ai = get_attach_info (child);
448 *l = ai->effective_left_attach;
450 *r = ai->effective_right_attach;
452 *t = ai->effective_top_attach;
454 *b = ai->effective_bottom_attach;
459 gtk_menu_class_init (GtkMenuClass *class)
461 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
462 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
463 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
464 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
465 GtkBindingSet *binding_set;
467 gobject_class->set_property = gtk_menu_set_property;
468 gobject_class->get_property = gtk_menu_get_property;
470 widget_class->destroy = gtk_menu_destroy;
471 widget_class->realize = gtk_menu_realize;
472 widget_class->unrealize = gtk_menu_unrealize;
473 widget_class->size_allocate = gtk_menu_size_allocate;
474 widget_class->show = gtk_menu_show;
475 widget_class->draw = gtk_menu_draw;
476 widget_class->scroll_event = gtk_menu_scroll;
477 widget_class->key_press_event = gtk_menu_key_press;
478 widget_class->button_press_event = gtk_menu_button_press;
479 widget_class->button_release_event = gtk_menu_button_release;
480 widget_class->motion_notify_event = gtk_menu_motion_notify;
481 widget_class->show_all = gtk_menu_show_all;
482 widget_class->hide_all = gtk_menu_hide_all;
483 widget_class->enter_notify_event = gtk_menu_enter_notify;
484 widget_class->leave_notify_event = gtk_menu_leave_notify;
485 widget_class->style_set = gtk_menu_style_set;
486 widget_class->focus = gtk_menu_focus;
487 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
488 widget_class->grab_notify = gtk_menu_grab_notify;
489 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
490 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
491 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
493 container_class->remove = gtk_menu_remove;
494 container_class->get_child_property = gtk_menu_get_child_property;
495 container_class->set_child_property = gtk_menu_set_child_property;
497 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
498 menu_shell_class->deactivate = gtk_menu_deactivate;
499 menu_shell_class->select_item = gtk_menu_select_item;
500 menu_shell_class->insert = gtk_menu_real_insert;
501 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
502 menu_shell_class->move_current = gtk_menu_move_current;
504 menu_signals[MOVE_SCROLL] =
505 g_signal_new_class_handler (I_("move-scroll"),
506 G_OBJECT_CLASS_TYPE (gobject_class),
507 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
508 G_CALLBACK (gtk_menu_real_move_scroll),
510 _gtk_marshal_VOID__ENUM,
512 GTK_TYPE_SCROLL_TYPE);
517 * The index of the currently selected menu item, or -1 if no
518 * menu item is selected.
522 g_object_class_install_property (gobject_class,
524 g_param_spec_int ("active",
526 P_("The currently selected menu item"),
528 GTK_PARAM_READWRITE));
531 * GtkMenu:accel-group:
533 * The accel group holding accelerators for the menu.
537 g_object_class_install_property (gobject_class,
539 g_param_spec_object ("accel-group",
541 P_("The accel group holding accelerators for the menu"),
542 GTK_TYPE_ACCEL_GROUP,
543 GTK_PARAM_READWRITE));
546 * GtkMenu:accel-path:
548 * An accel path used to conveniently construct accel paths of child items.
552 g_object_class_install_property (gobject_class,
554 g_param_spec_string ("accel-path",
556 P_("An accel path used to conveniently construct accel paths of child items"),
558 GTK_PARAM_READWRITE));
561 * GtkMenu:attach-widget:
563 * The widget the menu is attached to. Setting this property attaches
564 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
565 * use gtk_menu_attach_to_widget() directly.
569 g_object_class_install_property (gobject_class,
571 g_param_spec_object ("attach-widget",
573 P_("The widget the menu is attached to"),
575 GTK_PARAM_READWRITE));
577 g_object_class_install_property (gobject_class,
579 g_param_spec_string ("tearoff-title",
581 P_("A title that may be displayed by the window manager when this menu is torn-off"),
583 GTK_PARAM_READWRITE));
586 * GtkMenu:tearoff-state:
588 * A boolean that indicates whether the menu is torn-off.
592 g_object_class_install_property (gobject_class,
594 g_param_spec_boolean ("tearoff-state",
596 P_("A boolean that indicates whether the menu is torn-off"),
598 GTK_PARAM_READWRITE));
603 * The monitor the menu will be popped up on.
607 g_object_class_install_property (gobject_class,
609 g_param_spec_int ("monitor",
611 P_("The monitor the menu will be popped up on"),
613 GTK_PARAM_READWRITE));
615 gtk_widget_class_install_style_property (widget_class,
616 g_param_spec_int ("vertical-padding",
617 P_("Vertical Padding"),
618 P_("Extra space at the top and bottom of the menu"),
622 GTK_PARAM_READABLE));
625 * GtkMenu:reserve-toggle-size:
627 * A boolean that indicates whether the menu reserves space for
628 * toggles and icons, regardless of their actual presence.
630 * This property should only be changed from its default value
631 * for special-purposes such as tabular menus. Regular menus that
632 * are connected to a menu bar or context menus should reserve
633 * toggle space for consistency.
637 g_object_class_install_property (gobject_class,
638 PROP_RESERVE_TOGGLE_SIZE,
639 g_param_spec_boolean ("reserve-toggle-size",
640 P_("Reserve Toggle Size"),
641 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
643 GTK_PARAM_READWRITE));
645 gtk_widget_class_install_style_property (widget_class,
646 g_param_spec_int ("horizontal-padding",
647 P_("Horizontal Padding"),
648 P_("Extra space at the left and right edges of the menu"),
652 GTK_PARAM_READABLE));
654 gtk_widget_class_install_style_property (widget_class,
655 g_param_spec_int ("vertical-offset",
656 P_("Vertical Offset"),
657 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
661 GTK_PARAM_READABLE));
663 gtk_widget_class_install_style_property (widget_class,
664 g_param_spec_int ("horizontal-offset",
665 P_("Horizontal Offset"),
666 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
670 GTK_PARAM_READABLE));
672 gtk_widget_class_install_style_property (widget_class,
673 g_param_spec_boolean ("double-arrows",
675 P_("When scrolling, always show both arrows."),
677 GTK_PARAM_READABLE));
680 * GtkMenu:arrow-placement:
682 * Indicates where scroll arrows should be placed.
686 gtk_widget_class_install_style_property (widget_class,
687 g_param_spec_enum ("arrow-placement",
688 P_("Arrow Placement"),
689 P_("Indicates where scroll arrows should be placed"),
690 GTK_TYPE_ARROW_PLACEMENT,
692 GTK_PARAM_READABLE));
694 gtk_container_class_install_child_property (container_class,
695 CHILD_PROP_LEFT_ATTACH,
696 g_param_spec_int ("left-attach",
698 P_("The column number to attach the left side of the child to"),
700 GTK_PARAM_READWRITE));
702 gtk_container_class_install_child_property (container_class,
703 CHILD_PROP_RIGHT_ATTACH,
704 g_param_spec_int ("right-attach",
706 P_("The column number to attach the right side of the child to"),
708 GTK_PARAM_READWRITE));
710 gtk_container_class_install_child_property (container_class,
711 CHILD_PROP_TOP_ATTACH,
712 g_param_spec_int ("top-attach",
714 P_("The row number to attach the top of the child to"),
716 GTK_PARAM_READWRITE));
718 gtk_container_class_install_child_property (container_class,
719 CHILD_PROP_BOTTOM_ATTACH,
720 g_param_spec_int ("bottom-attach",
722 P_("The row number to attach the bottom of the child to"),
724 GTK_PARAM_READWRITE));
727 * GtkMenu::arrow-scaling
729 * Arbitrary constant to scale down the size of the scroll arrow.
733 gtk_widget_class_install_style_property (widget_class,
734 g_param_spec_float ("arrow-scaling",
736 P_("Arbitrary constant to scale down the size of the scroll arrow"),
738 GTK_PARAM_READABLE));
740 binding_set = gtk_binding_set_by_class (class);
741 gtk_binding_entry_add_signal (binding_set,
743 I_("move-current"), 1,
744 GTK_TYPE_MENU_DIRECTION_TYPE,
746 gtk_binding_entry_add_signal (binding_set,
749 GTK_TYPE_MENU_DIRECTION_TYPE,
751 gtk_binding_entry_add_signal (binding_set,
754 GTK_TYPE_MENU_DIRECTION_TYPE,
756 gtk_binding_entry_add_signal (binding_set,
759 GTK_TYPE_MENU_DIRECTION_TYPE,
761 gtk_binding_entry_add_signal (binding_set,
764 GTK_TYPE_MENU_DIRECTION_TYPE,
765 GTK_MENU_DIR_PARENT);
766 gtk_binding_entry_add_signal (binding_set,
769 GTK_TYPE_MENU_DIRECTION_TYPE,
770 GTK_MENU_DIR_PARENT);
771 gtk_binding_entry_add_signal (binding_set,
774 GTK_TYPE_MENU_DIRECTION_TYPE,
776 gtk_binding_entry_add_signal (binding_set,
779 GTK_TYPE_MENU_DIRECTION_TYPE,
781 gtk_binding_entry_add_signal (binding_set,
784 GTK_TYPE_SCROLL_TYPE,
786 gtk_binding_entry_add_signal (binding_set,
789 GTK_TYPE_SCROLL_TYPE,
791 gtk_binding_entry_add_signal (binding_set,
794 GTK_TYPE_SCROLL_TYPE,
796 gtk_binding_entry_add_signal (binding_set,
799 GTK_TYPE_SCROLL_TYPE,
801 gtk_binding_entry_add_signal (binding_set,
804 GTK_TYPE_SCROLL_TYPE,
806 gtk_binding_entry_add_signal (binding_set,
807 GDK_KEY_KP_Page_Up, 0,
809 GTK_TYPE_SCROLL_TYPE,
811 gtk_binding_entry_add_signal (binding_set,
812 GDK_KEY_Page_Down, 0,
814 GTK_TYPE_SCROLL_TYPE,
815 GTK_SCROLL_PAGE_DOWN);
816 gtk_binding_entry_add_signal (binding_set,
817 GDK_KEY_KP_Page_Down, 0,
819 GTK_TYPE_SCROLL_TYPE,
820 GTK_SCROLL_PAGE_DOWN);
822 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
823 P_("Can change accelerators"),
824 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
826 GTK_PARAM_READWRITE));
828 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
829 P_("Delay before submenus appear"),
830 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
834 GTK_PARAM_READWRITE));
836 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
837 P_("Delay before hiding a submenu"),
838 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
841 DEFAULT_POPDOWN_DELAY,
842 GTK_PARAM_READWRITE));
844 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
849 gtk_menu_set_property (GObject *object,
854 GtkMenu *menu = GTK_MENU (object);
859 gtk_menu_set_active (menu, g_value_get_int (value));
861 case PROP_ACCEL_GROUP:
862 gtk_menu_set_accel_group (menu, g_value_get_object (value));
864 case PROP_ACCEL_PATH:
865 gtk_menu_set_accel_path (menu, g_value_get_string (value));
867 case PROP_ATTACH_WIDGET:
871 widget = gtk_menu_get_attach_widget (menu);
873 gtk_menu_detach (menu);
875 widget = (GtkWidget*) g_value_get_object (value);
877 gtk_menu_attach_to_widget (menu, widget, NULL);
880 case PROP_TEAROFF_STATE:
881 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
883 case PROP_TEAROFF_TITLE:
884 gtk_menu_set_title (menu, g_value_get_string (value));
887 gtk_menu_set_monitor (menu, g_value_get_int (value));
889 case PROP_RESERVE_TOGGLE_SIZE:
890 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
893 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
899 gtk_menu_get_property (GObject *object,
904 GtkMenu *menu = GTK_MENU (object);
909 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
911 case PROP_ACCEL_GROUP:
912 g_value_set_object (value, gtk_menu_get_accel_group (menu));
914 case PROP_ACCEL_PATH:
915 g_value_set_string (value, gtk_menu_get_accel_path (menu));
917 case PROP_ATTACH_WIDGET:
918 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
920 case PROP_TEAROFF_STATE:
921 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
923 case PROP_TEAROFF_TITLE:
924 g_value_set_string (value, gtk_menu_get_title (menu));
927 g_value_set_int (value, gtk_menu_get_monitor (menu));
929 case PROP_RESERVE_TOGGLE_SIZE:
930 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
933 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
939 gtk_menu_set_child_property (GtkContainer *container,
945 GtkMenu *menu = GTK_MENU (container);
946 AttachInfo *ai = get_attach_info (child);
950 case CHILD_PROP_LEFT_ATTACH:
951 ai->left_attach = g_value_get_int (value);
953 case CHILD_PROP_RIGHT_ATTACH:
954 ai->right_attach = g_value_get_int (value);
956 case CHILD_PROP_TOP_ATTACH:
957 ai->top_attach = g_value_get_int (value);
959 case CHILD_PROP_BOTTOM_ATTACH:
960 ai->bottom_attach = g_value_get_int (value);
964 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
968 menu_queue_resize (menu);
972 gtk_menu_get_child_property (GtkContainer *container,
978 AttachInfo *ai = get_attach_info (child);
982 case CHILD_PROP_LEFT_ATTACH:
983 g_value_set_int (value, ai->left_attach);
985 case CHILD_PROP_RIGHT_ATTACH:
986 g_value_set_int (value, ai->right_attach);
988 case CHILD_PROP_TOP_ATTACH:
989 g_value_set_int (value, ai->top_attach);
991 case CHILD_PROP_BOTTOM_ATTACH:
992 g_value_set_int (value, ai->bottom_attach);
996 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1002 gtk_menu_window_event (GtkWidget *window,
1006 gboolean handled = FALSE;
1008 g_object_ref (window);
1009 g_object_ref (menu);
1011 switch (event->type)
1014 case GDK_KEY_RELEASE:
1015 handled = gtk_widget_event (menu, event);
1021 g_object_unref (window);
1022 g_object_unref (menu);
1028 gtk_menu_init (GtkMenu *menu)
1030 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1032 menu->parent_menu_item = NULL;
1033 menu->old_active_menu_item = NULL;
1034 menu->accel_group = NULL;
1035 menu->position_func = NULL;
1036 menu->position_func_data = NULL;
1037 menu->toggle_size = 0;
1039 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1040 "type", GTK_WINDOW_POPUP,
1043 "signal::event", gtk_menu_window_event, menu,
1044 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1046 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1047 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1049 /* Refloat the menu, so that reference counting for the menu isn't
1050 * affected by it being a child of the toplevel
1052 g_object_force_floating (G_OBJECT (menu));
1053 menu->needs_destruction_ref_count = TRUE;
1055 menu->view_window = NULL;
1056 menu->bin_window = NULL;
1058 menu->scroll_offset = 0;
1059 menu->scroll_step = 0;
1060 menu->timeout_id = 0;
1061 menu->scroll_fast = FALSE;
1063 menu->tearoff_window = NULL;
1064 menu->tearoff_hbox = NULL;
1065 menu->torn_off = FALSE;
1066 menu->tearoff_active = FALSE;
1067 menu->tearoff_adjustment = NULL;
1068 menu->tearoff_scrollbar = NULL;
1070 menu->upper_arrow_visible = FALSE;
1071 menu->lower_arrow_visible = FALSE;
1072 menu->upper_arrow_prelight = FALSE;
1073 menu->lower_arrow_prelight = FALSE;
1075 priv->upper_arrow_state = GTK_STATE_NORMAL;
1076 priv->lower_arrow_state = GTK_STATE_NORMAL;
1078 priv->have_layout = FALSE;
1079 priv->monitor_num = -1;
1083 gtk_menu_destroy (GtkWidget *widget)
1085 GtkMenu *menu = GTK_MENU (widget);
1086 GtkMenuAttachData *data;
1087 GtkMenuPrivate *priv;
1089 gtk_menu_remove_scroll_timeout (menu);
1091 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1093 gtk_menu_detach (menu);
1095 gtk_menu_stop_navigating_submenu (menu);
1097 if (menu->old_active_menu_item)
1099 g_object_unref (menu->old_active_menu_item);
1100 menu->old_active_menu_item = NULL;
1103 /* Add back the reference count for being a child */
1104 if (menu->needs_destruction_ref_count)
1106 menu->needs_destruction_ref_count = FALSE;
1107 g_object_ref (widget);
1110 if (menu->accel_group)
1112 g_object_unref (menu->accel_group);
1113 menu->accel_group = NULL;
1117 gtk_widget_destroy (menu->toplevel);
1119 if (menu->tearoff_window)
1120 gtk_widget_destroy (menu->tearoff_window);
1122 priv = gtk_menu_get_private (menu);
1126 g_free (priv->heights);
1127 priv->heights = NULL;
1132 g_free (priv->title);
1136 if (priv->position_func_data_destroy)
1138 priv->position_func_data_destroy (menu->position_func_data);
1139 menu->position_func_data = NULL;
1140 priv->position_func_data_destroy = NULL;
1143 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1147 menu_change_screen (GtkMenu *menu,
1148 GdkScreen *new_screen)
1150 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1152 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1154 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1160 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1161 gtk_menu_position (menu);
1164 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1165 private->monitor_num = -1;
1169 attach_widget_screen_changed (GtkWidget *attach_widget,
1170 GdkScreen *previous_screen,
1173 if (gtk_widget_has_screen (attach_widget) &&
1174 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1176 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1181 gtk_menu_attach_to_widget (GtkMenu *menu,
1182 GtkWidget *attach_widget,
1183 GtkMenuDetachFunc detacher)
1185 GtkMenuAttachData *data;
1188 g_return_if_fail (GTK_IS_MENU (menu));
1189 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1191 /* keep this function in sync with gtk_widget_set_parent()
1194 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1197 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1198 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1202 g_object_ref_sink (menu);
1204 data = g_slice_new (GtkMenuAttachData);
1205 data->attach_widget = attach_widget;
1207 g_signal_connect (attach_widget, "screen-changed",
1208 G_CALLBACK (attach_widget_screen_changed), menu);
1209 attach_widget_screen_changed (attach_widget, NULL, menu);
1211 data->detacher = detacher;
1212 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1213 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1214 if (!g_list_find (list, menu))
1216 list = g_list_prepend (list, menu);
1218 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1219 (GDestroyNotify) g_list_free);
1221 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1222 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1224 /* we don't need to set the style here, since
1225 * we are a toplevel widget.
1228 /* Fallback title for menu comes from attach widget */
1229 gtk_menu_update_title (menu);
1231 g_object_notify (G_OBJECT (menu), "attach-widget");
1235 gtk_menu_get_attach_widget (GtkMenu *menu)
1237 GtkMenuAttachData *data;
1239 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1241 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1243 return data->attach_widget;
1248 gtk_menu_detach (GtkMenu *menu)
1250 GtkMenuAttachData *data;
1253 g_return_if_fail (GTK_IS_MENU (menu));
1255 /* keep this function in sync with gtk_widget_unparent()
1257 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1260 g_warning ("gtk_menu_detach(): menu is not attached");
1263 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1265 g_signal_handlers_disconnect_by_func (data->attach_widget,
1266 (gpointer) attach_widget_screen_changed,
1270 data->detacher (data->attach_widget, menu);
1271 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1272 list = g_list_remove (list, menu);
1274 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1275 (GDestroyNotify) g_list_free);
1277 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1279 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1280 gtk_widget_unrealize (GTK_WIDGET (menu));
1282 g_slice_free (GtkMenuAttachData, data);
1284 /* Fallback title for menu comes from attach widget */
1285 gtk_menu_update_title (menu);
1287 g_object_unref (menu);
1291 gtk_menu_remove (GtkContainer *container,
1294 GtkMenu *menu = GTK_MENU (container);
1296 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1298 /* Clear out old_active_menu_item if it matches the item we are removing
1300 if (menu->old_active_menu_item == widget)
1302 g_object_unref (menu->old_active_menu_item);
1303 menu->old_active_menu_item = NULL;
1306 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1307 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1309 menu_queue_resize (menu);
1315 return g_object_new (GTK_TYPE_MENU, NULL);
1319 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1323 GtkMenu *menu = GTK_MENU (menu_shell);
1324 AttachInfo *ai = get_attach_info (child);
1326 ai->left_attach = -1;
1327 ai->right_attach = -1;
1328 ai->top_attach = -1;
1329 ai->bottom_attach = -1;
1331 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1332 gtk_widget_set_parent_window (child, menu->bin_window);
1334 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1336 menu_queue_resize (menu);
1340 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1345 widget = GTK_WIDGET (menu);
1350 cairo_surface_t *surface;
1351 cairo_pattern_t *pattern;
1354 menu->tearoff_active = FALSE;
1355 menu->saved_scroll_offset = menu->scroll_offset;
1357 window = gtk_widget_get_window (menu->tearoff_window);
1358 width = gdk_window_get_width (window);
1359 height = gdk_window_get_height (window);
1361 surface = gdk_window_create_similar_surface (window,
1362 CAIRO_CONTENT_COLOR,
1366 cr = cairo_create (surface);
1367 gdk_cairo_set_source_window (cr,
1373 gtk_widget_set_size_request (menu->tearoff_window,
1377 pattern = cairo_pattern_create_for_surface (surface);
1378 gdk_window_set_background_pattern (window, pattern);
1380 cairo_pattern_destroy (pattern);
1381 cairo_surface_destroy (surface);
1386 popup_grab_on_window (GdkWindow *window,
1387 GdkDevice *keyboard,
1389 guint32 activate_time)
1392 gdk_device_grab (keyboard, window,
1393 GDK_OWNERSHIP_WINDOW, TRUE,
1394 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1395 NULL, activate_time) != GDK_GRAB_SUCCESS)
1399 gdk_device_grab (pointer, window,
1400 GDK_OWNERSHIP_WINDOW, TRUE,
1401 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1402 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1403 GDK_POINTER_MOTION_MASK,
1404 NULL, activate_time) != GDK_GRAB_SUCCESS)
1407 gdk_device_ungrab (keyboard, activate_time);
1416 * gtk_menu_popup_for_device:
1417 * @menu: a #GtkMenu.
1418 * @device: (allow-none): a #GdkDevice
1419 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1420 * menu item, or %NULL
1421 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1422 * the popup, or %NULL
1423 * @func: (allow-none): a user supplied function used to position the menu,
1425 * @data: (allow-none): user supplied data to be passed to @func
1426 * @destroy: (allow-none): destroy notify for @data
1427 * @button: the mouse button which was pressed to initiate the event
1428 * @activate_time: the time at which the activation event occurred
1430 * Displays a menu and makes it available for selection.
1432 * Applications can use this function to display context-sensitive menus,
1433 * and will typically supply %NULL for the @parent_menu_shell,
1434 * @parent_menu_item, @func, @data and @destroy parameters. The default
1435 * menu positioning function will position the menu at the current position
1436 * of @device (or its corresponding pointer).
1438 * The @button parameter should be the mouse button pressed to initiate
1439 * the menu popup. If the menu popup was initiated by something other than
1440 * a mouse button press, such as a mouse button release or a keypress,
1441 * @button should be 0.
1443 * The @activate_time parameter is used to conflict-resolve initiation of
1444 * concurrent requests for mouse/keyboard grab requests. To function
1445 * properly, this needs to be the time stamp of the user event (such as
1446 * a mouse click or key press) that caused the initiation of the popup.
1447 * Only if no such event is available, gtk_get_current_event_time() can
1453 gtk_menu_popup_for_device (GtkMenu *menu,
1455 GtkWidget *parent_menu_shell,
1456 GtkWidget *parent_menu_item,
1457 GtkMenuPositionFunc func,
1459 GDestroyNotify destroy,
1461 guint32 activate_time)
1464 GtkWidget *xgrab_shell;
1466 GdkEvent *current_event;
1467 GtkMenuShell *menu_shell;
1468 gboolean grab_keyboard;
1469 GtkMenuPrivate *priv;
1470 GtkWidget *parent_toplevel;
1471 GdkDevice *keyboard, *pointer;
1473 g_return_if_fail (GTK_IS_MENU (menu));
1474 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1477 device = gtk_get_current_event_device ();
1481 GdkDisplay *display;
1482 GdkDeviceManager *device_manager;
1485 display = gtk_widget_get_display (GTK_WIDGET (menu));
1486 device_manager = gdk_display_get_device_manager (display);
1487 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1489 device = devices->data;
1491 g_list_free (devices);
1494 widget = GTK_WIDGET (menu);
1495 menu_shell = GTK_MENU_SHELL (menu);
1496 priv = gtk_menu_get_private (menu);
1498 if (device->source == GDK_SOURCE_KEYBOARD)
1501 pointer = gdk_device_get_associated_device (device);
1506 keyboard = gdk_device_get_associated_device (device);
1509 menu_shell->parent_menu_shell = parent_menu_shell;
1511 priv->seen_item_enter = FALSE;
1513 /* Find the last viewable ancestor, and make an X grab on it
1515 parent = GTK_WIDGET (menu);
1519 gboolean viewable = TRUE;
1520 GtkWidget *tmp = parent;
1524 if (!gtk_widget_get_mapped (tmp))
1529 tmp = gtk_widget_get_parent (tmp);
1533 xgrab_shell = parent;
1535 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1538 /* We want to receive events generated when we map the menu; unfortunately,
1539 * since there is probably already an implicit grab in place from the
1540 * button that the user used to pop up the menu, we won't receive then --
1541 * in particular, the EnterNotify when the menu pops up under the pointer.
1543 * If we are grabbing on a parent menu shell, no problem; just grab on
1544 * that menu shell first before popping up the window with owner_events = TRUE.
1546 * When grabbing on the menu itself, things get more convuluted - we
1547 * we do an explicit grab on a specially created window with
1548 * owner_events = TRUE, which we override further down with a grab
1549 * on the menu. (We can't grab on the menu until it is mapped; we
1550 * probably could just leave the grab on the other window, with a
1551 * little reorganization of the code in gtkmenu*).
1553 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1554 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1559 if (xgrab_shell && xgrab_shell != widget)
1561 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1563 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1564 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1569 GdkWindow *transfer_window;
1571 xgrab_shell = widget;
1572 transfer_window = menu_grab_transfer_window_get (menu);
1573 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1575 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1576 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1580 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1582 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1583 * with a stuck up window, we just abort here. Presumably the user will
1586 menu_shell->parent_menu_shell = NULL;
1587 menu_grab_transfer_window_destroy (menu);
1591 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1592 menu_shell->active = TRUE;
1593 menu_shell->button = button;
1595 /* If we are popping up the menu from something other than, a button
1596 * press then, as a heuristic, we ignore enter events for the menu
1597 * until we get a MOTION_NOTIFY.
1600 current_event = gtk_get_current_event ();
1603 if ((current_event->type != GDK_BUTTON_PRESS) &&
1604 (current_event->type != GDK_ENTER_NOTIFY))
1605 menu_shell->ignore_enter = TRUE;
1607 gdk_event_free (current_event);
1610 menu_shell->ignore_enter = TRUE;
1614 gtk_menu_tearoff_bg_copy (menu);
1616 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1619 parent_toplevel = NULL;
1620 if (parent_menu_shell)
1621 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1622 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1624 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1626 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1629 /* Set transient for to get the right window group and parent relationship */
1630 if (GTK_IS_WINDOW (parent_toplevel))
1631 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1632 GTK_WINDOW (parent_toplevel));
1634 menu->parent_menu_item = parent_menu_item;
1635 menu->position_func = func;
1636 menu->position_func_data = data;
1637 priv->position_func_data_destroy = destroy;
1638 menu_shell->activate_time = activate_time;
1640 /* We need to show the menu here rather in the init function because
1641 * code expects to be able to tell if the menu is onscreen by
1642 * looking at the gtk_widget_get_visible (menu)
1644 gtk_widget_show (GTK_WIDGET (menu));
1646 /* Position the menu, possibly changing the size request
1648 gtk_menu_position (menu);
1650 /* Compute the size of the toplevel and realize it so we
1651 * can scroll correctly.
1654 GtkRequisition tmp_request;
1655 GtkAllocation tmp_allocation = { 0, };
1657 /* Instead of trusting the menu position function to queue a resize when the
1658 * menu goes out of bounds, invalidate the cached size here. */
1659 gtk_widget_queue_resize (GTK_WIDGET (menu));
1660 gtk_widget_get_preferred_size (menu->toplevel, &tmp_request, NULL);
1662 tmp_allocation.width = tmp_request.width;
1663 tmp_allocation.height = tmp_request.height;
1665 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1667 gtk_widget_realize (GTK_WIDGET (menu));
1670 gtk_menu_scroll_to (menu, menu->scroll_offset);
1672 /* if no item is selected, select the first one */
1673 if (!menu_shell->active_menu_item)
1675 gboolean touchscreen_mode;
1677 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1678 "gtk-touchscreen-mode", &touchscreen_mode,
1681 if (touchscreen_mode)
1682 gtk_menu_shell_select_first (menu_shell, TRUE);
1685 /* Once everything is set up correctly, map the toplevel window on
1688 gtk_widget_show (menu->toplevel);
1690 if (xgrab_shell == widget)
1691 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1693 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1695 if (parent_menu_shell)
1697 gboolean keyboard_mode;
1699 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1700 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1702 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1703 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1705 _gtk_menu_shell_update_mnemonics (menu_shell);
1710 * @menu: a #GtkMenu.
1711 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1712 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1713 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1714 * @data: (allow-none): user supplied data to be passed to @func.
1715 * @button: the mouse button which was pressed to initiate the event.
1716 * @activate_time: the time at which the activation event occurred.
1718 * Displays a menu and makes it available for selection. Applications can use
1719 * this function to display context-sensitive menus, and will typically supply
1720 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1721 * parameters. The default menu positioning function will position the menu
1722 * at the current mouse cursor position.
1724 * The @button parameter should be the mouse button pressed to initiate
1725 * the menu popup. If the menu popup was initiated by something other than
1726 * a mouse button press, such as a mouse button release or a keypress,
1727 * @button should be 0.
1729 * The @activate_time parameter is used to conflict-resolve initiation of
1730 * concurrent requests for mouse/keyboard grab requests. To function
1731 * properly, this needs to be the time stamp of the user event (such as
1732 * a mouse click or key press) that caused the initiation of the popup.
1733 * Only if no such event is available, gtk_get_current_event_time() can
1737 gtk_menu_popup (GtkMenu *menu,
1738 GtkWidget *parent_menu_shell,
1739 GtkWidget *parent_menu_item,
1740 GtkMenuPositionFunc func,
1743 guint32 activate_time)
1745 g_return_if_fail (GTK_IS_MENU (menu));
1747 gtk_menu_popup_for_device (menu,
1752 button, activate_time);
1756 gtk_menu_popdown (GtkMenu *menu)
1758 GtkMenuPrivate *private;
1759 GtkMenuShell *menu_shell;
1762 g_return_if_fail (GTK_IS_MENU (menu));
1764 menu_shell = GTK_MENU_SHELL (menu);
1765 private = gtk_menu_get_private (menu);
1767 menu_shell->parent_menu_shell = NULL;
1768 menu_shell->active = FALSE;
1769 menu_shell->ignore_enter = FALSE;
1771 private->have_position = FALSE;
1773 gtk_menu_stop_scrolling (menu);
1775 gtk_menu_stop_navigating_submenu (menu);
1777 if (menu_shell->active_menu_item)
1779 if (menu->old_active_menu_item)
1780 g_object_unref (menu->old_active_menu_item);
1781 menu->old_active_menu_item = menu_shell->active_menu_item;
1782 g_object_ref (menu->old_active_menu_item);
1785 gtk_menu_shell_deselect (menu_shell);
1787 /* The X Grab, if present, will automatically be removed when we hide
1789 gtk_widget_hide (menu->toplevel);
1790 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1792 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1796 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1798 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1800 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1804 /* We popped up the menu from the tearoff, so we need to
1805 * release the grab - we aren't actually hiding the menu.
1807 if (menu_shell->have_xgrab && pointer)
1809 GdkDevice *keyboard;
1811 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1812 keyboard = gdk_device_get_associated_device (pointer);
1815 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1819 /* gtk_menu_popdown is called each time a menu item is selected from
1820 * a torn off menu. Only scroll back to the saved position if the
1821 * non-tearoff menu was popped down.
1823 if (!menu->tearoff_active)
1824 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1825 menu->tearoff_active = TRUE;
1828 gtk_widget_hide (GTK_WIDGET (menu));
1830 menu_shell->have_xgrab = FALSE;
1833 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1835 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1837 menu_grab_transfer_window_destroy (menu);
1841 gtk_menu_get_active (GtkMenu *menu)
1846 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1848 if (!menu->old_active_menu_item)
1851 children = GTK_MENU_SHELL (menu)->children;
1855 child = children->data;
1856 children = children->next;
1858 if (gtk_bin_get_child (GTK_BIN (child)))
1863 menu->old_active_menu_item = child;
1864 if (menu->old_active_menu_item)
1865 g_object_ref (menu->old_active_menu_item);
1868 return menu->old_active_menu_item;
1872 gtk_menu_set_active (GtkMenu *menu,
1878 g_return_if_fail (GTK_IS_MENU (menu));
1880 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1883 child = tmp_list->data;
1884 if (gtk_bin_get_child (GTK_BIN (child)))
1886 if (menu->old_active_menu_item)
1887 g_object_unref (menu->old_active_menu_item);
1888 menu->old_active_menu_item = child;
1889 g_object_ref (menu->old_active_menu_item);
1896 * gtk_menu_set_accel_group:
1897 * @accel_group: (allow-none):
1900 gtk_menu_set_accel_group (GtkMenu *menu,
1901 GtkAccelGroup *accel_group)
1903 g_return_if_fail (GTK_IS_MENU (menu));
1905 if (menu->accel_group != accel_group)
1907 if (menu->accel_group)
1908 g_object_unref (menu->accel_group);
1909 menu->accel_group = accel_group;
1910 if (menu->accel_group)
1911 g_object_ref (menu->accel_group);
1912 _gtk_menu_refresh_accel_paths (menu, TRUE);
1917 gtk_menu_get_accel_group (GtkMenu *menu)
1919 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1921 return menu->accel_group;
1925 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1928 /* Menu items chain here to figure whether they can activate their
1929 * accelerators. Unlike ordinary widgets, menus allow accel
1930 * activation even if invisible since that's the usual case for
1931 * submenus/popup-menus. however, the state of the attach widget
1932 * affects the "activeness" of the menu.
1934 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1937 return gtk_widget_can_activate_accel (awidget, signal_id);
1939 return gtk_widget_is_sensitive (widget);
1943 * gtk_menu_set_accel_path
1944 * @menu: a valid #GtkMenu
1945 * @accel_path: (allow-none): a valid accelerator path
1947 * Sets an accelerator path for this menu from which accelerator paths
1948 * for its immediate children, its menu items, can be constructed.
1949 * The main purpose of this function is to spare the programmer the
1950 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1951 * each menu item that should support runtime user changable accelerators.
1952 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1953 * each menu item of this menu, that contains a label describing its purpose,
1954 * automatically gets an accel path assigned. For example, a menu containing
1955 * menu items "New" and "Exit", will, after
1956 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1957 * has been called, assign its items the accel paths:
1958 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1959 * Assigning accel paths to menu items then enables the user to change
1960 * their accelerators at runtime. More details about accelerator paths
1961 * and their default setups can be found at gtk_accel_map_add_entry().
1963 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1964 * pass a static string, you can save some memory by interning it first with
1965 * g_intern_static_string().
1968 gtk_menu_set_accel_path (GtkMenu *menu,
1969 const gchar *accel_path)
1971 g_return_if_fail (GTK_IS_MENU (menu));
1973 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1975 /* FIXME: accel_path should be defined as const gchar* */
1976 menu->accel_path = (gchar*)g_intern_string (accel_path);
1977 if (menu->accel_path)
1978 _gtk_menu_refresh_accel_paths (menu, FALSE);
1982 * gtk_menu_get_accel_path
1983 * @menu: a valid #GtkMenu
1985 * Retrieves the accelerator path set on the menu.
1987 * Returns: the accelerator path set on the menu.
1992 gtk_menu_get_accel_path (GtkMenu *menu)
1994 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1996 return menu->accel_path;
2001 gboolean group_changed;
2005 refresh_accel_paths_foreach (GtkWidget *widget,
2008 AccelPropagation *prop = data;
2010 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2011 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2012 prop->menu->accel_path,
2013 prop->menu->accel_group,
2014 prop->group_changed);
2018 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2019 gboolean group_changed)
2021 g_return_if_fail (GTK_IS_MENU (menu));
2023 if (menu->accel_path && menu->accel_group)
2025 AccelPropagation prop;
2028 prop.group_changed = group_changed;
2029 gtk_container_foreach (GTK_CONTAINER (menu),
2030 refresh_accel_paths_foreach,
2036 gtk_menu_reposition (GtkMenu *menu)
2038 g_return_if_fail (GTK_IS_MENU (menu));
2040 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2041 gtk_menu_position (menu);
2045 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2048 g_return_if_fail (GTK_IS_MENU (menu));
2050 if (adjustment->value != menu->scroll_offset)
2051 gtk_menu_scroll_to (menu, adjustment->value);
2055 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2058 GdkGeometry geometry_hints;
2059 GtkMenuPrivate *priv;
2061 if (!menu->tearoff_window)
2064 priv = gtk_menu_get_private (menu);
2066 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2068 GtkRequisition requisition;
2070 gtk_widget_get_preferred_size (menu->tearoff_scrollbar,
2071 &requisition, NULL);
2072 width += requisition.width;
2075 geometry_hints.min_width = width;
2076 geometry_hints.max_width = width;
2078 geometry_hints.min_height = 0;
2079 geometry_hints.max_height = priv->requested_height;
2081 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2084 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2088 gtk_menu_update_title (GtkMenu *menu)
2090 if (menu->tearoff_window)
2093 GtkWidget *attach_widget;
2095 title = gtk_menu_get_title (menu);
2098 attach_widget = gtk_menu_get_attach_widget (menu);
2099 if (GTK_IS_MENU_ITEM (attach_widget))
2101 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2102 if (GTK_IS_LABEL (child))
2103 title = gtk_label_get_text (GTK_LABEL (child));
2108 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2113 gtk_menu_get_toplevel (GtkWidget *menu)
2115 GtkWidget *attach, *toplevel;
2117 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2119 if (GTK_IS_MENU_ITEM (attach))
2120 attach = gtk_widget_get_parent (attach);
2122 if (GTK_IS_MENU (attach))
2123 return gtk_menu_get_toplevel (attach);
2124 else if (GTK_IS_WIDGET (attach))
2126 toplevel = gtk_widget_get_toplevel (attach);
2127 if (gtk_widget_is_toplevel (toplevel))
2135 tearoff_window_destroyed (GtkWidget *widget,
2138 gtk_menu_set_tearoff_state (menu, FALSE);
2142 gtk_menu_set_tearoff_state (GtkMenu *menu,
2146 GtkMenuPrivate *priv;
2148 g_return_if_fail (GTK_IS_MENU (menu));
2150 priv = gtk_menu_get_private (menu);
2152 if (menu->torn_off != torn_off)
2154 menu->torn_off = torn_off;
2155 menu->tearoff_active = torn_off;
2159 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2160 gtk_menu_popdown (menu);
2162 if (!menu->tearoff_window)
2164 GtkWidget *toplevel;
2166 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2167 "type", GTK_WINDOW_TOPLEVEL,
2168 "screen", gtk_widget_get_screen (menu->toplevel),
2169 "app-paintable", TRUE,
2172 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2173 GDK_WINDOW_TYPE_HINT_MENU);
2174 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2175 g_signal_connect (menu->tearoff_window, "destroy",
2176 G_CALLBACK (tearoff_window_destroyed), menu);
2177 g_signal_connect (menu->tearoff_window, "event",
2178 G_CALLBACK (gtk_menu_window_event), menu);
2180 gtk_menu_update_title (menu);
2182 gtk_widget_realize (menu->tearoff_window);
2184 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2185 if (toplevel != NULL)
2186 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2187 GTK_WINDOW (toplevel));
2189 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2190 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2192 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2193 menu->tearoff_adjustment =
2194 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2196 priv->requested_height,
2200 g_object_connect (menu->tearoff_adjustment,
2201 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2203 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2205 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2206 menu->tearoff_scrollbar,
2209 if (menu->tearoff_adjustment->upper > height)
2210 gtk_widget_show (menu->tearoff_scrollbar);
2212 gtk_widget_show (menu->tearoff_hbox);
2215 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2217 /* Update menu->requisition
2219 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2222 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2224 gtk_widget_realize (menu->tearoff_window);
2225 gtk_menu_position (menu);
2227 gtk_widget_show (GTK_WIDGET (menu));
2228 gtk_widget_show (menu->tearoff_window);
2230 gtk_menu_scroll_to (menu, 0);
2235 gtk_widget_hide (GTK_WIDGET (menu));
2236 gtk_widget_hide (menu->tearoff_window);
2237 if (GTK_IS_CONTAINER (menu->toplevel))
2238 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2239 gtk_widget_destroy (menu->tearoff_window);
2241 menu->tearoff_window = NULL;
2242 menu->tearoff_hbox = NULL;
2243 menu->tearoff_scrollbar = NULL;
2244 menu->tearoff_adjustment = NULL;
2247 g_object_notify (G_OBJECT (menu), "tearoff-state");
2252 * gtk_menu_get_tearoff_state:
2255 * Returns whether the menu is torn off. See
2256 * gtk_menu_set_tearoff_state ().
2258 * Return value: %TRUE if the menu is currently torn off.
2261 gtk_menu_get_tearoff_state (GtkMenu *menu)
2263 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2265 return menu->torn_off;
2269 * gtk_menu_set_title:
2271 * @title: a string containing the title for the menu.
2273 * Sets the title string for the menu. The title is displayed when the menu
2274 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2275 * attached to a parent menu item, and if so it will try to use the same text as
2276 * that menu item's label.
2279 gtk_menu_set_title (GtkMenu *menu,
2282 GtkMenuPrivate *priv;
2285 g_return_if_fail (GTK_IS_MENU (menu));
2287 priv = gtk_menu_get_private (menu);
2289 old_title = priv->title;
2290 priv->title = g_strdup (title);
2293 gtk_menu_update_title (menu);
2294 g_object_notify (G_OBJECT (menu), "tearoff-title");
2298 * gtk_menu_get_title:
2301 * Returns the title of the menu. See gtk_menu_set_title().
2303 * Return value: the title of the menu, or %NULL if the menu has no
2304 * title set on it. This string is owned by the widget and should
2305 * not be modified or freed.
2307 G_CONST_RETURN gchar *
2308 gtk_menu_get_title (GtkMenu *menu)
2310 GtkMenuPrivate *priv;
2312 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2314 priv = gtk_menu_get_private (menu);
2320 gtk_menu_reorder_child (GtkMenu *menu,
2324 GtkMenuShell *menu_shell;
2326 g_return_if_fail (GTK_IS_MENU (menu));
2327 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2329 menu_shell = GTK_MENU_SHELL (menu);
2331 if (g_list_find (menu_shell->children, child))
2333 menu_shell->children = g_list_remove (menu_shell->children, child);
2334 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2336 menu_queue_resize (menu);
2341 gtk_menu_style_set (GtkWidget *widget,
2342 GtkStyle *previous_style)
2344 if (gtk_widget_get_realized (widget))
2346 GtkMenu *menu = GTK_MENU (widget);
2349 style = gtk_widget_get_style (widget);
2351 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2352 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2353 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2358 get_arrows_border (GtkMenu *menu,
2361 guint scroll_arrow_height;
2362 GtkArrowPlacement arrow_placement;
2364 gtk_widget_style_get (GTK_WIDGET (menu),
2365 "scroll-arrow-vlength", &scroll_arrow_height,
2366 "arrow_placement", &arrow_placement,
2369 switch (arrow_placement)
2371 case GTK_ARROWS_BOTH:
2372 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2373 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2376 case GTK_ARROWS_START:
2377 border->top = (menu->upper_arrow_visible ||
2378 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2382 case GTK_ARROWS_END:
2384 border->bottom = (menu->upper_arrow_visible ||
2385 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2389 border->left = border->right = 0;
2393 gtk_menu_realize (GtkWidget *widget)
2395 GtkAllocation allocation;
2398 GdkWindowAttr attributes;
2399 gint attributes_mask;
2402 GtkMenuPrivate *priv;
2405 guint vertical_padding;
2406 guint horizontal_padding;
2407 GtkBorder arrow_border;
2409 g_return_if_fail (GTK_IS_MENU (widget));
2411 menu = GTK_MENU (widget);
2412 priv = gtk_menu_get_private (menu);
2414 gtk_widget_set_realized (widget, TRUE);
2416 gtk_widget_get_allocation (widget, &allocation);
2418 attributes.window_type = GDK_WINDOW_CHILD;
2419 attributes.x = allocation.x;
2420 attributes.y = allocation.y;
2421 attributes.width = allocation.width;
2422 attributes.height = allocation.height;
2423 attributes.wclass = GDK_INPUT_OUTPUT;
2424 attributes.visual = gtk_widget_get_visual (widget);
2425 attributes.event_mask = gtk_widget_get_events (widget);
2426 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2427 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2429 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2431 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2432 &attributes, attributes_mask);
2433 gtk_widget_set_window (widget, window);
2434 gdk_window_set_user_data (window, widget);
2436 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2438 style = gtk_widget_get_style (widget);
2439 gtk_widget_style_get (GTK_WIDGET (menu),
2440 "vertical-padding", &vertical_padding,
2441 "horizontal-padding", &horizontal_padding,
2444 gtk_widget_get_allocation (widget, &allocation);
2446 attributes.x = border_width + style->xthickness + horizontal_padding;
2447 attributes.y = border_width + style->ythickness + vertical_padding;
2448 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2449 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2451 get_arrows_border (menu, &arrow_border);
2452 attributes.y += arrow_border.top;
2453 attributes.height -= arrow_border.top;
2454 attributes.height -= arrow_border.bottom;
2456 menu->view_window = gdk_window_new (window,
2457 &attributes, attributes_mask);
2458 gdk_window_set_user_data (menu->view_window, menu);
2460 gtk_widget_get_allocation (widget, &allocation);
2464 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2465 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2467 menu->bin_window = gdk_window_new (menu->view_window,
2468 &attributes, attributes_mask);
2469 gdk_window_set_user_data (menu->bin_window, menu);
2471 children = GTK_MENU_SHELL (menu)->children;
2474 child = children->data;
2475 children = children->next;
2477 gtk_widget_set_parent_window (child, menu->bin_window);
2480 gtk_widget_style_attach (widget);
2481 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2482 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2483 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2485 if (GTK_MENU_SHELL (widget)->active_menu_item)
2486 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2487 GTK_MENU_SHELL (widget)->active_menu_item);
2489 gdk_window_show (menu->bin_window);
2490 gdk_window_show (menu->view_window);
2494 gtk_menu_focus (GtkWidget *widget,
2495 GtkDirectionType direction)
2498 * A menu or its menu items cannot have focus
2503 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2506 menu_grab_transfer_window_get (GtkMenu *menu)
2508 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2511 GdkWindowAttr attributes;
2512 gint attributes_mask;
2514 attributes.x = -100;
2515 attributes.y = -100;
2516 attributes.width = 10;
2517 attributes.height = 10;
2518 attributes.window_type = GDK_WINDOW_TEMP;
2519 attributes.wclass = GDK_INPUT_ONLY;
2520 attributes.override_redirect = TRUE;
2521 attributes.event_mask = 0;
2523 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2525 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2526 &attributes, attributes_mask);
2527 gdk_window_set_user_data (window, menu);
2529 gdk_window_show (window);
2531 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2538 menu_grab_transfer_window_destroy (GtkMenu *menu)
2540 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2543 gdk_window_set_user_data (window, NULL);
2544 gdk_window_destroy (window);
2545 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2550 gtk_menu_unrealize (GtkWidget *widget)
2552 GtkMenu *menu = GTK_MENU (widget);
2554 menu_grab_transfer_window_destroy (menu);
2556 gdk_window_set_user_data (menu->view_window, NULL);
2557 gdk_window_destroy (menu->view_window);
2558 menu->view_window = NULL;
2560 gdk_window_set_user_data (menu->bin_window, NULL);
2561 gdk_window_destroy (menu->bin_window);
2562 menu->bin_window = NULL;
2564 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2569 calculate_line_heights (GtkMenu *menu,
2571 guint **ret_min_heights,
2572 guint **ret_nat_heights)
2574 GtkMenuShell *menu_shell;
2575 GtkMenuPrivate *priv;
2576 GtkWidget *child, *widget;
2578 guint horizontal_padding;
2586 widget = GTK_WIDGET (menu);
2587 menu_shell = GTK_MENU_SHELL (widget);
2588 priv = gtk_menu_get_private (menu);
2590 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2591 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2592 n_heights = gtk_menu_get_n_rows (menu);
2593 n_columns = gtk_menu_get_n_columns (menu);
2594 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2596 gtk_widget_style_get (GTK_WIDGET (menu),
2597 "horizontal-padding", &horizontal_padding,
2600 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2601 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2603 for (children = menu_shell->children; children; children = children->next)
2608 gint child_min, child_nat;
2610 child = children->data;
2612 if (! gtk_widget_get_visible (child))
2615 get_effective_child_attach (child, &l, &r, &t, &b);
2617 part = avail_width / (r - l);
2619 gtk_widget_get_preferred_height_for_width (child, part,
2620 &child_min, &child_nat);
2622 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2624 part = MAX (child_min, toggle_size) / (b - t);
2625 min_heights[t] = MAX (min_heights[t], part);
2627 part = MAX (child_nat, toggle_size) / (b - t);
2628 nat_heights[t] = MAX (nat_heights[t], part);
2631 if (ret_min_heights)
2632 *ret_min_heights = min_heights;
2634 g_free (min_heights);
2636 if (ret_nat_heights)
2637 *ret_nat_heights = nat_heights;
2639 g_free (nat_heights);
2645 gtk_menu_size_allocate (GtkWidget *widget,
2646 GtkAllocation *allocation)
2649 GtkMenuShell *menu_shell;
2651 GtkAllocation child_allocation;
2652 GtkMenuPrivate *priv;
2658 guint vertical_padding;
2659 guint horizontal_padding;
2661 g_return_if_fail (GTK_IS_MENU (widget));
2662 g_return_if_fail (allocation != NULL);
2664 menu = GTK_MENU (widget);
2665 menu_shell = GTK_MENU_SHELL (widget);
2666 priv = gtk_menu_get_private (menu);
2668 gtk_widget_set_allocation (widget, allocation);
2670 style = gtk_widget_get_style (widget);
2672 gtk_widget_style_get (GTK_WIDGET (menu),
2673 "vertical-padding", &vertical_padding,
2674 "horizontal-padding", &horizontal_padding,
2676 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2678 g_free (priv->heights);
2679 priv->heights_length =
2680 calculate_line_heights (menu,
2685 /* refresh our cached height request */
2686 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2687 for (i = 0; i < priv->heights_length; i++)
2688 priv->requested_height += priv->heights[i];
2690 x = border_width + style->xthickness + horizontal_padding;
2691 y = border_width + style->ythickness + vertical_padding;
2693 width = MAX (1, allocation->width - x * 2);
2694 height = MAX (1, allocation->height - y * 2);
2696 if (menu_shell->active)
2697 gtk_menu_scroll_to (menu, menu->scroll_offset);
2699 if (!menu->tearoff_active)
2701 GtkBorder arrow_border;
2703 get_arrows_border (menu, &arrow_border);
2704 y += arrow_border.top;
2705 height -= arrow_border.top;
2706 height -= arrow_border.bottom;
2709 if (gtk_widget_get_realized (widget))
2711 gdk_window_move_resize (gtk_widget_get_window (widget),
2712 allocation->x, allocation->y,
2713 allocation->width, allocation->height);
2715 gdk_window_move_resize (menu->view_window,
2722 if (menu_shell->children)
2724 gint base_width = width / gtk_menu_get_n_columns (menu);
2726 children = menu_shell->children;
2729 child = children->data;
2730 children = children->next;
2732 if (gtk_widget_get_visible (child))
2737 get_effective_child_attach (child, &l, &r, &t, &b);
2739 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2742 tmp = gtk_menu_get_n_columns (menu) - l;
2743 l = gtk_menu_get_n_columns (menu) - r;
2747 child_allocation.width = (r - l) * base_width;
2748 child_allocation.height = 0;
2749 child_allocation.x = l * base_width;
2750 child_allocation.y = 0;
2752 for (i = 0; i < b; i++)
2755 child_allocation.y += priv->heights[i];
2757 child_allocation.height += priv->heights[i];
2760 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2763 gtk_widget_size_allocate (child, &child_allocation);
2764 gtk_widget_queue_draw (child);
2768 /* Resize the item window */
2769 if (gtk_widget_get_realized (widget))
2775 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2776 height += priv->heights[i];
2778 width = gtk_menu_get_n_columns (menu) * base_width;
2779 gdk_window_resize (menu->bin_window, width, height);
2782 if (menu->tearoff_active)
2784 if (height >= priv->requested_height)
2786 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2788 gtk_widget_hide (menu->tearoff_scrollbar);
2789 gtk_menu_set_tearoff_hints (menu, allocation->width);
2791 gtk_menu_scroll_to (menu, 0);
2796 menu->tearoff_adjustment->upper = priv->requested_height;
2797 menu->tearoff_adjustment->page_size = allocation->height;
2799 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2800 menu->tearoff_adjustment->upper)
2803 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2806 gtk_menu_scroll_to (menu, value);
2809 gtk_adjustment_changed (menu->tearoff_adjustment);
2811 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2813 gtk_widget_show (menu->tearoff_scrollbar);
2814 gtk_menu_set_tearoff_hints (menu, allocation->width);
2822 get_arrows_visible_area (GtkMenu *menu,
2823 GdkRectangle *border,
2824 GdkRectangle *upper,
2825 GdkRectangle *lower,
2828 GtkArrowPlacement arrow_placement;
2830 GtkWidget *widget = GTK_WIDGET (menu);
2832 guint vertical_padding;
2833 guint horizontal_padding;
2834 gint scroll_arrow_height;
2836 style = gtk_widget_get_style (widget);
2838 gtk_widget_style_get (widget,
2839 "vertical-padding", &vertical_padding,
2840 "horizontal-padding", &horizontal_padding,
2841 "scroll-arrow-vlength", &scroll_arrow_height,
2842 "arrow-placement", &arrow_placement,
2845 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2846 border->x = border_width + style->xthickness + horizontal_padding;
2847 border->y = border_width + style->ythickness + vertical_padding;
2848 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2849 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2851 switch (arrow_placement)
2853 case GTK_ARROWS_BOTH:
2854 upper->x = border->x;
2855 upper->y = border->y;
2856 upper->width = border->width - 2 * border->x;
2857 upper->height = scroll_arrow_height;
2859 lower->x = border->x;
2860 lower->y = border->height - border->y - scroll_arrow_height;
2861 lower->width = border->width - 2 * border->x;
2862 lower->height = scroll_arrow_height;
2865 case GTK_ARROWS_START:
2866 upper->x = border->x;
2867 upper->y = border->y;
2868 upper->width = (border->width - 2 * border->x) / 2;
2869 upper->height = scroll_arrow_height;
2871 lower->x = border->x + upper->width;
2872 lower->y = border->y;
2873 lower->width = (border->width - 2 * border->x) / 2;
2874 lower->height = scroll_arrow_height;
2877 case GTK_ARROWS_END:
2878 upper->x = border->x;
2879 upper->y = border->height - border->y - scroll_arrow_height;
2880 upper->width = (border->width - 2 * border->x) / 2;
2881 upper->height = scroll_arrow_height;
2883 lower->x = border->x + upper->width;
2884 lower->y = border->height - border->y - scroll_arrow_height;
2885 lower->width = (border->width - 2 * border->x) / 2;
2886 lower->height = scroll_arrow_height;
2890 g_assert_not_reached();
2891 upper->x = upper->y = upper->width = upper->height = 0;
2892 lower->x = lower->y = lower->width = lower->height = 0;
2895 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2899 gtk_menu_draw (GtkWidget *widget,
2903 GtkMenuPrivate *priv;
2905 GdkRectangle border;
2911 menu = GTK_MENU (widget);
2912 priv = gtk_menu_get_private (menu);
2914 style = gtk_widget_get_style (widget);
2915 window = gtk_widget_get_window (widget);
2917 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2919 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2921 gfloat arrow_scaling;
2924 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2925 arrow_size = arrow_scaling * arrow_space;
2927 gtk_paint_box (style,
2933 gtk_widget_get_allocated_width (widget),
2934 gtk_widget_get_allocated_height (widget));
2936 if (menu->upper_arrow_visible && !menu->tearoff_active)
2938 gtk_paint_box (style,
2940 priv->upper_arrow_state,
2942 widget, "menu_scroll_arrow_up",
2948 gtk_paint_arrow (style,
2950 priv->upper_arrow_state,
2952 widget, "menu_scroll_arrow_up",
2955 upper.x + (upper.width - arrow_size) / 2,
2956 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2957 arrow_size, arrow_size);
2960 if (menu->lower_arrow_visible && !menu->tearoff_active)
2962 gtk_paint_box (style,
2964 priv->lower_arrow_state,
2966 widget, "menu_scroll_arrow_down",
2972 gtk_paint_arrow (style,
2974 priv->lower_arrow_state,
2976 widget, "menu_scroll_arrow_down",
2979 lower.x + (lower.width - arrow_size) / 2,
2980 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2981 arrow_size, arrow_size);
2985 if (gtk_cairo_should_draw_window (cr, menu->bin_window))
2987 gint y = -border.y + menu->scroll_offset;
2990 gtk_cairo_transform_to_window (cr, widget, menu->bin_window);
2992 if (!menu->tearoff_active)
2994 GtkBorder arrow_border;
2996 get_arrows_border (menu, &arrow_border);
2997 y -= arrow_border.top;
3000 gtk_paint_box (style,
3006 border.width, border.height);
3011 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3017 gtk_menu_show (GtkWidget *widget)
3019 GtkMenu *menu = GTK_MENU (widget);
3021 _gtk_menu_refresh_accel_paths (menu, FALSE);
3023 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3028 gtk_menu_get_preferred_width (GtkWidget *widget,
3033 GtkMenuShell *menu_shell;
3034 GtkMenuPrivate *priv;
3038 guint max_toggle_size;
3039 guint max_accel_width;
3040 guint horizontal_padding;
3042 gint child_min, child_nat;
3043 gint min_width, nat_width;
3045 menu = GTK_MENU (widget);
3046 menu_shell = GTK_MENU_SHELL (widget);
3047 priv = gtk_menu_get_private (menu);
3049 style = gtk_widget_get_style (GTK_WIDGET (widget));
3051 min_width = nat_width = 0;
3053 max_toggle_size = 0;
3054 max_accel_width = 0;
3056 children = menu_shell->children;
3063 child = children->data;
3064 children = children->next;
3066 if (! gtk_widget_get_visible (child))
3069 get_effective_child_attach (child, &l, &r, &t, &b);
3071 /* It's important to size_request the child
3072 * before doing the toggle size request, in
3073 * case the toggle size request depends on the size
3074 * request of a child of the child (e.g. for ImageMenuItem)
3077 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3078 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3080 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3081 max_toggle_size = MAX (max_toggle_size, toggle_size);
3082 max_accel_width = MAX (max_accel_width,
3083 GTK_MENU_ITEM (child)->accelerator_width);
3085 part = child_min / (r - l);
3086 min_width = MAX (min_width, part);
3088 part = child_nat / (r - l);
3089 nat_width = MAX (nat_width, part);
3092 /* If the menu doesn't include any images or check items
3093 * reserve the space so that all menus are consistent.
3094 * We only do this for 'ordinary' menus, not for combobox
3095 * menus or multi-column menus
3097 if (max_toggle_size == 0 &&
3098 gtk_menu_get_n_columns (menu) == 1 &&
3099 !priv->no_toggle_size)
3101 guint toggle_spacing;
3102 guint indicator_size;
3104 gtk_style_get (style,
3105 GTK_TYPE_CHECK_MENU_ITEM,
3106 "toggle-spacing", &toggle_spacing,
3107 "indicator-size", &indicator_size,
3110 max_toggle_size = indicator_size + toggle_spacing;
3113 min_width += 2 * max_toggle_size + max_accel_width;
3114 min_width *= gtk_menu_get_n_columns (menu);
3116 nat_width += 2 * max_toggle_size + max_accel_width;
3117 nat_width *= gtk_menu_get_n_columns (menu);
3119 gtk_widget_style_get (GTK_WIDGET (menu),
3120 "horizontal-padding", &horizontal_padding,
3123 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3124 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3125 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3127 menu->toggle_size = max_toggle_size;
3128 priv->accel_size = max_accel_width;
3131 *minimum_size = min_width;
3134 *natural_size = nat_width;
3136 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3138 if (menu->tearoff_active)
3139 gtk_menu_set_tearoff_hints (menu, min_width);
3143 gtk_menu_get_preferred_height (GtkWidget *widget,
3149 /* Menus are height-for-width only, just return the height for the minimum width */
3150 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3151 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3155 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3160 GtkMenu *menu = GTK_MENU (widget);
3161 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3162 guint *min_heights, *nat_heights;
3163 guint vertical_padding, border_width;
3165 gint min_height, nat_height;
3167 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3168 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3170 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3173 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3175 for (i = 0; i < n_heights; i++)
3177 min_height += min_heights[i];
3178 nat_height += nat_heights[i];
3181 if (private->have_position)
3183 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3184 GdkRectangle monitor;
3186 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3188 if (private->y + min_height > monitor.y + monitor.height)
3189 min_height = monitor.y + monitor.height - private->y;
3191 if (private->y + nat_height > monitor.y + monitor.height)
3192 nat_height = monitor.y + monitor.height - private->y;
3194 if (private->y < monitor.y)
3196 min_height -= monitor.y - private->y;
3197 nat_height -= monitor.y - private->y;
3202 *minimum_size = min_height;
3205 *natural_size = nat_height;
3207 g_free (min_heights);
3208 g_free (nat_heights);
3214 gtk_menu_button_scroll (GtkMenu *menu,
3215 GdkEventButton *event)
3217 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3219 gboolean touchscreen_mode;
3221 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3222 "gtk-touchscreen-mode", &touchscreen_mode,
3225 if (touchscreen_mode)
3226 gtk_menu_handle_scrolling (menu,
3227 event->x_root, event->y_root,
3228 event->type == GDK_BUTTON_PRESS,
3238 pointer_in_menu_window (GtkWidget *widget,
3242 GtkAllocation allocation;
3243 GtkMenu *menu = GTK_MENU (widget);
3245 if (gtk_widget_get_mapped (menu->toplevel))
3247 GtkMenuShell *menu_shell;
3248 gint window_x, window_y;
3250 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3251 &window_x, &window_y);
3253 gtk_widget_get_allocation (widget, &allocation);
3254 if (x_root >= window_x && x_root < window_x + allocation.width &&
3255 y_root >= window_y && y_root < window_y + allocation.height)
3258 menu_shell = GTK_MENU_SHELL (widget);
3260 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3261 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3269 gtk_menu_button_press (GtkWidget *widget,
3270 GdkEventButton *event)
3272 if (event->type != GDK_BUTTON_PRESS)
3275 /* Don't pass down to menu shell for presses over scroll arrows
3277 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3280 /* Don't pass down to menu shell if a non-menuitem part of the menu
3281 * was clicked. The check for the event_widget being a GtkMenuShell
3282 * works because we have the pointer grabbed on menu_shell->window
3283 * with owner_events=TRUE, so all events that are either outside
3284 * the menu or on its border are delivered relative to
3285 * menu_shell->window.
3287 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3288 pointer_in_menu_window (widget, event->x_root, event->y_root))
3291 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3295 gtk_menu_button_release (GtkWidget *widget,
3296 GdkEventButton *event)
3298 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3300 if (priv->ignore_button_release)
3302 priv->ignore_button_release = FALSE;
3306 if (event->type != GDK_BUTTON_RELEASE)
3309 /* Don't pass down to menu shell for releases over scroll arrows
3311 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3314 /* Don't pass down to menu shell if a non-menuitem part of the menu
3315 * was clicked (see comment in button_press()).
3317 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3318 pointer_in_menu_window (widget, event->x_root, event->y_root))
3320 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3321 * bail out early here so it is in a consistent state for the
3322 * next button_press/button_release in GtkMenuShell.
3325 if (GTK_MENU_SHELL (widget)->active)
3326 GTK_MENU_SHELL (widget)->button = 0;
3331 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3334 static const gchar *
3335 get_accel_path (GtkWidget *menu_item,
3340 GClosure *accel_closure;
3341 GtkAccelGroup *accel_group;
3343 path = _gtk_widget_get_accel_path (menu_item, locked);
3346 path = GTK_MENU_ITEM (menu_item)->accel_path;
3352 label = gtk_bin_get_child (GTK_BIN (menu_item));
3354 if (GTK_IS_ACCEL_LABEL (label))
3356 g_object_get (label,
3357 "accel-closure", &accel_closure,
3361 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3363 *locked = gtk_accel_group_get_is_locked (accel_group);
3373 gtk_menu_key_press (GtkWidget *widget,
3376 GtkMenuShell *menu_shell;
3378 gboolean delete = FALSE;
3379 gboolean can_change_accels;
3380 gchar *accel = NULL;
3381 guint accel_key, accel_mods;
3382 GdkModifierType consumed_modifiers;
3383 GdkDisplay *display;
3385 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3386 g_return_val_if_fail (event != NULL, FALSE);
3388 menu_shell = GTK_MENU_SHELL (widget);
3389 menu = GTK_MENU (widget);
3391 gtk_menu_stop_navigating_submenu (menu);
3393 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3396 display = gtk_widget_get_display (widget);
3398 g_object_get (gtk_widget_get_settings (widget),
3399 "gtk-menu-bar-accel", &accel,
3400 "gtk-can-change-accels", &can_change_accels,
3403 if (accel && *accel)
3406 GdkModifierType mods = 0;
3408 gtk_accelerator_parse (accel, &keyval, &mods);
3411 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3413 /* FIXME this is wrong, needs to be in the global accel resolution
3414 * thing, to properly consider i18n etc., but that probably requires
3415 * AccelGroup changes etc.
3417 if (event->keyval == keyval && (mods & event->state) == mods)
3419 gtk_menu_shell_cancel (menu_shell);
3427 switch (event->keyval)
3429 case GDK_KEY_Delete:
3430 case GDK_KEY_KP_Delete:
3431 case GDK_KEY_BackSpace:
3438 /* Figure out what modifiers went into determining the key symbol */
3439 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3440 event->hardware_keycode, event->state, event->group,
3441 NULL, NULL, NULL, &consumed_modifiers);
3443 accel_key = gdk_keyval_to_lower (event->keyval);
3444 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3446 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3447 * We re-upper case when we match against the keyval, but display and save in caseless form.
3449 if (accel_key != event->keyval)
3450 accel_mods |= GDK_SHIFT_MASK;
3452 /* Modify the accelerators */
3453 if (can_change_accels &&
3454 menu_shell->active_menu_item &&
3455 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3456 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3457 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3459 GtkWidget *menu_item = menu_shell->active_menu_item;
3460 gboolean locked, replace_accels = TRUE;
3463 path = get_accel_path (menu_item, &locked);
3464 if (!path || locked)
3466 /* can't change accelerators on menu_items without paths
3467 * (basically, those items are accelerator-locked).
3469 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3470 gtk_widget_error_bell (widget);
3476 /* For the keys that act to delete the current setting, we delete
3477 * the current setting if there is one, otherwise, we set the
3478 * key as the accelerator.
3484 if (gtk_accel_map_lookup_entry (path, &key) &&
3485 (key.accel_key || key.accel_mods))
3491 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3495 /* we failed, probably because this key is in use and
3498 /* g_print("failed to change\n"); */
3499 gtk_widget_error_bell (widget);
3508 check_threshold (GtkWidget *widget,
3517 ABS (start_x - x) > THRESHOLD ||
3518 ABS (start_y - y) > THRESHOLD;
3522 definitely_within_item (GtkWidget *widget,
3526 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3529 w = gdk_window_get_width (window);
3530 h = gdk_window_get_height (window);
3533 check_threshold (widget, 0, 0, x, y) &&
3534 check_threshold (widget, w - 1, 0, x, y) &&
3535 check_threshold (widget, w - 1, h - 1, x, y) &&
3536 check_threshold (widget, 0, h - 1, x, y);
3540 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3542 GtkMenuPrivate *priv;
3544 priv = gtk_menu_get_private (menu);
3546 return priv->navigation_height && priv->navigation_width;
3550 gtk_menu_motion_notify (GtkWidget *widget,
3551 GdkEventMotion *event)
3553 GtkWidget *menu_item;
3555 GtkMenuShell *menu_shell;
3558 gboolean need_enter;
3560 if (GTK_IS_MENU (widget))
3562 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3564 if (priv->ignore_button_release)
3565 priv->ignore_button_release = FALSE;
3567 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3571 /* We received the event for one of two reasons:
3573 * a) We are the active menu, and did gtk_grab_add()
3574 * b) The widget is a child of ours, and the event was propagated
3576 * Since for computation of navigation regions, we want the menu which
3577 * is the parent of the menu item, for a), we need to find that menu,
3578 * which may be different from 'widget'.
3580 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3581 parent = gtk_widget_get_parent (menu_item);
3582 if (!GTK_IS_MENU_ITEM (menu_item) ||
3583 !GTK_IS_MENU (parent))
3586 menu_shell = GTK_MENU_SHELL (parent);
3587 menu = GTK_MENU (menu_shell);
3589 if (definitely_within_item (menu_item, event->x, event->y))
3590 menu_shell->activate_time = 0;
3592 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3594 /* Check to see if we are within an active submenu's navigation region
3596 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3599 /* Make sure we pop down if we enter a non-selectable menu item, so we
3600 * don't show a submenu when the cursor is outside the stay-up triangle.
3602 if (!_gtk_menu_item_is_selectable (menu_item))
3604 /* We really want to deselect, but this gives the menushell code
3605 * a chance to do some bookkeeping about the menuitem.
3607 gtk_menu_shell_select_item (menu_shell, menu_item);
3613 /* The menu is now sensitive to enter events on its items, but
3614 * was previously sensitive. So we fake an enter event.
3616 menu_shell->ignore_enter = FALSE;
3618 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3619 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3621 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3624 send_event->crossing.window = g_object_ref (event->window);
3625 send_event->crossing.time = event->time;
3626 send_event->crossing.send_event = TRUE;
3627 send_event->crossing.x_root = event->x_root;
3628 send_event->crossing.y_root = event->y_root;
3629 send_event->crossing.x = event->x;
3630 send_event->crossing.y = event->y;
3631 send_event->crossing.state = event->state;
3632 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3634 /* We send the event to 'widget', the currently active menu,
3635 * instead of 'menu', the menu that the pointer is in. This
3636 * will ensure that the event will be ignored unless the
3637 * menuitem is a child of the active menu or some parent
3638 * menu of the active menu.
3640 result = gtk_widget_event (widget, send_event);
3641 gdk_event_free (send_event);
3651 get_double_arrows (GtkMenu *menu)
3653 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3654 gboolean double_arrows;
3655 GtkArrowPlacement arrow_placement;
3657 gtk_widget_style_get (GTK_WIDGET (menu),
3658 "double-arrows", &double_arrows,
3659 "arrow-placement", &arrow_placement,
3662 if (arrow_placement != GTK_ARROWS_BOTH)
3665 return double_arrows || (priv->initially_pushed_in &&
3666 menu->scroll_offset != 0);
3670 gtk_menu_scroll_by (GtkMenu *menu,
3673 GtkMenuPrivate *priv;
3674 GtkBorder arrow_border;
3678 gboolean double_arrows;
3680 widget = GTK_WIDGET (menu);
3681 offset = menu->scroll_offset + step;
3682 priv = gtk_menu_get_private (menu);
3684 get_arrows_border (menu, &arrow_border);
3686 double_arrows = get_double_arrows (menu);
3688 /* If we scroll upward and the non-visible top part
3689 * is smaller than the scroll arrow it would be
3690 * pretty stupid to show the arrow and taking more
3691 * screen space than just scrolling to the top.
3694 if ((step < 0) && (offset < arrow_border.top))
3697 /* Don't scroll over the top if we weren't before: */
3698 if ((menu->scroll_offset >= 0) && (offset < 0))
3701 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3703 if (menu->scroll_offset == 0 &&
3704 view_height >= priv->requested_height)
3707 /* Don't scroll past the bottom if we weren't before: */
3708 if (menu->scroll_offset > 0)
3709 view_height -= arrow_border.top;
3711 /* When both arrows are always shown, reduce
3712 * view height even more.
3715 view_height -= arrow_border.bottom;
3717 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3718 (offset + view_height > priv->requested_height))
3719 offset = priv->requested_height - view_height;
3721 if (offset != menu->scroll_offset)
3722 gtk_menu_scroll_to (menu, offset);
3726 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3727 gboolean touchscreen_mode)
3729 gboolean upper_visible;
3730 gboolean lower_visible;
3732 upper_visible = menu->upper_arrow_visible;
3733 lower_visible = menu->lower_arrow_visible;
3735 gtk_menu_scroll_by (menu, menu->scroll_step);
3737 if (touchscreen_mode &&
3738 (upper_visible != menu->upper_arrow_visible ||
3739 lower_visible != menu->lower_arrow_visible))
3741 /* We are about to hide a scroll arrow while the mouse is pressed,
3742 * this would cause the uncovered menu item to be activated on button
3743 * release. Therefore we need to ignore button release here
3745 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3746 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3751 gtk_menu_scroll_timeout (gpointer data)
3754 gboolean touchscreen_mode;
3756 menu = GTK_MENU (data);
3758 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3759 "gtk-touchscreen-mode", &touchscreen_mode,
3762 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3768 gtk_menu_scroll_timeout_initial (gpointer data)
3772 gboolean touchscreen_mode;
3774 menu = GTK_MENU (data);
3776 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3777 "gtk-timeout-repeat", &timeout,
3778 "gtk-touchscreen-mode", &touchscreen_mode,
3781 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3783 gtk_menu_remove_scroll_timeout (menu);
3785 menu->timeout_id = gdk_threads_add_timeout (timeout,
3786 gtk_menu_scroll_timeout,
3793 gtk_menu_start_scrolling (GtkMenu *menu)
3796 gboolean touchscreen_mode;
3798 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3799 "gtk-timeout-repeat", &timeout,
3800 "gtk-touchscreen-mode", &touchscreen_mode,
3803 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3805 menu->timeout_id = gdk_threads_add_timeout (timeout,
3806 gtk_menu_scroll_timeout_initial,
3811 gtk_menu_scroll (GtkWidget *widget,
3812 GdkEventScroll *event)
3814 GtkMenu *menu = GTK_MENU (widget);
3816 switch (event->direction)
3818 case GDK_SCROLL_RIGHT:
3819 case GDK_SCROLL_DOWN:
3820 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3822 case GDK_SCROLL_LEFT:
3824 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3832 get_arrows_sensitive_area (GtkMenu *menu,
3833 GdkRectangle *upper,
3834 GdkRectangle *lower)
3836 GtkArrowPlacement arrow_placement;
3837 GtkWidget *widget = GTK_WIDGET (menu);
3841 guint vertical_padding;
3843 gint scroll_arrow_height;
3845 window = gtk_widget_get_window (widget);
3846 width = gdk_window_get_width (window);
3847 height = gdk_window_get_height (window);
3849 gtk_widget_style_get (widget,
3850 "vertical-padding", &vertical_padding,
3851 "scroll-arrow-vlength", &scroll_arrow_height,
3852 "arrow-placement", &arrow_placement,
3855 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3856 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3858 gdk_window_get_position (window, &win_x, &win_y);
3860 switch (arrow_placement)
3862 case GTK_ARROWS_BOTH:
3867 upper->width = width;
3868 upper->height = scroll_arrow_height + border;
3874 lower->y = win_y + height - border - scroll_arrow_height;
3875 lower->width = width;
3876 lower->height = scroll_arrow_height + border;
3880 case GTK_ARROWS_START:
3885 upper->width = width / 2;
3886 upper->height = scroll_arrow_height + border;
3891 lower->x = win_x + width / 2;
3893 lower->width = width / 2;
3894 lower->height = scroll_arrow_height + border;
3898 case GTK_ARROWS_END:
3902 upper->y = win_y + height - border - scroll_arrow_height;
3903 upper->width = width / 2;
3904 upper->height = scroll_arrow_height + border;
3909 lower->x = win_x + width / 2;
3910 lower->y = win_y + height - border - scroll_arrow_height;
3911 lower->width = width / 2;
3912 lower->height = scroll_arrow_height + border;
3920 gtk_menu_handle_scrolling (GtkMenu *menu,
3926 GtkMenuShell *menu_shell;
3927 GtkMenuPrivate *priv;
3930 gboolean scroll_fast = FALSE;
3932 gboolean touchscreen_mode;
3934 priv = gtk_menu_get_private (menu);
3936 menu_shell = GTK_MENU_SHELL (menu);
3938 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3939 "gtk-touchscreen-mode", &touchscreen_mode,
3942 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3947 /* upper arrow handling */
3949 get_arrows_sensitive_area (menu, &rect, NULL);
3952 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3953 (x >= rect.x) && (x < rect.x + rect.width) &&
3954 (y >= rect.y) && (y < rect.y + rect.height))
3959 if (touchscreen_mode)
3960 menu->upper_arrow_prelight = in_arrow;
3962 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3964 gboolean arrow_pressed = FALSE;
3966 if (menu->upper_arrow_visible && !menu->tearoff_active)
3968 if (touchscreen_mode)
3970 if (enter && menu->upper_arrow_prelight)
3972 if (menu->timeout_id == 0)
3974 /* Deselect the active item so that
3975 * any submenus are popped down
3977 gtk_menu_shell_deselect (menu_shell);
3979 gtk_menu_remove_scroll_timeout (menu);
3980 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3984 /* Only do stuff on click. */
3985 gtk_menu_start_scrolling (menu);
3986 arrow_pressed = TRUE;
3991 arrow_pressed = TRUE;
3996 gtk_menu_stop_scrolling (menu);
3999 else /* !touchscreen_mode */
4001 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4003 if (enter && in_arrow &&
4004 (!menu->upper_arrow_prelight ||
4005 menu->scroll_fast != scroll_fast))
4007 menu->upper_arrow_prelight = TRUE;
4008 menu->scroll_fast = scroll_fast;
4010 /* Deselect the active item so that
4011 * any submenus are popped down
4013 gtk_menu_shell_deselect (menu_shell);
4015 gtk_menu_remove_scroll_timeout (menu);
4016 menu->scroll_step = scroll_fast ?
4017 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4020 gdk_threads_add_timeout (scroll_fast ?
4021 MENU_SCROLL_TIMEOUT2 :
4022 MENU_SCROLL_TIMEOUT1,
4023 gtk_menu_scroll_timeout, menu);
4025 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4027 gtk_menu_stop_scrolling (menu);
4032 /* gtk_menu_start_scrolling() might have hit the top of the
4033 * menu, so check if the button isn't insensitive before
4034 * changing it to something else.
4036 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4038 GtkStateType arrow_state = GTK_STATE_NORMAL;
4041 arrow_state = GTK_STATE_ACTIVE;
4042 else if (menu->upper_arrow_prelight)
4043 arrow_state = GTK_STATE_PRELIGHT;
4045 if (arrow_state != priv->upper_arrow_state)
4047 priv->upper_arrow_state = arrow_state;
4049 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4055 /* lower arrow handling */
4057 get_arrows_sensitive_area (menu, NULL, &rect);
4060 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4061 (x >= rect.x) && (x < rect.x + rect.width) &&
4062 (y >= rect.y) && (y < rect.y + rect.height))
4067 if (touchscreen_mode)
4068 menu->lower_arrow_prelight = in_arrow;
4070 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4072 gboolean arrow_pressed = FALSE;
4074 if (menu->lower_arrow_visible && !menu->tearoff_active)
4076 if (touchscreen_mode)
4078 if (enter && menu->lower_arrow_prelight)
4080 if (menu->timeout_id == 0)
4082 /* Deselect the active item so that
4083 * any submenus are popped down
4085 gtk_menu_shell_deselect (menu_shell);
4087 gtk_menu_remove_scroll_timeout (menu);
4088 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4092 /* Only do stuff on click. */
4093 gtk_menu_start_scrolling (menu);
4094 arrow_pressed = TRUE;
4099 arrow_pressed = TRUE;
4104 gtk_menu_stop_scrolling (menu);
4107 else /* !touchscreen_mode */
4109 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4111 if (enter && in_arrow &&
4112 (!menu->lower_arrow_prelight ||
4113 menu->scroll_fast != scroll_fast))
4115 menu->lower_arrow_prelight = TRUE;
4116 menu->scroll_fast = scroll_fast;
4118 /* Deselect the active item so that
4119 * any submenus are popped down
4121 gtk_menu_shell_deselect (menu_shell);
4123 gtk_menu_remove_scroll_timeout (menu);
4124 menu->scroll_step = scroll_fast ?
4125 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4128 gdk_threads_add_timeout (scroll_fast ?
4129 MENU_SCROLL_TIMEOUT2 :
4130 MENU_SCROLL_TIMEOUT1,
4131 gtk_menu_scroll_timeout, menu);
4133 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4135 gtk_menu_stop_scrolling (menu);
4140 /* gtk_menu_start_scrolling() might have hit the bottom of the
4141 * menu, so check if the button isn't insensitive before
4142 * changing it to something else.
4144 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4146 GtkStateType arrow_state = GTK_STATE_NORMAL;
4149 arrow_state = GTK_STATE_ACTIVE;
4150 else if (menu->lower_arrow_prelight)
4151 arrow_state = GTK_STATE_PRELIGHT;
4153 if (arrow_state != priv->lower_arrow_state)
4155 priv->lower_arrow_state = arrow_state;
4157 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4165 gtk_menu_enter_notify (GtkWidget *widget,
4166 GdkEventCrossing *event)
4168 GtkWidget *menu_item;
4170 gboolean touchscreen_mode;
4172 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4173 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4174 event->mode == GDK_CROSSING_STATE_CHANGED)
4177 g_object_get (gtk_widget_get_settings (widget),
4178 "gtk-touchscreen-mode", &touchscreen_mode,
4181 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4182 if (GTK_IS_MENU (widget))
4184 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4186 if (!menu_shell->ignore_enter)
4187 gtk_menu_handle_scrolling (GTK_MENU (widget),
4188 event->x_root, event->y_root, TRUE, TRUE);
4191 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4193 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4195 if (GTK_IS_MENU (menu))
4197 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4198 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4200 if (priv->seen_item_enter)
4202 /* This is the second enter we see for an item
4203 * on this menu. This means a release should always
4206 menu_shell->activate_time = 0;
4208 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4209 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4211 if (definitely_within_item (menu_item, event->x, event->y))
4213 /* This is an actual user-enter (ie. not a pop-under)
4214 * In this case, the user must either have entered
4215 * sufficiently far enough into the item, or he must move
4216 * far enough away from the enter point. (see
4217 * gtk_menu_motion_notify())
4219 menu_shell->activate_time = 0;
4223 priv->seen_item_enter = TRUE;
4227 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4228 * will not correspond to the event widget's parent. Check to see
4229 * if we are in the parent's navigation region.
4231 parent = gtk_widget_get_parent (menu_item);
4232 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4233 gtk_menu_navigating_submenu (GTK_MENU (parent),
4234 event->x_root, event->y_root))
4237 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4241 gtk_menu_leave_notify (GtkWidget *widget,
4242 GdkEventCrossing *event)
4244 GtkMenuShell *menu_shell;
4246 GtkMenuItem *menu_item;
4247 GtkWidget *event_widget;
4249 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4250 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4251 event->mode == GDK_CROSSING_STATE_CHANGED)
4254 menu = GTK_MENU (widget);
4255 menu_shell = GTK_MENU_SHELL (widget);
4257 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4260 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4262 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4264 if (!GTK_IS_MENU_ITEM (event_widget))
4267 menu_item = GTK_MENU_ITEM (event_widget);
4269 /* Here we check to see if we're leaving an active menu item with a submenu,
4270 * in which case we enter submenu navigation mode.
4272 if (menu_shell->active_menu_item != NULL
4273 && menu_item->submenu != NULL
4274 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4276 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4278 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4281 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4283 /* We are leaving an active menu item with nonactive submenu.
4284 * Deselect it so we don't surprise the user with by popping
4285 * up a submenu _after_ he left the item.
4287 gtk_menu_shell_deselect (menu_shell);
4292 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4296 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4298 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4300 priv->navigation_x = 0;
4301 priv->navigation_y = 0;
4302 priv->navigation_width = 0;
4303 priv->navigation_height = 0;
4305 if (menu->navigation_timeout)
4307 g_source_remove (menu->navigation_timeout);
4308 menu->navigation_timeout = 0;
4312 /* When the timeout is elapsed, the navigation region is destroyed
4313 * and the menuitem under the pointer (if any) is selected.
4316 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4318 GtkMenuPopdownData *popdown_data = user_data;
4319 GtkMenu *menu = popdown_data->menu;
4320 GdkWindow *child_window;
4322 gtk_menu_stop_navigating_submenu (menu);
4324 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4326 child_window = gdk_window_get_device_position (menu->bin_window,
4327 popdown_data->device,
4332 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4334 send_event->crossing.window = g_object_ref (child_window);
4335 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4336 send_event->crossing.send_event = TRUE;
4337 gdk_event_set_device (send_event, popdown_data->device);
4339 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4341 gdk_event_free (send_event);
4349 gtk_menu_navigating_submenu (GtkMenu *menu,
4353 GtkMenuPrivate *priv;
4356 if (!gtk_menu_has_navigation_triangle (menu))
4359 priv = gtk_menu_get_private (menu);
4360 width = priv->navigation_width;
4361 height = priv->navigation_height;
4363 /* check if x/y are in the triangle spanned by the navigation parameters */
4365 /* 1) Move the coordinates so the triangle starts at 0,0 */
4366 event_x -= priv->navigation_x;
4367 event_y -= priv->navigation_y;
4369 /* 2) Ensure both legs move along the positive axis */
4381 /* 3) Check that the given coordinate is inside the triangle. The formula
4382 * is a transformed form of this formula: x/w + y/h <= 1
4384 if (event_x >= 0 && event_y >= 0 &&
4385 event_x * height + event_y * width <= width * height)
4391 gtk_menu_stop_navigating_submenu (menu);
4397 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4398 GtkMenuItem *menu_item,
4399 GdkEventCrossing *event)
4401 gint submenu_left = 0;
4402 gint submenu_right = 0;
4403 gint submenu_top = 0;
4404 gint submenu_bottom = 0;
4407 GtkWidget *event_widget;
4408 GtkMenuPopdownData *popdown_data;
4409 GtkMenuPrivate *priv;
4412 g_return_if_fail (menu_item->submenu != NULL);
4413 g_return_if_fail (event != NULL);
4415 priv = gtk_menu_get_private (menu);
4417 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4419 window = gtk_widget_get_window (menu_item->submenu);
4420 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4422 submenu_right = submenu_left + gdk_window_get_width (window);
4423 submenu_bottom = submenu_top + gdk_window_get_height (window);
4425 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4426 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4428 if (event->x >= 0 && event->x < width)
4432 gtk_menu_stop_navigating_submenu (menu);
4434 /* The navigation region is the triangle closest to the x/y
4435 * location of the rectangle. This is why the width or height
4439 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4442 priv->navigation_x = submenu_left;
4443 priv->navigation_width = event->x_root - submenu_left;
4448 priv->navigation_x = submenu_right;
4449 priv->navigation_width = event->x_root - submenu_right;
4455 priv->navigation_y = event->y_root;
4456 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4458 if (priv->navigation_height >= 0)
4464 priv->navigation_y = event->y_root;
4465 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4467 if (priv->navigation_height <= 0)
4471 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4472 "gtk-menu-popdown-delay", &popdown_delay,
4475 popdown_data = g_new (GtkMenuPopdownData, 1);
4476 popdown_data->menu = menu;
4477 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4479 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4481 gtk_menu_stop_navigating_submenu_cb,
4483 (GDestroyNotify) g_free);
4488 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4492 g_return_if_fail (GTK_IS_MENU (menu_shell));
4494 parent = menu_shell->parent_menu_shell;
4496 menu_shell->activate_time = 0;
4497 gtk_menu_popdown (GTK_MENU (menu_shell));
4500 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4504 gtk_menu_position (GtkMenu *menu)
4507 GtkRequisition requisition;
4508 GtkMenuPrivate *private;
4513 GdkScreen *pointer_screen;
4514 GdkRectangle monitor;
4517 g_return_if_fail (GTK_IS_MENU (menu));
4519 widget = GTK_WIDGET (menu);
4521 screen = gtk_widget_get_screen (widget);
4522 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4523 gdk_display_get_device_state (gdk_screen_get_display (screen),
4524 pointer, &pointer_screen, &x, &y, NULL);
4526 /* Get the minimum height for minimum width to figure out
4527 * the right place to popup the menu.
4529 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4531 if (pointer_screen != screen)
4533 /* Pointer is on a different screen; roughly center the
4534 * menu on the screen. If someone was using multiscreen
4535 * + Xinerama together they'd probably want something
4536 * fancier; but that is likely to be vanishingly rare.
4538 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4539 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4542 private = gtk_menu_get_private (menu);
4543 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4545 private->initially_pushed_in = FALSE;
4547 /* Set the type hint here to allow custom position functions to set a different hint */
4548 if (!gtk_widget_get_visible (menu->toplevel))
4549 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4551 if (menu->position_func)
4553 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4554 menu->position_func_data);
4556 if (private->monitor_num < 0)
4557 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4559 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4563 GtkStyle *style = gtk_widget_get_style (widget);
4564 gint space_left, space_right, space_above, space_below;
4567 gint xthickness = style->xthickness;
4568 gint ythickness = style->ythickness;
4569 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4571 /* The placement of popup menus horizontally works like this (with
4572 * RTL in parentheses)
4574 * - If there is enough room to the right (left) of the mouse cursor,
4575 * position the menu there.
4577 * - Otherwise, if if there is enough room to the left (right) of the
4578 * mouse cursor, position the menu there.
4580 * - Otherwise if the menu is smaller than the monitor, position it
4581 * on the side of the mouse cursor that has the most space available
4583 * - Otherwise (if there is simply not enough room for the menu on the
4584 * monitor), position it as far left (right) as possible.
4586 * Positioning in the vertical direction is similar: first try below
4587 * mouse cursor, then above.
4589 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4591 space_left = x - monitor.x;
4592 space_right = monitor.x + monitor.width - x - 1;
4593 space_above = y - monitor.y;
4594 space_below = monitor.y + monitor.height - y - 1;
4596 /* position horizontally */
4598 /* the amount of space we need to position the menu. Note the
4599 * menu is offset "xthickness" pixels
4601 needed_width = requisition.width - xthickness;
4603 if (needed_width <= space_left ||
4604 needed_width <= space_right)
4606 if ((rtl && needed_width <= space_left) ||
4607 (!rtl && needed_width > space_right))
4610 x = x + xthickness - requisition.width + 1;
4614 /* position right */
4618 /* x is clamped on-screen further down */
4620 else if (requisition.width <= monitor.width)
4622 /* the menu is too big to fit on either side of the mouse
4623 * cursor, but smaller than the monitor. Position it on
4624 * the side that has the most space
4626 if (space_left > space_right)
4634 x = monitor.x + monitor.width - requisition.width;
4637 else /* menu is simply too big for the monitor */
4642 x = monitor.x + monitor.width - requisition.width;
4651 /* Position vertically. The algorithm is the same as above, but
4652 * simpler because we don't have to take RTL into account.
4654 needed_height = requisition.height - ythickness;
4656 if (needed_height <= space_above ||
4657 needed_height <= space_below)
4659 if (needed_height <= space_below)
4662 y = y + ythickness - requisition.height + 1;
4664 y = CLAMP (y, monitor.y,
4665 monitor.y + monitor.height - requisition.height);
4667 else if (needed_height > space_below && needed_height > space_above)
4669 if (space_below >= space_above)
4670 y = monitor.y + monitor.height - requisition.height;
4682 if (private->initially_pushed_in)
4684 menu_height = requisition.height;
4686 if (y + menu_height > monitor.y + monitor.height)
4688 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4689 y = (monitor.y + monitor.height) - menu_height;
4694 scroll_offset += monitor.y - y;
4699 /* FIXME: should this be done in the various position_funcs ? */
4700 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4702 if (GTK_MENU_SHELL (menu)->active)
4704 private->have_position = TRUE;
4709 if (y + requisition.height > monitor.y + monitor.height)
4710 requisition.height = (monitor.y + monitor.height) - y;
4714 scroll_offset += monitor.y - y;
4715 requisition.height -= monitor.y - y;
4719 if (scroll_offset > 0)
4721 GtkBorder arrow_border;
4723 get_arrows_border (menu, &arrow_border);
4724 scroll_offset += arrow_border.top;
4727 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4730 if (!GTK_MENU_SHELL (menu)->active)
4732 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4733 requisition.width, requisition.height);
4736 menu->scroll_offset = scroll_offset;
4740 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4742 if (menu->timeout_id)
4744 g_source_remove (menu->timeout_id);
4745 menu->timeout_id = 0;
4750 gtk_menu_stop_scrolling (GtkMenu *menu)
4752 gboolean touchscreen_mode;
4754 gtk_menu_remove_scroll_timeout (menu);
4756 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4757 "gtk-touchscreen-mode", &touchscreen_mode,
4760 if (!touchscreen_mode)
4762 menu->upper_arrow_prelight = FALSE;
4763 menu->lower_arrow_prelight = FALSE;
4768 gtk_menu_scroll_to (GtkMenu *menu,
4771 GtkMenuPrivate *priv;
4772 GtkAllocation allocation;
4773 GtkBorder arrow_border;
4777 gint view_width, view_height;
4780 guint vertical_padding;
4781 guint horizontal_padding;
4782 gboolean double_arrows;
4784 widget = GTK_WIDGET (menu);
4785 priv = gtk_menu_get_private (menu);
4787 if (menu->tearoff_active &&
4788 menu->tearoff_adjustment &&
4789 (menu->tearoff_adjustment->value != offset))
4791 menu->tearoff_adjustment->value =
4793 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4794 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4797 /* Move/resize the viewport according to arrows: */
4798 gtk_widget_get_allocation (widget, &allocation);
4799 view_width = allocation.width;
4800 view_height = allocation.height;
4802 style = gtk_widget_get_style (widget);
4804 gtk_widget_style_get (GTK_WIDGET (menu),
4805 "vertical-padding", &vertical_padding,
4806 "horizontal-padding", &horizontal_padding,
4809 double_arrows = get_double_arrows (menu);
4811 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4813 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4814 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4815 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4817 x = border_width + style->xthickness + horizontal_padding;
4818 y = border_width + style->ythickness + vertical_padding;
4820 if (double_arrows && !menu->tearoff_active)
4822 if (view_height < menu_height ||
4823 (offset > 0 && menu->scroll_offset > 0) ||
4824 (offset < 0 && menu->scroll_offset < 0))
4826 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4827 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4828 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4830 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4831 gtk_widget_queue_draw (GTK_WIDGET (menu));
4833 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4835 get_arrows_border (menu, &arrow_border);
4836 y += arrow_border.top;
4837 view_height -= arrow_border.top;
4838 view_height -= arrow_border.bottom;
4841 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4842 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4843 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4844 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4846 if (offset >= menu_height - view_height)
4847 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4848 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4849 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4850 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4852 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4853 (priv->lower_arrow_state != lower_arrow_previous_state))
4854 gtk_widget_queue_draw (GTK_WIDGET (menu));
4856 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4857 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4859 /* At the upper border, possibly remove timeout */
4860 if (menu->scroll_step < 0)
4862 gtk_menu_stop_scrolling (menu);
4863 gtk_widget_queue_draw (GTK_WIDGET (menu));
4867 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4868 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4870 /* At the lower border, possibly remove timeout */
4871 if (menu->scroll_step > 0)
4873 gtk_menu_stop_scrolling (menu);
4874 gtk_widget_queue_draw (GTK_WIDGET (menu));
4878 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4882 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4883 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4885 gtk_menu_stop_scrolling (menu);
4886 gtk_widget_queue_draw (GTK_WIDGET (menu));
4889 else if (!menu->tearoff_active)
4891 gboolean last_visible;
4893 last_visible = menu->upper_arrow_visible;
4894 menu->upper_arrow_visible = offset > 0;
4896 /* upper_arrow_visible may have changed, so requery the border */
4897 get_arrows_border (menu, &arrow_border);
4898 view_height -= arrow_border.top;
4900 if ((last_visible != menu->upper_arrow_visible) &&
4901 !menu->upper_arrow_visible)
4903 menu->upper_arrow_prelight = FALSE;
4905 /* If we hid the upper arrow, possibly remove timeout */
4906 if (menu->scroll_step < 0)
4908 gtk_menu_stop_scrolling (menu);
4909 gtk_widget_queue_draw (GTK_WIDGET (menu));
4913 last_visible = menu->lower_arrow_visible;
4914 menu->lower_arrow_visible = offset < menu_height - view_height;
4916 /* lower_arrow_visible may have changed, so requery the border */
4917 get_arrows_border (menu, &arrow_border);
4918 view_height -= arrow_border.bottom;
4920 if ((last_visible != menu->lower_arrow_visible) &&
4921 !menu->lower_arrow_visible)
4923 menu->lower_arrow_prelight = FALSE;
4925 /* If we hid the lower arrow, possibly remove timeout */
4926 if (menu->scroll_step > 0)
4928 gtk_menu_stop_scrolling (menu);
4929 gtk_widget_queue_draw (GTK_WIDGET (menu));
4933 y += arrow_border.top;
4936 /* Scroll the menu: */
4937 if (gtk_widget_get_realized (widget))
4938 gdk_window_move (menu->bin_window, 0, -offset);
4940 if (gtk_widget_get_realized (widget))
4941 gdk_window_move_resize (menu->view_window,
4947 menu->scroll_offset = offset;
4951 compute_child_offset (GtkMenu *menu,
4952 GtkWidget *menu_item,
4955 gboolean *is_last_child)
4957 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4958 gint item_top_attach;
4959 gint item_bottom_attach;
4960 gint child_offset = 0;
4963 get_effective_child_attach (menu_item, NULL, NULL,
4964 &item_top_attach, &item_bottom_attach);
4966 /* there is a possibility that we get called before _size_request, so
4967 * check the height table for safety.
4969 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4972 /* when we have a row with only invisible children, it's height will
4973 * be zero, so there's no need to check WIDGET_VISIBLE here
4975 for (i = 0; i < item_top_attach; i++)
4976 child_offset += priv->heights[i];
4979 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4981 *offset = child_offset;
4983 *height = priv->heights[item_top_attach];
4989 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4990 GtkWidget *menu_item)
4992 GtkMenu *menu = GTK_MENU (menu_shell);
4993 GtkWidget *widget = GTK_WIDGET (menu_shell);
4994 gint child_offset, child_height;
4998 gboolean last_child = 0;
5000 /* We need to check if the selected item fully visible.
5001 * If not we need to scroll the menu so that it becomes fully
5005 if (compute_child_offset (menu, menu_item,
5006 &child_offset, &child_height, &last_child))
5008 guint vertical_padding;
5009 gboolean double_arrows;
5011 y = menu->scroll_offset;
5012 width = gdk_window_get_width (gtk_widget_get_window (widget));
5013 height = gdk_window_get_height (gtk_widget_get_window (widget));
5015 gtk_widget_style_get (widget,
5016 "vertical-padding", &vertical_padding,
5019 double_arrows = get_double_arrows (menu);
5021 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5022 2 * gtk_widget_get_style (widget)->ythickness +
5023 2 * vertical_padding;
5024 if (child_offset < y)
5026 /* Ignore the enter event we might get if the pointer is on the menu
5028 menu_shell->ignore_enter = TRUE;
5029 gtk_menu_scroll_to (menu, child_offset);
5033 GtkBorder arrow_border;
5037 get_arrows_border (menu, &arrow_border);
5038 if (!menu->tearoff_active)
5039 arrow_height = arrow_border.top + arrow_border.bottom;
5041 if (child_offset + child_height > y + height - arrow_height)
5044 if ((!last_child && !menu->tearoff_active) || double_arrows)
5045 arrow_height += arrow_border.bottom;
5047 y = child_offset + child_height - height + arrow_height;
5048 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5050 /* Need upper arrow */
5051 arrow_height += arrow_border.top;
5052 y = child_offset + child_height - height + arrow_height;
5054 /* Ignore the enter event we might get if the pointer is on the menu
5056 menu_shell->ignore_enter = TRUE;
5057 gtk_menu_scroll_to (menu, y);
5065 gtk_menu_select_item (GtkMenuShell *menu_shell,
5066 GtkWidget *menu_item)
5068 GtkMenu *menu = GTK_MENU (menu_shell);
5070 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5071 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5073 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5077 /* Reparent the menu, taking care of the refcounting
5079 * If unrealize is true we force a unrealize while reparenting the parent.
5080 * This can help eliminate flicker in some cases.
5082 * What happens is that when the menu is unrealized and then re-realized,
5083 * the allocations are as follows:
5085 * parent - 1x1 at (0,0)
5086 * child1 - 100x20 at (0,0)
5087 * child2 - 100x20 at (0,20)
5088 * child3 - 100x20 at (0,40)
5090 * That is, the parent is small but the children are full sized. Then,
5091 * when the queued_resize gets processed, the parent gets resized to
5094 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5095 * contains the following logic:
5097 * - if a move or resize operation on a window would change the clip
5098 * region on the children, then before the window is resized
5099 * the background for children is temporarily set to None, the
5100 * move/resize done, and the background for the children restored.
5102 * So, at the point where the parent is resized to final size, the
5103 * background for the children is temporarily None, and thus they
5104 * are not cleared to the background color and the previous background
5105 * (the image of the menu) is left in place.
5108 gtk_menu_reparent (GtkMenu *menu,
5109 GtkWidget *new_parent,
5112 GObject *object = G_OBJECT (menu);
5113 GtkWidget *widget = GTK_WIDGET (menu);
5114 gboolean was_floating = g_object_is_floating (object);
5116 g_object_ref_sink (object);
5120 g_object_ref (object);
5121 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5122 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5123 g_object_unref (object);
5126 gtk_widget_reparent (widget, new_parent);
5129 g_object_force_floating (object);
5131 g_object_unref (object);
5135 gtk_menu_show_all (GtkWidget *widget)
5137 /* Show children, but not self. */
5138 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5143 gtk_menu_hide_all (GtkWidget *widget)
5145 /* Hide children, but not self. */
5146 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5150 * gtk_menu_set_screen:
5151 * @menu: a #GtkMenu.
5152 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5153 * determined by the widget the menu is attached to.
5155 * Sets the #GdkScreen on which the menu will be displayed.
5160 gtk_menu_set_screen (GtkMenu *menu,
5163 g_return_if_fail (GTK_IS_MENU (menu));
5164 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5166 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5170 menu_change_screen (menu, screen);
5174 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5176 attach_widget_screen_changed (attach_widget, NULL, menu);
5182 * @menu: a #GtkMenu.
5183 * @child: a #GtkMenuItem.
5184 * @left_attach: The column number to attach the left side of the item to.
5185 * @right_attach: The column number to attach the right side of the item to.
5186 * @top_attach: The row number to attach the top of the item to.
5187 * @bottom_attach: The row number to attach the bottom of the item to.
5189 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5190 * an item will occupy is specified by @left_attach, @right_attach,
5191 * @top_attach and @bottom_attach. These each represent the leftmost,
5192 * rightmost, uppermost and lower column and row numbers of the table.
5193 * (Columns and rows are indexed from zero).
5195 * Note that this function is not related to gtk_menu_detach().
5200 gtk_menu_attach (GtkMenu *menu,
5205 guint bottom_attach)
5207 GtkMenuShell *menu_shell;
5210 g_return_if_fail (GTK_IS_MENU (menu));
5211 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5212 parent = gtk_widget_get_parent (child);
5213 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5214 g_return_if_fail (left_attach < right_attach);
5215 g_return_if_fail (top_attach < bottom_attach);
5217 menu_shell = GTK_MENU_SHELL (menu);
5221 AttachInfo *ai = get_attach_info (child);
5223 ai->left_attach = left_attach;
5224 ai->right_attach = right_attach;
5225 ai->top_attach = top_attach;
5226 ai->bottom_attach = bottom_attach;
5228 menu_shell->children = g_list_append (menu_shell->children, child);
5230 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5232 menu_queue_resize (menu);
5236 gtk_container_child_set (GTK_CONTAINER (parent), child,
5237 "left-attach", left_attach,
5238 "right-attach", right_attach,
5239 "top-attach", top_attach,
5240 "bottom-attach", bottom_attach,
5246 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5250 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5251 "gtk-menu-popup-delay", &popup_delay,
5258 find_child_containing (GtkMenuShell *menu_shell,
5266 /* find a child which includes the area given by
5267 * left, right, top, bottom.
5270 for (list = menu_shell->children; list; list = list->next)
5274 if (!_gtk_menu_item_is_selectable (list->data))
5277 get_effective_child_attach (list->data, &l, &r, &t, &b);
5279 if (l <= left && right <= r
5280 && t <= top && bottom <= b)
5281 return GTK_WIDGET (list->data);
5288 gtk_menu_move_current (GtkMenuShell *menu_shell,
5289 GtkMenuDirectionType direction)
5291 GtkMenu *menu = GTK_MENU (menu_shell);
5294 GtkWidget *match = NULL;
5296 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5300 case GTK_MENU_DIR_CHILD:
5301 direction = GTK_MENU_DIR_PARENT;
5303 case GTK_MENU_DIR_PARENT:
5304 direction = GTK_MENU_DIR_CHILD;
5310 /* use special table menu key bindings */
5311 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5313 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5315 if (direction == GTK_MENU_DIR_NEXT)
5317 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5319 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5327 for (i = 0; i < t; i++)
5329 match = find_child_containing (menu_shell,
5330 l, l + 1, i, i + 1);
5336 else if (direction == GTK_MENU_DIR_PREV)
5338 for (i = t; i > 0; i--)
5340 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5348 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5350 match = find_child_containing (menu_shell,
5351 l, l + 1, i - 1, i);
5357 else if (direction == GTK_MENU_DIR_PARENT)
5359 /* we go one left if possible */
5361 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5365 GtkWidget *parent = menu_shell->parent_menu_shell;
5368 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5369 match = menu_shell->active_menu_item;
5372 else if (direction == GTK_MENU_DIR_CHILD)
5374 /* we go one right if possible */
5375 if (r < gtk_menu_get_n_columns (menu))
5376 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5380 GtkWidget *parent = menu_shell->parent_menu_shell;
5382 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5384 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5385 match = menu_shell->active_menu_item;
5391 gtk_menu_shell_select_item (menu_shell, match);
5396 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5400 get_visible_size (GtkMenu *menu)
5402 GtkAllocation allocation;
5403 GtkWidget *widget = GTK_WIDGET (menu);
5404 GtkContainer *container = GTK_CONTAINER (menu);
5407 gtk_widget_get_allocation (widget, &allocation);
5408 menu_height = (allocation.height
5409 - 2 * (gtk_container_get_border_width (container)
5410 + gtk_widget_get_style (widget)->ythickness));
5412 if (!menu->tearoff_active)
5414 GtkBorder arrow_border;
5416 get_arrows_border (menu, &arrow_border);
5417 menu_height -= arrow_border.top;
5418 menu_height -= arrow_border.bottom;
5424 /* Find the sensitive on-screen child containing @y, or if none,
5425 * the nearest selectable onscreen child. (%NULL if none)
5428 child_at (GtkMenu *menu,
5431 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5432 GtkWidget *child = NULL;
5433 gint child_offset = 0;
5436 gint lower, upper; /* Onscreen bounds */
5438 menu_height = get_visible_size (menu);
5439 lower = menu->scroll_offset;
5440 upper = menu->scroll_offset + menu_height;
5442 for (children = menu_shell->children; children; children = children->next)
5444 if (gtk_widget_get_visible (children->data))
5446 GtkRequisition child_requisition;
5448 gtk_widget_get_preferred_size (children->data,
5449 &child_requisition, NULL);
5451 if (_gtk_menu_item_is_selectable (children->data) &&
5452 child_offset >= lower &&
5453 child_offset + child_requisition.height <= upper)
5455 child = children->data;
5457 if (child_offset + child_requisition.height > y &&
5458 !GTK_IS_TEAROFF_MENU_ITEM (child))
5462 child_offset += child_requisition.height;
5470 get_menu_height (GtkMenu *menu)
5472 GtkAllocation allocation;
5473 GtkWidget *widget = GTK_WIDGET (menu);
5476 gtk_widget_get_allocation (widget, &allocation);
5478 height = allocation.height;
5479 height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + gtk_widget_get_style (widget)->ythickness) * 2;
5481 if (!menu->tearoff_active)
5483 GtkBorder arrow_border;
5485 get_arrows_border (menu, &arrow_border);
5486 height -= arrow_border.top;
5487 height -= arrow_border.bottom;
5494 gtk_menu_real_move_scroll (GtkMenu *menu,
5497 gint page_size = get_visible_size (menu);
5498 gint end_position = get_menu_height (menu);
5499 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5503 case GTK_SCROLL_PAGE_UP:
5504 case GTK_SCROLL_PAGE_DOWN:
5508 gint child_offset = 0;
5509 gboolean old_upper_arrow_visible;
5512 if (type == GTK_SCROLL_PAGE_UP)
5517 if (menu_shell->active_menu_item)
5521 compute_child_offset (menu, menu_shell->active_menu_item,
5522 &child_offset, &child_height, NULL);
5523 child_offset += child_height / 2;
5526 menu_shell->ignore_enter = TRUE;
5527 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5528 old_offset = menu->scroll_offset;
5530 new_offset = menu->scroll_offset + step;
5531 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5533 gtk_menu_scroll_to (menu, new_offset);
5535 if (menu_shell->active_menu_item)
5537 GtkWidget *new_child;
5538 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5539 GtkBorder arrow_border;
5541 get_arrows_border (menu, &arrow_border);
5543 if (menu->scroll_offset != old_offset)
5544 step = menu->scroll_offset - old_offset;
5546 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5548 new_child = child_at (menu, child_offset + step);
5550 gtk_menu_shell_select_item (menu_shell, new_child);
5554 case GTK_SCROLL_START:
5555 /* Ignore the enter event we might get if the pointer is on the menu
5557 menu_shell->ignore_enter = TRUE;
5558 gtk_menu_scroll_to (menu, 0);
5559 gtk_menu_shell_select_first (menu_shell, TRUE);
5561 case GTK_SCROLL_END:
5562 /* Ignore the enter event we might get if the pointer is on the menu
5564 menu_shell->ignore_enter = TRUE;
5565 gtk_menu_scroll_to (menu, end_position - page_size);
5566 _gtk_menu_shell_select_last (menu_shell, TRUE);
5575 * gtk_menu_set_monitor:
5577 * @monitor_num: the number of the monitor on which the menu should
5580 * Informs GTK+ on which monitor a menu should be popped up.
5581 * See gdk_screen_get_monitor_geometry().
5583 * This function should be called from a #GtkMenuPositionFunc if the
5584 * menu should not appear on the same monitor as the pointer. This
5585 * information can't be reliably inferred from the coordinates returned
5586 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5587 * may extend beyond the monitor boundaries or even the screen boundaries.
5592 gtk_menu_set_monitor (GtkMenu *menu,
5595 GtkMenuPrivate *priv;
5596 g_return_if_fail (GTK_IS_MENU (menu));
5598 priv = gtk_menu_get_private (menu);
5600 priv->monitor_num = monitor_num;
5604 * gtk_menu_get_monitor:
5607 * Retrieves the number of the monitor on which to show the menu.
5609 * Returns: the number of the monitor on which the menu should
5610 * be popped up or -1, if no monitor has been set
5615 gtk_menu_get_monitor (GtkMenu *menu)
5617 GtkMenuPrivate *priv;
5618 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5620 priv = gtk_menu_get_private (menu);
5622 return priv->monitor_num;
5626 * gtk_menu_get_for_attach_widget:
5627 * @widget: a #GtkWidget
5629 * Returns a list of the menus which are attached to this widget.
5630 * This list is owned by GTK+ and must not be modified.
5632 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5637 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5641 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5643 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5648 gtk_menu_grab_notify (GtkWidget *widget,
5649 gboolean was_grabbed)
5651 GtkWidget *toplevel;
5652 GtkWindowGroup *group;
5656 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5659 !gtk_widget_device_is_shadowed (widget, pointer))
5662 toplevel = gtk_widget_get_toplevel (widget);
5664 if (!GTK_IS_WINDOW (toplevel))
5667 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5668 grab = gtk_window_group_get_current_device_grab (group, pointer);
5670 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5671 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5675 * gtk_menu_set_reserve_toggle_size:
5677 * @reserve_toggle_size: whether to reserve size for toggles
5679 * Sets whether the menu should reserve space for drawing toggles
5680 * or icons, regardless of their actual presence.
5685 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5686 gboolean reserve_toggle_size)
5688 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5689 gboolean no_toggle_size;
5691 no_toggle_size = !reserve_toggle_size;
5693 if (priv->no_toggle_size != no_toggle_size)
5695 priv->no_toggle_size = no_toggle_size;
5697 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5702 * gtk_menu_get_reserve_toggle_size:
5705 * Returns whether the menu reserves space for toggles and
5706 * icons, regardless of their actual presence.
5708 * Returns: Whether the menu reserves toggle space
5713 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5715 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5717 return !priv->no_toggle_size;