1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkaccellabel.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtkcheckmenuitem.h"
34 #include <gobject/gvaluecollector.h>
36 #include "gtkmarshalers.h"
38 #include "gtkmenuprivate.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtksizerequest.h"
45 #include "gtkprivate.h"
48 #define DEFAULT_POPUP_DELAY 225
49 #define DEFAULT_POPDOWN_DELAY 1000
51 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
52 * extends below the submenu
55 #define MENU_SCROLL_STEP1 8
56 #define MENU_SCROLL_STEP2 15
57 #define MENU_SCROLL_FAST_ZONE 8
58 #define MENU_SCROLL_TIMEOUT1 50
59 #define MENU_SCROLL_TIMEOUT2 20
61 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
62 #define ATTACHED_MENUS "gtk-attached-menus"
64 typedef struct _GtkMenuAttachData GtkMenuAttachData;
65 typedef struct _GtkMenuPrivate GtkMenuPrivate;
66 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
68 struct _GtkMenuAttachData
70 GtkWidget *attach_widget;
71 GtkMenuDetachFunc detacher;
74 struct _GtkMenuPrivate
78 gboolean initially_pushed_in;
80 GDestroyNotify position_func_data_destroy;
82 /* info used for the table */
85 gint requested_height;
89 /* Cached layout information */
98 GtkStateType lower_arrow_state;
99 GtkStateType upper_arrow_state;
101 /* navigation region */
104 int navigation_width;
105 int navigation_height;
107 guint have_layout : 1;
108 guint seen_item_enter : 1;
109 guint have_position : 1;
110 guint ignore_button_release : 1;
111 guint no_toggle_size : 1;
114 struct _GtkMenuPopdownData
126 gint effective_left_attach;
127 gint effective_right_attach;
128 gint effective_top_attach;
129 gint effective_bottom_attach;
146 PROP_RESERVE_TOGGLE_SIZE
151 CHILD_PROP_LEFT_ATTACH,
152 CHILD_PROP_RIGHT_ATTACH,
153 CHILD_PROP_TOP_ATTACH,
154 CHILD_PROP_BOTTOM_ATTACH
157 static void gtk_menu_set_property (GObject *object,
161 static void gtk_menu_get_property (GObject *object,
165 static void gtk_menu_set_child_property(GtkContainer *container,
170 static void gtk_menu_get_child_property(GtkContainer *container,
175 static void gtk_menu_destroy (GtkObject *object);
176 static void gtk_menu_realize (GtkWidget *widget);
177 static void gtk_menu_unrealize (GtkWidget *widget);
178 static void gtk_menu_size_allocate (GtkWidget *widget,
179 GtkAllocation *allocation);
180 static void gtk_menu_paint (GtkWidget *widget,
181 GdkEventExpose *expose);
182 static void gtk_menu_show (GtkWidget *widget);
183 static gboolean gtk_menu_expose (GtkWidget *widget,
184 GdkEventExpose *event);
185 static gboolean gtk_menu_key_press (GtkWidget *widget,
187 static gboolean gtk_menu_scroll (GtkWidget *widget,
188 GdkEventScroll *event);
189 static gboolean gtk_menu_button_press (GtkWidget *widget,
190 GdkEventButton *event);
191 static gboolean gtk_menu_button_release (GtkWidget *widget,
192 GdkEventButton *event);
193 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
194 GdkEventMotion *event);
195 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
196 GdkEventCrossing *event);
197 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
198 GdkEventCrossing *event);
199 static void gtk_menu_scroll_to (GtkMenu *menu,
201 static void gtk_menu_grab_notify (GtkWidget *widget,
202 gboolean was_grabbed);
204 static void gtk_menu_stop_scrolling (GtkMenu *menu);
205 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
206 static gboolean gtk_menu_scroll_timeout (gpointer data);
207 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
208 static void gtk_menu_start_scrolling (GtkMenu *menu);
210 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
211 GtkWidget *menu_item);
212 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
213 GtkWidget *menu_item);
214 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
217 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
219 static void gtk_menu_handle_scrolling (GtkMenu *menu,
224 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
226 static void gtk_menu_style_set (GtkWidget *widget,
227 GtkStyle *previous_style);
228 static gboolean gtk_menu_focus (GtkWidget *widget,
229 GtkDirectionType direction);
230 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
231 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
232 GtkMenuDirectionType direction);
233 static void gtk_menu_real_move_scroll (GtkMenu *menu,
236 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
237 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
238 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
241 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
242 GtkMenuItem *menu_item,
243 GdkEventCrossing *event);
245 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
246 static void gtk_menu_show_all (GtkWidget *widget);
247 static void gtk_menu_hide_all (GtkWidget *widget);
248 static void gtk_menu_position (GtkMenu *menu);
249 static void gtk_menu_reparent (GtkMenu *menu,
250 GtkWidget *new_parent,
252 static void gtk_menu_remove (GtkContainer *menu,
255 static void gtk_menu_update_title (GtkMenu *menu);
257 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
258 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
260 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
262 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
263 gboolean group_changed);
265 static void gtk_menu_size_request_init (GtkSizeRequestIface *iface);
266 static void gtk_menu_get_width (GtkSizeRequest *widget,
269 static void gtk_menu_get_height (GtkSizeRequest *widget,
272 static void gtk_menu_get_height_for_width (GtkSizeRequest *widget,
278 static const gchar attach_data_key[] = "gtk-menu-attach-data";
280 static guint menu_signals[LAST_SIGNAL] = { 0 };
282 static GtkMenuPrivate *
283 gtk_menu_get_private (GtkMenu *menu)
285 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
288 G_DEFINE_TYPE_WITH_CODE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL,
289 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
290 gtk_menu_size_request_init))
293 menu_queue_resize (GtkMenu *menu)
295 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
297 priv->have_layout = FALSE;
298 gtk_widget_queue_resize (GTK_WIDGET (menu));
302 attach_info_free (AttachInfo *info)
304 g_slice_free (AttachInfo, info);
308 get_attach_info (GtkWidget *child)
310 GObject *object = G_OBJECT (child);
311 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
315 ai = g_slice_new0 (AttachInfo);
316 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
317 (GDestroyNotify) attach_info_free);
324 is_grid_attached (AttachInfo *ai)
326 return (ai->left_attach >= 0 &&
327 ai->right_attach >= 0 &&
328 ai->top_attach >= 0 &&
329 ai->bottom_attach >= 0);
333 menu_ensure_layout (GtkMenu *menu)
335 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
337 if (!priv->have_layout)
339 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
343 gint max_right_attach;
344 gint max_bottom_attach;
346 /* Find extents of gridded portion
348 max_right_attach = 1;
349 max_bottom_attach = 0;
351 for (l = menu_shell->children; l; l = l->next)
353 GtkWidget *child = l->data;
354 AttachInfo *ai = get_attach_info (child);
356 if (is_grid_attached (ai))
358 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
359 max_right_attach = MAX (max_right_attach, ai->right_attach);
365 row_occupied = g_malloc0 (max_bottom_attach);
367 for (l = menu_shell->children; l; l = l->next)
369 GtkWidget *child = l->data;
370 AttachInfo *ai = get_attach_info (child);
372 if (is_grid_attached (ai))
376 for (i = ai->top_attach; i < ai->bottom_attach; i++)
377 row_occupied[i] = TRUE;
381 /* Lay non-grid-items out in those rows
384 for (l = menu_shell->children; l; l = l->next)
386 GtkWidget *child = l->data;
387 AttachInfo *ai = get_attach_info (child);
389 if (!is_grid_attached (ai))
391 while (current_row < max_bottom_attach && row_occupied[current_row])
394 ai->effective_left_attach = 0;
395 ai->effective_right_attach = max_right_attach;
396 ai->effective_top_attach = current_row;
397 ai->effective_bottom_attach = current_row + 1;
403 ai->effective_left_attach = ai->left_attach;
404 ai->effective_right_attach = ai->right_attach;
405 ai->effective_top_attach = ai->top_attach;
406 ai->effective_bottom_attach = ai->bottom_attach;
410 g_free (row_occupied);
412 priv->n_rows = MAX (current_row, max_bottom_attach);
413 priv->n_columns = max_right_attach;
414 priv->have_layout = TRUE;
420 gtk_menu_get_n_columns (GtkMenu *menu)
422 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
424 menu_ensure_layout (menu);
426 return priv->n_columns;
430 gtk_menu_get_n_rows (GtkMenu *menu)
432 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
434 menu_ensure_layout (menu);
440 get_effective_child_attach (GtkWidget *child,
446 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
449 menu_ensure_layout (menu);
451 ai = get_attach_info (child);
454 *l = ai->effective_left_attach;
456 *r = ai->effective_right_attach;
458 *t = ai->effective_top_attach;
460 *b = ai->effective_bottom_attach;
465 gtk_menu_class_init (GtkMenuClass *class)
467 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
468 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
469 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
470 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
471 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
472 GtkBindingSet *binding_set;
474 gobject_class->set_property = gtk_menu_set_property;
475 gobject_class->get_property = gtk_menu_get_property;
477 object_class->destroy = gtk_menu_destroy;
479 widget_class->realize = gtk_menu_realize;
480 widget_class->unrealize = gtk_menu_unrealize;
481 widget_class->size_allocate = gtk_menu_size_allocate;
482 widget_class->show = gtk_menu_show;
483 widget_class->expose_event = gtk_menu_expose;
484 widget_class->scroll_event = gtk_menu_scroll;
485 widget_class->key_press_event = gtk_menu_key_press;
486 widget_class->button_press_event = gtk_menu_button_press;
487 widget_class->button_release_event = gtk_menu_button_release;
488 widget_class->motion_notify_event = gtk_menu_motion_notify;
489 widget_class->show_all = gtk_menu_show_all;
490 widget_class->hide_all = gtk_menu_hide_all;
491 widget_class->enter_notify_event = gtk_menu_enter_notify;
492 widget_class->leave_notify_event = gtk_menu_leave_notify;
493 widget_class->style_set = gtk_menu_style_set;
494 widget_class->focus = gtk_menu_focus;
495 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
496 widget_class->grab_notify = gtk_menu_grab_notify;
498 container_class->remove = gtk_menu_remove;
499 container_class->get_child_property = gtk_menu_get_child_property;
500 container_class->set_child_property = gtk_menu_set_child_property;
502 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
503 menu_shell_class->deactivate = gtk_menu_deactivate;
504 menu_shell_class->select_item = gtk_menu_select_item;
505 menu_shell_class->insert = gtk_menu_real_insert;
506 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
507 menu_shell_class->move_current = gtk_menu_move_current;
509 menu_signals[MOVE_SCROLL] =
510 g_signal_new_class_handler (I_("move-scroll"),
511 G_OBJECT_CLASS_TYPE (object_class),
512 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
513 G_CALLBACK (gtk_menu_real_move_scroll),
515 _gtk_marshal_VOID__ENUM,
517 GTK_TYPE_SCROLL_TYPE);
522 * The index of the currently selected menu item, or -1 if no
523 * menu item is selected.
527 g_object_class_install_property (gobject_class,
529 g_param_spec_int ("active",
531 P_("The currently selected menu item"),
533 GTK_PARAM_READWRITE));
536 * GtkMenu:accel-group:
538 * The accel group holding accelerators for the menu.
542 g_object_class_install_property (gobject_class,
544 g_param_spec_object ("accel-group",
546 P_("The accel group holding accelerators for the menu"),
547 GTK_TYPE_ACCEL_GROUP,
548 GTK_PARAM_READWRITE));
551 * GtkMenu:accel-path:
553 * An accel path used to conveniently construct accel paths of child items.
557 g_object_class_install_property (gobject_class,
559 g_param_spec_string ("accel-path",
561 P_("An accel path used to conveniently construct accel paths of child items"),
563 GTK_PARAM_READWRITE));
566 * GtkMenu:attach-widget:
568 * The widget the menu is attached to. Setting this property attaches
569 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
570 * use gtk_menu_attach_to_widget() directly.
574 g_object_class_install_property (gobject_class,
576 g_param_spec_object ("attach-widget",
578 P_("The widget the menu is attached to"),
580 GTK_PARAM_READWRITE));
582 g_object_class_install_property (gobject_class,
584 g_param_spec_string ("tearoff-title",
586 P_("A title that may be displayed by the window manager when this menu is torn-off"),
588 GTK_PARAM_READWRITE));
591 * GtkMenu:tearoff-state:
593 * A boolean that indicates whether the menu is torn-off.
597 g_object_class_install_property (gobject_class,
599 g_param_spec_boolean ("tearoff-state",
601 P_("A boolean that indicates whether the menu is torn-off"),
603 GTK_PARAM_READWRITE));
608 * The monitor the menu will be popped up on.
612 g_object_class_install_property (gobject_class,
614 g_param_spec_int ("monitor",
616 P_("The monitor the menu will be popped up on"),
618 GTK_PARAM_READWRITE));
620 gtk_widget_class_install_style_property (widget_class,
621 g_param_spec_int ("vertical-padding",
622 P_("Vertical Padding"),
623 P_("Extra space at the top and bottom of the menu"),
627 GTK_PARAM_READABLE));
630 * GtkMenu:reserve-toggle-size:
632 * A boolean that indicates whether the menu reserves space for
633 * toggles and icons, regardless of their actual presence.
635 * This property should only be changed from its default value
636 * for special-purposes such as tabular menus. Regular menus that
637 * are connected to a menu bar or context menus should reserve
638 * toggle space for consistency.
642 g_object_class_install_property (gobject_class,
643 PROP_RESERVE_TOGGLE_SIZE,
644 g_param_spec_boolean ("reserve-toggle-size",
645 P_("Reserve Toggle Size"),
646 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
648 GTK_PARAM_READWRITE));
650 gtk_widget_class_install_style_property (widget_class,
651 g_param_spec_int ("horizontal-padding",
652 P_("Horizontal Padding"),
653 P_("Extra space at the left and right edges of the menu"),
657 GTK_PARAM_READABLE));
659 gtk_widget_class_install_style_property (widget_class,
660 g_param_spec_int ("vertical-offset",
661 P_("Vertical Offset"),
662 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
666 GTK_PARAM_READABLE));
668 gtk_widget_class_install_style_property (widget_class,
669 g_param_spec_int ("horizontal-offset",
670 P_("Horizontal Offset"),
671 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
675 GTK_PARAM_READABLE));
677 gtk_widget_class_install_style_property (widget_class,
678 g_param_spec_boolean ("double-arrows",
680 P_("When scrolling, always show both arrows."),
682 GTK_PARAM_READABLE));
685 * GtkMenu:arrow-placement:
687 * Indicates where scroll arrows should be placed.
691 gtk_widget_class_install_style_property (widget_class,
692 g_param_spec_enum ("arrow-placement",
693 P_("Arrow Placement"),
694 P_("Indicates where scroll arrows should be placed"),
695 GTK_TYPE_ARROW_PLACEMENT,
697 GTK_PARAM_READABLE));
699 gtk_container_class_install_child_property (container_class,
700 CHILD_PROP_LEFT_ATTACH,
701 g_param_spec_int ("left-attach",
703 P_("The column number to attach the left side of the child to"),
705 GTK_PARAM_READWRITE));
707 gtk_container_class_install_child_property (container_class,
708 CHILD_PROP_RIGHT_ATTACH,
709 g_param_spec_int ("right-attach",
711 P_("The column number to attach the right side of the child to"),
713 GTK_PARAM_READWRITE));
715 gtk_container_class_install_child_property (container_class,
716 CHILD_PROP_TOP_ATTACH,
717 g_param_spec_int ("top-attach",
719 P_("The row number to attach the top of the child to"),
721 GTK_PARAM_READWRITE));
723 gtk_container_class_install_child_property (container_class,
724 CHILD_PROP_BOTTOM_ATTACH,
725 g_param_spec_int ("bottom-attach",
727 P_("The row number to attach the bottom of the child to"),
729 GTK_PARAM_READWRITE));
732 * GtkMenu::arrow-scaling
734 * Arbitrary constant to scale down the size of the scroll arrow.
738 gtk_widget_class_install_style_property (widget_class,
739 g_param_spec_float ("arrow-scaling",
741 P_("Arbitrary constant to scale down the size of the scroll arrow"),
743 GTK_PARAM_READABLE));
745 binding_set = gtk_binding_set_by_class (class);
746 gtk_binding_entry_add_signal (binding_set,
748 I_("move-current"), 1,
749 GTK_TYPE_MENU_DIRECTION_TYPE,
751 gtk_binding_entry_add_signal (binding_set,
754 GTK_TYPE_MENU_DIRECTION_TYPE,
756 gtk_binding_entry_add_signal (binding_set,
759 GTK_TYPE_MENU_DIRECTION_TYPE,
761 gtk_binding_entry_add_signal (binding_set,
764 GTK_TYPE_MENU_DIRECTION_TYPE,
766 gtk_binding_entry_add_signal (binding_set,
769 GTK_TYPE_MENU_DIRECTION_TYPE,
770 GTK_MENU_DIR_PARENT);
771 gtk_binding_entry_add_signal (binding_set,
774 GTK_TYPE_MENU_DIRECTION_TYPE,
775 GTK_MENU_DIR_PARENT);
776 gtk_binding_entry_add_signal (binding_set,
779 GTK_TYPE_MENU_DIRECTION_TYPE,
781 gtk_binding_entry_add_signal (binding_set,
784 GTK_TYPE_MENU_DIRECTION_TYPE,
786 gtk_binding_entry_add_signal (binding_set,
789 GTK_TYPE_SCROLL_TYPE,
791 gtk_binding_entry_add_signal (binding_set,
794 GTK_TYPE_SCROLL_TYPE,
796 gtk_binding_entry_add_signal (binding_set,
799 GTK_TYPE_SCROLL_TYPE,
801 gtk_binding_entry_add_signal (binding_set,
804 GTK_TYPE_SCROLL_TYPE,
806 gtk_binding_entry_add_signal (binding_set,
809 GTK_TYPE_SCROLL_TYPE,
811 gtk_binding_entry_add_signal (binding_set,
812 GDK_KEY_KP_Page_Up, 0,
814 GTK_TYPE_SCROLL_TYPE,
816 gtk_binding_entry_add_signal (binding_set,
817 GDK_KEY_Page_Down, 0,
819 GTK_TYPE_SCROLL_TYPE,
820 GTK_SCROLL_PAGE_DOWN);
821 gtk_binding_entry_add_signal (binding_set,
822 GDK_KEY_KP_Page_Down, 0,
824 GTK_TYPE_SCROLL_TYPE,
825 GTK_SCROLL_PAGE_DOWN);
827 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
828 P_("Can change accelerators"),
829 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
831 GTK_PARAM_READWRITE));
833 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
834 P_("Delay before submenus appear"),
835 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
839 GTK_PARAM_READWRITE));
841 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
842 P_("Delay before hiding a submenu"),
843 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
846 DEFAULT_POPDOWN_DELAY,
847 GTK_PARAM_READWRITE));
849 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
854 gtk_menu_set_property (GObject *object,
859 GtkMenu *menu = GTK_MENU (object);
864 gtk_menu_set_active (menu, g_value_get_int (value));
866 case PROP_ACCEL_GROUP:
867 gtk_menu_set_accel_group (menu, g_value_get_object (value));
869 case PROP_ACCEL_PATH:
870 gtk_menu_set_accel_path (menu, g_value_get_string (value));
872 case PROP_ATTACH_WIDGET:
876 widget = gtk_menu_get_attach_widget (menu);
878 gtk_menu_detach (menu);
880 widget = (GtkWidget*) g_value_get_object (value);
882 gtk_menu_attach_to_widget (menu, widget, NULL);
885 case PROP_TEAROFF_STATE:
886 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
888 case PROP_TEAROFF_TITLE:
889 gtk_menu_set_title (menu, g_value_get_string (value));
892 gtk_menu_set_monitor (menu, g_value_get_int (value));
894 case PROP_RESERVE_TOGGLE_SIZE:
895 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
898 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
904 gtk_menu_get_property (GObject *object,
909 GtkMenu *menu = GTK_MENU (object);
914 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
916 case PROP_ACCEL_GROUP:
917 g_value_set_object (value, gtk_menu_get_accel_group (menu));
919 case PROP_ACCEL_PATH:
920 g_value_set_string (value, gtk_menu_get_accel_path (menu));
922 case PROP_ATTACH_WIDGET:
923 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
925 case PROP_TEAROFF_STATE:
926 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
928 case PROP_TEAROFF_TITLE:
929 g_value_set_string (value, gtk_menu_get_title (menu));
932 g_value_set_int (value, gtk_menu_get_monitor (menu));
934 case PROP_RESERVE_TOGGLE_SIZE:
935 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
938 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
944 gtk_menu_set_child_property (GtkContainer *container,
950 GtkMenu *menu = GTK_MENU (container);
951 AttachInfo *ai = get_attach_info (child);
955 case CHILD_PROP_LEFT_ATTACH:
956 ai->left_attach = g_value_get_int (value);
958 case CHILD_PROP_RIGHT_ATTACH:
959 ai->right_attach = g_value_get_int (value);
961 case CHILD_PROP_TOP_ATTACH:
962 ai->top_attach = g_value_get_int (value);
964 case CHILD_PROP_BOTTOM_ATTACH:
965 ai->bottom_attach = g_value_get_int (value);
969 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
973 menu_queue_resize (menu);
977 gtk_menu_get_child_property (GtkContainer *container,
983 AttachInfo *ai = get_attach_info (child);
987 case CHILD_PROP_LEFT_ATTACH:
988 g_value_set_int (value, ai->left_attach);
990 case CHILD_PROP_RIGHT_ATTACH:
991 g_value_set_int (value, ai->right_attach);
993 case CHILD_PROP_TOP_ATTACH:
994 g_value_set_int (value, ai->top_attach);
996 case CHILD_PROP_BOTTOM_ATTACH:
997 g_value_set_int (value, ai->bottom_attach);
1001 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1007 gtk_menu_window_event (GtkWidget *window,
1011 gboolean handled = FALSE;
1013 g_object_ref (window);
1014 g_object_ref (menu);
1016 switch (event->type)
1019 case GDK_KEY_RELEASE:
1020 handled = gtk_widget_event (menu, event);
1026 g_object_unref (window);
1027 g_object_unref (menu);
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::destroy", gtk_widget_destroyed, &menu->toplevel,
1051 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1052 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1054 /* Refloat the menu, so that reference counting for the menu isn't
1055 * affected by it being a child of the toplevel
1057 g_object_force_floating (G_OBJECT (menu));
1058 menu->needs_destruction_ref_count = TRUE;
1060 menu->view_window = NULL;
1061 menu->bin_window = NULL;
1063 menu->scroll_offset = 0;
1064 menu->scroll_step = 0;
1065 menu->timeout_id = 0;
1066 menu->scroll_fast = FALSE;
1068 menu->tearoff_window = NULL;
1069 menu->tearoff_hbox = NULL;
1070 menu->torn_off = FALSE;
1071 menu->tearoff_active = FALSE;
1072 menu->tearoff_adjustment = NULL;
1073 menu->tearoff_scrollbar = NULL;
1075 menu->upper_arrow_visible = FALSE;
1076 menu->lower_arrow_visible = FALSE;
1077 menu->upper_arrow_prelight = FALSE;
1078 menu->lower_arrow_prelight = FALSE;
1080 priv->upper_arrow_state = GTK_STATE_NORMAL;
1081 priv->lower_arrow_state = GTK_STATE_NORMAL;
1083 priv->have_layout = FALSE;
1084 priv->monitor_num = -1;
1088 gtk_menu_destroy (GtkObject *object)
1090 GtkMenu *menu = GTK_MENU (object);
1091 GtkMenuAttachData *data;
1092 GtkMenuPrivate *priv;
1094 gtk_menu_remove_scroll_timeout (menu);
1096 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1098 gtk_menu_detach (menu);
1100 gtk_menu_stop_navigating_submenu (menu);
1102 if (menu->old_active_menu_item)
1104 g_object_unref (menu->old_active_menu_item);
1105 menu->old_active_menu_item = NULL;
1108 /* Add back the reference count for being a child */
1109 if (menu->needs_destruction_ref_count)
1111 menu->needs_destruction_ref_count = FALSE;
1112 g_object_ref (object);
1115 if (menu->accel_group)
1117 g_object_unref (menu->accel_group);
1118 menu->accel_group = NULL;
1122 gtk_widget_destroy (menu->toplevel);
1124 if (menu->tearoff_window)
1125 gtk_widget_destroy (menu->tearoff_window);
1127 priv = gtk_menu_get_private (menu);
1131 g_free (priv->heights);
1132 priv->heights = NULL;
1137 g_free (priv->title);
1141 if (priv->position_func_data_destroy)
1143 priv->position_func_data_destroy (menu->position_func_data);
1144 menu->position_func_data = NULL;
1145 priv->position_func_data_destroy = NULL;
1148 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1152 menu_change_screen (GtkMenu *menu,
1153 GdkScreen *new_screen)
1155 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1157 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1159 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1165 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1166 gtk_menu_position (menu);
1169 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1170 private->monitor_num = -1;
1174 attach_widget_screen_changed (GtkWidget *attach_widget,
1175 GdkScreen *previous_screen,
1178 if (gtk_widget_has_screen (attach_widget) &&
1179 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1181 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1186 gtk_menu_attach_to_widget (GtkMenu *menu,
1187 GtkWidget *attach_widget,
1188 GtkMenuDetachFunc detacher)
1190 GtkMenuAttachData *data;
1193 g_return_if_fail (GTK_IS_MENU (menu));
1194 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1196 /* keep this function in sync with gtk_widget_set_parent()
1199 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1202 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1203 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1207 g_object_ref_sink (menu);
1209 data = g_slice_new (GtkMenuAttachData);
1210 data->attach_widget = attach_widget;
1212 g_signal_connect (attach_widget, "screen-changed",
1213 G_CALLBACK (attach_widget_screen_changed), menu);
1214 attach_widget_screen_changed (attach_widget, NULL, menu);
1216 data->detacher = detacher;
1217 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1218 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1219 if (!g_list_find (list, menu))
1221 list = g_list_prepend (list, menu);
1223 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1224 (GDestroyNotify) g_list_free);
1226 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1227 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1229 /* we don't need to set the style here, since
1230 * we are a toplevel widget.
1233 /* Fallback title for menu comes from attach widget */
1234 gtk_menu_update_title (menu);
1236 g_object_notify (G_OBJECT (menu), "attach-widget");
1240 gtk_menu_get_attach_widget (GtkMenu *menu)
1242 GtkMenuAttachData *data;
1244 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1246 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1248 return data->attach_widget;
1253 gtk_menu_detach (GtkMenu *menu)
1255 GtkMenuAttachData *data;
1258 g_return_if_fail (GTK_IS_MENU (menu));
1260 /* keep this function in sync with gtk_widget_unparent()
1262 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1265 g_warning ("gtk_menu_detach(): menu is not attached");
1268 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1270 g_signal_handlers_disconnect_by_func (data->attach_widget,
1271 (gpointer) attach_widget_screen_changed,
1275 data->detacher (data->attach_widget, menu);
1276 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1277 list = g_list_remove (list, menu);
1279 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1280 (GDestroyNotify) g_list_free);
1282 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1284 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1285 gtk_widget_unrealize (GTK_WIDGET (menu));
1287 g_slice_free (GtkMenuAttachData, data);
1289 /* Fallback title for menu comes from attach widget */
1290 gtk_menu_update_title (menu);
1292 g_object_unref (menu);
1296 gtk_menu_remove (GtkContainer *container,
1299 GtkMenu *menu = GTK_MENU (container);
1301 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1303 /* Clear out old_active_menu_item if it matches the item we are removing
1305 if (menu->old_active_menu_item == widget)
1307 g_object_unref (menu->old_active_menu_item);
1308 menu->old_active_menu_item = NULL;
1311 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1312 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1314 menu_queue_resize (menu);
1320 return g_object_new (GTK_TYPE_MENU, NULL);
1324 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1328 GtkMenu *menu = GTK_MENU (menu_shell);
1329 AttachInfo *ai = get_attach_info (child);
1331 ai->left_attach = -1;
1332 ai->right_attach = -1;
1333 ai->top_attach = -1;
1334 ai->bottom_attach = -1;
1336 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1337 gtk_widget_set_parent_window (child, menu->bin_window);
1339 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1341 menu_queue_resize (menu);
1345 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1350 widget = GTK_WIDGET (menu);
1355 cairo_surface_t *surface;
1356 cairo_pattern_t *pattern;
1359 menu->tearoff_active = FALSE;
1360 menu->saved_scroll_offset = menu->scroll_offset;
1362 window = gtk_widget_get_window (menu->tearoff_window);
1364 gdk_drawable_get_size (window, &width, &height);
1366 surface = gdk_window_create_similar_surface (window,
1367 CAIRO_CONTENT_COLOR,
1371 cr = cairo_create (surface);
1372 gdk_cairo_set_source_window (cr,
1378 gtk_widget_set_size_request (menu->tearoff_window,
1382 pattern = cairo_pattern_create_for_surface (surface);
1383 gdk_window_set_background_pattern (window, pattern);
1385 cairo_pattern_destroy (pattern);
1386 cairo_surface_destroy (surface);
1391 popup_grab_on_window (GdkWindow *window,
1392 GdkDevice *keyboard,
1394 guint32 activate_time)
1397 gdk_device_grab (keyboard, window,
1398 GDK_OWNERSHIP_WINDOW, TRUE,
1399 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1400 NULL, activate_time) != GDK_GRAB_SUCCESS)
1404 gdk_device_grab (pointer, window,
1405 GDK_OWNERSHIP_WINDOW, TRUE,
1406 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1407 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1408 GDK_POINTER_MOTION_MASK,
1409 NULL, activate_time) != GDK_GRAB_SUCCESS)
1412 gdk_device_ungrab (keyboard, activate_time);
1421 * gtk_menu_popup_for_device:
1422 * @menu: a #GtkMenu.
1423 * @device: (allow-none): a #GdkDevice
1424 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1425 * menu item, or %NULL
1426 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1427 * the popup, or %NULL
1428 * @func: (allow-none): a user supplied function used to position the menu,
1430 * @data: (allow-none): user supplied data to be passed to @func
1431 * @destroy: (allow-none): destroy notify for @data
1432 * @button: the mouse button which was pressed to initiate the event
1433 * @activate_time: the time at which the activation event occurred
1435 * Displays a menu and makes it available for selection.
1437 * Applications can use this function to display context-sensitive menus,
1438 * and will typically supply %NULL for the @parent_menu_shell,
1439 * @parent_menu_item, @func, @data and @destroy parameters. The default
1440 * menu positioning function will position the menu at the current position
1441 * of @device (or its corresponding pointer).
1443 * The @button parameter should be the mouse button pressed to initiate
1444 * the menu popup. If the menu popup was initiated by something other than
1445 * a mouse button press, such as a mouse button release or a keypress,
1446 * @button should be 0.
1448 * The @activate_time parameter is used to conflict-resolve initiation of
1449 * concurrent requests for mouse/keyboard grab requests. To function
1450 * properly, this needs to be the time stamp of the user event (such as
1451 * a mouse click or key press) that caused the initiation of the popup.
1452 * Only if no such event is available, gtk_get_current_event_time() can
1458 gtk_menu_popup_for_device (GtkMenu *menu,
1460 GtkWidget *parent_menu_shell,
1461 GtkWidget *parent_menu_item,
1462 GtkMenuPositionFunc func,
1464 GDestroyNotify destroy,
1466 guint32 activate_time)
1469 GtkWidget *xgrab_shell;
1471 GdkEvent *current_event;
1472 GtkMenuShell *menu_shell;
1473 gboolean grab_keyboard;
1474 GtkMenuPrivate *priv;
1475 GtkWidget *parent_toplevel;
1476 GdkDevice *keyboard, *pointer;
1478 g_return_if_fail (GTK_IS_MENU (menu));
1479 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1482 device = gtk_get_current_event_device ();
1486 GdkDisplay *display;
1487 GdkDeviceManager *device_manager;
1490 display = gtk_widget_get_display (GTK_WIDGET (menu));
1491 device_manager = gdk_display_get_device_manager (display);
1492 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1494 device = devices->data;
1496 g_list_free (devices);
1499 widget = GTK_WIDGET (menu);
1500 menu_shell = GTK_MENU_SHELL (menu);
1501 priv = gtk_menu_get_private (menu);
1503 if (device->source == GDK_SOURCE_KEYBOARD)
1506 pointer = gdk_device_get_associated_device (device);
1511 keyboard = gdk_device_get_associated_device (device);
1514 menu_shell->parent_menu_shell = parent_menu_shell;
1516 priv->seen_item_enter = FALSE;
1518 /* Find the last viewable ancestor, and make an X grab on it
1520 parent = GTK_WIDGET (menu);
1524 gboolean viewable = TRUE;
1525 GtkWidget *tmp = parent;
1529 if (!gtk_widget_get_mapped (tmp))
1534 tmp = gtk_widget_get_parent (tmp);
1538 xgrab_shell = parent;
1540 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1543 /* We want to receive events generated when we map the menu; unfortunately,
1544 * since there is probably already an implicit grab in place from the
1545 * button that the user used to pop up the menu, we won't receive then --
1546 * in particular, the EnterNotify when the menu pops up under the pointer.
1548 * If we are grabbing on a parent menu shell, no problem; just grab on
1549 * that menu shell first before popping up the window with owner_events = TRUE.
1551 * When grabbing on the menu itself, things get more convuluted - we
1552 * we do an explicit grab on a specially created window with
1553 * owner_events = TRUE, which we override further down with a grab
1554 * on the menu. (We can't grab on the menu until it is mapped; we
1555 * probably could just leave the grab on the other window, with a
1556 * little reorganization of the code in gtkmenu*).
1558 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1559 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1564 if (xgrab_shell && xgrab_shell != widget)
1566 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1568 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1569 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1574 GdkWindow *transfer_window;
1576 xgrab_shell = widget;
1577 transfer_window = menu_grab_transfer_window_get (menu);
1578 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1580 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1581 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1585 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1587 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1588 * with a stuck up window, we just abort here. Presumably the user will
1591 menu_shell->parent_menu_shell = NULL;
1592 menu_grab_transfer_window_destroy (menu);
1596 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1597 menu_shell->active = TRUE;
1598 menu_shell->button = button;
1600 /* If we are popping up the menu from something other than, a button
1601 * press then, as a heuristic, we ignore enter events for the menu
1602 * until we get a MOTION_NOTIFY.
1605 current_event = gtk_get_current_event ();
1608 if ((current_event->type != GDK_BUTTON_PRESS) &&
1609 (current_event->type != GDK_ENTER_NOTIFY))
1610 menu_shell->ignore_enter = TRUE;
1612 gdk_event_free (current_event);
1615 menu_shell->ignore_enter = TRUE;
1619 gtk_menu_tearoff_bg_copy (menu);
1621 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1624 parent_toplevel = NULL;
1625 if (parent_menu_shell)
1626 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1627 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1629 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1631 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1634 /* Set transient for to get the right window group and parent relationship */
1635 if (GTK_IS_WINDOW (parent_toplevel))
1636 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1637 GTK_WINDOW (parent_toplevel));
1639 menu->parent_menu_item = parent_menu_item;
1640 menu->position_func = func;
1641 menu->position_func_data = data;
1642 priv->position_func_data_destroy = destroy;
1643 menu_shell->activate_time = activate_time;
1645 /* We need to show the menu here rather in the init function because
1646 * code expects to be able to tell if the menu is onscreen by
1647 * looking at the gtk_widget_get_visible (menu)
1649 gtk_widget_show (GTK_WIDGET (menu));
1651 /* Position the menu, possibly changing the size request
1653 gtk_menu_position (menu);
1655 /* Compute the size of the toplevel and realize it so we
1656 * can scroll correctly.
1659 GtkRequisition tmp_request;
1660 GtkAllocation tmp_allocation = { 0, };
1662 /* Instead of trusting the menu position function to queue a resize when the
1663 * menu goes out of bounds, invalidate the cached size here. */
1664 gtk_widget_queue_resize (GTK_WIDGET (menu));
1665 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->toplevel), &tmp_request, NULL);
1667 tmp_allocation.width = tmp_request.width;
1668 tmp_allocation.height = tmp_request.height;
1670 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1672 gtk_widget_realize (GTK_WIDGET (menu));
1675 gtk_menu_scroll_to (menu, menu->scroll_offset);
1677 /* if no item is selected, select the first one */
1678 if (!menu_shell->active_menu_item)
1680 gboolean touchscreen_mode;
1682 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1683 "gtk-touchscreen-mode", &touchscreen_mode,
1686 if (touchscreen_mode)
1687 gtk_menu_shell_select_first (menu_shell, TRUE);
1690 /* Once everything is set up correctly, map the toplevel window on
1693 gtk_widget_show (menu->toplevel);
1695 if (xgrab_shell == widget)
1696 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1698 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1700 if (parent_menu_shell)
1702 gboolean keyboard_mode;
1704 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1705 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1707 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1708 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1710 _gtk_menu_shell_update_mnemonics (menu_shell);
1715 * @menu: a #GtkMenu.
1716 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1717 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1718 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1719 * @data: (allow-none): user supplied data to be passed to @func.
1720 * @button: the mouse button which was pressed to initiate the event.
1721 * @activate_time: the time at which the activation event occurred.
1723 * Displays a menu and makes it available for selection. Applications can use
1724 * this function to display context-sensitive menus, and will typically supply
1725 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1726 * parameters. The default menu positioning function will position the menu
1727 * at the current mouse cursor position.
1729 * The @button parameter should be the mouse button pressed to initiate
1730 * the menu popup. If the menu popup was initiated by something other than
1731 * a mouse button press, such as a mouse button release or a keypress,
1732 * @button should be 0.
1734 * The @activate_time parameter is used to conflict-resolve initiation of
1735 * concurrent requests for mouse/keyboard grab requests. To function
1736 * properly, this needs to be the time stamp of the user event (such as
1737 * a mouse click or key press) that caused the initiation of the popup.
1738 * Only if no such event is available, gtk_get_current_event_time() can
1742 gtk_menu_popup (GtkMenu *menu,
1743 GtkWidget *parent_menu_shell,
1744 GtkWidget *parent_menu_item,
1745 GtkMenuPositionFunc func,
1748 guint32 activate_time)
1750 g_return_if_fail (GTK_IS_MENU (menu));
1752 gtk_menu_popup_for_device (menu,
1757 button, activate_time);
1761 gtk_menu_popdown (GtkMenu *menu)
1763 GtkMenuPrivate *private;
1764 GtkMenuShell *menu_shell;
1767 g_return_if_fail (GTK_IS_MENU (menu));
1769 menu_shell = GTK_MENU_SHELL (menu);
1770 private = gtk_menu_get_private (menu);
1772 menu_shell->parent_menu_shell = NULL;
1773 menu_shell->active = FALSE;
1774 menu_shell->ignore_enter = FALSE;
1776 private->have_position = FALSE;
1778 gtk_menu_stop_scrolling (menu);
1780 gtk_menu_stop_navigating_submenu (menu);
1782 if (menu_shell->active_menu_item)
1784 if (menu->old_active_menu_item)
1785 g_object_unref (menu->old_active_menu_item);
1786 menu->old_active_menu_item = menu_shell->active_menu_item;
1787 g_object_ref (menu->old_active_menu_item);
1790 gtk_menu_shell_deselect (menu_shell);
1792 /* The X Grab, if present, will automatically be removed when we hide
1794 gtk_widget_hide (menu->toplevel);
1795 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1797 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1801 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1803 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1805 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1809 /* We popped up the menu from the tearoff, so we need to
1810 * release the grab - we aren't actually hiding the menu.
1812 if (menu_shell->have_xgrab && pointer)
1814 GdkDevice *keyboard;
1816 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1817 keyboard = gdk_device_get_associated_device (pointer);
1820 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1824 /* gtk_menu_popdown is called each time a menu item is selected from
1825 * a torn off menu. Only scroll back to the saved position if the
1826 * non-tearoff menu was popped down.
1828 if (!menu->tearoff_active)
1829 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1830 menu->tearoff_active = TRUE;
1833 gtk_widget_hide (GTK_WIDGET (menu));
1835 menu_shell->have_xgrab = FALSE;
1838 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1840 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1842 menu_grab_transfer_window_destroy (menu);
1846 gtk_menu_get_active (GtkMenu *menu)
1851 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1853 if (!menu->old_active_menu_item)
1856 children = GTK_MENU_SHELL (menu)->children;
1860 child = children->data;
1861 children = children->next;
1863 if (gtk_bin_get_child (GTK_BIN (child)))
1868 menu->old_active_menu_item = child;
1869 if (menu->old_active_menu_item)
1870 g_object_ref (menu->old_active_menu_item);
1873 return menu->old_active_menu_item;
1877 gtk_menu_set_active (GtkMenu *menu,
1883 g_return_if_fail (GTK_IS_MENU (menu));
1885 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1888 child = tmp_list->data;
1889 if (gtk_bin_get_child (GTK_BIN (child)))
1891 if (menu->old_active_menu_item)
1892 g_object_unref (menu->old_active_menu_item);
1893 menu->old_active_menu_item = child;
1894 g_object_ref (menu->old_active_menu_item);
1901 * gtk_menu_set_accel_group:
1902 * @accel_group: (allow-none):
1905 gtk_menu_set_accel_group (GtkMenu *menu,
1906 GtkAccelGroup *accel_group)
1908 g_return_if_fail (GTK_IS_MENU (menu));
1910 if (menu->accel_group != accel_group)
1912 if (menu->accel_group)
1913 g_object_unref (menu->accel_group);
1914 menu->accel_group = accel_group;
1915 if (menu->accel_group)
1916 g_object_ref (menu->accel_group);
1917 _gtk_menu_refresh_accel_paths (menu, TRUE);
1922 gtk_menu_get_accel_group (GtkMenu *menu)
1924 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1926 return menu->accel_group;
1930 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1933 /* Menu items chain here to figure whether they can activate their
1934 * accelerators. Unlike ordinary widgets, menus allow accel
1935 * activation even if invisible since that's the usual case for
1936 * submenus/popup-menus. however, the state of the attach widget
1937 * affects the "activeness" of the menu.
1939 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1942 return gtk_widget_can_activate_accel (awidget, signal_id);
1944 return gtk_widget_is_sensitive (widget);
1948 * gtk_menu_set_accel_path
1949 * @menu: a valid #GtkMenu
1950 * @accel_path: (allow-none): a valid accelerator path
1952 * Sets an accelerator path for this menu from which accelerator paths
1953 * for its immediate children, its menu items, can be constructed.
1954 * The main purpose of this function is to spare the programmer the
1955 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1956 * each menu item that should support runtime user changable accelerators.
1957 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1958 * each menu item of this menu, that contains a label describing its purpose,
1959 * automatically gets an accel path assigned. For example, a menu containing
1960 * menu items "New" and "Exit", will, after
1961 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1962 * has been called, assign its items the accel paths:
1963 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1964 * Assigning accel paths to menu items then enables the user to change
1965 * their accelerators at runtime. More details about accelerator paths
1966 * and their default setups can be found at gtk_accel_map_add_entry().
1968 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1969 * pass a static string, you can save some memory by interning it first with
1970 * g_intern_static_string().
1973 gtk_menu_set_accel_path (GtkMenu *menu,
1974 const gchar *accel_path)
1976 g_return_if_fail (GTK_IS_MENU (menu));
1978 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1980 /* FIXME: accel_path should be defined as const gchar* */
1981 menu->accel_path = (gchar*)g_intern_string (accel_path);
1982 if (menu->accel_path)
1983 _gtk_menu_refresh_accel_paths (menu, FALSE);
1987 * gtk_menu_get_accel_path
1988 * @menu: a valid #GtkMenu
1990 * Retrieves the accelerator path set on the menu.
1992 * Returns: the accelerator path set on the menu.
1997 gtk_menu_get_accel_path (GtkMenu *menu)
1999 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2001 return menu->accel_path;
2006 gboolean group_changed;
2010 refresh_accel_paths_foreach (GtkWidget *widget,
2013 AccelPropagation *prop = data;
2015 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2016 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2017 prop->menu->accel_path,
2018 prop->menu->accel_group,
2019 prop->group_changed);
2023 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2024 gboolean group_changed)
2026 g_return_if_fail (GTK_IS_MENU (menu));
2028 if (menu->accel_path && menu->accel_group)
2030 AccelPropagation prop;
2033 prop.group_changed = group_changed;
2034 gtk_container_foreach (GTK_CONTAINER (menu),
2035 refresh_accel_paths_foreach,
2041 gtk_menu_reposition (GtkMenu *menu)
2043 g_return_if_fail (GTK_IS_MENU (menu));
2045 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2046 gtk_menu_position (menu);
2050 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2053 g_return_if_fail (GTK_IS_MENU (menu));
2055 if (adjustment->value != menu->scroll_offset)
2056 gtk_menu_scroll_to (menu, adjustment->value);
2060 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2063 GdkGeometry geometry_hints;
2064 GtkMenuPrivate *priv;
2066 if (!menu->tearoff_window)
2069 priv = gtk_menu_get_private (menu);
2071 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2073 GtkRequisition requisition;
2075 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->tearoff_scrollbar),
2076 &requisition, NULL);
2077 width += requisition.width;
2080 geometry_hints.min_width = width;
2081 geometry_hints.max_width = width;
2083 geometry_hints.min_height = 0;
2084 geometry_hints.max_height = priv->requested_height;
2086 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2089 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2093 gtk_menu_update_title (GtkMenu *menu)
2095 if (menu->tearoff_window)
2098 GtkWidget *attach_widget;
2100 title = gtk_menu_get_title (menu);
2103 attach_widget = gtk_menu_get_attach_widget (menu);
2104 if (GTK_IS_MENU_ITEM (attach_widget))
2106 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2107 if (GTK_IS_LABEL (child))
2108 title = gtk_label_get_text (GTK_LABEL (child));
2113 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2118 gtk_menu_get_toplevel (GtkWidget *menu)
2120 GtkWidget *attach, *toplevel;
2122 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2124 if (GTK_IS_MENU_ITEM (attach))
2125 attach = gtk_widget_get_parent (attach);
2127 if (GTK_IS_MENU (attach))
2128 return gtk_menu_get_toplevel (attach);
2129 else if (GTK_IS_WIDGET (attach))
2131 toplevel = gtk_widget_get_toplevel (attach);
2132 if (gtk_widget_is_toplevel (toplevel))
2140 tearoff_window_destroyed (GtkWidget *widget,
2143 gtk_menu_set_tearoff_state (menu, FALSE);
2147 gtk_menu_set_tearoff_state (GtkMenu *menu,
2151 GtkMenuPrivate *priv;
2153 g_return_if_fail (GTK_IS_MENU (menu));
2155 priv = gtk_menu_get_private (menu);
2157 if (menu->torn_off != torn_off)
2159 menu->torn_off = torn_off;
2160 menu->tearoff_active = torn_off;
2164 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2165 gtk_menu_popdown (menu);
2167 if (!menu->tearoff_window)
2169 GtkWidget *toplevel;
2171 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2172 "type", GTK_WINDOW_TOPLEVEL,
2173 "screen", gtk_widget_get_screen (menu->toplevel),
2174 "app-paintable", TRUE,
2177 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2178 GDK_WINDOW_TYPE_HINT_MENU);
2179 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2180 g_signal_connect (menu->tearoff_window, "destroy",
2181 G_CALLBACK (tearoff_window_destroyed), menu);
2182 g_signal_connect (menu->tearoff_window, "event",
2183 G_CALLBACK (gtk_menu_window_event), menu);
2185 gtk_menu_update_title (menu);
2187 gtk_widget_realize (menu->tearoff_window);
2189 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2190 if (toplevel != NULL)
2191 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2192 GTK_WINDOW (toplevel));
2194 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2195 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2197 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2199 menu->tearoff_adjustment =
2200 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2202 priv->requested_height,
2206 g_object_connect (menu->tearoff_adjustment,
2207 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2209 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2211 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2212 menu->tearoff_scrollbar,
2215 if (menu->tearoff_adjustment->upper > height)
2216 gtk_widget_show (menu->tearoff_scrollbar);
2218 gtk_widget_show (menu->tearoff_hbox);
2221 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2223 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2226 /* Update menu->requisition
2228 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu),
2231 gtk_menu_set_tearoff_hints (menu, width);
2233 gtk_widget_realize (menu->tearoff_window);
2234 gtk_menu_position (menu);
2236 gtk_widget_show (GTK_WIDGET (menu));
2237 gtk_widget_show (menu->tearoff_window);
2239 gtk_menu_scroll_to (menu, 0);
2244 gtk_widget_hide (GTK_WIDGET (menu));
2245 gtk_widget_hide (menu->tearoff_window);
2246 if (GTK_IS_CONTAINER (menu->toplevel))
2247 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2248 gtk_widget_destroy (menu->tearoff_window);
2250 menu->tearoff_window = NULL;
2251 menu->tearoff_hbox = NULL;
2252 menu->tearoff_scrollbar = NULL;
2253 menu->tearoff_adjustment = NULL;
2256 g_object_notify (G_OBJECT (menu), "tearoff-state");
2261 * gtk_menu_get_tearoff_state:
2264 * Returns whether the menu is torn off. See
2265 * gtk_menu_set_tearoff_state ().
2267 * Return value: %TRUE if the menu is currently torn off.
2270 gtk_menu_get_tearoff_state (GtkMenu *menu)
2272 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2274 return menu->torn_off;
2278 * gtk_menu_set_title:
2280 * @title: a string containing the title for the menu.
2282 * Sets the title string for the menu. The title is displayed when the menu
2283 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2284 * attached to a parent menu item, and if so it will try to use the same text as
2285 * that menu item's label.
2288 gtk_menu_set_title (GtkMenu *menu,
2291 GtkMenuPrivate *priv;
2294 g_return_if_fail (GTK_IS_MENU (menu));
2296 priv = gtk_menu_get_private (menu);
2298 old_title = priv->title;
2299 priv->title = g_strdup (title);
2302 gtk_menu_update_title (menu);
2303 g_object_notify (G_OBJECT (menu), "tearoff-title");
2307 * gtk_menu_get_title:
2310 * Returns the title of the menu. See gtk_menu_set_title().
2312 * Return value: the title of the menu, or %NULL if the menu has no
2313 * title set on it. This string is owned by the widget and should
2314 * not be modified or freed.
2316 G_CONST_RETURN gchar *
2317 gtk_menu_get_title (GtkMenu *menu)
2319 GtkMenuPrivate *priv;
2321 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2323 priv = gtk_menu_get_private (menu);
2329 gtk_menu_reorder_child (GtkMenu *menu,
2333 GtkMenuShell *menu_shell;
2335 g_return_if_fail (GTK_IS_MENU (menu));
2336 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2338 menu_shell = GTK_MENU_SHELL (menu);
2340 if (g_list_find (menu_shell->children, child))
2342 menu_shell->children = g_list_remove (menu_shell->children, child);
2343 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2345 menu_queue_resize (menu);
2350 gtk_menu_style_set (GtkWidget *widget,
2351 GtkStyle *previous_style)
2353 if (gtk_widget_get_realized (widget))
2355 GtkMenu *menu = GTK_MENU (widget);
2358 style = gtk_widget_get_style (widget);
2360 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2361 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2362 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2367 get_arrows_border (GtkMenu *menu,
2370 guint scroll_arrow_height;
2371 GtkArrowPlacement arrow_placement;
2373 gtk_widget_style_get (GTK_WIDGET (menu),
2374 "scroll-arrow-vlength", &scroll_arrow_height,
2375 "arrow_placement", &arrow_placement,
2378 switch (arrow_placement)
2380 case GTK_ARROWS_BOTH:
2381 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2382 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2385 case GTK_ARROWS_START:
2386 border->top = (menu->upper_arrow_visible ||
2387 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2391 case GTK_ARROWS_END:
2393 border->bottom = (menu->upper_arrow_visible ||
2394 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2398 border->left = border->right = 0;
2402 gtk_menu_realize (GtkWidget *widget)
2404 GtkAllocation allocation;
2407 GdkWindowAttr attributes;
2408 gint attributes_mask;
2411 GtkMenuPrivate *priv;
2414 guint vertical_padding;
2415 guint horizontal_padding;
2416 GtkBorder arrow_border;
2418 g_return_if_fail (GTK_IS_MENU (widget));
2420 menu = GTK_MENU (widget);
2421 priv = gtk_menu_get_private (menu);
2423 gtk_widget_set_realized (widget, TRUE);
2425 gtk_widget_get_allocation (widget, &allocation);
2427 attributes.window_type = GDK_WINDOW_CHILD;
2428 attributes.x = allocation.x;
2429 attributes.y = allocation.y;
2430 attributes.width = allocation.width;
2431 attributes.height = allocation.height;
2432 attributes.wclass = GDK_INPUT_OUTPUT;
2433 attributes.visual = gtk_widget_get_visual (widget);
2434 attributes.colormap = gtk_widget_get_colormap (widget);
2435 attributes.event_mask = gtk_widget_get_events (widget);
2436 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2437 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2439 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2441 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2442 &attributes, attributes_mask);
2443 gtk_widget_set_window (widget, window);
2444 gdk_window_set_user_data (window, widget);
2446 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2448 style = gtk_widget_get_style (widget);
2449 gtk_widget_style_get (GTK_WIDGET (menu),
2450 "vertical-padding", &vertical_padding,
2451 "horizontal-padding", &horizontal_padding,
2454 gtk_widget_get_allocation (widget, &allocation);
2456 attributes.x = border_width + style->xthickness + horizontal_padding;
2457 attributes.y = border_width + style->ythickness + vertical_padding;
2458 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2459 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2461 get_arrows_border (menu, &arrow_border);
2462 attributes.y += arrow_border.top;
2463 attributes.height -= arrow_border.top;
2464 attributes.height -= arrow_border.bottom;
2466 menu->view_window = gdk_window_new (window,
2467 &attributes, attributes_mask);
2468 gdk_window_set_user_data (menu->view_window, menu);
2470 gtk_widget_get_allocation (widget, &allocation);
2474 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2475 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2477 menu->bin_window = gdk_window_new (menu->view_window,
2478 &attributes, attributes_mask);
2479 gdk_window_set_user_data (menu->bin_window, menu);
2481 children = GTK_MENU_SHELL (menu)->children;
2484 child = children->data;
2485 children = children->next;
2487 gtk_widget_set_parent_window (child, menu->bin_window);
2490 gtk_widget_style_attach (widget);
2491 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2492 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2493 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2495 if (GTK_MENU_SHELL (widget)->active_menu_item)
2496 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2497 GTK_MENU_SHELL (widget)->active_menu_item);
2499 gdk_window_show (menu->bin_window);
2500 gdk_window_show (menu->view_window);
2504 gtk_menu_focus (GtkWidget *widget,
2505 GtkDirectionType direction)
2508 * A menu or its menu items cannot have focus
2513 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2516 menu_grab_transfer_window_get (GtkMenu *menu)
2518 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2521 GdkWindowAttr attributes;
2522 gint attributes_mask;
2524 attributes.x = -100;
2525 attributes.y = -100;
2526 attributes.width = 10;
2527 attributes.height = 10;
2528 attributes.window_type = GDK_WINDOW_TEMP;
2529 attributes.wclass = GDK_INPUT_ONLY;
2530 attributes.override_redirect = TRUE;
2531 attributes.event_mask = 0;
2533 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2535 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2536 &attributes, attributes_mask);
2537 gdk_window_set_user_data (window, menu);
2539 gdk_window_show (window);
2541 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2548 menu_grab_transfer_window_destroy (GtkMenu *menu)
2550 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2553 gdk_window_set_user_data (window, NULL);
2554 gdk_window_destroy (window);
2555 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2560 gtk_menu_unrealize (GtkWidget *widget)
2562 GtkMenu *menu = GTK_MENU (widget);
2564 menu_grab_transfer_window_destroy (menu);
2566 gdk_window_set_user_data (menu->view_window, NULL);
2567 gdk_window_destroy (menu->view_window);
2568 menu->view_window = NULL;
2570 gdk_window_set_user_data (menu->bin_window, NULL);
2571 gdk_window_destroy (menu->bin_window);
2572 menu->bin_window = NULL;
2574 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2579 calculate_line_heights (GtkMenu *menu,
2581 guint **ret_min_heights,
2582 guint **ret_nat_heights)
2584 GtkMenuShell *menu_shell;
2585 GtkMenuPrivate *priv;
2586 GtkWidget *child, *widget;
2588 guint horizontal_padding;
2596 widget = GTK_WIDGET (menu);
2597 menu_shell = GTK_MENU_SHELL (widget);
2598 priv = gtk_menu_get_private (menu);
2600 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2601 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2602 n_heights = gtk_menu_get_n_rows (menu);
2603 n_columns = gtk_menu_get_n_columns (menu);
2604 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2606 gtk_widget_style_get (GTK_WIDGET (menu),
2607 "horizontal-padding", &horizontal_padding,
2610 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2611 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2613 for (children = menu_shell->children; children; children = children->next)
2618 gint child_min, child_nat;
2620 child = children->data;
2622 if (! gtk_widget_get_visible (child))
2625 get_effective_child_attach (child, &l, &r, &t, &b);
2627 part = avail_width / (r - l);
2629 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child), part,
2630 &child_min, &child_nat);
2632 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2634 part = MAX (child_min, toggle_size) / (b - t);
2635 min_heights[t] = MAX (min_heights[t], part);
2637 part = MAX (child_nat, toggle_size) / (b - t);
2638 nat_heights[t] = MAX (nat_heights[t], part);
2641 if (ret_min_heights)
2642 *ret_min_heights = min_heights;
2644 g_free (min_heights);
2646 if (ret_nat_heights)
2647 *ret_nat_heights = nat_heights;
2649 g_free (nat_heights);
2655 gtk_menu_size_allocate (GtkWidget *widget,
2656 GtkAllocation *allocation)
2659 GtkMenuShell *menu_shell;
2661 GtkAllocation child_allocation;
2662 GtkMenuPrivate *priv;
2668 guint vertical_padding;
2669 guint horizontal_padding;
2671 g_return_if_fail (GTK_IS_MENU (widget));
2672 g_return_if_fail (allocation != NULL);
2674 menu = GTK_MENU (widget);
2675 menu_shell = GTK_MENU_SHELL (widget);
2676 priv = gtk_menu_get_private (menu);
2678 gtk_widget_set_allocation (widget, allocation);
2680 style = gtk_widget_get_style (widget);
2682 gtk_widget_style_get (GTK_WIDGET (menu),
2683 "vertical-padding", &vertical_padding,
2684 "horizontal-padding", &horizontal_padding,
2686 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2688 g_free (priv->heights);
2689 priv->heights_length =
2690 calculate_line_heights (menu,
2695 /* refresh our cached height request */
2696 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2697 for (i = 0; i < priv->heights_length; i++)
2698 priv->requested_height += priv->heights[i];
2700 x = border_width + style->xthickness + horizontal_padding;
2701 y = border_width + style->ythickness + vertical_padding;
2703 width = MAX (1, allocation->width - x * 2);
2704 height = MAX (1, allocation->height - y * 2);
2706 if (menu_shell->active)
2707 gtk_menu_scroll_to (menu, menu->scroll_offset);
2709 if (!menu->tearoff_active)
2711 GtkBorder arrow_border;
2713 get_arrows_border (menu, &arrow_border);
2714 y += arrow_border.top;
2715 height -= arrow_border.top;
2716 height -= arrow_border.bottom;
2719 if (gtk_widget_get_realized (widget))
2721 gdk_window_move_resize (gtk_widget_get_window (widget),
2722 allocation->x, allocation->y,
2723 allocation->width, allocation->height);
2725 gdk_window_move_resize (menu->view_window,
2732 if (menu_shell->children)
2734 gint base_width = width / gtk_menu_get_n_columns (menu);
2736 children = menu_shell->children;
2739 child = children->data;
2740 children = children->next;
2742 if (gtk_widget_get_visible (child))
2747 get_effective_child_attach (child, &l, &r, &t, &b);
2749 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2752 tmp = gtk_menu_get_n_columns (menu) - l;
2753 l = gtk_menu_get_n_columns (menu) - r;
2757 child_allocation.width = (r - l) * base_width;
2758 child_allocation.height = 0;
2759 child_allocation.x = l * base_width;
2760 child_allocation.y = 0;
2762 for (i = 0; i < b; i++)
2765 child_allocation.y += priv->heights[i];
2767 child_allocation.height += priv->heights[i];
2770 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2773 gtk_widget_size_allocate (child, &child_allocation);
2774 gtk_widget_queue_draw (child);
2778 /* Resize the item window */
2779 if (gtk_widget_get_realized (widget))
2785 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2786 height += priv->heights[i];
2788 width = gtk_menu_get_n_columns (menu) * base_width;
2789 gdk_window_resize (menu->bin_window, width, height);
2792 if (menu->tearoff_active)
2794 if (height >= priv->requested_height)
2796 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2798 gtk_widget_hide (menu->tearoff_scrollbar);
2799 gtk_menu_set_tearoff_hints (menu, allocation->width);
2801 gtk_menu_scroll_to (menu, 0);
2806 menu->tearoff_adjustment->upper = priv->requested_height;
2807 menu->tearoff_adjustment->page_size = allocation->height;
2809 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2810 menu->tearoff_adjustment->upper)
2813 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2816 gtk_menu_scroll_to (menu, value);
2819 gtk_adjustment_changed (menu->tearoff_adjustment);
2821 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2823 gtk_widget_show (menu->tearoff_scrollbar);
2824 gtk_menu_set_tearoff_hints (menu, allocation->width);
2832 get_arrows_visible_area (GtkMenu *menu,
2833 GdkRectangle *border,
2834 GdkRectangle *upper,
2835 GdkRectangle *lower,
2838 GtkArrowPlacement arrow_placement;
2840 GtkWidget *widget = GTK_WIDGET (menu);
2842 guint vertical_padding;
2843 guint horizontal_padding;
2844 gint scroll_arrow_height;
2846 style = gtk_widget_get_style (widget);
2848 gtk_widget_style_get (widget,
2849 "vertical-padding", &vertical_padding,
2850 "horizontal-padding", &horizontal_padding,
2851 "scroll-arrow-vlength", &scroll_arrow_height,
2852 "arrow-placement", &arrow_placement,
2855 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2856 border->x = border_width + style->xthickness + horizontal_padding;
2857 border->y = border_width + style->ythickness + vertical_padding;
2858 gdk_drawable_get_size (gtk_widget_get_window (widget),
2859 &border->width, &border->height);
2861 switch (arrow_placement)
2863 case GTK_ARROWS_BOTH:
2864 upper->x = border->x;
2865 upper->y = border->y;
2866 upper->width = border->width - 2 * border->x;
2867 upper->height = scroll_arrow_height;
2869 lower->x = border->x;
2870 lower->y = border->height - border->y - scroll_arrow_height;
2871 lower->width = border->width - 2 * border->x;
2872 lower->height = scroll_arrow_height;
2875 case GTK_ARROWS_START:
2876 upper->x = border->x;
2877 upper->y = border->y;
2878 upper->width = (border->width - 2 * border->x) / 2;
2879 upper->height = scroll_arrow_height;
2881 lower->x = border->x + upper->width;
2882 lower->y = border->y;
2883 lower->width = (border->width - 2 * border->x) / 2;
2884 lower->height = scroll_arrow_height;
2887 case GTK_ARROWS_END:
2888 upper->x = border->x;
2889 upper->y = border->height - border->y - scroll_arrow_height;
2890 upper->width = (border->width - 2 * border->x) / 2;
2891 upper->height = scroll_arrow_height;
2893 lower->x = border->x + upper->width;
2894 lower->y = border->height - border->y - scroll_arrow_height;
2895 lower->width = (border->width - 2 * border->x) / 2;
2896 lower->height = scroll_arrow_height;
2900 g_assert_not_reached();
2901 upper->x = upper->y = upper->width = upper->height = 0;
2902 lower->x = lower->y = lower->width = lower->height = 0;
2905 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2909 gtk_menu_paint (GtkWidget *widget,
2910 GdkEventExpose *event)
2913 GtkMenuPrivate *priv;
2915 GdkRectangle border;
2921 g_return_if_fail (GTK_IS_MENU (widget));
2923 menu = GTK_MENU (widget);
2924 priv = gtk_menu_get_private (menu);
2926 style = gtk_widget_get_style (widget);
2927 window = gtk_widget_get_window (widget);
2929 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2931 if (event->window == window)
2933 gfloat arrow_scaling;
2936 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2937 arrow_size = arrow_scaling * arrow_space;
2939 gtk_paint_box (style,
2943 &event->area, widget, "menu",
2946 if (menu->upper_arrow_visible && !menu->tearoff_active)
2948 gtk_paint_box (style,
2950 priv->upper_arrow_state,
2952 &event->area, widget, "menu_scroll_arrow_up",
2958 gtk_paint_arrow (style,
2960 priv->upper_arrow_state,
2962 &event->area, widget, "menu_scroll_arrow_up",
2965 upper.x + (upper.width - arrow_size) / 2,
2966 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2967 arrow_size, arrow_size);
2970 if (menu->lower_arrow_visible && !menu->tearoff_active)
2972 gtk_paint_box (style,
2974 priv->lower_arrow_state,
2976 &event->area, widget, "menu_scroll_arrow_down",
2982 gtk_paint_arrow (style,
2984 priv->lower_arrow_state,
2986 &event->area, widget, "menu_scroll_arrow_down",
2989 lower.x + (lower.width - arrow_size) / 2,
2990 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2991 arrow_size, arrow_size);
2994 else if (event->window == menu->bin_window)
2996 gint y = -border.y + menu->scroll_offset;
2998 if (!menu->tearoff_active)
3000 GtkBorder arrow_border;
3002 get_arrows_border (menu, &arrow_border);
3003 y -= arrow_border.top;
3006 gtk_paint_box (style,
3010 &event->area, widget, "menu",
3012 border.width, border.height);
3017 gtk_menu_expose (GtkWidget *widget,
3018 GdkEventExpose *event)
3020 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3021 g_return_val_if_fail (event != NULL, FALSE);
3023 if (gtk_widget_is_drawable (widget))
3025 gtk_menu_paint (widget, event);
3027 GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
3034 gtk_menu_show (GtkWidget *widget)
3036 GtkMenu *menu = GTK_MENU (widget);
3038 _gtk_menu_refresh_accel_paths (menu, FALSE);
3040 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3046 gtk_menu_size_request_init (GtkSizeRequestIface *iface)
3048 iface->get_width = gtk_menu_get_width;
3049 iface->get_height = gtk_menu_get_height;
3050 iface->get_height_for_width = gtk_menu_get_height_for_width;
3054 gtk_menu_get_width (GtkSizeRequest *widget,
3059 GtkMenuShell *menu_shell;
3060 GtkMenuPrivate *priv;
3064 guint max_toggle_size;
3065 guint max_accel_width;
3066 guint horizontal_padding;
3068 gint child_min, child_nat;
3069 gint min_width, nat_width;
3071 menu = GTK_MENU (widget);
3072 menu_shell = GTK_MENU_SHELL (widget);
3073 priv = gtk_menu_get_private (menu);
3075 style = gtk_widget_get_style (GTK_WIDGET (widget));
3077 min_width = nat_width = 0;
3079 max_toggle_size = 0;
3080 max_accel_width = 0;
3082 children = menu_shell->children;
3089 child = children->data;
3090 children = children->next;
3092 if (! gtk_widget_get_visible (child))
3095 get_effective_child_attach (child, &l, &r, &t, &b);
3097 /* It's important to size_request the child
3098 * before doing the toggle size request, in
3099 * case the toggle size request depends on the size
3100 * request of a child of the child (e.g. for ImageMenuItem)
3103 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3104 gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
3106 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3107 max_toggle_size = MAX (max_toggle_size, toggle_size);
3108 max_accel_width = MAX (max_accel_width,
3109 GTK_MENU_ITEM (child)->accelerator_width);
3111 part = child_min / (r - l);
3112 min_width = MAX (min_width, part);
3114 part = child_nat / (r - l);
3115 nat_width = MAX (nat_width, part);
3118 /* If the menu doesn't include any images or check items
3119 * reserve the space so that all menus are consistent.
3120 * We only do this for 'ordinary' menus, not for combobox
3121 * menus or multi-column menus
3123 if (max_toggle_size == 0 &&
3124 gtk_menu_get_n_columns (menu) == 1 &&
3125 !priv->no_toggle_size)
3127 guint toggle_spacing;
3128 guint indicator_size;
3130 gtk_style_get (style,
3131 GTK_TYPE_CHECK_MENU_ITEM,
3132 "toggle-spacing", &toggle_spacing,
3133 "indicator-size", &indicator_size,
3136 max_toggle_size = indicator_size + toggle_spacing;
3139 min_width += 2 * max_toggle_size + max_accel_width;
3140 min_width *= gtk_menu_get_n_columns (menu);
3142 nat_width += 2 * max_toggle_size + max_accel_width;
3143 nat_width *= gtk_menu_get_n_columns (menu);
3145 gtk_widget_style_get (GTK_WIDGET (menu),
3146 "horizontal-padding", &horizontal_padding,
3149 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3150 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3151 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3153 menu->toggle_size = max_toggle_size;
3154 priv->accel_size = max_accel_width;
3157 *minimum_size = min_width;
3160 *natural_size = nat_width;
3162 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3164 if (menu->tearoff_active)
3165 gtk_menu_set_tearoff_hints (menu, min_width);
3169 gtk_menu_get_height (GtkSizeRequest *widget,
3175 /* Menus are height-for-width only, just return the height for the minimum width */
3176 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, &min_width, NULL);
3177 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height_for_width (widget, min_width, minimum_size, natural_size);
3181 gtk_menu_get_height_for_width (GtkSizeRequest *widget,
3186 GtkMenu *menu = GTK_MENU (widget);
3187 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3188 guint *min_heights, *nat_heights;
3189 guint vertical_padding, border_width;
3191 gint min_height, nat_height;
3193 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3194 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3196 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3199 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3201 for (i = 0; i < n_heights; i++)
3203 min_height += min_heights[i];
3204 nat_height += nat_heights[i];
3207 if (private->have_position)
3209 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3210 GdkRectangle monitor;
3212 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3214 if (private->y + min_height > monitor.y + monitor.height)
3215 min_height = monitor.y + monitor.height - private->y;
3217 if (private->y + nat_height > monitor.y + monitor.height)
3218 nat_height = monitor.y + monitor.height - private->y;
3220 if (private->y < monitor.y)
3222 min_height -= monitor.y - private->y;
3223 nat_height -= monitor.y - private->y;
3228 *minimum_size = min_height;
3231 *natural_size = nat_height;
3233 g_free (min_heights);
3234 g_free (nat_heights);
3240 gtk_menu_button_scroll (GtkMenu *menu,
3241 GdkEventButton *event)
3243 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3245 gboolean touchscreen_mode;
3247 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3248 "gtk-touchscreen-mode", &touchscreen_mode,
3251 if (touchscreen_mode)
3252 gtk_menu_handle_scrolling (menu,
3253 event->x_root, event->y_root,
3254 event->type == GDK_BUTTON_PRESS,
3264 pointer_in_menu_window (GtkWidget *widget,
3268 GtkAllocation allocation;
3269 GtkMenu *menu = GTK_MENU (widget);
3271 if (gtk_widget_get_mapped (menu->toplevel))
3273 GtkMenuShell *menu_shell;
3274 gint window_x, window_y;
3276 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3277 &window_x, &window_y);
3279 gtk_widget_get_allocation (widget, &allocation);
3280 if (x_root >= window_x && x_root < window_x + allocation.width &&
3281 y_root >= window_y && y_root < window_y + allocation.height)
3284 menu_shell = GTK_MENU_SHELL (widget);
3286 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3287 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3295 gtk_menu_button_press (GtkWidget *widget,
3296 GdkEventButton *event)
3298 if (event->type != GDK_BUTTON_PRESS)
3301 /* Don't pass down to menu shell for presses over scroll arrows
3303 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3306 /* Don't pass down to menu shell if a non-menuitem part of the menu
3307 * was clicked. The check for the event_widget being a GtkMenuShell
3308 * works because we have the pointer grabbed on menu_shell->window
3309 * with owner_events=TRUE, so all events that are either outside
3310 * the menu or on its border are delivered relative to
3311 * menu_shell->window.
3313 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3314 pointer_in_menu_window (widget, event->x_root, event->y_root))
3317 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3321 gtk_menu_button_release (GtkWidget *widget,
3322 GdkEventButton *event)
3324 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3326 if (priv->ignore_button_release)
3328 priv->ignore_button_release = FALSE;
3332 if (event->type != GDK_BUTTON_RELEASE)
3335 /* Don't pass down to menu shell for releases over scroll arrows
3337 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3340 /* Don't pass down to menu shell if a non-menuitem part of the menu
3341 * was clicked (see comment in button_press()).
3343 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3344 pointer_in_menu_window (widget, event->x_root, event->y_root))
3346 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3347 * bail out early here so it is in a consistent state for the
3348 * next button_press/button_release in GtkMenuShell.
3351 if (GTK_MENU_SHELL (widget)->active)
3352 GTK_MENU_SHELL (widget)->button = 0;
3357 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3360 static const gchar *
3361 get_accel_path (GtkWidget *menu_item,
3366 GClosure *accel_closure;
3367 GtkAccelGroup *accel_group;
3369 path = _gtk_widget_get_accel_path (menu_item, locked);
3372 path = GTK_MENU_ITEM (menu_item)->accel_path;
3378 label = gtk_bin_get_child (GTK_BIN (menu_item));
3380 if (GTK_IS_ACCEL_LABEL (label))
3382 g_object_get (label,
3383 "accel-closure", &accel_closure,
3387 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3389 *locked = gtk_accel_group_get_is_locked (accel_group);
3399 gtk_menu_key_press (GtkWidget *widget,
3402 GtkMenuShell *menu_shell;
3404 gboolean delete = FALSE;
3405 gboolean can_change_accels;
3406 gchar *accel = NULL;
3407 guint accel_key, accel_mods;
3408 GdkModifierType consumed_modifiers;
3409 GdkDisplay *display;
3411 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3412 g_return_val_if_fail (event != NULL, FALSE);
3414 menu_shell = GTK_MENU_SHELL (widget);
3415 menu = GTK_MENU (widget);
3417 gtk_menu_stop_navigating_submenu (menu);
3419 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3422 display = gtk_widget_get_display (widget);
3424 g_object_get (gtk_widget_get_settings (widget),
3425 "gtk-menu-bar-accel", &accel,
3426 "gtk-can-change-accels", &can_change_accels,
3429 if (accel && *accel)
3432 GdkModifierType mods = 0;
3434 gtk_accelerator_parse (accel, &keyval, &mods);
3437 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3439 /* FIXME this is wrong, needs to be in the global accel resolution
3440 * thing, to properly consider i18n etc., but that probably requires
3441 * AccelGroup changes etc.
3443 if (event->keyval == keyval && (mods & event->state) == mods)
3445 gtk_menu_shell_cancel (menu_shell);
3453 switch (event->keyval)
3455 case GDK_KEY_Delete:
3456 case GDK_KEY_KP_Delete:
3457 case GDK_KEY_BackSpace:
3464 /* Figure out what modifiers went into determining the key symbol */
3465 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3466 event->hardware_keycode, event->state, event->group,
3467 NULL, NULL, NULL, &consumed_modifiers);
3469 accel_key = gdk_keyval_to_lower (event->keyval);
3470 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3472 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3473 * We re-upper case when we match against the keyval, but display and save in caseless form.
3475 if (accel_key != event->keyval)
3476 accel_mods |= GDK_SHIFT_MASK;
3478 /* Modify the accelerators */
3479 if (can_change_accels &&
3480 menu_shell->active_menu_item &&
3481 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3482 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3483 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3485 GtkWidget *menu_item = menu_shell->active_menu_item;
3486 gboolean locked, replace_accels = TRUE;
3489 path = get_accel_path (menu_item, &locked);
3490 if (!path || locked)
3492 /* can't change accelerators on menu_items without paths
3493 * (basically, those items are accelerator-locked).
3495 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3496 gtk_widget_error_bell (widget);
3502 /* For the keys that act to delete the current setting, we delete
3503 * the current setting if there is one, otherwise, we set the
3504 * key as the accelerator.
3510 if (gtk_accel_map_lookup_entry (path, &key) &&
3511 (key.accel_key || key.accel_mods))
3517 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3521 /* we failed, probably because this key is in use and
3524 /* g_print("failed to change\n"); */
3525 gtk_widget_error_bell (widget);
3534 check_threshold (GtkWidget *widget,
3543 ABS (start_x - x) > THRESHOLD ||
3544 ABS (start_y - y) > THRESHOLD;
3548 definitely_within_item (GtkWidget *widget,
3552 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3555 gdk_drawable_get_size (window, &w, &h);
3558 check_threshold (widget, 0, 0, x, y) &&
3559 check_threshold (widget, w - 1, 0, x, y) &&
3560 check_threshold (widget, w - 1, h - 1, x, y) &&
3561 check_threshold (widget, 0, h - 1, x, y);
3565 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3567 GtkMenuPrivate *priv;
3569 priv = gtk_menu_get_private (menu);
3571 return priv->navigation_height && priv->navigation_width;
3575 gtk_menu_motion_notify (GtkWidget *widget,
3576 GdkEventMotion *event)
3578 GtkWidget *menu_item;
3580 GtkMenuShell *menu_shell;
3583 gboolean need_enter;
3585 if (GTK_IS_MENU (widget))
3587 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3589 if (priv->ignore_button_release)
3590 priv->ignore_button_release = FALSE;
3592 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3596 /* We received the event for one of two reasons:
3598 * a) We are the active menu, and did gtk_grab_add()
3599 * b) The widget is a child of ours, and the event was propagated
3601 * Since for computation of navigation regions, we want the menu which
3602 * is the parent of the menu item, for a), we need to find that menu,
3603 * which may be different from 'widget'.
3605 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3606 parent = gtk_widget_get_parent (menu_item);
3607 if (!GTK_IS_MENU_ITEM (menu_item) ||
3608 !GTK_IS_MENU (parent))
3611 menu_shell = GTK_MENU_SHELL (parent);
3612 menu = GTK_MENU (menu_shell);
3614 if (definitely_within_item (menu_item, event->x, event->y))
3615 menu_shell->activate_time = 0;
3617 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3619 /* Check to see if we are within an active submenu's navigation region
3621 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3624 /* Make sure we pop down if we enter a non-selectable menu item, so we
3625 * don't show a submenu when the cursor is outside the stay-up triangle.
3627 if (!_gtk_menu_item_is_selectable (menu_item))
3629 /* We really want to deselect, but this gives the menushell code
3630 * a chance to do some bookkeeping about the menuitem.
3632 gtk_menu_shell_select_item (menu_shell, menu_item);
3638 /* The menu is now sensitive to enter events on its items, but
3639 * was previously sensitive. So we fake an enter event.
3643 menu_shell->ignore_enter = FALSE;
3645 gdk_drawable_get_size (event->window, &width, &height);
3646 if (event->x >= 0 && event->x < width &&
3647 event->y >= 0 && event->y < height)
3649 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3652 send_event->crossing.window = g_object_ref (event->window);
3653 send_event->crossing.time = event->time;
3654 send_event->crossing.send_event = TRUE;
3655 send_event->crossing.x_root = event->x_root;
3656 send_event->crossing.y_root = event->y_root;
3657 send_event->crossing.x = event->x;
3658 send_event->crossing.y = event->y;
3659 send_event->crossing.state = event->state;
3660 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3662 /* We send the event to 'widget', the currently active menu,
3663 * instead of 'menu', the menu that the pointer is in. This
3664 * will ensure that the event will be ignored unless the
3665 * menuitem is a child of the active menu or some parent
3666 * menu of the active menu.
3668 result = gtk_widget_event (widget, send_event);
3669 gdk_event_free (send_event);
3679 get_double_arrows (GtkMenu *menu)
3681 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3682 gboolean double_arrows;
3683 GtkArrowPlacement arrow_placement;
3685 gtk_widget_style_get (GTK_WIDGET (menu),
3686 "double-arrows", &double_arrows,
3687 "arrow-placement", &arrow_placement,
3690 if (arrow_placement != GTK_ARROWS_BOTH)
3693 return double_arrows || (priv->initially_pushed_in &&
3694 menu->scroll_offset != 0);
3698 gtk_menu_scroll_by (GtkMenu *menu,
3701 GtkMenuPrivate *priv;
3702 GtkBorder arrow_border;
3705 gint view_width, view_height;
3706 gboolean double_arrows;
3708 widget = GTK_WIDGET (menu);
3709 offset = menu->scroll_offset + step;
3710 priv = gtk_menu_get_private (menu);
3712 get_arrows_border (menu, &arrow_border);
3714 double_arrows = get_double_arrows (menu);
3716 /* If we scroll upward and the non-visible top part
3717 * is smaller than the scroll arrow it would be
3718 * pretty stupid to show the arrow and taking more
3719 * screen space than just scrolling to the top.
3722 if ((step < 0) && (offset < arrow_border.top))
3725 /* Don't scroll over the top if we weren't before: */
3726 if ((menu->scroll_offset >= 0) && (offset < 0))
3729 gdk_drawable_get_size (gtk_widget_get_window (widget),
3730 &view_width, &view_height);
3732 if (menu->scroll_offset == 0 &&
3733 view_height >= priv->requested_height)
3736 /* Don't scroll past the bottom if we weren't before: */
3737 if (menu->scroll_offset > 0)
3738 view_height -= arrow_border.top;
3740 /* When both arrows are always shown, reduce
3741 * view height even more.
3744 view_height -= arrow_border.bottom;
3746 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3747 (offset + view_height > priv->requested_height))
3748 offset = priv->requested_height - view_height;
3750 if (offset != menu->scroll_offset)
3751 gtk_menu_scroll_to (menu, offset);
3755 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3756 gboolean touchscreen_mode)
3758 gboolean upper_visible;
3759 gboolean lower_visible;
3761 upper_visible = menu->upper_arrow_visible;
3762 lower_visible = menu->lower_arrow_visible;
3764 gtk_menu_scroll_by (menu, menu->scroll_step);
3766 if (touchscreen_mode &&
3767 (upper_visible != menu->upper_arrow_visible ||
3768 lower_visible != menu->lower_arrow_visible))
3770 /* We are about to hide a scroll arrow while the mouse is pressed,
3771 * this would cause the uncovered menu item to be activated on button
3772 * release. Therefore we need to ignore button release here
3774 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3775 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3780 gtk_menu_scroll_timeout (gpointer data)
3783 gboolean touchscreen_mode;
3785 menu = GTK_MENU (data);
3787 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3788 "gtk-touchscreen-mode", &touchscreen_mode,
3791 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3797 gtk_menu_scroll_timeout_initial (gpointer data)
3801 gboolean touchscreen_mode;
3803 menu = GTK_MENU (data);
3805 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3806 "gtk-timeout-repeat", &timeout,
3807 "gtk-touchscreen-mode", &touchscreen_mode,
3810 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3812 gtk_menu_remove_scroll_timeout (menu);
3814 menu->timeout_id = gdk_threads_add_timeout (timeout,
3815 gtk_menu_scroll_timeout,
3822 gtk_menu_start_scrolling (GtkMenu *menu)
3825 gboolean touchscreen_mode;
3827 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3828 "gtk-timeout-repeat", &timeout,
3829 "gtk-touchscreen-mode", &touchscreen_mode,
3832 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3834 menu->timeout_id = gdk_threads_add_timeout (timeout,
3835 gtk_menu_scroll_timeout_initial,
3840 gtk_menu_scroll (GtkWidget *widget,
3841 GdkEventScroll *event)
3843 GtkMenu *menu = GTK_MENU (widget);
3845 switch (event->direction)
3847 case GDK_SCROLL_RIGHT:
3848 case GDK_SCROLL_DOWN:
3849 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3851 case GDK_SCROLL_LEFT:
3853 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3861 get_arrows_sensitive_area (GtkMenu *menu,
3862 GdkRectangle *upper,
3863 GdkRectangle *lower)
3865 GtkArrowPlacement arrow_placement;
3866 GtkWidget *widget = GTK_WIDGET (menu);
3870 guint vertical_padding;
3872 gint scroll_arrow_height;
3874 window = gtk_widget_get_window (widget);
3876 gdk_drawable_get_size (window, &width, &height);
3878 gtk_widget_style_get (widget,
3879 "vertical-padding", &vertical_padding,
3880 "scroll-arrow-vlength", &scroll_arrow_height,
3881 "arrow-placement", &arrow_placement,
3884 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3885 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3887 gdk_window_get_position (window, &win_x, &win_y);
3889 switch (arrow_placement)
3891 case GTK_ARROWS_BOTH:
3896 upper->width = width;
3897 upper->height = scroll_arrow_height + border;
3903 lower->y = win_y + height - border - scroll_arrow_height;
3904 lower->width = width;
3905 lower->height = scroll_arrow_height + border;
3909 case GTK_ARROWS_START:
3914 upper->width = width / 2;
3915 upper->height = scroll_arrow_height + border;
3920 lower->x = win_x + width / 2;
3922 lower->width = width / 2;
3923 lower->height = scroll_arrow_height + border;
3927 case GTK_ARROWS_END:
3931 upper->y = win_y + height - border - scroll_arrow_height;
3932 upper->width = width / 2;
3933 upper->height = scroll_arrow_height + border;
3938 lower->x = win_x + width / 2;
3939 lower->y = win_y + height - border - scroll_arrow_height;
3940 lower->width = width / 2;
3941 lower->height = scroll_arrow_height + border;
3949 gtk_menu_handle_scrolling (GtkMenu *menu,
3955 GtkMenuShell *menu_shell;
3956 GtkMenuPrivate *priv;
3959 gboolean scroll_fast = FALSE;
3961 gboolean touchscreen_mode;
3963 priv = gtk_menu_get_private (menu);
3965 menu_shell = GTK_MENU_SHELL (menu);
3967 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3968 "gtk-touchscreen-mode", &touchscreen_mode,
3971 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3976 /* upper arrow handling */
3978 get_arrows_sensitive_area (menu, &rect, NULL);
3981 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3982 (x >= rect.x) && (x < rect.x + rect.width) &&
3983 (y >= rect.y) && (y < rect.y + rect.height))
3988 if (touchscreen_mode)
3989 menu->upper_arrow_prelight = in_arrow;
3991 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3993 gboolean arrow_pressed = FALSE;
3995 if (menu->upper_arrow_visible && !menu->tearoff_active)
3997 if (touchscreen_mode)
3999 if (enter && menu->upper_arrow_prelight)
4001 if (menu->timeout_id == 0)
4003 /* Deselect the active item so that
4004 * any submenus are popped down
4006 gtk_menu_shell_deselect (menu_shell);
4008 gtk_menu_remove_scroll_timeout (menu);
4009 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4013 /* Only do stuff on click. */
4014 gtk_menu_start_scrolling (menu);
4015 arrow_pressed = TRUE;
4020 arrow_pressed = TRUE;
4025 gtk_menu_stop_scrolling (menu);
4028 else /* !touchscreen_mode */
4030 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4032 if (enter && in_arrow &&
4033 (!menu->upper_arrow_prelight ||
4034 menu->scroll_fast != scroll_fast))
4036 menu->upper_arrow_prelight = TRUE;
4037 menu->scroll_fast = scroll_fast;
4039 /* Deselect the active item so that
4040 * any submenus are popped down
4042 gtk_menu_shell_deselect (menu_shell);
4044 gtk_menu_remove_scroll_timeout (menu);
4045 menu->scroll_step = scroll_fast ?
4046 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4049 gdk_threads_add_timeout (scroll_fast ?
4050 MENU_SCROLL_TIMEOUT2 :
4051 MENU_SCROLL_TIMEOUT1,
4052 gtk_menu_scroll_timeout, menu);
4054 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4056 gtk_menu_stop_scrolling (menu);
4061 /* gtk_menu_start_scrolling() might have hit the top of the
4062 * menu, so check if the button isn't insensitive before
4063 * changing it to something else.
4065 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4067 GtkStateType arrow_state = GTK_STATE_NORMAL;
4070 arrow_state = GTK_STATE_ACTIVE;
4071 else if (menu->upper_arrow_prelight)
4072 arrow_state = GTK_STATE_PRELIGHT;
4074 if (arrow_state != priv->upper_arrow_state)
4076 priv->upper_arrow_state = arrow_state;
4078 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4084 /* lower arrow handling */
4086 get_arrows_sensitive_area (menu, NULL, &rect);
4089 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4090 (x >= rect.x) && (x < rect.x + rect.width) &&
4091 (y >= rect.y) && (y < rect.y + rect.height))
4096 if (touchscreen_mode)
4097 menu->lower_arrow_prelight = in_arrow;
4099 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4101 gboolean arrow_pressed = FALSE;
4103 if (menu->lower_arrow_visible && !menu->tearoff_active)
4105 if (touchscreen_mode)
4107 if (enter && menu->lower_arrow_prelight)
4109 if (menu->timeout_id == 0)
4111 /* Deselect the active item so that
4112 * any submenus are popped down
4114 gtk_menu_shell_deselect (menu_shell);
4116 gtk_menu_remove_scroll_timeout (menu);
4117 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4121 /* Only do stuff on click. */
4122 gtk_menu_start_scrolling (menu);
4123 arrow_pressed = TRUE;
4128 arrow_pressed = TRUE;
4133 gtk_menu_stop_scrolling (menu);
4136 else /* !touchscreen_mode */
4138 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4140 if (enter && in_arrow &&
4141 (!menu->lower_arrow_prelight ||
4142 menu->scroll_fast != scroll_fast))
4144 menu->lower_arrow_prelight = TRUE;
4145 menu->scroll_fast = scroll_fast;
4147 /* Deselect the active item so that
4148 * any submenus are popped down
4150 gtk_menu_shell_deselect (menu_shell);
4152 gtk_menu_remove_scroll_timeout (menu);
4153 menu->scroll_step = scroll_fast ?
4154 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4157 gdk_threads_add_timeout (scroll_fast ?
4158 MENU_SCROLL_TIMEOUT2 :
4159 MENU_SCROLL_TIMEOUT1,
4160 gtk_menu_scroll_timeout, menu);
4162 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4164 gtk_menu_stop_scrolling (menu);
4169 /* gtk_menu_start_scrolling() might have hit the bottom of the
4170 * menu, so check if the button isn't insensitive before
4171 * changing it to something else.
4173 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4175 GtkStateType arrow_state = GTK_STATE_NORMAL;
4178 arrow_state = GTK_STATE_ACTIVE;
4179 else if (menu->lower_arrow_prelight)
4180 arrow_state = GTK_STATE_PRELIGHT;
4182 if (arrow_state != priv->lower_arrow_state)
4184 priv->lower_arrow_state = arrow_state;
4186 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4194 gtk_menu_enter_notify (GtkWidget *widget,
4195 GdkEventCrossing *event)
4197 GtkWidget *menu_item;
4199 gboolean touchscreen_mode;
4201 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4202 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4203 event->mode == GDK_CROSSING_STATE_CHANGED)
4206 g_object_get (gtk_widget_get_settings (widget),
4207 "gtk-touchscreen-mode", &touchscreen_mode,
4210 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4211 if (GTK_IS_MENU (widget))
4213 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4215 if (!menu_shell->ignore_enter)
4216 gtk_menu_handle_scrolling (GTK_MENU (widget),
4217 event->x_root, event->y_root, TRUE, TRUE);
4220 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4222 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4224 if (GTK_IS_MENU (menu))
4226 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4227 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4229 if (priv->seen_item_enter)
4231 /* This is the second enter we see for an item
4232 * on this menu. This means a release should always
4235 menu_shell->activate_time = 0;
4237 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4238 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4240 if (definitely_within_item (menu_item, event->x, event->y))
4242 /* This is an actual user-enter (ie. not a pop-under)
4243 * In this case, the user must either have entered
4244 * sufficiently far enough into the item, or he must move
4245 * far enough away from the enter point. (see
4246 * gtk_menu_motion_notify())
4248 menu_shell->activate_time = 0;
4252 priv->seen_item_enter = TRUE;
4256 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4257 * will not correspond to the event widget's parent. Check to see
4258 * if we are in the parent's navigation region.
4260 parent = gtk_widget_get_parent (menu_item);
4261 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4262 gtk_menu_navigating_submenu (GTK_MENU (parent),
4263 event->x_root, event->y_root))
4266 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4270 gtk_menu_leave_notify (GtkWidget *widget,
4271 GdkEventCrossing *event)
4273 GtkMenuShell *menu_shell;
4275 GtkMenuItem *menu_item;
4276 GtkWidget *event_widget;
4278 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4279 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4280 event->mode == GDK_CROSSING_STATE_CHANGED)
4283 menu = GTK_MENU (widget);
4284 menu_shell = GTK_MENU_SHELL (widget);
4286 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4289 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4291 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4293 if (!GTK_IS_MENU_ITEM (event_widget))
4296 menu_item = GTK_MENU_ITEM (event_widget);
4298 /* Here we check to see if we're leaving an active menu item with a submenu,
4299 * in which case we enter submenu navigation mode.
4301 if (menu_shell->active_menu_item != NULL
4302 && menu_item->submenu != NULL
4303 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4305 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4307 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4310 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4312 /* We are leaving an active menu item with nonactive submenu.
4313 * Deselect it so we don't surprise the user with by popping
4314 * up a submenu _after_ he left the item.
4316 gtk_menu_shell_deselect (menu_shell);
4321 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4325 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4327 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4329 priv->navigation_x = 0;
4330 priv->navigation_y = 0;
4331 priv->navigation_width = 0;
4332 priv->navigation_height = 0;
4334 if (menu->navigation_timeout)
4336 g_source_remove (menu->navigation_timeout);
4337 menu->navigation_timeout = 0;
4341 /* When the timeout is elapsed, the navigation region is destroyed
4342 * and the menuitem under the pointer (if any) is selected.
4345 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4347 GtkMenuPopdownData *popdown_data = user_data;
4348 GtkMenu *menu = popdown_data->menu;
4349 GdkWindow *child_window;
4351 gtk_menu_stop_navigating_submenu (menu);
4353 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4355 child_window = gdk_window_get_device_position (menu->bin_window,
4356 popdown_data->device,
4361 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4363 send_event->crossing.window = g_object_ref (child_window);
4364 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4365 send_event->crossing.send_event = TRUE;
4366 gdk_event_set_device (send_event, popdown_data->device);
4368 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4370 gdk_event_free (send_event);
4378 gtk_menu_navigating_submenu (GtkMenu *menu,
4382 GtkMenuPrivate *priv;
4385 if (!gtk_menu_has_navigation_triangle (menu))
4388 priv = gtk_menu_get_private (menu);
4389 width = priv->navigation_width;
4390 height = priv->navigation_height;
4392 /* check if x/y are in the triangle spanned by the navigation parameters */
4394 /* 1) Move the coordinates so the triangle starts at 0,0 */
4395 event_x -= priv->navigation_x;
4396 event_y -= priv->navigation_y;
4398 /* 2) Ensure both legs move along the positive axis */
4410 /* 3) Check that the given coordinate is inside the triangle. The formula
4411 * is a transformed form of this formula: x/w + y/h <= 1
4413 if (event_x >= 0 && event_y >= 0 &&
4414 event_x * height + event_y * width <= width * height)
4420 gtk_menu_stop_navigating_submenu (menu);
4426 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4427 GtkMenuItem *menu_item,
4428 GdkEventCrossing *event)
4430 gint submenu_left = 0;
4431 gint submenu_right = 0;
4432 gint submenu_top = 0;
4433 gint submenu_bottom = 0;
4436 GtkWidget *event_widget;
4437 GtkMenuPopdownData *popdown_data;
4438 GtkMenuPrivate *priv;
4441 g_return_if_fail (menu_item->submenu != NULL);
4442 g_return_if_fail (event != NULL);
4444 priv = gtk_menu_get_private (menu);
4446 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4448 window = gtk_widget_get_window (menu_item->submenu);
4449 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4450 gdk_drawable_get_size (window, &width, &height);
4452 submenu_right = submenu_left + width;
4453 submenu_bottom = submenu_top + height;
4455 gdk_drawable_get_size (gtk_widget_get_window (event_widget),
4458 if (event->x >= 0 && event->x < width)
4462 gtk_menu_stop_navigating_submenu (menu);
4464 /* The navigation region is the triangle closest to the x/y
4465 * location of the rectangle. This is why the width or height
4469 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4472 priv->navigation_x = submenu_left;
4473 priv->navigation_width = event->x_root - submenu_left;
4478 priv->navigation_x = submenu_right;
4479 priv->navigation_width = event->x_root - submenu_right;
4485 priv->navigation_y = event->y_root;
4486 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4488 if (priv->navigation_height >= 0)
4494 priv->navigation_y = event->y_root;
4495 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4497 if (priv->navigation_height <= 0)
4501 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4502 "gtk-menu-popdown-delay", &popdown_delay,
4505 popdown_data = g_new (GtkMenuPopdownData, 1);
4506 popdown_data->menu = menu;
4507 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4509 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4511 gtk_menu_stop_navigating_submenu_cb,
4513 (GDestroyNotify) g_free);
4518 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4522 g_return_if_fail (GTK_IS_MENU (menu_shell));
4524 parent = menu_shell->parent_menu_shell;
4526 menu_shell->activate_time = 0;
4527 gtk_menu_popdown (GTK_MENU (menu_shell));
4530 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4534 gtk_menu_position (GtkMenu *menu)
4537 GtkRequisition requisition;
4538 GtkMenuPrivate *private;
4543 GdkScreen *pointer_screen;
4544 GdkRectangle monitor;
4547 g_return_if_fail (GTK_IS_MENU (menu));
4549 widget = GTK_WIDGET (menu);
4551 screen = gtk_widget_get_screen (widget);
4552 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4553 gdk_display_get_device_state (gdk_screen_get_display (screen),
4554 pointer, &pointer_screen, &x, &y, NULL);
4556 /* Get the minimum height for minimum width to figure out
4557 * the right place to popup the menu.
4559 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
4561 if (pointer_screen != screen)
4563 /* Pointer is on a different screen; roughly center the
4564 * menu on the screen. If someone was using multiscreen
4565 * + Xinerama together they'd probably want something
4566 * fancier; but that is likely to be vanishingly rare.
4568 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4569 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4572 private = gtk_menu_get_private (menu);
4573 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4575 private->initially_pushed_in = FALSE;
4577 /* Set the type hint here to allow custom position functions to set a different hint */
4578 if (!gtk_widget_get_visible (menu->toplevel))
4579 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4581 if (menu->position_func)
4583 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4584 menu->position_func_data);
4586 if (private->monitor_num < 0)
4587 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4589 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4593 GtkStyle *style = gtk_widget_get_style (widget);
4594 gint space_left, space_right, space_above, space_below;
4597 gint xthickness = style->xthickness;
4598 gint ythickness = style->ythickness;
4599 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4601 /* The placement of popup menus horizontally works like this (with
4602 * RTL in parentheses)
4604 * - If there is enough room to the right (left) of the mouse cursor,
4605 * position the menu there.
4607 * - Otherwise, if if there is enough room to the left (right) of the
4608 * mouse cursor, position the menu there.
4610 * - Otherwise if the menu is smaller than the monitor, position it
4611 * on the side of the mouse cursor that has the most space available
4613 * - Otherwise (if there is simply not enough room for the menu on the
4614 * monitor), position it as far left (right) as possible.
4616 * Positioning in the vertical direction is similar: first try below
4617 * mouse cursor, then above.
4619 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4621 space_left = x - monitor.x;
4622 space_right = monitor.x + monitor.width - x - 1;
4623 space_above = y - monitor.y;
4624 space_below = monitor.y + monitor.height - y - 1;
4626 /* position horizontally */
4628 /* the amount of space we need to position the menu. Note the
4629 * menu is offset "xthickness" pixels
4631 needed_width = requisition.width - xthickness;
4633 if (needed_width <= space_left ||
4634 needed_width <= space_right)
4636 if ((rtl && needed_width <= space_left) ||
4637 (!rtl && needed_width > space_right))
4640 x = x + xthickness - requisition.width + 1;
4644 /* position right */
4648 /* x is clamped on-screen further down */
4650 else if (requisition.width <= monitor.width)
4652 /* the menu is too big to fit on either side of the mouse
4653 * cursor, but smaller than the monitor. Position it on
4654 * the side that has the most space
4656 if (space_left > space_right)
4664 x = monitor.x + monitor.width - requisition.width;
4667 else /* menu is simply too big for the monitor */
4672 x = monitor.x + monitor.width - requisition.width;
4681 /* Position vertically. The algorithm is the same as above, but
4682 * simpler because we don't have to take RTL into account.
4684 needed_height = requisition.height - ythickness;
4686 if (needed_height <= space_above ||
4687 needed_height <= space_below)
4689 if (needed_height <= space_below)
4692 y = y + ythickness - requisition.height + 1;
4694 y = CLAMP (y, monitor.y,
4695 monitor.y + monitor.height - requisition.height);
4697 else if (needed_height > space_below && needed_height > space_above)
4699 if (space_below >= space_above)
4700 y = monitor.y + monitor.height - requisition.height;
4712 if (private->initially_pushed_in)
4714 menu_height = requisition.height;
4716 if (y + menu_height > monitor.y + monitor.height)
4718 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4719 y = (monitor.y + monitor.height) - menu_height;
4724 scroll_offset += monitor.y - y;
4729 /* FIXME: should this be done in the various position_funcs ? */
4730 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4732 if (GTK_MENU_SHELL (menu)->active)
4734 private->have_position = TRUE;
4739 if (y + requisition.height > monitor.y + monitor.height)
4740 requisition.height = (monitor.y + monitor.height) - y;
4744 scroll_offset += monitor.y - y;
4745 requisition.height -= monitor.y - y;
4749 if (scroll_offset > 0)
4751 GtkBorder arrow_border;
4753 get_arrows_border (menu, &arrow_border);
4754 scroll_offset += arrow_border.top;
4757 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4760 if (!GTK_MENU_SHELL (menu)->active)
4762 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4763 requisition.width, requisition.height);
4766 menu->scroll_offset = scroll_offset;
4770 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4772 if (menu->timeout_id)
4774 g_source_remove (menu->timeout_id);
4775 menu->timeout_id = 0;
4780 gtk_menu_stop_scrolling (GtkMenu *menu)
4782 gboolean touchscreen_mode;
4784 gtk_menu_remove_scroll_timeout (menu);
4786 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4787 "gtk-touchscreen-mode", &touchscreen_mode,
4790 if (!touchscreen_mode)
4792 menu->upper_arrow_prelight = FALSE;
4793 menu->lower_arrow_prelight = FALSE;
4798 gtk_menu_scroll_to (GtkMenu *menu,
4801 GtkMenuPrivate *priv;
4802 GtkAllocation allocation;
4803 GtkBorder arrow_border;
4807 gint view_width, view_height;
4810 guint vertical_padding;
4811 guint horizontal_padding;
4812 gboolean double_arrows;
4814 widget = GTK_WIDGET (menu);
4815 priv = gtk_menu_get_private (menu);
4817 if (menu->tearoff_active &&
4818 menu->tearoff_adjustment &&
4819 (menu->tearoff_adjustment->value != offset))
4821 menu->tearoff_adjustment->value =
4823 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4824 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4827 /* Move/resize the viewport according to arrows: */
4828 gtk_widget_get_allocation (widget, &allocation);
4829 view_width = allocation.width;
4830 view_height = allocation.height;
4832 style = gtk_widget_get_style (widget);
4834 gtk_widget_style_get (GTK_WIDGET (menu),
4835 "vertical-padding", &vertical_padding,
4836 "horizontal-padding", &horizontal_padding,
4839 double_arrows = get_double_arrows (menu);
4841 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4843 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4844 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4845 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4847 x = border_width + style->xthickness + horizontal_padding;
4848 y = border_width + style->ythickness + vertical_padding;
4850 if (double_arrows && !menu->tearoff_active)
4852 if (view_height < menu_height ||
4853 (offset > 0 && menu->scroll_offset > 0) ||
4854 (offset < 0 && menu->scroll_offset < 0))
4856 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4857 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4858 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4860 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4861 gtk_widget_queue_draw (GTK_WIDGET (menu));
4863 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4865 get_arrows_border (menu, &arrow_border);
4866 y += arrow_border.top;
4867 view_height -= arrow_border.top;
4868 view_height -= arrow_border.bottom;
4871 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4872 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4873 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4874 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4876 if (offset >= menu_height - view_height)
4877 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4878 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4879 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4880 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4882 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4883 (priv->lower_arrow_state != lower_arrow_previous_state))
4884 gtk_widget_queue_draw (GTK_WIDGET (menu));
4886 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4887 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4889 /* At the upper border, possibly remove timeout */
4890 if (menu->scroll_step < 0)
4892 gtk_menu_stop_scrolling (menu);
4893 gtk_widget_queue_draw (GTK_WIDGET (menu));
4897 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4898 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4900 /* At the lower border, possibly remove timeout */
4901 if (menu->scroll_step > 0)
4903 gtk_menu_stop_scrolling (menu);
4904 gtk_widget_queue_draw (GTK_WIDGET (menu));
4908 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4912 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4913 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4915 gtk_menu_stop_scrolling (menu);
4916 gtk_widget_queue_draw (GTK_WIDGET (menu));
4919 else if (!menu->tearoff_active)
4921 gboolean last_visible;
4923 last_visible = menu->upper_arrow_visible;
4924 menu->upper_arrow_visible = offset > 0;
4926 /* upper_arrow_visible may have changed, so requery the border */
4927 get_arrows_border (menu, &arrow_border);
4928 view_height -= arrow_border.top;
4930 if ((last_visible != menu->upper_arrow_visible) &&
4931 !menu->upper_arrow_visible)
4933 menu->upper_arrow_prelight = FALSE;
4935 /* If we hid the upper arrow, possibly remove timeout */
4936 if (menu->scroll_step < 0)
4938 gtk_menu_stop_scrolling (menu);
4939 gtk_widget_queue_draw (GTK_WIDGET (menu));
4943 last_visible = menu->lower_arrow_visible;
4944 menu->lower_arrow_visible = offset < menu_height - view_height;
4946 /* lower_arrow_visible may have changed, so requery the border */
4947 get_arrows_border (menu, &arrow_border);
4948 view_height -= arrow_border.bottom;
4950 if ((last_visible != menu->lower_arrow_visible) &&
4951 !menu->lower_arrow_visible)
4953 menu->lower_arrow_prelight = FALSE;
4955 /* If we hid the lower arrow, possibly remove timeout */
4956 if (menu->scroll_step > 0)
4958 gtk_menu_stop_scrolling (menu);
4959 gtk_widget_queue_draw (GTK_WIDGET (menu));
4963 y += arrow_border.top;
4966 /* Scroll the menu: */
4967 if (gtk_widget_get_realized (widget))
4968 gdk_window_move (menu->bin_window, 0, -offset);
4970 if (gtk_widget_get_realized (widget))
4971 gdk_window_move_resize (menu->view_window,
4977 menu->scroll_offset = offset;
4981 compute_child_offset (GtkMenu *menu,
4982 GtkWidget *menu_item,
4985 gboolean *is_last_child)
4987 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4988 gint item_top_attach;
4989 gint item_bottom_attach;
4990 gint child_offset = 0;
4993 get_effective_child_attach (menu_item, NULL, NULL,
4994 &item_top_attach, &item_bottom_attach);
4996 /* there is a possibility that we get called before _size_request, so
4997 * check the height table for safety.
4999 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5002 /* when we have a row with only invisible children, it's height will
5003 * be zero, so there's no need to check WIDGET_VISIBLE here
5005 for (i = 0; i < item_top_attach; i++)
5006 child_offset += priv->heights[i];
5009 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5011 *offset = child_offset;
5013 *height = priv->heights[item_top_attach];
5019 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5020 GtkWidget *menu_item)
5022 GtkMenu *menu = GTK_MENU (menu_shell);
5023 GtkWidget *widget = GTK_WIDGET (menu_shell);
5024 gint child_offset, child_height;
5028 gboolean last_child = 0;
5030 /* We need to check if the selected item fully visible.
5031 * If not we need to scroll the menu so that it becomes fully
5035 if (compute_child_offset (menu, menu_item,
5036 &child_offset, &child_height, &last_child))
5038 guint vertical_padding;
5039 gboolean double_arrows;
5041 y = menu->scroll_offset;
5042 gdk_drawable_get_size (gtk_widget_get_window (widget),
5045 gtk_widget_style_get (widget,
5046 "vertical-padding", &vertical_padding,
5049 double_arrows = get_double_arrows (menu);
5051 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5052 2 * gtk_widget_get_style (widget)->ythickness +
5053 2 * vertical_padding;
5054 if (child_offset < y)
5056 /* Ignore the enter event we might get if the pointer is on the menu
5058 menu_shell->ignore_enter = TRUE;
5059 gtk_menu_scroll_to (menu, child_offset);
5063 GtkBorder arrow_border;
5067 get_arrows_border (menu, &arrow_border);
5068 if (!menu->tearoff_active)
5069 arrow_height = arrow_border.top + arrow_border.bottom;
5071 if (child_offset + child_height > y + height - arrow_height)
5074 if ((!last_child && !menu->tearoff_active) || double_arrows)
5075 arrow_height += arrow_border.bottom;
5077 y = child_offset + child_height - height + arrow_height;
5078 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5080 /* Need upper arrow */
5081 arrow_height += arrow_border.top;
5082 y = child_offset + child_height - height + arrow_height;
5084 /* Ignore the enter event we might get if the pointer is on the menu
5086 menu_shell->ignore_enter = TRUE;
5087 gtk_menu_scroll_to (menu, y);
5095 gtk_menu_select_item (GtkMenuShell *menu_shell,
5096 GtkWidget *menu_item)
5098 GtkMenu *menu = GTK_MENU (menu_shell);
5100 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5101 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5103 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5107 /* Reparent the menu, taking care of the refcounting
5109 * If unrealize is true we force a unrealize while reparenting the parent.
5110 * This can help eliminate flicker in some cases.
5112 * What happens is that when the menu is unrealized and then re-realized,
5113 * the allocations are as follows:
5115 * parent - 1x1 at (0,0)
5116 * child1 - 100x20 at (0,0)
5117 * child2 - 100x20 at (0,20)
5118 * child3 - 100x20 at (0,40)
5120 * That is, the parent is small but the children are full sized. Then,
5121 * when the queued_resize gets processed, the parent gets resized to
5124 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5125 * contains the following logic:
5127 * - if a move or resize operation on a window would change the clip
5128 * region on the children, then before the window is resized
5129 * the background for children is temporarily set to None, the
5130 * move/resize done, and the background for the children restored.
5132 * So, at the point where the parent is resized to final size, the
5133 * background for the children is temporarily None, and thus they
5134 * are not cleared to the background color and the previous background
5135 * (the image of the menu) is left in place.
5138 gtk_menu_reparent (GtkMenu *menu,
5139 GtkWidget *new_parent,
5142 GtkObject *object = GTK_OBJECT (menu);
5143 GtkWidget *widget = GTK_WIDGET (menu);
5144 gboolean was_floating = g_object_is_floating (object);
5146 g_object_ref_sink (object);
5150 g_object_ref (object);
5151 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5152 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5153 g_object_unref (object);
5156 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
5159 g_object_force_floating (G_OBJECT (object));
5161 g_object_unref (object);
5165 gtk_menu_show_all (GtkWidget *widget)
5167 /* Show children, but not self. */
5168 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5173 gtk_menu_hide_all (GtkWidget *widget)
5175 /* Hide children, but not self. */
5176 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5180 * gtk_menu_set_screen:
5181 * @menu: a #GtkMenu.
5182 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5183 * determined by the widget the menu is attached to.
5185 * Sets the #GdkScreen on which the menu will be displayed.
5190 gtk_menu_set_screen (GtkMenu *menu,
5193 g_return_if_fail (GTK_IS_MENU (menu));
5194 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5196 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5200 menu_change_screen (menu, screen);
5204 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5206 attach_widget_screen_changed (attach_widget, NULL, menu);
5212 * @menu: a #GtkMenu.
5213 * @child: a #GtkMenuItem.
5214 * @left_attach: The column number to attach the left side of the item to.
5215 * @right_attach: The column number to attach the right side of the item to.
5216 * @top_attach: The row number to attach the top of the item to.
5217 * @bottom_attach: The row number to attach the bottom of the item to.
5219 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5220 * an item will occupy is specified by @left_attach, @right_attach,
5221 * @top_attach and @bottom_attach. These each represent the leftmost,
5222 * rightmost, uppermost and lower column and row numbers of the table.
5223 * (Columns and rows are indexed from zero).
5225 * Note that this function is not related to gtk_menu_detach().
5230 gtk_menu_attach (GtkMenu *menu,
5235 guint bottom_attach)
5237 GtkMenuShell *menu_shell;
5240 g_return_if_fail (GTK_IS_MENU (menu));
5241 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5242 parent = gtk_widget_get_parent (child);
5243 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5244 g_return_if_fail (left_attach < right_attach);
5245 g_return_if_fail (top_attach < bottom_attach);
5247 menu_shell = GTK_MENU_SHELL (menu);
5251 AttachInfo *ai = get_attach_info (child);
5253 ai->left_attach = left_attach;
5254 ai->right_attach = right_attach;
5255 ai->top_attach = top_attach;
5256 ai->bottom_attach = bottom_attach;
5258 menu_shell->children = g_list_append (menu_shell->children, child);
5260 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5262 menu_queue_resize (menu);
5266 gtk_container_child_set (GTK_CONTAINER (parent), child,
5267 "left-attach", left_attach,
5268 "right-attach", right_attach,
5269 "top-attach", top_attach,
5270 "bottom-attach", bottom_attach,
5276 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5280 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5281 "gtk-menu-popup-delay", &popup_delay,
5288 find_child_containing (GtkMenuShell *menu_shell,
5296 /* find a child which includes the area given by
5297 * left, right, top, bottom.
5300 for (list = menu_shell->children; list; list = list->next)
5304 if (!_gtk_menu_item_is_selectable (list->data))
5307 get_effective_child_attach (list->data, &l, &r, &t, &b);
5309 if (l <= left && right <= r
5310 && t <= top && bottom <= b)
5311 return GTK_WIDGET (list->data);
5318 gtk_menu_move_current (GtkMenuShell *menu_shell,
5319 GtkMenuDirectionType direction)
5321 GtkMenu *menu = GTK_MENU (menu_shell);
5324 GtkWidget *match = NULL;
5326 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5330 case GTK_MENU_DIR_CHILD:
5331 direction = GTK_MENU_DIR_PARENT;
5333 case GTK_MENU_DIR_PARENT:
5334 direction = GTK_MENU_DIR_CHILD;
5340 /* use special table menu key bindings */
5341 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5343 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5345 if (direction == GTK_MENU_DIR_NEXT)
5347 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5349 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5357 for (i = 0; i < t; i++)
5359 match = find_child_containing (menu_shell,
5360 l, l + 1, i, i + 1);
5366 else if (direction == GTK_MENU_DIR_PREV)
5368 for (i = t; i > 0; i--)
5370 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5378 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5380 match = find_child_containing (menu_shell,
5381 l, l + 1, i - 1, i);
5387 else if (direction == GTK_MENU_DIR_PARENT)
5389 /* we go one left if possible */
5391 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5395 GtkWidget *parent = menu_shell->parent_menu_shell;
5398 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5399 match = menu_shell->active_menu_item;
5402 else if (direction == GTK_MENU_DIR_CHILD)
5404 /* we go one right if possible */
5405 if (r < gtk_menu_get_n_columns (menu))
5406 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5410 GtkWidget *parent = menu_shell->parent_menu_shell;
5412 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5414 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5415 match = menu_shell->active_menu_item;
5421 gtk_menu_shell_select_item (menu_shell, match);
5426 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5430 get_visible_size (GtkMenu *menu)
5432 GtkAllocation allocation;
5433 GtkWidget *widget = GTK_WIDGET (menu);
5434 GtkContainer *container = GTK_CONTAINER (menu);
5437 gtk_widget_get_allocation (widget, &allocation);
5438 menu_height = (allocation.height
5439 - 2 * (gtk_container_get_border_width (container)
5440 + gtk_widget_get_style (widget)->ythickness));
5442 if (!menu->tearoff_active)
5444 GtkBorder arrow_border;
5446 get_arrows_border (menu, &arrow_border);
5447 menu_height -= arrow_border.top;
5448 menu_height -= arrow_border.bottom;
5454 /* Find the sensitive on-screen child containing @y, or if none,
5455 * the nearest selectable onscreen child. (%NULL if none)
5458 child_at (GtkMenu *menu,
5461 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5462 GtkWidget *child = NULL;
5463 gint child_offset = 0;
5466 gint lower, upper; /* Onscreen bounds */
5468 menu_height = get_visible_size (menu);
5469 lower = menu->scroll_offset;
5470 upper = menu->scroll_offset + menu_height;
5472 for (children = menu_shell->children; children; children = children->next)
5474 if (gtk_widget_get_visible (children->data))
5476 GtkRequisition child_requisition;
5478 gtk_size_request_get_size (GTK_SIZE_REQUEST (children->data),
5479 &child_requisition, NULL);
5481 if (_gtk_menu_item_is_selectable (children->data) &&
5482 child_offset >= lower &&
5483 child_offset + child_requisition.height <= upper)
5485 child = children->data;
5487 if (child_offset + child_requisition.height > y &&
5488 !GTK_IS_TEAROFF_MENU_ITEM (child))
5492 child_offset += child_requisition.height;
5500 get_menu_height (GtkMenu *menu)
5502 GtkAllocation allocation;
5503 GtkWidget *widget = GTK_WIDGET (menu);
5506 gtk_widget_get_allocation (widget, &allocation);
5508 height = allocation.height;
5509 height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + gtk_widget_get_style (widget)->ythickness) * 2;
5511 if (!menu->tearoff_active)
5513 GtkBorder arrow_border;
5515 get_arrows_border (menu, &arrow_border);
5516 height -= arrow_border.top;
5517 height -= arrow_border.bottom;
5524 gtk_menu_real_move_scroll (GtkMenu *menu,
5527 gint page_size = get_visible_size (menu);
5528 gint end_position = get_menu_height (menu);
5529 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5533 case GTK_SCROLL_PAGE_UP:
5534 case GTK_SCROLL_PAGE_DOWN:
5538 gint child_offset = 0;
5539 gboolean old_upper_arrow_visible;
5542 if (type == GTK_SCROLL_PAGE_UP)
5547 if (menu_shell->active_menu_item)
5551 compute_child_offset (menu, menu_shell->active_menu_item,
5552 &child_offset, &child_height, NULL);
5553 child_offset += child_height / 2;
5556 menu_shell->ignore_enter = TRUE;
5557 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5558 old_offset = menu->scroll_offset;
5560 new_offset = menu->scroll_offset + step;
5561 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5563 gtk_menu_scroll_to (menu, new_offset);
5565 if (menu_shell->active_menu_item)
5567 GtkWidget *new_child;
5568 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5569 GtkBorder arrow_border;
5571 get_arrows_border (menu, &arrow_border);
5573 if (menu->scroll_offset != old_offset)
5574 step = menu->scroll_offset - old_offset;
5576 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5578 new_child = child_at (menu, child_offset + step);
5580 gtk_menu_shell_select_item (menu_shell, new_child);
5584 case GTK_SCROLL_START:
5585 /* Ignore the enter event we might get if the pointer is on the menu
5587 menu_shell->ignore_enter = TRUE;
5588 gtk_menu_scroll_to (menu, 0);
5589 gtk_menu_shell_select_first (menu_shell, TRUE);
5591 case GTK_SCROLL_END:
5592 /* Ignore the enter event we might get if the pointer is on the menu
5594 menu_shell->ignore_enter = TRUE;
5595 gtk_menu_scroll_to (menu, end_position - page_size);
5596 _gtk_menu_shell_select_last (menu_shell, TRUE);
5605 * gtk_menu_set_monitor:
5607 * @monitor_num: the number of the monitor on which the menu should
5610 * Informs GTK+ on which monitor a menu should be popped up.
5611 * See gdk_screen_get_monitor_geometry().
5613 * This function should be called from a #GtkMenuPositionFunc if the
5614 * menu should not appear on the same monitor as the pointer. This
5615 * information can't be reliably inferred from the coordinates returned
5616 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5617 * may extend beyond the monitor boundaries or even the screen boundaries.
5622 gtk_menu_set_monitor (GtkMenu *menu,
5625 GtkMenuPrivate *priv;
5626 g_return_if_fail (GTK_IS_MENU (menu));
5628 priv = gtk_menu_get_private (menu);
5630 priv->monitor_num = monitor_num;
5634 * gtk_menu_get_monitor:
5637 * Retrieves the number of the monitor on which to show the menu.
5639 * Returns: the number of the monitor on which the menu should
5640 * be popped up or -1, if no monitor has been set
5645 gtk_menu_get_monitor (GtkMenu *menu)
5647 GtkMenuPrivate *priv;
5648 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5650 priv = gtk_menu_get_private (menu);
5652 return priv->monitor_num;
5656 * gtk_menu_get_for_attach_widget:
5657 * @widget: a #GtkWidget
5659 * Returns a list of the menus which are attached to this widget.
5660 * This list is owned by GTK+ and must not be modified.
5662 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5667 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5671 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5673 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5678 gtk_menu_grab_notify (GtkWidget *widget,
5679 gboolean was_grabbed)
5681 GtkWidget *toplevel;
5682 GtkWindowGroup *group;
5686 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5689 !gtk_widget_device_is_shadowed (widget, pointer))
5692 toplevel = gtk_widget_get_toplevel (widget);
5694 if (!GTK_IS_WINDOW (toplevel))
5697 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5698 grab = gtk_window_group_get_current_device_grab (group, pointer);
5700 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5701 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5705 * gtk_menu_set_reserve_toggle_size:
5707 * @reserve_toggle_size: whether to reserve size for toggles
5709 * Sets whether the menu should reserve space for drawing toggles
5710 * or icons, regardless of their actual presence.
5715 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5716 gboolean reserve_toggle_size)
5718 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5719 gboolean no_toggle_size;
5721 no_toggle_size = !reserve_toggle_size;
5723 if (priv->no_toggle_size != no_toggle_size)
5725 priv->no_toggle_size = no_toggle_size;
5727 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5732 * gtk_menu_get_reserve_toggle_size:
5735 * Returns whether the menu reserves space for toggles and
5736 * icons, regardless of their actual presence.
5738 * Returns: Whether the menu reserves toggle space
5743 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5745 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5747 return !priv->no_toggle_size;