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/.
27 #define GTK_MENU_INTERNALS
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
34 #include "gtkcheckmenuitem.h"
35 #include <gobject/gvaluecollector.h>
37 #include "gtkmarshalers.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
49 #define DEFAULT_POPUP_DELAY 225
50 #define DEFAULT_POPDOWN_DELAY 1000
52 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
53 * extends below the submenu
56 #define MENU_SCROLL_STEP1 8
57 #define MENU_SCROLL_STEP2 15
58 #define MENU_SCROLL_FAST_ZONE 8
59 #define MENU_SCROLL_TIMEOUT1 50
60 #define MENU_SCROLL_TIMEOUT2 20
62 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
63 #define ATTACHED_MENUS "gtk-attached-menus"
65 typedef struct _GtkMenuAttachData GtkMenuAttachData;
66 typedef struct _GtkMenuPrivate GtkMenuPrivate;
67 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
69 struct _GtkMenuAttachData
71 GtkWidget *attach_widget;
72 GtkMenuDetachFunc detacher;
75 struct _GtkMenuPrivate
79 gboolean initially_pushed_in;
81 /* info used for the table */
87 /* Cached layout information */
94 GtkStateType lower_arrow_state;
95 GtkStateType upper_arrow_state;
97 guint have_layout : 1;
98 guint seen_item_enter : 1;
99 guint have_position : 1;
100 guint ignore_button_release : 1;
101 guint no_toggle_size : 1;
104 struct _GtkMenuPopdownData
116 gint effective_left_attach;
117 gint effective_right_attach;
118 gint effective_top_attach;
119 gint effective_bottom_attach;
136 PROP_RESERVE_TOGGLE_SIZE
141 CHILD_PROP_LEFT_ATTACH,
142 CHILD_PROP_RIGHT_ATTACH,
143 CHILD_PROP_TOP_ATTACH,
144 CHILD_PROP_BOTTOM_ATTACH
147 static void gtk_menu_set_property (GObject *object,
151 static void gtk_menu_get_property (GObject *object,
155 static void gtk_menu_set_child_property(GtkContainer *container,
160 static void gtk_menu_get_child_property(GtkContainer *container,
165 static void gtk_menu_destroy (GtkObject *object);
166 static void gtk_menu_realize (GtkWidget *widget);
167 static void gtk_menu_unrealize (GtkWidget *widget);
168 static void gtk_menu_size_request (GtkWidget *widget,
169 GtkRequisition *requisition);
170 static void gtk_menu_size_allocate (GtkWidget *widget,
171 GtkAllocation *allocation);
172 static void gtk_menu_paint (GtkWidget *widget,
173 GdkEventExpose *expose);
174 static void gtk_menu_show (GtkWidget *widget);
175 static gboolean gtk_menu_expose (GtkWidget *widget,
176 GdkEventExpose *event);
177 static gboolean gtk_menu_key_press (GtkWidget *widget,
179 static gboolean gtk_menu_scroll (GtkWidget *widget,
180 GdkEventScroll *event);
181 static gboolean gtk_menu_button_press (GtkWidget *widget,
182 GdkEventButton *event);
183 static gboolean gtk_menu_button_release (GtkWidget *widget,
184 GdkEventButton *event);
185 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
186 GdkEventMotion *event);
187 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
188 GdkEventCrossing *event);
189 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
190 GdkEventCrossing *event);
191 static void gtk_menu_scroll_to (GtkMenu *menu,
193 static void gtk_menu_grab_notify (GtkWidget *widget,
194 gboolean was_grabbed);
196 static void gtk_menu_stop_scrolling (GtkMenu *menu);
197 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
198 static gboolean gtk_menu_scroll_timeout (gpointer data);
199 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
200 static void gtk_menu_start_scrolling (GtkMenu *menu);
202 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
203 GtkWidget *menu_item);
204 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
205 GtkWidget *menu_item);
206 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
209 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
211 static void gtk_menu_handle_scrolling (GtkMenu *menu,
216 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
218 static void gtk_menu_style_set (GtkWidget *widget,
219 GtkStyle *previous_style);
220 static gboolean gtk_menu_focus (GtkWidget *widget,
221 GtkDirectionType direction);
222 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
223 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
224 GtkMenuDirectionType direction);
225 static void gtk_menu_real_move_scroll (GtkMenu *menu,
228 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
229 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
230 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
233 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
234 GtkMenuItem *menu_item,
235 GdkEventCrossing *event);
237 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
238 static void gtk_menu_show_all (GtkWidget *widget);
239 static void gtk_menu_hide_all (GtkWidget *widget);
240 static void gtk_menu_position (GtkMenu *menu);
241 static void gtk_menu_reparent (GtkMenu *menu,
242 GtkWidget *new_parent,
244 static void gtk_menu_remove (GtkContainer *menu,
247 static void gtk_menu_update_title (GtkMenu *menu);
249 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
250 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
252 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
254 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
255 gboolean group_changed);
257 static const gchar attach_data_key[] = "gtk-menu-attach-data";
259 static guint menu_signals[LAST_SIGNAL] = { 0 };
261 static GtkMenuPrivate *
262 gtk_menu_get_private (GtkMenu *menu)
264 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
267 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
270 menu_queue_resize (GtkMenu *menu)
272 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
274 priv->have_layout = FALSE;
275 gtk_widget_queue_resize (GTK_WIDGET (menu));
279 attach_info_free (AttachInfo *info)
281 g_slice_free (AttachInfo, info);
285 get_attach_info (GtkWidget *child)
287 GObject *object = G_OBJECT (child);
288 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
292 ai = g_slice_new0 (AttachInfo);
293 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
294 (GDestroyNotify) attach_info_free);
301 is_grid_attached (AttachInfo *ai)
303 return (ai->left_attach >= 0 &&
304 ai->right_attach >= 0 &&
305 ai->top_attach >= 0 &&
306 ai->bottom_attach >= 0);
310 menu_ensure_layout (GtkMenu *menu)
312 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
314 if (!priv->have_layout)
316 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
320 gint max_right_attach;
321 gint max_bottom_attach;
323 /* Find extents of gridded portion
325 max_right_attach = 1;
326 max_bottom_attach = 0;
328 for (l = menu_shell->children; l; l = l->next)
330 GtkWidget *child = l->data;
331 AttachInfo *ai = get_attach_info (child);
333 if (is_grid_attached (ai))
335 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
336 max_right_attach = MAX (max_right_attach, ai->right_attach);
342 row_occupied = g_malloc0 (max_bottom_attach);
344 for (l = menu_shell->children; l; l = l->next)
346 GtkWidget *child = l->data;
347 AttachInfo *ai = get_attach_info (child);
349 if (is_grid_attached (ai))
353 for (i = ai->top_attach; i < ai->bottom_attach; i++)
354 row_occupied[i] = TRUE;
358 /* Lay non-grid-items out in those rows
361 for (l = menu_shell->children; l; l = l->next)
363 GtkWidget *child = l->data;
364 AttachInfo *ai = get_attach_info (child);
366 if (!is_grid_attached (ai))
368 while (current_row < max_bottom_attach && row_occupied[current_row])
371 ai->effective_left_attach = 0;
372 ai->effective_right_attach = max_right_attach;
373 ai->effective_top_attach = current_row;
374 ai->effective_bottom_attach = current_row + 1;
380 ai->effective_left_attach = ai->left_attach;
381 ai->effective_right_attach = ai->right_attach;
382 ai->effective_top_attach = ai->top_attach;
383 ai->effective_bottom_attach = ai->bottom_attach;
387 g_free (row_occupied);
389 priv->n_rows = MAX (current_row, max_bottom_attach);
390 priv->n_columns = max_right_attach;
391 priv->have_layout = TRUE;
397 gtk_menu_get_n_columns (GtkMenu *menu)
399 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
401 menu_ensure_layout (menu);
403 return priv->n_columns;
407 gtk_menu_get_n_rows (GtkMenu *menu)
409 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
411 menu_ensure_layout (menu);
417 get_effective_child_attach (GtkWidget *child,
423 GtkMenu *menu = GTK_MENU (child->parent);
426 menu_ensure_layout (menu);
428 ai = get_attach_info (child);
431 *l = ai->effective_left_attach;
433 *r = ai->effective_right_attach;
435 *t = ai->effective_top_attach;
437 *b = ai->effective_bottom_attach;
442 gtk_menu_class_init (GtkMenuClass *class)
444 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
445 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
446 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
447 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
448 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
449 GtkBindingSet *binding_set;
451 gobject_class->set_property = gtk_menu_set_property;
452 gobject_class->get_property = gtk_menu_get_property;
454 object_class->destroy = gtk_menu_destroy;
456 widget_class->realize = gtk_menu_realize;
457 widget_class->unrealize = gtk_menu_unrealize;
458 widget_class->size_request = gtk_menu_size_request;
459 widget_class->size_allocate = gtk_menu_size_allocate;
460 widget_class->show = gtk_menu_show;
461 widget_class->expose_event = gtk_menu_expose;
462 widget_class->scroll_event = gtk_menu_scroll;
463 widget_class->key_press_event = gtk_menu_key_press;
464 widget_class->button_press_event = gtk_menu_button_press;
465 widget_class->button_release_event = gtk_menu_button_release;
466 widget_class->motion_notify_event = gtk_menu_motion_notify;
467 widget_class->show_all = gtk_menu_show_all;
468 widget_class->hide_all = gtk_menu_hide_all;
469 widget_class->enter_notify_event = gtk_menu_enter_notify;
470 widget_class->leave_notify_event = gtk_menu_leave_notify;
471 widget_class->style_set = gtk_menu_style_set;
472 widget_class->focus = gtk_menu_focus;
473 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
474 widget_class->grab_notify = gtk_menu_grab_notify;
476 container_class->remove = gtk_menu_remove;
477 container_class->get_child_property = gtk_menu_get_child_property;
478 container_class->set_child_property = gtk_menu_set_child_property;
480 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
481 menu_shell_class->deactivate = gtk_menu_deactivate;
482 menu_shell_class->select_item = gtk_menu_select_item;
483 menu_shell_class->insert = gtk_menu_real_insert;
484 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
485 menu_shell_class->move_current = gtk_menu_move_current;
487 menu_signals[MOVE_SCROLL] =
488 g_signal_new_class_handler (I_("move-scroll"),
489 G_OBJECT_CLASS_TYPE (object_class),
490 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
491 G_CALLBACK (gtk_menu_real_move_scroll),
493 _gtk_marshal_VOID__ENUM,
495 GTK_TYPE_SCROLL_TYPE);
500 * The index of the currently selected menu item, or -1 if no
501 * menu item is selected.
505 g_object_class_install_property (gobject_class,
507 g_param_spec_int ("active",
509 P_("The currently selected menu item"),
511 GTK_PARAM_READWRITE));
514 * GtkMenu:accel-group:
516 * The accel group holding accelerators for the menu.
520 g_object_class_install_property (gobject_class,
522 g_param_spec_object ("accel-group",
524 P_("The accel group holding accelerators for the menu"),
525 GTK_TYPE_ACCEL_GROUP,
526 GTK_PARAM_READWRITE));
529 * GtkMenu:accel-path:
531 * An accel path used to conveniently construct accel paths of child items.
535 g_object_class_install_property (gobject_class,
537 g_param_spec_string ("accel-path",
539 P_("An accel path used to conveniently construct accel paths of child items"),
541 GTK_PARAM_READWRITE));
544 * GtkMenu:attach-widget:
546 * The widget the menu is attached to. Setting this property attaches
547 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
548 * use gtk_menu_attach_to_widget() directly.
552 g_object_class_install_property (gobject_class,
554 g_param_spec_object ("attach-widget",
556 P_("The widget the menu is attached to"),
558 GTK_PARAM_READWRITE));
560 g_object_class_install_property (gobject_class,
562 g_param_spec_string ("tearoff-title",
564 P_("A title that may be displayed by the window manager when this menu is torn-off"),
566 GTK_PARAM_READWRITE));
569 * GtkMenu:tearoff-state:
571 * A boolean that indicates whether the menu is torn-off.
575 g_object_class_install_property (gobject_class,
577 g_param_spec_boolean ("tearoff-state",
579 P_("A boolean that indicates whether the menu is torn-off"),
581 GTK_PARAM_READWRITE));
586 * The monitor the menu will be popped up on.
590 g_object_class_install_property (gobject_class,
592 g_param_spec_int ("monitor",
594 P_("The monitor the menu will be popped up on"),
596 GTK_PARAM_READWRITE));
598 gtk_widget_class_install_style_property (widget_class,
599 g_param_spec_int ("vertical-padding",
600 P_("Vertical Padding"),
601 P_("Extra space at the top and bottom of the menu"),
605 GTK_PARAM_READABLE));
608 * GtkMenu:reserve-toggle-size:
610 * A boolean that indicates whether the menu reserves space for
611 * toggles and icons, regardless of their actual presence.
613 * This property should only be changed from its default value
614 * for special-purposes such as tabular menus. Regular menus that
615 * are connected to a menu bar or context menus should reserve
616 * toggle space for consistency.
620 g_object_class_install_property (gobject_class,
621 PROP_RESERVE_TOGGLE_SIZE,
622 g_param_spec_boolean ("reserve-toggle-size",
623 P_("Reserve Toggle Size"),
624 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
626 GTK_PARAM_READWRITE));
628 gtk_widget_class_install_style_property (widget_class,
629 g_param_spec_int ("horizontal-padding",
630 P_("Horizontal Padding"),
631 P_("Extra space at the left and right edges of the menu"),
635 GTK_PARAM_READABLE));
637 gtk_widget_class_install_style_property (widget_class,
638 g_param_spec_int ("vertical-offset",
639 P_("Vertical Offset"),
640 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
644 GTK_PARAM_READABLE));
646 gtk_widget_class_install_style_property (widget_class,
647 g_param_spec_int ("horizontal-offset",
648 P_("Horizontal Offset"),
649 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
653 GTK_PARAM_READABLE));
655 gtk_widget_class_install_style_property (widget_class,
656 g_param_spec_boolean ("double-arrows",
658 P_("When scrolling, always show both arrows."),
660 GTK_PARAM_READABLE));
663 * GtkMenu:arrow-placement:
665 * Indicates where scroll arrows should be placed.
669 gtk_widget_class_install_style_property (widget_class,
670 g_param_spec_enum ("arrow-placement",
671 P_("Arrow Placement"),
672 P_("Indicates where scroll arrows should be placed"),
673 GTK_TYPE_ARROW_PLACEMENT,
675 GTK_PARAM_READABLE));
677 gtk_container_class_install_child_property (container_class,
678 CHILD_PROP_LEFT_ATTACH,
679 g_param_spec_int ("left-attach",
681 P_("The column number to attach the left side of the child to"),
683 GTK_PARAM_READWRITE));
685 gtk_container_class_install_child_property (container_class,
686 CHILD_PROP_RIGHT_ATTACH,
687 g_param_spec_int ("right-attach",
689 P_("The column number to attach the right side of the child to"),
691 GTK_PARAM_READWRITE));
693 gtk_container_class_install_child_property (container_class,
694 CHILD_PROP_TOP_ATTACH,
695 g_param_spec_int ("top-attach",
697 P_("The row number to attach the top of the child to"),
699 GTK_PARAM_READWRITE));
701 gtk_container_class_install_child_property (container_class,
702 CHILD_PROP_BOTTOM_ATTACH,
703 g_param_spec_int ("bottom-attach",
705 P_("The row number to attach the bottom of the child to"),
707 GTK_PARAM_READWRITE));
710 * GtkMenu::arrow-scaling
712 * Arbitrary constant to scale down the size of the scroll arrow.
716 gtk_widget_class_install_style_property (widget_class,
717 g_param_spec_float ("arrow-scaling",
719 P_("Arbitrary constant to scale down the size of the scroll arrow"),
721 GTK_PARAM_READABLE));
723 binding_set = gtk_binding_set_by_class (class);
724 gtk_binding_entry_add_signal (binding_set,
726 I_("move-current"), 1,
727 GTK_TYPE_MENU_DIRECTION_TYPE,
729 gtk_binding_entry_add_signal (binding_set,
732 GTK_TYPE_MENU_DIRECTION_TYPE,
734 gtk_binding_entry_add_signal (binding_set,
737 GTK_TYPE_MENU_DIRECTION_TYPE,
739 gtk_binding_entry_add_signal (binding_set,
742 GTK_TYPE_MENU_DIRECTION_TYPE,
744 gtk_binding_entry_add_signal (binding_set,
747 GTK_TYPE_MENU_DIRECTION_TYPE,
748 GTK_MENU_DIR_PARENT);
749 gtk_binding_entry_add_signal (binding_set,
752 GTK_TYPE_MENU_DIRECTION_TYPE,
753 GTK_MENU_DIR_PARENT);
754 gtk_binding_entry_add_signal (binding_set,
757 GTK_TYPE_MENU_DIRECTION_TYPE,
759 gtk_binding_entry_add_signal (binding_set,
762 GTK_TYPE_MENU_DIRECTION_TYPE,
764 gtk_binding_entry_add_signal (binding_set,
767 GTK_TYPE_SCROLL_TYPE,
769 gtk_binding_entry_add_signal (binding_set,
772 GTK_TYPE_SCROLL_TYPE,
774 gtk_binding_entry_add_signal (binding_set,
777 GTK_TYPE_SCROLL_TYPE,
779 gtk_binding_entry_add_signal (binding_set,
782 GTK_TYPE_SCROLL_TYPE,
784 gtk_binding_entry_add_signal (binding_set,
787 GTK_TYPE_SCROLL_TYPE,
789 gtk_binding_entry_add_signal (binding_set,
792 GTK_TYPE_SCROLL_TYPE,
794 gtk_binding_entry_add_signal (binding_set,
797 GTK_TYPE_SCROLL_TYPE,
798 GTK_SCROLL_PAGE_DOWN);
799 gtk_binding_entry_add_signal (binding_set,
802 GTK_TYPE_SCROLL_TYPE,
803 GTK_SCROLL_PAGE_DOWN);
805 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
806 P_("Can change accelerators"),
807 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
809 GTK_PARAM_READWRITE));
811 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
812 P_("Delay before submenus appear"),
813 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
817 GTK_PARAM_READWRITE));
819 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
820 P_("Delay before hiding a submenu"),
821 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
824 DEFAULT_POPDOWN_DELAY,
825 GTK_PARAM_READWRITE));
827 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
832 gtk_menu_set_property (GObject *object,
837 GtkMenu *menu = GTK_MENU (object);
842 gtk_menu_set_active (menu, g_value_get_int (value));
844 case PROP_ACCEL_GROUP:
845 gtk_menu_set_accel_group (menu, g_value_get_object (value));
847 case PROP_ACCEL_PATH:
848 gtk_menu_set_accel_path (menu, g_value_get_string (value));
850 case PROP_ATTACH_WIDGET:
854 widget = gtk_menu_get_attach_widget (menu);
856 gtk_menu_detach (menu);
858 widget = (GtkWidget*) g_value_get_object (value);
860 gtk_menu_attach_to_widget (menu, widget, NULL);
863 case PROP_TEAROFF_STATE:
864 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
866 case PROP_TEAROFF_TITLE:
867 gtk_menu_set_title (menu, g_value_get_string (value));
870 gtk_menu_set_monitor (menu, g_value_get_int (value));
872 case PROP_RESERVE_TOGGLE_SIZE:
873 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
876 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
882 gtk_menu_get_property (GObject *object,
887 GtkMenu *menu = GTK_MENU (object);
892 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
894 case PROP_ACCEL_GROUP:
895 g_value_set_object (value, gtk_menu_get_accel_group (menu));
897 case PROP_ACCEL_PATH:
898 g_value_set_string (value, gtk_menu_get_accel_path (menu));
900 case PROP_ATTACH_WIDGET:
901 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
903 case PROP_TEAROFF_STATE:
904 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
906 case PROP_TEAROFF_TITLE:
907 g_value_set_string (value, gtk_menu_get_title (menu));
910 g_value_set_int (value, gtk_menu_get_monitor (menu));
912 case PROP_RESERVE_TOGGLE_SIZE:
913 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
916 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
922 gtk_menu_set_child_property (GtkContainer *container,
928 GtkMenu *menu = GTK_MENU (container);
929 AttachInfo *ai = get_attach_info (child);
933 case CHILD_PROP_LEFT_ATTACH:
934 ai->left_attach = g_value_get_int (value);
936 case CHILD_PROP_RIGHT_ATTACH:
937 ai->right_attach = g_value_get_int (value);
939 case CHILD_PROP_TOP_ATTACH:
940 ai->top_attach = g_value_get_int (value);
942 case CHILD_PROP_BOTTOM_ATTACH:
943 ai->bottom_attach = g_value_get_int (value);
947 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
951 menu_queue_resize (menu);
955 gtk_menu_get_child_property (GtkContainer *container,
961 AttachInfo *ai = get_attach_info (child);
965 case CHILD_PROP_LEFT_ATTACH:
966 g_value_set_int (value, ai->left_attach);
968 case CHILD_PROP_RIGHT_ATTACH:
969 g_value_set_int (value, ai->right_attach);
971 case CHILD_PROP_TOP_ATTACH:
972 g_value_set_int (value, ai->top_attach);
974 case CHILD_PROP_BOTTOM_ATTACH:
975 g_value_set_int (value, ai->bottom_attach);
979 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
985 gtk_menu_window_event (GtkWidget *window,
989 gboolean handled = FALSE;
991 g_object_ref (window);
997 case GDK_KEY_RELEASE:
998 handled = gtk_widget_event (menu, event);
1004 g_object_unref (window);
1005 g_object_unref (menu);
1011 gtk_menu_window_size_request (GtkWidget *window,
1012 GtkRequisition *requisition,
1015 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1017 if (private->have_position)
1019 GdkScreen *screen = gtk_widget_get_screen (window);
1020 GdkRectangle monitor;
1022 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
1024 if (private->y + requisition->height > monitor.y + monitor.height)
1025 requisition->height = monitor.y + monitor.height - private->y;
1027 if (private->y < monitor.y)
1028 requisition->height -= monitor.y - private->y;
1033 gtk_menu_init (GtkMenu *menu)
1035 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1037 menu->parent_menu_item = NULL;
1038 menu->old_active_menu_item = NULL;
1039 menu->accel_group = NULL;
1040 menu->position_func = NULL;
1041 menu->position_func_data = NULL;
1042 menu->toggle_size = 0;
1044 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1045 "type", GTK_WINDOW_POPUP,
1048 "signal::event", gtk_menu_window_event, menu,
1049 "signal::size-request", gtk_menu_window_size_request, menu,
1050 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1052 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1053 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1055 /* Refloat the menu, so that reference counting for the menu isn't
1056 * affected by it being a child of the toplevel
1058 g_object_force_floating (G_OBJECT (menu));
1059 menu->needs_destruction_ref_count = TRUE;
1061 menu->view_window = NULL;
1062 menu->bin_window = NULL;
1064 menu->scroll_offset = 0;
1065 menu->scroll_step = 0;
1066 menu->timeout_id = 0;
1067 menu->scroll_fast = FALSE;
1069 menu->tearoff_window = NULL;
1070 menu->tearoff_hbox = NULL;
1071 menu->torn_off = FALSE;
1072 menu->tearoff_active = FALSE;
1073 menu->tearoff_adjustment = NULL;
1074 menu->tearoff_scrollbar = NULL;
1076 menu->upper_arrow_visible = FALSE;
1077 menu->lower_arrow_visible = FALSE;
1078 menu->upper_arrow_prelight = FALSE;
1079 menu->lower_arrow_prelight = FALSE;
1081 priv->upper_arrow_state = GTK_STATE_NORMAL;
1082 priv->lower_arrow_state = GTK_STATE_NORMAL;
1084 priv->have_layout = FALSE;
1085 priv->monitor_num = -1;
1089 gtk_menu_destroy (GtkObject *object)
1091 GtkMenu *menu = GTK_MENU (object);
1092 GtkMenuAttachData *data;
1093 GtkMenuPrivate *priv;
1095 gtk_menu_remove_scroll_timeout (menu);
1097 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1099 gtk_menu_detach (menu);
1101 gtk_menu_stop_navigating_submenu (menu);
1103 if (menu->old_active_menu_item)
1105 g_object_unref (menu->old_active_menu_item);
1106 menu->old_active_menu_item = NULL;
1109 /* Add back the reference count for being a child */
1110 if (menu->needs_destruction_ref_count)
1112 menu->needs_destruction_ref_count = FALSE;
1113 g_object_ref (object);
1116 if (menu->accel_group)
1118 g_object_unref (menu->accel_group);
1119 menu->accel_group = NULL;
1123 gtk_widget_destroy (menu->toplevel);
1125 if (menu->tearoff_window)
1126 gtk_widget_destroy (menu->tearoff_window);
1128 priv = gtk_menu_get_private (menu);
1132 g_free (priv->heights);
1133 priv->heights = NULL;
1138 g_free (priv->title);
1142 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1146 menu_change_screen (GtkMenu *menu,
1147 GdkScreen *new_screen)
1149 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1151 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1153 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1159 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1160 gtk_menu_position (menu);
1163 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1164 private->monitor_num = -1;
1168 attach_widget_screen_changed (GtkWidget *attach_widget,
1169 GdkScreen *previous_screen,
1172 if (gtk_widget_has_screen (attach_widget) &&
1173 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1175 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1180 gtk_menu_attach_to_widget (GtkMenu *menu,
1181 GtkWidget *attach_widget,
1182 GtkMenuDetachFunc detacher)
1184 GtkMenuAttachData *data;
1187 g_return_if_fail (GTK_IS_MENU (menu));
1188 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1190 /* keep this function in sync with gtk_widget_set_parent()
1193 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1196 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1197 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1201 g_object_ref_sink (menu);
1203 data = g_slice_new (GtkMenuAttachData);
1204 data->attach_widget = attach_widget;
1206 g_signal_connect (attach_widget, "screen-changed",
1207 G_CALLBACK (attach_widget_screen_changed), menu);
1208 attach_widget_screen_changed (attach_widget, NULL, menu);
1210 data->detacher = detacher;
1211 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1212 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1213 if (!g_list_find (list, menu))
1215 list = g_list_prepend (list, menu);
1217 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1218 (GDestroyNotify) g_list_free);
1220 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1221 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1223 /* we don't need to set the style here, since
1224 * we are a toplevel widget.
1227 /* Fallback title for menu comes from attach widget */
1228 gtk_menu_update_title (menu);
1232 gtk_menu_get_attach_widget (GtkMenu *menu)
1234 GtkMenuAttachData *data;
1236 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1238 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1240 return data->attach_widget;
1245 gtk_menu_detach (GtkMenu *menu)
1247 GtkMenuAttachData *data;
1250 g_return_if_fail (GTK_IS_MENU (menu));
1252 /* keep this function in sync with gtk_widget_unparent()
1254 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1257 g_warning ("gtk_menu_detach(): menu is not attached");
1260 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1262 g_signal_handlers_disconnect_by_func (data->attach_widget,
1263 (gpointer) attach_widget_screen_changed,
1267 data->detacher (data->attach_widget, menu);
1268 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1269 list = g_list_remove (list, menu);
1271 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1272 (GDestroyNotify) g_list_free);
1274 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1276 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1277 gtk_widget_unrealize (GTK_WIDGET (menu));
1279 g_slice_free (GtkMenuAttachData, data);
1281 /* Fallback title for menu comes from attach widget */
1282 gtk_menu_update_title (menu);
1284 g_object_unref (menu);
1288 gtk_menu_remove (GtkContainer *container,
1291 GtkMenu *menu = GTK_MENU (container);
1293 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1295 /* Clear out old_active_menu_item if it matches the item we are removing
1297 if (menu->old_active_menu_item == widget)
1299 g_object_unref (menu->old_active_menu_item);
1300 menu->old_active_menu_item = NULL;
1303 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1304 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1306 menu_queue_resize (menu);
1312 return g_object_new (GTK_TYPE_MENU, NULL);
1316 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1320 GtkMenu *menu = GTK_MENU (menu_shell);
1321 AttachInfo *ai = get_attach_info (child);
1323 ai->left_attach = -1;
1324 ai->right_attach = -1;
1325 ai->top_attach = -1;
1326 ai->bottom_attach = -1;
1328 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1329 gtk_widget_set_parent_window (child, menu->bin_window);
1331 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1333 menu_queue_resize (menu);
1337 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1342 widget = GTK_WIDGET (menu);
1348 GdkGCValues gc_values;
1350 menu->tearoff_active = FALSE;
1351 menu->saved_scroll_offset = menu->scroll_offset;
1353 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1354 gc = gdk_gc_new_with_values (widget->window,
1355 &gc_values, GDK_GC_SUBWINDOW);
1357 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1359 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1364 gdk_draw_drawable (pixmap, gc,
1365 menu->tearoff_window->window,
1366 0, 0, 0, 0, -1, -1);
1367 g_object_unref (gc);
1369 gtk_widget_set_size_request (menu->tearoff_window,
1373 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1374 g_object_unref (pixmap);
1379 popup_grab_on_window (GdkWindow *window,
1380 GdkDevice *keyboard,
1382 guint32 activate_time)
1385 gdk_device_grab (keyboard, window,
1386 GDK_OWNERSHIP_WINDOW, TRUE,
1387 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1388 NULL, activate_time) != GDK_GRAB_SUCCESS)
1392 gdk_device_grab (pointer, window,
1393 GDK_OWNERSHIP_WINDOW, TRUE,
1394 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1395 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1396 GDK_POINTER_MOTION_MASK,
1397 NULL, activate_time) != GDK_GRAB_SUCCESS)
1400 gdk_device_ungrab (keyboard, activate_time);
1409 * gtk_menu_popup_for_device:
1410 * @menu: a #GtkMenu.
1411 * @device: (allow-none): a #GdkDevice
1412 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1413 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1414 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1415 * @data: (allow-none): user supplied data to be passed to @func.
1416 * @button: the mouse button which was pressed to initiate the event.
1417 * @activate_time: the time at which the activation event occurred.
1419 * Displays a menu and makes it available for selection. Applications can use
1420 * this function to display context-sensitive menus, and will typically supply
1421 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1422 * parameters. The default menu positioning function will position the menu
1423 * at the current position of @device (or its corresponding pointer).
1425 * The @button parameter should be the mouse button pressed to initiate
1426 * the menu popup. If the menu popup was initiated by something other than
1427 * a mouse button press, such as a mouse button release or a keypress,
1428 * @button should be 0.
1430 * The @activate_time parameter is used to conflict-resolve initiation of
1431 * concurrent requests for mouse/keyboard grab requests. To function
1432 * properly, this needs to be the time stamp of the user event (such as
1433 * a mouse click or key press) that caused the initiation of the popup.
1434 * Only if no such event is available, gtk_get_current_event_time() can
1440 gtk_menu_popup_for_device (GtkMenu *menu,
1442 GtkWidget *parent_menu_shell,
1443 GtkWidget *parent_menu_item,
1444 GtkMenuPositionFunc func,
1447 guint32 activate_time)
1450 GtkWidget *xgrab_shell;
1452 GdkEvent *current_event;
1453 GtkMenuShell *menu_shell;
1454 gboolean grab_keyboard;
1455 GtkMenuPrivate *priv;
1456 GtkWidget *parent_toplevel;
1457 GdkDevice *keyboard, *pointer;
1459 g_return_if_fail (GTK_IS_MENU (menu));
1460 g_return_if_fail (GDK_IS_DEVICE (device));
1462 widget = GTK_WIDGET (menu);
1463 menu_shell = GTK_MENU_SHELL (menu);
1464 priv = gtk_menu_get_private (menu);
1466 if (device->source == GDK_SOURCE_KEYBOARD)
1469 pointer = gdk_device_get_associated_device (device);
1474 keyboard = gdk_device_get_associated_device (device);
1477 menu_shell->parent_menu_shell = parent_menu_shell;
1479 priv->seen_item_enter = FALSE;
1481 /* Find the last viewable ancestor, and make an X grab on it
1483 parent = GTK_WIDGET (menu);
1487 gboolean viewable = TRUE;
1488 GtkWidget *tmp = parent;
1492 if (!gtk_widget_get_mapped (tmp))
1501 xgrab_shell = parent;
1503 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1506 /* We want to receive events generated when we map the menu; unfortunately,
1507 * since there is probably already an implicit grab in place from the
1508 * button that the user used to pop up the menu, we won't receive then --
1509 * in particular, the EnterNotify when the menu pops up under the pointer.
1511 * If we are grabbing on a parent menu shell, no problem; just grab on
1512 * that menu shell first before popping up the window with owner_events = TRUE.
1514 * When grabbing on the menu itself, things get more convuluted - we
1515 * we do an explicit grab on a specially created window with
1516 * owner_events = TRUE, which we override further down with a grab
1517 * on the menu. (We can't grab on the menu until it is mapped; we
1518 * probably could just leave the grab on the other window, with a
1519 * little reorganization of the code in gtkmenu*).
1521 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1522 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1527 if (xgrab_shell && xgrab_shell != widget)
1529 if (popup_grab_on_window (xgrab_shell->window, keyboard, pointer, activate_time))
1531 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1532 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1537 GdkWindow *transfer_window;
1539 xgrab_shell = widget;
1540 transfer_window = menu_grab_transfer_window_get (menu);
1541 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1543 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1544 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1548 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1550 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1551 * with a stuck up window, we just abort here. Presumably the user will
1554 menu_shell->parent_menu_shell = NULL;
1555 menu_grab_transfer_window_destroy (menu);
1559 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1560 menu_shell->active = TRUE;
1561 menu_shell->button = button;
1563 /* If we are popping up the menu from something other than, a button
1564 * press then, as a heuristic, we ignore enter events for the menu
1565 * until we get a MOTION_NOTIFY.
1568 current_event = gtk_get_current_event ();
1571 if ((current_event->type != GDK_BUTTON_PRESS) &&
1572 (current_event->type != GDK_ENTER_NOTIFY))
1573 menu_shell->ignore_enter = TRUE;
1575 gdk_event_free (current_event);
1578 menu_shell->ignore_enter = TRUE;
1582 gtk_menu_tearoff_bg_copy (menu);
1584 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1587 parent_toplevel = NULL;
1588 if (parent_menu_shell)
1589 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1590 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1592 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1594 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1597 /* Set transient for to get the right window group and parent relationship */
1598 if (GTK_IS_WINDOW (parent_toplevel))
1599 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1600 GTK_WINDOW (parent_toplevel));
1602 menu->parent_menu_item = parent_menu_item;
1603 menu->position_func = func;
1604 menu->position_func_data = data;
1605 menu_shell->activate_time = activate_time;
1607 /* We need to show the menu here rather in the init function because
1608 * code expects to be able to tell if the menu is onscreen by
1609 * looking at the gtk_widget_get_visible (menu)
1611 gtk_widget_show (GTK_WIDGET (menu));
1613 /* Position the menu, possibly changing the size request
1615 gtk_menu_position (menu);
1617 /* Compute the size of the toplevel and realize it so we
1618 * can scroll correctly.
1621 GtkRequisition tmp_request;
1622 GtkAllocation tmp_allocation = { 0, };
1624 gtk_widget_size_request (menu->toplevel, &tmp_request);
1626 tmp_allocation.width = tmp_request.width;
1627 tmp_allocation.height = tmp_request.height;
1629 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1631 gtk_widget_realize (GTK_WIDGET (menu));
1634 gtk_menu_scroll_to (menu, menu->scroll_offset);
1636 /* if no item is selected, select the first one */
1637 if (!menu_shell->active_menu_item)
1639 gboolean touchscreen_mode;
1641 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1642 "gtk-touchscreen-mode", &touchscreen_mode,
1645 if (touchscreen_mode)
1646 gtk_menu_shell_select_first (menu_shell, TRUE);
1649 /* Once everything is set up correctly, map the toplevel window on
1652 gtk_widget_show (menu->toplevel);
1654 if (xgrab_shell == widget)
1655 popup_grab_on_window (widget->window, keyboard, pointer, activate_time); /* Should always succeed */
1657 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1659 if (parent_menu_shell)
1661 gboolean keyboard_mode;
1663 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1664 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1666 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1667 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1669 _gtk_menu_shell_update_mnemonics (menu_shell);
1674 * @menu: a #GtkMenu.
1675 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1676 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1677 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1678 * @data: (allow-none): user supplied data to be passed to @func.
1679 * @button: the mouse button which was pressed to initiate the event.
1680 * @activate_time: the time at which the activation event occurred.
1682 * Displays a menu and makes it available for selection. Applications can use
1683 * this function to display context-sensitive menus, and will typically supply
1684 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1685 * parameters. The default menu positioning function will position the menu
1686 * at the current mouse cursor position.
1688 * The @button parameter should be the mouse button pressed to initiate
1689 * the menu popup. If the menu popup was initiated by something other than
1690 * a mouse button press, such as a mouse button release or a keypress,
1691 * @button should be 0.
1693 * The @activate_time parameter is used to conflict-resolve initiation of
1694 * concurrent requests for mouse/keyboard grab requests. To function
1695 * properly, this needs to be the time stamp of the user event (such as
1696 * a mouse click or key press) that caused the initiation of the popup.
1697 * Only if no such event is available, gtk_get_current_event_time() can
1701 gtk_menu_popup (GtkMenu *menu,
1702 GtkWidget *parent_menu_shell,
1703 GtkWidget *parent_menu_item,
1704 GtkMenuPositionFunc func,
1707 guint32 activate_time)
1711 g_return_if_fail (GTK_IS_MENU (menu));
1713 device = gtk_get_current_event_device ();
1717 GdkDisplay *display;
1718 GdkDeviceManager *device_manager;
1721 display = gtk_widget_get_display (GTK_WIDGET (menu));
1722 device_manager = gdk_display_get_device_manager (display);
1723 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1725 device = devices->data;
1727 g_list_free (devices);
1730 gtk_menu_popup_for_device (menu, device,
1734 button, activate_time);
1738 gtk_menu_popdown (GtkMenu *menu)
1740 GtkMenuPrivate *private;
1741 GtkMenuShell *menu_shell;
1744 g_return_if_fail (GTK_IS_MENU (menu));
1746 menu_shell = GTK_MENU_SHELL (menu);
1747 private = gtk_menu_get_private (menu);
1749 menu_shell->parent_menu_shell = NULL;
1750 menu_shell->active = FALSE;
1751 menu_shell->ignore_enter = FALSE;
1753 private->have_position = FALSE;
1755 gtk_menu_stop_scrolling (menu);
1757 gtk_menu_stop_navigating_submenu (menu);
1759 if (menu_shell->active_menu_item)
1761 if (menu->old_active_menu_item)
1762 g_object_unref (menu->old_active_menu_item);
1763 menu->old_active_menu_item = menu_shell->active_menu_item;
1764 g_object_ref (menu->old_active_menu_item);
1767 gtk_menu_shell_deselect (menu_shell);
1769 /* The X Grab, if present, will automatically be removed when we hide
1771 gtk_widget_hide (menu->toplevel);
1772 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1774 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1778 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1780 if (GTK_BIN (menu->toplevel)->child)
1782 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1786 /* We popped up the menu from the tearoff, so we need to
1787 * release the grab - we aren't actually hiding the menu.
1789 if (menu_shell->have_xgrab && pointer)
1791 GdkDevice *keyboard;
1793 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1794 keyboard = gdk_device_get_associated_device (pointer);
1797 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1801 /* gtk_menu_popdown is called each time a menu item is selected from
1802 * a torn off menu. Only scroll back to the saved position if the
1803 * non-tearoff menu was popped down.
1805 if (!menu->tearoff_active)
1806 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1807 menu->tearoff_active = TRUE;
1810 gtk_widget_hide (GTK_WIDGET (menu));
1812 menu_shell->have_xgrab = FALSE;
1815 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1817 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1819 menu_grab_transfer_window_destroy (menu);
1823 gtk_menu_get_active (GtkMenu *menu)
1828 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1830 if (!menu->old_active_menu_item)
1833 children = GTK_MENU_SHELL (menu)->children;
1837 child = children->data;
1838 children = children->next;
1840 if (GTK_BIN (child)->child)
1845 menu->old_active_menu_item = child;
1846 if (menu->old_active_menu_item)
1847 g_object_ref (menu->old_active_menu_item);
1850 return menu->old_active_menu_item;
1854 gtk_menu_set_active (GtkMenu *menu,
1860 g_return_if_fail (GTK_IS_MENU (menu));
1862 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1865 child = tmp_list->data;
1866 if (GTK_BIN (child)->child)
1868 if (menu->old_active_menu_item)
1869 g_object_unref (menu->old_active_menu_item);
1870 menu->old_active_menu_item = child;
1871 g_object_ref (menu->old_active_menu_item);
1878 * gtk_menu_set_accel_group:
1879 * @accel_group: (allow-none):
1882 gtk_menu_set_accel_group (GtkMenu *menu,
1883 GtkAccelGroup *accel_group)
1885 g_return_if_fail (GTK_IS_MENU (menu));
1887 if (menu->accel_group != accel_group)
1889 if (menu->accel_group)
1890 g_object_unref (menu->accel_group);
1891 menu->accel_group = accel_group;
1892 if (menu->accel_group)
1893 g_object_ref (menu->accel_group);
1894 _gtk_menu_refresh_accel_paths (menu, TRUE);
1899 gtk_menu_get_accel_group (GtkMenu *menu)
1901 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1903 return menu->accel_group;
1907 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1910 /* Menu items chain here to figure whether they can activate their
1911 * accelerators. Unlike ordinary widgets, menus allow accel
1912 * activation even if invisible since that's the usual case for
1913 * submenus/popup-menus. however, the state of the attach widget
1914 * affects the "activeness" of the menu.
1916 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1919 return gtk_widget_can_activate_accel (awidget, signal_id);
1921 return gtk_widget_is_sensitive (widget);
1925 * gtk_menu_set_accel_path
1926 * @menu: a valid #GtkMenu
1927 * @accel_path: (allow-none): a valid accelerator path
1929 * Sets an accelerator path for this menu from which accelerator paths
1930 * for its immediate children, its menu items, can be constructed.
1931 * The main purpose of this function is to spare the programmer the
1932 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1933 * each menu item that should support runtime user changable accelerators.
1934 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1935 * each menu item of this menu, that contains a label describing its purpose,
1936 * automatically gets an accel path assigned. For example, a menu containing
1937 * menu items "New" and "Exit", will, after
1938 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1939 * has been called, assign its items the accel paths:
1940 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1941 * Assigning accel paths to menu items then enables the user to change
1942 * their accelerators at runtime. More details about accelerator paths
1943 * and their default setups can be found at gtk_accel_map_add_entry().
1945 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1946 * pass a static string, you can save some memory by interning it first with
1947 * g_intern_static_string().
1950 gtk_menu_set_accel_path (GtkMenu *menu,
1951 const gchar *accel_path)
1953 g_return_if_fail (GTK_IS_MENU (menu));
1955 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1957 /* FIXME: accel_path should be defined as const gchar* */
1958 menu->accel_path = (gchar*)g_intern_string (accel_path);
1959 if (menu->accel_path)
1960 _gtk_menu_refresh_accel_paths (menu, FALSE);
1964 * gtk_menu_get_accel_path
1965 * @menu: a valid #GtkMenu
1967 * Retrieves the accelerator path set on the menu.
1969 * Returns: the accelerator path set on the menu.
1974 gtk_menu_get_accel_path (GtkMenu *menu)
1976 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1978 return menu->accel_path;
1983 gboolean group_changed;
1987 refresh_accel_paths_foreach (GtkWidget *widget,
1990 AccelPropagation *prop = data;
1992 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1993 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1994 prop->menu->accel_path,
1995 prop->menu->accel_group,
1996 prop->group_changed);
2000 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2001 gboolean group_changed)
2003 g_return_if_fail (GTK_IS_MENU (menu));
2005 if (menu->accel_path && menu->accel_group)
2007 AccelPropagation prop;
2010 prop.group_changed = group_changed;
2011 gtk_container_foreach (GTK_CONTAINER (menu),
2012 refresh_accel_paths_foreach,
2018 gtk_menu_reposition (GtkMenu *menu)
2020 g_return_if_fail (GTK_IS_MENU (menu));
2022 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2023 gtk_menu_position (menu);
2027 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2030 g_return_if_fail (GTK_IS_MENU (menu));
2032 if (adjustment->value != menu->scroll_offset)
2033 gtk_menu_scroll_to (menu, adjustment->value);
2037 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2040 GdkGeometry geometry_hints;
2042 if (!menu->tearoff_window)
2045 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2047 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
2048 width += menu->tearoff_scrollbar->requisition.width;
2051 geometry_hints.min_width = width;
2052 geometry_hints.max_width = width;
2054 geometry_hints.min_height = 0;
2055 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
2057 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2060 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2064 gtk_menu_update_title (GtkMenu *menu)
2066 if (menu->tearoff_window)
2069 GtkWidget *attach_widget;
2071 title = gtk_menu_get_title (menu);
2074 attach_widget = gtk_menu_get_attach_widget (menu);
2075 if (GTK_IS_MENU_ITEM (attach_widget))
2077 GtkWidget *child = GTK_BIN (attach_widget)->child;
2078 if (GTK_IS_LABEL (child))
2079 title = gtk_label_get_text (GTK_LABEL (child));
2084 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2089 gtk_menu_get_toplevel (GtkWidget *menu)
2091 GtkWidget *attach, *toplevel;
2093 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2095 if (GTK_IS_MENU_ITEM (attach))
2096 attach = attach->parent;
2098 if (GTK_IS_MENU (attach))
2099 return gtk_menu_get_toplevel (attach);
2100 else if (GTK_IS_WIDGET (attach))
2102 toplevel = gtk_widget_get_toplevel (attach);
2103 if (gtk_widget_is_toplevel (toplevel))
2111 tearoff_window_destroyed (GtkWidget *widget,
2114 gtk_menu_set_tearoff_state (menu, FALSE);
2118 gtk_menu_set_tearoff_state (GtkMenu *menu,
2123 g_return_if_fail (GTK_IS_MENU (menu));
2125 if (menu->torn_off != torn_off)
2127 menu->torn_off = torn_off;
2128 menu->tearoff_active = torn_off;
2132 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2133 gtk_menu_popdown (menu);
2135 if (!menu->tearoff_window)
2137 GtkWidget *toplevel;
2139 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2140 "type", GTK_WINDOW_TOPLEVEL,
2141 "screen", gtk_widget_get_screen (menu->toplevel),
2142 "app-paintable", TRUE,
2145 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2146 GDK_WINDOW_TYPE_HINT_MENU);
2147 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2148 g_signal_connect (menu->tearoff_window, "destroy",
2149 G_CALLBACK (tearoff_window_destroyed), menu);
2150 g_signal_connect (menu->tearoff_window, "event",
2151 G_CALLBACK (gtk_menu_window_event), menu);
2153 gtk_menu_update_title (menu);
2155 gtk_widget_realize (menu->tearoff_window);
2157 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2158 if (toplevel != NULL)
2159 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2160 GTK_WINDOW (toplevel));
2162 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2163 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2165 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2166 menu->tearoff_adjustment =
2167 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2169 GTK_WIDGET (menu)->requisition.height,
2173 g_object_connect (menu->tearoff_adjustment,
2174 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2176 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2178 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2179 menu->tearoff_scrollbar,
2182 if (menu->tearoff_adjustment->upper > height)
2183 gtk_widget_show (menu->tearoff_scrollbar);
2185 gtk_widget_show (menu->tearoff_hbox);
2188 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2190 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
2192 /* Update menu->requisition
2194 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2196 gtk_menu_set_tearoff_hints (menu, width);
2198 gtk_widget_realize (menu->tearoff_window);
2199 gtk_menu_position (menu);
2201 gtk_widget_show (GTK_WIDGET (menu));
2202 gtk_widget_show (menu->tearoff_window);
2204 gtk_menu_scroll_to (menu, 0);
2209 gtk_widget_hide (GTK_WIDGET (menu));
2210 gtk_widget_hide (menu->tearoff_window);
2211 if (GTK_IS_CONTAINER (menu->toplevel))
2212 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2213 gtk_widget_destroy (menu->tearoff_window);
2215 menu->tearoff_window = NULL;
2216 menu->tearoff_hbox = NULL;
2217 menu->tearoff_scrollbar = NULL;
2218 menu->tearoff_adjustment = NULL;
2221 g_object_notify (G_OBJECT (menu), "tearoff-state");
2226 * gtk_menu_get_tearoff_state:
2229 * Returns whether the menu is torn off. See
2230 * gtk_menu_set_tearoff_state ().
2232 * Return value: %TRUE if the menu is currently torn off.
2235 gtk_menu_get_tearoff_state (GtkMenu *menu)
2237 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2239 return menu->torn_off;
2243 * gtk_menu_set_title:
2245 * @title: a string containing the title for the menu.
2247 * Sets the title string for the menu. The title is displayed when the menu
2248 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2249 * attached to a parent menu item, and if so it will try to use the same text as
2250 * that menu item's label.
2253 gtk_menu_set_title (GtkMenu *menu,
2256 GtkMenuPrivate *priv;
2259 g_return_if_fail (GTK_IS_MENU (menu));
2261 priv = gtk_menu_get_private (menu);
2263 old_title = priv->title;
2264 priv->title = g_strdup (title);
2267 gtk_menu_update_title (menu);
2268 g_object_notify (G_OBJECT (menu), "tearoff-title");
2272 * gtk_menu_get_title:
2275 * Returns the title of the menu. See gtk_menu_set_title().
2277 * Return value: the title of the menu, or %NULL if the menu has no
2278 * title set on it. This string is owned by the widget and should
2279 * not be modified or freed.
2281 G_CONST_RETURN gchar *
2282 gtk_menu_get_title (GtkMenu *menu)
2284 GtkMenuPrivate *priv;
2286 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2288 priv = gtk_menu_get_private (menu);
2294 gtk_menu_reorder_child (GtkMenu *menu,
2298 GtkMenuShell *menu_shell;
2300 g_return_if_fail (GTK_IS_MENU (menu));
2301 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2303 menu_shell = GTK_MENU_SHELL (menu);
2305 if (g_list_find (menu_shell->children, child))
2307 menu_shell->children = g_list_remove (menu_shell->children, child);
2308 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2310 menu_queue_resize (menu);
2315 gtk_menu_style_set (GtkWidget *widget,
2316 GtkStyle *previous_style)
2318 if (gtk_widget_get_realized (widget))
2320 GtkMenu *menu = GTK_MENU (widget);
2322 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2323 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2324 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2329 get_arrows_border (GtkMenu *menu,
2332 guint scroll_arrow_height;
2333 GtkArrowPlacement arrow_placement;
2335 gtk_widget_style_get (GTK_WIDGET (menu),
2336 "scroll-arrow-vlength", &scroll_arrow_height,
2337 "arrow_placement", &arrow_placement,
2340 switch (arrow_placement)
2342 case GTK_ARROWS_BOTH:
2343 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2344 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2347 case GTK_ARROWS_START:
2348 border->top = (menu->upper_arrow_visible ||
2349 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2353 case GTK_ARROWS_END:
2355 border->bottom = (menu->upper_arrow_visible ||
2356 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2360 border->left = border->right = 0;
2364 gtk_menu_realize (GtkWidget *widget)
2366 GdkWindowAttr attributes;
2367 gint attributes_mask;
2372 guint vertical_padding;
2373 guint horizontal_padding;
2374 GtkBorder arrow_border;
2376 g_return_if_fail (GTK_IS_MENU (widget));
2378 menu = GTK_MENU (widget);
2380 gtk_widget_set_realized (widget, TRUE);
2382 attributes.window_type = GDK_WINDOW_CHILD;
2383 attributes.x = widget->allocation.x;
2384 attributes.y = widget->allocation.y;
2385 attributes.width = widget->allocation.width;
2386 attributes.height = widget->allocation.height;
2387 attributes.wclass = GDK_INPUT_OUTPUT;
2388 attributes.visual = gtk_widget_get_visual (widget);
2389 attributes.colormap = gtk_widget_get_colormap (widget);
2391 attributes.event_mask = gtk_widget_get_events (widget);
2393 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2394 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2396 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2397 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2398 gdk_window_set_user_data (widget->window, widget);
2400 border_width = GTK_CONTAINER (widget)->border_width;
2402 gtk_widget_style_get (GTK_WIDGET (menu),
2403 "vertical-padding", &vertical_padding,
2404 "horizontal-padding", &horizontal_padding,
2407 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2408 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2409 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2410 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2412 get_arrows_border (menu, &arrow_border);
2413 attributes.y += arrow_border.top;
2414 attributes.height -= arrow_border.top;
2415 attributes.height -= arrow_border.bottom;
2417 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2418 gdk_window_set_user_data (menu->view_window, menu);
2422 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2423 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2425 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2426 gdk_window_set_user_data (menu->bin_window, menu);
2428 children = GTK_MENU_SHELL (menu)->children;
2431 child = children->data;
2432 children = children->next;
2434 gtk_widget_set_parent_window (child, menu->bin_window);
2437 widget->style = gtk_style_attach (widget->style, widget->window);
2438 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2439 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2440 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2442 if (GTK_MENU_SHELL (widget)->active_menu_item)
2443 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2444 GTK_MENU_SHELL (widget)->active_menu_item);
2446 gdk_window_show (menu->bin_window);
2447 gdk_window_show (menu->view_window);
2451 gtk_menu_focus (GtkWidget *widget,
2452 GtkDirectionType direction)
2455 * A menu or its menu items cannot have focus
2460 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2463 menu_grab_transfer_window_get (GtkMenu *menu)
2465 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2468 GdkWindowAttr attributes;
2469 gint attributes_mask;
2471 attributes.x = -100;
2472 attributes.y = -100;
2473 attributes.width = 10;
2474 attributes.height = 10;
2475 attributes.window_type = GDK_WINDOW_TEMP;
2476 attributes.wclass = GDK_INPUT_ONLY;
2477 attributes.override_redirect = TRUE;
2478 attributes.event_mask = 0;
2480 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2482 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2483 &attributes, attributes_mask);
2484 gdk_window_set_user_data (window, menu);
2486 gdk_window_show (window);
2488 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2495 menu_grab_transfer_window_destroy (GtkMenu *menu)
2497 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2500 gdk_window_set_user_data (window, NULL);
2501 gdk_window_destroy (window);
2502 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2507 gtk_menu_unrealize (GtkWidget *widget)
2509 GtkMenu *menu = GTK_MENU (widget);
2511 menu_grab_transfer_window_destroy (menu);
2513 gdk_window_set_user_data (menu->view_window, NULL);
2514 gdk_window_destroy (menu->view_window);
2515 menu->view_window = NULL;
2517 gdk_window_set_user_data (menu->bin_window, NULL);
2518 gdk_window_destroy (menu->bin_window);
2519 menu->bin_window = NULL;
2521 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2525 gtk_menu_size_request (GtkWidget *widget,
2526 GtkRequisition *requisition)
2530 GtkMenuShell *menu_shell;
2533 guint max_toggle_size;
2534 guint max_accel_width;
2535 guint vertical_padding;
2536 guint horizontal_padding;
2537 GtkRequisition child_requisition;
2538 GtkMenuPrivate *priv;
2540 g_return_if_fail (GTK_IS_MENU (widget));
2541 g_return_if_fail (requisition != NULL);
2543 menu = GTK_MENU (widget);
2544 menu_shell = GTK_MENU_SHELL (widget);
2545 priv = gtk_menu_get_private (menu);
2547 requisition->width = 0;
2548 requisition->height = 0;
2550 max_toggle_size = 0;
2551 max_accel_width = 0;
2553 g_free (priv->heights);
2554 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2555 priv->heights_length = gtk_menu_get_n_rows (menu);
2557 children = menu_shell->children;
2564 child = children->data;
2565 children = children->next;
2567 if (! gtk_widget_get_visible (child))
2570 get_effective_child_attach (child, &l, &r, &t, &b);
2572 /* It's important to size_request the child
2573 * before doing the toggle size request, in
2574 * case the toggle size request depends on the size
2575 * request of a child of the child (e.g. for ImageMenuItem)
2578 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2579 gtk_widget_size_request (child, &child_requisition);
2581 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2582 max_toggle_size = MAX (max_toggle_size, toggle_size);
2583 max_accel_width = MAX (max_accel_width,
2584 GTK_MENU_ITEM (child)->accelerator_width);
2586 part = child_requisition.width / (r - l);
2587 requisition->width = MAX (requisition->width, part);
2589 part = MAX (child_requisition.height, toggle_size) / (b - t);
2590 priv->heights[t] = MAX (priv->heights[t], part);
2593 /* If the menu doesn't include any images or check items
2594 * reserve the space so that all menus are consistent.
2595 * We only do this for 'ordinary' menus, not for combobox
2596 * menus or multi-column menus
2598 if (max_toggle_size == 0 &&
2599 gtk_menu_get_n_columns (menu) == 1 &&
2600 !priv->no_toggle_size)
2602 guint toggle_spacing;
2603 guint indicator_size;
2605 gtk_style_get (widget->style,
2606 GTK_TYPE_CHECK_MENU_ITEM,
2607 "toggle-spacing", &toggle_spacing,
2608 "indicator-size", &indicator_size,
2611 max_toggle_size = indicator_size + toggle_spacing;
2614 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2615 requisition->height += priv->heights[i];
2617 requisition->width += 2 * max_toggle_size + max_accel_width;
2618 requisition->width *= gtk_menu_get_n_columns (menu);
2620 gtk_widget_style_get (GTK_WIDGET (menu),
2621 "vertical-padding", &vertical_padding,
2622 "horizontal-padding", &horizontal_padding,
2625 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2626 widget->style->xthickness) * 2;
2627 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2628 widget->style->ythickness) * 2;
2630 menu->toggle_size = max_toggle_size;
2632 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2634 if (menu->tearoff_active)
2635 gtk_menu_set_tearoff_hints (menu, requisition->width);
2639 gtk_menu_size_allocate (GtkWidget *widget,
2640 GtkAllocation *allocation)
2643 GtkMenuShell *menu_shell;
2645 GtkAllocation child_allocation;
2646 GtkRequisition child_requisition;
2647 GtkMenuPrivate *priv;
2651 guint vertical_padding;
2652 guint horizontal_padding;
2654 g_return_if_fail (GTK_IS_MENU (widget));
2655 g_return_if_fail (allocation != NULL);
2657 menu = GTK_MENU (widget);
2658 menu_shell = GTK_MENU_SHELL (widget);
2659 priv = gtk_menu_get_private (menu);
2661 widget->allocation = *allocation;
2662 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2664 gtk_widget_style_get (GTK_WIDGET (menu),
2665 "vertical-padding", &vertical_padding,
2666 "horizontal-padding", &horizontal_padding,
2669 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2670 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2672 width = MAX (1, allocation->width - x * 2);
2673 height = MAX (1, allocation->height - y * 2);
2675 child_requisition.width -= x * 2;
2676 child_requisition.height -= y * 2;
2678 if (menu_shell->active)
2679 gtk_menu_scroll_to (menu, menu->scroll_offset);
2681 if (!menu->tearoff_active)
2683 GtkBorder arrow_border;
2685 get_arrows_border (menu, &arrow_border);
2686 y += arrow_border.top;
2687 height -= arrow_border.top;
2688 height -= arrow_border.bottom;
2691 if (gtk_widget_get_realized (widget))
2693 gdk_window_move_resize (widget->window,
2694 allocation->x, allocation->y,
2695 allocation->width, allocation->height);
2697 gdk_window_move_resize (menu->view_window,
2704 if (menu_shell->children)
2706 gint base_width = width / gtk_menu_get_n_columns (menu);
2708 children = menu_shell->children;
2711 child = children->data;
2712 children = children->next;
2714 if (gtk_widget_get_visible (child))
2719 get_effective_child_attach (child, &l, &r, &t, &b);
2721 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2724 tmp = gtk_menu_get_n_columns (menu) - l;
2725 l = gtk_menu_get_n_columns (menu) - r;
2729 child_allocation.width = (r - l) * base_width;
2730 child_allocation.height = 0;
2731 child_allocation.x = l * base_width;
2732 child_allocation.y = 0;
2734 for (i = 0; i < b; i++)
2737 child_allocation.y += priv->heights[i];
2739 child_allocation.height += priv->heights[i];
2742 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2745 gtk_widget_size_allocate (child, &child_allocation);
2746 gtk_widget_queue_draw (child);
2750 /* Resize the item window */
2751 if (gtk_widget_get_realized (widget))
2757 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2758 height += priv->heights[i];
2760 width = gtk_menu_get_n_columns (menu) * base_width;
2761 gdk_window_resize (menu->bin_window, width, height);
2764 if (menu->tearoff_active)
2766 if (allocation->height >= widget->requisition.height)
2768 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2770 gtk_widget_hide (menu->tearoff_scrollbar);
2771 gtk_menu_set_tearoff_hints (menu, allocation->width);
2773 gtk_menu_scroll_to (menu, 0);
2778 menu->tearoff_adjustment->upper = widget->requisition.height;
2779 menu->tearoff_adjustment->page_size = allocation->height;
2781 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2782 menu->tearoff_adjustment->upper)
2785 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2788 gtk_menu_scroll_to (menu, value);
2791 gtk_adjustment_changed (menu->tearoff_adjustment);
2793 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2795 gtk_widget_show (menu->tearoff_scrollbar);
2796 gtk_menu_set_tearoff_hints (menu, allocation->width);
2804 get_arrows_visible_area (GtkMenu *menu,
2805 GdkRectangle *border,
2806 GdkRectangle *upper,
2807 GdkRectangle *lower,
2810 GtkWidget *widget = GTK_WIDGET (menu);
2811 guint vertical_padding;
2812 guint horizontal_padding;
2813 gint scroll_arrow_height;
2814 GtkArrowPlacement arrow_placement;
2816 gtk_widget_style_get (widget,
2817 "vertical-padding", &vertical_padding,
2818 "horizontal-padding", &horizontal_padding,
2819 "scroll-arrow-vlength", &scroll_arrow_height,
2820 "arrow-placement", &arrow_placement,
2823 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2824 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2825 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2827 switch (arrow_placement)
2829 case GTK_ARROWS_BOTH:
2830 upper->x = border->x;
2831 upper->y = border->y;
2832 upper->width = border->width - 2 * border->x;
2833 upper->height = scroll_arrow_height;
2835 lower->x = border->x;
2836 lower->y = border->height - border->y - scroll_arrow_height;
2837 lower->width = border->width - 2 * border->x;
2838 lower->height = scroll_arrow_height;
2841 case GTK_ARROWS_START:
2842 upper->x = border->x;
2843 upper->y = border->y;
2844 upper->width = (border->width - 2 * border->x) / 2;
2845 upper->height = scroll_arrow_height;
2847 lower->x = border->x + upper->width;
2848 lower->y = border->y;
2849 lower->width = (border->width - 2 * border->x) / 2;
2850 lower->height = scroll_arrow_height;
2853 case GTK_ARROWS_END:
2854 upper->x = border->x;
2855 upper->y = border->height - border->y - scroll_arrow_height;
2856 upper->width = (border->width - 2 * border->x) / 2;
2857 upper->height = scroll_arrow_height;
2859 lower->x = border->x + upper->width;
2860 lower->y = border->height - border->y - scroll_arrow_height;
2861 lower->width = (border->width - 2 * border->x) / 2;
2862 lower->height = scroll_arrow_height;
2866 g_assert_not_reached();
2867 upper->x = upper->y = upper->width = upper->height = 0;
2868 lower->x = lower->y = lower->width = lower->height = 0;
2871 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2875 gtk_menu_paint (GtkWidget *widget,
2876 GdkEventExpose *event)
2879 GtkMenuPrivate *priv;
2880 GdkRectangle border;
2885 g_return_if_fail (GTK_IS_MENU (widget));
2887 menu = GTK_MENU (widget);
2888 priv = gtk_menu_get_private (menu);
2890 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2892 if (event->window == widget->window)
2894 gfloat arrow_scaling;
2897 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2898 arrow_size = arrow_scaling * arrow_space;
2900 gtk_paint_box (widget->style,
2904 &event->area, widget, "menu",
2907 if (menu->upper_arrow_visible && !menu->tearoff_active)
2909 gtk_paint_box (widget->style,
2911 priv->upper_arrow_state,
2913 &event->area, widget, "menu_scroll_arrow_up",
2919 gtk_paint_arrow (widget->style,
2921 priv->upper_arrow_state,
2923 &event->area, widget, "menu_scroll_arrow_up",
2926 upper.x + (upper.width - arrow_size) / 2,
2927 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2928 arrow_size, arrow_size);
2931 if (menu->lower_arrow_visible && !menu->tearoff_active)
2933 gtk_paint_box (widget->style,
2935 priv->lower_arrow_state,
2937 &event->area, widget, "menu_scroll_arrow_down",
2943 gtk_paint_arrow (widget->style,
2945 priv->lower_arrow_state,
2947 &event->area, widget, "menu_scroll_arrow_down",
2950 lower.x + (lower.width - arrow_size) / 2,
2951 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2952 arrow_size, arrow_size);
2955 else if (event->window == menu->bin_window)
2957 gint y = -border.y + menu->scroll_offset;
2959 if (!menu->tearoff_active)
2961 GtkBorder arrow_border;
2963 get_arrows_border (menu, &arrow_border);
2964 y -= arrow_border.top;
2967 gtk_paint_box (widget->style,
2971 &event->area, widget, "menu",
2973 border.width, border.height);
2978 gtk_menu_expose (GtkWidget *widget,
2979 GdkEventExpose *event)
2981 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2982 g_return_val_if_fail (event != NULL, FALSE);
2984 if (gtk_widget_is_drawable (widget))
2986 gtk_menu_paint (widget, event);
2988 GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
2995 gtk_menu_show (GtkWidget *widget)
2997 GtkMenu *menu = GTK_MENU (widget);
2999 _gtk_menu_refresh_accel_paths (menu, FALSE);
3001 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3005 gtk_menu_button_scroll (GtkMenu *menu,
3006 GdkEventButton *event)
3008 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3010 gboolean touchscreen_mode;
3012 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3013 "gtk-touchscreen-mode", &touchscreen_mode,
3016 if (touchscreen_mode)
3017 gtk_menu_handle_scrolling (menu,
3018 event->x_root, event->y_root,
3019 event->type == GDK_BUTTON_PRESS,
3029 pointer_in_menu_window (GtkWidget *widget,
3033 GtkMenu *menu = GTK_MENU (widget);
3035 if (gtk_widget_get_mapped (menu->toplevel))
3037 GtkMenuShell *menu_shell;
3038 gint window_x, window_y;
3040 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
3042 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
3043 y_root >= window_y && y_root < window_y + widget->allocation.height)
3046 menu_shell = GTK_MENU_SHELL (widget);
3048 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3049 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3057 gtk_menu_button_press (GtkWidget *widget,
3058 GdkEventButton *event)
3060 if (event->type != GDK_BUTTON_PRESS)
3063 /* Don't pass down to menu shell for presses over scroll arrows
3065 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3068 /* Don't pass down to menu shell if a non-menuitem part of the menu
3069 * was clicked. The check for the event_widget being a GtkMenuShell
3070 * works because we have the pointer grabbed on menu_shell->window
3071 * with owner_events=TRUE, so all events that are either outside
3072 * the menu or on its border are delivered relative to
3073 * menu_shell->window.
3075 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3076 pointer_in_menu_window (widget, event->x_root, event->y_root))
3079 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3083 gtk_menu_button_release (GtkWidget *widget,
3084 GdkEventButton *event)
3086 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3088 if (priv->ignore_button_release)
3090 priv->ignore_button_release = FALSE;
3094 if (event->type != GDK_BUTTON_RELEASE)
3097 /* Don't pass down to menu shell for releases over scroll arrows
3099 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3102 /* Don't pass down to menu shell if a non-menuitem part of the menu
3103 * was clicked (see comment in button_press()).
3105 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3106 pointer_in_menu_window (widget, event->x_root, event->y_root))
3108 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3109 * bail out early here so it is in a consistent state for the
3110 * next button_press/button_release in GtkMenuShell.
3113 if (GTK_MENU_SHELL (widget)->active)
3114 GTK_MENU_SHELL (widget)->button = 0;
3119 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3122 static const gchar *
3123 get_accel_path (GtkWidget *menu_item,
3128 GClosure *accel_closure;
3129 GtkAccelGroup *accel_group;
3131 path = _gtk_widget_get_accel_path (menu_item, locked);
3134 path = GTK_MENU_ITEM (menu_item)->accel_path;
3140 label = GTK_BIN (menu_item)->child;
3142 if (GTK_IS_ACCEL_LABEL (label))
3144 g_object_get (label,
3145 "accel-closure", &accel_closure,
3149 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3151 *locked = gtk_accel_group_get_is_locked (accel_group);
3161 gtk_menu_key_press (GtkWidget *widget,
3164 GtkMenuShell *menu_shell;
3166 gboolean delete = FALSE;
3167 gboolean can_change_accels;
3168 gchar *accel = NULL;
3169 guint accel_key, accel_mods;
3170 GdkModifierType consumed_modifiers;
3171 GdkDisplay *display;
3173 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3174 g_return_val_if_fail (event != NULL, FALSE);
3176 menu_shell = GTK_MENU_SHELL (widget);
3177 menu = GTK_MENU (widget);
3179 gtk_menu_stop_navigating_submenu (menu);
3181 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3184 display = gtk_widget_get_display (widget);
3186 g_object_get (gtk_widget_get_settings (widget),
3187 "gtk-menu-bar-accel", &accel,
3188 "gtk-can-change-accels", &can_change_accels,
3191 if (accel && *accel)
3194 GdkModifierType mods = 0;
3196 gtk_accelerator_parse (accel, &keyval, &mods);
3199 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3201 /* FIXME this is wrong, needs to be in the global accel resolution
3202 * thing, to properly consider i18n etc., but that probably requires
3203 * AccelGroup changes etc.
3205 if (event->keyval == keyval && (mods & event->state) == mods)
3207 gtk_menu_shell_cancel (menu_shell);
3215 switch (event->keyval)
3226 /* Figure out what modifiers went into determining the key symbol */
3227 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3228 event->hardware_keycode, event->state, event->group,
3229 NULL, NULL, NULL, &consumed_modifiers);
3231 accel_key = gdk_keyval_to_lower (event->keyval);
3232 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3234 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3235 * We re-upper case when we match against the keyval, but display and save in caseless form.
3237 if (accel_key != event->keyval)
3238 accel_mods |= GDK_SHIFT_MASK;
3240 /* Modify the accelerators */
3241 if (can_change_accels &&
3242 menu_shell->active_menu_item &&
3243 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
3244 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3245 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3247 GtkWidget *menu_item = menu_shell->active_menu_item;
3248 gboolean locked, replace_accels = TRUE;
3251 path = get_accel_path (menu_item, &locked);
3252 if (!path || locked)
3254 /* can't change accelerators on menu_items without paths
3255 * (basically, those items are accelerator-locked).
3257 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3258 gtk_widget_error_bell (widget);
3264 /* For the keys that act to delete the current setting, we delete
3265 * the current setting if there is one, otherwise, we set the
3266 * key as the accelerator.
3272 if (gtk_accel_map_lookup_entry (path, &key) &&
3273 (key.accel_key || key.accel_mods))
3279 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3283 /* we failed, probably because this key is in use and
3286 /* g_print("failed to change\n"); */
3287 gtk_widget_error_bell (widget);
3296 check_threshold (GtkWidget *widget,
3305 ABS (start_x - x) > THRESHOLD ||
3306 ABS (start_y - y) > THRESHOLD;
3310 definitely_within_item (GtkWidget *widget,
3314 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3317 gdk_drawable_get_size (window, &w, &h);
3320 check_threshold (widget, 0, 0, x, y) &&
3321 check_threshold (widget, w - 1, 0, x, y) &&
3322 check_threshold (widget, w - 1, h - 1, x, y) &&
3323 check_threshold (widget, 0, h - 1, x, y);
3327 gtk_menu_motion_notify (GtkWidget *widget,
3328 GdkEventMotion *event)
3330 GtkWidget *menu_item;
3332 GtkMenuShell *menu_shell;
3334 gboolean need_enter;
3336 if (GTK_IS_MENU (widget))
3338 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3340 if (priv->ignore_button_release)
3341 priv->ignore_button_release = FALSE;
3343 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3347 /* We received the event for one of two reasons:
3349 * a) We are the active menu, and did gtk_grab_add()
3350 * b) The widget is a child of ours, and the event was propagated
3352 * Since for computation of navigation regions, we want the menu which
3353 * is the parent of the menu item, for a), we need to find that menu,
3354 * which may be different from 'widget'.
3356 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3357 if (!GTK_IS_MENU_ITEM (menu_item) ||
3358 !GTK_IS_MENU (menu_item->parent))
3361 menu_shell = GTK_MENU_SHELL (menu_item->parent);
3362 menu = GTK_MENU (menu_shell);
3364 if (definitely_within_item (menu_item, event->x, event->y))
3365 menu_shell->activate_time = 0;
3367 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
3369 /* Check to see if we are within an active submenu's navigation region
3371 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3374 /* Make sure we pop down if we enter a non-selectable menu item, so we
3375 * don't show a submenu when the cursor is outside the stay-up triangle.
3377 if (!_gtk_menu_item_is_selectable (menu_item))
3379 /* We really want to deselect, but this gives the menushell code
3380 * a chance to do some bookkeeping about the menuitem.
3382 gtk_menu_shell_select_item (menu_shell, menu_item);
3388 /* The menu is now sensitive to enter events on its items, but
3389 * was previously sensitive. So we fake an enter event.
3393 menu_shell->ignore_enter = FALSE;
3395 gdk_drawable_get_size (event->window, &width, &height);
3396 if (event->x >= 0 && event->x < width &&
3397 event->y >= 0 && event->y < height)
3399 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3402 send_event->crossing.window = g_object_ref (event->window);
3403 send_event->crossing.time = event->time;
3404 send_event->crossing.send_event = TRUE;
3405 send_event->crossing.x_root = event->x_root;
3406 send_event->crossing.y_root = event->y_root;
3407 send_event->crossing.x = event->x;
3408 send_event->crossing.y = event->y;
3409 send_event->crossing.state = event->state;
3410 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3412 /* We send the event to 'widget', the currently active menu,
3413 * instead of 'menu', the menu that the pointer is in. This
3414 * will ensure that the event will be ignored unless the
3415 * menuitem is a child of the active menu or some parent
3416 * menu of the active menu.
3418 result = gtk_widget_event (widget, send_event);
3419 gdk_event_free (send_event);
3429 get_double_arrows (GtkMenu *menu)
3431 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3432 gboolean double_arrows;
3433 GtkArrowPlacement arrow_placement;
3435 gtk_widget_style_get (GTK_WIDGET (menu),
3436 "double-arrows", &double_arrows,
3437 "arrow-placement", &arrow_placement,
3440 if (arrow_placement != GTK_ARROWS_BOTH)
3443 return double_arrows || (priv->initially_pushed_in &&
3444 menu->scroll_offset != 0);
3448 gtk_menu_scroll_by (GtkMenu *menu,
3453 gint view_width, view_height;
3454 gboolean double_arrows;
3455 GtkBorder arrow_border;
3457 widget = GTK_WIDGET (menu);
3458 offset = menu->scroll_offset + step;
3460 get_arrows_border (menu, &arrow_border);
3462 double_arrows = get_double_arrows (menu);
3464 /* If we scroll upward and the non-visible top part
3465 * is smaller than the scroll arrow it would be
3466 * pretty stupid to show the arrow and taking more
3467 * screen space than just scrolling to the top.
3470 if ((step < 0) && (offset < arrow_border.top))
3473 /* Don't scroll over the top if we weren't before: */
3474 if ((menu->scroll_offset >= 0) && (offset < 0))
3477 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3479 if (menu->scroll_offset == 0 &&
3480 view_height >= widget->requisition.height)
3483 /* Don't scroll past the bottom if we weren't before: */
3484 if (menu->scroll_offset > 0)
3485 view_height -= arrow_border.top;
3487 /* When both arrows are always shown, reduce
3488 * view height even more.
3491 view_height -= arrow_border.bottom;
3493 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3494 (offset + view_height > widget->requisition.height))
3495 offset = widget->requisition.height - view_height;
3497 if (offset != menu->scroll_offset)
3498 gtk_menu_scroll_to (menu, offset);
3502 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3503 gboolean touchscreen_mode)
3505 gboolean upper_visible;
3506 gboolean lower_visible;
3508 upper_visible = menu->upper_arrow_visible;
3509 lower_visible = menu->lower_arrow_visible;
3511 gtk_menu_scroll_by (menu, menu->scroll_step);
3513 if (touchscreen_mode &&
3514 (upper_visible != menu->upper_arrow_visible ||
3515 lower_visible != menu->lower_arrow_visible))
3517 /* We are about to hide a scroll arrow while the mouse is pressed,
3518 * this would cause the uncovered menu item to be activated on button
3519 * release. Therefore we need to ignore button release here
3521 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3522 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3527 gtk_menu_scroll_timeout (gpointer data)
3530 gboolean touchscreen_mode;
3532 menu = GTK_MENU (data);
3534 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3535 "gtk-touchscreen-mode", &touchscreen_mode,
3538 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3544 gtk_menu_scroll_timeout_initial (gpointer data)
3548 gboolean touchscreen_mode;
3550 menu = GTK_MENU (data);
3552 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3553 "gtk-timeout-repeat", &timeout,
3554 "gtk-touchscreen-mode", &touchscreen_mode,
3557 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3559 gtk_menu_remove_scroll_timeout (menu);
3561 menu->timeout_id = gdk_threads_add_timeout (timeout,
3562 gtk_menu_scroll_timeout,
3569 gtk_menu_start_scrolling (GtkMenu *menu)
3572 gboolean touchscreen_mode;
3574 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3575 "gtk-timeout-repeat", &timeout,
3576 "gtk-touchscreen-mode", &touchscreen_mode,
3579 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3581 menu->timeout_id = gdk_threads_add_timeout (timeout,
3582 gtk_menu_scroll_timeout_initial,
3587 gtk_menu_scroll (GtkWidget *widget,
3588 GdkEventScroll *event)
3590 GtkMenu *menu = GTK_MENU (widget);
3592 switch (event->direction)
3594 case GDK_SCROLL_RIGHT:
3595 case GDK_SCROLL_DOWN:
3596 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3598 case GDK_SCROLL_LEFT:
3600 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3608 get_arrows_sensitive_area (GtkMenu *menu,
3609 GdkRectangle *upper,
3610 GdkRectangle *lower)
3614 guint vertical_padding;
3616 gint scroll_arrow_height;
3617 GtkArrowPlacement arrow_placement;
3619 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3621 gtk_widget_style_get (GTK_WIDGET (menu),
3622 "vertical-padding", &vertical_padding,
3623 "scroll-arrow-vlength", &scroll_arrow_height,
3624 "arrow-placement", &arrow_placement,
3627 border = GTK_CONTAINER (menu)->border_width +
3628 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3630 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3632 switch (arrow_placement)
3634 case GTK_ARROWS_BOTH:
3639 upper->width = width;
3640 upper->height = scroll_arrow_height + border;
3646 lower->y = win_y + height - border - scroll_arrow_height;
3647 lower->width = width;
3648 lower->height = scroll_arrow_height + border;
3652 case GTK_ARROWS_START:
3657 upper->width = width / 2;
3658 upper->height = scroll_arrow_height + border;
3663 lower->x = win_x + width / 2;
3665 lower->width = width / 2;
3666 lower->height = scroll_arrow_height + border;
3670 case GTK_ARROWS_END:
3674 upper->y = win_y + height - border - scroll_arrow_height;
3675 upper->width = width / 2;
3676 upper->height = scroll_arrow_height + border;
3681 lower->x = win_x + width / 2;
3682 lower->y = win_y + height - border - scroll_arrow_height;
3683 lower->width = width / 2;
3684 lower->height = scroll_arrow_height + border;
3692 gtk_menu_handle_scrolling (GtkMenu *menu,
3698 GtkMenuShell *menu_shell;
3699 GtkMenuPrivate *priv;
3702 gboolean scroll_fast = FALSE;
3704 gboolean touchscreen_mode;
3706 priv = gtk_menu_get_private (menu);
3708 menu_shell = GTK_MENU_SHELL (menu);
3710 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3711 "gtk-touchscreen-mode", &touchscreen_mode,
3714 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3718 /* upper arrow handling */
3720 get_arrows_sensitive_area (menu, &rect, NULL);
3723 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3724 (x >= rect.x) && (x < rect.x + rect.width) &&
3725 (y >= rect.y) && (y < rect.y + rect.height))
3730 if (touchscreen_mode)
3731 menu->upper_arrow_prelight = in_arrow;
3733 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3735 gboolean arrow_pressed = FALSE;
3737 if (menu->upper_arrow_visible && !menu->tearoff_active)
3739 if (touchscreen_mode)
3741 if (enter && menu->upper_arrow_prelight)
3743 if (menu->timeout_id == 0)
3745 /* Deselect the active item so that
3746 * any submenus are popped down
3748 gtk_menu_shell_deselect (menu_shell);
3750 gtk_menu_remove_scroll_timeout (menu);
3751 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3755 /* Only do stuff on click. */
3756 gtk_menu_start_scrolling (menu);
3757 arrow_pressed = TRUE;
3762 arrow_pressed = TRUE;
3767 gtk_menu_stop_scrolling (menu);
3770 else /* !touchscreen_mode */
3772 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3774 if (enter && in_arrow &&
3775 (!menu->upper_arrow_prelight ||
3776 menu->scroll_fast != scroll_fast))
3778 menu->upper_arrow_prelight = TRUE;
3779 menu->scroll_fast = scroll_fast;
3781 /* Deselect the active item so that
3782 * any submenus are popped down
3784 gtk_menu_shell_deselect (menu_shell);
3786 gtk_menu_remove_scroll_timeout (menu);
3787 menu->scroll_step = scroll_fast ?
3788 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3791 gdk_threads_add_timeout (scroll_fast ?
3792 MENU_SCROLL_TIMEOUT2 :
3793 MENU_SCROLL_TIMEOUT1,
3794 gtk_menu_scroll_timeout, menu);
3796 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3798 gtk_menu_stop_scrolling (menu);
3803 /* gtk_menu_start_scrolling() might have hit the top of the
3804 * menu, so check if the button isn't insensitive before
3805 * changing it to something else.
3807 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3809 GtkStateType arrow_state = GTK_STATE_NORMAL;
3812 arrow_state = GTK_STATE_ACTIVE;
3813 else if (menu->upper_arrow_prelight)
3814 arrow_state = GTK_STATE_PRELIGHT;
3816 if (arrow_state != priv->upper_arrow_state)
3818 priv->upper_arrow_state = arrow_state;
3820 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3826 /* lower arrow handling */
3828 get_arrows_sensitive_area (menu, NULL, &rect);
3831 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3832 (x >= rect.x) && (x < rect.x + rect.width) &&
3833 (y >= rect.y) && (y < rect.y + rect.height))
3838 if (touchscreen_mode)
3839 menu->lower_arrow_prelight = in_arrow;
3841 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3843 gboolean arrow_pressed = FALSE;
3845 if (menu->lower_arrow_visible && !menu->tearoff_active)
3847 if (touchscreen_mode)
3849 if (enter && menu->lower_arrow_prelight)
3851 if (menu->timeout_id == 0)
3853 /* Deselect the active item so that
3854 * any submenus are popped down
3856 gtk_menu_shell_deselect (menu_shell);
3858 gtk_menu_remove_scroll_timeout (menu);
3859 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3863 /* Only do stuff on click. */
3864 gtk_menu_start_scrolling (menu);
3865 arrow_pressed = TRUE;
3870 arrow_pressed = TRUE;
3875 gtk_menu_stop_scrolling (menu);
3878 else /* !touchscreen_mode */
3880 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3882 if (enter && in_arrow &&
3883 (!menu->lower_arrow_prelight ||
3884 menu->scroll_fast != scroll_fast))
3886 menu->lower_arrow_prelight = TRUE;
3887 menu->scroll_fast = scroll_fast;
3889 /* Deselect the active item so that
3890 * any submenus are popped down
3892 gtk_menu_shell_deselect (menu_shell);
3894 gtk_menu_remove_scroll_timeout (menu);
3895 menu->scroll_step = scroll_fast ?
3896 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3899 gdk_threads_add_timeout (scroll_fast ?
3900 MENU_SCROLL_TIMEOUT2 :
3901 MENU_SCROLL_TIMEOUT1,
3902 gtk_menu_scroll_timeout, menu);
3904 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3906 gtk_menu_stop_scrolling (menu);
3911 /* gtk_menu_start_scrolling() might have hit the bottom of the
3912 * menu, so check if the button isn't insensitive before
3913 * changing it to something else.
3915 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3917 GtkStateType arrow_state = GTK_STATE_NORMAL;
3920 arrow_state = GTK_STATE_ACTIVE;
3921 else if (menu->lower_arrow_prelight)
3922 arrow_state = GTK_STATE_PRELIGHT;
3924 if (arrow_state != priv->lower_arrow_state)
3926 priv->lower_arrow_state = arrow_state;
3928 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3936 gtk_menu_enter_notify (GtkWidget *widget,
3937 GdkEventCrossing *event)
3939 GtkWidget *menu_item;
3940 gboolean touchscreen_mode;
3942 if (event->mode == GDK_CROSSING_GTK_GRAB ||
3943 event->mode == GDK_CROSSING_GTK_UNGRAB ||
3944 event->mode == GDK_CROSSING_STATE_CHANGED)
3947 g_object_get (gtk_widget_get_settings (widget),
3948 "gtk-touchscreen-mode", &touchscreen_mode,
3951 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3952 if (GTK_IS_MENU (widget))
3954 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3956 if (!menu_shell->ignore_enter)
3957 gtk_menu_handle_scrolling (GTK_MENU (widget),
3958 event->x_root, event->y_root, TRUE, TRUE);
3961 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3963 GtkWidget *menu = menu_item->parent;
3965 if (GTK_IS_MENU (menu))
3967 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3968 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3970 if (priv->seen_item_enter)
3972 /* This is the second enter we see for an item
3973 * on this menu. This means a release should always
3976 menu_shell->activate_time = 0;
3978 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3979 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3981 if (definitely_within_item (menu_item, event->x, event->y))
3983 /* This is an actual user-enter (ie. not a pop-under)
3984 * In this case, the user must either have entered
3985 * sufficiently far enough into the item, or he must move
3986 * far enough away from the enter point. (see
3987 * gtk_menu_motion_notify())
3989 menu_shell->activate_time = 0;
3993 priv->seen_item_enter = TRUE;
3997 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3998 * will not correspond to the event widget's parent. Check to see
3999 * if we are in the parent's navigation region.
4001 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
4002 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
4003 event->x_root, event->y_root))
4006 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4010 gtk_menu_leave_notify (GtkWidget *widget,
4011 GdkEventCrossing *event)
4013 GtkMenuShell *menu_shell;
4015 GtkMenuItem *menu_item;
4016 GtkWidget *event_widget;
4018 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4019 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4020 event->mode == GDK_CROSSING_STATE_CHANGED)
4023 menu = GTK_MENU (widget);
4024 menu_shell = GTK_MENU_SHELL (widget);
4026 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4029 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4031 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4033 if (!GTK_IS_MENU_ITEM (event_widget))
4036 menu_item = GTK_MENU_ITEM (event_widget);
4038 /* Here we check to see if we're leaving an active menu item with a submenu,
4039 * in which case we enter submenu navigation mode.
4041 if (menu_shell->active_menu_item != NULL
4042 && menu_item->submenu != NULL
4043 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4045 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4047 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4050 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4052 /* We are leaving an active menu item with nonactive submenu.
4053 * Deselect it so we don't surprise the user with by popping
4054 * up a submenu _after_ he left the item.
4056 gtk_menu_shell_deselect (menu_shell);
4061 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4065 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4067 if (menu->navigation_region)
4069 gdk_region_destroy (menu->navigation_region);
4070 menu->navigation_region = NULL;
4072 if (menu->navigation_timeout)
4074 g_source_remove (menu->navigation_timeout);
4075 menu->navigation_timeout = 0;
4079 /* When the timeout is elapsed, the navigation region is destroyed
4080 * and the menuitem under the pointer (if any) is selected.
4083 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4085 GtkMenuPopdownData *popdown_data = user_data;
4086 GtkMenu *menu = popdown_data->menu;
4087 GdkWindow *child_window;
4089 gtk_menu_stop_navigating_submenu (menu);
4091 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4093 child_window = gdk_window_get_device_position (menu->bin_window,
4094 popdown_data->device,
4099 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4101 send_event->crossing.window = g_object_ref (child_window);
4102 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4103 send_event->crossing.send_event = TRUE;
4104 gdk_event_set_device (send_event, popdown_data->device);
4106 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4108 gdk_event_free (send_event);
4116 gtk_menu_navigating_submenu (GtkMenu *menu,
4120 if (menu->navigation_region)
4122 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
4126 gtk_menu_stop_navigating_submenu (menu);
4133 #undef DRAW_STAY_UP_TRIANGLE
4135 #ifdef DRAW_STAY_UP_TRIANGLE
4138 draw_stay_up_triangle (GdkWindow *window,
4141 /* Draw ugly color all over the stay-up triangle */
4142 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
4143 GdkGCValues gc_values;
4145 GdkRectangle clipbox;
4147 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
4148 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
4149 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
4150 gdk_gc_set_clip_region (ugly_gc, region);
4152 gdk_region_get_clipbox (region, &clipbox);
4154 gdk_draw_rectangle (window,
4157 clipbox.x, clipbox.y,
4158 clipbox.width, clipbox.height);
4160 g_object_unref (ugly_gc);
4165 flip_region (GdkRegion *region,
4170 GdkRectangle *rectangles;
4171 GdkRectangle clipbox;
4172 GdkRegion *new_region;
4175 new_region = gdk_region_new ();
4177 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
4178 gdk_region_get_clipbox (region, &clipbox);
4180 for (i = 0; i < n_rectangles; ++i)
4182 GdkRectangle rect = rectangles[i];
4185 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
4188 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
4190 gdk_region_union_with_rect (new_region, &rect);
4193 g_free (rectangles);
4199 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4200 GtkMenuItem *menu_item,
4201 GdkEventCrossing *event)
4203 gint submenu_left = 0;
4204 gint submenu_right = 0;
4205 gint submenu_top = 0;
4206 gint submenu_bottom = 0;
4210 GtkWidget *event_widget;
4211 GtkMenuPopdownData *popdown_data;
4213 g_return_if_fail (menu_item->submenu != NULL);
4214 g_return_if_fail (event != NULL);
4216 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4218 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
4219 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
4221 submenu_right = submenu_left + width;
4222 submenu_bottom = submenu_top + height;
4224 gdk_drawable_get_size (event_widget->window, &width, &height);
4226 if (event->x >= 0 && event->x < width)
4229 gboolean flip_y = FALSE;
4230 gboolean flip_x = FALSE;
4232 gtk_menu_stop_navigating_submenu (menu);
4234 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4237 point[0].x = event->x_root;
4238 point[1].x = submenu_left;
4243 point[0].x = event->x_root + 1;
4244 point[1].x = 2 * (event->x_root + 1) - submenu_right;
4252 point[0].y = event->y_root + 1;
4253 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
4255 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
4263 point[0].y = event->y_root;
4264 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
4266 if (point[0].y >= submenu_bottom)
4270 point[2].x = point[1].x;
4271 point[2].y = point[0].y;
4273 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
4275 if (flip_x || flip_y)
4277 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
4278 gdk_region_destroy (menu->navigation_region);
4279 menu->navigation_region = new_region;
4282 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4283 "gtk-menu-popdown-delay", &popdown_delay,
4286 popdown_data = g_new (GtkMenuPopdownData, 1);
4287 popdown_data->menu = menu;
4288 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4290 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4292 gtk_menu_stop_navigating_submenu_cb,
4294 (GDestroyNotify) g_free);
4296 #ifdef DRAW_STAY_UP_TRIANGLE
4297 draw_stay_up_triangle (gdk_get_default_root_window(),
4298 menu->navigation_region);
4304 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4308 g_return_if_fail (GTK_IS_MENU (menu_shell));
4310 parent = menu_shell->parent_menu_shell;
4312 menu_shell->activate_time = 0;
4313 gtk_menu_popdown (GTK_MENU (menu_shell));
4316 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4320 gtk_menu_position (GtkMenu *menu)
4323 GtkRequisition requisition;
4324 GtkMenuPrivate *private;
4329 GdkScreen *pointer_screen;
4330 GdkRectangle monitor;
4333 g_return_if_fail (GTK_IS_MENU (menu));
4335 widget = GTK_WIDGET (menu);
4337 screen = gtk_widget_get_screen (widget);
4338 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4339 gdk_display_get_device_state (gdk_screen_get_display (screen),
4340 pointer, &pointer_screen, &x, &y, NULL);
4342 /* We need the requisition to figure out the right place to
4343 * popup the menu. In fact, we always need to ask here, since
4344 * if a size_request was queued while we weren't popped up,
4345 * the requisition won't have been recomputed yet.
4347 gtk_widget_size_request (widget, &requisition);
4349 if (pointer_screen != screen)
4351 /* Pointer is on a different screen; roughly center the
4352 * menu on the screen. If someone was using multiscreen
4353 * + Xinerama together they'd probably want something
4354 * fancier; but that is likely to be vanishingly rare.
4356 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4357 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4360 private = gtk_menu_get_private (menu);
4361 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4363 private->initially_pushed_in = FALSE;
4365 /* Set the type hint here to allow custom position functions to set a different hint */
4366 if (!gtk_widget_get_visible (menu->toplevel))
4367 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4369 if (menu->position_func)
4371 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4372 menu->position_func_data);
4374 if (private->monitor_num < 0)
4375 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4377 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4381 gint space_left, space_right, space_above, space_below;
4384 gint xthickness = widget->style->xthickness;
4385 gint ythickness = widget->style->ythickness;
4386 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4388 /* The placement of popup menus horizontally works like this (with
4389 * RTL in parentheses)
4391 * - If there is enough room to the right (left) of the mouse cursor,
4392 * position the menu there.
4394 * - Otherwise, if if there is enough room to the left (right) of the
4395 * mouse cursor, position the menu there.
4397 * - Otherwise if the menu is smaller than the monitor, position it
4398 * on the side of the mouse cursor that has the most space available
4400 * - Otherwise (if there is simply not enough room for the menu on the
4401 * monitor), position it as far left (right) as possible.
4403 * Positioning in the vertical direction is similar: first try below
4404 * mouse cursor, then above.
4406 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4408 space_left = x - monitor.x;
4409 space_right = monitor.x + monitor.width - x - 1;
4410 space_above = y - monitor.y;
4411 space_below = monitor.y + monitor.height - y - 1;
4413 /* position horizontally */
4415 /* the amount of space we need to position the menu. Note the
4416 * menu is offset "xthickness" pixels
4418 needed_width = requisition.width - xthickness;
4420 if (needed_width <= space_left ||
4421 needed_width <= space_right)
4423 if ((rtl && needed_width <= space_left) ||
4424 (!rtl && needed_width > space_right))
4427 x = x + xthickness - requisition.width + 1;
4431 /* position right */
4435 /* x is clamped on-screen further down */
4437 else if (requisition.width <= monitor.width)
4439 /* the menu is too big to fit on either side of the mouse
4440 * cursor, but smaller than the monitor. Position it on
4441 * the side that has the most space
4443 if (space_left > space_right)
4451 x = monitor.x + monitor.width - requisition.width;
4454 else /* menu is simply too big for the monitor */
4459 x = monitor.x + monitor.width - requisition.width;
4468 /* Position vertically. The algorithm is the same as above, but
4469 * simpler because we don't have to take RTL into account.
4471 needed_height = requisition.height - ythickness;
4473 if (needed_height <= space_above ||
4474 needed_height <= space_below)
4476 if (needed_height <= space_below)
4479 y = y + ythickness - requisition.height + 1;
4481 y = CLAMP (y, monitor.y,
4482 monitor.y + monitor.height - requisition.height);
4484 else if (needed_height > space_below && needed_height > space_above)
4486 if (space_below >= space_above)
4487 y = monitor.y + monitor.height - requisition.height;
4499 if (private->initially_pushed_in)
4501 menu_height = GTK_WIDGET (menu)->requisition.height;
4503 if (y + menu_height > monitor.y + monitor.height)
4505 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4506 y = (monitor.y + monitor.height) - menu_height;
4511 scroll_offset += monitor.y - y;
4516 /* FIXME: should this be done in the various position_funcs ? */
4517 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4519 if (GTK_MENU_SHELL (menu)->active)
4521 private->have_position = TRUE;
4526 if (y + requisition.height > monitor.y + monitor.height)
4527 requisition.height = (monitor.y + monitor.height) - y;
4531 scroll_offset += monitor.y - y;
4532 requisition.height -= monitor.y - y;
4536 if (scroll_offset > 0)
4538 GtkBorder arrow_border;
4540 get_arrows_border (menu, &arrow_border);
4541 scroll_offset += arrow_border.top;
4544 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4547 if (!GTK_MENU_SHELL (menu)->active)
4549 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4550 requisition.width, requisition.height);
4553 menu->scroll_offset = scroll_offset;
4557 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4559 if (menu->timeout_id)
4561 g_source_remove (menu->timeout_id);
4562 menu->timeout_id = 0;
4567 gtk_menu_stop_scrolling (GtkMenu *menu)
4569 gboolean touchscreen_mode;
4571 gtk_menu_remove_scroll_timeout (menu);
4573 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4574 "gtk-touchscreen-mode", &touchscreen_mode,
4577 if (!touchscreen_mode)
4579 menu->upper_arrow_prelight = FALSE;
4580 menu->lower_arrow_prelight = FALSE;
4585 gtk_menu_scroll_to (GtkMenu *menu,
4590 gint view_width, view_height;
4593 guint vertical_padding;
4594 guint horizontal_padding;
4595 gboolean double_arrows;
4596 GtkBorder arrow_border;
4598 widget = GTK_WIDGET (menu);
4600 if (menu->tearoff_active &&
4601 menu->tearoff_adjustment &&
4602 (menu->tearoff_adjustment->value != offset))
4604 menu->tearoff_adjustment->value =
4606 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4607 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4610 /* Move/resize the viewport according to arrows: */
4611 view_width = widget->allocation.width;
4612 view_height = widget->allocation.height;
4614 gtk_widget_style_get (GTK_WIDGET (menu),
4615 "vertical-padding", &vertical_padding,
4616 "horizontal-padding", &horizontal_padding,
4619 double_arrows = get_double_arrows (menu);
4621 border_width = GTK_CONTAINER (menu)->border_width;
4622 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4623 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4624 menu_height = widget->requisition.height -
4625 (border_width + widget->style->ythickness + vertical_padding) * 2;
4627 x = border_width + widget->style->xthickness + horizontal_padding;
4628 y = border_width + widget->style->ythickness + vertical_padding;
4630 if (double_arrows && !menu->tearoff_active)
4632 if (view_height < menu_height ||
4633 (offset > 0 && menu->scroll_offset > 0) ||
4634 (offset < 0 && menu->scroll_offset < 0))
4636 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4637 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4638 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4640 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4641 gtk_widget_queue_draw (GTK_WIDGET (menu));
4643 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4645 get_arrows_border (menu, &arrow_border);
4646 y += arrow_border.top;
4647 view_height -= arrow_border.top;
4648 view_height -= arrow_border.bottom;
4651 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4652 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4653 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4654 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4656 if (offset >= menu_height - view_height)
4657 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4658 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4659 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4660 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4662 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4663 (priv->lower_arrow_state != lower_arrow_previous_state))
4664 gtk_widget_queue_draw (GTK_WIDGET (menu));
4666 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4667 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4669 /* At the upper border, possibly remove timeout */
4670 if (menu->scroll_step < 0)
4672 gtk_menu_stop_scrolling (menu);
4673 gtk_widget_queue_draw (GTK_WIDGET (menu));
4677 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4678 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4680 /* At the lower border, possibly remove timeout */
4681 if (menu->scroll_step > 0)
4683 gtk_menu_stop_scrolling (menu);
4684 gtk_widget_queue_draw (GTK_WIDGET (menu));
4688 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4692 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4693 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4695 gtk_menu_stop_scrolling (menu);
4696 gtk_widget_queue_draw (GTK_WIDGET (menu));
4699 else if (!menu->tearoff_active)
4701 gboolean last_visible;
4703 last_visible = menu->upper_arrow_visible;
4704 menu->upper_arrow_visible = offset > 0;
4706 /* upper_arrow_visible may have changed, so requery the border */
4707 get_arrows_border (menu, &arrow_border);
4708 view_height -= arrow_border.top;
4710 if ((last_visible != menu->upper_arrow_visible) &&
4711 !menu->upper_arrow_visible)
4713 menu->upper_arrow_prelight = FALSE;
4715 /* If we hid the upper arrow, possibly remove timeout */
4716 if (menu->scroll_step < 0)
4718 gtk_menu_stop_scrolling (menu);
4719 gtk_widget_queue_draw (GTK_WIDGET (menu));
4723 last_visible = menu->lower_arrow_visible;
4724 menu->lower_arrow_visible = offset < menu_height - view_height;
4726 /* lower_arrow_visible may have changed, so requery the border */
4727 get_arrows_border (menu, &arrow_border);
4728 view_height -= arrow_border.bottom;
4730 if ((last_visible != menu->lower_arrow_visible) &&
4731 !menu->lower_arrow_visible)
4733 menu->lower_arrow_prelight = FALSE;
4735 /* If we hid the lower arrow, possibly remove timeout */
4736 if (menu->scroll_step > 0)
4738 gtk_menu_stop_scrolling (menu);
4739 gtk_widget_queue_draw (GTK_WIDGET (menu));
4743 y += arrow_border.top;
4746 /* Scroll the menu: */
4747 if (gtk_widget_get_realized (widget))
4748 gdk_window_move (menu->bin_window, 0, -offset);
4750 if (gtk_widget_get_realized (widget))
4751 gdk_window_move_resize (menu->view_window,
4757 menu->scroll_offset = offset;
4761 compute_child_offset (GtkMenu *menu,
4762 GtkWidget *menu_item,
4765 gboolean *is_last_child)
4767 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4768 gint item_top_attach;
4769 gint item_bottom_attach;
4770 gint child_offset = 0;
4773 get_effective_child_attach (menu_item, NULL, NULL,
4774 &item_top_attach, &item_bottom_attach);
4776 /* there is a possibility that we get called before _size_request, so
4777 * check the height table for safety.
4779 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4782 /* when we have a row with only invisible children, it's height will
4783 * be zero, so there's no need to check WIDGET_VISIBLE here
4785 for (i = 0; i < item_top_attach; i++)
4786 child_offset += priv->heights[i];
4789 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4791 *offset = child_offset;
4793 *height = priv->heights[item_top_attach];
4799 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4800 GtkWidget *menu_item)
4803 gint child_offset, child_height;
4807 gboolean last_child = 0;
4809 menu = GTK_MENU (menu_shell);
4811 /* We need to check if the selected item fully visible.
4812 * If not we need to scroll the menu so that it becomes fully
4816 if (compute_child_offset (menu, menu_item,
4817 &child_offset, &child_height, &last_child))
4819 guint vertical_padding;
4820 gboolean double_arrows;
4822 y = menu->scroll_offset;
4823 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4825 gtk_widget_style_get (GTK_WIDGET (menu),
4826 "vertical-padding", &vertical_padding,
4829 double_arrows = get_double_arrows (menu);
4831 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4833 if (child_offset < y)
4835 /* Ignore the enter event we might get if the pointer is on the menu
4837 menu_shell->ignore_enter = TRUE;
4838 gtk_menu_scroll_to (menu, child_offset);
4842 GtkBorder arrow_border;
4846 get_arrows_border (menu, &arrow_border);
4847 if (!menu->tearoff_active)
4848 arrow_height = arrow_border.top + arrow_border.bottom;
4850 if (child_offset + child_height > y + height - arrow_height)
4853 if ((!last_child && !menu->tearoff_active) || double_arrows)
4854 arrow_height += arrow_border.bottom;
4856 y = child_offset + child_height - height + arrow_height;
4857 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4859 /* Need upper arrow */
4860 arrow_height += arrow_border.top;
4861 y = child_offset + child_height - height + arrow_height;
4863 /* Ignore the enter event we might get if the pointer is on the menu
4865 menu_shell->ignore_enter = TRUE;
4866 gtk_menu_scroll_to (menu, y);
4874 gtk_menu_select_item (GtkMenuShell *menu_shell,
4875 GtkWidget *menu_item)
4877 GtkMenu *menu = GTK_MENU (menu_shell);
4879 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4880 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4882 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4886 /* Reparent the menu, taking care of the refcounting
4888 * If unrealize is true we force a unrealize while reparenting the parent.
4889 * This can help eliminate flicker in some cases.
4891 * What happens is that when the menu is unrealized and then re-realized,
4892 * the allocations are as follows:
4894 * parent - 1x1 at (0,0)
4895 * child1 - 100x20 at (0,0)
4896 * child2 - 100x20 at (0,20)
4897 * child3 - 100x20 at (0,40)
4899 * That is, the parent is small but the children are full sized. Then,
4900 * when the queued_resize gets processed, the parent gets resized to
4903 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4904 * contains the following logic:
4906 * - if a move or resize operation on a window would change the clip
4907 * region on the children, then before the window is resized
4908 * the background for children is temporarily set to None, the
4909 * move/resize done, and the background for the children restored.
4911 * So, at the point where the parent is resized to final size, the
4912 * background for the children is temporarily None, and thus they
4913 * are not cleared to the background color and the previous background
4914 * (the image of the menu) is left in place.
4917 gtk_menu_reparent (GtkMenu *menu,
4918 GtkWidget *new_parent,
4921 GtkObject *object = GTK_OBJECT (menu);
4922 GtkWidget *widget = GTK_WIDGET (menu);
4923 gboolean was_floating = g_object_is_floating (object);
4925 g_object_ref_sink (object);
4929 g_object_ref (object);
4930 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4931 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4932 g_object_unref (object);
4935 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4938 g_object_force_floating (G_OBJECT (object));
4940 g_object_unref (object);
4944 gtk_menu_show_all (GtkWidget *widget)
4946 /* Show children, but not self. */
4947 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4952 gtk_menu_hide_all (GtkWidget *widget)
4954 /* Hide children, but not self. */
4955 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4959 * gtk_menu_set_screen:
4960 * @menu: a #GtkMenu.
4961 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
4962 * determined by the widget the menu is attached to.
4964 * Sets the #GdkScreen on which the menu will be displayed.
4969 gtk_menu_set_screen (GtkMenu *menu,
4972 g_return_if_fail (GTK_IS_MENU (menu));
4973 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4975 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4979 menu_change_screen (menu, screen);
4983 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4985 attach_widget_screen_changed (attach_widget, NULL, menu);
4991 * @menu: a #GtkMenu.
4992 * @child: a #GtkMenuItem.
4993 * @left_attach: The column number to attach the left side of the item to.
4994 * @right_attach: The column number to attach the right side of the item to.
4995 * @top_attach: The row number to attach the top of the item to.
4996 * @bottom_attach: The row number to attach the bottom of the item to.
4998 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4999 * an item will occupy is specified by @left_attach, @right_attach,
5000 * @top_attach and @bottom_attach. These each represent the leftmost,
5001 * rightmost, uppermost and lower column and row numbers of the table.
5002 * (Columns and rows are indexed from zero).
5004 * Note that this function is not related to gtk_menu_detach().
5009 gtk_menu_attach (GtkMenu *menu,
5014 guint bottom_attach)
5016 GtkMenuShell *menu_shell;
5018 g_return_if_fail (GTK_IS_MENU (menu));
5019 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5020 g_return_if_fail (child->parent == NULL ||
5021 child->parent == GTK_WIDGET (menu));
5022 g_return_if_fail (left_attach < right_attach);
5023 g_return_if_fail (top_attach < bottom_attach);
5025 menu_shell = GTK_MENU_SHELL (menu);
5029 AttachInfo *ai = get_attach_info (child);
5031 ai->left_attach = left_attach;
5032 ai->right_attach = right_attach;
5033 ai->top_attach = top_attach;
5034 ai->bottom_attach = bottom_attach;
5036 menu_shell->children = g_list_append (menu_shell->children, child);
5038 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5040 menu_queue_resize (menu);
5044 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
5045 "left-attach", left_attach,
5046 "right-attach", right_attach,
5047 "top-attach", top_attach,
5048 "bottom-attach", bottom_attach,
5054 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5058 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5059 "gtk-menu-popup-delay", &popup_delay,
5066 find_child_containing (GtkMenuShell *menu_shell,
5074 /* find a child which includes the area given by
5075 * left, right, top, bottom.
5078 for (list = menu_shell->children; list; list = list->next)
5082 if (!_gtk_menu_item_is_selectable (list->data))
5085 get_effective_child_attach (list->data, &l, &r, &t, &b);
5087 if (l <= left && right <= r
5088 && t <= top && bottom <= b)
5089 return GTK_WIDGET (list->data);
5096 gtk_menu_move_current (GtkMenuShell *menu_shell,
5097 GtkMenuDirectionType direction)
5099 GtkMenu *menu = GTK_MENU (menu_shell);
5102 GtkWidget *match = NULL;
5104 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5108 case GTK_MENU_DIR_CHILD:
5109 direction = GTK_MENU_DIR_PARENT;
5111 case GTK_MENU_DIR_PARENT:
5112 direction = GTK_MENU_DIR_CHILD;
5118 /* use special table menu key bindings */
5119 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5121 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5123 if (direction == GTK_MENU_DIR_NEXT)
5125 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5127 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5135 for (i = 0; i < t; i++)
5137 match = find_child_containing (menu_shell,
5138 l, l + 1, i, i + 1);
5144 else if (direction == GTK_MENU_DIR_PREV)
5146 for (i = t; i > 0; i--)
5148 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5156 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5158 match = find_child_containing (menu_shell,
5159 l, l + 1, i - 1, i);
5165 else if (direction == GTK_MENU_DIR_PARENT)
5167 /* we go one left if possible */
5169 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5173 GtkWidget *parent = menu_shell->parent_menu_shell;
5176 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5177 match = menu_shell->active_menu_item;
5180 else if (direction == GTK_MENU_DIR_CHILD)
5182 /* we go one right if possible */
5183 if (r < gtk_menu_get_n_columns (menu))
5184 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5188 GtkWidget *parent = menu_shell->parent_menu_shell;
5190 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5192 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5193 match = menu_shell->active_menu_item;
5199 gtk_menu_shell_select_item (menu_shell, match);
5204 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5208 get_visible_size (GtkMenu *menu)
5210 GtkWidget *widget = GTK_WIDGET (menu);
5211 GtkContainer *container = GTK_CONTAINER (menu);
5213 gint menu_height = (widget->allocation.height
5214 - 2 * (container->border_width
5215 + widget->style->ythickness));
5217 if (!menu->tearoff_active)
5219 GtkBorder arrow_border;
5221 get_arrows_border (menu, &arrow_border);
5222 menu_height -= arrow_border.top;
5223 menu_height -= arrow_border.bottom;
5229 /* Find the sensitive on-screen child containing @y, or if none,
5230 * the nearest selectable onscreen child. (%NULL if none)
5233 child_at (GtkMenu *menu,
5236 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5237 GtkWidget *child = NULL;
5238 gint child_offset = 0;
5241 gint lower, upper; /* Onscreen bounds */
5243 menu_height = get_visible_size (menu);
5244 lower = menu->scroll_offset;
5245 upper = menu->scroll_offset + menu_height;
5247 for (children = menu_shell->children; children; children = children->next)
5249 if (gtk_widget_get_visible (children->data))
5251 GtkRequisition child_requisition;
5253 gtk_widget_size_request (children->data, &child_requisition);
5255 if (_gtk_menu_item_is_selectable (children->data) &&
5256 child_offset >= lower &&
5257 child_offset + child_requisition.height <= upper)
5259 child = children->data;
5261 if (child_offset + child_requisition.height > y &&
5262 !GTK_IS_TEAROFF_MENU_ITEM (child))
5266 child_offset += child_requisition.height;
5274 get_menu_height (GtkMenu *menu)
5277 GtkWidget *widget = GTK_WIDGET (menu);
5279 height = widget->requisition.height;
5280 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
5282 if (!menu->tearoff_active)
5284 GtkBorder arrow_border;
5286 get_arrows_border (menu, &arrow_border);
5287 height -= arrow_border.top;
5288 height -= arrow_border.bottom;
5295 gtk_menu_real_move_scroll (GtkMenu *menu,
5298 gint page_size = get_visible_size (menu);
5299 gint end_position = get_menu_height (menu);
5300 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5304 case GTK_SCROLL_PAGE_UP:
5305 case GTK_SCROLL_PAGE_DOWN:
5309 gint child_offset = 0;
5310 gboolean old_upper_arrow_visible;
5313 if (type == GTK_SCROLL_PAGE_UP)
5318 if (menu_shell->active_menu_item)
5322 compute_child_offset (menu, menu_shell->active_menu_item,
5323 &child_offset, &child_height, NULL);
5324 child_offset += child_height / 2;
5327 menu_shell->ignore_enter = TRUE;
5328 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5329 old_offset = menu->scroll_offset;
5331 new_offset = menu->scroll_offset + step;
5332 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5334 gtk_menu_scroll_to (menu, new_offset);
5336 if (menu_shell->active_menu_item)
5338 GtkWidget *new_child;
5339 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5340 GtkBorder arrow_border;
5342 get_arrows_border (menu, &arrow_border);
5344 if (menu->scroll_offset != old_offset)
5345 step = menu->scroll_offset - old_offset;
5347 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5349 new_child = child_at (menu, child_offset + step);
5351 gtk_menu_shell_select_item (menu_shell, new_child);
5355 case GTK_SCROLL_START:
5356 /* Ignore the enter event we might get if the pointer is on the menu
5358 menu_shell->ignore_enter = TRUE;
5359 gtk_menu_scroll_to (menu, 0);
5360 gtk_menu_shell_select_first (menu_shell, TRUE);
5362 case GTK_SCROLL_END:
5363 /* Ignore the enter event we might get if the pointer is on the menu
5365 menu_shell->ignore_enter = TRUE;
5366 gtk_menu_scroll_to (menu, end_position - page_size);
5367 _gtk_menu_shell_select_last (menu_shell, TRUE);
5376 * gtk_menu_set_monitor:
5378 * @monitor_num: the number of the monitor on which the menu should
5381 * Informs GTK+ on which monitor a menu should be popped up.
5382 * See gdk_screen_get_monitor_geometry().
5384 * This function should be called from a #GtkMenuPositionFunc if the
5385 * menu should not appear on the same monitor as the pointer. This
5386 * information can't be reliably inferred from the coordinates returned
5387 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5388 * may extend beyond the monitor boundaries or even the screen boundaries.
5393 gtk_menu_set_monitor (GtkMenu *menu,
5396 GtkMenuPrivate *priv;
5397 g_return_if_fail (GTK_IS_MENU (menu));
5399 priv = gtk_menu_get_private (menu);
5401 priv->monitor_num = monitor_num;
5405 * gtk_menu_get_monitor:
5408 * Retrieves the number of the monitor on which to show the menu.
5410 * Returns: the number of the monitor on which the menu should
5411 * be popped up or -1, if no monitor has been set
5416 gtk_menu_get_monitor (GtkMenu *menu)
5418 GtkMenuPrivate *priv;
5419 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5421 priv = gtk_menu_get_private (menu);
5423 return priv->monitor_num;
5427 * gtk_menu_get_for_attach_widget:
5428 * @widget: a #GtkWidget
5430 * Returns a list of the menus which are attached to this widget.
5431 * This list is owned by GTK+ and must not be modified.
5433 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5438 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5442 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5444 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5449 gtk_menu_grab_notify (GtkWidget *widget,
5450 gboolean was_grabbed)
5452 GtkWidget *toplevel;
5453 GtkWindowGroup *group;
5457 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5460 !gtk_widget_device_is_shadowed (widget, pointer))
5463 toplevel = gtk_widget_get_toplevel (widget);
5465 if (!GTK_IS_WINDOW (toplevel))
5468 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5469 grab = gtk_window_group_get_current_device_grab (group, pointer);
5471 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5472 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5476 * gtk_menu_set_reserve_toggle_size:
5478 * @reserve_toggle_size: whether to reserve size for toggles
5480 * Sets whether the menu should reserve space for drawing toggles
5481 * or icons, regardless of their actual presence.
5486 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5487 gboolean reserve_toggle_size)
5489 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5490 gboolean no_toggle_size;
5492 no_toggle_size = !reserve_toggle_size;
5494 if (priv->no_toggle_size != no_toggle_size)
5496 priv->no_toggle_size = no_toggle_size;
5498 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5503 * gtk_menu_get_reserve_toggle_size:
5506 * Returns whether the menu reserves space for toggles and
5507 * icons, regardless of their actual presence.
5509 * Returns: Whether the menu reserves toggle space
5514 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5516 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5518 return !priv->no_toggle_size;
5521 #define __GTK_MENU_C__
5522 #include "gtkaliasdef.c"