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/.
31 #include <gobject/gvaluecollector.h>
33 #include "gtkaccellabel.h"
34 #include "gtkaccelmap.h"
35 #include "gtkbindings.h"
36 #include "gtkcheckmenuitem.h"
38 #include "gtkmarshalers.h"
39 #include "gtkmenuprivate.h"
40 #include "gtkmenuitemprivate.h"
41 #include "gtkmenushellprivate.h"
42 #include "gtktearoffmenuitem.h"
43 #include "gtkwindow.h"
45 #include "gtkvscrollbar.h"
46 #include "gtksettings.h"
47 #include "gtkprivate.h"
49 #include "gtktypebuiltins.h"
51 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
52 * extends below the submenu
55 #define MENU_SCROLL_STEP1 8
56 #define MENU_SCROLL_STEP2 15
57 #define MENU_SCROLL_FAST_ZONE 8
58 #define MENU_SCROLL_TIMEOUT1 50
59 #define MENU_SCROLL_TIMEOUT2 20
61 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
62 #define ATTACHED_MENUS "gtk-attached-menus"
64 typedef struct _GtkMenuAttachData GtkMenuAttachData;
65 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
67 struct _GtkMenuAttachData
69 GtkWidget *attach_widget;
70 GtkMenuDetachFunc detacher;
73 struct _OldGtkMenuPrivate
77 gboolean initially_pushed_in;
79 GDestroyNotify position_func_data_destroy;
81 /* info used for the table */
84 gint requested_height;
88 /* Cached layout information */
97 GtkStateFlags lower_arrow_state;
98 GtkStateFlags upper_arrow_state;
100 /* navigation region */
103 int navigation_width;
104 int navigation_height;
106 guint have_layout : 1;
107 guint seen_item_enter : 1;
108 guint have_position : 1;
109 guint ignore_button_release : 1;
110 guint no_toggle_size : 1;
113 struct _GtkMenuPopdownData
125 gint effective_left_attach;
126 gint effective_right_attach;
127 gint effective_top_attach;
128 gint effective_bottom_attach;
145 PROP_RESERVE_TOGGLE_SIZE
150 CHILD_PROP_LEFT_ATTACH,
151 CHILD_PROP_RIGHT_ATTACH,
152 CHILD_PROP_TOP_ATTACH,
153 CHILD_PROP_BOTTOM_ATTACH
156 static void gtk_menu_set_property (GObject *object,
160 static void gtk_menu_get_property (GObject *object,
164 static void gtk_menu_set_child_property(GtkContainer *container,
169 static void gtk_menu_get_child_property(GtkContainer *container,
174 static void gtk_menu_destroy (GtkWidget *widget);
175 static void gtk_menu_realize (GtkWidget *widget);
176 static void gtk_menu_unrealize (GtkWidget *widget);
177 static void gtk_menu_size_allocate (GtkWidget *widget,
178 GtkAllocation *allocation);
179 static void gtk_menu_show (GtkWidget *widget);
180 static gboolean gtk_menu_draw (GtkWidget *widget,
182 static gboolean gtk_menu_key_press (GtkWidget *widget,
184 static gboolean gtk_menu_scroll (GtkWidget *widget,
185 GdkEventScroll *event);
186 static gboolean gtk_menu_button_press (GtkWidget *widget,
187 GdkEventButton *event);
188 static gboolean gtk_menu_button_release (GtkWidget *widget,
189 GdkEventButton *event);
190 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
191 GdkEventMotion *event);
192 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
193 GdkEventCrossing *event);
194 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
195 GdkEventCrossing *event);
196 static void gtk_menu_scroll_to (GtkMenu *menu,
198 static void gtk_menu_grab_notify (GtkWidget *widget,
199 gboolean was_grabbed);
201 static void gtk_menu_stop_scrolling (GtkMenu *menu);
202 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
203 static gboolean gtk_menu_scroll_timeout (gpointer data);
204 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
205 static void gtk_menu_start_scrolling (GtkMenu *menu);
207 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
208 GtkWidget *menu_item);
209 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
210 GtkWidget *menu_item);
211 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
214 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
216 static void gtk_menu_handle_scrolling (GtkMenu *menu,
221 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
223 static void gtk_menu_style_updated (GtkWidget *widget);
224 static gboolean gtk_menu_focus (GtkWidget *widget,
225 GtkDirectionType direction);
226 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
227 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
228 GtkMenuDirectionType direction);
229 static void gtk_menu_real_move_scroll (GtkMenu *menu,
232 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
233 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
234 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
237 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
238 GtkMenuItem *menu_item,
239 GdkEventCrossing *event);
241 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
242 static void gtk_menu_show_all (GtkWidget *widget);
243 static void gtk_menu_position (GtkMenu *menu,
244 gboolean set_scroll_offset);
245 static void gtk_menu_reparent (GtkMenu *menu,
246 GtkWidget *new_parent,
248 static void gtk_menu_remove (GtkContainer *menu,
251 static void gtk_menu_update_title (GtkMenu *menu);
253 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
254 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
256 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
258 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
259 gboolean group_changed);
261 static void gtk_menu_get_preferred_width (GtkWidget *widget,
264 static void gtk_menu_get_preferred_height (GtkWidget *widget,
267 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
273 static const gchar attach_data_key[] = "gtk-menu-attach-data";
275 static guint menu_signals[LAST_SIGNAL] = { 0 };
277 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
280 menu_queue_resize (GtkMenu *menu)
282 GtkMenuPrivate *priv = menu->priv;
284 priv->have_layout = FALSE;
285 gtk_widget_queue_resize (GTK_WIDGET (menu));
289 attach_info_free (AttachInfo *info)
291 g_slice_free (AttachInfo, info);
295 get_attach_info (GtkWidget *child)
297 GObject *object = G_OBJECT (child);
298 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
302 ai = g_slice_new0 (AttachInfo);
303 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
304 (GDestroyNotify) attach_info_free);
311 is_grid_attached (AttachInfo *ai)
313 return (ai->left_attach >= 0 &&
314 ai->right_attach >= 0 &&
315 ai->top_attach >= 0 &&
316 ai->bottom_attach >= 0);
320 menu_ensure_layout (GtkMenu *menu)
322 GtkMenuPrivate *priv = menu->priv;
324 if (!priv->have_layout)
326 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
330 gint max_right_attach;
331 gint max_bottom_attach;
333 /* Find extents of gridded portion
335 max_right_attach = 1;
336 max_bottom_attach = 0;
338 for (l = menu_shell->priv->children; l; l = l->next)
340 GtkWidget *child = l->data;
341 AttachInfo *ai = get_attach_info (child);
343 if (is_grid_attached (ai))
345 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
346 max_right_attach = MAX (max_right_attach, ai->right_attach);
350 /* Find empty rows */
351 row_occupied = g_malloc0 (max_bottom_attach);
353 for (l = menu_shell->priv->children; l; l = l->next)
355 GtkWidget *child = l->data;
356 AttachInfo *ai = get_attach_info (child);
358 if (is_grid_attached (ai))
362 for (i = ai->top_attach; i < ai->bottom_attach; i++)
363 row_occupied[i] = TRUE;
367 /* Lay non-grid-items out in those rows
370 for (l = menu_shell->priv->children; l; l = l->next)
372 GtkWidget *child = l->data;
373 AttachInfo *ai = get_attach_info (child);
375 if (!is_grid_attached (ai))
377 while (current_row < max_bottom_attach && row_occupied[current_row])
380 ai->effective_left_attach = 0;
381 ai->effective_right_attach = max_right_attach;
382 ai->effective_top_attach = current_row;
383 ai->effective_bottom_attach = current_row + 1;
389 ai->effective_left_attach = ai->left_attach;
390 ai->effective_right_attach = ai->right_attach;
391 ai->effective_top_attach = ai->top_attach;
392 ai->effective_bottom_attach = ai->bottom_attach;
396 g_free (row_occupied);
398 priv->n_rows = MAX (current_row, max_bottom_attach);
399 priv->n_columns = max_right_attach;
400 priv->have_layout = TRUE;
406 gtk_menu_get_n_columns (GtkMenu *menu)
408 GtkMenuPrivate *priv = menu->priv;
410 menu_ensure_layout (menu);
412 return priv->n_columns;
416 gtk_menu_get_n_rows (GtkMenu *menu)
418 GtkMenuPrivate *priv = menu->priv;
420 menu_ensure_layout (menu);
426 get_effective_child_attach (GtkWidget *child,
432 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
435 menu_ensure_layout (menu);
437 ai = get_attach_info (child);
440 *l = ai->effective_left_attach;
442 *r = ai->effective_right_attach;
444 *t = ai->effective_top_attach;
446 *b = ai->effective_bottom_attach;
451 gtk_menu_class_init (GtkMenuClass *class)
453 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
454 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
455 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
456 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
457 GtkBindingSet *binding_set;
459 gobject_class->set_property = gtk_menu_set_property;
460 gobject_class->get_property = gtk_menu_get_property;
462 widget_class->destroy = gtk_menu_destroy;
463 widget_class->realize = gtk_menu_realize;
464 widget_class->unrealize = gtk_menu_unrealize;
465 widget_class->size_allocate = gtk_menu_size_allocate;
466 widget_class->show = gtk_menu_show;
467 widget_class->draw = gtk_menu_draw;
468 widget_class->scroll_event = gtk_menu_scroll;
469 widget_class->key_press_event = gtk_menu_key_press;
470 widget_class->button_press_event = gtk_menu_button_press;
471 widget_class->button_release_event = gtk_menu_button_release;
472 widget_class->motion_notify_event = gtk_menu_motion_notify;
473 widget_class->show_all = gtk_menu_show_all;
474 widget_class->enter_notify_event = gtk_menu_enter_notify;
475 widget_class->leave_notify_event = gtk_menu_leave_notify;
476 widget_class->style_updated = gtk_menu_style_updated;
477 widget_class->focus = gtk_menu_focus;
478 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
479 widget_class->grab_notify = gtk_menu_grab_notify;
480 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
481 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
482 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
484 container_class->remove = gtk_menu_remove;
485 container_class->get_child_property = gtk_menu_get_child_property;
486 container_class->set_child_property = gtk_menu_set_child_property;
488 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
489 menu_shell_class->deactivate = gtk_menu_deactivate;
490 menu_shell_class->select_item = gtk_menu_select_item;
491 menu_shell_class->insert = gtk_menu_real_insert;
492 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
493 menu_shell_class->move_current = gtk_menu_move_current;
495 menu_signals[MOVE_SCROLL] =
496 g_signal_new_class_handler (I_("move-scroll"),
497 G_OBJECT_CLASS_TYPE (gobject_class),
498 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
499 G_CALLBACK (gtk_menu_real_move_scroll),
501 _gtk_marshal_VOID__ENUM,
503 GTK_TYPE_SCROLL_TYPE);
508 * The index of the currently selected menu item, or -1 if no
509 * menu item is selected.
513 g_object_class_install_property (gobject_class,
515 g_param_spec_int ("active",
517 P_("The currently selected menu item"),
519 GTK_PARAM_READWRITE));
522 * GtkMenu:accel-group:
524 * The accel group holding accelerators for the menu.
528 g_object_class_install_property (gobject_class,
530 g_param_spec_object ("accel-group",
532 P_("The accel group holding accelerators for the menu"),
533 GTK_TYPE_ACCEL_GROUP,
534 GTK_PARAM_READWRITE));
537 * GtkMenu:accel-path:
539 * An accel path used to conveniently construct accel paths of child items.
543 g_object_class_install_property (gobject_class,
545 g_param_spec_string ("accel-path",
547 P_("An accel path used to conveniently construct accel paths of child items"),
549 GTK_PARAM_READWRITE));
552 * GtkMenu:attach-widget:
554 * The widget the menu is attached to. Setting this property attaches
555 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
556 * use gtk_menu_attach_to_widget() directly.
560 g_object_class_install_property (gobject_class,
562 g_param_spec_object ("attach-widget",
564 P_("The widget the menu is attached to"),
566 GTK_PARAM_READWRITE));
568 g_object_class_install_property (gobject_class,
570 g_param_spec_string ("tearoff-title",
572 P_("A title that may be displayed by the window manager when this menu is torn-off"),
574 GTK_PARAM_READWRITE));
577 * GtkMenu:tearoff-state:
579 * A boolean that indicates whether the menu is torn-off.
583 g_object_class_install_property (gobject_class,
585 g_param_spec_boolean ("tearoff-state",
587 P_("A boolean that indicates whether the menu is torn-off"),
589 GTK_PARAM_READWRITE));
594 * The monitor the menu will be popped up on.
598 g_object_class_install_property (gobject_class,
600 g_param_spec_int ("monitor",
602 P_("The monitor the menu will be popped up on"),
604 GTK_PARAM_READWRITE));
606 gtk_widget_class_install_style_property (widget_class,
607 g_param_spec_int ("vertical-padding",
608 P_("Vertical Padding"),
609 P_("Extra space at the top and bottom of the menu"),
613 GTK_PARAM_READABLE));
616 * GtkMenu:reserve-toggle-size:
618 * A boolean that indicates whether the menu reserves space for
619 * toggles and icons, regardless of their actual presence.
621 * This property should only be changed from its default value
622 * for special-purposes such as tabular menus. Regular menus that
623 * are connected to a menu bar or context menus should reserve
624 * toggle space for consistency.
628 g_object_class_install_property (gobject_class,
629 PROP_RESERVE_TOGGLE_SIZE,
630 g_param_spec_boolean ("reserve-toggle-size",
631 P_("Reserve Toggle Size"),
632 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
634 GTK_PARAM_READWRITE));
636 gtk_widget_class_install_style_property (widget_class,
637 g_param_spec_int ("horizontal-padding",
638 P_("Horizontal Padding"),
639 P_("Extra space at the left and right edges of the menu"),
643 GTK_PARAM_READABLE));
645 gtk_widget_class_install_style_property (widget_class,
646 g_param_spec_int ("vertical-offset",
647 P_("Vertical Offset"),
648 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
652 GTK_PARAM_READABLE));
654 gtk_widget_class_install_style_property (widget_class,
655 g_param_spec_int ("horizontal-offset",
656 P_("Horizontal Offset"),
657 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
661 GTK_PARAM_READABLE));
663 gtk_widget_class_install_style_property (widget_class,
664 g_param_spec_boolean ("double-arrows",
666 P_("When scrolling, always show both arrows."),
668 GTK_PARAM_READABLE));
671 * GtkMenu:arrow-placement:
673 * Indicates where scroll arrows should be placed.
677 gtk_widget_class_install_style_property (widget_class,
678 g_param_spec_enum ("arrow-placement",
679 P_("Arrow Placement"),
680 P_("Indicates where scroll arrows should be placed"),
681 GTK_TYPE_ARROW_PLACEMENT,
683 GTK_PARAM_READABLE));
685 gtk_container_class_install_child_property (container_class,
686 CHILD_PROP_LEFT_ATTACH,
687 g_param_spec_int ("left-attach",
689 P_("The column number to attach the left side of the child to"),
691 GTK_PARAM_READWRITE));
693 gtk_container_class_install_child_property (container_class,
694 CHILD_PROP_RIGHT_ATTACH,
695 g_param_spec_int ("right-attach",
697 P_("The column number to attach the right side of the child to"),
699 GTK_PARAM_READWRITE));
701 gtk_container_class_install_child_property (container_class,
702 CHILD_PROP_TOP_ATTACH,
703 g_param_spec_int ("top-attach",
705 P_("The row number to attach the top of the child to"),
707 GTK_PARAM_READWRITE));
709 gtk_container_class_install_child_property (container_class,
710 CHILD_PROP_BOTTOM_ATTACH,
711 g_param_spec_int ("bottom-attach",
713 P_("The row number to attach the bottom of the child to"),
715 GTK_PARAM_READWRITE));
718 * GtkMenu::arrow-scaling
720 * Arbitrary constant to scale down the size of the scroll arrow.
724 gtk_widget_class_install_style_property (widget_class,
725 g_param_spec_float ("arrow-scaling",
727 P_("Arbitrary constant to scale down the size of the scroll arrow"),
729 GTK_PARAM_READABLE));
731 binding_set = gtk_binding_set_by_class (class);
732 gtk_binding_entry_add_signal (binding_set,
734 I_("move-current"), 1,
735 GTK_TYPE_MENU_DIRECTION_TYPE,
737 gtk_binding_entry_add_signal (binding_set,
740 GTK_TYPE_MENU_DIRECTION_TYPE,
742 gtk_binding_entry_add_signal (binding_set,
745 GTK_TYPE_MENU_DIRECTION_TYPE,
747 gtk_binding_entry_add_signal (binding_set,
750 GTK_TYPE_MENU_DIRECTION_TYPE,
752 gtk_binding_entry_add_signal (binding_set,
755 GTK_TYPE_MENU_DIRECTION_TYPE,
756 GTK_MENU_DIR_PARENT);
757 gtk_binding_entry_add_signal (binding_set,
760 GTK_TYPE_MENU_DIRECTION_TYPE,
761 GTK_MENU_DIR_PARENT);
762 gtk_binding_entry_add_signal (binding_set,
765 GTK_TYPE_MENU_DIRECTION_TYPE,
767 gtk_binding_entry_add_signal (binding_set,
770 GTK_TYPE_MENU_DIRECTION_TYPE,
772 gtk_binding_entry_add_signal (binding_set,
775 GTK_TYPE_SCROLL_TYPE,
777 gtk_binding_entry_add_signal (binding_set,
780 GTK_TYPE_SCROLL_TYPE,
782 gtk_binding_entry_add_signal (binding_set,
785 GTK_TYPE_SCROLL_TYPE,
787 gtk_binding_entry_add_signal (binding_set,
790 GTK_TYPE_SCROLL_TYPE,
792 gtk_binding_entry_add_signal (binding_set,
795 GTK_TYPE_SCROLL_TYPE,
797 gtk_binding_entry_add_signal (binding_set,
798 GDK_KEY_KP_Page_Up, 0,
800 GTK_TYPE_SCROLL_TYPE,
802 gtk_binding_entry_add_signal (binding_set,
803 GDK_KEY_Page_Down, 0,
805 GTK_TYPE_SCROLL_TYPE,
806 GTK_SCROLL_PAGE_DOWN);
807 gtk_binding_entry_add_signal (binding_set,
808 GDK_KEY_KP_Page_Down, 0,
810 GTK_TYPE_SCROLL_TYPE,
811 GTK_SCROLL_PAGE_DOWN);
813 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
818 gtk_menu_set_property (GObject *object,
823 GtkMenu *menu = GTK_MENU (object);
828 gtk_menu_set_active (menu, g_value_get_int (value));
830 case PROP_ACCEL_GROUP:
831 gtk_menu_set_accel_group (menu, g_value_get_object (value));
833 case PROP_ACCEL_PATH:
834 gtk_menu_set_accel_path (menu, g_value_get_string (value));
836 case PROP_ATTACH_WIDGET:
840 widget = gtk_menu_get_attach_widget (menu);
842 gtk_menu_detach (menu);
844 widget = (GtkWidget*) g_value_get_object (value);
846 gtk_menu_attach_to_widget (menu, widget, NULL);
849 case PROP_TEAROFF_STATE:
850 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
852 case PROP_TEAROFF_TITLE:
853 gtk_menu_set_title (menu, g_value_get_string (value));
856 gtk_menu_set_monitor (menu, g_value_get_int (value));
858 case PROP_RESERVE_TOGGLE_SIZE:
859 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
862 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
868 gtk_menu_get_property (GObject *object,
873 GtkMenu *menu = GTK_MENU (object);
878 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
880 case PROP_ACCEL_GROUP:
881 g_value_set_object (value, gtk_menu_get_accel_group (menu));
883 case PROP_ACCEL_PATH:
884 g_value_set_string (value, gtk_menu_get_accel_path (menu));
886 case PROP_ATTACH_WIDGET:
887 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
889 case PROP_TEAROFF_STATE:
890 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
892 case PROP_TEAROFF_TITLE:
893 g_value_set_string (value, gtk_menu_get_title (menu));
896 g_value_set_int (value, gtk_menu_get_monitor (menu));
898 case PROP_RESERVE_TOGGLE_SIZE:
899 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
902 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
908 gtk_menu_set_child_property (GtkContainer *container,
914 GtkMenu *menu = GTK_MENU (container);
915 AttachInfo *ai = get_attach_info (child);
919 case CHILD_PROP_LEFT_ATTACH:
920 ai->left_attach = g_value_get_int (value);
922 case CHILD_PROP_RIGHT_ATTACH:
923 ai->right_attach = g_value_get_int (value);
925 case CHILD_PROP_TOP_ATTACH:
926 ai->top_attach = g_value_get_int (value);
928 case CHILD_PROP_BOTTOM_ATTACH:
929 ai->bottom_attach = g_value_get_int (value);
933 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
937 menu_queue_resize (menu);
941 gtk_menu_get_child_property (GtkContainer *container,
947 AttachInfo *ai = get_attach_info (child);
951 case CHILD_PROP_LEFT_ATTACH:
952 g_value_set_int (value, ai->left_attach);
954 case CHILD_PROP_RIGHT_ATTACH:
955 g_value_set_int (value, ai->right_attach);
957 case CHILD_PROP_TOP_ATTACH:
958 g_value_set_int (value, ai->top_attach);
960 case CHILD_PROP_BOTTOM_ATTACH:
961 g_value_set_int (value, ai->bottom_attach);
965 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
971 gtk_menu_window_event (GtkWidget *window,
975 gboolean handled = FALSE;
977 g_object_ref (window);
983 case GDK_KEY_RELEASE:
984 handled = gtk_widget_event (menu, event);
990 g_object_unref (window);
991 g_object_unref (menu);
997 gtk_menu_init (GtkMenu *menu)
999 GtkMenuPrivate *priv;
1000 GtkStyleContext *context;
1002 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1006 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1007 "type", GTK_WINDOW_POPUP,
1010 "signal::event", gtk_menu_window_event, menu,
1011 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1013 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1014 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1016 /* Refloat the menu, so that reference counting for the menu isn't
1017 * affected by it being a child of the toplevel
1019 g_object_force_floating (G_OBJECT (menu));
1020 priv->needs_destruction_ref = TRUE;
1022 priv->monitor_num = -1;
1024 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1025 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1029 gtk_menu_destroy (GtkWidget *widget)
1031 GtkMenu *menu = GTK_MENU (widget);
1032 GtkMenuPrivate *priv = menu->priv;
1033 GtkMenuAttachData *data;
1035 gtk_menu_remove_scroll_timeout (menu);
1037 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1039 gtk_menu_detach (menu);
1041 gtk_menu_stop_navigating_submenu (menu);
1043 if (priv->old_active_menu_item)
1044 g_clear_object (&priv->old_active_menu_item);
1046 /* Add back the reference count for being a child */
1047 if (priv->needs_destruction_ref)
1049 priv->needs_destruction_ref = FALSE;
1050 g_object_ref (widget);
1053 if (priv->accel_group)
1054 g_clear_object (&priv->accel_group);
1057 gtk_widget_destroy (priv->toplevel);
1059 if (priv->tearoff_window)
1060 gtk_widget_destroy (priv->tearoff_window);
1064 g_free (priv->heights);
1065 priv->heights = NULL;
1070 g_free (priv->title);
1074 if (priv->position_func_data_destroy)
1076 priv->position_func_data_destroy (priv->position_func_data);
1077 priv->position_func_data = NULL;
1078 priv->position_func_data_destroy = NULL;
1081 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1085 menu_change_screen (GtkMenu *menu,
1086 GdkScreen *new_screen)
1088 GtkMenuPrivate *priv = menu->priv;
1090 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1092 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1098 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1099 gtk_menu_position (menu, TRUE);
1102 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1103 priv->monitor_num = -1;
1107 attach_widget_screen_changed (GtkWidget *attach_widget,
1108 GdkScreen *previous_screen,
1111 if (gtk_widget_has_screen (attach_widget) &&
1112 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1113 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1117 gtk_menu_attach_to_widget (GtkMenu *menu,
1118 GtkWidget *attach_widget,
1119 GtkMenuDetachFunc detacher)
1121 GtkMenuAttachData *data;
1124 g_return_if_fail (GTK_IS_MENU (menu));
1125 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1127 /* keep this function in sync with gtk_widget_set_parent() */
1128 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1131 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1132 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1136 g_object_ref_sink (menu);
1138 data = g_slice_new (GtkMenuAttachData);
1139 data->attach_widget = attach_widget;
1141 g_signal_connect (attach_widget, "screen-changed",
1142 G_CALLBACK (attach_widget_screen_changed), menu);
1143 attach_widget_screen_changed (attach_widget, NULL, menu);
1145 data->detacher = detacher;
1146 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1147 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1148 if (!g_list_find (list, menu))
1149 list = g_list_prepend (list, menu);
1151 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1152 (GDestroyNotify) g_list_free);
1154 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1155 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1157 /* we don't need to set the style here, since
1158 * we are a toplevel widget.
1161 /* Fallback title for menu comes from attach widget */
1162 gtk_menu_update_title (menu);
1164 g_object_notify (G_OBJECT (menu), "attach-widget");
1168 gtk_menu_get_attach_widget (GtkMenu *menu)
1170 GtkMenuAttachData *data;
1172 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1174 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1176 return data->attach_widget;
1181 gtk_menu_detach (GtkMenu *menu)
1183 GtkMenuAttachData *data;
1186 g_return_if_fail (GTK_IS_MENU (menu));
1188 /* keep this function in sync with gtk_widget_unparent() */
1189 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1192 g_warning ("gtk_menu_detach(): menu is not attached");
1195 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1197 g_signal_handlers_disconnect_by_func (data->attach_widget,
1198 (gpointer) attach_widget_screen_changed,
1202 data->detacher (data->attach_widget, menu);
1203 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1204 list = g_list_remove (list, menu);
1206 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1207 (GDestroyNotify) g_list_free);
1209 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1211 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1212 gtk_widget_unrealize (GTK_WIDGET (menu));
1214 g_slice_free (GtkMenuAttachData, data);
1216 /* Fallback title for menu comes from attach widget */
1217 gtk_menu_update_title (menu);
1219 g_object_unref (menu);
1223 gtk_menu_remove (GtkContainer *container,
1226 GtkMenu *menu = GTK_MENU (container);
1227 GtkMenuPrivate *priv = menu->priv;
1229 /* Clear out old_active_menu_item if it matches the item we are removing */
1230 if (priv->old_active_menu_item == widget)
1231 g_clear_object (&priv->old_active_menu_item);
1233 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1235 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1237 menu_queue_resize (menu);
1243 return g_object_new (GTK_TYPE_MENU, NULL);
1247 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1251 GtkMenu *menu = GTK_MENU (menu_shell);
1252 GtkMenuPrivate *priv = menu->priv;
1253 AttachInfo *ai = get_attach_info (child);
1255 ai->left_attach = -1;
1256 ai->right_attach = -1;
1257 ai->top_attach = -1;
1258 ai->bottom_attach = -1;
1260 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1261 gtk_widget_set_parent_window (child, priv->bin_window);
1263 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1265 menu_queue_resize (menu);
1269 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1271 GtkMenuPrivate *priv = menu->priv;
1275 widget = GTK_WIDGET (menu);
1280 cairo_surface_t *surface;
1281 cairo_pattern_t *pattern;
1284 priv->tearoff_active = FALSE;
1285 priv->saved_scroll_offset = priv->scroll_offset;
1287 window = gtk_widget_get_window (priv->tearoff_window);
1288 width = gdk_window_get_width (window);
1289 height = gdk_window_get_height (window);
1291 surface = gdk_window_create_similar_surface (window,
1292 CAIRO_CONTENT_COLOR,
1296 cr = cairo_create (surface);
1297 gdk_cairo_set_source_window (cr, window, 0, 0);
1301 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1303 pattern = cairo_pattern_create_for_surface (surface);
1304 gdk_window_set_background_pattern (window, pattern);
1306 cairo_pattern_destroy (pattern);
1307 cairo_surface_destroy (surface);
1312 popup_grab_on_window (GdkWindow *window,
1313 GdkDevice *keyboard,
1315 guint32 activate_time)
1318 gdk_device_grab (keyboard, window,
1319 GDK_OWNERSHIP_WINDOW, TRUE,
1320 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1321 NULL, activate_time) != GDK_GRAB_SUCCESS)
1325 gdk_device_grab (pointer, window,
1326 GDK_OWNERSHIP_WINDOW, TRUE,
1327 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1328 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1329 GDK_POINTER_MOTION_MASK,
1330 NULL, activate_time) != GDK_GRAB_SUCCESS)
1333 gdk_device_ungrab (keyboard, activate_time);
1342 * gtk_menu_popup_for_device:
1343 * @menu: a #GtkMenu.
1344 * @device: (allow-none): a #GdkDevice
1345 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1346 * menu item, or %NULL
1347 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1348 * the popup, or %NULL
1349 * @func: (allow-none): a user supplied function used to position the menu,
1351 * @data: (allow-none): user supplied data to be passed to @func
1352 * @destroy: (allow-none): destroy notify for @data
1353 * @button: the mouse button which was pressed to initiate the event
1354 * @activate_time: the time at which the activation event occurred
1356 * Displays a menu and makes it available for selection.
1358 * Applications can use this function to display context-sensitive menus,
1359 * and will typically supply %NULL for the @parent_menu_shell,
1360 * @parent_menu_item, @func, @data and @destroy parameters. The default
1361 * menu positioning function will position the menu at the current position
1362 * of @device (or its corresponding pointer).
1364 * The @button parameter should be the mouse button pressed to initiate
1365 * the menu popup. If the menu popup was initiated by something other than
1366 * a mouse button press, such as a mouse button release or a keypress,
1367 * @button should be 0.
1369 * The @activate_time parameter is used to conflict-resolve initiation of
1370 * concurrent requests for mouse/keyboard grab requests. To function
1371 * properly, this needs to be the time stamp of the user event (such as
1372 * a mouse click or key press) that caused the initiation of the popup.
1373 * Only if no such event is available, gtk_get_current_event_time() can
1379 gtk_menu_popup_for_device (GtkMenu *menu,
1381 GtkWidget *parent_menu_shell,
1382 GtkWidget *parent_menu_item,
1383 GtkMenuPositionFunc func,
1385 GDestroyNotify destroy,
1387 guint32 activate_time)
1389 GtkMenuPrivate *priv = menu->priv;
1391 GtkWidget *xgrab_shell;
1393 GdkEvent *current_event;
1394 GtkMenuShell *menu_shell;
1395 gboolean grab_keyboard;
1396 GtkWidget *parent_toplevel;
1397 GdkDevice *keyboard, *pointer;
1399 g_return_if_fail (GTK_IS_MENU (menu));
1400 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1403 device = gtk_get_current_event_device ();
1407 GdkDisplay *display;
1408 GdkDeviceManager *device_manager;
1411 display = gtk_widget_get_display (GTK_WIDGET (menu));
1412 device_manager = gdk_display_get_device_manager (display);
1413 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1415 device = devices->data;
1417 g_list_free (devices);
1420 widget = GTK_WIDGET (menu);
1421 menu_shell = GTK_MENU_SHELL (menu);
1423 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1426 pointer = gdk_device_get_associated_device (device);
1431 keyboard = gdk_device_get_associated_device (device);
1434 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1436 priv->seen_item_enter = FALSE;
1438 /* Find the last viewable ancestor, and make an X grab on it
1440 parent = GTK_WIDGET (menu);
1444 gboolean viewable = TRUE;
1445 GtkWidget *tmp = parent;
1449 if (!gtk_widget_get_mapped (tmp))
1454 tmp = gtk_widget_get_parent (tmp);
1458 xgrab_shell = parent;
1460 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1463 /* We want to receive events generated when we map the menu;
1464 * unfortunately, since there is probably already an implicit
1465 * grab in place from the button that the user used to pop up
1466 * the menu, we won't receive then -- in particular, the EnterNotify
1467 * when the menu pops up under the pointer.
1469 * If we are grabbing on a parent menu shell, no problem; just grab
1470 * on that menu shell first before popping up the window with
1471 * owner_events = TRUE.
1473 * When grabbing on the menu itself, things get more convoluted --
1474 * we do an explicit grab on a specially created window with
1475 * owner_events = TRUE, which we override further down with a
1476 * grab on the menu. (We can't grab on the menu until it is mapped;
1477 * we probably could just leave the grab on the other window,
1478 * with a little reorganization of the code in gtkmenu*).
1480 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1481 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1486 if (xgrab_shell && xgrab_shell != widget)
1488 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1490 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1491 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1496 GdkWindow *transfer_window;
1498 xgrab_shell = widget;
1499 transfer_window = menu_grab_transfer_window_get (menu);
1500 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1502 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1503 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1507 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1509 /* We failed to make our pointer/keyboard grab.
1510 * Rather than leaving the user with a stuck up window,
1511 * we just abort here. Presumably the user will try again.
1513 menu_shell->priv->parent_menu_shell = NULL;
1514 menu_grab_transfer_window_destroy (menu);
1518 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1519 menu_shell->priv->active = TRUE;
1520 menu_shell->priv->button = button;
1522 /* If we are popping up the menu from something other than, a button
1523 * press then, as a heuristic, we ignore enter events for the menu
1524 * until we get a MOTION_NOTIFY.
1527 current_event = gtk_get_current_event ();
1530 if ((current_event->type != GDK_BUTTON_PRESS) &&
1531 (current_event->type != GDK_ENTER_NOTIFY))
1532 menu_shell->priv->ignore_enter = TRUE;
1534 gdk_event_free (current_event);
1537 menu_shell->priv->ignore_enter = TRUE;
1541 gtk_menu_tearoff_bg_copy (menu);
1543 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1546 parent_toplevel = NULL;
1547 if (parent_menu_shell)
1548 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1549 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1551 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1553 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1556 /* Set transient for to get the right window group and parent */
1557 if (GTK_IS_WINDOW (parent_toplevel))
1558 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1559 GTK_WINDOW (parent_toplevel));
1561 priv->parent_menu_item = parent_menu_item;
1562 priv->position_func = func;
1563 priv->position_func_data = data;
1564 priv->position_func_data_destroy = destroy;
1565 menu_shell->priv->activate_time = activate_time;
1567 /* We need to show the menu here rather in the init function
1568 * because code expects to be able to tell if the menu is onscreen
1569 * by looking at gtk_widget_get_visible (menu)
1571 gtk_widget_show (GTK_WIDGET (menu));
1573 /* Position the menu, possibly changing the size request
1575 gtk_menu_position (menu, TRUE);
1577 /* Compute the size of the toplevel and realize it so we
1578 * can scroll correctly.
1581 GtkRequisition tmp_request;
1582 GtkAllocation tmp_allocation = { 0, };
1584 /* Instead of trusting the menu position function to queue a
1585 * resize when the menu goes out of bounds, invalidate the cached
1588 gtk_widget_queue_resize (GTK_WIDGET (menu));
1589 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1591 tmp_allocation.width = tmp_request.width;
1592 tmp_allocation.height = tmp_request.height;
1594 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1596 gtk_widget_realize (GTK_WIDGET (menu));
1599 gtk_menu_scroll_to (menu, priv->scroll_offset);
1601 /* if no item is selected, select the first one */
1602 if (!menu_shell->priv->active_menu_item)
1604 gboolean touchscreen_mode;
1606 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1607 "gtk-touchscreen-mode", &touchscreen_mode,
1610 if (touchscreen_mode)
1611 gtk_menu_shell_select_first (menu_shell, TRUE);
1614 /* Once everything is set up correctly, map the toplevel */
1615 gtk_widget_show (priv->toplevel);
1617 if (xgrab_shell == widget)
1618 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1620 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1622 if (parent_menu_shell)
1624 gboolean keyboard_mode;
1626 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1627 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1629 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1630 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1632 _gtk_menu_shell_update_mnemonics (menu_shell);
1637 * @menu: a #GtkMenu.
1638 * @parent_menu_shell: (allow-none): the menu shell containing the
1639 * triggering menu item, or %NULL
1640 * @parent_menu_item: (allow-none): the menu item whose activation
1641 * triggered the popup, or %NULL
1642 * @func: (allow-none): a user supplied function used to position
1643 * the menu, or %NULL
1644 * @data: (allow-none): user supplied data to be passed to @func.
1645 * @button: the mouse button which was pressed to initiate the event.
1646 * @activate_time: the time at which the activation event occurred.
1648 * Displays a menu and makes it available for selection.
1650 * Applications can use this function to display context-sensitive
1651 * menus, and will typically supply %NULL for the @parent_menu_shell,
1652 * @parent_menu_item, @func and @data parameters. The default menu
1653 * positioning function will position the menu at the current mouse
1656 * The @button parameter should be the mouse button pressed to initiate
1657 * the menu popup. If the menu popup was initiated by something other
1658 * than a mouse button press, such as a mouse button release or a keypress,
1659 * @button should be 0.
1661 * The @activate_time parameter is used to conflict-resolve initiation
1662 * of concurrent requests for mouse/keyboard grab requests. To function
1663 * properly, this needs to be the timestamp of the user event (such as
1664 * a mouse click or key press) that caused the initiation of the popup.
1665 * Only if no such event is available, gtk_get_current_event_time() can
1669 gtk_menu_popup (GtkMenu *menu,
1670 GtkWidget *parent_menu_shell,
1671 GtkWidget *parent_menu_item,
1672 GtkMenuPositionFunc func,
1675 guint32 activate_time)
1677 g_return_if_fail (GTK_IS_MENU (menu));
1679 gtk_menu_popup_for_device (menu,
1684 button, activate_time);
1688 gtk_menu_popdown (GtkMenu *menu)
1690 GtkMenuPrivate *priv;
1691 GtkMenuShell *menu_shell;
1694 g_return_if_fail (GTK_IS_MENU (menu));
1696 menu_shell = GTK_MENU_SHELL (menu);
1699 menu_shell->priv->parent_menu_shell = NULL;
1700 menu_shell->priv->active = FALSE;
1701 menu_shell->priv->ignore_enter = FALSE;
1703 priv->have_position = FALSE;
1705 gtk_menu_stop_scrolling (menu);
1706 gtk_menu_stop_navigating_submenu (menu);
1708 if (menu_shell->priv->active_menu_item)
1710 if (priv->old_active_menu_item)
1711 g_object_unref (priv->old_active_menu_item);
1712 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1713 g_object_ref (priv->old_active_menu_item);
1716 gtk_menu_shell_deselect (menu_shell);
1718 /* The X Grab, if present, will automatically be removed
1719 * when we hide the window
1723 gtk_widget_hide (priv->toplevel);
1724 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1727 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1731 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1733 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1735 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1739 /* We popped up the menu from the tearoff, so we need to
1740 * release the grab - we aren't actually hiding the menu.
1742 if (menu_shell->priv->have_xgrab && pointer)
1744 GdkDevice *keyboard;
1746 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1747 keyboard = gdk_device_get_associated_device (pointer);
1750 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1754 /* gtk_menu_popdown is called each time a menu item is selected from
1755 * a torn off menu. Only scroll back to the saved position if the
1756 * non-tearoff menu was popped down.
1758 if (!priv->tearoff_active)
1759 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1760 priv->tearoff_active = TRUE;
1763 gtk_widget_hide (GTK_WIDGET (menu));
1765 menu_shell->priv->have_xgrab = FALSE;
1768 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1770 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1772 menu_grab_transfer_window_destroy (menu);
1776 gtk_menu_get_active (GtkMenu *menu)
1778 GtkMenuPrivate *priv = menu->priv;
1782 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1784 if (!priv->old_active_menu_item)
1787 children = GTK_MENU_SHELL (menu)->priv->children;
1791 child = children->data;
1792 children = children->next;
1794 if (gtk_bin_get_child (GTK_BIN (child)))
1799 priv->old_active_menu_item = child;
1800 if (priv->old_active_menu_item)
1801 g_object_ref (priv->old_active_menu_item);
1804 return priv->old_active_menu_item;
1808 gtk_menu_set_active (GtkMenu *menu,
1811 GtkMenuPrivate *priv = menu->priv;
1815 g_return_if_fail (GTK_IS_MENU (menu));
1817 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1820 child = tmp_list->data;
1821 if (gtk_bin_get_child (GTK_BIN (child)))
1823 if (priv->old_active_menu_item)
1824 g_object_unref (priv->old_active_menu_item);
1825 priv->old_active_menu_item = child;
1826 g_object_ref (priv->old_active_menu_item);
1832 * gtk_menu_set_accel_group:
1833 * @accel_group: (allow-none):
1836 gtk_menu_set_accel_group (GtkMenu *menu,
1837 GtkAccelGroup *accel_group)
1839 GtkMenuPrivate *priv = menu->priv;
1840 g_return_if_fail (GTK_IS_MENU (menu));
1842 if (priv->accel_group != accel_group)
1844 if (priv->accel_group)
1845 g_object_unref (priv->accel_group);
1846 priv->accel_group = accel_group;
1847 if (priv->accel_group)
1848 g_object_ref (priv->accel_group);
1849 _gtk_menu_refresh_accel_paths (menu, TRUE);
1854 gtk_menu_get_accel_group (GtkMenu *menu)
1856 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1858 return menu->priv->accel_group;
1862 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1865 /* Menu items chain here to figure whether they can activate their
1866 * accelerators. Unlike ordinary widgets, menus allow accel
1867 * activation even if invisible since that's the usual case for
1868 * submenus/popup-menus. however, the state of the attach widget
1869 * affects the "activeness" of the menu.
1871 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1874 return gtk_widget_can_activate_accel (awidget, signal_id);
1876 return gtk_widget_is_sensitive (widget);
1880 * gtk_menu_set_accel_path
1881 * @menu: a valid #GtkMenu
1882 * @accel_path: (allow-none): a valid accelerator path
1884 * Sets an accelerator path for this menu from which accelerator paths
1885 * for its immediate children, its menu items, can be constructed.
1886 * The main purpose of this function is to spare the programmer the
1887 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1888 * each menu item that should support runtime user changable accelerators.
1889 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1890 * each menu item of this menu, that contains a label describing its
1891 * purpose, automatically gets an accel path assigned.
1893 * For example, a menu containing menu items "New" and "Exit", will, after
1894 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1895 * has been called, assign its items the accel paths:
1896 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1898 * Assigning accel paths to menu items then enables the user to change
1899 * their accelerators at runtime. More details about accelerator paths
1900 * and their default setups can be found at gtk_accel_map_add_entry().
1902 * Note that @accel_path string will be stored in a #GQuark. Therefore,
1903 * if you pass a static string, you can save some memory by interning
1904 * it first with g_intern_static_string().
1907 gtk_menu_set_accel_path (GtkMenu *menu,
1908 const gchar *accel_path)
1910 GtkMenuPrivate *priv = menu->priv;
1911 g_return_if_fail (GTK_IS_MENU (menu));
1914 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1916 /* FIXME: accel_path should be defined as const gchar* */
1917 priv->accel_path = (gchar*)g_intern_string (accel_path);
1918 if (priv->accel_path)
1919 _gtk_menu_refresh_accel_paths (menu, FALSE);
1923 * gtk_menu_get_accel_path
1924 * @menu: a valid #GtkMenu
1926 * Retrieves the accelerator path set on the menu.
1928 * Returns: the accelerator path set on the menu.
1933 gtk_menu_get_accel_path (GtkMenu *menu)
1935 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1937 return menu->priv->accel_path;
1942 gboolean group_changed;
1946 refresh_accel_paths_foreach (GtkWidget *widget,
1949 GtkMenuPrivate *priv;
1950 AccelPropagation *prop = data;
1952 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1954 priv = prop->menu->priv;
1955 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1958 prop->group_changed);
1963 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1964 gboolean group_changed)
1966 GtkMenuPrivate *priv = menu->priv;
1967 g_return_if_fail (GTK_IS_MENU (menu));
1969 if (priv->accel_path && priv->accel_group)
1971 AccelPropagation prop;
1974 prop.group_changed = group_changed;
1975 gtk_container_foreach (GTK_CONTAINER (menu),
1976 refresh_accel_paths_foreach,
1982 gtk_menu_reposition (GtkMenu *menu)
1984 g_return_if_fail (GTK_IS_MENU (menu));
1986 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
1987 gtk_menu_position (menu, FALSE);
1991 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1996 value = gtk_adjustment_get_value (adjustment);
1997 if (menu->priv->scroll_offset != value)
1998 gtk_menu_scroll_to (menu, value);
2002 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2005 GtkMenuPrivate *priv = menu->priv;
2006 GdkGeometry geometry_hints;
2008 if (!priv->tearoff_window)
2011 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2013 GtkRequisition requisition;
2015 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2016 &requisition, NULL);
2017 width += requisition.width;
2020 geometry_hints.min_width = width;
2021 geometry_hints.max_width = width;
2023 geometry_hints.min_height = 0;
2024 geometry_hints.max_height = priv->requested_height;
2026 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2029 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2033 gtk_menu_update_title (GtkMenu *menu)
2035 GtkMenuPrivate *priv = menu->priv;
2037 if (priv->tearoff_window)
2040 GtkWidget *attach_widget;
2042 title = gtk_menu_get_title (menu);
2045 attach_widget = gtk_menu_get_attach_widget (menu);
2046 if (GTK_IS_MENU_ITEM (attach_widget))
2048 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2049 if (GTK_IS_LABEL (child))
2050 title = gtk_label_get_text (GTK_LABEL (child));
2055 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2060 gtk_menu_get_toplevel (GtkWidget *menu)
2062 GtkWidget *attach, *toplevel;
2064 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2066 if (GTK_IS_MENU_ITEM (attach))
2067 attach = gtk_widget_get_parent (attach);
2069 if (GTK_IS_MENU (attach))
2070 return gtk_menu_get_toplevel (attach);
2071 else if (GTK_IS_WIDGET (attach))
2073 toplevel = gtk_widget_get_toplevel (attach);
2074 if (gtk_widget_is_toplevel (toplevel))
2082 tearoff_window_destroyed (GtkWidget *widget,
2085 gtk_menu_set_tearoff_state (menu, FALSE);
2089 gtk_menu_set_tearoff_state (GtkMenu *menu,
2092 GtkMenuPrivate *priv = menu->priv;
2095 g_return_if_fail (GTK_IS_MENU (menu));
2097 if (priv->torn_off != torn_off)
2099 priv->torn_off = torn_off;
2100 priv->tearoff_active = torn_off;
2104 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2105 gtk_menu_popdown (menu);
2107 if (!priv->tearoff_window)
2109 GtkWidget *toplevel;
2111 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2112 "type", GTK_WINDOW_TOPLEVEL,
2113 "screen", gtk_widget_get_screen (priv->toplevel),
2114 "app-paintable", TRUE,
2117 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2118 GDK_WINDOW_TYPE_HINT_MENU);
2119 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2120 g_signal_connect (priv->tearoff_window, "destroy",
2121 G_CALLBACK (tearoff_window_destroyed), menu);
2122 g_signal_connect (priv->tearoff_window, "event",
2123 G_CALLBACK (gtk_menu_window_event), menu);
2125 gtk_menu_update_title (menu);
2127 gtk_widget_realize (priv->tearoff_window);
2129 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2130 if (toplevel != NULL)
2131 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2132 GTK_WINDOW (toplevel));
2134 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2135 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2136 priv->tearoff_hbox);
2138 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2139 priv->tearoff_adjustment = gtk_adjustment_new (0,
2140 0, priv->requested_height,
2144 g_object_connect (priv->tearoff_adjustment,
2145 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2147 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2149 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2150 priv->tearoff_scrollbar,
2153 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2154 gtk_widget_show (priv->tearoff_scrollbar);
2156 gtk_widget_show (priv->tearoff_hbox);
2159 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2161 /* Update menu->requisition */
2162 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2164 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2166 gtk_widget_realize (priv->tearoff_window);
2167 gtk_menu_position (menu, TRUE);
2169 gtk_widget_show (GTK_WIDGET (menu));
2170 gtk_widget_show (priv->tearoff_window);
2172 gtk_menu_scroll_to (menu, 0);
2177 gtk_widget_hide (GTK_WIDGET (menu));
2178 gtk_widget_hide (priv->tearoff_window);
2179 if (GTK_IS_CONTAINER (priv->toplevel))
2180 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2181 gtk_widget_destroy (priv->tearoff_window);
2183 priv->tearoff_window = NULL;
2184 priv->tearoff_hbox = NULL;
2185 priv->tearoff_scrollbar = NULL;
2186 priv->tearoff_adjustment = NULL;
2189 g_object_notify (G_OBJECT (menu), "tearoff-state");
2194 * gtk_menu_get_tearoff_state:
2197 * Returns whether the menu is torn off.
2198 * See gtk_menu_set_tearoff_state().
2200 * Return value: %TRUE if the menu is currently torn off.
2203 gtk_menu_get_tearoff_state (GtkMenu *menu)
2205 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2207 return menu->priv->torn_off;
2211 * gtk_menu_set_title:
2213 * @title: a string containing the title for the menu
2215 * Sets the title string for the menu.
2217 * The title is displayed when the menu is shown as a tearoff
2218 * menu. If @title is %NULL, the menu will see if it is attached
2219 * to a parent menu item, and if so it will try to use the same
2220 * text as that menu item's label.
2223 gtk_menu_set_title (GtkMenu *menu,
2226 GtkMenuPrivate *priv = menu->priv;
2229 g_return_if_fail (GTK_IS_MENU (menu));
2231 old_title = priv->title;
2232 priv->title = g_strdup (title);
2235 gtk_menu_update_title (menu);
2236 g_object_notify (G_OBJECT (menu), "tearoff-title");
2240 * gtk_menu_get_title:
2243 * Returns the title of the menu. See gtk_menu_set_title().
2245 * Return value: the title of the menu, or %NULL if the menu
2246 * has no title set on it. This string is owned by GTK+
2247 * and should not be modified or freed.
2249 G_CONST_RETURN gchar *
2250 gtk_menu_get_title (GtkMenu *menu)
2252 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2254 return menu->priv->title;
2258 gtk_menu_reorder_child (GtkMenu *menu,
2262 GtkMenuShell *menu_shell;
2264 g_return_if_fail (GTK_IS_MENU (menu));
2265 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2267 menu_shell = GTK_MENU_SHELL (menu);
2269 if (g_list_find (menu_shell->priv->children, child))
2271 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2272 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2274 menu_queue_resize (menu);
2279 gtk_menu_style_updated (GtkWidget *widget)
2281 if (gtk_widget_get_realized (widget))
2283 GtkMenu *menu = GTK_MENU (widget);
2284 GtkMenuPrivate *priv = menu->priv;
2285 GtkStyleContext *context;
2287 context = gtk_widget_get_style_context (widget);
2289 gtk_style_context_set_background (context, priv->bin_window);
2290 gtk_style_context_set_background (context, priv->view_window);
2291 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2296 get_arrows_border (GtkMenu *menu,
2299 GtkMenuPrivate *priv = menu->priv;
2300 guint scroll_arrow_height;
2301 GtkArrowPlacement arrow_placement;
2303 gtk_widget_style_get (GTK_WIDGET (menu),
2304 "scroll-arrow-vlength", &scroll_arrow_height,
2305 "arrow_placement", &arrow_placement,
2308 switch (arrow_placement)
2310 case GTK_ARROWS_BOTH:
2311 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2312 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2315 case GTK_ARROWS_START:
2316 border->top = (priv->upper_arrow_visible ||
2317 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2321 case GTK_ARROWS_END:
2323 border->bottom = (priv->upper_arrow_visible ||
2324 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2328 border->left = border->right = 0;
2332 get_menu_border (GtkWidget *widget,
2335 GtkStyleContext *context;
2336 GtkStateFlags state;
2337 GtkBorder padding, border_width;
2339 context = gtk_widget_get_style_context (widget);
2340 state = gtk_widget_get_state_flags (widget);
2342 gtk_style_context_get_padding (context, state, &padding);
2343 gtk_style_context_get_border (context, state, &border_width);
2345 border->left = border_width.left + padding.left;
2346 border->right = border_width.right + padding.right;
2347 border->top = border_width.top + padding.top;
2348 border->bottom = border_width.bottom + padding.bottom;
2352 gtk_menu_realize (GtkWidget *widget)
2354 GtkMenu *menu = GTK_MENU (widget);
2355 GtkMenuPrivate *priv = menu->priv;
2356 GtkAllocation allocation;
2357 GtkStyleContext *context;
2359 GdkWindowAttr attributes;
2360 gint attributes_mask;
2364 guint vertical_padding;
2365 guint horizontal_padding;
2366 GtkBorder arrow_border, border;
2368 g_return_if_fail (GTK_IS_MENU (widget));
2370 gtk_widget_set_realized (widget, TRUE);
2372 gtk_widget_get_allocation (widget, &allocation);
2374 attributes.window_type = GDK_WINDOW_CHILD;
2375 attributes.x = allocation.x;
2376 attributes.y = allocation.y;
2377 attributes.width = allocation.width;
2378 attributes.height = allocation.height;
2379 attributes.wclass = GDK_INPUT_OUTPUT;
2380 attributes.visual = gtk_widget_get_visual (widget);
2381 attributes.event_mask = gtk_widget_get_events (widget);
2382 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2383 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2385 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2387 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2388 &attributes, attributes_mask);
2389 gtk_widget_set_window (widget, window);
2390 gdk_window_set_user_data (window, widget);
2392 get_menu_border (widget, &border);
2393 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2394 context = gtk_widget_get_style_context (widget);
2396 gtk_widget_style_get (GTK_WIDGET (menu),
2397 "vertical-padding", &vertical_padding,
2398 "horizontal-padding", &horizontal_padding,
2401 gtk_widget_get_allocation (widget, &allocation);
2403 attributes.x = border_width + border.left + horizontal_padding;
2404 attributes.y = border_width + border.top + vertical_padding;
2405 attributes.width = allocation.width -
2406 (2 * (border_width + horizontal_padding)) - border.left - border.right;
2407 attributes.height = allocation.height -
2408 (2 * (border_width + vertical_padding)) - border.top - border.bottom;
2410 get_arrows_border (menu, &arrow_border);
2411 attributes.y += arrow_border.top;
2412 attributes.height -= arrow_border.top;
2413 attributes.height -= arrow_border.bottom;
2415 attributes.width = MAX (1, attributes.width);
2416 attributes.height = MAX (1, attributes.height);
2418 priv->view_window = gdk_window_new (window,
2419 &attributes, attributes_mask);
2420 gdk_window_set_user_data (priv->view_window, menu);
2422 gtk_widget_get_allocation (widget, &allocation);
2426 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2427 border.left + border.right;
2428 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2429 border.top + border.bottom;
2431 attributes.width = MAX (1, attributes.width);
2432 attributes.height = MAX (1, attributes.height);
2434 priv->bin_window = gdk_window_new (priv->view_window,
2435 &attributes, attributes_mask);
2436 gdk_window_set_user_data (priv->bin_window, menu);
2438 children = GTK_MENU_SHELL (menu)->priv->children;
2441 child = children->data;
2442 children = children->next;
2444 gtk_widget_set_parent_window (child, priv->bin_window);
2447 gtk_style_context_set_background (context, priv->bin_window);
2448 gtk_style_context_set_background (context, priv->view_window);
2449 gtk_style_context_set_background (context, window);
2451 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2452 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2453 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2455 gdk_window_show (priv->bin_window);
2456 gdk_window_show (priv->view_window);
2460 gtk_menu_focus (GtkWidget *widget,
2461 GtkDirectionType direction)
2463 /* A menu or its menu items cannot have focus */
2467 /* See notes in gtk_menu_popup() for information
2468 * about the "grab transfer window"
2471 menu_grab_transfer_window_get (GtkMenu *menu)
2473 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2476 GdkWindowAttr attributes;
2477 gint attributes_mask;
2479 attributes.x = -100;
2480 attributes.y = -100;
2481 attributes.width = 10;
2482 attributes.height = 10;
2483 attributes.window_type = GDK_WINDOW_TEMP;
2484 attributes.wclass = GDK_INPUT_ONLY;
2485 attributes.override_redirect = TRUE;
2486 attributes.event_mask = 0;
2488 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2490 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2491 &attributes, attributes_mask);
2492 gdk_window_set_user_data (window, menu);
2494 gdk_window_show (window);
2496 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2503 menu_grab_transfer_window_destroy (GtkMenu *menu)
2505 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2508 gdk_window_set_user_data (window, NULL);
2509 gdk_window_destroy (window);
2510 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2515 gtk_menu_unrealize (GtkWidget *widget)
2517 GtkMenu *menu = GTK_MENU (widget);
2518 GtkMenuPrivate *priv = menu->priv;
2520 menu_grab_transfer_window_destroy (menu);
2522 gdk_window_set_user_data (priv->view_window, NULL);
2523 gdk_window_destroy (priv->view_window);
2524 priv->view_window = NULL;
2526 gdk_window_set_user_data (priv->bin_window, NULL);
2527 gdk_window_destroy (priv->bin_window);
2528 priv->bin_window = NULL;
2530 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2534 calculate_line_heights (GtkMenu *menu,
2536 guint **ret_min_heights,
2537 guint **ret_nat_heights)
2539 GtkStyleContext *context;
2540 GtkStateFlags state;
2542 GtkMenuPrivate *priv;
2543 GtkMenuShell *menu_shell;
2544 GtkWidget *child, *widget;
2546 guint horizontal_padding;
2555 widget = GTK_WIDGET (menu);
2556 menu_shell = GTK_MENU_SHELL (widget);
2558 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2559 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2560 n_heights = gtk_menu_get_n_rows (menu);
2561 n_columns = gtk_menu_get_n_columns (menu);
2562 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2564 gtk_widget_style_get (GTK_WIDGET (menu),
2565 "horizontal-padding", &horizontal_padding,
2568 context = gtk_widget_get_style_context (widget);
2569 state = gtk_widget_get_state_flags (widget);
2570 gtk_style_context_get_padding (context, state, &padding);
2572 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2573 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2575 for (children = menu_shell->priv->children; children; children = children->next)
2580 gint child_min, child_nat;
2582 child = children->data;
2584 if (!gtk_widget_get_visible (child))
2587 get_effective_child_attach (child, &l, &r, &t, &b);
2589 part = avail_width / (r - l);
2591 gtk_widget_get_preferred_height_for_width (child, part,
2592 &child_min, &child_nat);
2594 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2596 part = MAX (child_min, toggle_size) / (b - t);
2597 min_heights[t] = MAX (min_heights[t], part);
2599 part = MAX (child_nat, toggle_size) / (b - t);
2600 nat_heights[t] = MAX (nat_heights[t], part);
2603 if (ret_min_heights)
2604 *ret_min_heights = min_heights;
2606 g_free (min_heights);
2608 if (ret_nat_heights)
2609 *ret_nat_heights = nat_heights;
2611 g_free (nat_heights);
2617 gtk_menu_size_allocate (GtkWidget *widget,
2618 GtkAllocation *allocation)
2621 GtkMenuPrivate *priv;
2622 GtkMenuShell *menu_shell;
2624 GtkAllocation child_allocation;
2629 guint vertical_padding;
2630 guint horizontal_padding;
2633 g_return_if_fail (GTK_IS_MENU (widget));
2634 g_return_if_fail (allocation != NULL);
2636 menu = GTK_MENU (widget);
2637 menu_shell = GTK_MENU_SHELL (widget);
2640 gtk_widget_set_allocation (widget, allocation);
2642 gtk_widget_style_get (GTK_WIDGET (menu),
2643 "vertical-padding", &vertical_padding,
2644 "horizontal-padding", &horizontal_padding,
2647 get_menu_border (widget, &border);
2648 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2650 g_free (priv->heights);
2651 priv->heights_length = calculate_line_heights (menu,
2656 /* refresh our cached height request */
2657 priv->requested_height = (2 * (border_width + vertical_padding)) +
2658 border.top + border.bottom;
2659 for (i = 0; i < priv->heights_length; i++)
2660 priv->requested_height += priv->heights[i];
2662 x = border_width + border.left + horizontal_padding;
2663 y = border_width + border.top + vertical_padding;
2664 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2665 border.left - border.right;
2666 height = allocation->height - (2 * (border_width + vertical_padding)) -
2667 border.top - border.bottom;
2669 if (menu_shell->priv->active)
2670 gtk_menu_scroll_to (menu, priv->scroll_offset);
2672 if (!priv->tearoff_active)
2674 GtkBorder arrow_border;
2676 get_arrows_border (menu, &arrow_border);
2677 y += arrow_border.top;
2678 height -= arrow_border.top;
2679 height -= arrow_border.bottom;
2682 width = MAX (1, width);
2683 height = MAX (1, height);
2685 if (gtk_widget_get_realized (widget))
2687 gdk_window_move_resize (gtk_widget_get_window (widget),
2688 allocation->x, allocation->y,
2689 allocation->width, allocation->height);
2691 gdk_window_move_resize (priv->view_window, x, y, width, height);
2694 if (menu_shell->priv->children)
2696 gint base_width = width / gtk_menu_get_n_columns (menu);
2698 children = menu_shell->priv->children;
2701 child = children->data;
2702 children = children->next;
2704 if (gtk_widget_get_visible (child))
2709 get_effective_child_attach (child, &l, &r, &t, &b);
2711 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2714 tmp = gtk_menu_get_n_columns (menu) - l;
2715 l = gtk_menu_get_n_columns (menu) - r;
2719 child_allocation.width = (r - l) * base_width;
2720 child_allocation.height = 0;
2721 child_allocation.x = l * base_width;
2722 child_allocation.y = 0;
2724 for (i = 0; i < b; i++)
2727 child_allocation.y += priv->heights[i];
2729 child_allocation.height += priv->heights[i];
2732 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2735 gtk_widget_size_allocate (child, &child_allocation);
2736 gtk_widget_queue_draw (child);
2740 /* Resize the item window */
2741 if (gtk_widget_get_realized (widget))
2747 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2748 height += priv->heights[i];
2750 width = gtk_menu_get_n_columns (menu) * base_width;
2751 gdk_window_resize (priv->bin_window, width, height);
2754 if (priv->tearoff_active)
2756 if (height >= priv->requested_height)
2758 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2760 gtk_widget_hide (priv->tearoff_scrollbar);
2761 gtk_menu_set_tearoff_hints (menu, allocation->width);
2763 gtk_menu_scroll_to (menu, 0);
2768 gtk_adjustment_configure (priv->tearoff_adjustment,
2769 gtk_adjustment_get_value (priv->tearoff_adjustment),
2771 priv->requested_height,
2772 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2773 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2774 allocation->height);
2776 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2778 gtk_widget_show (priv->tearoff_scrollbar);
2779 gtk_menu_set_tearoff_hints (menu, allocation->width);
2787 get_arrows_visible_area (GtkMenu *menu,
2788 GdkRectangle *border,
2789 GdkRectangle *upper,
2790 GdkRectangle *lower,
2793 GtkArrowPlacement arrow_placement;
2794 GtkWidget *widget = GTK_WIDGET (menu);
2796 guint vertical_padding;
2797 guint horizontal_padding;
2798 gint scroll_arrow_height;
2799 GtkBorder menu_border;
2801 gtk_widget_style_get (widget,
2802 "vertical-padding", &vertical_padding,
2803 "horizontal-padding", &horizontal_padding,
2804 "scroll-arrow-vlength", &scroll_arrow_height,
2805 "arrow-placement", &arrow_placement,
2808 get_menu_border (widget, &menu_border);
2809 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2810 border->x = border_width + menu_border.left + horizontal_padding;
2811 border->y = border_width + menu_border.top + vertical_padding;
2812 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2813 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2815 switch (arrow_placement)
2817 case GTK_ARROWS_BOTH:
2818 upper->x = border->x;
2819 upper->y = border->y;
2820 upper->width = border->width - 2 * border->x;
2821 upper->height = scroll_arrow_height;
2823 lower->x = border->x;
2824 lower->y = border->height - border->y - scroll_arrow_height;
2825 lower->width = border->width - 2 * border->x;
2826 lower->height = scroll_arrow_height;
2829 case GTK_ARROWS_START:
2830 upper->x = border->x;
2831 upper->y = border->y;
2832 upper->width = (border->width - 2 * border->x) / 2;
2833 upper->height = scroll_arrow_height;
2835 lower->x = border->x + upper->width;
2836 lower->y = border->y;
2837 lower->width = (border->width - 2 * border->x) / 2;
2838 lower->height = scroll_arrow_height;
2841 case GTK_ARROWS_END:
2842 upper->x = border->x;
2843 upper->y = border->height - border->y - scroll_arrow_height;
2844 upper->width = (border->width - 2 * border->x) / 2;
2845 upper->height = scroll_arrow_height;
2847 lower->x = border->x + upper->width;
2848 lower->y = border->height - border->y - scroll_arrow_height;
2849 lower->width = (border->width - 2 * border->x) / 2;
2850 lower->height = scroll_arrow_height;
2854 g_assert_not_reached();
2855 upper->x = upper->y = upper->width = upper->height = 0;
2856 lower->x = lower->y = lower->width = lower->height = 0;
2859 *arrow_space = scroll_arrow_height - menu_border.top - menu_border.bottom;
2863 gtk_menu_draw (GtkWidget *widget,
2867 GtkMenuPrivate *priv;
2868 GtkStyleContext *context;
2869 GdkRectangle border;
2874 GtkStateFlags state;
2875 GtkBorder menu_border;
2877 menu = GTK_MENU (widget);
2879 context = gtk_widget_get_style_context (widget);
2880 window = gtk_widget_get_window (widget);
2881 state = gtk_widget_get_state_flags (widget);
2883 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2884 get_menu_border (widget, &menu_border);
2886 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2888 gfloat arrow_scaling;
2891 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2892 arrow_size = arrow_scaling * arrow_space;
2894 gtk_render_background (context, cr, 0, 0,
2895 gtk_widget_get_allocated_width (widget),
2896 gtk_widget_get_allocated_height (widget));
2897 gtk_render_frame (context, cr, 0, 0,
2898 gtk_widget_get_allocated_width (widget),
2899 gtk_widget_get_allocated_height (widget));
2901 gtk_style_context_save (context);
2902 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
2904 if (priv->upper_arrow_visible && !priv->tearoff_active)
2906 gtk_style_context_save (context);
2907 gtk_style_context_set_state (context, priv->upper_arrow_state);
2909 gtk_render_background (context, cr,
2911 upper.width, upper.height);
2912 gtk_render_frame (context, cr,
2914 upper.width, upper.height);
2916 gtk_render_arrow (context, cr, 0,
2917 upper.x + (upper.width - arrow_size) / 2,
2918 upper.y + menu_border.top + (arrow_space - arrow_size) / 2,
2921 gtk_style_context_restore (context);
2924 if (priv->lower_arrow_visible && !priv->tearoff_active)
2926 gtk_style_context_save (context);
2927 gtk_style_context_set_state (context, priv->lower_arrow_state);
2929 gtk_render_background (context, cr,
2931 lower.width, lower.height);
2932 gtk_render_frame (context, cr,
2934 lower.width, lower.height);
2936 gtk_render_arrow (context, cr, G_PI,
2937 lower.x + (lower.width - arrow_size) / 2,
2938 lower.y + menu_border.top + (arrow_space - arrow_size) / 2,
2941 gtk_style_context_restore (context);
2944 gtk_style_context_restore (context);
2947 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
2949 gint y = -border.y + priv->scroll_offset;
2952 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
2954 if (!priv->tearoff_active)
2956 GtkBorder arrow_border;
2958 get_arrows_border (menu, &arrow_border);
2959 y -= arrow_border.top;
2962 gtk_render_background (context, cr,
2964 border.width, border.height);
2965 gtk_render_frame (context, cr,
2967 border.width, border.height);
2972 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
2978 gtk_menu_show (GtkWidget *widget)
2980 GtkMenu *menu = GTK_MENU (widget);
2982 _gtk_menu_refresh_accel_paths (menu, FALSE);
2984 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2989 gtk_menu_get_preferred_width (GtkWidget *widget,
2994 GtkMenuShell *menu_shell;
2995 GtkMenuPrivate *priv;
2998 guint max_toggle_size;
2999 guint max_accel_width;
3000 guint horizontal_padding;
3002 gint child_min, child_nat;
3003 gint min_width, nat_width;
3006 menu = GTK_MENU (widget);
3007 menu_shell = GTK_MENU_SHELL (widget);
3010 min_width = nat_width = 0;
3012 max_toggle_size = 0;
3013 max_accel_width = 0;
3015 children = menu_shell->priv->children;
3022 child = children->data;
3023 children = children->next;
3025 if (! gtk_widget_get_visible (child))
3028 get_effective_child_attach (child, &l, &r, &t, &b);
3030 /* It's important to size_request the child
3031 * before doing the toggle size request, in
3032 * case the toggle size request depends on the size
3033 * request of a child of the child (e.g. for ImageMenuItem)
3035 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3037 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3038 max_toggle_size = MAX (max_toggle_size, toggle_size);
3039 max_accel_width = MAX (max_accel_width,
3040 GTK_MENU_ITEM (child)->priv->accelerator_width);
3042 part = child_min / (r - l);
3043 min_width = MAX (min_width, part);
3045 part = child_nat / (r - l);
3046 nat_width = MAX (nat_width, part);
3049 /* If the menu doesn't include any images or check items
3050 * reserve the space so that all menus are consistent.
3051 * We only do this for 'ordinary' menus, not for combobox
3052 * menus or multi-column menus
3054 if (max_toggle_size == 0 &&
3055 gtk_menu_get_n_columns (menu) == 1 &&
3056 !priv->no_toggle_size)
3058 GtkStyleContext *context;
3059 GtkWidgetPath *menu_path, *check_path;
3060 guint toggle_spacing;
3061 guint indicator_size;
3063 context = gtk_widget_get_style_context (widget);
3064 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3066 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3067 check_path = gtk_widget_path_copy (menu_path);
3068 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3070 gtk_style_context_set_path (context, check_path);
3071 gtk_widget_path_free (check_path);
3073 gtk_style_context_get_style (context,
3074 "toggle-spacing", &toggle_spacing,
3075 "indicator-size", &indicator_size,
3078 max_toggle_size = indicator_size + toggle_spacing;
3080 /* Restore real widget path */
3081 gtk_style_context_set_path (context, menu_path);
3082 gtk_widget_path_free (menu_path);
3085 min_width += 2 * max_toggle_size + max_accel_width;
3086 min_width *= gtk_menu_get_n_columns (menu);
3088 nat_width += 2 * max_toggle_size + max_accel_width;
3089 nat_width *= gtk_menu_get_n_columns (menu);
3091 gtk_widget_style_get (GTK_WIDGET (menu),
3092 "horizontal-padding", &horizontal_padding,
3095 get_menu_border (widget, &border);
3096 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3097 min_width += (2 * (border_width + horizontal_padding)) +
3098 border.left + border.right;
3099 nat_width += (2 * (border_width + horizontal_padding)) +
3100 border.top + border.bottom;
3102 priv->toggle_size = max_toggle_size;
3103 priv->accel_size = max_accel_width;
3106 *minimum_size = min_width;
3109 *natural_size = nat_width;
3111 /* Don't resize the tearoff if it is not active,
3112 * because it won't redraw (it is only a background pixmap).
3114 if (priv->tearoff_active)
3115 gtk_menu_set_tearoff_hints (menu, min_width);
3119 gtk_menu_get_preferred_height (GtkWidget *widget,
3125 /* Menus are height-for-width only, just return the height
3126 * for the minimum width
3128 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3129 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3133 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3138 GtkStyleContext *context;
3139 GtkStateFlags state;
3140 GtkBorder padding, border;
3141 GtkMenu *menu = GTK_MENU (widget);
3142 GtkMenuPrivate *priv = menu->priv;
3143 guint *min_heights, *nat_heights;
3144 guint vertical_padding, border_width;
3146 gint min_height, nat_height;
3148 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3149 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3151 context = gtk_widget_get_style_context (widget);
3152 state = gtk_widget_get_state_flags (widget);
3153 gtk_style_context_get_padding (context, state, &padding);
3154 gtk_style_context_get_border (context, state, &border);
3156 min_height = nat_height = (border_width + vertical_padding) * 2 +
3157 padding.left + padding.right + border.left + border.right;
3160 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3162 for (i = 0; i < n_heights; i++)
3164 min_height += min_heights[i];
3165 nat_height += nat_heights[i];
3168 if (priv->have_position)
3170 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3171 GdkRectangle monitor;
3173 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
3175 if (priv->position_y + min_height > monitor.y + monitor.height)
3176 min_height = monitor.y + monitor.height - priv->position_y;
3178 if (priv->position_y + nat_height > monitor.y + monitor.height)
3179 nat_height = monitor.y + monitor.height - priv->position_y;
3181 if (priv->position_y < monitor.y)
3183 min_height -= monitor.y - priv->position_y;
3184 nat_height -= monitor.y - priv->position_y;
3189 *minimum_size = min_height;
3192 *natural_size = nat_height;
3194 g_free (min_heights);
3195 g_free (nat_heights);
3201 gtk_menu_button_scroll (GtkMenu *menu,
3202 GdkEventButton *event)
3204 GtkMenuPrivate *priv = menu->priv;
3206 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3208 gboolean touchscreen_mode;
3210 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3211 "gtk-touchscreen-mode", &touchscreen_mode,
3214 if (touchscreen_mode)
3215 gtk_menu_handle_scrolling (menu,
3216 event->x_root, event->y_root,
3217 event->type == GDK_BUTTON_PRESS,
3227 pointer_in_menu_window (GtkWidget *widget,
3231 GtkMenu *menu = GTK_MENU (widget);
3232 GtkMenuPrivate *priv = menu->priv;
3233 GtkAllocation allocation;
3235 if (gtk_widget_get_mapped (priv->toplevel))
3237 GtkMenuShell *menu_shell;
3238 gint window_x, window_y;
3240 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3241 &window_x, &window_y);
3243 gtk_widget_get_allocation (widget, &allocation);
3244 if (x_root >= window_x && x_root < window_x + allocation.width &&
3245 y_root >= window_y && y_root < window_y + allocation.height)
3248 menu_shell = GTK_MENU_SHELL (widget);
3250 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3251 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3259 gtk_menu_button_press (GtkWidget *widget,
3260 GdkEventButton *event)
3262 if (event->type != GDK_BUTTON_PRESS)
3265 /* Don't pass down to menu shell for presses over scroll arrows
3267 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3270 /* Don't pass down to menu shell if a non-menuitem part of the menu
3271 * was clicked. The check for the event_widget being a GtkMenuShell
3272 * works because we have the pointer grabbed on menu_shell->window
3273 * with owner_events=TRUE, so all events that are either outside
3274 * the menu or on its border are delivered relative to
3275 * menu_shell->window.
3277 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3278 pointer_in_menu_window (widget, event->x_root, event->y_root))
3281 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3285 gtk_menu_button_release (GtkWidget *widget,
3286 GdkEventButton *event)
3288 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3290 if (priv->ignore_button_release)
3292 priv->ignore_button_release = FALSE;
3296 if (event->type != GDK_BUTTON_RELEASE)
3299 /* Don't pass down to menu shell for releases over scroll arrows
3301 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3304 /* Don't pass down to menu shell if a non-menuitem part of the menu
3305 * was clicked (see comment in button_press()).
3307 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3308 pointer_in_menu_window (widget, event->x_root, event->y_root))
3310 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3311 * bail out early here so it is in a consistent state for the
3312 * next button_press/button_release in GtkMenuShell.
3315 if (GTK_MENU_SHELL (widget)->priv->active)
3316 GTK_MENU_SHELL (widget)->priv->button = 0;
3321 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3324 static const gchar *
3325 get_accel_path (GtkWidget *menu_item,
3330 GClosure *accel_closure;
3331 GtkAccelGroup *accel_group;
3333 path = _gtk_widget_get_accel_path (menu_item, locked);
3336 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3342 label = gtk_bin_get_child (GTK_BIN (menu_item));
3344 if (GTK_IS_ACCEL_LABEL (label))
3346 g_object_get (label,
3347 "accel-closure", &accel_closure,
3351 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3353 *locked = gtk_accel_group_get_is_locked (accel_group);
3363 gtk_menu_key_press (GtkWidget *widget,
3366 GtkMenuShell *menu_shell;
3368 gboolean delete = FALSE;
3369 gboolean can_change_accels;
3370 gchar *accel = NULL;
3371 guint accel_key, accel_mods;
3372 GdkModifierType consumed_modifiers;
3373 GdkDisplay *display;
3375 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3376 g_return_val_if_fail (event != NULL, FALSE);
3378 menu_shell = GTK_MENU_SHELL (widget);
3379 menu = GTK_MENU (widget);
3381 gtk_menu_stop_navigating_submenu (menu);
3383 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3386 display = gtk_widget_get_display (widget);
3388 g_object_get (gtk_widget_get_settings (widget),
3389 "gtk-menu-bar-accel", &accel,
3390 "gtk-can-change-accels", &can_change_accels,
3393 if (accel && *accel)
3396 GdkModifierType mods = 0;
3398 gtk_accelerator_parse (accel, &keyval, &mods);
3401 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3403 /* FIXME this is wrong, needs to be in the global accel resolution
3404 * thing, to properly consider i18n etc., but that probably requires
3405 * AccelGroup changes etc.
3407 if (event->keyval == keyval && (mods & event->state) == mods)
3409 gtk_menu_shell_cancel (menu_shell);
3417 switch (event->keyval)
3419 case GDK_KEY_Delete:
3420 case GDK_KEY_KP_Delete:
3421 case GDK_KEY_BackSpace:
3428 /* Figure out what modifiers went into determining the key symbol */
3429 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3430 event->hardware_keycode,
3431 event->state, event->group,
3432 NULL, NULL, NULL, &consumed_modifiers);
3434 accel_key = gdk_keyval_to_lower (event->keyval);
3435 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3437 /* If lowercasing affects the keysym, then we need to include SHIFT
3438 * in the modifiers, we re-uppercase when we match against the keyval,
3439 * but display and save in caseless form.
3441 if (accel_key != event->keyval)
3442 accel_mods |= GDK_SHIFT_MASK;
3444 /* Modify the accelerators */
3445 if (can_change_accels &&
3446 menu_shell->priv->active_menu_item &&
3447 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3448 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3449 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3451 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3452 gboolean locked, replace_accels = TRUE;
3455 path = get_accel_path (menu_item, &locked);
3456 if (!path || locked)
3458 /* Can't change accelerators on menu_items without paths
3459 * (basically, those items are accelerator-locked).
3461 gtk_widget_error_bell (widget);
3467 /* For the keys that act to delete the current setting,
3468 * we delete the current setting if there is one, otherwise,
3469 * we set the key as the accelerator.
3475 if (gtk_accel_map_lookup_entry (path, &key) &&
3476 (key.accel_key || key.accel_mods))
3482 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3486 /* We failed, probably because this key is in use
3487 * and locked already.
3489 gtk_widget_error_bell (widget);
3498 check_threshold (GtkWidget *widget,
3507 ABS (start_x - x) > THRESHOLD ||
3508 ABS (start_y - y) > THRESHOLD;
3512 definitely_within_item (GtkWidget *widget,
3516 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3519 w = gdk_window_get_width (window);
3520 h = gdk_window_get_height (window);
3523 check_threshold (widget, 0, 0, x, y) &&
3524 check_threshold (widget, w - 1, 0, x, y) &&
3525 check_threshold (widget, w - 1, h - 1, x, y) &&
3526 check_threshold (widget, 0, h - 1, x, y);
3530 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3532 GtkMenuPrivate *priv = menu->priv;
3534 return priv->navigation_height && priv->navigation_width;
3538 gtk_menu_motion_notify (GtkWidget *widget,
3539 GdkEventMotion *event)
3541 GtkWidget *menu_item;
3543 GtkMenuShell *menu_shell;
3546 gboolean need_enter;
3548 if (GTK_IS_MENU (widget))
3550 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3552 if (priv->ignore_button_release)
3553 priv->ignore_button_release = FALSE;
3555 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3559 /* We received the event for one of two reasons:
3561 * a) We are the active menu, and did gtk_grab_add()
3562 * b) The widget is a child of ours, and the event was propagated
3564 * Since for computation of navigation regions, we want the menu which
3565 * is the parent of the menu item, for a), we need to find that menu,
3566 * which may be different from 'widget'.
3568 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3569 parent = gtk_widget_get_parent (menu_item);
3570 if (!GTK_IS_MENU_ITEM (menu_item) ||
3571 !GTK_IS_MENU (parent))
3574 menu_shell = GTK_MENU_SHELL (parent);
3575 menu = GTK_MENU (menu_shell);
3577 if (definitely_within_item (menu_item, event->x, event->y))
3578 menu_shell->priv->activate_time = 0;
3580 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3582 /* Check to see if we are within an active submenu's navigation region
3584 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3587 /* Make sure we pop down if we enter a non-selectable menu item, so we
3588 * don't show a submenu when the cursor is outside the stay-up triangle.
3590 if (!_gtk_menu_item_is_selectable (menu_item))
3592 /* We really want to deselect, but this gives the menushell code
3593 * a chance to do some bookkeeping about the menuitem.
3595 gtk_menu_shell_select_item (menu_shell, menu_item);
3601 /* The menu is now sensitive to enter events on its items, but
3602 * was previously sensitive. So we fake an enter event.
3604 menu_shell->priv->ignore_enter = FALSE;
3606 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3607 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3609 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3612 send_event->crossing.window = g_object_ref (event->window);
3613 send_event->crossing.time = event->time;
3614 send_event->crossing.send_event = TRUE;
3615 send_event->crossing.x_root = event->x_root;
3616 send_event->crossing.y_root = event->y_root;
3617 send_event->crossing.x = event->x;
3618 send_event->crossing.y = event->y;
3619 send_event->crossing.state = event->state;
3620 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3622 /* We send the event to 'widget', the currently active menu,
3623 * instead of 'menu', the menu that the pointer is in. This
3624 * will ensure that the event will be ignored unless the
3625 * menuitem is a child of the active menu or some parent
3626 * menu of the active menu.
3628 result = gtk_widget_event (widget, send_event);
3629 gdk_event_free (send_event);
3639 get_double_arrows (GtkMenu *menu)
3641 GtkMenuPrivate *priv = menu->priv;
3642 gboolean double_arrows;
3643 GtkArrowPlacement arrow_placement;
3645 gtk_widget_style_get (GTK_WIDGET (menu),
3646 "double-arrows", &double_arrows,
3647 "arrow-placement", &arrow_placement,
3650 if (arrow_placement != GTK_ARROWS_BOTH)
3653 return double_arrows || (priv->initially_pushed_in &&
3654 priv->scroll_offset != 0);
3658 gtk_menu_scroll_by (GtkMenu *menu,
3661 GtkMenuPrivate *priv = menu->priv;
3662 GtkBorder arrow_border;
3666 gboolean double_arrows;
3668 widget = GTK_WIDGET (menu);
3669 offset = priv->scroll_offset + step;
3671 get_arrows_border (menu, &arrow_border);
3673 double_arrows = get_double_arrows (menu);
3675 /* If we scroll upward and the non-visible top part
3676 * is smaller than the scroll arrow it would be
3677 * pretty stupid to show the arrow and taking more
3678 * screen space than just scrolling to the top.
3681 if ((step < 0) && (offset < arrow_border.top))
3684 /* Don't scroll over the top if we weren't before: */
3685 if ((priv->scroll_offset >= 0) && (offset < 0))
3688 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3690 if (priv->scroll_offset == 0 &&
3691 view_height >= priv->requested_height)
3694 /* Don't scroll past the bottom if we weren't before: */
3695 if (priv->scroll_offset > 0)
3696 view_height -= arrow_border.top;
3698 /* When both arrows are always shown,
3699 * reduce view height even more.
3702 view_height -= arrow_border.bottom;
3704 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3705 (offset + view_height > priv->requested_height))
3706 offset = priv->requested_height - view_height;
3708 if (offset != priv->scroll_offset)
3709 gtk_menu_scroll_to (menu, offset);
3713 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3714 gboolean touchscreen_mode)
3716 GtkMenuPrivate *priv = menu->priv;
3717 gboolean upper_visible;
3718 gboolean lower_visible;
3720 upper_visible = priv->upper_arrow_visible;
3721 lower_visible = priv->lower_arrow_visible;
3723 gtk_menu_scroll_by (menu, priv->scroll_step);
3725 if (touchscreen_mode &&
3726 (upper_visible != priv->upper_arrow_visible ||
3727 lower_visible != priv->lower_arrow_visible))
3729 /* We are about to hide a scroll arrow while the mouse is pressed,
3730 * this would cause the uncovered menu item to be activated on button
3731 * release. Therefore we need to ignore button release here
3733 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3734 priv->ignore_button_release = TRUE;
3739 gtk_menu_scroll_timeout (gpointer data)
3742 gboolean touchscreen_mode;
3744 menu = GTK_MENU (data);
3746 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3747 "gtk-touchscreen-mode", &touchscreen_mode,
3750 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3756 gtk_menu_scroll_timeout_initial (gpointer data)
3760 gboolean touchscreen_mode;
3762 menu = GTK_MENU (data);
3764 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3765 "gtk-timeout-repeat", &timeout,
3766 "gtk-touchscreen-mode", &touchscreen_mode,
3769 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3771 gtk_menu_remove_scroll_timeout (menu);
3773 menu->priv->scroll_timeout =
3774 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3780 gtk_menu_start_scrolling (GtkMenu *menu)
3783 gboolean touchscreen_mode;
3785 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3786 "gtk-timeout-repeat", &timeout,
3787 "gtk-touchscreen-mode", &touchscreen_mode,
3790 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3792 menu->priv->scroll_timeout =
3793 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3797 gtk_menu_scroll (GtkWidget *widget,
3798 GdkEventScroll *event)
3800 GtkMenu *menu = GTK_MENU (widget);
3802 switch (event->direction)
3804 case GDK_SCROLL_RIGHT:
3805 case GDK_SCROLL_DOWN:
3806 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3808 case GDK_SCROLL_LEFT:
3810 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3818 get_arrows_sensitive_area (GtkMenu *menu,
3819 GdkRectangle *upper,
3820 GdkRectangle *lower)
3822 GtkArrowPlacement arrow_placement;
3823 GtkWidget *widget = GTK_WIDGET (menu);
3827 guint vertical_padding;
3829 gint scroll_arrow_height;
3830 GtkStyleContext *context;
3831 GtkStateFlags state;
3834 window = gtk_widget_get_window (widget);
3835 width = gdk_window_get_width (window);
3836 height = gdk_window_get_height (window);
3838 gtk_widget_style_get (widget,
3839 "vertical-padding", &vertical_padding,
3840 "scroll-arrow-vlength", &scroll_arrow_height,
3841 "arrow-placement", &arrow_placement,
3844 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3846 context = gtk_widget_get_style_context (widget);
3847 state = gtk_widget_get_state_flags (widget);
3848 gtk_style_context_get_padding (context, state, &padding);
3850 gdk_window_get_position (window, &win_x, &win_y);
3852 switch (arrow_placement)
3854 case GTK_ARROWS_BOTH:
3859 upper->width = width;
3860 upper->height = scroll_arrow_height + border + padding.top;
3866 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3867 lower->width = width;
3868 lower->height = scroll_arrow_height + border + padding.bottom;
3872 case GTK_ARROWS_START:
3877 upper->width = width / 2;
3878 upper->height = scroll_arrow_height + border + padding.top;
3883 lower->x = win_x + width / 2;
3885 lower->width = width / 2;
3886 lower->height = scroll_arrow_height + border + padding.bottom;
3890 case GTK_ARROWS_END:
3894 upper->y = win_y + height - border - scroll_arrow_height;
3895 upper->width = width / 2;
3896 upper->height = scroll_arrow_height + border + padding.top;
3901 lower->x = win_x + width / 2;
3902 lower->y = win_y + height - border - scroll_arrow_height;
3903 lower->width = width / 2;
3904 lower->height = scroll_arrow_height + border + padding.bottom;
3912 gtk_menu_handle_scrolling (GtkMenu *menu,
3918 GtkMenuPrivate *priv = menu->priv;
3919 GtkMenuShell *menu_shell;
3922 gboolean scroll_fast = FALSE;
3924 gboolean touchscreen_mode;
3926 menu_shell = GTK_MENU_SHELL (menu);
3928 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3929 "gtk-touchscreen-mode", &touchscreen_mode,
3932 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3937 /* upper arrow handling */
3939 get_arrows_sensitive_area (menu, &rect, NULL);
3942 if (priv->upper_arrow_visible && !priv->tearoff_active &&
3943 (x >= rect.x) && (x < rect.x + rect.width) &&
3944 (y >= rect.y) && (y < rect.y + rect.height))
3949 if (touchscreen_mode)
3950 priv->upper_arrow_prelight = in_arrow;
3952 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
3954 gboolean arrow_pressed = FALSE;
3956 if (priv->upper_arrow_visible && !priv->tearoff_active)
3958 if (touchscreen_mode)
3960 if (enter && priv->upper_arrow_prelight)
3962 if (priv->scroll_timeout == 0)
3964 /* Deselect the active item so that
3965 * any submenus are popped down
3967 gtk_menu_shell_deselect (menu_shell);
3969 gtk_menu_remove_scroll_timeout (menu);
3970 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3974 /* Only do stuff on click. */
3975 gtk_menu_start_scrolling (menu);
3976 arrow_pressed = TRUE;
3981 arrow_pressed = TRUE;
3986 gtk_menu_stop_scrolling (menu);
3989 else /* !touchscreen_mode */
3991 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3993 if (enter && in_arrow &&
3994 (!priv->upper_arrow_prelight ||
3995 priv->scroll_fast != scroll_fast))
3997 priv->upper_arrow_prelight = TRUE;
3998 priv->scroll_fast = scroll_fast;
4000 /* Deselect the active item so that
4001 * any submenus are popped down
4003 gtk_menu_shell_deselect (menu_shell);
4005 gtk_menu_remove_scroll_timeout (menu);
4006 priv->scroll_step = scroll_fast
4007 ? -MENU_SCROLL_STEP2
4008 : -MENU_SCROLL_STEP1;
4010 priv->scroll_timeout =
4011 gdk_threads_add_timeout (scroll_fast
4012 ? MENU_SCROLL_TIMEOUT2
4013 : MENU_SCROLL_TIMEOUT1,
4014 gtk_menu_scroll_timeout, menu);
4016 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4018 gtk_menu_stop_scrolling (menu);
4023 /* gtk_menu_start_scrolling() might have hit the top of the
4024 * menu, so check if the button isn't insensitive before
4025 * changing it to something else.
4027 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4029 GtkStateFlags arrow_state = 0;
4032 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4034 if (priv->upper_arrow_prelight)
4035 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4037 if (arrow_state != priv->upper_arrow_state)
4039 priv->upper_arrow_state = arrow_state;
4041 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4047 /* lower arrow handling */
4049 get_arrows_sensitive_area (menu, NULL, &rect);
4052 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4053 (x >= rect.x) && (x < rect.x + rect.width) &&
4054 (y >= rect.y) && (y < rect.y + rect.height))
4059 if (touchscreen_mode)
4060 priv->lower_arrow_prelight = in_arrow;
4062 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4064 gboolean arrow_pressed = FALSE;
4066 if (priv->lower_arrow_visible && !priv->tearoff_active)
4068 if (touchscreen_mode)
4070 if (enter && priv->lower_arrow_prelight)
4072 if (priv->scroll_timeout == 0)
4074 /* Deselect the active item so that
4075 * any submenus are popped down
4077 gtk_menu_shell_deselect (menu_shell);
4079 gtk_menu_remove_scroll_timeout (menu);
4080 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4084 /* Only do stuff on click. */
4085 gtk_menu_start_scrolling (menu);
4086 arrow_pressed = TRUE;
4091 arrow_pressed = TRUE;
4096 gtk_menu_stop_scrolling (menu);
4099 else /* !touchscreen_mode */
4101 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4103 if (enter && in_arrow &&
4104 (!priv->lower_arrow_prelight ||
4105 priv->scroll_fast != scroll_fast))
4107 priv->lower_arrow_prelight = TRUE;
4108 priv->scroll_fast = scroll_fast;
4110 /* Deselect the active item so that
4111 * any submenus are popped down
4113 gtk_menu_shell_deselect (menu_shell);
4115 gtk_menu_remove_scroll_timeout (menu);
4116 priv->scroll_step = scroll_fast
4118 : MENU_SCROLL_STEP1;
4120 priv->scroll_timeout =
4121 gdk_threads_add_timeout (scroll_fast
4122 ? MENU_SCROLL_TIMEOUT2
4123 : MENU_SCROLL_TIMEOUT1,
4124 gtk_menu_scroll_timeout, menu);
4126 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4128 gtk_menu_stop_scrolling (menu);
4133 /* gtk_menu_start_scrolling() might have hit the bottom of the
4134 * menu, so check if the button isn't insensitive before
4135 * changing it to something else.
4137 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4139 GtkStateFlags arrow_state = 0;
4142 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4144 if (priv->lower_arrow_prelight)
4145 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4147 if (arrow_state != priv->lower_arrow_state)
4149 priv->lower_arrow_state = arrow_state;
4151 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4159 gtk_menu_enter_notify (GtkWidget *widget,
4160 GdkEventCrossing *event)
4162 GtkWidget *menu_item;
4164 gboolean touchscreen_mode;
4166 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4167 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4168 event->mode == GDK_CROSSING_STATE_CHANGED)
4171 g_object_get (gtk_widget_get_settings (widget),
4172 "gtk-touchscreen-mode", &touchscreen_mode,
4175 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4176 if (GTK_IS_MENU (widget))
4178 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4180 if (!menu_shell->priv->ignore_enter)
4181 gtk_menu_handle_scrolling (GTK_MENU (widget),
4182 event->x_root, event->y_root, TRUE, TRUE);
4185 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4187 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4189 if (GTK_IS_MENU (menu))
4191 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4192 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4194 if (priv->seen_item_enter)
4196 /* This is the second enter we see for an item
4197 * on this menu. This means a release should always
4200 menu_shell->priv->activate_time = 0;
4202 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4203 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4205 if (definitely_within_item (menu_item, event->x, event->y))
4207 /* This is an actual user-enter (ie. not a pop-under)
4208 * In this case, the user must either have entered
4209 * sufficiently far enough into the item, or he must move
4210 * far enough away from the enter point. (see
4211 * gtk_menu_motion_notify())
4213 menu_shell->priv->activate_time = 0;
4217 priv->seen_item_enter = TRUE;
4221 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4222 * will not correspond to the event widget's parent. Check to see
4223 * if we are in the parent's navigation region.
4225 parent = gtk_widget_get_parent (menu_item);
4226 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4227 gtk_menu_navigating_submenu (GTK_MENU (parent),
4228 event->x_root, event->y_root))
4231 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4235 gtk_menu_leave_notify (GtkWidget *widget,
4236 GdkEventCrossing *event)
4238 GtkMenuShell *menu_shell;
4240 GtkMenuItem *menu_item;
4241 GtkWidget *event_widget;
4243 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4244 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4245 event->mode == GDK_CROSSING_STATE_CHANGED)
4248 menu = GTK_MENU (widget);
4249 menu_shell = GTK_MENU_SHELL (widget);
4251 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4254 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4256 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4258 if (!GTK_IS_MENU_ITEM (event_widget))
4261 menu_item = GTK_MENU_ITEM (event_widget);
4263 /* Here we check to see if we're leaving an active menu item
4264 * with a submenu, in which case we enter submenu navigation mode.
4266 if (menu_shell->priv->active_menu_item != NULL
4267 && menu_item->priv->submenu != NULL
4268 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4270 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4272 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4275 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4277 /* We are leaving an active menu item with nonactive submenu.
4278 * Deselect it so we don't surprise the user with by popping
4279 * up a submenu _after_ he left the item.
4281 gtk_menu_shell_deselect (menu_shell);
4286 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4290 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4292 GtkMenuPrivate *priv = menu->priv;
4294 priv->navigation_x = 0;
4295 priv->navigation_y = 0;
4296 priv->navigation_width = 0;
4297 priv->navigation_height = 0;
4299 if (priv->navigation_timeout)
4301 g_source_remove (priv->navigation_timeout);
4302 priv->navigation_timeout = 0;
4306 /* When the timeout is elapsed, the navigation region is destroyed
4307 * and the menuitem under the pointer (if any) is selected.
4310 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4312 GtkMenuPopdownData *popdown_data = user_data;
4313 GtkMenu *menu = popdown_data->menu;
4314 GtkMenuPrivate *priv = menu->priv;
4315 GdkWindow *child_window;
4317 gtk_menu_stop_navigating_submenu (menu);
4319 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4321 child_window = gdk_window_get_device_position (priv->bin_window,
4322 popdown_data->device,
4327 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4329 send_event->crossing.window = g_object_ref (child_window);
4330 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4331 send_event->crossing.send_event = TRUE;
4332 gdk_event_set_device (send_event, popdown_data->device);
4334 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4336 gdk_event_free (send_event);
4344 gtk_menu_navigating_submenu (GtkMenu *menu,
4348 GtkMenuPrivate *priv = menu->priv;
4351 if (!gtk_menu_has_navigation_triangle (menu))
4354 width = priv->navigation_width;
4355 height = priv->navigation_height;
4357 /* Check if x/y are in the triangle spanned by the navigation parameters */
4359 /* 1) Move the coordinates so the triangle starts at 0,0 */
4360 event_x -= priv->navigation_x;
4361 event_y -= priv->navigation_y;
4363 /* 2) Ensure both legs move along the positive axis */
4375 /* 3) Check that the given coordinate is inside the triangle. The formula
4376 * is a transformed form of this formula: x/w + y/h <= 1
4378 if (event_x >= 0 && event_y >= 0 &&
4379 event_x * height + event_y * width <= width * height)
4385 gtk_menu_stop_navigating_submenu (menu);
4391 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4392 GtkMenuItem *menu_item,
4393 GdkEventCrossing *event)
4395 GtkMenuPrivate *priv = menu->priv;
4396 gint submenu_left = 0;
4397 gint submenu_right = 0;
4398 gint submenu_top = 0;
4399 gint submenu_bottom = 0;
4402 GtkWidget *event_widget;
4403 GtkMenuPopdownData *popdown_data;
4406 g_return_if_fail (menu_item->priv->submenu != NULL);
4407 g_return_if_fail (event != NULL);
4409 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4411 window = gtk_widget_get_window (menu_item->priv->submenu);
4412 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4414 submenu_right = submenu_left + gdk_window_get_width (window);
4415 submenu_bottom = submenu_top + gdk_window_get_height (window);
4417 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4418 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4420 if (event->x >= 0 && event->x < width)
4424 gtk_menu_stop_navigating_submenu (menu);
4426 /* The navigation region is the triangle closest to the x/y
4427 * location of the rectangle. This is why the width or height
4430 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4433 priv->navigation_x = submenu_left;
4434 priv->navigation_width = event->x_root - submenu_left;
4439 priv->navigation_x = submenu_right;
4440 priv->navigation_width = event->x_root - submenu_right;
4446 priv->navigation_y = event->y_root;
4447 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4449 if (priv->navigation_height >= 0)
4455 priv->navigation_y = event->y_root;
4456 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4458 if (priv->navigation_height <= 0)
4462 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4463 "gtk-menu-popdown-delay", &popdown_delay,
4466 popdown_data = g_new (GtkMenuPopdownData, 1);
4467 popdown_data->menu = menu;
4468 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4470 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4472 gtk_menu_stop_navigating_submenu_cb,
4474 (GDestroyNotify) g_free);
4479 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4483 g_return_if_fail (GTK_IS_MENU (menu_shell));
4485 parent = menu_shell->priv->parent_menu_shell;
4487 menu_shell->priv->activate_time = 0;
4488 gtk_menu_popdown (GTK_MENU (menu_shell));
4491 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4495 gtk_menu_position (GtkMenu *menu,
4496 gboolean set_scroll_offset)
4498 GtkMenuPrivate *priv = menu->priv;
4500 GtkRequisition requisition;
4505 GdkScreen *pointer_screen;
4506 GdkRectangle monitor;
4509 widget = GTK_WIDGET (menu);
4511 screen = gtk_widget_get_screen (widget);
4512 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4513 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4515 /* Get the minimum height for minimum width to figure out
4516 * the right place to popup the menu.
4518 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4520 if (pointer_screen != screen)
4522 /* Pointer is on a different screen; roughly center the
4523 * menu on the screen. If someone was using multiscreen
4524 * + Xinerama together they'd probably want something
4525 * fancier; but that is likely to be vanishingly rare.
4527 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4528 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4531 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4532 priv->initially_pushed_in = FALSE;
4534 /* Set the type hint here to allow custom position functions
4535 * to set a different hint
4537 if (!gtk_widget_get_visible (priv->toplevel))
4538 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4540 if (priv->position_func)
4542 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4543 priv->position_func_data);
4545 if (priv->monitor_num < 0)
4546 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4548 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4552 gint space_left, space_right, space_above, space_below;
4556 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4558 get_menu_border (widget, &border);
4560 /* The placement of popup menus horizontally works like this (with
4561 * RTL in parentheses)
4563 * - If there is enough room to the right (left) of the mouse cursor,
4564 * position the menu there.
4566 * - Otherwise, if if there is enough room to the left (right) of the
4567 * mouse cursor, position the menu there.
4569 * - Otherwise if the menu is smaller than the monitor, position it
4570 * on the side of the mouse cursor that has the most space available
4572 * - Otherwise (if there is simply not enough room for the menu on the
4573 * monitor), position it as far left (right) as possible.
4575 * Positioning in the vertical direction is similar: first try below
4576 * mouse cursor, then above.
4578 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4580 space_left = x - monitor.x;
4581 space_right = monitor.x + monitor.width - x - 1;
4582 space_above = y - monitor.y;
4583 space_below = monitor.y + monitor.height - y - 1;
4585 /* Position horizontally. */
4587 /* the amount of space we need to position the menu.
4588 * Note the menu is offset "thickness" pixels
4590 needed_width = requisition.width - border.left;
4592 if (needed_width <= space_left ||
4593 needed_width <= space_right)
4595 if ((rtl && needed_width <= space_left) ||
4596 (!rtl && needed_width > space_right))
4599 x = x + border.left - requisition.width + 1;
4603 /* position right */
4604 x = x - border.right;
4607 /* x is clamped on-screen further down */
4609 else if (requisition.width <= monitor.width)
4611 /* the menu is too big to fit on either side of the mouse
4612 * cursor, but smaller than the monitor. Position it on
4613 * the side that has the most space
4615 if (space_left > space_right)
4623 x = monitor.x + monitor.width - requisition.width;
4626 else /* menu is simply too big for the monitor */
4631 x = monitor.x + monitor.width - requisition.width;
4640 /* Position vertically.
4641 * The algorithm is the same as above, but simpler
4642 * because we don't have to take RTL into account.
4644 needed_height = requisition.height - border.top;
4646 if (needed_height <= space_above ||
4647 needed_height <= space_below)
4649 if (needed_height <= space_below)
4652 y = y + border.bottom - requisition.height + 1;
4654 y = CLAMP (y, monitor.y,
4655 monitor.y + monitor.height - requisition.height);
4657 else if (needed_height > space_below && needed_height > space_above)
4659 if (space_below >= space_above)
4660 y = monitor.y + monitor.height - requisition.height;
4672 if (priv->initially_pushed_in)
4674 menu_height = requisition.height;
4676 if (y + menu_height > monitor.y + monitor.height)
4678 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4679 y = (monitor.y + monitor.height) - menu_height;
4684 scroll_offset += monitor.y - y;
4689 /* FIXME: should this be done in the various position_funcs ? */
4690 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4692 if (GTK_MENU_SHELL (menu)->priv->active)
4694 priv->have_position = TRUE;
4695 priv->position_x = x;
4696 priv->position_y = y;
4699 if (y + requisition.height > monitor.y + monitor.height)
4700 requisition.height = (monitor.y + monitor.height) - y;
4704 scroll_offset += monitor.y - y;
4705 requisition.height -= monitor.y - y;
4709 if (scroll_offset > 0)
4711 GtkBorder arrow_border;
4713 get_arrows_border (menu, &arrow_border);
4714 scroll_offset += arrow_border.top;
4717 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4720 if (!GTK_MENU_SHELL (menu)->priv->active)
4722 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4723 requisition.width, requisition.height);
4726 if (set_scroll_offset)
4727 priv->scroll_offset = scroll_offset;
4731 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4733 GtkMenuPrivate *priv = menu->priv;
4735 if (priv->scroll_timeout)
4737 g_source_remove (priv->scroll_timeout);
4738 priv->scroll_timeout = 0;
4743 gtk_menu_stop_scrolling (GtkMenu *menu)
4745 GtkMenuPrivate *priv = menu->priv;
4746 gboolean touchscreen_mode;
4748 gtk_menu_remove_scroll_timeout (menu);
4750 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4751 "gtk-touchscreen-mode", &touchscreen_mode,
4754 if (!touchscreen_mode)
4756 priv->upper_arrow_prelight = FALSE;
4757 priv->lower_arrow_prelight = FALSE;
4762 gtk_menu_scroll_to (GtkMenu *menu,
4765 GtkMenuPrivate *priv = menu->priv;
4766 GtkAllocation allocation;
4767 GtkBorder arrow_border, border;
4770 gint view_width, view_height;
4773 guint vertical_padding;
4774 guint horizontal_padding;
4775 gboolean double_arrows;
4777 widget = GTK_WIDGET (menu);
4779 if (priv->tearoff_active && priv->tearoff_adjustment)
4780 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4782 /* Move/resize the viewport according to arrows: */
4783 gtk_widget_get_allocation (widget, &allocation);
4784 view_width = allocation.width;
4785 view_height = allocation.height;
4787 gtk_widget_style_get (GTK_WIDGET (menu),
4788 "vertical-padding", &vertical_padding,
4789 "horizontal-padding", &horizontal_padding,
4792 get_menu_border (widget, &border);
4793 double_arrows = get_double_arrows (menu);
4795 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4797 view_width -= (2 * (border_width + horizontal_padding)) + border.left + border.right;
4798 view_height -= (2 * (border_width + vertical_padding)) + border.top + border.bottom;
4799 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4800 border.top - border.bottom;
4802 x = border_width + border.left + horizontal_padding;
4803 y = border_width + border.top + vertical_padding;
4805 if (double_arrows && !priv->tearoff_active)
4807 if (view_height < menu_height ||
4808 (offset > 0 && priv->scroll_offset > 0) ||
4809 (offset < 0 && priv->scroll_offset < 0))
4811 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4812 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4814 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4815 gtk_widget_queue_draw (GTK_WIDGET (menu));
4817 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4819 get_arrows_border (menu, &arrow_border);
4820 y += arrow_border.top;
4821 view_height -= arrow_border.top;
4822 view_height -= arrow_border.bottom;
4825 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4828 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4830 if (priv->upper_arrow_prelight)
4831 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4833 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4836 if (offset >= menu_height - view_height)
4837 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4840 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4842 if (priv->lower_arrow_prelight)
4843 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4845 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4848 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4849 (priv->lower_arrow_state != lower_arrow_previous_state))
4850 gtk_widget_queue_draw (GTK_WIDGET (menu));
4852 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4853 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4855 /* At the upper border, possibly remove timeout */
4856 if (priv->scroll_step < 0)
4858 gtk_menu_stop_scrolling (menu);
4859 gtk_widget_queue_draw (GTK_WIDGET (menu));
4863 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4864 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4866 /* At the lower border, possibly remove timeout */
4867 if (priv->scroll_step > 0)
4869 gtk_menu_stop_scrolling (menu);
4870 gtk_widget_queue_draw (GTK_WIDGET (menu));
4874 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4878 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4879 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4881 gtk_menu_stop_scrolling (menu);
4882 gtk_widget_queue_draw (GTK_WIDGET (menu));
4885 else if (!priv->tearoff_active)
4887 gboolean last_visible;
4889 last_visible = priv->upper_arrow_visible;
4890 priv->upper_arrow_visible = offset > 0;
4892 /* upper_arrow_visible may have changed, so requery the border */
4893 get_arrows_border (menu, &arrow_border);
4894 view_height -= arrow_border.top;
4896 if ((last_visible != priv->upper_arrow_visible) &&
4897 !priv->upper_arrow_visible)
4899 priv->upper_arrow_prelight = FALSE;
4901 /* If we hide the upper arrow, possibly remove timeout */
4902 if (priv->scroll_step < 0)
4904 gtk_menu_stop_scrolling (menu);
4905 gtk_widget_queue_draw (GTK_WIDGET (menu));
4909 last_visible = priv->lower_arrow_visible;
4910 priv->lower_arrow_visible = offset < menu_height - view_height;
4912 /* lower_arrow_visible may have changed, so requery the border */
4913 get_arrows_border (menu, &arrow_border);
4914 view_height -= arrow_border.bottom;
4916 if ((last_visible != priv->lower_arrow_visible) &&
4917 !priv->lower_arrow_visible)
4919 priv->lower_arrow_prelight = FALSE;
4921 /* If we hide the lower arrow, possibly remove timeout */
4922 if (priv->scroll_step > 0)
4924 gtk_menu_stop_scrolling (menu);
4925 gtk_widget_queue_draw (GTK_WIDGET (menu));
4929 y += arrow_border.top;
4932 /* Scroll the menu: */
4933 if (gtk_widget_get_realized (widget))
4934 gdk_window_move (priv->bin_window, 0, -offset);
4936 if (gtk_widget_get_realized (widget))
4937 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
4939 priv->scroll_offset = offset;
4943 compute_child_offset (GtkMenu *menu,
4944 GtkWidget *menu_item,
4947 gboolean *is_last_child)
4949 GtkMenuPrivate *priv = menu->priv;
4950 gint item_top_attach;
4951 gint item_bottom_attach;
4952 gint child_offset = 0;
4955 get_effective_child_attach (menu_item, NULL, NULL,
4956 &item_top_attach, &item_bottom_attach);
4958 /* there is a possibility that we get called before _size_request,
4959 * so check the height table for safety.
4961 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4964 /* when we have a row with only invisible children, it's height will
4965 * be zero, so there's no need to check WIDGET_VISIBLE here
4967 for (i = 0; i < item_top_attach; i++)
4968 child_offset += priv->heights[i];
4971 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4973 *offset = child_offset;
4975 *height = priv->heights[item_top_attach];
4981 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4982 GtkWidget *menu_item)
4984 GtkMenu *menu = GTK_MENU (menu_shell);
4985 GtkMenuPrivate *priv = menu->priv;
4986 GtkWidget *widget = GTK_WIDGET (menu_shell);
4987 gint child_offset, child_height;
4991 gboolean last_child = 0;
4993 /* We need to check if the selected item fully visible.
4994 * If not we need to scroll the menu so that it becomes fully
4998 if (compute_child_offset (menu, menu_item,
4999 &child_offset, &child_height, &last_child))
5001 guint vertical_padding;
5002 gboolean double_arrows;
5003 GtkStyleContext *context;
5004 GtkStateFlags state;
5007 y = priv->scroll_offset;
5008 width = gdk_window_get_width (gtk_widget_get_window (widget));
5009 height = gdk_window_get_height (gtk_widget_get_window (widget));
5011 gtk_widget_style_get (widget,
5012 "vertical-padding", &vertical_padding,
5015 double_arrows = get_double_arrows (menu);
5017 context = gtk_widget_get_style_context (widget);
5018 state = gtk_widget_get_state_flags (widget);
5019 gtk_style_context_get_padding (context, state, &padding);
5021 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5022 padding.top + padding.bottom +
5023 2 * vertical_padding;
5024 if (child_offset < y)
5026 /* Ignore the enter event we might get if the pointer
5029 menu_shell->priv->ignore_enter = TRUE;
5030 gtk_menu_scroll_to (menu, child_offset);
5034 GtkBorder arrow_border;
5038 get_arrows_border (menu, &arrow_border);
5039 if (!priv->tearoff_active)
5040 arrow_height = arrow_border.top + arrow_border.bottom;
5042 if (child_offset + child_height > y + height - arrow_height)
5045 if ((!last_child && !priv->tearoff_active) || double_arrows)
5046 arrow_height += arrow_border.bottom;
5048 y = child_offset + child_height - height + arrow_height;
5049 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5051 /* Need upper arrow */
5052 arrow_height += arrow_border.top;
5053 y = child_offset + child_height - height + arrow_height;
5055 /* Ignore the enter event we might get if the pointer
5058 menu_shell->priv->ignore_enter = TRUE;
5059 gtk_menu_scroll_to (menu, y);
5066 gtk_menu_select_item (GtkMenuShell *menu_shell,
5067 GtkWidget *menu_item)
5069 GtkMenu *menu = GTK_MENU (menu_shell);
5071 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5072 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5074 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5078 /* Reparent the menu, taking care of the refcounting
5080 * If unrealize is true we force a unrealize while reparenting the parent.
5081 * This can help eliminate flicker in some cases.
5083 * What happens is that when the menu is unrealized and then re-realized,
5084 * the allocations are as follows:
5086 * parent - 1x1 at (0,0)
5087 * child1 - 100x20 at (0,0)
5088 * child2 - 100x20 at (0,20)
5089 * child3 - 100x20 at (0,40)
5091 * That is, the parent is small but the children are full sized. Then,
5092 * when the queued_resize gets processed, the parent gets resized to
5095 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5096 * contains the following logic:
5098 * - if a move or resize operation on a window would change the clip
5099 * region on the children, then before the window is resized
5100 * the background for children is temporarily set to None, the
5101 * move/resize done, and the background for the children restored.
5103 * So, at the point where the parent is resized to final size, the
5104 * background for the children is temporarily None, and thus they
5105 * are not cleared to the background color and the previous background
5106 * (the image of the menu) is left in place.
5109 gtk_menu_reparent (GtkMenu *menu,
5110 GtkWidget *new_parent,
5113 GObject *object = G_OBJECT (menu);
5114 GtkWidget *widget = GTK_WIDGET (menu);
5115 gboolean was_floating = g_object_is_floating (object);
5117 g_object_ref_sink (object);
5121 g_object_ref (object);
5122 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5123 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5124 g_object_unref (object);
5127 gtk_widget_reparent (widget, new_parent);
5130 g_object_force_floating (object);
5132 g_object_unref (object);
5136 gtk_menu_show_all (GtkWidget *widget)
5138 /* Show children, but not self. */
5139 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5143 * gtk_menu_set_screen:
5145 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5146 * determined by the widget the menu is attached to
5148 * Sets the #GdkScreen on which the menu will be displayed.
5153 gtk_menu_set_screen (GtkMenu *menu,
5156 g_return_if_fail (GTK_IS_MENU (menu));
5157 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5159 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5163 menu_change_screen (menu, screen);
5167 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5169 attach_widget_screen_changed (attach_widget, NULL, menu);
5176 * @child: a #GtkMenuItem
5177 * @left_attach: The column number to attach the left side of the item to
5178 * @right_attach: The column number to attach the right side of the item to
5179 * @top_attach: The row number to attach the top of the item to
5180 * @bottom_attach: The row number to attach the bottom of the item to
5182 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5183 * an item will occupy is specified by @left_attach, @right_attach,
5184 * @top_attach and @bottom_attach. These each represent the leftmost,
5185 * rightmost, uppermost and lower column and row numbers of the table.
5186 * (Columns and rows are indexed from zero).
5188 * Note that this function is not related to gtk_menu_detach().
5193 gtk_menu_attach (GtkMenu *menu,
5198 guint bottom_attach)
5200 GtkMenuShell *menu_shell;
5203 g_return_if_fail (GTK_IS_MENU (menu));
5204 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5205 parent = gtk_widget_get_parent (child);
5206 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5207 g_return_if_fail (left_attach < right_attach);
5208 g_return_if_fail (top_attach < bottom_attach);
5210 menu_shell = GTK_MENU_SHELL (menu);
5214 AttachInfo *ai = get_attach_info (child);
5216 ai->left_attach = left_attach;
5217 ai->right_attach = right_attach;
5218 ai->top_attach = top_attach;
5219 ai->bottom_attach = bottom_attach;
5221 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5223 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5225 menu_queue_resize (menu);
5229 gtk_container_child_set (GTK_CONTAINER (parent), child,
5230 "left-attach", left_attach,
5231 "right-attach", right_attach,
5232 "top-attach", top_attach,
5233 "bottom-attach", bottom_attach,
5239 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5243 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5244 "gtk-menu-popup-delay", &popup_delay,
5251 find_child_containing (GtkMenuShell *menu_shell,
5259 /* find a child which includes the area given by
5260 * left, right, top, bottom.
5262 for (list = menu_shell->priv->children; list; list = list->next)
5266 if (!_gtk_menu_item_is_selectable (list->data))
5269 get_effective_child_attach (list->data, &l, &r, &t, &b);
5271 if (l <= left && right <= r && t <= top && bottom <= b)
5272 return GTK_WIDGET (list->data);
5279 gtk_menu_move_current (GtkMenuShell *menu_shell,
5280 GtkMenuDirectionType direction)
5282 GtkMenu *menu = GTK_MENU (menu_shell);
5285 GtkWidget *match = NULL;
5287 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5291 case GTK_MENU_DIR_CHILD:
5292 direction = GTK_MENU_DIR_PARENT;
5294 case GTK_MENU_DIR_PARENT:
5295 direction = GTK_MENU_DIR_CHILD;
5301 /* use special table menu key bindings */
5302 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5304 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5306 if (direction == GTK_MENU_DIR_NEXT)
5308 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5310 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5318 for (i = 0; i < t; i++)
5320 match = find_child_containing (menu_shell,
5321 l, l + 1, i, i + 1);
5327 else if (direction == GTK_MENU_DIR_PREV)
5329 for (i = t; i > 0; i--)
5331 match = find_child_containing (menu_shell,
5332 l, l + 1, i - 1, i);
5340 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5342 match = find_child_containing (menu_shell,
5343 l, l + 1, i - 1, i);
5349 else if (direction == GTK_MENU_DIR_PARENT)
5351 /* we go one left if possible */
5353 match = find_child_containing (menu_shell,
5354 l - 1, l, t, t + 1);
5358 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5361 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5362 match = menu_shell->priv->active_menu_item;
5365 else if (direction == GTK_MENU_DIR_CHILD)
5367 /* we go one right if possible */
5368 if (r < gtk_menu_get_n_columns (menu))
5369 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5373 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5375 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5377 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5378 match = menu_shell->priv->active_menu_item;
5384 gtk_menu_shell_select_item (menu_shell, match);
5389 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5393 get_visible_size (GtkMenu *menu)
5395 GtkMenuPrivate *priv = menu->priv;
5396 GtkAllocation allocation;
5397 GtkWidget *widget = GTK_WIDGET (menu);
5398 GtkContainer *container = GTK_CONTAINER (menu);
5399 GtkStyleContext *context;
5400 GtkStateFlags state;
5404 gtk_widget_get_allocation (widget, &allocation);
5406 context = gtk_widget_get_style_context (widget);
5407 state = gtk_widget_get_state_flags (widget);
5408 gtk_style_context_get_padding (context, state, &padding);
5410 menu_height = (allocation.height -
5411 (2 * gtk_container_get_border_width (container)) -
5412 padding.top - padding.bottom);
5414 if (!priv->tearoff_active)
5416 GtkBorder arrow_border;
5418 get_arrows_border (menu, &arrow_border);
5419 menu_height -= arrow_border.top;
5420 menu_height -= arrow_border.bottom;
5426 /* Find the sensitive on-screen child containing @y, or if none,
5427 * the nearest selectable onscreen child. (%NULL if none)
5430 child_at (GtkMenu *menu,
5433 GtkMenuPrivate *priv = menu->priv;
5434 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5435 GtkWidget *child = NULL;
5436 gint child_offset = 0;
5439 gint lower, upper; /* Onscreen bounds */
5441 menu_height = get_visible_size (menu);
5442 lower = priv->scroll_offset;
5443 upper = priv->scroll_offset + menu_height;
5445 for (children = menu_shell->priv->children; children; children = children->next)
5447 if (gtk_widget_get_visible (children->data))
5449 GtkRequisition child_requisition;
5451 gtk_widget_get_preferred_size (children->data,
5452 &child_requisition, NULL);
5454 if (_gtk_menu_item_is_selectable (children->data) &&
5455 child_offset >= lower &&
5456 child_offset + child_requisition.height <= upper)
5458 child = children->data;
5460 if (child_offset + child_requisition.height > y &&
5461 !GTK_IS_TEAROFF_MENU_ITEM (child))
5465 child_offset += child_requisition.height;
5473 get_menu_height (GtkMenu *menu)
5475 GtkMenuPrivate *priv = menu->priv;
5476 GtkAllocation allocation;
5477 GtkWidget *widget = GTK_WIDGET (menu);
5478 GtkStyleContext *context;
5479 GtkStateFlags state;
5480 GtkBorder padding, border;
5483 gtk_widget_get_allocation (widget, &allocation);
5485 context = gtk_widget_get_style_context (widget);
5486 state = gtk_widget_get_state_flags (widget);
5487 gtk_style_context_get_padding (context, state, &padding);
5488 gtk_style_context_get_border (context, state, &border);
5490 height = allocation.height;
5491 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5492 padding.top + padding.bottom + border.top + border.bottom;
5494 if (!priv->tearoff_active)
5496 GtkBorder arrow_border;
5498 get_arrows_border (menu, &arrow_border);
5499 height -= arrow_border.top;
5500 height -= arrow_border.bottom;
5507 gtk_menu_real_move_scroll (GtkMenu *menu,
5510 GtkMenuPrivate *priv = menu->priv;
5511 gint page_size = get_visible_size (menu);
5512 gint end_position = get_menu_height (menu);
5513 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5517 case GTK_SCROLL_PAGE_UP:
5518 case GTK_SCROLL_PAGE_DOWN:
5522 gint child_offset = 0;
5523 gboolean old_upper_arrow_visible;
5526 if (type == GTK_SCROLL_PAGE_UP)
5531 if (menu_shell->priv->active_menu_item)
5535 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5536 &child_offset, &child_height, NULL);
5537 child_offset += child_height / 2;
5540 menu_shell->priv->ignore_enter = TRUE;
5541 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5542 old_offset = priv->scroll_offset;
5544 new_offset = priv->scroll_offset + step;
5545 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5547 gtk_menu_scroll_to (menu, new_offset);
5549 if (menu_shell->priv->active_menu_item)
5551 GtkWidget *new_child;
5552 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5553 GtkBorder arrow_border;
5554 get_arrows_border (menu, &arrow_border);
5556 if (priv->scroll_offset != old_offset)
5557 step = priv->scroll_offset - old_offset;
5559 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5561 new_child = child_at (menu, child_offset + step);
5563 gtk_menu_shell_select_item (menu_shell, new_child);
5567 case GTK_SCROLL_START:
5568 /* Ignore the enter event we might get if the pointer is on the menu */
5569 menu_shell->priv->ignore_enter = TRUE;
5570 gtk_menu_scroll_to (menu, 0);
5571 gtk_menu_shell_select_first (menu_shell, TRUE);
5573 case GTK_SCROLL_END:
5574 /* Ignore the enter event we might get if the pointer is on the menu */
5575 menu_shell->priv->ignore_enter = TRUE;
5576 gtk_menu_scroll_to (menu, end_position - page_size);
5577 _gtk_menu_shell_select_last (menu_shell, TRUE);
5586 * gtk_menu_set_monitor:
5588 * @monitor_num: the number of the monitor on which the menu should
5591 * Informs GTK+ on which monitor a menu should be popped up.
5592 * See gdk_screen_get_monitor_geometry().
5594 * This function should be called from a #GtkMenuPositionFunc
5595 * if the menu should not appear on the same monitor as the pointer.
5596 * This information can't be reliably inferred from the coordinates
5597 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5598 * these coordinates may extend beyond the monitor boundaries or even
5599 * the screen boundaries.
5604 gtk_menu_set_monitor (GtkMenu *menu,
5607 GtkMenuPrivate *priv = menu->priv;
5609 g_return_if_fail (GTK_IS_MENU (menu));
5611 priv->monitor_num = monitor_num;
5615 * gtk_menu_get_monitor:
5618 * Retrieves the number of the monitor on which to show the menu.
5620 * Returns: the number of the monitor on which the menu should
5621 * be popped up or -1, if no monitor has been set
5626 gtk_menu_get_monitor (GtkMenu *menu)
5628 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5630 return menu->priv->monitor_num;
5634 * gtk_menu_get_for_attach_widget:
5635 * @widget: a #GtkWidget
5637 * Returns a list of the menus which are attached to this widget.
5638 * This list is owned by GTK+ and must not be modified.
5640 * Return value: (element-type GtkWidget) (transfer none): the list
5641 * of menus attached to his widget.
5646 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5650 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5652 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5658 gtk_menu_grab_notify (GtkWidget *widget,
5659 gboolean was_grabbed)
5661 GtkWidget *toplevel;
5662 GtkWindowGroup *group;
5666 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5669 !gtk_widget_device_is_shadowed (widget, pointer))
5672 toplevel = gtk_widget_get_toplevel (widget);
5674 if (!GTK_IS_WINDOW (toplevel))
5677 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5678 grab = gtk_window_group_get_current_device_grab (group, pointer);
5680 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5681 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5685 * gtk_menu_set_reserve_toggle_size:
5687 * @reserve_toggle_size: whether to reserve size for toggles
5689 * Sets whether the menu should reserve space for drawing toggles
5690 * or icons, regardless of their actual presence.
5695 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5696 gboolean reserve_toggle_size)
5698 GtkMenuPrivate *priv = menu->priv;
5699 gboolean no_toggle_size;
5701 g_return_if_fail (GTK_IS_MENU (menu));
5703 no_toggle_size = !reserve_toggle_size;
5705 if (priv->no_toggle_size != no_toggle_size)
5707 priv->no_toggle_size = no_toggle_size;
5709 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5714 * gtk_menu_get_reserve_toggle_size:
5717 * Returns whether the menu reserves space for toggles and
5718 * icons, regardless of their actual presence.
5720 * Returns: Whether the menu reserves toggle space
5725 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5727 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5729 return !menu->priv->no_toggle_size;