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,
1994 g_return_if_fail (GTK_IS_MENU (menu));
1996 if (adjustment->value != menu->priv->scroll_offset)
1997 gtk_menu_scroll_to (menu, adjustment->value);
2001 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2004 GtkMenuPrivate *priv = menu->priv;
2005 GdkGeometry geometry_hints;
2007 if (!priv->tearoff_window)
2010 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2012 GtkRequisition requisition;
2014 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2015 &requisition, NULL);
2016 width += requisition.width;
2019 geometry_hints.min_width = width;
2020 geometry_hints.max_width = width;
2022 geometry_hints.min_height = 0;
2023 geometry_hints.max_height = priv->requested_height;
2025 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2028 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2032 gtk_menu_update_title (GtkMenu *menu)
2034 GtkMenuPrivate *priv = menu->priv;
2036 if (priv->tearoff_window)
2039 GtkWidget *attach_widget;
2041 title = gtk_menu_get_title (menu);
2044 attach_widget = gtk_menu_get_attach_widget (menu);
2045 if (GTK_IS_MENU_ITEM (attach_widget))
2047 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2048 if (GTK_IS_LABEL (child))
2049 title = gtk_label_get_text (GTK_LABEL (child));
2054 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2059 gtk_menu_get_toplevel (GtkWidget *menu)
2061 GtkWidget *attach, *toplevel;
2063 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2065 if (GTK_IS_MENU_ITEM (attach))
2066 attach = gtk_widget_get_parent (attach);
2068 if (GTK_IS_MENU (attach))
2069 return gtk_menu_get_toplevel (attach);
2070 else if (GTK_IS_WIDGET (attach))
2072 toplevel = gtk_widget_get_toplevel (attach);
2073 if (gtk_widget_is_toplevel (toplevel))
2081 tearoff_window_destroyed (GtkWidget *widget,
2084 gtk_menu_set_tearoff_state (menu, FALSE);
2088 gtk_menu_set_tearoff_state (GtkMenu *menu,
2091 GtkMenuPrivate *priv = menu->priv;
2094 g_return_if_fail (GTK_IS_MENU (menu));
2096 if (priv->torn_off != torn_off)
2098 priv->torn_off = torn_off;
2099 priv->tearoff_active = torn_off;
2103 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2104 gtk_menu_popdown (menu);
2106 if (!priv->tearoff_window)
2108 GtkWidget *toplevel;
2110 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2111 "type", GTK_WINDOW_TOPLEVEL,
2112 "screen", gtk_widget_get_screen (priv->toplevel),
2113 "app-paintable", TRUE,
2116 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2117 GDK_WINDOW_TYPE_HINT_MENU);
2118 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2119 g_signal_connect (priv->tearoff_window, "destroy",
2120 G_CALLBACK (tearoff_window_destroyed), menu);
2121 g_signal_connect (priv->tearoff_window, "event",
2122 G_CALLBACK (gtk_menu_window_event), menu);
2124 gtk_menu_update_title (menu);
2126 gtk_widget_realize (priv->tearoff_window);
2128 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2129 if (toplevel != NULL)
2130 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2131 GTK_WINDOW (toplevel));
2133 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2134 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2135 priv->tearoff_hbox);
2137 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2138 priv->tearoff_adjustment = gtk_adjustment_new (0,
2139 0, priv->requested_height,
2143 g_object_connect (priv->tearoff_adjustment,
2144 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2146 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2148 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2149 priv->tearoff_scrollbar,
2152 if (priv->tearoff_adjustment->upper > height)
2153 gtk_widget_show (priv->tearoff_scrollbar);
2155 gtk_widget_show (priv->tearoff_hbox);
2158 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2160 /* Update menu->requisition */
2161 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2163 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2165 gtk_widget_realize (priv->tearoff_window);
2166 gtk_menu_position (menu, TRUE);
2168 gtk_widget_show (GTK_WIDGET (menu));
2169 gtk_widget_show (priv->tearoff_window);
2171 gtk_menu_scroll_to (menu, 0);
2176 gtk_widget_hide (GTK_WIDGET (menu));
2177 gtk_widget_hide (priv->tearoff_window);
2178 if (GTK_IS_CONTAINER (priv->toplevel))
2179 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2180 gtk_widget_destroy (priv->tearoff_window);
2182 priv->tearoff_window = NULL;
2183 priv->tearoff_hbox = NULL;
2184 priv->tearoff_scrollbar = NULL;
2185 priv->tearoff_adjustment = NULL;
2188 g_object_notify (G_OBJECT (menu), "tearoff-state");
2193 * gtk_menu_get_tearoff_state:
2196 * Returns whether the menu is torn off.
2197 * See gtk_menu_set_tearoff_state().
2199 * Return value: %TRUE if the menu is currently torn off.
2202 gtk_menu_get_tearoff_state (GtkMenu *menu)
2204 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2206 return menu->priv->torn_off;
2210 * gtk_menu_set_title:
2212 * @title: a string containing the title for the menu
2214 * Sets the title string for the menu.
2216 * The title is displayed when the menu is shown as a tearoff
2217 * menu. If @title is %NULL, the menu will see if it is attached
2218 * to a parent menu item, and if so it will try to use the same
2219 * text as that menu item's label.
2222 gtk_menu_set_title (GtkMenu *menu,
2225 GtkMenuPrivate *priv = menu->priv;
2228 g_return_if_fail (GTK_IS_MENU (menu));
2230 old_title = priv->title;
2231 priv->title = g_strdup (title);
2234 gtk_menu_update_title (menu);
2235 g_object_notify (G_OBJECT (menu), "tearoff-title");
2239 * gtk_menu_get_title:
2242 * Returns the title of the menu. See gtk_menu_set_title().
2244 * Return value: the title of the menu, or %NULL if the menu
2245 * has no title set on it. This string is owned by GTK+
2246 * and should not be modified or freed.
2248 G_CONST_RETURN gchar *
2249 gtk_menu_get_title (GtkMenu *menu)
2251 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2253 return menu->priv->title;
2257 gtk_menu_reorder_child (GtkMenu *menu,
2261 GtkMenuShell *menu_shell;
2263 g_return_if_fail (GTK_IS_MENU (menu));
2264 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2266 menu_shell = GTK_MENU_SHELL (menu);
2268 if (g_list_find (menu_shell->priv->children, child))
2270 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2271 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2273 menu_queue_resize (menu);
2278 gtk_menu_style_updated (GtkWidget *widget)
2280 if (gtk_widget_get_realized (widget))
2282 GtkMenu *menu = GTK_MENU (widget);
2283 GtkMenuPrivate *priv = menu->priv;
2284 GtkStyleContext *context;
2286 context = gtk_widget_get_style_context (widget);
2288 gtk_style_context_set_background (context, priv->bin_window);
2289 gtk_style_context_set_background (context, priv->view_window);
2290 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2295 get_arrows_border (GtkMenu *menu,
2298 GtkMenuPrivate *priv = menu->priv;
2299 guint scroll_arrow_height;
2300 GtkArrowPlacement arrow_placement;
2302 gtk_widget_style_get (GTK_WIDGET (menu),
2303 "scroll-arrow-vlength", &scroll_arrow_height,
2304 "arrow_placement", &arrow_placement,
2307 switch (arrow_placement)
2309 case GTK_ARROWS_BOTH:
2310 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2311 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2314 case GTK_ARROWS_START:
2315 border->top = (priv->upper_arrow_visible ||
2316 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2320 case GTK_ARROWS_END:
2322 border->bottom = (priv->upper_arrow_visible ||
2323 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2327 border->left = border->right = 0;
2331 get_menu_border (GtkWidget *widget,
2334 GtkStyleContext *context;
2335 GtkStateFlags state;
2336 GtkBorder *border_width;
2338 context = gtk_widget_get_style_context (widget);
2339 state = gtk_widget_get_state_flags (widget);
2341 gtk_style_context_get (context, state,
2342 "border-width", &border_width,
2345 *border = *border_width;
2346 gtk_border_free (border_width);
2350 gtk_menu_realize (GtkWidget *widget)
2352 GtkMenu *menu = GTK_MENU (widget);
2353 GtkMenuPrivate *priv = menu->priv;
2354 GtkAllocation allocation;
2355 GtkStyleContext *context;
2357 GdkWindowAttr attributes;
2358 gint attributes_mask;
2362 guint vertical_padding;
2363 guint horizontal_padding;
2364 GtkBorder arrow_border, border;
2366 g_return_if_fail (GTK_IS_MENU (widget));
2368 gtk_widget_set_realized (widget, TRUE);
2370 gtk_widget_get_allocation (widget, &allocation);
2372 attributes.window_type = GDK_WINDOW_CHILD;
2373 attributes.x = allocation.x;
2374 attributes.y = allocation.y;
2375 attributes.width = allocation.width;
2376 attributes.height = allocation.height;
2377 attributes.wclass = GDK_INPUT_OUTPUT;
2378 attributes.visual = gtk_widget_get_visual (widget);
2379 attributes.event_mask = gtk_widget_get_events (widget);
2380 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2381 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2383 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2385 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2386 &attributes, attributes_mask);
2387 gtk_widget_set_window (widget, window);
2388 gdk_window_set_user_data (window, widget);
2390 get_menu_border (widget, &border);
2391 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2392 context = gtk_widget_get_style_context (widget);
2394 gtk_widget_style_get (GTK_WIDGET (menu),
2395 "vertical-padding", &vertical_padding,
2396 "horizontal-padding", &horizontal_padding,
2399 gtk_widget_get_allocation (widget, &allocation);
2401 attributes.x = border_width + border.left + horizontal_padding;
2402 attributes.y = border_width + border.top + vertical_padding;
2403 attributes.width = allocation.width -
2404 (2 * (border_width + horizontal_padding)) - border.left - border.right;
2405 attributes.height = allocation.height -
2406 (2 * (border_width + vertical_padding)) - border.top - border.bottom;
2408 get_arrows_border (menu, &arrow_border);
2409 attributes.y += arrow_border.top;
2410 attributes.height -= arrow_border.top;
2411 attributes.height -= arrow_border.bottom;
2413 attributes.width = MAX (1, attributes.width);
2414 attributes.height = MAX (1, attributes.height);
2416 priv->view_window = gdk_window_new (window,
2417 &attributes, attributes_mask);
2418 gdk_window_set_user_data (priv->view_window, menu);
2420 gtk_widget_get_allocation (widget, &allocation);
2424 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2425 border.left + border.right;
2426 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2427 border.top + border.bottom;
2429 attributes.width = MAX (1, attributes.width);
2430 attributes.height = MAX (1, attributes.height);
2432 priv->bin_window = gdk_window_new (priv->view_window,
2433 &attributes, attributes_mask);
2434 gdk_window_set_user_data (priv->bin_window, menu);
2436 children = GTK_MENU_SHELL (menu)->priv->children;
2439 child = children->data;
2440 children = children->next;
2442 gtk_widget_set_parent_window (child, priv->bin_window);
2445 gtk_style_context_set_background (context, priv->bin_window);
2446 gtk_style_context_set_background (context, priv->view_window);
2447 gtk_style_context_set_background (context, window);
2449 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2450 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2451 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2453 gdk_window_show (priv->bin_window);
2454 gdk_window_show (priv->view_window);
2458 gtk_menu_focus (GtkWidget *widget,
2459 GtkDirectionType direction)
2461 /* A menu or its menu items cannot have focus */
2465 /* See notes in gtk_menu_popup() for information
2466 * about the "grab transfer window"
2469 menu_grab_transfer_window_get (GtkMenu *menu)
2471 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2474 GdkWindowAttr attributes;
2475 gint attributes_mask;
2477 attributes.x = -100;
2478 attributes.y = -100;
2479 attributes.width = 10;
2480 attributes.height = 10;
2481 attributes.window_type = GDK_WINDOW_TEMP;
2482 attributes.wclass = GDK_INPUT_ONLY;
2483 attributes.override_redirect = TRUE;
2484 attributes.event_mask = 0;
2486 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2488 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2489 &attributes, attributes_mask);
2490 gdk_window_set_user_data (window, menu);
2492 gdk_window_show (window);
2494 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2501 menu_grab_transfer_window_destroy (GtkMenu *menu)
2503 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2506 gdk_window_set_user_data (window, NULL);
2507 gdk_window_destroy (window);
2508 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2513 gtk_menu_unrealize (GtkWidget *widget)
2515 GtkMenu *menu = GTK_MENU (widget);
2516 GtkMenuPrivate *priv = menu->priv;
2518 menu_grab_transfer_window_destroy (menu);
2520 gdk_window_set_user_data (priv->view_window, NULL);
2521 gdk_window_destroy (priv->view_window);
2522 priv->view_window = NULL;
2524 gdk_window_set_user_data (priv->bin_window, NULL);
2525 gdk_window_destroy (priv->bin_window);
2526 priv->bin_window = NULL;
2528 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2532 calculate_line_heights (GtkMenu *menu,
2534 guint **ret_min_heights,
2535 guint **ret_nat_heights)
2537 GtkMenuPrivate *priv;
2538 GtkMenuShell *menu_shell;
2539 GtkWidget *child, *widget;
2541 guint horizontal_padding;
2550 widget = GTK_WIDGET (menu);
2551 menu_shell = GTK_MENU_SHELL (widget);
2553 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2554 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2555 n_heights = gtk_menu_get_n_rows (menu);
2556 n_columns = gtk_menu_get_n_columns (menu);
2557 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2559 gtk_widget_style_get (GTK_WIDGET (menu),
2560 "horizontal-padding", &horizontal_padding,
2563 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2564 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2566 for (children = menu_shell->priv->children; children; children = children->next)
2571 gint child_min, child_nat;
2573 child = children->data;
2575 if (!gtk_widget_get_visible (child))
2578 get_effective_child_attach (child, &l, &r, &t, &b);
2580 part = avail_width / (r - l);
2582 gtk_widget_get_preferred_height_for_width (child, part,
2583 &child_min, &child_nat);
2585 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2587 part = MAX (child_min, toggle_size) / (b - t);
2588 min_heights[t] = MAX (min_heights[t], part);
2590 part = MAX (child_nat, toggle_size) / (b - t);
2591 nat_heights[t] = MAX (nat_heights[t], part);
2594 if (ret_min_heights)
2595 *ret_min_heights = min_heights;
2597 g_free (min_heights);
2599 if (ret_nat_heights)
2600 *ret_nat_heights = nat_heights;
2602 g_free (nat_heights);
2608 gtk_menu_size_allocate (GtkWidget *widget,
2609 GtkAllocation *allocation)
2612 GtkMenuPrivate *priv;
2613 GtkMenuShell *menu_shell;
2615 GtkAllocation child_allocation;
2620 guint vertical_padding;
2621 guint horizontal_padding;
2624 g_return_if_fail (GTK_IS_MENU (widget));
2625 g_return_if_fail (allocation != NULL);
2627 menu = GTK_MENU (widget);
2628 menu_shell = GTK_MENU_SHELL (widget);
2631 gtk_widget_set_allocation (widget, allocation);
2633 gtk_widget_style_get (GTK_WIDGET (menu),
2634 "vertical-padding", &vertical_padding,
2635 "horizontal-padding", &horizontal_padding,
2638 get_menu_border (widget, &border);
2639 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2641 g_free (priv->heights);
2642 priv->heights_length = calculate_line_heights (menu,
2647 /* refresh our cached height request */
2648 priv->requested_height = (2 * (border_width + vertical_padding)) +
2649 border.top + border.bottom;
2650 for (i = 0; i < priv->heights_length; i++)
2651 priv->requested_height += priv->heights[i];
2653 x = border_width + border.left + horizontal_padding;
2654 y = border_width + border.top + vertical_padding;
2655 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2656 border.left - border.right;
2657 height = allocation->height - (2 * (border_width + vertical_padding)) -
2658 border.top - border.bottom;
2660 if (menu_shell->priv->active)
2661 gtk_menu_scroll_to (menu, priv->scroll_offset);
2663 if (!priv->tearoff_active)
2665 GtkBorder arrow_border;
2667 get_arrows_border (menu, &arrow_border);
2668 y += arrow_border.top;
2669 height -= arrow_border.top;
2670 height -= arrow_border.bottom;
2673 width = MAX (1, width);
2674 height = MAX (1, height);
2676 if (gtk_widget_get_realized (widget))
2678 gdk_window_move_resize (gtk_widget_get_window (widget),
2679 allocation->x, allocation->y,
2680 allocation->width, allocation->height);
2682 gdk_window_move_resize (priv->view_window, x, y, width, height);
2685 if (menu_shell->priv->children)
2687 gint base_width = width / gtk_menu_get_n_columns (menu);
2689 children = menu_shell->priv->children;
2692 child = children->data;
2693 children = children->next;
2695 if (gtk_widget_get_visible (child))
2700 get_effective_child_attach (child, &l, &r, &t, &b);
2702 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2705 tmp = gtk_menu_get_n_columns (menu) - l;
2706 l = gtk_menu_get_n_columns (menu) - r;
2710 child_allocation.width = (r - l) * base_width;
2711 child_allocation.height = 0;
2712 child_allocation.x = l * base_width;
2713 child_allocation.y = 0;
2715 for (i = 0; i < b; i++)
2718 child_allocation.y += priv->heights[i];
2720 child_allocation.height += priv->heights[i];
2723 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2726 gtk_widget_size_allocate (child, &child_allocation);
2727 gtk_widget_queue_draw (child);
2731 /* Resize the item window */
2732 if (gtk_widget_get_realized (widget))
2738 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2739 height += priv->heights[i];
2741 width = gtk_menu_get_n_columns (menu) * base_width;
2742 gdk_window_resize (priv->bin_window, width, height);
2745 if (priv->tearoff_active)
2747 if (height >= priv->requested_height)
2749 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2751 gtk_widget_hide (priv->tearoff_scrollbar);
2752 gtk_menu_set_tearoff_hints (menu, allocation->width);
2754 gtk_menu_scroll_to (menu, 0);
2759 priv->tearoff_adjustment->upper = priv->requested_height;
2760 priv->tearoff_adjustment->page_size = allocation->height;
2762 if (priv->tearoff_adjustment->value + priv->tearoff_adjustment->page_size >
2763 priv->tearoff_adjustment->upper)
2766 value = priv->tearoff_adjustment->upper - priv->tearoff_adjustment->page_size;
2769 gtk_menu_scroll_to (menu, value);
2772 gtk_adjustment_changed (priv->tearoff_adjustment);
2774 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2776 gtk_widget_show (priv->tearoff_scrollbar);
2777 gtk_menu_set_tearoff_hints (menu, allocation->width);
2785 get_arrows_visible_area (GtkMenu *menu,
2786 GdkRectangle *border,
2787 GdkRectangle *upper,
2788 GdkRectangle *lower,
2791 GtkArrowPlacement arrow_placement;
2792 GtkWidget *widget = GTK_WIDGET (menu);
2794 guint vertical_padding;
2795 guint horizontal_padding;
2796 gint scroll_arrow_height;
2797 GtkBorder menu_border;
2799 gtk_widget_style_get (widget,
2800 "vertical-padding", &vertical_padding,
2801 "horizontal-padding", &horizontal_padding,
2802 "scroll-arrow-vlength", &scroll_arrow_height,
2803 "arrow-placement", &arrow_placement,
2806 get_menu_border (widget, &menu_border);
2807 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2808 border->x = border_width + menu_border.left + horizontal_padding;
2809 border->y = border_width + menu_border.top + vertical_padding;
2810 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2811 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2813 switch (arrow_placement)
2815 case GTK_ARROWS_BOTH:
2816 upper->x = border->x;
2817 upper->y = border->y;
2818 upper->width = border->width - 2 * border->x;
2819 upper->height = scroll_arrow_height;
2821 lower->x = border->x;
2822 lower->y = border->height - border->y - scroll_arrow_height;
2823 lower->width = border->width - 2 * border->x;
2824 lower->height = scroll_arrow_height;
2827 case GTK_ARROWS_START:
2828 upper->x = border->x;
2829 upper->y = border->y;
2830 upper->width = (border->width - 2 * border->x) / 2;
2831 upper->height = scroll_arrow_height;
2833 lower->x = border->x + upper->width;
2834 lower->y = border->y;
2835 lower->width = (border->width - 2 * border->x) / 2;
2836 lower->height = scroll_arrow_height;
2839 case GTK_ARROWS_END:
2840 upper->x = border->x;
2841 upper->y = border->height - border->y - scroll_arrow_height;
2842 upper->width = (border->width - 2 * border->x) / 2;
2843 upper->height = scroll_arrow_height;
2845 lower->x = border->x + upper->width;
2846 lower->y = border->height - border->y - scroll_arrow_height;
2847 lower->width = (border->width - 2 * border->x) / 2;
2848 lower->height = scroll_arrow_height;
2852 g_assert_not_reached();
2853 upper->x = upper->y = upper->width = upper->height = 0;
2854 lower->x = lower->y = lower->width = lower->height = 0;
2857 *arrow_space = scroll_arrow_height - menu_border.top - menu_border.bottom;
2861 gtk_menu_draw (GtkWidget *widget,
2865 GtkMenuPrivate *priv;
2866 GtkStyleContext *context;
2867 GdkRectangle border;
2872 GtkStateFlags state;
2873 GtkBorder menu_border;
2875 menu = GTK_MENU (widget);
2877 context = gtk_widget_get_style_context (widget);
2878 window = gtk_widget_get_window (widget);
2879 state = gtk_widget_get_state_flags (widget);
2881 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2882 get_menu_border (widget, &menu_border);
2884 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2886 gfloat arrow_scaling;
2889 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2890 arrow_size = arrow_scaling * arrow_space;
2892 gtk_render_background (context, cr, 0, 0,
2893 gtk_widget_get_allocated_width (widget),
2894 gtk_widget_get_allocated_height (widget));
2895 gtk_render_frame (context, cr, 0, 0,
2896 gtk_widget_get_allocated_width (widget),
2897 gtk_widget_get_allocated_height (widget));
2899 gtk_style_context_save (context);
2900 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
2902 if (priv->upper_arrow_visible && !priv->tearoff_active)
2904 gtk_style_context_save (context);
2905 gtk_style_context_set_state (context, priv->upper_arrow_state);
2907 gtk_render_background (context, cr,
2909 upper.width, upper.height);
2910 gtk_render_frame (context, cr,
2912 upper.width, upper.height);
2914 gtk_render_arrow (context, cr, 0,
2915 upper.x + (upper.width - arrow_size) / 2,
2916 upper.y + menu_border.top + (arrow_space - arrow_size) / 2,
2919 gtk_style_context_restore (context);
2922 if (priv->lower_arrow_visible && !priv->tearoff_active)
2924 gtk_style_context_save (context);
2925 gtk_style_context_set_state (context, priv->lower_arrow_state);
2927 gtk_render_background (context, cr,
2929 lower.width, lower.height);
2930 gtk_render_frame (context, cr,
2932 lower.width, lower.height);
2934 gtk_render_arrow (context, cr, G_PI,
2935 lower.x + (lower.width - arrow_size) / 2,
2936 lower.y + menu_border.top + (arrow_space - arrow_size) / 2,
2939 gtk_style_context_restore (context);
2942 gtk_style_context_restore (context);
2945 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
2947 gint y = -border.y + priv->scroll_offset;
2950 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
2952 if (!priv->tearoff_active)
2954 GtkBorder arrow_border;
2956 get_arrows_border (menu, &arrow_border);
2957 y -= arrow_border.top;
2960 gtk_render_background (context, cr,
2962 border.width, border.height);
2963 gtk_render_frame (context, cr,
2965 border.width, border.height);
2970 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
2976 gtk_menu_show (GtkWidget *widget)
2978 GtkMenu *menu = GTK_MENU (widget);
2980 _gtk_menu_refresh_accel_paths (menu, FALSE);
2982 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2987 gtk_menu_get_preferred_width (GtkWidget *widget,
2992 GtkMenuShell *menu_shell;
2993 GtkMenuPrivate *priv;
2996 guint max_toggle_size;
2997 guint max_accel_width;
2998 guint horizontal_padding;
3000 gint child_min, child_nat;
3001 gint min_width, nat_width;
3004 menu = GTK_MENU (widget);
3005 menu_shell = GTK_MENU_SHELL (widget);
3008 min_width = nat_width = 0;
3010 max_toggle_size = 0;
3011 max_accel_width = 0;
3013 children = menu_shell->priv->children;
3020 child = children->data;
3021 children = children->next;
3023 if (! gtk_widget_get_visible (child))
3026 get_effective_child_attach (child, &l, &r, &t, &b);
3028 /* It's important to size_request the child
3029 * before doing the toggle size request, in
3030 * case the toggle size request depends on the size
3031 * request of a child of the child (e.g. for ImageMenuItem)
3033 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3035 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3036 max_toggle_size = MAX (max_toggle_size, toggle_size);
3037 max_accel_width = MAX (max_accel_width,
3038 GTK_MENU_ITEM (child)->priv->accelerator_width);
3040 part = child_min / (r - l);
3041 min_width = MAX (min_width, part);
3043 part = child_nat / (r - l);
3044 nat_width = MAX (nat_width, part);
3047 /* If the menu doesn't include any images or check items
3048 * reserve the space so that all menus are consistent.
3049 * We only do this for 'ordinary' menus, not for combobox
3050 * menus or multi-column menus
3052 if (max_toggle_size == 0 &&
3053 gtk_menu_get_n_columns (menu) == 1 &&
3054 !priv->no_toggle_size)
3056 GtkStyleContext *context;
3057 GtkWidgetPath *menu_path, *check_path;
3058 guint toggle_spacing;
3059 guint indicator_size;
3061 context = gtk_widget_get_style_context (widget);
3062 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3064 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3065 check_path = gtk_widget_path_copy (menu_path);
3066 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3068 gtk_style_context_set_path (context, check_path);
3069 gtk_widget_path_free (check_path);
3071 gtk_style_context_get_style (context,
3072 "toggle-spacing", &toggle_spacing,
3073 "indicator-size", &indicator_size,
3076 max_toggle_size = indicator_size + toggle_spacing;
3078 /* Restore real widget path */
3079 gtk_style_context_set_path (context, menu_path);
3080 gtk_widget_path_free (menu_path);
3083 min_width += 2 * max_toggle_size + max_accel_width;
3084 min_width *= gtk_menu_get_n_columns (menu);
3086 nat_width += 2 * max_toggle_size + max_accel_width;
3087 nat_width *= gtk_menu_get_n_columns (menu);
3089 gtk_widget_style_get (GTK_WIDGET (menu),
3090 "horizontal-padding", &horizontal_padding,
3093 get_menu_border (widget, &border);
3094 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3095 min_width += (2 * (border_width + horizontal_padding)) +
3096 border.left + border.right;
3097 nat_width += (2 * (border_width + horizontal_padding)) +
3098 border.top + border.bottom;
3100 priv->toggle_size = max_toggle_size;
3101 priv->accel_size = max_accel_width;
3104 *minimum_size = min_width;
3107 *natural_size = nat_width;
3109 /* Don't resize the tearoff if it is not active,
3110 * because it won't redraw (it is only a background pixmap).
3112 if (priv->tearoff_active)
3113 gtk_menu_set_tearoff_hints (menu, min_width);
3117 gtk_menu_get_preferred_height (GtkWidget *widget,
3123 /* Menus are height-for-width only, just return the height
3124 * for the minimum width
3126 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3127 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3131 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3136 GtkMenu *menu = GTK_MENU (widget);
3137 GtkMenuPrivate *priv = menu->priv;
3138 guint *min_heights, *nat_heights;
3139 guint vertical_padding, border_width;
3141 gint min_height, nat_height;
3143 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3144 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3146 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (widget)->ythickness) * 2;
3149 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3151 for (i = 0; i < n_heights; i++)
3153 min_height += min_heights[i];
3154 nat_height += nat_heights[i];
3157 if (priv->have_position)
3159 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3160 GdkRectangle monitor;
3162 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
3164 if (priv->position_y + min_height > monitor.y + monitor.height)
3165 min_height = monitor.y + monitor.height - priv->position_y;
3167 if (priv->position_y + nat_height > monitor.y + monitor.height)
3168 nat_height = monitor.y + monitor.height - priv->position_y;
3170 if (priv->position_y < monitor.y)
3172 min_height -= monitor.y - priv->position_y;
3173 nat_height -= monitor.y - priv->position_y;
3178 *minimum_size = min_height;
3181 *natural_size = nat_height;
3183 g_free (min_heights);
3184 g_free (nat_heights);
3190 gtk_menu_button_scroll (GtkMenu *menu,
3191 GdkEventButton *event)
3193 GtkMenuPrivate *priv = menu->priv;
3195 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3197 gboolean touchscreen_mode;
3199 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3200 "gtk-touchscreen-mode", &touchscreen_mode,
3203 if (touchscreen_mode)
3204 gtk_menu_handle_scrolling (menu,
3205 event->x_root, event->y_root,
3206 event->type == GDK_BUTTON_PRESS,
3216 pointer_in_menu_window (GtkWidget *widget,
3220 GtkMenu *menu = GTK_MENU (widget);
3221 GtkMenuPrivate *priv = menu->priv;
3222 GtkAllocation allocation;
3224 if (gtk_widget_get_mapped (priv->toplevel))
3226 GtkMenuShell *menu_shell;
3227 gint window_x, window_y;
3229 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3230 &window_x, &window_y);
3232 gtk_widget_get_allocation (widget, &allocation);
3233 if (x_root >= window_x && x_root < window_x + allocation.width &&
3234 y_root >= window_y && y_root < window_y + allocation.height)
3237 menu_shell = GTK_MENU_SHELL (widget);
3239 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3240 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3248 gtk_menu_button_press (GtkWidget *widget,
3249 GdkEventButton *event)
3251 if (event->type != GDK_BUTTON_PRESS)
3254 /* Don't pass down to menu shell for presses over scroll arrows
3256 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3259 /* Don't pass down to menu shell if a non-menuitem part of the menu
3260 * was clicked. The check for the event_widget being a GtkMenuShell
3261 * works because we have the pointer grabbed on menu_shell->window
3262 * with owner_events=TRUE, so all events that are either outside
3263 * the menu or on its border are delivered relative to
3264 * menu_shell->window.
3266 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3267 pointer_in_menu_window (widget, event->x_root, event->y_root))
3270 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3274 gtk_menu_button_release (GtkWidget *widget,
3275 GdkEventButton *event)
3277 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3279 if (priv->ignore_button_release)
3281 priv->ignore_button_release = FALSE;
3285 if (event->type != GDK_BUTTON_RELEASE)
3288 /* Don't pass down to menu shell for releases over scroll arrows
3290 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3293 /* Don't pass down to menu shell if a non-menuitem part of the menu
3294 * was clicked (see comment in button_press()).
3296 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3297 pointer_in_menu_window (widget, event->x_root, event->y_root))
3299 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3300 * bail out early here so it is in a consistent state for the
3301 * next button_press/button_release in GtkMenuShell.
3304 if (GTK_MENU_SHELL (widget)->priv->active)
3305 GTK_MENU_SHELL (widget)->priv->button = 0;
3310 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3313 static const gchar *
3314 get_accel_path (GtkWidget *menu_item,
3319 GClosure *accel_closure;
3320 GtkAccelGroup *accel_group;
3322 path = _gtk_widget_get_accel_path (menu_item, locked);
3325 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3331 label = gtk_bin_get_child (GTK_BIN (menu_item));
3333 if (GTK_IS_ACCEL_LABEL (label))
3335 g_object_get (label,
3336 "accel-closure", &accel_closure,
3340 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3342 *locked = gtk_accel_group_get_is_locked (accel_group);
3352 gtk_menu_key_press (GtkWidget *widget,
3355 GtkMenuShell *menu_shell;
3357 gboolean delete = FALSE;
3358 gboolean can_change_accels;
3359 gchar *accel = NULL;
3360 guint accel_key, accel_mods;
3361 GdkModifierType consumed_modifiers;
3362 GdkDisplay *display;
3364 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3365 g_return_val_if_fail (event != NULL, FALSE);
3367 menu_shell = GTK_MENU_SHELL (widget);
3368 menu = GTK_MENU (widget);
3370 gtk_menu_stop_navigating_submenu (menu);
3372 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3375 display = gtk_widget_get_display (widget);
3377 g_object_get (gtk_widget_get_settings (widget),
3378 "gtk-menu-bar-accel", &accel,
3379 "gtk-can-change-accels", &can_change_accels,
3382 if (accel && *accel)
3385 GdkModifierType mods = 0;
3387 gtk_accelerator_parse (accel, &keyval, &mods);
3390 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3392 /* FIXME this is wrong, needs to be in the global accel resolution
3393 * thing, to properly consider i18n etc., but that probably requires
3394 * AccelGroup changes etc.
3396 if (event->keyval == keyval && (mods & event->state) == mods)
3398 gtk_menu_shell_cancel (menu_shell);
3406 switch (event->keyval)
3408 case GDK_KEY_Delete:
3409 case GDK_KEY_KP_Delete:
3410 case GDK_KEY_BackSpace:
3417 /* Figure out what modifiers went into determining the key symbol */
3418 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3419 event->hardware_keycode,
3420 event->state, event->group,
3421 NULL, NULL, NULL, &consumed_modifiers);
3423 accel_key = gdk_keyval_to_lower (event->keyval);
3424 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3426 /* If lowercasing affects the keysym, then we need to include SHIFT
3427 * in the modifiers, we re-uppercase when we match against the keyval,
3428 * but display and save in caseless form.
3430 if (accel_key != event->keyval)
3431 accel_mods |= GDK_SHIFT_MASK;
3433 /* Modify the accelerators */
3434 if (can_change_accels &&
3435 menu_shell->priv->active_menu_item &&
3436 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3437 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3438 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3440 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3441 gboolean locked, replace_accels = TRUE;
3444 path = get_accel_path (menu_item, &locked);
3445 if (!path || locked)
3447 /* Can't change accelerators on menu_items without paths
3448 * (basically, those items are accelerator-locked).
3450 gtk_widget_error_bell (widget);
3456 /* For the keys that act to delete the current setting,
3457 * we delete the current setting if there is one, otherwise,
3458 * we set the key as the accelerator.
3464 if (gtk_accel_map_lookup_entry (path, &key) &&
3465 (key.accel_key || key.accel_mods))
3471 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3475 /* We failed, probably because this key is in use
3476 * and locked already.
3478 gtk_widget_error_bell (widget);
3487 check_threshold (GtkWidget *widget,
3496 ABS (start_x - x) > THRESHOLD ||
3497 ABS (start_y - y) > THRESHOLD;
3501 definitely_within_item (GtkWidget *widget,
3505 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3508 w = gdk_window_get_width (window);
3509 h = gdk_window_get_height (window);
3512 check_threshold (widget, 0, 0, x, y) &&
3513 check_threshold (widget, w - 1, 0, x, y) &&
3514 check_threshold (widget, w - 1, h - 1, x, y) &&
3515 check_threshold (widget, 0, h - 1, x, y);
3519 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3521 GtkMenuPrivate *priv = menu->priv;
3523 return priv->navigation_height && priv->navigation_width;
3527 gtk_menu_motion_notify (GtkWidget *widget,
3528 GdkEventMotion *event)
3530 GtkWidget *menu_item;
3532 GtkMenuShell *menu_shell;
3535 gboolean need_enter;
3537 if (GTK_IS_MENU (widget))
3539 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3541 if (priv->ignore_button_release)
3542 priv->ignore_button_release = FALSE;
3544 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3548 /* We received the event for one of two reasons:
3550 * a) We are the active menu, and did gtk_grab_add()
3551 * b) The widget is a child of ours, and the event was propagated
3553 * Since for computation of navigation regions, we want the menu which
3554 * is the parent of the menu item, for a), we need to find that menu,
3555 * which may be different from 'widget'.
3557 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3558 parent = gtk_widget_get_parent (menu_item);
3559 if (!GTK_IS_MENU_ITEM (menu_item) ||
3560 !GTK_IS_MENU (parent))
3563 menu_shell = GTK_MENU_SHELL (parent);
3564 menu = GTK_MENU (menu_shell);
3566 if (definitely_within_item (menu_item, event->x, event->y))
3567 menu_shell->priv->activate_time = 0;
3569 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3571 /* Check to see if we are within an active submenu's navigation region
3573 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3576 /* Make sure we pop down if we enter a non-selectable menu item, so we
3577 * don't show a submenu when the cursor is outside the stay-up triangle.
3579 if (!_gtk_menu_item_is_selectable (menu_item))
3581 /* We really want to deselect, but this gives the menushell code
3582 * a chance to do some bookkeeping about the menuitem.
3584 gtk_menu_shell_select_item (menu_shell, menu_item);
3590 /* The menu is now sensitive to enter events on its items, but
3591 * was previously sensitive. So we fake an enter event.
3593 menu_shell->priv->ignore_enter = FALSE;
3595 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3596 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3598 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3601 send_event->crossing.window = g_object_ref (event->window);
3602 send_event->crossing.time = event->time;
3603 send_event->crossing.send_event = TRUE;
3604 send_event->crossing.x_root = event->x_root;
3605 send_event->crossing.y_root = event->y_root;
3606 send_event->crossing.x = event->x;
3607 send_event->crossing.y = event->y;
3608 send_event->crossing.state = event->state;
3609 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3611 /* We send the event to 'widget', the currently active menu,
3612 * instead of 'menu', the menu that the pointer is in. This
3613 * will ensure that the event will be ignored unless the
3614 * menuitem is a child of the active menu or some parent
3615 * menu of the active menu.
3617 result = gtk_widget_event (widget, send_event);
3618 gdk_event_free (send_event);
3628 get_double_arrows (GtkMenu *menu)
3630 GtkMenuPrivate *priv = menu->priv;
3631 gboolean double_arrows;
3632 GtkArrowPlacement arrow_placement;
3634 gtk_widget_style_get (GTK_WIDGET (menu),
3635 "double-arrows", &double_arrows,
3636 "arrow-placement", &arrow_placement,
3639 if (arrow_placement != GTK_ARROWS_BOTH)
3642 return double_arrows || (priv->initially_pushed_in &&
3643 priv->scroll_offset != 0);
3647 gtk_menu_scroll_by (GtkMenu *menu,
3650 GtkMenuPrivate *priv = menu->priv;
3651 GtkBorder arrow_border;
3655 gboolean double_arrows;
3657 widget = GTK_WIDGET (menu);
3658 offset = priv->scroll_offset + step;
3660 get_arrows_border (menu, &arrow_border);
3662 double_arrows = get_double_arrows (menu);
3664 /* If we scroll upward and the non-visible top part
3665 * is smaller than the scroll arrow it would be
3666 * pretty stupid to show the arrow and taking more
3667 * screen space than just scrolling to the top.
3670 if ((step < 0) && (offset < arrow_border.top))
3673 /* Don't scroll over the top if we weren't before: */
3674 if ((priv->scroll_offset >= 0) && (offset < 0))
3677 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3679 if (priv->scroll_offset == 0 &&
3680 view_height >= priv->requested_height)
3683 /* Don't scroll past the bottom if we weren't before: */
3684 if (priv->scroll_offset > 0)
3685 view_height -= arrow_border.top;
3687 /* When both arrows are always shown,
3688 * reduce view height even more.
3691 view_height -= arrow_border.bottom;
3693 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3694 (offset + view_height > priv->requested_height))
3695 offset = priv->requested_height - view_height;
3697 if (offset != priv->scroll_offset)
3698 gtk_menu_scroll_to (menu, offset);
3702 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3703 gboolean touchscreen_mode)
3705 GtkMenuPrivate *priv = menu->priv;
3706 gboolean upper_visible;
3707 gboolean lower_visible;
3709 upper_visible = priv->upper_arrow_visible;
3710 lower_visible = priv->lower_arrow_visible;
3712 gtk_menu_scroll_by (menu, priv->scroll_step);
3714 if (touchscreen_mode &&
3715 (upper_visible != priv->upper_arrow_visible ||
3716 lower_visible != priv->lower_arrow_visible))
3718 /* We are about to hide a scroll arrow while the mouse is pressed,
3719 * this would cause the uncovered menu item to be activated on button
3720 * release. Therefore we need to ignore button release here
3722 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3723 priv->ignore_button_release = TRUE;
3728 gtk_menu_scroll_timeout (gpointer data)
3731 gboolean touchscreen_mode;
3733 menu = GTK_MENU (data);
3735 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3736 "gtk-touchscreen-mode", &touchscreen_mode,
3739 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3745 gtk_menu_scroll_timeout_initial (gpointer data)
3749 gboolean touchscreen_mode;
3751 menu = GTK_MENU (data);
3753 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3754 "gtk-timeout-repeat", &timeout,
3755 "gtk-touchscreen-mode", &touchscreen_mode,
3758 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3760 gtk_menu_remove_scroll_timeout (menu);
3762 menu->priv->scroll_timeout =
3763 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3769 gtk_menu_start_scrolling (GtkMenu *menu)
3772 gboolean touchscreen_mode;
3774 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3775 "gtk-timeout-repeat", &timeout,
3776 "gtk-touchscreen-mode", &touchscreen_mode,
3779 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3781 menu->priv->scroll_timeout =
3782 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3786 gtk_menu_scroll (GtkWidget *widget,
3787 GdkEventScroll *event)
3789 GtkMenu *menu = GTK_MENU (widget);
3791 switch (event->direction)
3793 case GDK_SCROLL_RIGHT:
3794 case GDK_SCROLL_DOWN:
3795 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3797 case GDK_SCROLL_LEFT:
3799 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3807 get_arrows_sensitive_area (GtkMenu *menu,
3808 GdkRectangle *upper,
3809 GdkRectangle *lower)
3811 GtkArrowPlacement arrow_placement;
3812 GtkWidget *widget = GTK_WIDGET (menu);
3816 guint vertical_padding;
3818 gint scroll_arrow_height;
3820 window = gtk_widget_get_window (widget);
3821 width = gdk_window_get_width (window);
3822 height = gdk_window_get_height (window);
3824 gtk_widget_style_get (widget,
3825 "vertical-padding", &vertical_padding,
3826 "scroll-arrow-vlength", &scroll_arrow_height,
3827 "arrow-placement", &arrow_placement,
3830 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3831 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3833 gdk_window_get_position (window, &win_x, &win_y);
3835 switch (arrow_placement)
3837 case GTK_ARROWS_BOTH:
3842 upper->width = width;
3843 upper->height = scroll_arrow_height + border;
3849 lower->y = win_y + height - border - scroll_arrow_height;
3850 lower->width = width;
3851 lower->height = scroll_arrow_height + border;
3855 case GTK_ARROWS_START:
3860 upper->width = width / 2;
3861 upper->height = scroll_arrow_height + border;
3866 lower->x = win_x + width / 2;
3868 lower->width = width / 2;
3869 lower->height = scroll_arrow_height + border;
3873 case GTK_ARROWS_END:
3877 upper->y = win_y + height - border - scroll_arrow_height;
3878 upper->width = width / 2;
3879 upper->height = scroll_arrow_height + border;
3884 lower->x = win_x + width / 2;
3885 lower->y = win_y + height - border - scroll_arrow_height;
3886 lower->width = width / 2;
3887 lower->height = scroll_arrow_height + border;
3895 gtk_menu_handle_scrolling (GtkMenu *menu,
3901 GtkMenuPrivate *priv = menu->priv;
3902 GtkMenuShell *menu_shell;
3905 gboolean scroll_fast = FALSE;
3907 gboolean touchscreen_mode;
3909 menu_shell = GTK_MENU_SHELL (menu);
3911 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3912 "gtk-touchscreen-mode", &touchscreen_mode,
3915 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3920 /* upper arrow handling */
3922 get_arrows_sensitive_area (menu, &rect, NULL);
3925 if (priv->upper_arrow_visible && !priv->tearoff_active &&
3926 (x >= rect.x) && (x < rect.x + rect.width) &&
3927 (y >= rect.y) && (y < rect.y + rect.height))
3932 if (touchscreen_mode)
3933 priv->upper_arrow_prelight = in_arrow;
3935 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
3937 gboolean arrow_pressed = FALSE;
3939 if (priv->upper_arrow_visible && !priv->tearoff_active)
3941 if (touchscreen_mode)
3943 if (enter && priv->upper_arrow_prelight)
3945 if (priv->scroll_timeout == 0)
3947 /* Deselect the active item so that
3948 * any submenus are popped down
3950 gtk_menu_shell_deselect (menu_shell);
3952 gtk_menu_remove_scroll_timeout (menu);
3953 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3957 /* Only do stuff on click. */
3958 gtk_menu_start_scrolling (menu);
3959 arrow_pressed = TRUE;
3964 arrow_pressed = TRUE;
3969 gtk_menu_stop_scrolling (menu);
3972 else /* !touchscreen_mode */
3974 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3976 if (enter && in_arrow &&
3977 (!priv->upper_arrow_prelight ||
3978 priv->scroll_fast != scroll_fast))
3980 priv->upper_arrow_prelight = TRUE;
3981 priv->scroll_fast = scroll_fast;
3983 /* Deselect the active item so that
3984 * any submenus are popped down
3986 gtk_menu_shell_deselect (menu_shell);
3988 gtk_menu_remove_scroll_timeout (menu);
3989 priv->scroll_step = scroll_fast
3990 ? -MENU_SCROLL_STEP2
3991 : -MENU_SCROLL_STEP1;
3993 priv->scroll_timeout =
3994 gdk_threads_add_timeout (scroll_fast
3995 ? MENU_SCROLL_TIMEOUT2
3996 : MENU_SCROLL_TIMEOUT1,
3997 gtk_menu_scroll_timeout, menu);
3999 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4001 gtk_menu_stop_scrolling (menu);
4006 /* gtk_menu_start_scrolling() might have hit the top of the
4007 * menu, so check if the button isn't insensitive before
4008 * changing it to something else.
4010 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4012 GtkStateFlags arrow_state = 0;
4015 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4017 if (priv->upper_arrow_prelight)
4018 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4020 if (arrow_state != priv->upper_arrow_state)
4022 priv->upper_arrow_state = arrow_state;
4024 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4030 /* lower arrow handling */
4032 get_arrows_sensitive_area (menu, NULL, &rect);
4035 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4036 (x >= rect.x) && (x < rect.x + rect.width) &&
4037 (y >= rect.y) && (y < rect.y + rect.height))
4042 if (touchscreen_mode)
4043 priv->lower_arrow_prelight = in_arrow;
4045 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4047 gboolean arrow_pressed = FALSE;
4049 if (priv->lower_arrow_visible && !priv->tearoff_active)
4051 if (touchscreen_mode)
4053 if (enter && priv->lower_arrow_prelight)
4055 if (priv->scroll_timeout == 0)
4057 /* Deselect the active item so that
4058 * any submenus are popped down
4060 gtk_menu_shell_deselect (menu_shell);
4062 gtk_menu_remove_scroll_timeout (menu);
4063 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4067 /* Only do stuff on click. */
4068 gtk_menu_start_scrolling (menu);
4069 arrow_pressed = TRUE;
4074 arrow_pressed = TRUE;
4079 gtk_menu_stop_scrolling (menu);
4082 else /* !touchscreen_mode */
4084 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4086 if (enter && in_arrow &&
4087 (!priv->lower_arrow_prelight ||
4088 priv->scroll_fast != scroll_fast))
4090 priv->lower_arrow_prelight = TRUE;
4091 priv->scroll_fast = scroll_fast;
4093 /* Deselect the active item so that
4094 * any submenus are popped down
4096 gtk_menu_shell_deselect (menu_shell);
4098 gtk_menu_remove_scroll_timeout (menu);
4099 priv->scroll_step = scroll_fast
4101 : MENU_SCROLL_STEP1;
4103 priv->scroll_timeout =
4104 gdk_threads_add_timeout (scroll_fast
4105 ? MENU_SCROLL_TIMEOUT2
4106 : MENU_SCROLL_TIMEOUT1,
4107 gtk_menu_scroll_timeout, menu);
4109 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4111 gtk_menu_stop_scrolling (menu);
4116 /* gtk_menu_start_scrolling() might have hit the bottom of the
4117 * menu, so check if the button isn't insensitive before
4118 * changing it to something else.
4120 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4122 GtkStateFlags arrow_state = 0;
4125 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4127 if (priv->lower_arrow_prelight)
4128 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4130 if (arrow_state != priv->lower_arrow_state)
4132 priv->lower_arrow_state = arrow_state;
4134 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4142 gtk_menu_enter_notify (GtkWidget *widget,
4143 GdkEventCrossing *event)
4145 GtkWidget *menu_item;
4147 gboolean touchscreen_mode;
4149 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4150 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4151 event->mode == GDK_CROSSING_STATE_CHANGED)
4154 g_object_get (gtk_widget_get_settings (widget),
4155 "gtk-touchscreen-mode", &touchscreen_mode,
4158 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4159 if (GTK_IS_MENU (widget))
4161 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4163 if (!menu_shell->priv->ignore_enter)
4164 gtk_menu_handle_scrolling (GTK_MENU (widget),
4165 event->x_root, event->y_root, TRUE, TRUE);
4168 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4170 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4172 if (GTK_IS_MENU (menu))
4174 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4175 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4177 if (priv->seen_item_enter)
4179 /* This is the second enter we see for an item
4180 * on this menu. This means a release should always
4183 menu_shell->priv->activate_time = 0;
4185 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4186 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4188 if (definitely_within_item (menu_item, event->x, event->y))
4190 /* This is an actual user-enter (ie. not a pop-under)
4191 * In this case, the user must either have entered
4192 * sufficiently far enough into the item, or he must move
4193 * far enough away from the enter point. (see
4194 * gtk_menu_motion_notify())
4196 menu_shell->priv->activate_time = 0;
4200 priv->seen_item_enter = TRUE;
4204 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4205 * will not correspond to the event widget's parent. Check to see
4206 * if we are in the parent's navigation region.
4208 parent = gtk_widget_get_parent (menu_item);
4209 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4210 gtk_menu_navigating_submenu (GTK_MENU (parent),
4211 event->x_root, event->y_root))
4214 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4218 gtk_menu_leave_notify (GtkWidget *widget,
4219 GdkEventCrossing *event)
4221 GtkMenuShell *menu_shell;
4223 GtkMenuItem *menu_item;
4224 GtkWidget *event_widget;
4226 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4227 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4228 event->mode == GDK_CROSSING_STATE_CHANGED)
4231 menu = GTK_MENU (widget);
4232 menu_shell = GTK_MENU_SHELL (widget);
4234 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4237 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4239 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4241 if (!GTK_IS_MENU_ITEM (event_widget))
4244 menu_item = GTK_MENU_ITEM (event_widget);
4246 /* Here we check to see if we're leaving an active menu item
4247 * with a submenu, in which case we enter submenu navigation mode.
4249 if (menu_shell->priv->active_menu_item != NULL
4250 && menu_item->priv->submenu != NULL
4251 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4253 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4255 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4258 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4260 /* We are leaving an active menu item with nonactive submenu.
4261 * Deselect it so we don't surprise the user with by popping
4262 * up a submenu _after_ he left the item.
4264 gtk_menu_shell_deselect (menu_shell);
4269 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4273 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4275 GtkMenuPrivate *priv = menu->priv;
4277 priv->navigation_x = 0;
4278 priv->navigation_y = 0;
4279 priv->navigation_width = 0;
4280 priv->navigation_height = 0;
4282 if (priv->navigation_timeout)
4284 g_source_remove (priv->navigation_timeout);
4285 priv->navigation_timeout = 0;
4289 /* When the timeout is elapsed, the navigation region is destroyed
4290 * and the menuitem under the pointer (if any) is selected.
4293 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4295 GtkMenuPopdownData *popdown_data = user_data;
4296 GtkMenu *menu = popdown_data->menu;
4297 GtkMenuPrivate *priv = menu->priv;
4298 GdkWindow *child_window;
4300 gtk_menu_stop_navigating_submenu (menu);
4302 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4304 child_window = gdk_window_get_device_position (priv->bin_window,
4305 popdown_data->device,
4310 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4312 send_event->crossing.window = g_object_ref (child_window);
4313 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4314 send_event->crossing.send_event = TRUE;
4315 gdk_event_set_device (send_event, popdown_data->device);
4317 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4319 gdk_event_free (send_event);
4327 gtk_menu_navigating_submenu (GtkMenu *menu,
4331 GtkMenuPrivate *priv = menu->priv;
4334 if (!gtk_menu_has_navigation_triangle (menu))
4337 width = priv->navigation_width;
4338 height = priv->navigation_height;
4340 /* Check if x/y are in the triangle spanned by the navigation parameters */
4342 /* 1) Move the coordinates so the triangle starts at 0,0 */
4343 event_x -= priv->navigation_x;
4344 event_y -= priv->navigation_y;
4346 /* 2) Ensure both legs move along the positive axis */
4358 /* 3) Check that the given coordinate is inside the triangle. The formula
4359 * is a transformed form of this formula: x/w + y/h <= 1
4361 if (event_x >= 0 && event_y >= 0 &&
4362 event_x * height + event_y * width <= width * height)
4368 gtk_menu_stop_navigating_submenu (menu);
4374 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4375 GtkMenuItem *menu_item,
4376 GdkEventCrossing *event)
4378 GtkMenuPrivate *priv = menu->priv;
4379 gint submenu_left = 0;
4380 gint submenu_right = 0;
4381 gint submenu_top = 0;
4382 gint submenu_bottom = 0;
4385 GtkWidget *event_widget;
4386 GtkMenuPopdownData *popdown_data;
4389 g_return_if_fail (menu_item->priv->submenu != NULL);
4390 g_return_if_fail (event != NULL);
4392 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4394 window = gtk_widget_get_window (menu_item->priv->submenu);
4395 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4397 submenu_right = submenu_left + gdk_window_get_width (window);
4398 submenu_bottom = submenu_top + gdk_window_get_height (window);
4400 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4401 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4403 if (event->x >= 0 && event->x < width)
4407 gtk_menu_stop_navigating_submenu (menu);
4409 /* The navigation region is the triangle closest to the x/y
4410 * location of the rectangle. This is why the width or height
4413 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4416 priv->navigation_x = submenu_left;
4417 priv->navigation_width = event->x_root - submenu_left;
4422 priv->navigation_x = submenu_right;
4423 priv->navigation_width = event->x_root - submenu_right;
4429 priv->navigation_y = event->y_root;
4430 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4432 if (priv->navigation_height >= 0)
4438 priv->navigation_y = event->y_root;
4439 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4441 if (priv->navigation_height <= 0)
4445 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4446 "gtk-menu-popdown-delay", &popdown_delay,
4449 popdown_data = g_new (GtkMenuPopdownData, 1);
4450 popdown_data->menu = menu;
4451 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4453 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4455 gtk_menu_stop_navigating_submenu_cb,
4457 (GDestroyNotify) g_free);
4462 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4466 g_return_if_fail (GTK_IS_MENU (menu_shell));
4468 parent = menu_shell->priv->parent_menu_shell;
4470 menu_shell->priv->activate_time = 0;
4471 gtk_menu_popdown (GTK_MENU (menu_shell));
4474 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4478 gtk_menu_position (GtkMenu *menu,
4479 gboolean set_scroll_offset)
4481 GtkMenuPrivate *priv = menu->priv;
4483 GtkRequisition requisition;
4488 GdkScreen *pointer_screen;
4489 GdkRectangle monitor;
4492 widget = GTK_WIDGET (menu);
4494 screen = gtk_widget_get_screen (widget);
4495 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4496 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4498 /* Get the minimum height for minimum width to figure out
4499 * the right place to popup the menu.
4501 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4503 if (pointer_screen != screen)
4505 /* Pointer is on a different screen; roughly center the
4506 * menu on the screen. If someone was using multiscreen
4507 * + Xinerama together they'd probably want something
4508 * fancier; but that is likely to be vanishingly rare.
4510 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4511 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4514 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4515 priv->initially_pushed_in = FALSE;
4517 /* Set the type hint here to allow custom position functions
4518 * to set a different hint
4520 if (!gtk_widget_get_visible (priv->toplevel))
4521 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4523 if (priv->position_func)
4525 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4526 priv->position_func_data);
4528 if (priv->monitor_num < 0)
4529 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4531 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4535 gint space_left, space_right, space_above, space_below;
4539 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4541 get_menu_border (widget, &border);
4543 /* The placement of popup menus horizontally works like this (with
4544 * RTL in parentheses)
4546 * - If there is enough room to the right (left) of the mouse cursor,
4547 * position the menu there.
4549 * - Otherwise, if if there is enough room to the left (right) of the
4550 * mouse cursor, position the menu there.
4552 * - Otherwise if the menu is smaller than the monitor, position it
4553 * on the side of the mouse cursor that has the most space available
4555 * - Otherwise (if there is simply not enough room for the menu on the
4556 * monitor), position it as far left (right) as possible.
4558 * Positioning in the vertical direction is similar: first try below
4559 * mouse cursor, then above.
4561 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4563 space_left = x - monitor.x;
4564 space_right = monitor.x + monitor.width - x - 1;
4565 space_above = y - monitor.y;
4566 space_below = monitor.y + monitor.height - y - 1;
4568 /* Position horizontally. */
4570 /* the amount of space we need to position the menu.
4571 * Note the menu is offset "thickness" pixels
4573 needed_width = requisition.width - border.left;
4575 if (needed_width <= space_left ||
4576 needed_width <= space_right)
4578 if ((rtl && needed_width <= space_left) ||
4579 (!rtl && needed_width > space_right))
4582 x = x + border.left - requisition.width + 1;
4586 /* position right */
4587 x = x - border.right;
4590 /* x is clamped on-screen further down */
4592 else if (requisition.width <= monitor.width)
4594 /* the menu is too big to fit on either side of the mouse
4595 * cursor, but smaller than the monitor. Position it on
4596 * the side that has the most space
4598 if (space_left > space_right)
4606 x = monitor.x + monitor.width - requisition.width;
4609 else /* menu is simply too big for the monitor */
4614 x = monitor.x + monitor.width - requisition.width;
4623 /* Position vertically.
4624 * The algorithm is the same as above, but simpler
4625 * because we don't have to take RTL into account.
4627 needed_height = requisition.height - border.top;
4629 if (needed_height <= space_above ||
4630 needed_height <= space_below)
4632 if (needed_height <= space_below)
4635 y = y + border.bottom - requisition.height + 1;
4637 y = CLAMP (y, monitor.y,
4638 monitor.y + monitor.height - requisition.height);
4640 else if (needed_height > space_below && needed_height > space_above)
4642 if (space_below >= space_above)
4643 y = monitor.y + monitor.height - requisition.height;
4655 if (priv->initially_pushed_in)
4657 menu_height = requisition.height;
4659 if (y + menu_height > monitor.y + monitor.height)
4661 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4662 y = (monitor.y + monitor.height) - menu_height;
4667 scroll_offset += monitor.y - y;
4672 /* FIXME: should this be done in the various position_funcs ? */
4673 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4675 if (GTK_MENU_SHELL (menu)->priv->active)
4677 priv->have_position = TRUE;
4678 priv->position_x = x;
4679 priv->position_y = y;
4682 if (y + requisition.height > monitor.y + monitor.height)
4683 requisition.height = (monitor.y + monitor.height) - y;
4687 scroll_offset += monitor.y - y;
4688 requisition.height -= monitor.y - y;
4692 if (scroll_offset > 0)
4694 GtkBorder arrow_border;
4696 get_arrows_border (menu, &arrow_border);
4697 scroll_offset += arrow_border.top;
4700 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4703 if (!GTK_MENU_SHELL (menu)->priv->active)
4705 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4706 requisition.width, requisition.height);
4709 if (set_scroll_offset)
4710 priv->scroll_offset = scroll_offset;
4714 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4716 GtkMenuPrivate *priv = menu->priv;
4718 if (priv->scroll_timeout)
4720 g_source_remove (priv->scroll_timeout);
4721 priv->scroll_timeout = 0;
4726 gtk_menu_stop_scrolling (GtkMenu *menu)
4728 GtkMenuPrivate *priv = menu->priv;
4729 gboolean touchscreen_mode;
4731 gtk_menu_remove_scroll_timeout (menu);
4733 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4734 "gtk-touchscreen-mode", &touchscreen_mode,
4737 if (!touchscreen_mode)
4739 priv->upper_arrow_prelight = FALSE;
4740 priv->lower_arrow_prelight = FALSE;
4745 gtk_menu_scroll_to (GtkMenu *menu,
4748 GtkMenuPrivate *priv = menu->priv;
4749 GtkAllocation allocation;
4750 GtkBorder arrow_border, border;
4753 gint view_width, view_height;
4756 guint vertical_padding;
4757 guint horizontal_padding;
4758 gboolean double_arrows;
4760 widget = GTK_WIDGET (menu);
4762 if (priv->tearoff_active &&
4763 priv->tearoff_adjustment &&
4764 (priv->tearoff_adjustment->value != offset))
4766 priv->tearoff_adjustment->value =
4768 0, priv->tearoff_adjustment->upper - priv->tearoff_adjustment->page_size);
4769 gtk_adjustment_value_changed (priv->tearoff_adjustment);
4772 /* Move/resize the viewport according to arrows: */
4773 gtk_widget_get_allocation (widget, &allocation);
4774 view_width = allocation.width;
4775 view_height = allocation.height;
4777 gtk_widget_style_get (GTK_WIDGET (menu),
4778 "vertical-padding", &vertical_padding,
4779 "horizontal-padding", &horizontal_padding,
4782 get_menu_border (widget, &border);
4783 double_arrows = get_double_arrows (menu);
4785 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4787 view_width -= (2 * (border_width + horizontal_padding)) + border.left + border.right;
4788 view_height -= (2 * (border_width + vertical_padding)) + border.top + border.bottom;
4789 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4790 border.top - border.bottom;
4792 x = border_width + border.left + horizontal_padding;
4793 y = border_width + border.top + vertical_padding;
4795 if (double_arrows && !priv->tearoff_active)
4797 if (view_height < menu_height ||
4798 (offset > 0 && priv->scroll_offset > 0) ||
4799 (offset < 0 && priv->scroll_offset < 0))
4801 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4802 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4804 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4805 gtk_widget_queue_draw (GTK_WIDGET (menu));
4807 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4809 get_arrows_border (menu, &arrow_border);
4810 y += arrow_border.top;
4811 view_height -= arrow_border.top;
4812 view_height -= arrow_border.bottom;
4815 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4818 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4820 if (priv->upper_arrow_prelight)
4821 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4823 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4826 if (offset >= menu_height - view_height)
4827 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4830 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4832 if (priv->lower_arrow_prelight)
4833 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4835 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4838 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4839 (priv->lower_arrow_state != lower_arrow_previous_state))
4840 gtk_widget_queue_draw (GTK_WIDGET (menu));
4842 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4843 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4845 /* At the upper border, possibly remove timeout */
4846 if (priv->scroll_step < 0)
4848 gtk_menu_stop_scrolling (menu);
4849 gtk_widget_queue_draw (GTK_WIDGET (menu));
4853 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4854 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4856 /* At the lower border, possibly remove timeout */
4857 if (priv->scroll_step > 0)
4859 gtk_menu_stop_scrolling (menu);
4860 gtk_widget_queue_draw (GTK_WIDGET (menu));
4864 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4868 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4869 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4871 gtk_menu_stop_scrolling (menu);
4872 gtk_widget_queue_draw (GTK_WIDGET (menu));
4875 else if (!priv->tearoff_active)
4877 gboolean last_visible;
4879 last_visible = priv->upper_arrow_visible;
4880 priv->upper_arrow_visible = offset > 0;
4882 /* upper_arrow_visible may have changed, so requery the border */
4883 get_arrows_border (menu, &arrow_border);
4884 view_height -= arrow_border.top;
4886 if ((last_visible != priv->upper_arrow_visible) &&
4887 !priv->upper_arrow_visible)
4889 priv->upper_arrow_prelight = FALSE;
4891 /* If we hide the upper arrow, possibly remove timeout */
4892 if (priv->scroll_step < 0)
4894 gtk_menu_stop_scrolling (menu);
4895 gtk_widget_queue_draw (GTK_WIDGET (menu));
4899 last_visible = priv->lower_arrow_visible;
4900 priv->lower_arrow_visible = offset < menu_height - view_height;
4902 /* lower_arrow_visible may have changed, so requery the border */
4903 get_arrows_border (menu, &arrow_border);
4904 view_height -= arrow_border.bottom;
4906 if ((last_visible != priv->lower_arrow_visible) &&
4907 !priv->lower_arrow_visible)
4909 priv->lower_arrow_prelight = FALSE;
4911 /* If we hide the lower arrow, possibly remove timeout */
4912 if (priv->scroll_step > 0)
4914 gtk_menu_stop_scrolling (menu);
4915 gtk_widget_queue_draw (GTK_WIDGET (menu));
4919 y += arrow_border.top;
4922 /* Scroll the menu: */
4923 if (gtk_widget_get_realized (widget))
4924 gdk_window_move (priv->bin_window, 0, -offset);
4926 if (gtk_widget_get_realized (widget))
4927 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
4929 priv->scroll_offset = offset;
4933 compute_child_offset (GtkMenu *menu,
4934 GtkWidget *menu_item,
4937 gboolean *is_last_child)
4939 GtkMenuPrivate *priv = menu->priv;
4940 gint item_top_attach;
4941 gint item_bottom_attach;
4942 gint child_offset = 0;
4945 get_effective_child_attach (menu_item, NULL, NULL,
4946 &item_top_attach, &item_bottom_attach);
4948 /* there is a possibility that we get called before _size_request,
4949 * so check the height table for safety.
4951 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4954 /* when we have a row with only invisible children, it's height will
4955 * be zero, so there's no need to check WIDGET_VISIBLE here
4957 for (i = 0; i < item_top_attach; i++)
4958 child_offset += priv->heights[i];
4961 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4963 *offset = child_offset;
4965 *height = priv->heights[item_top_attach];
4971 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4972 GtkWidget *menu_item)
4974 GtkMenu *menu = GTK_MENU (menu_shell);
4975 GtkMenuPrivate *priv = menu->priv;
4976 GtkWidget *widget = GTK_WIDGET (menu_shell);
4977 gint child_offset, child_height;
4981 gboolean last_child = 0;
4983 /* We need to check if the selected item fully visible.
4984 * If not we need to scroll the menu so that it becomes fully
4988 if (compute_child_offset (menu, menu_item,
4989 &child_offset, &child_height, &last_child))
4991 guint vertical_padding;
4992 gboolean double_arrows;
4994 y = priv->scroll_offset;
4995 width = gdk_window_get_width (gtk_widget_get_window (widget));
4996 height = gdk_window_get_height (gtk_widget_get_window (widget));
4998 gtk_widget_style_get (widget,
4999 "vertical-padding", &vertical_padding,
5002 double_arrows = get_double_arrows (menu);
5004 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5005 2 * gtk_widget_get_style (widget)->ythickness +
5006 2 * vertical_padding;
5007 if (child_offset < y)
5009 /* Ignore the enter event we might get if the pointer
5012 menu_shell->priv->ignore_enter = TRUE;
5013 gtk_menu_scroll_to (menu, child_offset);
5017 GtkBorder arrow_border;
5021 get_arrows_border (menu, &arrow_border);
5022 if (!priv->tearoff_active)
5023 arrow_height = arrow_border.top + arrow_border.bottom;
5025 if (child_offset + child_height > y + height - arrow_height)
5028 if ((!last_child && !priv->tearoff_active) || double_arrows)
5029 arrow_height += arrow_border.bottom;
5031 y = child_offset + child_height - height + arrow_height;
5032 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5034 /* Need upper arrow */
5035 arrow_height += arrow_border.top;
5036 y = child_offset + child_height - height + arrow_height;
5038 /* Ignore the enter event we might get if the pointer
5041 menu_shell->priv->ignore_enter = TRUE;
5042 gtk_menu_scroll_to (menu, y);
5049 gtk_menu_select_item (GtkMenuShell *menu_shell,
5050 GtkWidget *menu_item)
5052 GtkMenu *menu = GTK_MENU (menu_shell);
5054 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5055 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5057 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5061 /* Reparent the menu, taking care of the refcounting
5063 * If unrealize is true we force a unrealize while reparenting the parent.
5064 * This can help eliminate flicker in some cases.
5066 * What happens is that when the menu is unrealized and then re-realized,
5067 * the allocations are as follows:
5069 * parent - 1x1 at (0,0)
5070 * child1 - 100x20 at (0,0)
5071 * child2 - 100x20 at (0,20)
5072 * child3 - 100x20 at (0,40)
5074 * That is, the parent is small but the children are full sized. Then,
5075 * when the queued_resize gets processed, the parent gets resized to
5078 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5079 * contains the following logic:
5081 * - if a move or resize operation on a window would change the clip
5082 * region on the children, then before the window is resized
5083 * the background for children is temporarily set to None, the
5084 * move/resize done, and the background for the children restored.
5086 * So, at the point where the parent is resized to final size, the
5087 * background for the children is temporarily None, and thus they
5088 * are not cleared to the background color and the previous background
5089 * (the image of the menu) is left in place.
5092 gtk_menu_reparent (GtkMenu *menu,
5093 GtkWidget *new_parent,
5096 GObject *object = G_OBJECT (menu);
5097 GtkWidget *widget = GTK_WIDGET (menu);
5098 gboolean was_floating = g_object_is_floating (object);
5100 g_object_ref_sink (object);
5104 g_object_ref (object);
5105 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5106 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5107 g_object_unref (object);
5110 gtk_widget_reparent (widget, new_parent);
5113 g_object_force_floating (object);
5115 g_object_unref (object);
5119 gtk_menu_show_all (GtkWidget *widget)
5121 /* Show children, but not self. */
5122 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5126 * gtk_menu_set_screen:
5128 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5129 * determined by the widget the menu is attached to
5131 * Sets the #GdkScreen on which the menu will be displayed.
5136 gtk_menu_set_screen (GtkMenu *menu,
5139 g_return_if_fail (GTK_IS_MENU (menu));
5140 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5142 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5146 menu_change_screen (menu, screen);
5150 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5152 attach_widget_screen_changed (attach_widget, NULL, menu);
5159 * @child: a #GtkMenuItem
5160 * @left_attach: The column number to attach the left side of the item to
5161 * @right_attach: The column number to attach the right side of the item to
5162 * @top_attach: The row number to attach the top of the item to
5163 * @bottom_attach: The row number to attach the bottom of the item to
5165 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5166 * an item will occupy is specified by @left_attach, @right_attach,
5167 * @top_attach and @bottom_attach. These each represent the leftmost,
5168 * rightmost, uppermost and lower column and row numbers of the table.
5169 * (Columns and rows are indexed from zero).
5171 * Note that this function is not related to gtk_menu_detach().
5176 gtk_menu_attach (GtkMenu *menu,
5181 guint bottom_attach)
5183 GtkMenuShell *menu_shell;
5186 g_return_if_fail (GTK_IS_MENU (menu));
5187 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5188 parent = gtk_widget_get_parent (child);
5189 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5190 g_return_if_fail (left_attach < right_attach);
5191 g_return_if_fail (top_attach < bottom_attach);
5193 menu_shell = GTK_MENU_SHELL (menu);
5197 AttachInfo *ai = get_attach_info (child);
5199 ai->left_attach = left_attach;
5200 ai->right_attach = right_attach;
5201 ai->top_attach = top_attach;
5202 ai->bottom_attach = bottom_attach;
5204 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5206 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5208 menu_queue_resize (menu);
5212 gtk_container_child_set (GTK_CONTAINER (parent), child,
5213 "left-attach", left_attach,
5214 "right-attach", right_attach,
5215 "top-attach", top_attach,
5216 "bottom-attach", bottom_attach,
5222 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5226 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5227 "gtk-menu-popup-delay", &popup_delay,
5234 find_child_containing (GtkMenuShell *menu_shell,
5242 /* find a child which includes the area given by
5243 * left, right, top, bottom.
5245 for (list = menu_shell->priv->children; list; list = list->next)
5249 if (!_gtk_menu_item_is_selectable (list->data))
5252 get_effective_child_attach (list->data, &l, &r, &t, &b);
5254 if (l <= left && right <= r && t <= top && bottom <= b)
5255 return GTK_WIDGET (list->data);
5262 gtk_menu_move_current (GtkMenuShell *menu_shell,
5263 GtkMenuDirectionType direction)
5265 GtkMenu *menu = GTK_MENU (menu_shell);
5268 GtkWidget *match = NULL;
5270 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5274 case GTK_MENU_DIR_CHILD:
5275 direction = GTK_MENU_DIR_PARENT;
5277 case GTK_MENU_DIR_PARENT:
5278 direction = GTK_MENU_DIR_CHILD;
5284 /* use special table menu key bindings */
5285 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5287 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5289 if (direction == GTK_MENU_DIR_NEXT)
5291 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5293 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5301 for (i = 0; i < t; i++)
5303 match = find_child_containing (menu_shell,
5304 l, l + 1, i, i + 1);
5310 else if (direction == GTK_MENU_DIR_PREV)
5312 for (i = t; i > 0; i--)
5314 match = find_child_containing (menu_shell,
5315 l, l + 1, i - 1, i);
5323 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5325 match = find_child_containing (menu_shell,
5326 l, l + 1, i - 1, i);
5332 else if (direction == GTK_MENU_DIR_PARENT)
5334 /* we go one left if possible */
5336 match = find_child_containing (menu_shell,
5337 l - 1, l, t, t + 1);
5341 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5344 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5345 match = menu_shell->priv->active_menu_item;
5348 else if (direction == GTK_MENU_DIR_CHILD)
5350 /* we go one right if possible */
5351 if (r < gtk_menu_get_n_columns (menu))
5352 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5356 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5358 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5360 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5361 match = menu_shell->priv->active_menu_item;
5367 gtk_menu_shell_select_item (menu_shell, match);
5372 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5376 get_visible_size (GtkMenu *menu)
5378 GtkMenuPrivate *priv = menu->priv;
5379 GtkAllocation allocation;
5380 GtkWidget *widget = GTK_WIDGET (menu);
5381 GtkContainer *container = GTK_CONTAINER (menu);
5384 gtk_widget_get_allocation (widget, &allocation);
5385 menu_height = (allocation.height
5386 - 2 * (gtk_container_get_border_width (container)
5387 + gtk_widget_get_style (widget)->ythickness));
5389 if (!priv->tearoff_active)
5391 GtkBorder arrow_border;
5393 get_arrows_border (menu, &arrow_border);
5394 menu_height -= arrow_border.top;
5395 menu_height -= arrow_border.bottom;
5401 /* Find the sensitive on-screen child containing @y, or if none,
5402 * the nearest selectable onscreen child. (%NULL if none)
5405 child_at (GtkMenu *menu,
5408 GtkMenuPrivate *priv = menu->priv;
5409 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5410 GtkWidget *child = NULL;
5411 gint child_offset = 0;
5414 gint lower, upper; /* Onscreen bounds */
5416 menu_height = get_visible_size (menu);
5417 lower = priv->scroll_offset;
5418 upper = priv->scroll_offset + menu_height;
5420 for (children = menu_shell->priv->children; children; children = children->next)
5422 if (gtk_widget_get_visible (children->data))
5424 GtkRequisition child_requisition;
5426 gtk_widget_get_preferred_size (children->data,
5427 &child_requisition, NULL);
5429 if (_gtk_menu_item_is_selectable (children->data) &&
5430 child_offset >= lower &&
5431 child_offset + child_requisition.height <= upper)
5433 child = children->data;
5435 if (child_offset + child_requisition.height > y &&
5436 !GTK_IS_TEAROFF_MENU_ITEM (child))
5440 child_offset += child_requisition.height;
5448 get_menu_height (GtkMenu *menu)
5450 GtkMenuPrivate *priv = menu->priv;
5451 GtkAllocation allocation;
5452 GtkWidget *widget = GTK_WIDGET (menu);
5455 gtk_widget_get_allocation (widget, &allocation);
5457 height = allocation.height;
5458 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) + gtk_widget_get_style (widget)->ythickness) * 2;
5460 if (!priv->tearoff_active)
5462 GtkBorder arrow_border;
5464 get_arrows_border (menu, &arrow_border);
5465 height -= arrow_border.top;
5466 height -= arrow_border.bottom;
5473 gtk_menu_real_move_scroll (GtkMenu *menu,
5476 GtkMenuPrivate *priv = menu->priv;
5477 gint page_size = get_visible_size (menu);
5478 gint end_position = get_menu_height (menu);
5479 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5483 case GTK_SCROLL_PAGE_UP:
5484 case GTK_SCROLL_PAGE_DOWN:
5488 gint child_offset = 0;
5489 gboolean old_upper_arrow_visible;
5492 if (type == GTK_SCROLL_PAGE_UP)
5497 if (menu_shell->priv->active_menu_item)
5501 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5502 &child_offset, &child_height, NULL);
5503 child_offset += child_height / 2;
5506 menu_shell->priv->ignore_enter = TRUE;
5507 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5508 old_offset = priv->scroll_offset;
5510 new_offset = priv->scroll_offset + step;
5511 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5513 gtk_menu_scroll_to (menu, new_offset);
5515 if (menu_shell->priv->active_menu_item)
5517 GtkWidget *new_child;
5518 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5519 GtkBorder arrow_border;
5520 get_arrows_border (menu, &arrow_border);
5522 if (priv->scroll_offset != old_offset)
5523 step = priv->scroll_offset - old_offset;
5525 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5527 new_child = child_at (menu, child_offset + step);
5529 gtk_menu_shell_select_item (menu_shell, new_child);
5533 case GTK_SCROLL_START:
5534 /* Ignore the enter event we might get if the pointer is on the menu */
5535 menu_shell->priv->ignore_enter = TRUE;
5536 gtk_menu_scroll_to (menu, 0);
5537 gtk_menu_shell_select_first (menu_shell, TRUE);
5539 case GTK_SCROLL_END:
5540 /* Ignore the enter event we might get if the pointer is on the menu */
5541 menu_shell->priv->ignore_enter = TRUE;
5542 gtk_menu_scroll_to (menu, end_position - page_size);
5543 _gtk_menu_shell_select_last (menu_shell, TRUE);
5552 * gtk_menu_set_monitor:
5554 * @monitor_num: the number of the monitor on which the menu should
5557 * Informs GTK+ on which monitor a menu should be popped up.
5558 * See gdk_screen_get_monitor_geometry().
5560 * This function should be called from a #GtkMenuPositionFunc
5561 * if the menu should not appear on the same monitor as the pointer.
5562 * This information can't be reliably inferred from the coordinates
5563 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5564 * these coordinates may extend beyond the monitor boundaries or even
5565 * the screen boundaries.
5570 gtk_menu_set_monitor (GtkMenu *menu,
5573 GtkMenuPrivate *priv = menu->priv;
5575 g_return_if_fail (GTK_IS_MENU (menu));
5577 priv->monitor_num = monitor_num;
5581 * gtk_menu_get_monitor:
5584 * Retrieves the number of the monitor on which to show the menu.
5586 * Returns: the number of the monitor on which the menu should
5587 * be popped up or -1, if no monitor has been set
5592 gtk_menu_get_monitor (GtkMenu *menu)
5594 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5596 return menu->priv->monitor_num;
5600 * gtk_menu_get_for_attach_widget:
5601 * @widget: a #GtkWidget
5603 * Returns a list of the menus which are attached to this widget.
5604 * This list is owned by GTK+ and must not be modified.
5606 * Return value: (element-type GtkWidget) (transfer none): the list
5607 * of menus attached to his widget.
5612 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5616 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5618 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5624 gtk_menu_grab_notify (GtkWidget *widget,
5625 gboolean was_grabbed)
5627 GtkWidget *toplevel;
5628 GtkWindowGroup *group;
5632 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5635 !gtk_widget_device_is_shadowed (widget, pointer))
5638 toplevel = gtk_widget_get_toplevel (widget);
5640 if (!GTK_IS_WINDOW (toplevel))
5643 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5644 grab = gtk_window_group_get_current_device_grab (group, pointer);
5646 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5647 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5651 * gtk_menu_set_reserve_toggle_size:
5653 * @reserve_toggle_size: whether to reserve size for toggles
5655 * Sets whether the menu should reserve space for drawing toggles
5656 * or icons, regardless of their actual presence.
5661 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5662 gboolean reserve_toggle_size)
5664 GtkMenuPrivate *priv = menu->priv;
5665 gboolean no_toggle_size;
5667 g_return_if_fail (GTK_IS_MENU (menu));
5669 no_toggle_size = !reserve_toggle_size;
5671 if (priv->no_toggle_size != no_toggle_size)
5673 priv->no_toggle_size = no_toggle_size;
5675 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5680 * gtk_menu_get_reserve_toggle_size:
5683 * Returns whether the menu reserves space for toggles and
5684 * icons, regardless of their actual presence.
5686 * Returns: Whether the menu reserves toggle space
5691 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5693 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5695 return !menu->priv->no_toggle_size;