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 NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
48 * extends below the submenu
51 #define MENU_SCROLL_STEP1 8
52 #define MENU_SCROLL_STEP2 15
53 #define MENU_SCROLL_FAST_ZONE 8
54 #define MENU_SCROLL_TIMEOUT1 50
55 #define MENU_SCROLL_TIMEOUT2 20
57 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
58 #define ATTACHED_MENUS "gtk-attached-menus"
60 typedef struct _GtkMenuAttachData GtkMenuAttachData;
61 typedef struct _GtkMenuPrivate GtkMenuPrivate;
62 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
64 struct _GtkMenuAttachData
66 GtkWidget *attach_widget;
67 GtkMenuDetachFunc detacher;
70 struct _GtkMenuPrivate
74 gboolean initially_pushed_in;
76 GDestroyNotify position_func_data_destroy;
78 /* info used for the table */
81 gint requested_height;
85 /* Cached layout information */
94 GtkStateType lower_arrow_state;
95 GtkStateType upper_arrow_state;
97 /* navigation region */
100 int navigation_width;
101 int navigation_height;
103 guint have_layout : 1;
104 guint seen_item_enter : 1;
105 guint have_position : 1;
106 guint ignore_button_release : 1;
107 guint no_toggle_size : 1;
110 struct _GtkMenuPopdownData
122 gint effective_left_attach;
123 gint effective_right_attach;
124 gint effective_top_attach;
125 gint effective_bottom_attach;
142 PROP_RESERVE_TOGGLE_SIZE
147 CHILD_PROP_LEFT_ATTACH,
148 CHILD_PROP_RIGHT_ATTACH,
149 CHILD_PROP_TOP_ATTACH,
150 CHILD_PROP_BOTTOM_ATTACH
153 static void gtk_menu_set_property (GObject *object,
157 static void gtk_menu_get_property (GObject *object,
161 static void gtk_menu_set_child_property(GtkContainer *container,
166 static void gtk_menu_get_child_property(GtkContainer *container,
171 static void gtk_menu_destroy (GtkWidget *widget);
172 static void gtk_menu_realize (GtkWidget *widget);
173 static void gtk_menu_unrealize (GtkWidget *widget);
174 static void gtk_menu_size_allocate (GtkWidget *widget,
175 GtkAllocation *allocation);
176 static void gtk_menu_show (GtkWidget *widget);
177 static gboolean gtk_menu_draw (GtkWidget *widget,
179 static gboolean gtk_menu_key_press (GtkWidget *widget,
181 static gboolean gtk_menu_scroll (GtkWidget *widget,
182 GdkEventScroll *event);
183 static gboolean gtk_menu_button_press (GtkWidget *widget,
184 GdkEventButton *event);
185 static gboolean gtk_menu_button_release (GtkWidget *widget,
186 GdkEventButton *event);
187 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
188 GdkEventMotion *event);
189 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
190 GdkEventCrossing *event);
191 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
192 GdkEventCrossing *event);
193 static void gtk_menu_scroll_to (GtkMenu *menu,
195 static void gtk_menu_grab_notify (GtkWidget *widget,
196 gboolean was_grabbed);
198 static void gtk_menu_stop_scrolling (GtkMenu *menu);
199 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
200 static gboolean gtk_menu_scroll_timeout (gpointer data);
201 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
202 static void gtk_menu_start_scrolling (GtkMenu *menu);
204 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
205 GtkWidget *menu_item);
206 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
207 GtkWidget *menu_item);
208 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
211 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
213 static void gtk_menu_handle_scrolling (GtkMenu *menu,
218 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
220 static void gtk_menu_style_set (GtkWidget *widget,
221 GtkStyle *previous_style);
222 static gboolean gtk_menu_focus (GtkWidget *widget,
223 GtkDirectionType direction);
224 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
225 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
226 GtkMenuDirectionType direction);
227 static void gtk_menu_real_move_scroll (GtkMenu *menu,
230 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
231 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
232 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
235 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
236 GtkMenuItem *menu_item,
237 GdkEventCrossing *event);
239 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
240 static void gtk_menu_show_all (GtkWidget *widget);
241 static void gtk_menu_position (GtkMenu *menu);
242 static void gtk_menu_reparent (GtkMenu *menu,
243 GtkWidget *new_parent,
245 static void gtk_menu_remove (GtkContainer *menu,
248 static void gtk_menu_update_title (GtkMenu *menu);
250 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
251 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
253 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
255 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
256 gboolean group_changed);
258 static void gtk_menu_get_preferred_width (GtkWidget *widget,
261 static void gtk_menu_get_preferred_height (GtkWidget *widget,
264 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
270 static const gchar attach_data_key[] = "gtk-menu-attach-data";
272 static guint menu_signals[LAST_SIGNAL] = { 0 };
274 static GtkMenuPrivate *
275 gtk_menu_get_private (GtkMenu *menu)
277 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
280 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
283 menu_queue_resize (GtkMenu *menu)
285 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
287 priv->have_layout = FALSE;
288 gtk_widget_queue_resize (GTK_WIDGET (menu));
292 attach_info_free (AttachInfo *info)
294 g_slice_free (AttachInfo, info);
298 get_attach_info (GtkWidget *child)
300 GObject *object = G_OBJECT (child);
301 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
305 ai = g_slice_new0 (AttachInfo);
306 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
307 (GDestroyNotify) attach_info_free);
314 is_grid_attached (AttachInfo *ai)
316 return (ai->left_attach >= 0 &&
317 ai->right_attach >= 0 &&
318 ai->top_attach >= 0 &&
319 ai->bottom_attach >= 0);
323 menu_ensure_layout (GtkMenu *menu)
325 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
327 if (!priv->have_layout)
329 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
333 gint max_right_attach;
334 gint max_bottom_attach;
336 /* Find extents of gridded portion
338 max_right_attach = 1;
339 max_bottom_attach = 0;
341 for (l = menu_shell->children; l; l = l->next)
343 GtkWidget *child = l->data;
344 AttachInfo *ai = get_attach_info (child);
346 if (is_grid_attached (ai))
348 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
349 max_right_attach = MAX (max_right_attach, ai->right_attach);
355 row_occupied = g_malloc0 (max_bottom_attach);
357 for (l = menu_shell->children; l; l = l->next)
359 GtkWidget *child = l->data;
360 AttachInfo *ai = get_attach_info (child);
362 if (is_grid_attached (ai))
366 for (i = ai->top_attach; i < ai->bottom_attach; i++)
367 row_occupied[i] = TRUE;
371 /* Lay non-grid-items out in those rows
374 for (l = menu_shell->children; l; l = l->next)
376 GtkWidget *child = l->data;
377 AttachInfo *ai = get_attach_info (child);
379 if (!is_grid_attached (ai))
381 while (current_row < max_bottom_attach && row_occupied[current_row])
384 ai->effective_left_attach = 0;
385 ai->effective_right_attach = max_right_attach;
386 ai->effective_top_attach = current_row;
387 ai->effective_bottom_attach = current_row + 1;
393 ai->effective_left_attach = ai->left_attach;
394 ai->effective_right_attach = ai->right_attach;
395 ai->effective_top_attach = ai->top_attach;
396 ai->effective_bottom_attach = ai->bottom_attach;
400 g_free (row_occupied);
402 priv->n_rows = MAX (current_row, max_bottom_attach);
403 priv->n_columns = max_right_attach;
404 priv->have_layout = TRUE;
410 gtk_menu_get_n_columns (GtkMenu *menu)
412 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
414 menu_ensure_layout (menu);
416 return priv->n_columns;
420 gtk_menu_get_n_rows (GtkMenu *menu)
422 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
424 menu_ensure_layout (menu);
430 get_effective_child_attach (GtkWidget *child,
436 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
439 menu_ensure_layout (menu);
441 ai = get_attach_info (child);
444 *l = ai->effective_left_attach;
446 *r = ai->effective_right_attach;
448 *t = ai->effective_top_attach;
450 *b = ai->effective_bottom_attach;
455 gtk_menu_class_init (GtkMenuClass *class)
457 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
458 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
459 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
460 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
461 GtkBindingSet *binding_set;
463 gobject_class->set_property = gtk_menu_set_property;
464 gobject_class->get_property = gtk_menu_get_property;
466 widget_class->destroy = gtk_menu_destroy;
467 widget_class->realize = gtk_menu_realize;
468 widget_class->unrealize = gtk_menu_unrealize;
469 widget_class->size_allocate = gtk_menu_size_allocate;
470 widget_class->show = gtk_menu_show;
471 widget_class->draw = gtk_menu_draw;
472 widget_class->scroll_event = gtk_menu_scroll;
473 widget_class->key_press_event = gtk_menu_key_press;
474 widget_class->button_press_event = gtk_menu_button_press;
475 widget_class->button_release_event = gtk_menu_button_release;
476 widget_class->motion_notify_event = gtk_menu_motion_notify;
477 widget_class->show_all = gtk_menu_show_all;
478 widget_class->enter_notify_event = gtk_menu_enter_notify;
479 widget_class->leave_notify_event = gtk_menu_leave_notify;
480 widget_class->style_set = gtk_menu_style_set;
481 widget_class->focus = gtk_menu_focus;
482 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
483 widget_class->grab_notify = gtk_menu_grab_notify;
484 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
485 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
486 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
488 container_class->remove = gtk_menu_remove;
489 container_class->get_child_property = gtk_menu_get_child_property;
490 container_class->set_child_property = gtk_menu_set_child_property;
492 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
493 menu_shell_class->deactivate = gtk_menu_deactivate;
494 menu_shell_class->select_item = gtk_menu_select_item;
495 menu_shell_class->insert = gtk_menu_real_insert;
496 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
497 menu_shell_class->move_current = gtk_menu_move_current;
499 menu_signals[MOVE_SCROLL] =
500 g_signal_new_class_handler (I_("move-scroll"),
501 G_OBJECT_CLASS_TYPE (gobject_class),
502 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
503 G_CALLBACK (gtk_menu_real_move_scroll),
505 _gtk_marshal_VOID__ENUM,
507 GTK_TYPE_SCROLL_TYPE);
512 * The index of the currently selected menu item, or -1 if no
513 * menu item is selected.
517 g_object_class_install_property (gobject_class,
519 g_param_spec_int ("active",
521 P_("The currently selected menu item"),
523 GTK_PARAM_READWRITE));
526 * GtkMenu:accel-group:
528 * The accel group holding accelerators for the menu.
532 g_object_class_install_property (gobject_class,
534 g_param_spec_object ("accel-group",
536 P_("The accel group holding accelerators for the menu"),
537 GTK_TYPE_ACCEL_GROUP,
538 GTK_PARAM_READWRITE));
541 * GtkMenu:accel-path:
543 * An accel path used to conveniently construct accel paths of child items.
547 g_object_class_install_property (gobject_class,
549 g_param_spec_string ("accel-path",
551 P_("An accel path used to conveniently construct accel paths of child items"),
553 GTK_PARAM_READWRITE));
556 * GtkMenu:attach-widget:
558 * The widget the menu is attached to. Setting this property attaches
559 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
560 * use gtk_menu_attach_to_widget() directly.
564 g_object_class_install_property (gobject_class,
566 g_param_spec_object ("attach-widget",
568 P_("The widget the menu is attached to"),
570 GTK_PARAM_READWRITE));
572 g_object_class_install_property (gobject_class,
574 g_param_spec_string ("tearoff-title",
576 P_("A title that may be displayed by the window manager when this menu is torn-off"),
578 GTK_PARAM_READWRITE));
581 * GtkMenu:tearoff-state:
583 * A boolean that indicates whether the menu is torn-off.
587 g_object_class_install_property (gobject_class,
589 g_param_spec_boolean ("tearoff-state",
591 P_("A boolean that indicates whether the menu is torn-off"),
593 GTK_PARAM_READWRITE));
598 * The monitor the menu will be popped up on.
602 g_object_class_install_property (gobject_class,
604 g_param_spec_int ("monitor",
606 P_("The monitor the menu will be popped up on"),
608 GTK_PARAM_READWRITE));
610 gtk_widget_class_install_style_property (widget_class,
611 g_param_spec_int ("vertical-padding",
612 P_("Vertical Padding"),
613 P_("Extra space at the top and bottom of the menu"),
617 GTK_PARAM_READABLE));
620 * GtkMenu:reserve-toggle-size:
622 * A boolean that indicates whether the menu reserves space for
623 * toggles and icons, regardless of their actual presence.
625 * This property should only be changed from its default value
626 * for special-purposes such as tabular menus. Regular menus that
627 * are connected to a menu bar or context menus should reserve
628 * toggle space for consistency.
632 g_object_class_install_property (gobject_class,
633 PROP_RESERVE_TOGGLE_SIZE,
634 g_param_spec_boolean ("reserve-toggle-size",
635 P_("Reserve Toggle Size"),
636 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
638 GTK_PARAM_READWRITE));
640 gtk_widget_class_install_style_property (widget_class,
641 g_param_spec_int ("horizontal-padding",
642 P_("Horizontal Padding"),
643 P_("Extra space at the left and right edges of the menu"),
647 GTK_PARAM_READABLE));
649 gtk_widget_class_install_style_property (widget_class,
650 g_param_spec_int ("vertical-offset",
651 P_("Vertical Offset"),
652 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
656 GTK_PARAM_READABLE));
658 gtk_widget_class_install_style_property (widget_class,
659 g_param_spec_int ("horizontal-offset",
660 P_("Horizontal Offset"),
661 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
665 GTK_PARAM_READABLE));
667 gtk_widget_class_install_style_property (widget_class,
668 g_param_spec_boolean ("double-arrows",
670 P_("When scrolling, always show both arrows."),
672 GTK_PARAM_READABLE));
675 * GtkMenu:arrow-placement:
677 * Indicates where scroll arrows should be placed.
681 gtk_widget_class_install_style_property (widget_class,
682 g_param_spec_enum ("arrow-placement",
683 P_("Arrow Placement"),
684 P_("Indicates where scroll arrows should be placed"),
685 GTK_TYPE_ARROW_PLACEMENT,
687 GTK_PARAM_READABLE));
689 gtk_container_class_install_child_property (container_class,
690 CHILD_PROP_LEFT_ATTACH,
691 g_param_spec_int ("left-attach",
693 P_("The column number to attach the left side of the child to"),
695 GTK_PARAM_READWRITE));
697 gtk_container_class_install_child_property (container_class,
698 CHILD_PROP_RIGHT_ATTACH,
699 g_param_spec_int ("right-attach",
701 P_("The column number to attach the right side of the child to"),
703 GTK_PARAM_READWRITE));
705 gtk_container_class_install_child_property (container_class,
706 CHILD_PROP_TOP_ATTACH,
707 g_param_spec_int ("top-attach",
709 P_("The row number to attach the top of the child to"),
711 GTK_PARAM_READWRITE));
713 gtk_container_class_install_child_property (container_class,
714 CHILD_PROP_BOTTOM_ATTACH,
715 g_param_spec_int ("bottom-attach",
717 P_("The row number to attach the bottom of the child to"),
719 GTK_PARAM_READWRITE));
722 * GtkMenu::arrow-scaling
724 * Arbitrary constant to scale down the size of the scroll arrow.
728 gtk_widget_class_install_style_property (widget_class,
729 g_param_spec_float ("arrow-scaling",
731 P_("Arbitrary constant to scale down the size of the scroll arrow"),
733 GTK_PARAM_READABLE));
735 binding_set = gtk_binding_set_by_class (class);
736 gtk_binding_entry_add_signal (binding_set,
738 I_("move-current"), 1,
739 GTK_TYPE_MENU_DIRECTION_TYPE,
741 gtk_binding_entry_add_signal (binding_set,
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,
760 GTK_MENU_DIR_PARENT);
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,
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_SCROLL_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,
802 GDK_KEY_KP_Page_Up, 0,
804 GTK_TYPE_SCROLL_TYPE,
806 gtk_binding_entry_add_signal (binding_set,
807 GDK_KEY_Page_Down, 0,
809 GTK_TYPE_SCROLL_TYPE,
810 GTK_SCROLL_PAGE_DOWN);
811 gtk_binding_entry_add_signal (binding_set,
812 GDK_KEY_KP_Page_Down, 0,
814 GTK_TYPE_SCROLL_TYPE,
815 GTK_SCROLL_PAGE_DOWN);
817 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
822 gtk_menu_set_property (GObject *object,
827 GtkMenu *menu = GTK_MENU (object);
832 gtk_menu_set_active (menu, g_value_get_int (value));
834 case PROP_ACCEL_GROUP:
835 gtk_menu_set_accel_group (menu, g_value_get_object (value));
837 case PROP_ACCEL_PATH:
838 gtk_menu_set_accel_path (menu, g_value_get_string (value));
840 case PROP_ATTACH_WIDGET:
844 widget = gtk_menu_get_attach_widget (menu);
846 gtk_menu_detach (menu);
848 widget = (GtkWidget*) g_value_get_object (value);
850 gtk_menu_attach_to_widget (menu, widget, NULL);
853 case PROP_TEAROFF_STATE:
854 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
856 case PROP_TEAROFF_TITLE:
857 gtk_menu_set_title (menu, g_value_get_string (value));
860 gtk_menu_set_monitor (menu, g_value_get_int (value));
862 case PROP_RESERVE_TOGGLE_SIZE:
863 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
866 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
872 gtk_menu_get_property (GObject *object,
877 GtkMenu *menu = GTK_MENU (object);
882 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
884 case PROP_ACCEL_GROUP:
885 g_value_set_object (value, gtk_menu_get_accel_group (menu));
887 case PROP_ACCEL_PATH:
888 g_value_set_string (value, gtk_menu_get_accel_path (menu));
890 case PROP_ATTACH_WIDGET:
891 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
893 case PROP_TEAROFF_STATE:
894 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
896 case PROP_TEAROFF_TITLE:
897 g_value_set_string (value, gtk_menu_get_title (menu));
900 g_value_set_int (value, gtk_menu_get_monitor (menu));
902 case PROP_RESERVE_TOGGLE_SIZE:
903 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
906 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
912 gtk_menu_set_child_property (GtkContainer *container,
918 GtkMenu *menu = GTK_MENU (container);
919 AttachInfo *ai = get_attach_info (child);
923 case CHILD_PROP_LEFT_ATTACH:
924 ai->left_attach = g_value_get_int (value);
926 case CHILD_PROP_RIGHT_ATTACH:
927 ai->right_attach = g_value_get_int (value);
929 case CHILD_PROP_TOP_ATTACH:
930 ai->top_attach = g_value_get_int (value);
932 case CHILD_PROP_BOTTOM_ATTACH:
933 ai->bottom_attach = g_value_get_int (value);
937 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
941 menu_queue_resize (menu);
945 gtk_menu_get_child_property (GtkContainer *container,
951 AttachInfo *ai = get_attach_info (child);
955 case CHILD_PROP_LEFT_ATTACH:
956 g_value_set_int (value, ai->left_attach);
958 case CHILD_PROP_RIGHT_ATTACH:
959 g_value_set_int (value, ai->right_attach);
961 case CHILD_PROP_TOP_ATTACH:
962 g_value_set_int (value, ai->top_attach);
964 case CHILD_PROP_BOTTOM_ATTACH:
965 g_value_set_int (value, ai->bottom_attach);
969 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
975 gtk_menu_window_event (GtkWidget *window,
979 gboolean handled = FALSE;
981 g_object_ref (window);
987 case GDK_KEY_RELEASE:
988 handled = gtk_widget_event (menu, event);
994 g_object_unref (window);
995 g_object_unref (menu);
1001 gtk_menu_init (GtkMenu *menu)
1003 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1005 menu->parent_menu_item = NULL;
1006 menu->old_active_menu_item = NULL;
1007 menu->accel_group = NULL;
1008 menu->position_func = NULL;
1009 menu->position_func_data = NULL;
1010 menu->toggle_size = 0;
1012 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1013 "type", GTK_WINDOW_POPUP,
1016 "signal::event", gtk_menu_window_event, menu,
1017 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1019 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1020 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1022 /* Refloat the menu, so that reference counting for the menu isn't
1023 * affected by it being a child of the toplevel
1025 g_object_force_floating (G_OBJECT (menu));
1026 menu->needs_destruction_ref_count = TRUE;
1028 menu->view_window = NULL;
1029 menu->bin_window = NULL;
1031 menu->scroll_offset = 0;
1032 menu->scroll_step = 0;
1033 menu->timeout_id = 0;
1034 menu->scroll_fast = FALSE;
1036 menu->tearoff_window = NULL;
1037 menu->tearoff_hbox = NULL;
1038 menu->torn_off = FALSE;
1039 menu->tearoff_active = FALSE;
1040 menu->tearoff_adjustment = NULL;
1041 menu->tearoff_scrollbar = NULL;
1043 menu->upper_arrow_visible = FALSE;
1044 menu->lower_arrow_visible = FALSE;
1045 menu->upper_arrow_prelight = FALSE;
1046 menu->lower_arrow_prelight = FALSE;
1048 priv->upper_arrow_state = GTK_STATE_NORMAL;
1049 priv->lower_arrow_state = GTK_STATE_NORMAL;
1051 priv->have_layout = FALSE;
1052 priv->monitor_num = -1;
1056 gtk_menu_destroy (GtkWidget *widget)
1058 GtkMenu *menu = GTK_MENU (widget);
1059 GtkMenuAttachData *data;
1060 GtkMenuPrivate *priv;
1062 gtk_menu_remove_scroll_timeout (menu);
1064 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1066 gtk_menu_detach (menu);
1068 gtk_menu_stop_navigating_submenu (menu);
1070 if (menu->old_active_menu_item)
1072 g_object_unref (menu->old_active_menu_item);
1073 menu->old_active_menu_item = NULL;
1076 /* Add back the reference count for being a child */
1077 if (menu->needs_destruction_ref_count)
1079 menu->needs_destruction_ref_count = FALSE;
1080 g_object_ref (widget);
1083 if (menu->accel_group)
1085 g_object_unref (menu->accel_group);
1086 menu->accel_group = NULL;
1090 gtk_widget_destroy (menu->toplevel);
1092 if (menu->tearoff_window)
1093 gtk_widget_destroy (menu->tearoff_window);
1095 priv = gtk_menu_get_private (menu);
1099 g_free (priv->heights);
1100 priv->heights = NULL;
1105 g_free (priv->title);
1109 if (priv->position_func_data_destroy)
1111 priv->position_func_data_destroy (menu->position_func_data);
1112 menu->position_func_data = NULL;
1113 priv->position_func_data_destroy = NULL;
1116 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1120 menu_change_screen (GtkMenu *menu,
1121 GdkScreen *new_screen)
1123 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1125 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1127 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1133 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1134 gtk_menu_position (menu);
1137 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1138 private->monitor_num = -1;
1142 attach_widget_screen_changed (GtkWidget *attach_widget,
1143 GdkScreen *previous_screen,
1146 if (gtk_widget_has_screen (attach_widget) &&
1147 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1149 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1154 gtk_menu_attach_to_widget (GtkMenu *menu,
1155 GtkWidget *attach_widget,
1156 GtkMenuDetachFunc detacher)
1158 GtkMenuAttachData *data;
1161 g_return_if_fail (GTK_IS_MENU (menu));
1162 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1164 /* keep this function in sync with gtk_widget_set_parent()
1167 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1170 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1171 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1175 g_object_ref_sink (menu);
1177 data = g_slice_new (GtkMenuAttachData);
1178 data->attach_widget = attach_widget;
1180 g_signal_connect (attach_widget, "screen-changed",
1181 G_CALLBACK (attach_widget_screen_changed), menu);
1182 attach_widget_screen_changed (attach_widget, NULL, menu);
1184 data->detacher = detacher;
1185 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1186 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1187 if (!g_list_find (list, menu))
1189 list = g_list_prepend (list, menu);
1191 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1192 (GDestroyNotify) g_list_free);
1194 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1195 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1197 /* we don't need to set the style here, since
1198 * we are a toplevel widget.
1201 /* Fallback title for menu comes from attach widget */
1202 gtk_menu_update_title (menu);
1204 g_object_notify (G_OBJECT (menu), "attach-widget");
1208 gtk_menu_get_attach_widget (GtkMenu *menu)
1210 GtkMenuAttachData *data;
1212 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1214 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1216 return data->attach_widget;
1221 gtk_menu_detach (GtkMenu *menu)
1223 GtkMenuAttachData *data;
1226 g_return_if_fail (GTK_IS_MENU (menu));
1228 /* keep this function in sync with gtk_widget_unparent()
1230 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1233 g_warning ("gtk_menu_detach(): menu is not attached");
1236 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1238 g_signal_handlers_disconnect_by_func (data->attach_widget,
1239 (gpointer) attach_widget_screen_changed,
1243 data->detacher (data->attach_widget, menu);
1244 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1245 list = g_list_remove (list, menu);
1247 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1248 (GDestroyNotify) g_list_free);
1250 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1252 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1253 gtk_widget_unrealize (GTK_WIDGET (menu));
1255 g_slice_free (GtkMenuAttachData, data);
1257 /* Fallback title for menu comes from attach widget */
1258 gtk_menu_update_title (menu);
1260 g_object_unref (menu);
1264 gtk_menu_remove (GtkContainer *container,
1267 GtkMenu *menu = GTK_MENU (container);
1269 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1271 /* Clear out old_active_menu_item if it matches the item we are removing
1273 if (menu->old_active_menu_item == widget)
1275 g_object_unref (menu->old_active_menu_item);
1276 menu->old_active_menu_item = NULL;
1279 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1280 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1282 menu_queue_resize (menu);
1288 return g_object_new (GTK_TYPE_MENU, NULL);
1292 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1296 GtkMenu *menu = GTK_MENU (menu_shell);
1297 AttachInfo *ai = get_attach_info (child);
1299 ai->left_attach = -1;
1300 ai->right_attach = -1;
1301 ai->top_attach = -1;
1302 ai->bottom_attach = -1;
1304 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1305 gtk_widget_set_parent_window (child, menu->bin_window);
1307 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1309 menu_queue_resize (menu);
1313 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1318 widget = GTK_WIDGET (menu);
1323 cairo_surface_t *surface;
1324 cairo_pattern_t *pattern;
1327 menu->tearoff_active = FALSE;
1328 menu->saved_scroll_offset = menu->scroll_offset;
1330 window = gtk_widget_get_window (menu->tearoff_window);
1331 width = gdk_window_get_width (window);
1332 height = gdk_window_get_height (window);
1334 surface = gdk_window_create_similar_surface (window,
1335 CAIRO_CONTENT_COLOR,
1339 cr = cairo_create (surface);
1340 gdk_cairo_set_source_window (cr,
1346 gtk_widget_set_size_request (menu->tearoff_window,
1350 pattern = cairo_pattern_create_for_surface (surface);
1351 gdk_window_set_background_pattern (window, pattern);
1353 cairo_pattern_destroy (pattern);
1354 cairo_surface_destroy (surface);
1359 popup_grab_on_window (GdkWindow *window,
1360 GdkDevice *keyboard,
1362 guint32 activate_time)
1365 gdk_device_grab (keyboard, window,
1366 GDK_OWNERSHIP_WINDOW, TRUE,
1367 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1368 NULL, activate_time) != GDK_GRAB_SUCCESS)
1372 gdk_device_grab (pointer, window,
1373 GDK_OWNERSHIP_WINDOW, TRUE,
1374 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1375 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1376 GDK_POINTER_MOTION_MASK,
1377 NULL, activate_time) != GDK_GRAB_SUCCESS)
1380 gdk_device_ungrab (keyboard, activate_time);
1389 * gtk_menu_popup_for_device:
1390 * @menu: a #GtkMenu.
1391 * @device: (allow-none): a #GdkDevice
1392 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1393 * menu item, or %NULL
1394 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1395 * the popup, or %NULL
1396 * @func: (allow-none): a user supplied function used to position the menu,
1398 * @data: (allow-none): user supplied data to be passed to @func
1399 * @destroy: (allow-none): destroy notify for @data
1400 * @button: the mouse button which was pressed to initiate the event
1401 * @activate_time: the time at which the activation event occurred
1403 * Displays a menu and makes it available for selection.
1405 * Applications can use this function to display context-sensitive menus,
1406 * and will typically supply %NULL for the @parent_menu_shell,
1407 * @parent_menu_item, @func, @data and @destroy parameters. The default
1408 * menu positioning function will position the menu at the current position
1409 * of @device (or its corresponding pointer).
1411 * The @button parameter should be the mouse button pressed to initiate
1412 * the menu popup. If the menu popup was initiated by something other than
1413 * a mouse button press, such as a mouse button release or a keypress,
1414 * @button should be 0.
1416 * The @activate_time parameter is used to conflict-resolve initiation of
1417 * concurrent requests for mouse/keyboard grab requests. To function
1418 * properly, this needs to be the time stamp of the user event (such as
1419 * a mouse click or key press) that caused the initiation of the popup.
1420 * Only if no such event is available, gtk_get_current_event_time() can
1426 gtk_menu_popup_for_device (GtkMenu *menu,
1428 GtkWidget *parent_menu_shell,
1429 GtkWidget *parent_menu_item,
1430 GtkMenuPositionFunc func,
1432 GDestroyNotify destroy,
1434 guint32 activate_time)
1437 GtkWidget *xgrab_shell;
1439 GdkEvent *current_event;
1440 GtkMenuShell *menu_shell;
1441 gboolean grab_keyboard;
1442 GtkMenuPrivate *priv;
1443 GtkWidget *parent_toplevel;
1444 GdkDevice *keyboard, *pointer;
1446 g_return_if_fail (GTK_IS_MENU (menu));
1447 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1450 device = gtk_get_current_event_device ();
1454 GdkDisplay *display;
1455 GdkDeviceManager *device_manager;
1458 display = gtk_widget_get_display (GTK_WIDGET (menu));
1459 device_manager = gdk_display_get_device_manager (display);
1460 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1462 device = devices->data;
1464 g_list_free (devices);
1467 widget = GTK_WIDGET (menu);
1468 menu_shell = GTK_MENU_SHELL (menu);
1469 priv = gtk_menu_get_private (menu);
1471 if (device->source == GDK_SOURCE_KEYBOARD)
1474 pointer = gdk_device_get_associated_device (device);
1479 keyboard = gdk_device_get_associated_device (device);
1482 menu_shell->parent_menu_shell = parent_menu_shell;
1484 priv->seen_item_enter = FALSE;
1486 /* Find the last viewable ancestor, and make an X grab on it
1488 parent = GTK_WIDGET (menu);
1492 gboolean viewable = TRUE;
1493 GtkWidget *tmp = parent;
1497 if (!gtk_widget_get_mapped (tmp))
1502 tmp = gtk_widget_get_parent (tmp);
1506 xgrab_shell = parent;
1508 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1511 /* We want to receive events generated when we map the menu; unfortunately,
1512 * since there is probably already an implicit grab in place from the
1513 * button that the user used to pop up the menu, we won't receive then --
1514 * in particular, the EnterNotify when the menu pops up under the pointer.
1516 * If we are grabbing on a parent menu shell, no problem; just grab on
1517 * that menu shell first before popping up the window with owner_events = TRUE.
1519 * When grabbing on the menu itself, things get more convuluted - we
1520 * we do an explicit grab on a specially created window with
1521 * owner_events = TRUE, which we override further down with a grab
1522 * on the menu. (We can't grab on the menu until it is mapped; we
1523 * probably could just leave the grab on the other window, with a
1524 * little reorganization of the code in gtkmenu*).
1526 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1527 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1532 if (xgrab_shell && xgrab_shell != widget)
1534 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1536 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1537 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1542 GdkWindow *transfer_window;
1544 xgrab_shell = widget;
1545 transfer_window = menu_grab_transfer_window_get (menu);
1546 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1548 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1549 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1553 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1555 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1556 * with a stuck up window, we just abort here. Presumably the user will
1559 menu_shell->parent_menu_shell = NULL;
1560 menu_grab_transfer_window_destroy (menu);
1564 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1565 menu_shell->active = TRUE;
1566 menu_shell->button = button;
1568 /* If we are popping up the menu from something other than, a button
1569 * press then, as a heuristic, we ignore enter events for the menu
1570 * until we get a MOTION_NOTIFY.
1573 current_event = gtk_get_current_event ();
1576 if ((current_event->type != GDK_BUTTON_PRESS) &&
1577 (current_event->type != GDK_ENTER_NOTIFY))
1578 menu_shell->ignore_enter = TRUE;
1580 gdk_event_free (current_event);
1583 menu_shell->ignore_enter = TRUE;
1587 gtk_menu_tearoff_bg_copy (menu);
1589 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1592 parent_toplevel = NULL;
1593 if (parent_menu_shell)
1594 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1595 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1597 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1599 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1602 /* Set transient for to get the right window group and parent relationship */
1603 if (GTK_IS_WINDOW (parent_toplevel))
1604 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1605 GTK_WINDOW (parent_toplevel));
1607 menu->parent_menu_item = parent_menu_item;
1608 menu->position_func = func;
1609 menu->position_func_data = data;
1610 priv->position_func_data_destroy = destroy;
1611 menu_shell->activate_time = activate_time;
1613 /* We need to show the menu here rather in the init function because
1614 * code expects to be able to tell if the menu is onscreen by
1615 * looking at the gtk_widget_get_visible (menu)
1617 gtk_widget_show (GTK_WIDGET (menu));
1619 /* Position the menu, possibly changing the size request
1621 gtk_menu_position (menu);
1623 /* Compute the size of the toplevel and realize it so we
1624 * can scroll correctly.
1627 GtkRequisition tmp_request;
1628 GtkAllocation tmp_allocation = { 0, };
1630 /* Instead of trusting the menu position function to queue a resize when the
1631 * menu goes out of bounds, invalidate the cached size here. */
1632 gtk_widget_queue_resize (GTK_WIDGET (menu));
1633 gtk_widget_get_preferred_size (menu->toplevel, &tmp_request, NULL);
1635 tmp_allocation.width = tmp_request.width;
1636 tmp_allocation.height = tmp_request.height;
1638 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1640 gtk_widget_realize (GTK_WIDGET (menu));
1643 gtk_menu_scroll_to (menu, menu->scroll_offset);
1645 /* if no item is selected, select the first one */
1646 if (!menu_shell->active_menu_item)
1648 gboolean touchscreen_mode;
1650 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1651 "gtk-touchscreen-mode", &touchscreen_mode,
1654 if (touchscreen_mode)
1655 gtk_menu_shell_select_first (menu_shell, TRUE);
1658 /* Once everything is set up correctly, map the toplevel window on
1661 gtk_widget_show (menu->toplevel);
1663 if (xgrab_shell == widget)
1664 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1666 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1668 if (parent_menu_shell)
1670 gboolean keyboard_mode;
1672 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1673 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1675 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1676 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1678 _gtk_menu_shell_update_mnemonics (menu_shell);
1683 * @menu: a #GtkMenu.
1684 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1685 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1686 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1687 * @data: (allow-none): user supplied data to be passed to @func.
1688 * @button: the mouse button which was pressed to initiate the event.
1689 * @activate_time: the time at which the activation event occurred.
1691 * Displays a menu and makes it available for selection. Applications can use
1692 * this function to display context-sensitive menus, and will typically supply
1693 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1694 * parameters. The default menu positioning function will position the menu
1695 * at the current mouse cursor position.
1697 * The @button parameter should be the mouse button pressed to initiate
1698 * the menu popup. If the menu popup was initiated by something other than
1699 * a mouse button press, such as a mouse button release or a keypress,
1700 * @button should be 0.
1702 * The @activate_time parameter is used to conflict-resolve initiation of
1703 * concurrent requests for mouse/keyboard grab requests. To function
1704 * properly, this needs to be the time stamp of the user event (such as
1705 * a mouse click or key press) that caused the initiation of the popup.
1706 * Only if no such event is available, gtk_get_current_event_time() can
1710 gtk_menu_popup (GtkMenu *menu,
1711 GtkWidget *parent_menu_shell,
1712 GtkWidget *parent_menu_item,
1713 GtkMenuPositionFunc func,
1716 guint32 activate_time)
1718 g_return_if_fail (GTK_IS_MENU (menu));
1720 gtk_menu_popup_for_device (menu,
1725 button, activate_time);
1729 gtk_menu_popdown (GtkMenu *menu)
1731 GtkMenuPrivate *private;
1732 GtkMenuShell *menu_shell;
1735 g_return_if_fail (GTK_IS_MENU (menu));
1737 menu_shell = GTK_MENU_SHELL (menu);
1738 private = gtk_menu_get_private (menu);
1740 menu_shell->parent_menu_shell = NULL;
1741 menu_shell->active = FALSE;
1742 menu_shell->ignore_enter = FALSE;
1744 private->have_position = FALSE;
1746 gtk_menu_stop_scrolling (menu);
1748 gtk_menu_stop_navigating_submenu (menu);
1750 if (menu_shell->active_menu_item)
1752 if (menu->old_active_menu_item)
1753 g_object_unref (menu->old_active_menu_item);
1754 menu->old_active_menu_item = menu_shell->active_menu_item;
1755 g_object_ref (menu->old_active_menu_item);
1758 gtk_menu_shell_deselect (menu_shell);
1760 /* The X Grab, if present, will automatically be removed when we hide
1762 gtk_widget_hide (menu->toplevel);
1763 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1765 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1769 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1771 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1773 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1777 /* We popped up the menu from the tearoff, so we need to
1778 * release the grab - we aren't actually hiding the menu.
1780 if (menu_shell->have_xgrab && pointer)
1782 GdkDevice *keyboard;
1784 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1785 keyboard = gdk_device_get_associated_device (pointer);
1788 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1792 /* gtk_menu_popdown is called each time a menu item is selected from
1793 * a torn off menu. Only scroll back to the saved position if the
1794 * non-tearoff menu was popped down.
1796 if (!menu->tearoff_active)
1797 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1798 menu->tearoff_active = TRUE;
1801 gtk_widget_hide (GTK_WIDGET (menu));
1803 menu_shell->have_xgrab = FALSE;
1806 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1808 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1810 menu_grab_transfer_window_destroy (menu);
1814 gtk_menu_get_active (GtkMenu *menu)
1819 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1821 if (!menu->old_active_menu_item)
1824 children = GTK_MENU_SHELL (menu)->children;
1828 child = children->data;
1829 children = children->next;
1831 if (gtk_bin_get_child (GTK_BIN (child)))
1836 menu->old_active_menu_item = child;
1837 if (menu->old_active_menu_item)
1838 g_object_ref (menu->old_active_menu_item);
1841 return menu->old_active_menu_item;
1845 gtk_menu_set_active (GtkMenu *menu,
1851 g_return_if_fail (GTK_IS_MENU (menu));
1853 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1856 child = tmp_list->data;
1857 if (gtk_bin_get_child (GTK_BIN (child)))
1859 if (menu->old_active_menu_item)
1860 g_object_unref (menu->old_active_menu_item);
1861 menu->old_active_menu_item = child;
1862 g_object_ref (menu->old_active_menu_item);
1869 * gtk_menu_set_accel_group:
1870 * @accel_group: (allow-none):
1873 gtk_menu_set_accel_group (GtkMenu *menu,
1874 GtkAccelGroup *accel_group)
1876 g_return_if_fail (GTK_IS_MENU (menu));
1878 if (menu->accel_group != accel_group)
1880 if (menu->accel_group)
1881 g_object_unref (menu->accel_group);
1882 menu->accel_group = accel_group;
1883 if (menu->accel_group)
1884 g_object_ref (menu->accel_group);
1885 _gtk_menu_refresh_accel_paths (menu, TRUE);
1890 gtk_menu_get_accel_group (GtkMenu *menu)
1892 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1894 return menu->accel_group;
1898 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1901 /* Menu items chain here to figure whether they can activate their
1902 * accelerators. Unlike ordinary widgets, menus allow accel
1903 * activation even if invisible since that's the usual case for
1904 * submenus/popup-menus. however, the state of the attach widget
1905 * affects the "activeness" of the menu.
1907 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1910 return gtk_widget_can_activate_accel (awidget, signal_id);
1912 return gtk_widget_is_sensitive (widget);
1916 * gtk_menu_set_accel_path
1917 * @menu: a valid #GtkMenu
1918 * @accel_path: (allow-none): a valid accelerator path
1920 * Sets an accelerator path for this menu from which accelerator paths
1921 * for its immediate children, its menu items, can be constructed.
1922 * The main purpose of this function is to spare the programmer the
1923 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1924 * each menu item that should support runtime user changable accelerators.
1925 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1926 * each menu item of this menu, that contains a label describing its purpose,
1927 * automatically gets an accel path assigned. For example, a menu containing
1928 * menu items "New" and "Exit", will, after
1929 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1930 * has been called, assign its items the accel paths:
1931 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1932 * Assigning accel paths to menu items then enables the user to change
1933 * their accelerators at runtime. More details about accelerator paths
1934 * and their default setups can be found at gtk_accel_map_add_entry().
1936 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1937 * pass a static string, you can save some memory by interning it first with
1938 * g_intern_static_string().
1941 gtk_menu_set_accel_path (GtkMenu *menu,
1942 const gchar *accel_path)
1944 g_return_if_fail (GTK_IS_MENU (menu));
1946 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1948 /* FIXME: accel_path should be defined as const gchar* */
1949 menu->accel_path = (gchar*)g_intern_string (accel_path);
1950 if (menu->accel_path)
1951 _gtk_menu_refresh_accel_paths (menu, FALSE);
1955 * gtk_menu_get_accel_path
1956 * @menu: a valid #GtkMenu
1958 * Retrieves the accelerator path set on the menu.
1960 * Returns: the accelerator path set on the menu.
1965 gtk_menu_get_accel_path (GtkMenu *menu)
1967 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1969 return menu->accel_path;
1974 gboolean group_changed;
1978 refresh_accel_paths_foreach (GtkWidget *widget,
1981 AccelPropagation *prop = data;
1983 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1984 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1985 prop->menu->accel_path,
1986 prop->menu->accel_group,
1987 prop->group_changed);
1991 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1992 gboolean group_changed)
1994 g_return_if_fail (GTK_IS_MENU (menu));
1996 if (menu->accel_path && menu->accel_group)
1998 AccelPropagation prop;
2001 prop.group_changed = group_changed;
2002 gtk_container_foreach (GTK_CONTAINER (menu),
2003 refresh_accel_paths_foreach,
2009 gtk_menu_reposition (GtkMenu *menu)
2011 g_return_if_fail (GTK_IS_MENU (menu));
2013 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2014 gtk_menu_position (menu);
2018 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2021 g_return_if_fail (GTK_IS_MENU (menu));
2023 if (adjustment->value != menu->scroll_offset)
2024 gtk_menu_scroll_to (menu, adjustment->value);
2028 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2031 GdkGeometry geometry_hints;
2032 GtkMenuPrivate *priv;
2034 if (!menu->tearoff_window)
2037 priv = gtk_menu_get_private (menu);
2039 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2041 GtkRequisition requisition;
2043 gtk_widget_get_preferred_size (menu->tearoff_scrollbar,
2044 &requisition, NULL);
2045 width += requisition.width;
2048 geometry_hints.min_width = width;
2049 geometry_hints.max_width = width;
2051 geometry_hints.min_height = 0;
2052 geometry_hints.max_height = priv->requested_height;
2054 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2057 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2061 gtk_menu_update_title (GtkMenu *menu)
2063 if (menu->tearoff_window)
2066 GtkWidget *attach_widget;
2068 title = gtk_menu_get_title (menu);
2071 attach_widget = gtk_menu_get_attach_widget (menu);
2072 if (GTK_IS_MENU_ITEM (attach_widget))
2074 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2075 if (GTK_IS_LABEL (child))
2076 title = gtk_label_get_text (GTK_LABEL (child));
2081 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2086 gtk_menu_get_toplevel (GtkWidget *menu)
2088 GtkWidget *attach, *toplevel;
2090 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2092 if (GTK_IS_MENU_ITEM (attach))
2093 attach = gtk_widget_get_parent (attach);
2095 if (GTK_IS_MENU (attach))
2096 return gtk_menu_get_toplevel (attach);
2097 else if (GTK_IS_WIDGET (attach))
2099 toplevel = gtk_widget_get_toplevel (attach);
2100 if (gtk_widget_is_toplevel (toplevel))
2108 tearoff_window_destroyed (GtkWidget *widget,
2111 gtk_menu_set_tearoff_state (menu, FALSE);
2115 gtk_menu_set_tearoff_state (GtkMenu *menu,
2119 GtkMenuPrivate *priv;
2121 g_return_if_fail (GTK_IS_MENU (menu));
2123 priv = gtk_menu_get_private (menu);
2125 if (menu->torn_off != torn_off)
2127 menu->torn_off = torn_off;
2128 menu->tearoff_active = torn_off;
2132 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2133 gtk_menu_popdown (menu);
2135 if (!menu->tearoff_window)
2137 GtkWidget *toplevel;
2139 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2140 "type", GTK_WINDOW_TOPLEVEL,
2141 "screen", gtk_widget_get_screen (menu->toplevel),
2142 "app-paintable", TRUE,
2145 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2146 GDK_WINDOW_TYPE_HINT_MENU);
2147 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2148 g_signal_connect (menu->tearoff_window, "destroy",
2149 G_CALLBACK (tearoff_window_destroyed), menu);
2150 g_signal_connect (menu->tearoff_window, "event",
2151 G_CALLBACK (gtk_menu_window_event), menu);
2153 gtk_menu_update_title (menu);
2155 gtk_widget_realize (menu->tearoff_window);
2157 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2158 if (toplevel != NULL)
2159 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2160 GTK_WINDOW (toplevel));
2162 menu->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 0);
2163 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2165 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2166 menu->tearoff_adjustment = gtk_adjustment_new (0,
2167 0, priv->requested_height,
2171 g_object_connect (menu->tearoff_adjustment,
2172 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2174 menu->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, menu->tearoff_adjustment);
2176 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2177 menu->tearoff_scrollbar,
2180 if (menu->tearoff_adjustment->upper > height)
2181 gtk_widget_show (menu->tearoff_scrollbar);
2183 gtk_widget_show (menu->tearoff_hbox);
2186 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2188 /* Update menu->requisition
2190 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2193 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2195 gtk_widget_realize (menu->tearoff_window);
2196 gtk_menu_position (menu);
2198 gtk_widget_show (GTK_WIDGET (menu));
2199 gtk_widget_show (menu->tearoff_window);
2201 gtk_menu_scroll_to (menu, 0);
2206 gtk_widget_hide (GTK_WIDGET (menu));
2207 gtk_widget_hide (menu->tearoff_window);
2208 if (GTK_IS_CONTAINER (menu->toplevel))
2209 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2210 gtk_widget_destroy (menu->tearoff_window);
2212 menu->tearoff_window = NULL;
2213 menu->tearoff_hbox = NULL;
2214 menu->tearoff_scrollbar = NULL;
2215 menu->tearoff_adjustment = NULL;
2218 g_object_notify (G_OBJECT (menu), "tearoff-state");
2223 * gtk_menu_get_tearoff_state:
2226 * Returns whether the menu is torn off. See
2227 * gtk_menu_set_tearoff_state ().
2229 * Return value: %TRUE if the menu is currently torn off.
2232 gtk_menu_get_tearoff_state (GtkMenu *menu)
2234 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2236 return menu->torn_off;
2240 * gtk_menu_set_title:
2242 * @title: a string containing the title for the menu.
2244 * Sets the title string for the menu. The title is displayed when the menu
2245 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2246 * attached to a parent menu item, and if so it will try to use the same text as
2247 * that menu item's label.
2250 gtk_menu_set_title (GtkMenu *menu,
2253 GtkMenuPrivate *priv;
2256 g_return_if_fail (GTK_IS_MENU (menu));
2258 priv = gtk_menu_get_private (menu);
2260 old_title = priv->title;
2261 priv->title = g_strdup (title);
2264 gtk_menu_update_title (menu);
2265 g_object_notify (G_OBJECT (menu), "tearoff-title");
2269 * gtk_menu_get_title:
2272 * Returns the title of the menu. See gtk_menu_set_title().
2274 * Return value: the title of the menu, or %NULL if the menu has no
2275 * title set on it. This string is owned by the widget and should
2276 * not be modified or freed.
2278 G_CONST_RETURN gchar *
2279 gtk_menu_get_title (GtkMenu *menu)
2281 GtkMenuPrivate *priv;
2283 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2285 priv = gtk_menu_get_private (menu);
2291 gtk_menu_reorder_child (GtkMenu *menu,
2295 GtkMenuShell *menu_shell;
2297 g_return_if_fail (GTK_IS_MENU (menu));
2298 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2300 menu_shell = GTK_MENU_SHELL (menu);
2302 if (g_list_find (menu_shell->children, child))
2304 menu_shell->children = g_list_remove (menu_shell->children, child);
2305 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2307 menu_queue_resize (menu);
2312 gtk_menu_style_set (GtkWidget *widget,
2313 GtkStyle *previous_style)
2315 if (gtk_widget_get_realized (widget))
2317 GtkMenu *menu = GTK_MENU (widget);
2320 style = gtk_widget_get_style (widget);
2322 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2323 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2324 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2329 get_arrows_border (GtkMenu *menu,
2332 guint scroll_arrow_height;
2333 GtkArrowPlacement arrow_placement;
2335 gtk_widget_style_get (GTK_WIDGET (menu),
2336 "scroll-arrow-vlength", &scroll_arrow_height,
2337 "arrow_placement", &arrow_placement,
2340 switch (arrow_placement)
2342 case GTK_ARROWS_BOTH:
2343 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2344 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2347 case GTK_ARROWS_START:
2348 border->top = (menu->upper_arrow_visible ||
2349 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2353 case GTK_ARROWS_END:
2355 border->bottom = (menu->upper_arrow_visible ||
2356 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2360 border->left = border->right = 0;
2364 gtk_menu_realize (GtkWidget *widget)
2366 GtkAllocation allocation;
2369 GdkWindowAttr attributes;
2370 gint attributes_mask;
2373 GtkMenuPrivate *priv;
2376 guint vertical_padding;
2377 guint horizontal_padding;
2378 GtkBorder arrow_border;
2380 g_return_if_fail (GTK_IS_MENU (widget));
2382 menu = GTK_MENU (widget);
2383 priv = gtk_menu_get_private (menu);
2385 gtk_widget_set_realized (widget, TRUE);
2387 gtk_widget_get_allocation (widget, &allocation);
2389 attributes.window_type = GDK_WINDOW_CHILD;
2390 attributes.x = allocation.x;
2391 attributes.y = allocation.y;
2392 attributes.width = allocation.width;
2393 attributes.height = allocation.height;
2394 attributes.wclass = GDK_INPUT_OUTPUT;
2395 attributes.visual = gtk_widget_get_visual (widget);
2396 attributes.event_mask = gtk_widget_get_events (widget);
2397 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2398 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2400 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2402 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2403 &attributes, attributes_mask);
2404 gtk_widget_set_window (widget, window);
2405 gdk_window_set_user_data (window, widget);
2407 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2409 style = gtk_widget_get_style (widget);
2410 gtk_widget_style_get (GTK_WIDGET (menu),
2411 "vertical-padding", &vertical_padding,
2412 "horizontal-padding", &horizontal_padding,
2415 gtk_widget_get_allocation (widget, &allocation);
2417 attributes.x = border_width + style->xthickness + horizontal_padding;
2418 attributes.y = border_width + style->ythickness + vertical_padding;
2419 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2420 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2422 get_arrows_border (menu, &arrow_border);
2423 attributes.y += arrow_border.top;
2424 attributes.height -= arrow_border.top;
2425 attributes.height -= arrow_border.bottom;
2427 menu->view_window = gdk_window_new (window,
2428 &attributes, attributes_mask);
2429 gdk_window_set_user_data (menu->view_window, menu);
2431 gtk_widget_get_allocation (widget, &allocation);
2435 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2436 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2438 menu->bin_window = gdk_window_new (menu->view_window,
2439 &attributes, attributes_mask);
2440 gdk_window_set_user_data (menu->bin_window, menu);
2442 children = GTK_MENU_SHELL (menu)->children;
2445 child = children->data;
2446 children = children->next;
2448 gtk_widget_set_parent_window (child, menu->bin_window);
2451 gtk_widget_style_attach (widget);
2452 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2453 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2454 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2456 if (GTK_MENU_SHELL (widget)->active_menu_item)
2457 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2458 GTK_MENU_SHELL (widget)->active_menu_item);
2460 gdk_window_show (menu->bin_window);
2461 gdk_window_show (menu->view_window);
2465 gtk_menu_focus (GtkWidget *widget,
2466 GtkDirectionType direction)
2469 * A menu or its menu items cannot have focus
2474 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2477 menu_grab_transfer_window_get (GtkMenu *menu)
2479 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2482 GdkWindowAttr attributes;
2483 gint attributes_mask;
2485 attributes.x = -100;
2486 attributes.y = -100;
2487 attributes.width = 10;
2488 attributes.height = 10;
2489 attributes.window_type = GDK_WINDOW_TEMP;
2490 attributes.wclass = GDK_INPUT_ONLY;
2491 attributes.override_redirect = TRUE;
2492 attributes.event_mask = 0;
2494 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2496 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2497 &attributes, attributes_mask);
2498 gdk_window_set_user_data (window, menu);
2500 gdk_window_show (window);
2502 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2509 menu_grab_transfer_window_destroy (GtkMenu *menu)
2511 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2514 gdk_window_set_user_data (window, NULL);
2515 gdk_window_destroy (window);
2516 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2521 gtk_menu_unrealize (GtkWidget *widget)
2523 GtkMenu *menu = GTK_MENU (widget);
2525 menu_grab_transfer_window_destroy (menu);
2527 gdk_window_set_user_data (menu->view_window, NULL);
2528 gdk_window_destroy (menu->view_window);
2529 menu->view_window = NULL;
2531 gdk_window_set_user_data (menu->bin_window, NULL);
2532 gdk_window_destroy (menu->bin_window);
2533 menu->bin_window = NULL;
2535 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2540 calculate_line_heights (GtkMenu *menu,
2542 guint **ret_min_heights,
2543 guint **ret_nat_heights)
2545 GtkMenuShell *menu_shell;
2546 GtkMenuPrivate *priv;
2547 GtkWidget *child, *widget;
2549 guint horizontal_padding;
2557 widget = GTK_WIDGET (menu);
2558 menu_shell = GTK_MENU_SHELL (widget);
2559 priv = gtk_menu_get_private (menu);
2561 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2562 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2563 n_heights = gtk_menu_get_n_rows (menu);
2564 n_columns = gtk_menu_get_n_columns (menu);
2565 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2567 gtk_widget_style_get (GTK_WIDGET (menu),
2568 "horizontal-padding", &horizontal_padding,
2571 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2572 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2574 for (children = menu_shell->children; children; children = children->next)
2579 gint child_min, child_nat;
2581 child = children->data;
2583 if (! gtk_widget_get_visible (child))
2586 get_effective_child_attach (child, &l, &r, &t, &b);
2588 part = avail_width / (r - l);
2590 gtk_widget_get_preferred_height_for_width (child, part,
2591 &child_min, &child_nat);
2593 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2595 part = MAX (child_min, toggle_size) / (b - t);
2596 min_heights[t] = MAX (min_heights[t], part);
2598 part = MAX (child_nat, toggle_size) / (b - t);
2599 nat_heights[t] = MAX (nat_heights[t], part);
2602 if (ret_min_heights)
2603 *ret_min_heights = min_heights;
2605 g_free (min_heights);
2607 if (ret_nat_heights)
2608 *ret_nat_heights = nat_heights;
2610 g_free (nat_heights);
2616 gtk_menu_size_allocate (GtkWidget *widget,
2617 GtkAllocation *allocation)
2620 GtkMenuShell *menu_shell;
2622 GtkAllocation child_allocation;
2623 GtkMenuPrivate *priv;
2629 guint vertical_padding;
2630 guint horizontal_padding;
2632 g_return_if_fail (GTK_IS_MENU (widget));
2633 g_return_if_fail (allocation != NULL);
2635 menu = GTK_MENU (widget);
2636 menu_shell = GTK_MENU_SHELL (widget);
2637 priv = gtk_menu_get_private (menu);
2639 gtk_widget_set_allocation (widget, allocation);
2641 style = gtk_widget_get_style (widget);
2643 gtk_widget_style_get (GTK_WIDGET (menu),
2644 "vertical-padding", &vertical_padding,
2645 "horizontal-padding", &horizontal_padding,
2647 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2649 g_free (priv->heights);
2650 priv->heights_length =
2651 calculate_line_heights (menu,
2656 /* refresh our cached height request */
2657 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2658 for (i = 0; i < priv->heights_length; i++)
2659 priv->requested_height += priv->heights[i];
2661 x = border_width + style->xthickness + horizontal_padding;
2662 y = border_width + style->ythickness + vertical_padding;
2664 width = MAX (1, allocation->width - x * 2);
2665 height = MAX (1, allocation->height - y * 2);
2667 if (menu_shell->active)
2668 gtk_menu_scroll_to (menu, menu->scroll_offset);
2670 if (!menu->tearoff_active)
2672 GtkBorder arrow_border;
2674 get_arrows_border (menu, &arrow_border);
2675 y += arrow_border.top;
2676 height -= arrow_border.top;
2677 height -= arrow_border.bottom;
2680 if (gtk_widget_get_realized (widget))
2682 gdk_window_move_resize (gtk_widget_get_window (widget),
2683 allocation->x, allocation->y,
2684 allocation->width, allocation->height);
2686 gdk_window_move_resize (menu->view_window,
2693 if (menu_shell->children)
2695 gint base_width = width / gtk_menu_get_n_columns (menu);
2697 children = menu_shell->children;
2700 child = children->data;
2701 children = children->next;
2703 if (gtk_widget_get_visible (child))
2708 get_effective_child_attach (child, &l, &r, &t, &b);
2710 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2713 tmp = gtk_menu_get_n_columns (menu) - l;
2714 l = gtk_menu_get_n_columns (menu) - r;
2718 child_allocation.width = (r - l) * base_width;
2719 child_allocation.height = 0;
2720 child_allocation.x = l * base_width;
2721 child_allocation.y = 0;
2723 for (i = 0; i < b; i++)
2726 child_allocation.y += priv->heights[i];
2728 child_allocation.height += priv->heights[i];
2731 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2734 gtk_widget_size_allocate (child, &child_allocation);
2735 gtk_widget_queue_draw (child);
2739 /* Resize the item window */
2740 if (gtk_widget_get_realized (widget))
2746 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2747 height += priv->heights[i];
2749 width = gtk_menu_get_n_columns (menu) * base_width;
2750 gdk_window_resize (menu->bin_window, width, height);
2753 if (menu->tearoff_active)
2755 if (height >= priv->requested_height)
2757 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2759 gtk_widget_hide (menu->tearoff_scrollbar);
2760 gtk_menu_set_tearoff_hints (menu, allocation->width);
2762 gtk_menu_scroll_to (menu, 0);
2767 menu->tearoff_adjustment->upper = priv->requested_height;
2768 menu->tearoff_adjustment->page_size = allocation->height;
2770 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2771 menu->tearoff_adjustment->upper)
2774 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2777 gtk_menu_scroll_to (menu, value);
2780 gtk_adjustment_changed (menu->tearoff_adjustment);
2782 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2784 gtk_widget_show (menu->tearoff_scrollbar);
2785 gtk_menu_set_tearoff_hints (menu, allocation->width);
2793 get_arrows_visible_area (GtkMenu *menu,
2794 GdkRectangle *border,
2795 GdkRectangle *upper,
2796 GdkRectangle *lower,
2799 GtkArrowPlacement arrow_placement;
2801 GtkWidget *widget = GTK_WIDGET (menu);
2803 guint vertical_padding;
2804 guint horizontal_padding;
2805 gint scroll_arrow_height;
2807 style = gtk_widget_get_style (widget);
2809 gtk_widget_style_get (widget,
2810 "vertical-padding", &vertical_padding,
2811 "horizontal-padding", &horizontal_padding,
2812 "scroll-arrow-vlength", &scroll_arrow_height,
2813 "arrow-placement", &arrow_placement,
2816 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2817 border->x = border_width + style->xthickness + horizontal_padding;
2818 border->y = border_width + style->ythickness + vertical_padding;
2819 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2820 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2822 switch (arrow_placement)
2824 case GTK_ARROWS_BOTH:
2825 upper->x = border->x;
2826 upper->y = border->y;
2827 upper->width = border->width - 2 * border->x;
2828 upper->height = scroll_arrow_height;
2830 lower->x = border->x;
2831 lower->y = border->height - border->y - scroll_arrow_height;
2832 lower->width = border->width - 2 * border->x;
2833 lower->height = scroll_arrow_height;
2836 case GTK_ARROWS_START:
2837 upper->x = border->x;
2838 upper->y = border->y;
2839 upper->width = (border->width - 2 * border->x) / 2;
2840 upper->height = scroll_arrow_height;
2842 lower->x = border->x + upper->width;
2843 lower->y = border->y;
2844 lower->width = (border->width - 2 * border->x) / 2;
2845 lower->height = scroll_arrow_height;
2848 case GTK_ARROWS_END:
2849 upper->x = border->x;
2850 upper->y = border->height - border->y - scroll_arrow_height;
2851 upper->width = (border->width - 2 * border->x) / 2;
2852 upper->height = scroll_arrow_height;
2854 lower->x = border->x + upper->width;
2855 lower->y = border->height - border->y - scroll_arrow_height;
2856 lower->width = (border->width - 2 * border->x) / 2;
2857 lower->height = scroll_arrow_height;
2861 g_assert_not_reached();
2862 upper->x = upper->y = upper->width = upper->height = 0;
2863 lower->x = lower->y = lower->width = lower->height = 0;
2866 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2870 gtk_menu_draw (GtkWidget *widget,
2874 GtkMenuPrivate *priv;
2876 GdkRectangle border;
2882 menu = GTK_MENU (widget);
2883 priv = gtk_menu_get_private (menu);
2885 style = gtk_widget_get_style (widget);
2886 window = gtk_widget_get_window (widget);
2888 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2890 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2892 gfloat arrow_scaling;
2895 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2896 arrow_size = arrow_scaling * arrow_space;
2898 gtk_paint_box (style,
2904 gtk_widget_get_allocated_width (widget),
2905 gtk_widget_get_allocated_height (widget));
2907 if (menu->upper_arrow_visible && !menu->tearoff_active)
2909 gtk_paint_box (style,
2911 priv->upper_arrow_state,
2913 widget, "menu_scroll_arrow_up",
2919 gtk_paint_arrow (style,
2921 priv->upper_arrow_state,
2923 widget, "menu_scroll_arrow_up",
2926 upper.x + (upper.width - arrow_size) / 2,
2927 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2928 arrow_size, arrow_size);
2931 if (menu->lower_arrow_visible && !menu->tearoff_active)
2933 gtk_paint_box (style,
2935 priv->lower_arrow_state,
2937 widget, "menu_scroll_arrow_down",
2943 gtk_paint_arrow (style,
2945 priv->lower_arrow_state,
2947 widget, "menu_scroll_arrow_down",
2950 lower.x + (lower.width - arrow_size) / 2,
2951 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2952 arrow_size, arrow_size);
2956 if (gtk_cairo_should_draw_window (cr, menu->bin_window))
2958 gint y = -border.y + menu->scroll_offset;
2961 gtk_cairo_transform_to_window (cr, widget, menu->bin_window);
2963 if (!menu->tearoff_active)
2965 GtkBorder arrow_border;
2967 get_arrows_border (menu, &arrow_border);
2968 y -= arrow_border.top;
2971 gtk_paint_box (style,
2977 border.width, border.height);
2982 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
2988 gtk_menu_show (GtkWidget *widget)
2990 GtkMenu *menu = GTK_MENU (widget);
2992 _gtk_menu_refresh_accel_paths (menu, FALSE);
2994 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2999 gtk_menu_get_preferred_width (GtkWidget *widget,
3004 GtkMenuShell *menu_shell;
3005 GtkMenuPrivate *priv;
3009 guint max_toggle_size;
3010 guint max_accel_width;
3011 guint horizontal_padding;
3013 gint child_min, child_nat;
3014 gint min_width, nat_width;
3016 menu = GTK_MENU (widget);
3017 menu_shell = GTK_MENU_SHELL (widget);
3018 priv = gtk_menu_get_private (menu);
3020 style = gtk_widget_get_style (GTK_WIDGET (widget));
3022 min_width = nat_width = 0;
3024 max_toggle_size = 0;
3025 max_accel_width = 0;
3027 children = menu_shell->children;
3034 child = children->data;
3035 children = children->next;
3037 if (! gtk_widget_get_visible (child))
3040 get_effective_child_attach (child, &l, &r, &t, &b);
3042 /* It's important to size_request the child
3043 * before doing the toggle size request, in
3044 * case the toggle size request depends on the size
3045 * request of a child of the child (e.g. for ImageMenuItem)
3048 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3049 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3051 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3052 max_toggle_size = MAX (max_toggle_size, toggle_size);
3053 max_accel_width = MAX (max_accel_width,
3054 GTK_MENU_ITEM (child)->accelerator_width);
3056 part = child_min / (r - l);
3057 min_width = MAX (min_width, part);
3059 part = child_nat / (r - l);
3060 nat_width = MAX (nat_width, part);
3063 /* If the menu doesn't include any images or check items
3064 * reserve the space so that all menus are consistent.
3065 * We only do this for 'ordinary' menus, not for combobox
3066 * menus or multi-column menus
3068 if (max_toggle_size == 0 &&
3069 gtk_menu_get_n_columns (menu) == 1 &&
3070 !priv->no_toggle_size)
3072 guint toggle_spacing;
3073 guint indicator_size;
3075 gtk_style_get (style,
3076 GTK_TYPE_CHECK_MENU_ITEM,
3077 "toggle-spacing", &toggle_spacing,
3078 "indicator-size", &indicator_size,
3081 max_toggle_size = indicator_size + toggle_spacing;
3084 min_width += 2 * max_toggle_size + max_accel_width;
3085 min_width *= gtk_menu_get_n_columns (menu);
3087 nat_width += 2 * max_toggle_size + max_accel_width;
3088 nat_width *= gtk_menu_get_n_columns (menu);
3090 gtk_widget_style_get (GTK_WIDGET (menu),
3091 "horizontal-padding", &horizontal_padding,
3094 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3095 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3096 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3098 menu->toggle_size = max_toggle_size;
3099 priv->accel_size = max_accel_width;
3102 *minimum_size = min_width;
3105 *natural_size = nat_width;
3107 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3109 if (menu->tearoff_active)
3110 gtk_menu_set_tearoff_hints (menu, min_width);
3114 gtk_menu_get_preferred_height (GtkWidget *widget,
3120 /* Menus are height-for-width only, just return the height for the minimum width */
3121 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3122 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3126 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3131 GtkMenu *menu = GTK_MENU (widget);
3132 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3133 guint *min_heights, *nat_heights;
3134 guint vertical_padding, border_width;
3136 gint min_height, nat_height;
3138 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3139 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3141 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3144 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3146 for (i = 0; i < n_heights; i++)
3148 min_height += min_heights[i];
3149 nat_height += nat_heights[i];
3152 if (private->have_position)
3154 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3155 GdkRectangle monitor;
3157 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3159 if (private->y + min_height > monitor.y + monitor.height)
3160 min_height = monitor.y + monitor.height - private->y;
3162 if (private->y + nat_height > monitor.y + monitor.height)
3163 nat_height = monitor.y + monitor.height - private->y;
3165 if (private->y < monitor.y)
3167 min_height -= monitor.y - private->y;
3168 nat_height -= monitor.y - private->y;
3173 *minimum_size = min_height;
3176 *natural_size = nat_height;
3178 g_free (min_heights);
3179 g_free (nat_heights);
3185 gtk_menu_button_scroll (GtkMenu *menu,
3186 GdkEventButton *event)
3188 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3190 gboolean touchscreen_mode;
3192 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3193 "gtk-touchscreen-mode", &touchscreen_mode,
3196 if (touchscreen_mode)
3197 gtk_menu_handle_scrolling (menu,
3198 event->x_root, event->y_root,
3199 event->type == GDK_BUTTON_PRESS,
3209 pointer_in_menu_window (GtkWidget *widget,
3213 GtkAllocation allocation;
3214 GtkMenu *menu = GTK_MENU (widget);
3216 if (gtk_widget_get_mapped (menu->toplevel))
3218 GtkMenuShell *menu_shell;
3219 gint window_x, window_y;
3221 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3222 &window_x, &window_y);
3224 gtk_widget_get_allocation (widget, &allocation);
3225 if (x_root >= window_x && x_root < window_x + allocation.width &&
3226 y_root >= window_y && y_root < window_y + allocation.height)
3229 menu_shell = GTK_MENU_SHELL (widget);
3231 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3232 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3240 gtk_menu_button_press (GtkWidget *widget,
3241 GdkEventButton *event)
3243 if (event->type != GDK_BUTTON_PRESS)
3246 /* Don't pass down to menu shell for presses over scroll arrows
3248 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3251 /* Don't pass down to menu shell if a non-menuitem part of the menu
3252 * was clicked. The check for the event_widget being a GtkMenuShell
3253 * works because we have the pointer grabbed on menu_shell->window
3254 * with owner_events=TRUE, so all events that are either outside
3255 * the menu or on its border are delivered relative to
3256 * menu_shell->window.
3258 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3259 pointer_in_menu_window (widget, event->x_root, event->y_root))
3262 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3266 gtk_menu_button_release (GtkWidget *widget,
3267 GdkEventButton *event)
3269 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3271 if (priv->ignore_button_release)
3273 priv->ignore_button_release = FALSE;
3277 if (event->type != GDK_BUTTON_RELEASE)
3280 /* Don't pass down to menu shell for releases over scroll arrows
3282 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3285 /* Don't pass down to menu shell if a non-menuitem part of the menu
3286 * was clicked (see comment in button_press()).
3288 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3289 pointer_in_menu_window (widget, event->x_root, event->y_root))
3291 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3292 * bail out early here so it is in a consistent state for the
3293 * next button_press/button_release in GtkMenuShell.
3296 if (GTK_MENU_SHELL (widget)->active)
3297 GTK_MENU_SHELL (widget)->button = 0;
3302 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3305 static const gchar *
3306 get_accel_path (GtkWidget *menu_item,
3311 GClosure *accel_closure;
3312 GtkAccelGroup *accel_group;
3314 path = _gtk_widget_get_accel_path (menu_item, locked);
3317 path = GTK_MENU_ITEM (menu_item)->accel_path;
3323 label = gtk_bin_get_child (GTK_BIN (menu_item));
3325 if (GTK_IS_ACCEL_LABEL (label))
3327 g_object_get (label,
3328 "accel-closure", &accel_closure,
3332 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3334 *locked = gtk_accel_group_get_is_locked (accel_group);
3344 gtk_menu_key_press (GtkWidget *widget,
3347 GtkMenuShell *menu_shell;
3349 gboolean delete = FALSE;
3350 gboolean can_change_accels;
3351 gchar *accel = NULL;
3352 guint accel_key, accel_mods;
3353 GdkModifierType consumed_modifiers;
3354 GdkDisplay *display;
3356 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3357 g_return_val_if_fail (event != NULL, FALSE);
3359 menu_shell = GTK_MENU_SHELL (widget);
3360 menu = GTK_MENU (widget);
3362 gtk_menu_stop_navigating_submenu (menu);
3364 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3367 display = gtk_widget_get_display (widget);
3369 g_object_get (gtk_widget_get_settings (widget),
3370 "gtk-menu-bar-accel", &accel,
3371 "gtk-can-change-accels", &can_change_accels,
3374 if (accel && *accel)
3377 GdkModifierType mods = 0;
3379 gtk_accelerator_parse (accel, &keyval, &mods);
3382 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3384 /* FIXME this is wrong, needs to be in the global accel resolution
3385 * thing, to properly consider i18n etc., but that probably requires
3386 * AccelGroup changes etc.
3388 if (event->keyval == keyval && (mods & event->state) == mods)
3390 gtk_menu_shell_cancel (menu_shell);
3398 switch (event->keyval)
3400 case GDK_KEY_Delete:
3401 case GDK_KEY_KP_Delete:
3402 case GDK_KEY_BackSpace:
3409 /* Figure out what modifiers went into determining the key symbol */
3410 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3411 event->hardware_keycode, event->state, event->group,
3412 NULL, NULL, NULL, &consumed_modifiers);
3414 accel_key = gdk_keyval_to_lower (event->keyval);
3415 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3417 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3418 * We re-upper case when we match against the keyval, but display and save in caseless form.
3420 if (accel_key != event->keyval)
3421 accel_mods |= GDK_SHIFT_MASK;
3423 /* Modify the accelerators */
3424 if (can_change_accels &&
3425 menu_shell->active_menu_item &&
3426 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3427 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3428 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3430 GtkWidget *menu_item = menu_shell->active_menu_item;
3431 gboolean locked, replace_accels = TRUE;
3434 path = get_accel_path (menu_item, &locked);
3435 if (!path || locked)
3437 /* can't change accelerators on menu_items without paths
3438 * (basically, those items are accelerator-locked).
3440 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3441 gtk_widget_error_bell (widget);
3447 /* For the keys that act to delete the current setting, we delete
3448 * the current setting if there is one, otherwise, we set the
3449 * key as the accelerator.
3455 if (gtk_accel_map_lookup_entry (path, &key) &&
3456 (key.accel_key || key.accel_mods))
3462 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3466 /* we failed, probably because this key is in use and
3469 /* g_print("failed to change\n"); */
3470 gtk_widget_error_bell (widget);
3479 check_threshold (GtkWidget *widget,
3488 ABS (start_x - x) > THRESHOLD ||
3489 ABS (start_y - y) > THRESHOLD;
3493 definitely_within_item (GtkWidget *widget,
3497 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3500 w = gdk_window_get_width (window);
3501 h = gdk_window_get_height (window);
3504 check_threshold (widget, 0, 0, x, y) &&
3505 check_threshold (widget, w - 1, 0, x, y) &&
3506 check_threshold (widget, w - 1, h - 1, x, y) &&
3507 check_threshold (widget, 0, h - 1, x, y);
3511 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3513 GtkMenuPrivate *priv;
3515 priv = gtk_menu_get_private (menu);
3517 return priv->navigation_height && priv->navigation_width;
3521 gtk_menu_motion_notify (GtkWidget *widget,
3522 GdkEventMotion *event)
3524 GtkWidget *menu_item;
3526 GtkMenuShell *menu_shell;
3529 gboolean need_enter;
3531 if (GTK_IS_MENU (widget))
3533 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3535 if (priv->ignore_button_release)
3536 priv->ignore_button_release = FALSE;
3538 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3542 /* We received the event for one of two reasons:
3544 * a) We are the active menu, and did gtk_grab_add()
3545 * b) The widget is a child of ours, and the event was propagated
3547 * Since for computation of navigation regions, we want the menu which
3548 * is the parent of the menu item, for a), we need to find that menu,
3549 * which may be different from 'widget'.
3551 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3552 parent = gtk_widget_get_parent (menu_item);
3553 if (!GTK_IS_MENU_ITEM (menu_item) ||
3554 !GTK_IS_MENU (parent))
3557 menu_shell = GTK_MENU_SHELL (parent);
3558 menu = GTK_MENU (menu_shell);
3560 if (definitely_within_item (menu_item, event->x, event->y))
3561 menu_shell->activate_time = 0;
3563 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3565 /* Check to see if we are within an active submenu's navigation region
3567 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3570 /* Make sure we pop down if we enter a non-selectable menu item, so we
3571 * don't show a submenu when the cursor is outside the stay-up triangle.
3573 if (!_gtk_menu_item_is_selectable (menu_item))
3575 /* We really want to deselect, but this gives the menushell code
3576 * a chance to do some bookkeeping about the menuitem.
3578 gtk_menu_shell_select_item (menu_shell, menu_item);
3584 /* The menu is now sensitive to enter events on its items, but
3585 * was previously sensitive. So we fake an enter event.
3587 menu_shell->ignore_enter = FALSE;
3589 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3590 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3592 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3595 send_event->crossing.window = g_object_ref (event->window);
3596 send_event->crossing.time = event->time;
3597 send_event->crossing.send_event = TRUE;
3598 send_event->crossing.x_root = event->x_root;
3599 send_event->crossing.y_root = event->y_root;
3600 send_event->crossing.x = event->x;
3601 send_event->crossing.y = event->y;
3602 send_event->crossing.state = event->state;
3603 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3605 /* We send the event to 'widget', the currently active menu,
3606 * instead of 'menu', the menu that the pointer is in. This
3607 * will ensure that the event will be ignored unless the
3608 * menuitem is a child of the active menu or some parent
3609 * menu of the active menu.
3611 result = gtk_widget_event (widget, send_event);
3612 gdk_event_free (send_event);
3622 get_double_arrows (GtkMenu *menu)
3624 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3625 gboolean double_arrows;
3626 GtkArrowPlacement arrow_placement;
3628 gtk_widget_style_get (GTK_WIDGET (menu),
3629 "double-arrows", &double_arrows,
3630 "arrow-placement", &arrow_placement,
3633 if (arrow_placement != GTK_ARROWS_BOTH)
3636 return double_arrows || (priv->initially_pushed_in &&
3637 menu->scroll_offset != 0);
3641 gtk_menu_scroll_by (GtkMenu *menu,
3644 GtkMenuPrivate *priv;
3645 GtkBorder arrow_border;
3649 gboolean double_arrows;
3651 widget = GTK_WIDGET (menu);
3652 offset = menu->scroll_offset + step;
3653 priv = gtk_menu_get_private (menu);
3655 get_arrows_border (menu, &arrow_border);
3657 double_arrows = get_double_arrows (menu);
3659 /* If we scroll upward and the non-visible top part
3660 * is smaller than the scroll arrow it would be
3661 * pretty stupid to show the arrow and taking more
3662 * screen space than just scrolling to the top.
3665 if ((step < 0) && (offset < arrow_border.top))
3668 /* Don't scroll over the top if we weren't before: */
3669 if ((menu->scroll_offset >= 0) && (offset < 0))
3672 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3674 if (menu->scroll_offset == 0 &&
3675 view_height >= priv->requested_height)
3678 /* Don't scroll past the bottom if we weren't before: */
3679 if (menu->scroll_offset > 0)
3680 view_height -= arrow_border.top;
3682 /* When both arrows are always shown, reduce
3683 * view height even more.
3686 view_height -= arrow_border.bottom;
3688 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3689 (offset + view_height > priv->requested_height))
3690 offset = priv->requested_height - view_height;
3692 if (offset != menu->scroll_offset)
3693 gtk_menu_scroll_to (menu, offset);
3697 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3698 gboolean touchscreen_mode)
3700 gboolean upper_visible;
3701 gboolean lower_visible;
3703 upper_visible = menu->upper_arrow_visible;
3704 lower_visible = menu->lower_arrow_visible;
3706 gtk_menu_scroll_by (menu, menu->scroll_step);
3708 if (touchscreen_mode &&
3709 (upper_visible != menu->upper_arrow_visible ||
3710 lower_visible != menu->lower_arrow_visible))
3712 /* We are about to hide a scroll arrow while the mouse is pressed,
3713 * this would cause the uncovered menu item to be activated on button
3714 * release. Therefore we need to ignore button release here
3716 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3717 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3722 gtk_menu_scroll_timeout (gpointer data)
3725 gboolean touchscreen_mode;
3727 menu = GTK_MENU (data);
3729 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3730 "gtk-touchscreen-mode", &touchscreen_mode,
3733 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3739 gtk_menu_scroll_timeout_initial (gpointer data)
3743 gboolean touchscreen_mode;
3745 menu = GTK_MENU (data);
3747 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3748 "gtk-timeout-repeat", &timeout,
3749 "gtk-touchscreen-mode", &touchscreen_mode,
3752 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3754 gtk_menu_remove_scroll_timeout (menu);
3756 menu->timeout_id = gdk_threads_add_timeout (timeout,
3757 gtk_menu_scroll_timeout,
3764 gtk_menu_start_scrolling (GtkMenu *menu)
3767 gboolean touchscreen_mode;
3769 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3770 "gtk-timeout-repeat", &timeout,
3771 "gtk-touchscreen-mode", &touchscreen_mode,
3774 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3776 menu->timeout_id = gdk_threads_add_timeout (timeout,
3777 gtk_menu_scroll_timeout_initial,
3782 gtk_menu_scroll (GtkWidget *widget,
3783 GdkEventScroll *event)
3785 GtkMenu *menu = GTK_MENU (widget);
3787 switch (event->direction)
3789 case GDK_SCROLL_RIGHT:
3790 case GDK_SCROLL_DOWN:
3791 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3793 case GDK_SCROLL_LEFT:
3795 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3803 get_arrows_sensitive_area (GtkMenu *menu,
3804 GdkRectangle *upper,
3805 GdkRectangle *lower)
3807 GtkArrowPlacement arrow_placement;
3808 GtkWidget *widget = GTK_WIDGET (menu);
3812 guint vertical_padding;
3814 gint scroll_arrow_height;
3816 window = gtk_widget_get_window (widget);
3817 width = gdk_window_get_width (window);
3818 height = gdk_window_get_height (window);
3820 gtk_widget_style_get (widget,
3821 "vertical-padding", &vertical_padding,
3822 "scroll-arrow-vlength", &scroll_arrow_height,
3823 "arrow-placement", &arrow_placement,
3826 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3827 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3829 gdk_window_get_position (window, &win_x, &win_y);
3831 switch (arrow_placement)
3833 case GTK_ARROWS_BOTH:
3838 upper->width = width;
3839 upper->height = scroll_arrow_height + border;
3845 lower->y = win_y + height - border - scroll_arrow_height;
3846 lower->width = width;
3847 lower->height = scroll_arrow_height + border;
3851 case GTK_ARROWS_START:
3856 upper->width = width / 2;
3857 upper->height = scroll_arrow_height + border;
3862 lower->x = win_x + width / 2;
3864 lower->width = width / 2;
3865 lower->height = scroll_arrow_height + border;
3869 case GTK_ARROWS_END:
3873 upper->y = win_y + height - border - scroll_arrow_height;
3874 upper->width = width / 2;
3875 upper->height = scroll_arrow_height + border;
3880 lower->x = win_x + width / 2;
3881 lower->y = win_y + height - border - scroll_arrow_height;
3882 lower->width = width / 2;
3883 lower->height = scroll_arrow_height + border;
3891 gtk_menu_handle_scrolling (GtkMenu *menu,
3897 GtkMenuShell *menu_shell;
3898 GtkMenuPrivate *priv;
3901 gboolean scroll_fast = FALSE;
3903 gboolean touchscreen_mode;
3905 priv = gtk_menu_get_private (menu);
3907 menu_shell = GTK_MENU_SHELL (menu);
3909 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3910 "gtk-touchscreen-mode", &touchscreen_mode,
3913 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3918 /* upper arrow handling */
3920 get_arrows_sensitive_area (menu, &rect, NULL);
3923 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3924 (x >= rect.x) && (x < rect.x + rect.width) &&
3925 (y >= rect.y) && (y < rect.y + rect.height))
3930 if (touchscreen_mode)
3931 menu->upper_arrow_prelight = in_arrow;
3933 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3935 gboolean arrow_pressed = FALSE;
3937 if (menu->upper_arrow_visible && !menu->tearoff_active)
3939 if (touchscreen_mode)
3941 if (enter && menu->upper_arrow_prelight)
3943 if (menu->timeout_id == 0)
3945 /* Deselect the active item so that
3946 * any submenus are popped down
3948 gtk_menu_shell_deselect (menu_shell);
3950 gtk_menu_remove_scroll_timeout (menu);
3951 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3955 /* Only do stuff on click. */
3956 gtk_menu_start_scrolling (menu);
3957 arrow_pressed = TRUE;
3962 arrow_pressed = TRUE;
3967 gtk_menu_stop_scrolling (menu);
3970 else /* !touchscreen_mode */
3972 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3974 if (enter && in_arrow &&
3975 (!menu->upper_arrow_prelight ||
3976 menu->scroll_fast != scroll_fast))
3978 menu->upper_arrow_prelight = TRUE;
3979 menu->scroll_fast = scroll_fast;
3981 /* Deselect the active item so that
3982 * any submenus are popped down
3984 gtk_menu_shell_deselect (menu_shell);
3986 gtk_menu_remove_scroll_timeout (menu);
3987 menu->scroll_step = scroll_fast ?
3988 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3991 gdk_threads_add_timeout (scroll_fast ?
3992 MENU_SCROLL_TIMEOUT2 :
3993 MENU_SCROLL_TIMEOUT1,
3994 gtk_menu_scroll_timeout, menu);
3996 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3998 gtk_menu_stop_scrolling (menu);
4003 /* gtk_menu_start_scrolling() might have hit the top of the
4004 * menu, so check if the button isn't insensitive before
4005 * changing it to something else.
4007 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4009 GtkStateType arrow_state = GTK_STATE_NORMAL;
4012 arrow_state = GTK_STATE_ACTIVE;
4013 else if (menu->upper_arrow_prelight)
4014 arrow_state = GTK_STATE_PRELIGHT;
4016 if (arrow_state != priv->upper_arrow_state)
4018 priv->upper_arrow_state = arrow_state;
4020 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4026 /* lower arrow handling */
4028 get_arrows_sensitive_area (menu, NULL, &rect);
4031 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4032 (x >= rect.x) && (x < rect.x + rect.width) &&
4033 (y >= rect.y) && (y < rect.y + rect.height))
4038 if (touchscreen_mode)
4039 menu->lower_arrow_prelight = in_arrow;
4041 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4043 gboolean arrow_pressed = FALSE;
4045 if (menu->lower_arrow_visible && !menu->tearoff_active)
4047 if (touchscreen_mode)
4049 if (enter && menu->lower_arrow_prelight)
4051 if (menu->timeout_id == 0)
4053 /* Deselect the active item so that
4054 * any submenus are popped down
4056 gtk_menu_shell_deselect (menu_shell);
4058 gtk_menu_remove_scroll_timeout (menu);
4059 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4063 /* Only do stuff on click. */
4064 gtk_menu_start_scrolling (menu);
4065 arrow_pressed = TRUE;
4070 arrow_pressed = TRUE;
4075 gtk_menu_stop_scrolling (menu);
4078 else /* !touchscreen_mode */
4080 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4082 if (enter && in_arrow &&
4083 (!menu->lower_arrow_prelight ||
4084 menu->scroll_fast != scroll_fast))
4086 menu->lower_arrow_prelight = TRUE;
4087 menu->scroll_fast = scroll_fast;
4089 /* Deselect the active item so that
4090 * any submenus are popped down
4092 gtk_menu_shell_deselect (menu_shell);
4094 gtk_menu_remove_scroll_timeout (menu);
4095 menu->scroll_step = scroll_fast ?
4096 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4099 gdk_threads_add_timeout (scroll_fast ?
4100 MENU_SCROLL_TIMEOUT2 :
4101 MENU_SCROLL_TIMEOUT1,
4102 gtk_menu_scroll_timeout, menu);
4104 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4106 gtk_menu_stop_scrolling (menu);
4111 /* gtk_menu_start_scrolling() might have hit the bottom of the
4112 * menu, so check if the button isn't insensitive before
4113 * changing it to something else.
4115 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4117 GtkStateType arrow_state = GTK_STATE_NORMAL;
4120 arrow_state = GTK_STATE_ACTIVE;
4121 else if (menu->lower_arrow_prelight)
4122 arrow_state = GTK_STATE_PRELIGHT;
4124 if (arrow_state != priv->lower_arrow_state)
4126 priv->lower_arrow_state = arrow_state;
4128 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4136 gtk_menu_enter_notify (GtkWidget *widget,
4137 GdkEventCrossing *event)
4139 GtkWidget *menu_item;
4141 gboolean touchscreen_mode;
4143 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4144 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4145 event->mode == GDK_CROSSING_STATE_CHANGED)
4148 g_object_get (gtk_widget_get_settings (widget),
4149 "gtk-touchscreen-mode", &touchscreen_mode,
4152 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4153 if (GTK_IS_MENU (widget))
4155 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4157 if (!menu_shell->ignore_enter)
4158 gtk_menu_handle_scrolling (GTK_MENU (widget),
4159 event->x_root, event->y_root, TRUE, TRUE);
4162 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4164 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4166 if (GTK_IS_MENU (menu))
4168 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4169 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4171 if (priv->seen_item_enter)
4173 /* This is the second enter we see for an item
4174 * on this menu. This means a release should always
4177 menu_shell->activate_time = 0;
4179 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4180 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4182 if (definitely_within_item (menu_item, event->x, event->y))
4184 /* This is an actual user-enter (ie. not a pop-under)
4185 * In this case, the user must either have entered
4186 * sufficiently far enough into the item, or he must move
4187 * far enough away from the enter point. (see
4188 * gtk_menu_motion_notify())
4190 menu_shell->activate_time = 0;
4194 priv->seen_item_enter = TRUE;
4198 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4199 * will not correspond to the event widget's parent. Check to see
4200 * if we are in the parent's navigation region.
4202 parent = gtk_widget_get_parent (menu_item);
4203 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4204 gtk_menu_navigating_submenu (GTK_MENU (parent),
4205 event->x_root, event->y_root))
4208 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4212 gtk_menu_leave_notify (GtkWidget *widget,
4213 GdkEventCrossing *event)
4215 GtkMenuShell *menu_shell;
4217 GtkMenuItem *menu_item;
4218 GtkWidget *event_widget;
4220 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4221 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4222 event->mode == GDK_CROSSING_STATE_CHANGED)
4225 menu = GTK_MENU (widget);
4226 menu_shell = GTK_MENU_SHELL (widget);
4228 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4231 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4233 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4235 if (!GTK_IS_MENU_ITEM (event_widget))
4238 menu_item = GTK_MENU_ITEM (event_widget);
4240 /* Here we check to see if we're leaving an active menu item with a submenu,
4241 * in which case we enter submenu navigation mode.
4243 if (menu_shell->active_menu_item != NULL
4244 && menu_item->submenu != NULL
4245 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4247 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4249 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4252 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4254 /* We are leaving an active menu item with nonactive submenu.
4255 * Deselect it so we don't surprise the user with by popping
4256 * up a submenu _after_ he left the item.
4258 gtk_menu_shell_deselect (menu_shell);
4263 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4267 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4269 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4271 priv->navigation_x = 0;
4272 priv->navigation_y = 0;
4273 priv->navigation_width = 0;
4274 priv->navigation_height = 0;
4276 if (menu->navigation_timeout)
4278 g_source_remove (menu->navigation_timeout);
4279 menu->navigation_timeout = 0;
4283 /* When the timeout is elapsed, the navigation region is destroyed
4284 * and the menuitem under the pointer (if any) is selected.
4287 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4289 GtkMenuPopdownData *popdown_data = user_data;
4290 GtkMenu *menu = popdown_data->menu;
4291 GdkWindow *child_window;
4293 gtk_menu_stop_navigating_submenu (menu);
4295 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4297 child_window = gdk_window_get_device_position (menu->bin_window,
4298 popdown_data->device,
4303 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4305 send_event->crossing.window = g_object_ref (child_window);
4306 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4307 send_event->crossing.send_event = TRUE;
4308 gdk_event_set_device (send_event, popdown_data->device);
4310 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4312 gdk_event_free (send_event);
4320 gtk_menu_navigating_submenu (GtkMenu *menu,
4324 GtkMenuPrivate *priv;
4327 if (!gtk_menu_has_navigation_triangle (menu))
4330 priv = gtk_menu_get_private (menu);
4331 width = priv->navigation_width;
4332 height = priv->navigation_height;
4334 /* check if x/y are in the triangle spanned by the navigation parameters */
4336 /* 1) Move the coordinates so the triangle starts at 0,0 */
4337 event_x -= priv->navigation_x;
4338 event_y -= priv->navigation_y;
4340 /* 2) Ensure both legs move along the positive axis */
4352 /* 3) Check that the given coordinate is inside the triangle. The formula
4353 * is a transformed form of this formula: x/w + y/h <= 1
4355 if (event_x >= 0 && event_y >= 0 &&
4356 event_x * height + event_y * width <= width * height)
4362 gtk_menu_stop_navigating_submenu (menu);
4368 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4369 GtkMenuItem *menu_item,
4370 GdkEventCrossing *event)
4372 gint submenu_left = 0;
4373 gint submenu_right = 0;
4374 gint submenu_top = 0;
4375 gint submenu_bottom = 0;
4378 GtkWidget *event_widget;
4379 GtkMenuPopdownData *popdown_data;
4380 GtkMenuPrivate *priv;
4383 g_return_if_fail (menu_item->submenu != NULL);
4384 g_return_if_fail (event != NULL);
4386 priv = gtk_menu_get_private (menu);
4388 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4390 window = gtk_widget_get_window (menu_item->submenu);
4391 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4393 submenu_right = submenu_left + gdk_window_get_width (window);
4394 submenu_bottom = submenu_top + gdk_window_get_height (window);
4396 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4397 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4399 if (event->x >= 0 && event->x < width)
4403 gtk_menu_stop_navigating_submenu (menu);
4405 /* The navigation region is the triangle closest to the x/y
4406 * location of the rectangle. This is why the width or height
4410 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4413 priv->navigation_x = submenu_left;
4414 priv->navigation_width = event->x_root - submenu_left;
4419 priv->navigation_x = submenu_right;
4420 priv->navigation_width = event->x_root - submenu_right;
4426 priv->navigation_y = event->y_root;
4427 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4429 if (priv->navigation_height >= 0)
4435 priv->navigation_y = event->y_root;
4436 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4438 if (priv->navigation_height <= 0)
4442 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4443 "gtk-menu-popdown-delay", &popdown_delay,
4446 popdown_data = g_new (GtkMenuPopdownData, 1);
4447 popdown_data->menu = menu;
4448 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4450 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4452 gtk_menu_stop_navigating_submenu_cb,
4454 (GDestroyNotify) g_free);
4459 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4463 g_return_if_fail (GTK_IS_MENU (menu_shell));
4465 parent = menu_shell->parent_menu_shell;
4467 menu_shell->activate_time = 0;
4468 gtk_menu_popdown (GTK_MENU (menu_shell));
4471 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4475 gtk_menu_position (GtkMenu *menu)
4478 GtkRequisition requisition;
4479 GtkMenuPrivate *private;
4484 GdkScreen *pointer_screen;
4485 GdkRectangle monitor;
4488 g_return_if_fail (GTK_IS_MENU (menu));
4490 widget = GTK_WIDGET (menu);
4492 screen = gtk_widget_get_screen (widget);
4493 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4494 gdk_display_get_device_state (gdk_screen_get_display (screen),
4495 pointer, &pointer_screen, &x, &y, NULL);
4497 /* Get the minimum height for minimum width to figure out
4498 * the right place to popup the menu.
4500 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4502 if (pointer_screen != screen)
4504 /* Pointer is on a different screen; roughly center the
4505 * menu on the screen. If someone was using multiscreen
4506 * + Xinerama together they'd probably want something
4507 * fancier; but that is likely to be vanishingly rare.
4509 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4510 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4513 private = gtk_menu_get_private (menu);
4514 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4516 private->initially_pushed_in = FALSE;
4518 /* Set the type hint here to allow custom position functions to set a different hint */
4519 if (!gtk_widget_get_visible (menu->toplevel))
4520 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4522 if (menu->position_func)
4524 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4525 menu->position_func_data);
4527 if (private->monitor_num < 0)
4528 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4530 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4534 GtkStyle *style = gtk_widget_get_style (widget);
4535 gint space_left, space_right, space_above, space_below;
4538 gint xthickness = style->xthickness;
4539 gint ythickness = style->ythickness;
4540 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4542 /* The placement of popup menus horizontally works like this (with
4543 * RTL in parentheses)
4545 * - If there is enough room to the right (left) of the mouse cursor,
4546 * position the menu there.
4548 * - Otherwise, if if there is enough room to the left (right) of the
4549 * mouse cursor, position the menu there.
4551 * - Otherwise if the menu is smaller than the monitor, position it
4552 * on the side of the mouse cursor that has the most space available
4554 * - Otherwise (if there is simply not enough room for the menu on the
4555 * monitor), position it as far left (right) as possible.
4557 * Positioning in the vertical direction is similar: first try below
4558 * mouse cursor, then above.
4560 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4562 space_left = x - monitor.x;
4563 space_right = monitor.x + monitor.width - x - 1;
4564 space_above = y - monitor.y;
4565 space_below = monitor.y + monitor.height - y - 1;
4567 /* position horizontally */
4569 /* the amount of space we need to position the menu. Note the
4570 * menu is offset "xthickness" pixels
4572 needed_width = requisition.width - xthickness;
4574 if (needed_width <= space_left ||
4575 needed_width <= space_right)
4577 if ((rtl && needed_width <= space_left) ||
4578 (!rtl && needed_width > space_right))
4581 x = x + xthickness - requisition.width + 1;
4585 /* position right */
4589 /* x is clamped on-screen further down */
4591 else if (requisition.width <= monitor.width)
4593 /* the menu is too big to fit on either side of the mouse
4594 * cursor, but smaller than the monitor. Position it on
4595 * the side that has the most space
4597 if (space_left > space_right)
4605 x = monitor.x + monitor.width - requisition.width;
4608 else /* menu is simply too big for the monitor */
4613 x = monitor.x + monitor.width - requisition.width;
4622 /* Position vertically. The algorithm is the same as above, but
4623 * simpler because we don't have to take RTL into account.
4625 needed_height = requisition.height - ythickness;
4627 if (needed_height <= space_above ||
4628 needed_height <= space_below)
4630 if (needed_height <= space_below)
4633 y = y + ythickness - requisition.height + 1;
4635 y = CLAMP (y, monitor.y,
4636 monitor.y + monitor.height - requisition.height);
4638 else if (needed_height > space_below && needed_height > space_above)
4640 if (space_below >= space_above)
4641 y = monitor.y + monitor.height - requisition.height;
4653 if (private->initially_pushed_in)
4655 menu_height = requisition.height;
4657 if (y + menu_height > monitor.y + monitor.height)
4659 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4660 y = (monitor.y + monitor.height) - menu_height;
4665 scroll_offset += monitor.y - y;
4670 /* FIXME: should this be done in the various position_funcs ? */
4671 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4673 if (GTK_MENU_SHELL (menu)->active)
4675 private->have_position = TRUE;
4680 if (y + requisition.height > monitor.y + monitor.height)
4681 requisition.height = (monitor.y + monitor.height) - y;
4685 scroll_offset += monitor.y - y;
4686 requisition.height -= monitor.y - y;
4690 if (scroll_offset > 0)
4692 GtkBorder arrow_border;
4694 get_arrows_border (menu, &arrow_border);
4695 scroll_offset += arrow_border.top;
4698 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4701 if (!GTK_MENU_SHELL (menu)->active)
4703 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4704 requisition.width, requisition.height);
4707 menu->scroll_offset = scroll_offset;
4711 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4713 if (menu->timeout_id)
4715 g_source_remove (menu->timeout_id);
4716 menu->timeout_id = 0;
4721 gtk_menu_stop_scrolling (GtkMenu *menu)
4723 gboolean touchscreen_mode;
4725 gtk_menu_remove_scroll_timeout (menu);
4727 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4728 "gtk-touchscreen-mode", &touchscreen_mode,
4731 if (!touchscreen_mode)
4733 menu->upper_arrow_prelight = FALSE;
4734 menu->lower_arrow_prelight = FALSE;
4739 gtk_menu_scroll_to (GtkMenu *menu,
4742 GtkMenuPrivate *priv;
4743 GtkAllocation allocation;
4744 GtkBorder arrow_border;
4748 gint view_width, view_height;
4751 guint vertical_padding;
4752 guint horizontal_padding;
4753 gboolean double_arrows;
4755 widget = GTK_WIDGET (menu);
4756 priv = gtk_menu_get_private (menu);
4758 if (menu->tearoff_active &&
4759 menu->tearoff_adjustment &&
4760 (menu->tearoff_adjustment->value != offset))
4762 menu->tearoff_adjustment->value =
4764 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4765 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4768 /* Move/resize the viewport according to arrows: */
4769 gtk_widget_get_allocation (widget, &allocation);
4770 view_width = allocation.width;
4771 view_height = allocation.height;
4773 style = gtk_widget_get_style (widget);
4775 gtk_widget_style_get (GTK_WIDGET (menu),
4776 "vertical-padding", &vertical_padding,
4777 "horizontal-padding", &horizontal_padding,
4780 double_arrows = get_double_arrows (menu);
4782 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4784 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4785 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4786 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4788 x = border_width + style->xthickness + horizontal_padding;
4789 y = border_width + style->ythickness + vertical_padding;
4791 if (double_arrows && !menu->tearoff_active)
4793 if (view_height < menu_height ||
4794 (offset > 0 && menu->scroll_offset > 0) ||
4795 (offset < 0 && menu->scroll_offset < 0))
4797 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4798 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4799 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4801 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4802 gtk_widget_queue_draw (GTK_WIDGET (menu));
4804 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4806 get_arrows_border (menu, &arrow_border);
4807 y += arrow_border.top;
4808 view_height -= arrow_border.top;
4809 view_height -= arrow_border.bottom;
4812 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4813 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4814 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4815 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4817 if (offset >= menu_height - view_height)
4818 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4819 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4820 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4821 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4823 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4824 (priv->lower_arrow_state != lower_arrow_previous_state))
4825 gtk_widget_queue_draw (GTK_WIDGET (menu));
4827 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4828 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4830 /* At the upper border, possibly remove timeout */
4831 if (menu->scroll_step < 0)
4833 gtk_menu_stop_scrolling (menu);
4834 gtk_widget_queue_draw (GTK_WIDGET (menu));
4838 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4839 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4841 /* At the lower border, possibly remove timeout */
4842 if (menu->scroll_step > 0)
4844 gtk_menu_stop_scrolling (menu);
4845 gtk_widget_queue_draw (GTK_WIDGET (menu));
4849 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4853 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4854 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4856 gtk_menu_stop_scrolling (menu);
4857 gtk_widget_queue_draw (GTK_WIDGET (menu));
4860 else if (!menu->tearoff_active)
4862 gboolean last_visible;
4864 last_visible = menu->upper_arrow_visible;
4865 menu->upper_arrow_visible = offset > 0;
4867 /* upper_arrow_visible may have changed, so requery the border */
4868 get_arrows_border (menu, &arrow_border);
4869 view_height -= arrow_border.top;
4871 if ((last_visible != menu->upper_arrow_visible) &&
4872 !menu->upper_arrow_visible)
4874 menu->upper_arrow_prelight = FALSE;
4876 /* If we hid the upper arrow, possibly remove timeout */
4877 if (menu->scroll_step < 0)
4879 gtk_menu_stop_scrolling (menu);
4880 gtk_widget_queue_draw (GTK_WIDGET (menu));
4884 last_visible = menu->lower_arrow_visible;
4885 menu->lower_arrow_visible = offset < menu_height - view_height;
4887 /* lower_arrow_visible may have changed, so requery the border */
4888 get_arrows_border (menu, &arrow_border);
4889 view_height -= arrow_border.bottom;
4891 if ((last_visible != menu->lower_arrow_visible) &&
4892 !menu->lower_arrow_visible)
4894 menu->lower_arrow_prelight = FALSE;
4896 /* If we hid the lower arrow, possibly remove timeout */
4897 if (menu->scroll_step > 0)
4899 gtk_menu_stop_scrolling (menu);
4900 gtk_widget_queue_draw (GTK_WIDGET (menu));
4904 y += arrow_border.top;
4907 /* Scroll the menu: */
4908 if (gtk_widget_get_realized (widget))
4909 gdk_window_move (menu->bin_window, 0, -offset);
4911 if (gtk_widget_get_realized (widget))
4912 gdk_window_move_resize (menu->view_window,
4918 menu->scroll_offset = offset;
4922 compute_child_offset (GtkMenu *menu,
4923 GtkWidget *menu_item,
4926 gboolean *is_last_child)
4928 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4929 gint item_top_attach;
4930 gint item_bottom_attach;
4931 gint child_offset = 0;
4934 get_effective_child_attach (menu_item, NULL, NULL,
4935 &item_top_attach, &item_bottom_attach);
4937 /* there is a possibility that we get called before _size_request, so
4938 * check the height table for safety.
4940 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4943 /* when we have a row with only invisible children, it's height will
4944 * be zero, so there's no need to check WIDGET_VISIBLE here
4946 for (i = 0; i < item_top_attach; i++)
4947 child_offset += priv->heights[i];
4950 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4952 *offset = child_offset;
4954 *height = priv->heights[item_top_attach];
4960 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4961 GtkWidget *menu_item)
4963 GtkMenu *menu = GTK_MENU (menu_shell);
4964 GtkWidget *widget = GTK_WIDGET (menu_shell);
4965 gint child_offset, child_height;
4969 gboolean last_child = 0;
4971 /* We need to check if the selected item fully visible.
4972 * If not we need to scroll the menu so that it becomes fully
4976 if (compute_child_offset (menu, menu_item,
4977 &child_offset, &child_height, &last_child))
4979 guint vertical_padding;
4980 gboolean double_arrows;
4982 y = menu->scroll_offset;
4983 width = gdk_window_get_width (gtk_widget_get_window (widget));
4984 height = gdk_window_get_height (gtk_widget_get_window (widget));
4986 gtk_widget_style_get (widget,
4987 "vertical-padding", &vertical_padding,
4990 double_arrows = get_double_arrows (menu);
4992 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
4993 2 * gtk_widget_get_style (widget)->ythickness +
4994 2 * vertical_padding;
4995 if (child_offset < y)
4997 /* Ignore the enter event we might get if the pointer is on the menu
4999 menu_shell->ignore_enter = TRUE;
5000 gtk_menu_scroll_to (menu, child_offset);
5004 GtkBorder arrow_border;
5008 get_arrows_border (menu, &arrow_border);
5009 if (!menu->tearoff_active)
5010 arrow_height = arrow_border.top + arrow_border.bottom;
5012 if (child_offset + child_height > y + height - arrow_height)
5015 if ((!last_child && !menu->tearoff_active) || double_arrows)
5016 arrow_height += arrow_border.bottom;
5018 y = child_offset + child_height - height + arrow_height;
5019 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5021 /* Need upper arrow */
5022 arrow_height += arrow_border.top;
5023 y = child_offset + child_height - height + arrow_height;
5025 /* Ignore the enter event we might get if the pointer is on the menu
5027 menu_shell->ignore_enter = TRUE;
5028 gtk_menu_scroll_to (menu, y);
5036 gtk_menu_select_item (GtkMenuShell *menu_shell,
5037 GtkWidget *menu_item)
5039 GtkMenu *menu = GTK_MENU (menu_shell);
5041 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5042 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5044 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5048 /* Reparent the menu, taking care of the refcounting
5050 * If unrealize is true we force a unrealize while reparenting the parent.
5051 * This can help eliminate flicker in some cases.
5053 * What happens is that when the menu is unrealized and then re-realized,
5054 * the allocations are as follows:
5056 * parent - 1x1 at (0,0)
5057 * child1 - 100x20 at (0,0)
5058 * child2 - 100x20 at (0,20)
5059 * child3 - 100x20 at (0,40)
5061 * That is, the parent is small but the children are full sized. Then,
5062 * when the queued_resize gets processed, the parent gets resized to
5065 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5066 * contains the following logic:
5068 * - if a move or resize operation on a window would change the clip
5069 * region on the children, then before the window is resized
5070 * the background for children is temporarily set to None, the
5071 * move/resize done, and the background for the children restored.
5073 * So, at the point where the parent is resized to final size, the
5074 * background for the children is temporarily None, and thus they
5075 * are not cleared to the background color and the previous background
5076 * (the image of the menu) is left in place.
5079 gtk_menu_reparent (GtkMenu *menu,
5080 GtkWidget *new_parent,
5083 GObject *object = G_OBJECT (menu);
5084 GtkWidget *widget = GTK_WIDGET (menu);
5085 gboolean was_floating = g_object_is_floating (object);
5087 g_object_ref_sink (object);
5091 g_object_ref (object);
5092 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5093 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5094 g_object_unref (object);
5097 gtk_widget_reparent (widget, new_parent);
5100 g_object_force_floating (object);
5102 g_object_unref (object);
5106 gtk_menu_show_all (GtkWidget *widget)
5108 /* Show children, but not self. */
5109 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5113 * gtk_menu_set_screen:
5114 * @menu: a #GtkMenu.
5115 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5116 * determined by the widget the menu is attached to.
5118 * Sets the #GdkScreen on which the menu will be displayed.
5123 gtk_menu_set_screen (GtkMenu *menu,
5126 g_return_if_fail (GTK_IS_MENU (menu));
5127 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5129 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5133 menu_change_screen (menu, screen);
5137 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5139 attach_widget_screen_changed (attach_widget, NULL, menu);
5145 * @menu: a #GtkMenu.
5146 * @child: a #GtkMenuItem.
5147 * @left_attach: The column number to attach the left side of the item to.
5148 * @right_attach: The column number to attach the right side of the item to.
5149 * @top_attach: The row number to attach the top of the item to.
5150 * @bottom_attach: The row number to attach the bottom of the item to.
5152 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5153 * an item will occupy is specified by @left_attach, @right_attach,
5154 * @top_attach and @bottom_attach. These each represent the leftmost,
5155 * rightmost, uppermost and lower column and row numbers of the table.
5156 * (Columns and rows are indexed from zero).
5158 * Note that this function is not related to gtk_menu_detach().
5163 gtk_menu_attach (GtkMenu *menu,
5168 guint bottom_attach)
5170 GtkMenuShell *menu_shell;
5173 g_return_if_fail (GTK_IS_MENU (menu));
5174 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5175 parent = gtk_widget_get_parent (child);
5176 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5177 g_return_if_fail (left_attach < right_attach);
5178 g_return_if_fail (top_attach < bottom_attach);
5180 menu_shell = GTK_MENU_SHELL (menu);
5184 AttachInfo *ai = get_attach_info (child);
5186 ai->left_attach = left_attach;
5187 ai->right_attach = right_attach;
5188 ai->top_attach = top_attach;
5189 ai->bottom_attach = bottom_attach;
5191 menu_shell->children = g_list_append (menu_shell->children, child);
5193 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5195 menu_queue_resize (menu);
5199 gtk_container_child_set (GTK_CONTAINER (parent), child,
5200 "left-attach", left_attach,
5201 "right-attach", right_attach,
5202 "top-attach", top_attach,
5203 "bottom-attach", bottom_attach,
5209 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5213 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5214 "gtk-menu-popup-delay", &popup_delay,
5221 find_child_containing (GtkMenuShell *menu_shell,
5229 /* find a child which includes the area given by
5230 * left, right, top, bottom.
5233 for (list = menu_shell->children; list; list = list->next)
5237 if (!_gtk_menu_item_is_selectable (list->data))
5240 get_effective_child_attach (list->data, &l, &r, &t, &b);
5242 if (l <= left && right <= r
5243 && t <= top && bottom <= b)
5244 return GTK_WIDGET (list->data);
5251 gtk_menu_move_current (GtkMenuShell *menu_shell,
5252 GtkMenuDirectionType direction)
5254 GtkMenu *menu = GTK_MENU (menu_shell);
5257 GtkWidget *match = NULL;
5259 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5263 case GTK_MENU_DIR_CHILD:
5264 direction = GTK_MENU_DIR_PARENT;
5266 case GTK_MENU_DIR_PARENT:
5267 direction = GTK_MENU_DIR_CHILD;
5273 /* use special table menu key bindings */
5274 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5276 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5278 if (direction == GTK_MENU_DIR_NEXT)
5280 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5282 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5290 for (i = 0; i < t; i++)
5292 match = find_child_containing (menu_shell,
5293 l, l + 1, i, i + 1);
5299 else if (direction == GTK_MENU_DIR_PREV)
5301 for (i = t; i > 0; i--)
5303 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5311 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5313 match = find_child_containing (menu_shell,
5314 l, l + 1, i - 1, i);
5320 else if (direction == GTK_MENU_DIR_PARENT)
5322 /* we go one left if possible */
5324 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5328 GtkWidget *parent = menu_shell->parent_menu_shell;
5331 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5332 match = menu_shell->active_menu_item;
5335 else if (direction == GTK_MENU_DIR_CHILD)
5337 /* we go one right if possible */
5338 if (r < gtk_menu_get_n_columns (menu))
5339 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5343 GtkWidget *parent = menu_shell->parent_menu_shell;
5345 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5347 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5348 match = menu_shell->active_menu_item;
5354 gtk_menu_shell_select_item (menu_shell, match);
5359 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5363 get_visible_size (GtkMenu *menu)
5365 GtkAllocation allocation;
5366 GtkWidget *widget = GTK_WIDGET (menu);
5367 GtkContainer *container = GTK_CONTAINER (menu);
5370 gtk_widget_get_allocation (widget, &allocation);
5371 menu_height = (allocation.height
5372 - 2 * (gtk_container_get_border_width (container)
5373 + gtk_widget_get_style (widget)->ythickness));
5375 if (!menu->tearoff_active)
5377 GtkBorder arrow_border;
5379 get_arrows_border (menu, &arrow_border);
5380 menu_height -= arrow_border.top;
5381 menu_height -= arrow_border.bottom;
5387 /* Find the sensitive on-screen child containing @y, or if none,
5388 * the nearest selectable onscreen child. (%NULL if none)
5391 child_at (GtkMenu *menu,
5394 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5395 GtkWidget *child = NULL;
5396 gint child_offset = 0;
5399 gint lower, upper; /* Onscreen bounds */
5401 menu_height = get_visible_size (menu);
5402 lower = menu->scroll_offset;
5403 upper = menu->scroll_offset + menu_height;
5405 for (children = menu_shell->children; children; children = children->next)
5407 if (gtk_widget_get_visible (children->data))
5409 GtkRequisition child_requisition;
5411 gtk_widget_get_preferred_size (children->data,
5412 &child_requisition, NULL);
5414 if (_gtk_menu_item_is_selectable (children->data) &&
5415 child_offset >= lower &&
5416 child_offset + child_requisition.height <= upper)
5418 child = children->data;
5420 if (child_offset + child_requisition.height > y &&
5421 !GTK_IS_TEAROFF_MENU_ITEM (child))
5425 child_offset += child_requisition.height;
5433 get_menu_height (GtkMenu *menu)
5435 GtkAllocation allocation;
5436 GtkWidget *widget = GTK_WIDGET (menu);
5439 gtk_widget_get_allocation (widget, &allocation);
5441 height = allocation.height;
5442 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) + gtk_widget_get_style (widget)->ythickness) * 2;
5444 if (!menu->tearoff_active)
5446 GtkBorder arrow_border;
5448 get_arrows_border (menu, &arrow_border);
5449 height -= arrow_border.top;
5450 height -= arrow_border.bottom;
5457 gtk_menu_real_move_scroll (GtkMenu *menu,
5460 gint page_size = get_visible_size (menu);
5461 gint end_position = get_menu_height (menu);
5462 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5466 case GTK_SCROLL_PAGE_UP:
5467 case GTK_SCROLL_PAGE_DOWN:
5471 gint child_offset = 0;
5472 gboolean old_upper_arrow_visible;
5475 if (type == GTK_SCROLL_PAGE_UP)
5480 if (menu_shell->active_menu_item)
5484 compute_child_offset (menu, menu_shell->active_menu_item,
5485 &child_offset, &child_height, NULL);
5486 child_offset += child_height / 2;
5489 menu_shell->ignore_enter = TRUE;
5490 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5491 old_offset = menu->scroll_offset;
5493 new_offset = menu->scroll_offset + step;
5494 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5496 gtk_menu_scroll_to (menu, new_offset);
5498 if (menu_shell->active_menu_item)
5500 GtkWidget *new_child;
5501 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5502 GtkBorder arrow_border;
5504 get_arrows_border (menu, &arrow_border);
5506 if (menu->scroll_offset != old_offset)
5507 step = menu->scroll_offset - old_offset;
5509 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5511 new_child = child_at (menu, child_offset + step);
5513 gtk_menu_shell_select_item (menu_shell, new_child);
5517 case GTK_SCROLL_START:
5518 /* Ignore the enter event we might get if the pointer is on the menu
5520 menu_shell->ignore_enter = TRUE;
5521 gtk_menu_scroll_to (menu, 0);
5522 gtk_menu_shell_select_first (menu_shell, TRUE);
5524 case GTK_SCROLL_END:
5525 /* Ignore the enter event we might get if the pointer is on the menu
5527 menu_shell->ignore_enter = TRUE;
5528 gtk_menu_scroll_to (menu, end_position - page_size);
5529 _gtk_menu_shell_select_last (menu_shell, TRUE);
5538 * gtk_menu_set_monitor:
5540 * @monitor_num: the number of the monitor on which the menu should
5543 * Informs GTK+ on which monitor a menu should be popped up.
5544 * See gdk_screen_get_monitor_geometry().
5546 * This function should be called from a #GtkMenuPositionFunc if the
5547 * menu should not appear on the same monitor as the pointer. This
5548 * information can't be reliably inferred from the coordinates returned
5549 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5550 * may extend beyond the monitor boundaries or even the screen boundaries.
5555 gtk_menu_set_monitor (GtkMenu *menu,
5558 GtkMenuPrivate *priv;
5559 g_return_if_fail (GTK_IS_MENU (menu));
5561 priv = gtk_menu_get_private (menu);
5563 priv->monitor_num = monitor_num;
5567 * gtk_menu_get_monitor:
5570 * Retrieves the number of the monitor on which to show the menu.
5572 * Returns: the number of the monitor on which the menu should
5573 * be popped up or -1, if no monitor has been set
5578 gtk_menu_get_monitor (GtkMenu *menu)
5580 GtkMenuPrivate *priv;
5581 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5583 priv = gtk_menu_get_private (menu);
5585 return priv->monitor_num;
5589 * gtk_menu_get_for_attach_widget:
5590 * @widget: a #GtkWidget
5592 * Returns a list of the menus which are attached to this widget.
5593 * This list is owned by GTK+ and must not be modified.
5595 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5600 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5604 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5606 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5611 gtk_menu_grab_notify (GtkWidget *widget,
5612 gboolean was_grabbed)
5614 GtkWidget *toplevel;
5615 GtkWindowGroup *group;
5619 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5622 !gtk_widget_device_is_shadowed (widget, pointer))
5625 toplevel = gtk_widget_get_toplevel (widget);
5627 if (!GTK_IS_WINDOW (toplevel))
5630 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5631 grab = gtk_window_group_get_current_device_grab (group, pointer);
5633 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5634 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5638 * gtk_menu_set_reserve_toggle_size:
5640 * @reserve_toggle_size: whether to reserve size for toggles
5642 * Sets whether the menu should reserve space for drawing toggles
5643 * or icons, regardless of their actual presence.
5648 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5649 gboolean reserve_toggle_size)
5651 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5652 gboolean no_toggle_size;
5654 no_toggle_size = !reserve_toggle_size;
5656 if (priv->no_toggle_size != no_toggle_size)
5658 priv->no_toggle_size = no_toggle_size;
5660 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5665 * gtk_menu_get_reserve_toggle_size:
5668 * Returns whether the menu reserves space for toggles and
5669 * icons, regardless of their actual presence.
5671 * Returns: Whether the menu reserves toggle space
5676 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5678 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5680 return !priv->no_toggle_size;