1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkaccellabel.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtkcheckmenuitem.h"
34 #include <gobject/gvaluecollector.h>
36 #include "gtkmarshalers.h"
38 #include "gtkmenuprivate.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
47 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
48 * extends below the submenu
51 #define MENU_SCROLL_STEP1 8
52 #define MENU_SCROLL_STEP2 15
53 #define MENU_SCROLL_FAST_ZONE 8
54 #define MENU_SCROLL_TIMEOUT1 50
55 #define MENU_SCROLL_TIMEOUT2 20
57 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
58 #define ATTACHED_MENUS "gtk-attached-menus"
60 typedef struct _GtkMenuAttachData GtkMenuAttachData;
61 typedef struct _GtkMenuPrivate GtkMenuPrivate;
62 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
64 struct _GtkMenuAttachData
66 GtkWidget *attach_widget;
67 GtkMenuDetachFunc detacher;
70 struct _GtkMenuPrivate
74 gboolean initially_pushed_in;
76 GDestroyNotify position_func_data_destroy;
78 /* info used for the table */
81 gint requested_height;
85 /* Cached layout information */
94 GtkStateType lower_arrow_state;
95 GtkStateType upper_arrow_state;
97 /* navigation region */
100 int navigation_width;
101 int navigation_height;
103 guint have_layout : 1;
104 guint seen_item_enter : 1;
105 guint have_position : 1;
106 guint ignore_button_release : 1;
107 guint no_toggle_size : 1;
110 struct _GtkMenuPopdownData
122 gint effective_left_attach;
123 gint effective_right_attach;
124 gint effective_top_attach;
125 gint effective_bottom_attach;
142 PROP_RESERVE_TOGGLE_SIZE
147 CHILD_PROP_LEFT_ATTACH,
148 CHILD_PROP_RIGHT_ATTACH,
149 CHILD_PROP_TOP_ATTACH,
150 CHILD_PROP_BOTTOM_ATTACH
153 static void gtk_menu_set_property (GObject *object,
157 static void gtk_menu_get_property (GObject *object,
161 static void gtk_menu_set_child_property(GtkContainer *container,
166 static void gtk_menu_get_child_property(GtkContainer *container,
171 static void gtk_menu_destroy (GtkWidget *widget);
172 static void gtk_menu_realize (GtkWidget *widget);
173 static void gtk_menu_unrealize (GtkWidget *widget);
174 static void gtk_menu_size_allocate (GtkWidget *widget,
175 GtkAllocation *allocation);
176 static void gtk_menu_show (GtkWidget *widget);
177 static gboolean gtk_menu_draw (GtkWidget *widget,
179 static gboolean gtk_menu_key_press (GtkWidget *widget,
181 static gboolean gtk_menu_scroll (GtkWidget *widget,
182 GdkEventScroll *event);
183 static gboolean gtk_menu_button_press (GtkWidget *widget,
184 GdkEventButton *event);
185 static gboolean gtk_menu_button_release (GtkWidget *widget,
186 GdkEventButton *event);
187 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
188 GdkEventMotion *event);
189 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
190 GdkEventCrossing *event);
191 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
192 GdkEventCrossing *event);
193 static void gtk_menu_scroll_to (GtkMenu *menu,
195 static void gtk_menu_grab_notify (GtkWidget *widget,
196 gboolean was_grabbed);
198 static void gtk_menu_stop_scrolling (GtkMenu *menu);
199 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
200 static gboolean gtk_menu_scroll_timeout (gpointer data);
201 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
202 static void gtk_menu_start_scrolling (GtkMenu *menu);
204 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
205 GtkWidget *menu_item);
206 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
207 GtkWidget *menu_item);
208 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
211 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
213 static void gtk_menu_handle_scrolling (GtkMenu *menu,
218 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
220 static void gtk_menu_style_set (GtkWidget *widget,
221 GtkStyle *previous_style);
222 static gboolean gtk_menu_focus (GtkWidget *widget,
223 GtkDirectionType direction);
224 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
225 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
226 GtkMenuDirectionType direction);
227 static void gtk_menu_real_move_scroll (GtkMenu *menu,
230 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
231 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
232 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
235 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
236 GtkMenuItem *menu_item,
237 GdkEventCrossing *event);
239 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
240 static void gtk_menu_show_all (GtkWidget *widget);
241 static void gtk_menu_position (GtkMenu *menu,
242 gboolean set_scroll_offset);
243 static void gtk_menu_reparent (GtkMenu *menu,
244 GtkWidget *new_parent,
246 static void gtk_menu_remove (GtkContainer *menu,
249 static void gtk_menu_update_title (GtkMenu *menu);
251 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
252 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
254 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
256 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
257 gboolean group_changed);
259 static void gtk_menu_get_preferred_width (GtkWidget *widget,
262 static void gtk_menu_get_preferred_height (GtkWidget *widget,
265 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
271 static const gchar attach_data_key[] = "gtk-menu-attach-data";
273 static guint menu_signals[LAST_SIGNAL] = { 0 };
275 static GtkMenuPrivate *
276 gtk_menu_get_private (GtkMenu *menu)
278 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
281 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
284 menu_queue_resize (GtkMenu *menu)
286 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
288 priv->have_layout = FALSE;
289 gtk_widget_queue_resize (GTK_WIDGET (menu));
293 attach_info_free (AttachInfo *info)
295 g_slice_free (AttachInfo, info);
299 get_attach_info (GtkWidget *child)
301 GObject *object = G_OBJECT (child);
302 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
306 ai = g_slice_new0 (AttachInfo);
307 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
308 (GDestroyNotify) attach_info_free);
315 is_grid_attached (AttachInfo *ai)
317 return (ai->left_attach >= 0 &&
318 ai->right_attach >= 0 &&
319 ai->top_attach >= 0 &&
320 ai->bottom_attach >= 0);
324 menu_ensure_layout (GtkMenu *menu)
326 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
328 if (!priv->have_layout)
330 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
334 gint max_right_attach;
335 gint max_bottom_attach;
337 /* Find extents of gridded portion
339 max_right_attach = 1;
340 max_bottom_attach = 0;
342 for (l = menu_shell->children; l; l = l->next)
344 GtkWidget *child = l->data;
345 AttachInfo *ai = get_attach_info (child);
347 if (is_grid_attached (ai))
349 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
350 max_right_attach = MAX (max_right_attach, ai->right_attach);
356 row_occupied = g_malloc0 (max_bottom_attach);
358 for (l = menu_shell->children; l; l = l->next)
360 GtkWidget *child = l->data;
361 AttachInfo *ai = get_attach_info (child);
363 if (is_grid_attached (ai))
367 for (i = ai->top_attach; i < ai->bottom_attach; i++)
368 row_occupied[i] = TRUE;
372 /* Lay non-grid-items out in those rows
375 for (l = menu_shell->children; l; l = l->next)
377 GtkWidget *child = l->data;
378 AttachInfo *ai = get_attach_info (child);
380 if (!is_grid_attached (ai))
382 while (current_row < max_bottom_attach && row_occupied[current_row])
385 ai->effective_left_attach = 0;
386 ai->effective_right_attach = max_right_attach;
387 ai->effective_top_attach = current_row;
388 ai->effective_bottom_attach = current_row + 1;
394 ai->effective_left_attach = ai->left_attach;
395 ai->effective_right_attach = ai->right_attach;
396 ai->effective_top_attach = ai->top_attach;
397 ai->effective_bottom_attach = ai->bottom_attach;
401 g_free (row_occupied);
403 priv->n_rows = MAX (current_row, max_bottom_attach);
404 priv->n_columns = max_right_attach;
405 priv->have_layout = TRUE;
411 gtk_menu_get_n_columns (GtkMenu *menu)
413 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
415 menu_ensure_layout (menu);
417 return priv->n_columns;
421 gtk_menu_get_n_rows (GtkMenu *menu)
423 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
425 menu_ensure_layout (menu);
431 get_effective_child_attach (GtkWidget *child,
437 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
440 menu_ensure_layout (menu);
442 ai = get_attach_info (child);
445 *l = ai->effective_left_attach;
447 *r = ai->effective_right_attach;
449 *t = ai->effective_top_attach;
451 *b = ai->effective_bottom_attach;
456 gtk_menu_class_init (GtkMenuClass *class)
458 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
459 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
460 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
461 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
462 GtkBindingSet *binding_set;
464 gobject_class->set_property = gtk_menu_set_property;
465 gobject_class->get_property = gtk_menu_get_property;
467 widget_class->destroy = gtk_menu_destroy;
468 widget_class->realize = gtk_menu_realize;
469 widget_class->unrealize = gtk_menu_unrealize;
470 widget_class->size_allocate = gtk_menu_size_allocate;
471 widget_class->show = gtk_menu_show;
472 widget_class->draw = gtk_menu_draw;
473 widget_class->scroll_event = gtk_menu_scroll;
474 widget_class->key_press_event = gtk_menu_key_press;
475 widget_class->button_press_event = gtk_menu_button_press;
476 widget_class->button_release_event = gtk_menu_button_release;
477 widget_class->motion_notify_event = gtk_menu_motion_notify;
478 widget_class->show_all = gtk_menu_show_all;
479 widget_class->enter_notify_event = gtk_menu_enter_notify;
480 widget_class->leave_notify_event = gtk_menu_leave_notify;
481 widget_class->style_set = gtk_menu_style_set;
482 widget_class->focus = gtk_menu_focus;
483 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
484 widget_class->grab_notify = gtk_menu_grab_notify;
485 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
486 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
487 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
489 container_class->remove = gtk_menu_remove;
490 container_class->get_child_property = gtk_menu_get_child_property;
491 container_class->set_child_property = gtk_menu_set_child_property;
493 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
494 menu_shell_class->deactivate = gtk_menu_deactivate;
495 menu_shell_class->select_item = gtk_menu_select_item;
496 menu_shell_class->insert = gtk_menu_real_insert;
497 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
498 menu_shell_class->move_current = gtk_menu_move_current;
500 menu_signals[MOVE_SCROLL] =
501 g_signal_new_class_handler (I_("move-scroll"),
502 G_OBJECT_CLASS_TYPE (gobject_class),
503 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
504 G_CALLBACK (gtk_menu_real_move_scroll),
506 _gtk_marshal_VOID__ENUM,
508 GTK_TYPE_SCROLL_TYPE);
513 * The index of the currently selected menu item, or -1 if no
514 * menu item is selected.
518 g_object_class_install_property (gobject_class,
520 g_param_spec_int ("active",
522 P_("The currently selected menu item"),
524 GTK_PARAM_READWRITE));
527 * GtkMenu:accel-group:
529 * The accel group holding accelerators for the menu.
533 g_object_class_install_property (gobject_class,
535 g_param_spec_object ("accel-group",
537 P_("The accel group holding accelerators for the menu"),
538 GTK_TYPE_ACCEL_GROUP,
539 GTK_PARAM_READWRITE));
542 * GtkMenu:accel-path:
544 * An accel path used to conveniently construct accel paths of child items.
548 g_object_class_install_property (gobject_class,
550 g_param_spec_string ("accel-path",
552 P_("An accel path used to conveniently construct accel paths of child items"),
554 GTK_PARAM_READWRITE));
557 * GtkMenu:attach-widget:
559 * The widget the menu is attached to. Setting this property attaches
560 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
561 * use gtk_menu_attach_to_widget() directly.
565 g_object_class_install_property (gobject_class,
567 g_param_spec_object ("attach-widget",
569 P_("The widget the menu is attached to"),
571 GTK_PARAM_READWRITE));
573 g_object_class_install_property (gobject_class,
575 g_param_spec_string ("tearoff-title",
577 P_("A title that may be displayed by the window manager when this menu is torn-off"),
579 GTK_PARAM_READWRITE));
582 * GtkMenu:tearoff-state:
584 * A boolean that indicates whether the menu is torn-off.
588 g_object_class_install_property (gobject_class,
590 g_param_spec_boolean ("tearoff-state",
592 P_("A boolean that indicates whether the menu is torn-off"),
594 GTK_PARAM_READWRITE));
599 * The monitor the menu will be popped up on.
603 g_object_class_install_property (gobject_class,
605 g_param_spec_int ("monitor",
607 P_("The monitor the menu will be popped up on"),
609 GTK_PARAM_READWRITE));
611 gtk_widget_class_install_style_property (widget_class,
612 g_param_spec_int ("vertical-padding",
613 P_("Vertical Padding"),
614 P_("Extra space at the top and bottom of the menu"),
618 GTK_PARAM_READABLE));
621 * GtkMenu:reserve-toggle-size:
623 * A boolean that indicates whether the menu reserves space for
624 * toggles and icons, regardless of their actual presence.
626 * This property should only be changed from its default value
627 * for special-purposes such as tabular menus. Regular menus that
628 * are connected to a menu bar or context menus should reserve
629 * toggle space for consistency.
633 g_object_class_install_property (gobject_class,
634 PROP_RESERVE_TOGGLE_SIZE,
635 g_param_spec_boolean ("reserve-toggle-size",
636 P_("Reserve Toggle Size"),
637 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
639 GTK_PARAM_READWRITE));
641 gtk_widget_class_install_style_property (widget_class,
642 g_param_spec_int ("horizontal-padding",
643 P_("Horizontal Padding"),
644 P_("Extra space at the left and right edges of the menu"),
648 GTK_PARAM_READABLE));
650 gtk_widget_class_install_style_property (widget_class,
651 g_param_spec_int ("vertical-offset",
652 P_("Vertical Offset"),
653 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
657 GTK_PARAM_READABLE));
659 gtk_widget_class_install_style_property (widget_class,
660 g_param_spec_int ("horizontal-offset",
661 P_("Horizontal Offset"),
662 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
666 GTK_PARAM_READABLE));
668 gtk_widget_class_install_style_property (widget_class,
669 g_param_spec_boolean ("double-arrows",
671 P_("When scrolling, always show both arrows."),
673 GTK_PARAM_READABLE));
676 * GtkMenu:arrow-placement:
678 * Indicates where scroll arrows should be placed.
682 gtk_widget_class_install_style_property (widget_class,
683 g_param_spec_enum ("arrow-placement",
684 P_("Arrow Placement"),
685 P_("Indicates where scroll arrows should be placed"),
686 GTK_TYPE_ARROW_PLACEMENT,
688 GTK_PARAM_READABLE));
690 gtk_container_class_install_child_property (container_class,
691 CHILD_PROP_LEFT_ATTACH,
692 g_param_spec_int ("left-attach",
694 P_("The column number to attach the left side of the child to"),
696 GTK_PARAM_READWRITE));
698 gtk_container_class_install_child_property (container_class,
699 CHILD_PROP_RIGHT_ATTACH,
700 g_param_spec_int ("right-attach",
702 P_("The column number to attach the right side of the child to"),
704 GTK_PARAM_READWRITE));
706 gtk_container_class_install_child_property (container_class,
707 CHILD_PROP_TOP_ATTACH,
708 g_param_spec_int ("top-attach",
710 P_("The row number to attach the top of the child to"),
712 GTK_PARAM_READWRITE));
714 gtk_container_class_install_child_property (container_class,
715 CHILD_PROP_BOTTOM_ATTACH,
716 g_param_spec_int ("bottom-attach",
718 P_("The row number to attach the bottom of the child to"),
720 GTK_PARAM_READWRITE));
723 * GtkMenu::arrow-scaling
725 * Arbitrary constant to scale down the size of the scroll arrow.
729 gtk_widget_class_install_style_property (widget_class,
730 g_param_spec_float ("arrow-scaling",
732 P_("Arbitrary constant to scale down the size of the scroll arrow"),
734 GTK_PARAM_READABLE));
736 binding_set = gtk_binding_set_by_class (class);
737 gtk_binding_entry_add_signal (binding_set,
739 I_("move-current"), 1,
740 GTK_TYPE_MENU_DIRECTION_TYPE,
742 gtk_binding_entry_add_signal (binding_set,
745 GTK_TYPE_MENU_DIRECTION_TYPE,
747 gtk_binding_entry_add_signal (binding_set,
750 GTK_TYPE_MENU_DIRECTION_TYPE,
752 gtk_binding_entry_add_signal (binding_set,
755 GTK_TYPE_MENU_DIRECTION_TYPE,
757 gtk_binding_entry_add_signal (binding_set,
760 GTK_TYPE_MENU_DIRECTION_TYPE,
761 GTK_MENU_DIR_PARENT);
762 gtk_binding_entry_add_signal (binding_set,
765 GTK_TYPE_MENU_DIRECTION_TYPE,
766 GTK_MENU_DIR_PARENT);
767 gtk_binding_entry_add_signal (binding_set,
770 GTK_TYPE_MENU_DIRECTION_TYPE,
772 gtk_binding_entry_add_signal (binding_set,
775 GTK_TYPE_MENU_DIRECTION_TYPE,
777 gtk_binding_entry_add_signal (binding_set,
780 GTK_TYPE_SCROLL_TYPE,
782 gtk_binding_entry_add_signal (binding_set,
785 GTK_TYPE_SCROLL_TYPE,
787 gtk_binding_entry_add_signal (binding_set,
790 GTK_TYPE_SCROLL_TYPE,
792 gtk_binding_entry_add_signal (binding_set,
795 GTK_TYPE_SCROLL_TYPE,
797 gtk_binding_entry_add_signal (binding_set,
800 GTK_TYPE_SCROLL_TYPE,
802 gtk_binding_entry_add_signal (binding_set,
803 GDK_KEY_KP_Page_Up, 0,
805 GTK_TYPE_SCROLL_TYPE,
807 gtk_binding_entry_add_signal (binding_set,
808 GDK_KEY_Page_Down, 0,
810 GTK_TYPE_SCROLL_TYPE,
811 GTK_SCROLL_PAGE_DOWN);
812 gtk_binding_entry_add_signal (binding_set,
813 GDK_KEY_KP_Page_Down, 0,
815 GTK_TYPE_SCROLL_TYPE,
816 GTK_SCROLL_PAGE_DOWN);
818 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
823 gtk_menu_set_property (GObject *object,
828 GtkMenu *menu = GTK_MENU (object);
833 gtk_menu_set_active (menu, g_value_get_int (value));
835 case PROP_ACCEL_GROUP:
836 gtk_menu_set_accel_group (menu, g_value_get_object (value));
838 case PROP_ACCEL_PATH:
839 gtk_menu_set_accel_path (menu, g_value_get_string (value));
841 case PROP_ATTACH_WIDGET:
845 widget = gtk_menu_get_attach_widget (menu);
847 gtk_menu_detach (menu);
849 widget = (GtkWidget*) g_value_get_object (value);
851 gtk_menu_attach_to_widget (menu, widget, NULL);
854 case PROP_TEAROFF_STATE:
855 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
857 case PROP_TEAROFF_TITLE:
858 gtk_menu_set_title (menu, g_value_get_string (value));
861 gtk_menu_set_monitor (menu, g_value_get_int (value));
863 case PROP_RESERVE_TOGGLE_SIZE:
864 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
867 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
873 gtk_menu_get_property (GObject *object,
878 GtkMenu *menu = GTK_MENU (object);
883 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
885 case PROP_ACCEL_GROUP:
886 g_value_set_object (value, gtk_menu_get_accel_group (menu));
888 case PROP_ACCEL_PATH:
889 g_value_set_string (value, gtk_menu_get_accel_path (menu));
891 case PROP_ATTACH_WIDGET:
892 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
894 case PROP_TEAROFF_STATE:
895 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
897 case PROP_TEAROFF_TITLE:
898 g_value_set_string (value, gtk_menu_get_title (menu));
901 g_value_set_int (value, gtk_menu_get_monitor (menu));
903 case PROP_RESERVE_TOGGLE_SIZE:
904 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
907 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
913 gtk_menu_set_child_property (GtkContainer *container,
919 GtkMenu *menu = GTK_MENU (container);
920 AttachInfo *ai = get_attach_info (child);
924 case CHILD_PROP_LEFT_ATTACH:
925 ai->left_attach = g_value_get_int (value);
927 case CHILD_PROP_RIGHT_ATTACH:
928 ai->right_attach = g_value_get_int (value);
930 case CHILD_PROP_TOP_ATTACH:
931 ai->top_attach = g_value_get_int (value);
933 case CHILD_PROP_BOTTOM_ATTACH:
934 ai->bottom_attach = g_value_get_int (value);
938 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
942 menu_queue_resize (menu);
946 gtk_menu_get_child_property (GtkContainer *container,
952 AttachInfo *ai = get_attach_info (child);
956 case CHILD_PROP_LEFT_ATTACH:
957 g_value_set_int (value, ai->left_attach);
959 case CHILD_PROP_RIGHT_ATTACH:
960 g_value_set_int (value, ai->right_attach);
962 case CHILD_PROP_TOP_ATTACH:
963 g_value_set_int (value, ai->top_attach);
965 case CHILD_PROP_BOTTOM_ATTACH:
966 g_value_set_int (value, ai->bottom_attach);
970 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
976 gtk_menu_window_event (GtkWidget *window,
980 gboolean handled = FALSE;
982 g_object_ref (window);
988 case GDK_KEY_RELEASE:
989 handled = gtk_widget_event (menu, event);
995 g_object_unref (window);
996 g_object_unref (menu);
1002 gtk_menu_init (GtkMenu *menu)
1004 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1006 menu->parent_menu_item = NULL;
1007 menu->old_active_menu_item = NULL;
1008 menu->accel_group = NULL;
1009 menu->position_func = NULL;
1010 menu->position_func_data = NULL;
1011 menu->toggle_size = 0;
1013 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1014 "type", GTK_WINDOW_POPUP,
1017 "signal::event", gtk_menu_window_event, menu,
1018 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1020 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1021 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1023 /* Refloat the menu, so that reference counting for the menu isn't
1024 * affected by it being a child of the toplevel
1026 g_object_force_floating (G_OBJECT (menu));
1027 menu->needs_destruction_ref_count = TRUE;
1029 menu->view_window = NULL;
1030 menu->bin_window = NULL;
1032 menu->scroll_offset = 0;
1033 menu->scroll_step = 0;
1034 menu->timeout_id = 0;
1035 menu->scroll_fast = FALSE;
1037 menu->tearoff_window = NULL;
1038 menu->tearoff_hbox = NULL;
1039 menu->torn_off = FALSE;
1040 menu->tearoff_active = FALSE;
1041 menu->tearoff_adjustment = NULL;
1042 menu->tearoff_scrollbar = NULL;
1044 menu->upper_arrow_visible = FALSE;
1045 menu->lower_arrow_visible = FALSE;
1046 menu->upper_arrow_prelight = FALSE;
1047 menu->lower_arrow_prelight = FALSE;
1049 priv->upper_arrow_state = GTK_STATE_NORMAL;
1050 priv->lower_arrow_state = GTK_STATE_NORMAL;
1052 priv->have_layout = FALSE;
1053 priv->monitor_num = -1;
1057 gtk_menu_destroy (GtkWidget *widget)
1059 GtkMenu *menu = GTK_MENU (widget);
1060 GtkMenuAttachData *data;
1061 GtkMenuPrivate *priv;
1063 gtk_menu_remove_scroll_timeout (menu);
1065 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1067 gtk_menu_detach (menu);
1069 gtk_menu_stop_navigating_submenu (menu);
1071 if (menu->old_active_menu_item)
1073 g_object_unref (menu->old_active_menu_item);
1074 menu->old_active_menu_item = NULL;
1077 /* Add back the reference count for being a child */
1078 if (menu->needs_destruction_ref_count)
1080 menu->needs_destruction_ref_count = FALSE;
1081 g_object_ref (widget);
1084 if (menu->accel_group)
1086 g_object_unref (menu->accel_group);
1087 menu->accel_group = NULL;
1091 gtk_widget_destroy (menu->toplevel);
1093 if (menu->tearoff_window)
1094 gtk_widget_destroy (menu->tearoff_window);
1096 priv = gtk_menu_get_private (menu);
1100 g_free (priv->heights);
1101 priv->heights = NULL;
1106 g_free (priv->title);
1110 if (priv->position_func_data_destroy)
1112 priv->position_func_data_destroy (menu->position_func_data);
1113 menu->position_func_data = NULL;
1114 priv->position_func_data_destroy = NULL;
1117 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1121 menu_change_screen (GtkMenu *menu,
1122 GdkScreen *new_screen)
1124 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1126 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1128 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1134 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1135 gtk_menu_position (menu, TRUE);
1138 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1139 private->monitor_num = -1;
1143 attach_widget_screen_changed (GtkWidget *attach_widget,
1144 GdkScreen *previous_screen,
1147 if (gtk_widget_has_screen (attach_widget) &&
1148 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1150 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1155 gtk_menu_attach_to_widget (GtkMenu *menu,
1156 GtkWidget *attach_widget,
1157 GtkMenuDetachFunc detacher)
1159 GtkMenuAttachData *data;
1162 g_return_if_fail (GTK_IS_MENU (menu));
1163 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1165 /* keep this function in sync with gtk_widget_set_parent()
1168 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1171 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1172 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1176 g_object_ref_sink (menu);
1178 data = g_slice_new (GtkMenuAttachData);
1179 data->attach_widget = attach_widget;
1181 g_signal_connect (attach_widget, "screen-changed",
1182 G_CALLBACK (attach_widget_screen_changed), menu);
1183 attach_widget_screen_changed (attach_widget, NULL, menu);
1185 data->detacher = detacher;
1186 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1187 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1188 if (!g_list_find (list, menu))
1190 list = g_list_prepend (list, menu);
1192 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1193 (GDestroyNotify) g_list_free);
1195 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1196 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1198 /* we don't need to set the style here, since
1199 * we are a toplevel widget.
1202 /* Fallback title for menu comes from attach widget */
1203 gtk_menu_update_title (menu);
1205 g_object_notify (G_OBJECT (menu), "attach-widget");
1209 gtk_menu_get_attach_widget (GtkMenu *menu)
1211 GtkMenuAttachData *data;
1213 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1215 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1217 return data->attach_widget;
1222 gtk_menu_detach (GtkMenu *menu)
1224 GtkMenuAttachData *data;
1227 g_return_if_fail (GTK_IS_MENU (menu));
1229 /* keep this function in sync with gtk_widget_unparent()
1231 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1234 g_warning ("gtk_menu_detach(): menu is not attached");
1237 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1239 g_signal_handlers_disconnect_by_func (data->attach_widget,
1240 (gpointer) attach_widget_screen_changed,
1244 data->detacher (data->attach_widget, menu);
1245 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1246 list = g_list_remove (list, menu);
1248 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1249 (GDestroyNotify) g_list_free);
1251 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1253 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1254 gtk_widget_unrealize (GTK_WIDGET (menu));
1256 g_slice_free (GtkMenuAttachData, data);
1258 /* Fallback title for menu comes from attach widget */
1259 gtk_menu_update_title (menu);
1261 g_object_unref (menu);
1265 gtk_menu_remove (GtkContainer *container,
1268 GtkMenu *menu = GTK_MENU (container);
1270 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1272 /* Clear out old_active_menu_item if it matches the item we are removing
1274 if (menu->old_active_menu_item == widget)
1276 g_object_unref (menu->old_active_menu_item);
1277 menu->old_active_menu_item = NULL;
1280 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1281 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1283 menu_queue_resize (menu);
1289 return g_object_new (GTK_TYPE_MENU, NULL);
1293 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1297 GtkMenu *menu = GTK_MENU (menu_shell);
1298 AttachInfo *ai = get_attach_info (child);
1300 ai->left_attach = -1;
1301 ai->right_attach = -1;
1302 ai->top_attach = -1;
1303 ai->bottom_attach = -1;
1305 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1306 gtk_widget_set_parent_window (child, menu->bin_window);
1308 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1310 menu_queue_resize (menu);
1314 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1319 widget = GTK_WIDGET (menu);
1324 cairo_surface_t *surface;
1325 cairo_pattern_t *pattern;
1328 menu->tearoff_active = FALSE;
1329 menu->saved_scroll_offset = menu->scroll_offset;
1331 window = gtk_widget_get_window (menu->tearoff_window);
1332 width = gdk_window_get_width (window);
1333 height = gdk_window_get_height (window);
1335 surface = gdk_window_create_similar_surface (window,
1336 CAIRO_CONTENT_COLOR,
1340 cr = cairo_create (surface);
1341 gdk_cairo_set_source_window (cr,
1347 gtk_widget_set_size_request (menu->tearoff_window,
1351 pattern = cairo_pattern_create_for_surface (surface);
1352 gdk_window_set_background_pattern (window, pattern);
1354 cairo_pattern_destroy (pattern);
1355 cairo_surface_destroy (surface);
1360 popup_grab_on_window (GdkWindow *window,
1361 GdkDevice *keyboard,
1363 guint32 activate_time)
1366 gdk_device_grab (keyboard, window,
1367 GDK_OWNERSHIP_WINDOW, TRUE,
1368 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1369 NULL, activate_time) != GDK_GRAB_SUCCESS)
1373 gdk_device_grab (pointer, window,
1374 GDK_OWNERSHIP_WINDOW, TRUE,
1375 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1376 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1377 GDK_POINTER_MOTION_MASK,
1378 NULL, activate_time) != GDK_GRAB_SUCCESS)
1381 gdk_device_ungrab (keyboard, activate_time);
1390 * gtk_menu_popup_for_device:
1391 * @menu: a #GtkMenu.
1392 * @device: (allow-none): a #GdkDevice
1393 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1394 * menu item, or %NULL
1395 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1396 * the popup, or %NULL
1397 * @func: (allow-none): a user supplied function used to position the menu,
1399 * @data: (allow-none): user supplied data to be passed to @func
1400 * @destroy: (allow-none): destroy notify for @data
1401 * @button: the mouse button which was pressed to initiate the event
1402 * @activate_time: the time at which the activation event occurred
1404 * Displays a menu and makes it available for selection.
1406 * Applications can use this function to display context-sensitive menus,
1407 * and will typically supply %NULL for the @parent_menu_shell,
1408 * @parent_menu_item, @func, @data and @destroy parameters. The default
1409 * menu positioning function will position the menu at the current position
1410 * of @device (or its corresponding pointer).
1412 * The @button parameter should be the mouse button pressed to initiate
1413 * the menu popup. If the menu popup was initiated by something other than
1414 * a mouse button press, such as a mouse button release or a keypress,
1415 * @button should be 0.
1417 * The @activate_time parameter is used to conflict-resolve initiation of
1418 * concurrent requests for mouse/keyboard grab requests. To function
1419 * properly, this needs to be the time stamp of the user event (such as
1420 * a mouse click or key press) that caused the initiation of the popup.
1421 * Only if no such event is available, gtk_get_current_event_time() can
1427 gtk_menu_popup_for_device (GtkMenu *menu,
1429 GtkWidget *parent_menu_shell,
1430 GtkWidget *parent_menu_item,
1431 GtkMenuPositionFunc func,
1433 GDestroyNotify destroy,
1435 guint32 activate_time)
1438 GtkWidget *xgrab_shell;
1440 GdkEvent *current_event;
1441 GtkMenuShell *menu_shell;
1442 gboolean grab_keyboard;
1443 GtkMenuPrivate *priv;
1444 GtkWidget *parent_toplevel;
1445 GdkDevice *keyboard, *pointer;
1447 g_return_if_fail (GTK_IS_MENU (menu));
1448 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1451 device = gtk_get_current_event_device ();
1455 GdkDisplay *display;
1456 GdkDeviceManager *device_manager;
1459 display = gtk_widget_get_display (GTK_WIDGET (menu));
1460 device_manager = gdk_display_get_device_manager (display);
1461 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1463 device = devices->data;
1465 g_list_free (devices);
1468 widget = GTK_WIDGET (menu);
1469 menu_shell = GTK_MENU_SHELL (menu);
1470 priv = gtk_menu_get_private (menu);
1472 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1475 pointer = gdk_device_get_associated_device (device);
1480 keyboard = gdk_device_get_associated_device (device);
1483 menu_shell->parent_menu_shell = parent_menu_shell;
1485 priv->seen_item_enter = FALSE;
1487 /* Find the last viewable ancestor, and make an X grab on it
1489 parent = GTK_WIDGET (menu);
1493 gboolean viewable = TRUE;
1494 GtkWidget *tmp = parent;
1498 if (!gtk_widget_get_mapped (tmp))
1503 tmp = gtk_widget_get_parent (tmp);
1507 xgrab_shell = parent;
1509 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1512 /* We want to receive events generated when we map the menu; unfortunately,
1513 * since there is probably already an implicit grab in place from the
1514 * button that the user used to pop up the menu, we won't receive then --
1515 * in particular, the EnterNotify when the menu pops up under the pointer.
1517 * If we are grabbing on a parent menu shell, no problem; just grab on
1518 * that menu shell first before popping up the window with owner_events = TRUE.
1520 * When grabbing on the menu itself, things get more convuluted - we
1521 * we do an explicit grab on a specially created window with
1522 * owner_events = TRUE, which we override further down with a grab
1523 * on the menu. (We can't grab on the menu until it is mapped; we
1524 * probably could just leave the grab on the other window, with a
1525 * little reorganization of the code in gtkmenu*).
1527 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1528 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1533 if (xgrab_shell && xgrab_shell != widget)
1535 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1537 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1538 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1543 GdkWindow *transfer_window;
1545 xgrab_shell = widget;
1546 transfer_window = menu_grab_transfer_window_get (menu);
1547 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1549 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1550 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1554 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1556 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1557 * with a stuck up window, we just abort here. Presumably the user will
1560 menu_shell->parent_menu_shell = NULL;
1561 menu_grab_transfer_window_destroy (menu);
1565 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1566 menu_shell->active = TRUE;
1567 menu_shell->button = button;
1569 /* If we are popping up the menu from something other than, a button
1570 * press then, as a heuristic, we ignore enter events for the menu
1571 * until we get a MOTION_NOTIFY.
1574 current_event = gtk_get_current_event ();
1577 if ((current_event->type != GDK_BUTTON_PRESS) &&
1578 (current_event->type != GDK_ENTER_NOTIFY))
1579 menu_shell->ignore_enter = TRUE;
1581 gdk_event_free (current_event);
1584 menu_shell->ignore_enter = TRUE;
1588 gtk_menu_tearoff_bg_copy (menu);
1590 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1593 parent_toplevel = NULL;
1594 if (parent_menu_shell)
1595 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1596 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1598 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1600 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1603 /* Set transient for to get the right window group and parent relationship */
1604 if (GTK_IS_WINDOW (parent_toplevel))
1605 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1606 GTK_WINDOW (parent_toplevel));
1608 menu->parent_menu_item = parent_menu_item;
1609 menu->position_func = func;
1610 menu->position_func_data = data;
1611 priv->position_func_data_destroy = destroy;
1612 menu_shell->activate_time = activate_time;
1614 /* We need to show the menu here rather in the init function because
1615 * code expects to be able to tell if the menu is onscreen by
1616 * looking at the gtk_widget_get_visible (menu)
1618 gtk_widget_show (GTK_WIDGET (menu));
1620 /* Position the menu, possibly changing the size request
1622 gtk_menu_position (menu, TRUE);
1624 /* Compute the size of the toplevel and realize it so we
1625 * can scroll correctly.
1628 GtkRequisition tmp_request;
1629 GtkAllocation tmp_allocation = { 0, };
1631 /* Instead of trusting the menu position function to queue a resize when the
1632 * menu goes out of bounds, invalidate the cached size here. */
1633 gtk_widget_queue_resize (GTK_WIDGET (menu));
1634 gtk_widget_get_preferred_size (menu->toplevel, &tmp_request, NULL);
1636 tmp_allocation.width = tmp_request.width;
1637 tmp_allocation.height = tmp_request.height;
1639 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1641 gtk_widget_realize (GTK_WIDGET (menu));
1644 gtk_menu_scroll_to (menu, menu->scroll_offset);
1646 /* if no item is selected, select the first one */
1647 if (!menu_shell->active_menu_item)
1649 gboolean touchscreen_mode;
1651 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1652 "gtk-touchscreen-mode", &touchscreen_mode,
1655 if (touchscreen_mode)
1656 gtk_menu_shell_select_first (menu_shell, TRUE);
1659 /* Once everything is set up correctly, map the toplevel window on
1662 gtk_widget_show (menu->toplevel);
1664 if (xgrab_shell == widget)
1665 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1667 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1669 if (parent_menu_shell)
1671 gboolean keyboard_mode;
1673 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1674 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1676 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1677 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1679 _gtk_menu_shell_update_mnemonics (menu_shell);
1684 * @menu: a #GtkMenu.
1685 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1686 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1687 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1688 * @data: (allow-none): user supplied data to be passed to @func.
1689 * @button: the mouse button which was pressed to initiate the event.
1690 * @activate_time: the time at which the activation event occurred.
1692 * Displays a menu and makes it available for selection. Applications can use
1693 * this function to display context-sensitive menus, and will typically supply
1694 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1695 * parameters. The default menu positioning function will position the menu
1696 * at the current mouse cursor position.
1698 * The @button parameter should be the mouse button pressed to initiate
1699 * the menu popup. If the menu popup was initiated by something other than
1700 * a mouse button press, such as a mouse button release or a keypress,
1701 * @button should be 0.
1703 * The @activate_time parameter is used to conflict-resolve initiation of
1704 * concurrent requests for mouse/keyboard grab requests. To function
1705 * properly, this needs to be the time stamp of the user event (such as
1706 * a mouse click or key press) that caused the initiation of the popup.
1707 * Only if no such event is available, gtk_get_current_event_time() can
1711 gtk_menu_popup (GtkMenu *menu,
1712 GtkWidget *parent_menu_shell,
1713 GtkWidget *parent_menu_item,
1714 GtkMenuPositionFunc func,
1717 guint32 activate_time)
1719 g_return_if_fail (GTK_IS_MENU (menu));
1721 gtk_menu_popup_for_device (menu,
1726 button, activate_time);
1730 gtk_menu_popdown (GtkMenu *menu)
1732 GtkMenuPrivate *private;
1733 GtkMenuShell *menu_shell;
1736 g_return_if_fail (GTK_IS_MENU (menu));
1738 menu_shell = GTK_MENU_SHELL (menu);
1739 private = gtk_menu_get_private (menu);
1741 menu_shell->parent_menu_shell = NULL;
1742 menu_shell->active = FALSE;
1743 menu_shell->ignore_enter = FALSE;
1745 private->have_position = FALSE;
1747 gtk_menu_stop_scrolling (menu);
1749 gtk_menu_stop_navigating_submenu (menu);
1751 if (menu_shell->active_menu_item)
1753 if (menu->old_active_menu_item)
1754 g_object_unref (menu->old_active_menu_item);
1755 menu->old_active_menu_item = menu_shell->active_menu_item;
1756 g_object_ref (menu->old_active_menu_item);
1759 gtk_menu_shell_deselect (menu_shell);
1761 /* The X Grab, if present, will automatically be removed when we hide
1763 gtk_widget_hide (menu->toplevel);
1764 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1766 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1770 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1772 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1774 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1778 /* We popped up the menu from the tearoff, so we need to
1779 * release the grab - we aren't actually hiding the menu.
1781 if (menu_shell->have_xgrab && pointer)
1783 GdkDevice *keyboard;
1785 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1786 keyboard = gdk_device_get_associated_device (pointer);
1789 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1793 /* gtk_menu_popdown is called each time a menu item is selected from
1794 * a torn off menu. Only scroll back to the saved position if the
1795 * non-tearoff menu was popped down.
1797 if (!menu->tearoff_active)
1798 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1799 menu->tearoff_active = TRUE;
1802 gtk_widget_hide (GTK_WIDGET (menu));
1804 menu_shell->have_xgrab = FALSE;
1807 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1809 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1811 menu_grab_transfer_window_destroy (menu);
1815 gtk_menu_get_active (GtkMenu *menu)
1820 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1822 if (!menu->old_active_menu_item)
1825 children = GTK_MENU_SHELL (menu)->children;
1829 child = children->data;
1830 children = children->next;
1832 if (gtk_bin_get_child (GTK_BIN (child)))
1837 menu->old_active_menu_item = child;
1838 if (menu->old_active_menu_item)
1839 g_object_ref (menu->old_active_menu_item);
1842 return menu->old_active_menu_item;
1846 gtk_menu_set_active (GtkMenu *menu,
1852 g_return_if_fail (GTK_IS_MENU (menu));
1854 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1857 child = tmp_list->data;
1858 if (gtk_bin_get_child (GTK_BIN (child)))
1860 if (menu->old_active_menu_item)
1861 g_object_unref (menu->old_active_menu_item);
1862 menu->old_active_menu_item = child;
1863 g_object_ref (menu->old_active_menu_item);
1870 * gtk_menu_set_accel_group:
1871 * @accel_group: (allow-none):
1874 gtk_menu_set_accel_group (GtkMenu *menu,
1875 GtkAccelGroup *accel_group)
1877 g_return_if_fail (GTK_IS_MENU (menu));
1879 if (menu->accel_group != accel_group)
1881 if (menu->accel_group)
1882 g_object_unref (menu->accel_group);
1883 menu->accel_group = accel_group;
1884 if (menu->accel_group)
1885 g_object_ref (menu->accel_group);
1886 _gtk_menu_refresh_accel_paths (menu, TRUE);
1891 gtk_menu_get_accel_group (GtkMenu *menu)
1893 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1895 return menu->accel_group;
1899 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1902 /* Menu items chain here to figure whether they can activate their
1903 * accelerators. Unlike ordinary widgets, menus allow accel
1904 * activation even if invisible since that's the usual case for
1905 * submenus/popup-menus. however, the state of the attach widget
1906 * affects the "activeness" of the menu.
1908 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1911 return gtk_widget_can_activate_accel (awidget, signal_id);
1913 return gtk_widget_is_sensitive (widget);
1917 * gtk_menu_set_accel_path
1918 * @menu: a valid #GtkMenu
1919 * @accel_path: (allow-none): a valid accelerator path
1921 * Sets an accelerator path for this menu from which accelerator paths
1922 * for its immediate children, its menu items, can be constructed.
1923 * The main purpose of this function is to spare the programmer the
1924 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1925 * each menu item that should support runtime user changable accelerators.
1926 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1927 * each menu item of this menu, that contains a label describing its purpose,
1928 * automatically gets an accel path assigned. For example, a menu containing
1929 * menu items "New" and "Exit", will, after
1930 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1931 * has been called, assign its items the accel paths:
1932 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1933 * Assigning accel paths to menu items then enables the user to change
1934 * their accelerators at runtime. More details about accelerator paths
1935 * and their default setups can be found at gtk_accel_map_add_entry().
1937 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1938 * pass a static string, you can save some memory by interning it first with
1939 * g_intern_static_string().
1942 gtk_menu_set_accel_path (GtkMenu *menu,
1943 const gchar *accel_path)
1945 g_return_if_fail (GTK_IS_MENU (menu));
1947 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1949 /* FIXME: accel_path should be defined as const gchar* */
1950 menu->accel_path = (gchar*)g_intern_string (accel_path);
1951 if (menu->accel_path)
1952 _gtk_menu_refresh_accel_paths (menu, FALSE);
1956 * gtk_menu_get_accel_path
1957 * @menu: a valid #GtkMenu
1959 * Retrieves the accelerator path set on the menu.
1961 * Returns: the accelerator path set on the menu.
1966 gtk_menu_get_accel_path (GtkMenu *menu)
1968 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1970 return menu->accel_path;
1975 gboolean group_changed;
1979 refresh_accel_paths_foreach (GtkWidget *widget,
1982 AccelPropagation *prop = data;
1984 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1985 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1986 prop->menu->accel_path,
1987 prop->menu->accel_group,
1988 prop->group_changed);
1992 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1993 gboolean group_changed)
1995 g_return_if_fail (GTK_IS_MENU (menu));
1997 if (menu->accel_path && menu->accel_group)
1999 AccelPropagation prop;
2002 prop.group_changed = group_changed;
2003 gtk_container_foreach (GTK_CONTAINER (menu),
2004 refresh_accel_paths_foreach,
2010 gtk_menu_reposition (GtkMenu *menu)
2012 g_return_if_fail (GTK_IS_MENU (menu));
2014 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2015 gtk_menu_position (menu, FALSE);
2019 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2022 g_return_if_fail (GTK_IS_MENU (menu));
2024 if (adjustment->value != menu->scroll_offset)
2025 gtk_menu_scroll_to (menu, adjustment->value);
2029 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2032 GdkGeometry geometry_hints;
2033 GtkMenuPrivate *priv;
2035 if (!menu->tearoff_window)
2038 priv = gtk_menu_get_private (menu);
2040 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2042 GtkRequisition requisition;
2044 gtk_widget_get_preferred_size (menu->tearoff_scrollbar,
2045 &requisition, NULL);
2046 width += requisition.width;
2049 geometry_hints.min_width = width;
2050 geometry_hints.max_width = width;
2052 geometry_hints.min_height = 0;
2053 geometry_hints.max_height = priv->requested_height;
2055 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2058 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2062 gtk_menu_update_title (GtkMenu *menu)
2064 if (menu->tearoff_window)
2067 GtkWidget *attach_widget;
2069 title = gtk_menu_get_title (menu);
2072 attach_widget = gtk_menu_get_attach_widget (menu);
2073 if (GTK_IS_MENU_ITEM (attach_widget))
2075 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2076 if (GTK_IS_LABEL (child))
2077 title = gtk_label_get_text (GTK_LABEL (child));
2082 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2087 gtk_menu_get_toplevel (GtkWidget *menu)
2089 GtkWidget *attach, *toplevel;
2091 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2093 if (GTK_IS_MENU_ITEM (attach))
2094 attach = gtk_widget_get_parent (attach);
2096 if (GTK_IS_MENU (attach))
2097 return gtk_menu_get_toplevel (attach);
2098 else if (GTK_IS_WIDGET (attach))
2100 toplevel = gtk_widget_get_toplevel (attach);
2101 if (gtk_widget_is_toplevel (toplevel))
2109 tearoff_window_destroyed (GtkWidget *widget,
2112 gtk_menu_set_tearoff_state (menu, FALSE);
2116 gtk_menu_set_tearoff_state (GtkMenu *menu,
2120 GtkMenuPrivate *priv;
2122 g_return_if_fail (GTK_IS_MENU (menu));
2124 priv = gtk_menu_get_private (menu);
2126 if (menu->torn_off != torn_off)
2128 menu->torn_off = torn_off;
2129 menu->tearoff_active = torn_off;
2133 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2134 gtk_menu_popdown (menu);
2136 if (!menu->tearoff_window)
2138 GtkWidget *toplevel;
2140 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2141 "type", GTK_WINDOW_TOPLEVEL,
2142 "screen", gtk_widget_get_screen (menu->toplevel),
2143 "app-paintable", TRUE,
2146 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2147 GDK_WINDOW_TYPE_HINT_MENU);
2148 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2149 g_signal_connect (menu->tearoff_window, "destroy",
2150 G_CALLBACK (tearoff_window_destroyed), menu);
2151 g_signal_connect (menu->tearoff_window, "event",
2152 G_CALLBACK (gtk_menu_window_event), menu);
2154 gtk_menu_update_title (menu);
2156 gtk_widget_realize (menu->tearoff_window);
2158 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2159 if (toplevel != NULL)
2160 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2161 GTK_WINDOW (toplevel));
2163 menu->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2164 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2166 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2167 menu->tearoff_adjustment = gtk_adjustment_new (0,
2168 0, priv->requested_height,
2172 g_object_connect (menu->tearoff_adjustment,
2173 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2175 menu->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, menu->tearoff_adjustment);
2177 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2178 menu->tearoff_scrollbar,
2181 if (menu->tearoff_adjustment->upper > height)
2182 gtk_widget_show (menu->tearoff_scrollbar);
2184 gtk_widget_show (menu->tearoff_hbox);
2187 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2189 /* Update menu->requisition
2191 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2194 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2196 gtk_widget_realize (menu->tearoff_window);
2197 gtk_menu_position (menu, TRUE);
2199 gtk_widget_show (GTK_WIDGET (menu));
2200 gtk_widget_show (menu->tearoff_window);
2202 gtk_menu_scroll_to (menu, 0);
2207 gtk_widget_hide (GTK_WIDGET (menu));
2208 gtk_widget_hide (menu->tearoff_window);
2209 if (GTK_IS_CONTAINER (menu->toplevel))
2210 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2211 gtk_widget_destroy (menu->tearoff_window);
2213 menu->tearoff_window = NULL;
2214 menu->tearoff_hbox = NULL;
2215 menu->tearoff_scrollbar = NULL;
2216 menu->tearoff_adjustment = NULL;
2219 g_object_notify (G_OBJECT (menu), "tearoff-state");
2224 * gtk_menu_get_tearoff_state:
2227 * Returns whether the menu is torn off. See
2228 * gtk_menu_set_tearoff_state ().
2230 * Return value: %TRUE if the menu is currently torn off.
2233 gtk_menu_get_tearoff_state (GtkMenu *menu)
2235 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2237 return menu->torn_off;
2241 * gtk_menu_set_title:
2243 * @title: a string containing the title for the menu.
2245 * Sets the title string for the menu. The title is displayed when the menu
2246 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2247 * attached to a parent menu item, and if so it will try to use the same text as
2248 * that menu item's label.
2251 gtk_menu_set_title (GtkMenu *menu,
2254 GtkMenuPrivate *priv;
2257 g_return_if_fail (GTK_IS_MENU (menu));
2259 priv = gtk_menu_get_private (menu);
2261 old_title = priv->title;
2262 priv->title = g_strdup (title);
2265 gtk_menu_update_title (menu);
2266 g_object_notify (G_OBJECT (menu), "tearoff-title");
2270 * gtk_menu_get_title:
2273 * Returns the title of the menu. See gtk_menu_set_title().
2275 * Return value: the title of the menu, or %NULL if the menu has no
2276 * title set on it. This string is owned by the widget and should
2277 * not be modified or freed.
2279 G_CONST_RETURN gchar *
2280 gtk_menu_get_title (GtkMenu *menu)
2282 GtkMenuPrivate *priv;
2284 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2286 priv = gtk_menu_get_private (menu);
2292 gtk_menu_reorder_child (GtkMenu *menu,
2296 GtkMenuShell *menu_shell;
2298 g_return_if_fail (GTK_IS_MENU (menu));
2299 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2301 menu_shell = GTK_MENU_SHELL (menu);
2303 if (g_list_find (menu_shell->children, child))
2305 menu_shell->children = g_list_remove (menu_shell->children, child);
2306 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2308 menu_queue_resize (menu);
2313 gtk_menu_style_set (GtkWidget *widget,
2314 GtkStyle *previous_style)
2316 if (gtk_widget_get_realized (widget))
2318 GtkMenu *menu = GTK_MENU (widget);
2321 style = gtk_widget_get_style (widget);
2323 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2324 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2325 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2330 get_arrows_border (GtkMenu *menu,
2333 guint scroll_arrow_height;
2334 GtkArrowPlacement arrow_placement;
2336 gtk_widget_style_get (GTK_WIDGET (menu),
2337 "scroll-arrow-vlength", &scroll_arrow_height,
2338 "arrow_placement", &arrow_placement,
2341 switch (arrow_placement)
2343 case GTK_ARROWS_BOTH:
2344 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2345 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2348 case GTK_ARROWS_START:
2349 border->top = (menu->upper_arrow_visible ||
2350 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2354 case GTK_ARROWS_END:
2356 border->bottom = (menu->upper_arrow_visible ||
2357 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2361 border->left = border->right = 0;
2365 gtk_menu_realize (GtkWidget *widget)
2367 GtkAllocation allocation;
2370 GdkWindowAttr attributes;
2371 gint attributes_mask;
2374 GtkMenuPrivate *priv;
2377 guint vertical_padding;
2378 guint horizontal_padding;
2379 GtkBorder arrow_border;
2381 g_return_if_fail (GTK_IS_MENU (widget));
2383 menu = GTK_MENU (widget);
2384 priv = gtk_menu_get_private (menu);
2386 gtk_widget_set_realized (widget, TRUE);
2388 gtk_widget_get_allocation (widget, &allocation);
2390 attributes.window_type = GDK_WINDOW_CHILD;
2391 attributes.x = allocation.x;
2392 attributes.y = allocation.y;
2393 attributes.width = allocation.width;
2394 attributes.height = allocation.height;
2395 attributes.wclass = GDK_INPUT_OUTPUT;
2396 attributes.visual = gtk_widget_get_visual (widget);
2397 attributes.event_mask = gtk_widget_get_events (widget);
2398 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2399 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2401 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2403 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2404 &attributes, attributes_mask);
2405 gtk_widget_set_window (widget, window);
2406 gdk_window_set_user_data (window, widget);
2408 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2410 style = gtk_widget_get_style (widget);
2411 gtk_widget_style_get (GTK_WIDGET (menu),
2412 "vertical-padding", &vertical_padding,
2413 "horizontal-padding", &horizontal_padding,
2416 gtk_widget_get_allocation (widget, &allocation);
2418 attributes.x = border_width + style->xthickness + horizontal_padding;
2419 attributes.y = border_width + style->ythickness + vertical_padding;
2420 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2421 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2423 get_arrows_border (menu, &arrow_border);
2424 attributes.y += arrow_border.top;
2425 attributes.height -= arrow_border.top;
2426 attributes.height -= arrow_border.bottom;
2428 menu->view_window = gdk_window_new (window,
2429 &attributes, attributes_mask);
2430 gdk_window_set_user_data (menu->view_window, menu);
2432 gtk_widget_get_allocation (widget, &allocation);
2436 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2437 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2439 menu->bin_window = gdk_window_new (menu->view_window,
2440 &attributes, attributes_mask);
2441 gdk_window_set_user_data (menu->bin_window, menu);
2443 children = GTK_MENU_SHELL (menu)->children;
2446 child = children->data;
2447 children = children->next;
2449 gtk_widget_set_parent_window (child, menu->bin_window);
2452 gtk_widget_style_attach (widget);
2453 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2454 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2455 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2457 if (GTK_MENU_SHELL (widget)->active_menu_item)
2458 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2459 GTK_MENU_SHELL (widget)->active_menu_item);
2461 gdk_window_show (menu->bin_window);
2462 gdk_window_show (menu->view_window);
2466 gtk_menu_focus (GtkWidget *widget,
2467 GtkDirectionType direction)
2470 * A menu or its menu items cannot have focus
2475 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2478 menu_grab_transfer_window_get (GtkMenu *menu)
2480 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2483 GdkWindowAttr attributes;
2484 gint attributes_mask;
2486 attributes.x = -100;
2487 attributes.y = -100;
2488 attributes.width = 10;
2489 attributes.height = 10;
2490 attributes.window_type = GDK_WINDOW_TEMP;
2491 attributes.wclass = GDK_INPUT_ONLY;
2492 attributes.override_redirect = TRUE;
2493 attributes.event_mask = 0;
2495 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2497 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2498 &attributes, attributes_mask);
2499 gdk_window_set_user_data (window, menu);
2501 gdk_window_show (window);
2503 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2510 menu_grab_transfer_window_destroy (GtkMenu *menu)
2512 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2515 gdk_window_set_user_data (window, NULL);
2516 gdk_window_destroy (window);
2517 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2522 gtk_menu_unrealize (GtkWidget *widget)
2524 GtkMenu *menu = GTK_MENU (widget);
2526 menu_grab_transfer_window_destroy (menu);
2528 gdk_window_set_user_data (menu->view_window, NULL);
2529 gdk_window_destroy (menu->view_window);
2530 menu->view_window = NULL;
2532 gdk_window_set_user_data (menu->bin_window, NULL);
2533 gdk_window_destroy (menu->bin_window);
2534 menu->bin_window = NULL;
2536 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2541 calculate_line_heights (GtkMenu *menu,
2543 guint **ret_min_heights,
2544 guint **ret_nat_heights)
2546 GtkMenuShell *menu_shell;
2547 GtkMenuPrivate *priv;
2548 GtkWidget *child, *widget;
2550 guint horizontal_padding;
2558 widget = GTK_WIDGET (menu);
2559 menu_shell = GTK_MENU_SHELL (widget);
2560 priv = gtk_menu_get_private (menu);
2562 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2563 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2564 n_heights = gtk_menu_get_n_rows (menu);
2565 n_columns = gtk_menu_get_n_columns (menu);
2566 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2568 gtk_widget_style_get (GTK_WIDGET (menu),
2569 "horizontal-padding", &horizontal_padding,
2572 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2573 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2575 for (children = menu_shell->children; children; children = children->next)
2580 gint child_min, child_nat;
2582 child = children->data;
2584 if (! gtk_widget_get_visible (child))
2587 get_effective_child_attach (child, &l, &r, &t, &b);
2589 part = avail_width / (r - l);
2591 gtk_widget_get_preferred_height_for_width (child, part,
2592 &child_min, &child_nat);
2594 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2596 part = MAX (child_min, toggle_size) / (b - t);
2597 min_heights[t] = MAX (min_heights[t], part);
2599 part = MAX (child_nat, toggle_size) / (b - t);
2600 nat_heights[t] = MAX (nat_heights[t], part);
2603 if (ret_min_heights)
2604 *ret_min_heights = min_heights;
2606 g_free (min_heights);
2608 if (ret_nat_heights)
2609 *ret_nat_heights = nat_heights;
2611 g_free (nat_heights);
2617 gtk_menu_size_allocate (GtkWidget *widget,
2618 GtkAllocation *allocation)
2621 GtkMenuShell *menu_shell;
2623 GtkAllocation child_allocation;
2624 GtkMenuPrivate *priv;
2630 guint vertical_padding;
2631 guint horizontal_padding;
2633 g_return_if_fail (GTK_IS_MENU (widget));
2634 g_return_if_fail (allocation != NULL);
2636 menu = GTK_MENU (widget);
2637 menu_shell = GTK_MENU_SHELL (widget);
2638 priv = gtk_menu_get_private (menu);
2640 gtk_widget_set_allocation (widget, allocation);
2642 style = gtk_widget_get_style (widget);
2644 gtk_widget_style_get (GTK_WIDGET (menu),
2645 "vertical-padding", &vertical_padding,
2646 "horizontal-padding", &horizontal_padding,
2648 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2650 g_free (priv->heights);
2651 priv->heights_length =
2652 calculate_line_heights (menu,
2657 /* refresh our cached height request */
2658 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2659 for (i = 0; i < priv->heights_length; i++)
2660 priv->requested_height += priv->heights[i];
2662 x = border_width + style->xthickness + horizontal_padding;
2663 y = border_width + style->ythickness + vertical_padding;
2665 width = MAX (1, allocation->width - x * 2);
2666 height = MAX (1, allocation->height - y * 2);
2668 if (menu_shell->active)
2669 gtk_menu_scroll_to (menu, menu->scroll_offset);
2671 if (!menu->tearoff_active)
2673 GtkBorder arrow_border;
2675 get_arrows_border (menu, &arrow_border);
2676 y += arrow_border.top;
2677 height -= arrow_border.top;
2678 height -= arrow_border.bottom;
2681 if (gtk_widget_get_realized (widget))
2683 gdk_window_move_resize (gtk_widget_get_window (widget),
2684 allocation->x, allocation->y,
2685 allocation->width, allocation->height);
2687 gdk_window_move_resize (menu->view_window,
2694 if (menu_shell->children)
2696 gint base_width = width / gtk_menu_get_n_columns (menu);
2698 children = menu_shell->children;
2701 child = children->data;
2702 children = children->next;
2704 if (gtk_widget_get_visible (child))
2709 get_effective_child_attach (child, &l, &r, &t, &b);
2711 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2714 tmp = gtk_menu_get_n_columns (menu) - l;
2715 l = gtk_menu_get_n_columns (menu) - r;
2719 child_allocation.width = (r - l) * base_width;
2720 child_allocation.height = 0;
2721 child_allocation.x = l * base_width;
2722 child_allocation.y = 0;
2724 for (i = 0; i < b; i++)
2727 child_allocation.y += priv->heights[i];
2729 child_allocation.height += priv->heights[i];
2732 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2735 gtk_widget_size_allocate (child, &child_allocation);
2736 gtk_widget_queue_draw (child);
2740 /* Resize the item window */
2741 if (gtk_widget_get_realized (widget))
2747 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2748 height += priv->heights[i];
2750 width = gtk_menu_get_n_columns (menu) * base_width;
2751 gdk_window_resize (menu->bin_window, width, height);
2754 if (menu->tearoff_active)
2756 if (height >= priv->requested_height)
2758 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2760 gtk_widget_hide (menu->tearoff_scrollbar);
2761 gtk_menu_set_tearoff_hints (menu, allocation->width);
2763 gtk_menu_scroll_to (menu, 0);
2768 menu->tearoff_adjustment->upper = priv->requested_height;
2769 menu->tearoff_adjustment->page_size = allocation->height;
2771 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2772 menu->tearoff_adjustment->upper)
2775 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2778 gtk_menu_scroll_to (menu, value);
2781 gtk_adjustment_changed (menu->tearoff_adjustment);
2783 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2785 gtk_widget_show (menu->tearoff_scrollbar);
2786 gtk_menu_set_tearoff_hints (menu, allocation->width);
2794 get_arrows_visible_area (GtkMenu *menu,
2795 GdkRectangle *border,
2796 GdkRectangle *upper,
2797 GdkRectangle *lower,
2800 GtkArrowPlacement arrow_placement;
2802 GtkWidget *widget = GTK_WIDGET (menu);
2804 guint vertical_padding;
2805 guint horizontal_padding;
2806 gint scroll_arrow_height;
2808 style = gtk_widget_get_style (widget);
2810 gtk_widget_style_get (widget,
2811 "vertical-padding", &vertical_padding,
2812 "horizontal-padding", &horizontal_padding,
2813 "scroll-arrow-vlength", &scroll_arrow_height,
2814 "arrow-placement", &arrow_placement,
2817 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2818 border->x = border_width + style->xthickness + horizontal_padding;
2819 border->y = border_width + style->ythickness + vertical_padding;
2820 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2821 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2823 switch (arrow_placement)
2825 case GTK_ARROWS_BOTH:
2826 upper->x = border->x;
2827 upper->y = border->y;
2828 upper->width = border->width - 2 * border->x;
2829 upper->height = scroll_arrow_height;
2831 lower->x = border->x;
2832 lower->y = border->height - border->y - scroll_arrow_height;
2833 lower->width = border->width - 2 * border->x;
2834 lower->height = scroll_arrow_height;
2837 case GTK_ARROWS_START:
2838 upper->x = border->x;
2839 upper->y = border->y;
2840 upper->width = (border->width - 2 * border->x) / 2;
2841 upper->height = scroll_arrow_height;
2843 lower->x = border->x + upper->width;
2844 lower->y = border->y;
2845 lower->width = (border->width - 2 * border->x) / 2;
2846 lower->height = scroll_arrow_height;
2849 case GTK_ARROWS_END:
2850 upper->x = border->x;
2851 upper->y = border->height - border->y - scroll_arrow_height;
2852 upper->width = (border->width - 2 * border->x) / 2;
2853 upper->height = scroll_arrow_height;
2855 lower->x = border->x + upper->width;
2856 lower->y = border->height - border->y - scroll_arrow_height;
2857 lower->width = (border->width - 2 * border->x) / 2;
2858 lower->height = scroll_arrow_height;
2862 g_assert_not_reached();
2863 upper->x = upper->y = upper->width = upper->height = 0;
2864 lower->x = lower->y = lower->width = lower->height = 0;
2867 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2871 gtk_menu_draw (GtkWidget *widget,
2875 GtkMenuPrivate *priv;
2877 GdkRectangle border;
2883 menu = GTK_MENU (widget);
2884 priv = gtk_menu_get_private (menu);
2886 style = gtk_widget_get_style (widget);
2887 window = gtk_widget_get_window (widget);
2889 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2891 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2893 gfloat arrow_scaling;
2896 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2897 arrow_size = arrow_scaling * arrow_space;
2899 gtk_paint_box (style,
2905 gtk_widget_get_allocated_width (widget),
2906 gtk_widget_get_allocated_height (widget));
2908 if (menu->upper_arrow_visible && !menu->tearoff_active)
2910 gtk_paint_box (style,
2912 priv->upper_arrow_state,
2914 widget, "menu_scroll_arrow_up",
2920 gtk_paint_arrow (style,
2922 priv->upper_arrow_state,
2924 widget, "menu_scroll_arrow_up",
2927 upper.x + (upper.width - arrow_size) / 2,
2928 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2929 arrow_size, arrow_size);
2932 if (menu->lower_arrow_visible && !menu->tearoff_active)
2934 gtk_paint_box (style,
2936 priv->lower_arrow_state,
2938 widget, "menu_scroll_arrow_down",
2944 gtk_paint_arrow (style,
2946 priv->lower_arrow_state,
2948 widget, "menu_scroll_arrow_down",
2951 lower.x + (lower.width - arrow_size) / 2,
2952 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2953 arrow_size, arrow_size);
2957 if (gtk_cairo_should_draw_window (cr, menu->bin_window))
2959 gint y = -border.y + menu->scroll_offset;
2962 gtk_cairo_transform_to_window (cr, widget, menu->bin_window);
2964 if (!menu->tearoff_active)
2966 GtkBorder arrow_border;
2968 get_arrows_border (menu, &arrow_border);
2969 y -= arrow_border.top;
2972 gtk_paint_box (style,
2978 border.width, border.height);
2983 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
2989 gtk_menu_show (GtkWidget *widget)
2991 GtkMenu *menu = GTK_MENU (widget);
2993 _gtk_menu_refresh_accel_paths (menu, FALSE);
2995 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3000 gtk_menu_get_preferred_width (GtkWidget *widget,
3005 GtkMenuShell *menu_shell;
3006 GtkMenuPrivate *priv;
3010 guint max_toggle_size;
3011 guint max_accel_width;
3012 guint horizontal_padding;
3014 gint child_min, child_nat;
3015 gint min_width, nat_width;
3017 menu = GTK_MENU (widget);
3018 menu_shell = GTK_MENU_SHELL (widget);
3019 priv = gtk_menu_get_private (menu);
3021 style = gtk_widget_get_style (GTK_WIDGET (widget));
3023 min_width = nat_width = 0;
3025 max_toggle_size = 0;
3026 max_accel_width = 0;
3028 children = menu_shell->children;
3035 child = children->data;
3036 children = children->next;
3038 if (! gtk_widget_get_visible (child))
3041 get_effective_child_attach (child, &l, &r, &t, &b);
3043 /* It's important to size_request the child
3044 * before doing the toggle size request, in
3045 * case the toggle size request depends on the size
3046 * request of a child of the child (e.g. for ImageMenuItem)
3049 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3050 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3052 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3053 max_toggle_size = MAX (max_toggle_size, toggle_size);
3054 max_accel_width = MAX (max_accel_width,
3055 GTK_MENU_ITEM (child)->accelerator_width);
3057 part = child_min / (r - l);
3058 min_width = MAX (min_width, part);
3060 part = child_nat / (r - l);
3061 nat_width = MAX (nat_width, part);
3064 /* If the menu doesn't include any images or check items
3065 * reserve the space so that all menus are consistent.
3066 * We only do this for 'ordinary' menus, not for combobox
3067 * menus or multi-column menus
3069 if (max_toggle_size == 0 &&
3070 gtk_menu_get_n_columns (menu) == 1 &&
3071 !priv->no_toggle_size)
3073 guint toggle_spacing;
3074 guint indicator_size;
3076 gtk_style_get (style,
3077 GTK_TYPE_CHECK_MENU_ITEM,
3078 "toggle-spacing", &toggle_spacing,
3079 "indicator-size", &indicator_size,
3082 max_toggle_size = indicator_size + toggle_spacing;
3085 min_width += 2 * max_toggle_size + max_accel_width;
3086 min_width *= gtk_menu_get_n_columns (menu);
3088 nat_width += 2 * max_toggle_size + max_accel_width;
3089 nat_width *= gtk_menu_get_n_columns (menu);
3091 gtk_widget_style_get (GTK_WIDGET (menu),
3092 "horizontal-padding", &horizontal_padding,
3095 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3096 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3097 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3099 menu->toggle_size = max_toggle_size;
3100 priv->accel_size = max_accel_width;
3103 *minimum_size = min_width;
3106 *natural_size = nat_width;
3108 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3110 if (menu->tearoff_active)
3111 gtk_menu_set_tearoff_hints (menu, min_width);
3115 gtk_menu_get_preferred_height (GtkWidget *widget,
3121 /* Menus are height-for-width only, just return the height for the minimum width */
3122 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3123 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3127 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3132 GtkMenu *menu = GTK_MENU (widget);
3133 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3134 guint *min_heights, *nat_heights;
3135 guint vertical_padding, border_width;
3137 gint min_height, nat_height;
3139 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3140 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3142 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3145 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3147 for (i = 0; i < n_heights; i++)
3149 min_height += min_heights[i];
3150 nat_height += nat_heights[i];
3153 if (private->have_position)
3155 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3156 GdkRectangle monitor;
3158 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3160 if (private->y + min_height > monitor.y + monitor.height)
3161 min_height = monitor.y + monitor.height - private->y;
3163 if (private->y + nat_height > monitor.y + monitor.height)
3164 nat_height = monitor.y + monitor.height - private->y;
3166 if (private->y < monitor.y)
3168 min_height -= monitor.y - private->y;
3169 nat_height -= monitor.y - private->y;
3174 *minimum_size = min_height;
3177 *natural_size = nat_height;
3179 g_free (min_heights);
3180 g_free (nat_heights);
3186 gtk_menu_button_scroll (GtkMenu *menu,
3187 GdkEventButton *event)
3189 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3191 gboolean touchscreen_mode;
3193 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3194 "gtk-touchscreen-mode", &touchscreen_mode,
3197 if (touchscreen_mode)
3198 gtk_menu_handle_scrolling (menu,
3199 event->x_root, event->y_root,
3200 event->type == GDK_BUTTON_PRESS,
3210 pointer_in_menu_window (GtkWidget *widget,
3214 GtkAllocation allocation;
3215 GtkMenu *menu = GTK_MENU (widget);
3217 if (gtk_widget_get_mapped (menu->toplevel))
3219 GtkMenuShell *menu_shell;
3220 gint window_x, window_y;
3222 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3223 &window_x, &window_y);
3225 gtk_widget_get_allocation (widget, &allocation);
3226 if (x_root >= window_x && x_root < window_x + allocation.width &&
3227 y_root >= window_y && y_root < window_y + allocation.height)
3230 menu_shell = GTK_MENU_SHELL (widget);
3232 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3233 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3241 gtk_menu_button_press (GtkWidget *widget,
3242 GdkEventButton *event)
3244 if (event->type != GDK_BUTTON_PRESS)
3247 /* Don't pass down to menu shell for presses over scroll arrows
3249 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3252 /* Don't pass down to menu shell if a non-menuitem part of the menu
3253 * was clicked. The check for the event_widget being a GtkMenuShell
3254 * works because we have the pointer grabbed on menu_shell->window
3255 * with owner_events=TRUE, so all events that are either outside
3256 * the menu or on its border are delivered relative to
3257 * menu_shell->window.
3259 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3260 pointer_in_menu_window (widget, event->x_root, event->y_root))
3263 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3267 gtk_menu_button_release (GtkWidget *widget,
3268 GdkEventButton *event)
3270 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3272 if (priv->ignore_button_release)
3274 priv->ignore_button_release = FALSE;
3278 if (event->type != GDK_BUTTON_RELEASE)
3281 /* Don't pass down to menu shell for releases over scroll arrows
3283 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3286 /* Don't pass down to menu shell if a non-menuitem part of the menu
3287 * was clicked (see comment in button_press()).
3289 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3290 pointer_in_menu_window (widget, event->x_root, event->y_root))
3292 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3293 * bail out early here so it is in a consistent state for the
3294 * next button_press/button_release in GtkMenuShell.
3297 if (GTK_MENU_SHELL (widget)->active)
3298 GTK_MENU_SHELL (widget)->button = 0;
3303 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3306 static const gchar *
3307 get_accel_path (GtkWidget *menu_item,
3312 GClosure *accel_closure;
3313 GtkAccelGroup *accel_group;
3315 path = _gtk_widget_get_accel_path (menu_item, locked);
3318 path = GTK_MENU_ITEM (menu_item)->accel_path;
3324 label = gtk_bin_get_child (GTK_BIN (menu_item));
3326 if (GTK_IS_ACCEL_LABEL (label))
3328 g_object_get (label,
3329 "accel-closure", &accel_closure,
3333 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3335 *locked = gtk_accel_group_get_is_locked (accel_group);
3345 gtk_menu_key_press (GtkWidget *widget,
3348 GtkMenuShell *menu_shell;
3350 gboolean delete = FALSE;
3351 gboolean can_change_accels;
3352 gchar *accel = NULL;
3353 guint accel_key, accel_mods;
3354 GdkModifierType consumed_modifiers;
3355 GdkDisplay *display;
3357 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3358 g_return_val_if_fail (event != NULL, FALSE);
3360 menu_shell = GTK_MENU_SHELL (widget);
3361 menu = GTK_MENU (widget);
3363 gtk_menu_stop_navigating_submenu (menu);
3365 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3368 display = gtk_widget_get_display (widget);
3370 g_object_get (gtk_widget_get_settings (widget),
3371 "gtk-menu-bar-accel", &accel,
3372 "gtk-can-change-accels", &can_change_accels,
3375 if (accel && *accel)
3378 GdkModifierType mods = 0;
3380 gtk_accelerator_parse (accel, &keyval, &mods);
3383 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3385 /* FIXME this is wrong, needs to be in the global accel resolution
3386 * thing, to properly consider i18n etc., but that probably requires
3387 * AccelGroup changes etc.
3389 if (event->keyval == keyval && (mods & event->state) == mods)
3391 gtk_menu_shell_cancel (menu_shell);
3399 switch (event->keyval)
3401 case GDK_KEY_Delete:
3402 case GDK_KEY_KP_Delete:
3403 case GDK_KEY_BackSpace:
3410 /* Figure out what modifiers went into determining the key symbol */
3411 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3412 event->hardware_keycode, event->state, event->group,
3413 NULL, NULL, NULL, &consumed_modifiers);
3415 accel_key = gdk_keyval_to_lower (event->keyval);
3416 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3418 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3419 * We re-upper case when we match against the keyval, but display and save in caseless form.
3421 if (accel_key != event->keyval)
3422 accel_mods |= GDK_SHIFT_MASK;
3424 /* Modify the accelerators */
3425 if (can_change_accels &&
3426 menu_shell->active_menu_item &&
3427 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3428 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3429 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3431 GtkWidget *menu_item = menu_shell->active_menu_item;
3432 gboolean locked, replace_accels = TRUE;
3435 path = get_accel_path (menu_item, &locked);
3436 if (!path || locked)
3438 /* can't change accelerators on menu_items without paths
3439 * (basically, those items are accelerator-locked).
3441 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3442 gtk_widget_error_bell (widget);
3448 /* For the keys that act to delete the current setting, we delete
3449 * the current setting if there is one, otherwise, we set the
3450 * key as the accelerator.
3456 if (gtk_accel_map_lookup_entry (path, &key) &&
3457 (key.accel_key || key.accel_mods))
3463 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3467 /* we failed, probably because this key is in use and
3470 /* g_print("failed to change\n"); */
3471 gtk_widget_error_bell (widget);
3480 check_threshold (GtkWidget *widget,
3489 ABS (start_x - x) > THRESHOLD ||
3490 ABS (start_y - y) > THRESHOLD;
3494 definitely_within_item (GtkWidget *widget,
3498 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3501 w = gdk_window_get_width (window);
3502 h = gdk_window_get_height (window);
3505 check_threshold (widget, 0, 0, x, y) &&
3506 check_threshold (widget, w - 1, 0, x, y) &&
3507 check_threshold (widget, w - 1, h - 1, x, y) &&
3508 check_threshold (widget, 0, h - 1, x, y);
3512 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3514 GtkMenuPrivate *priv;
3516 priv = gtk_menu_get_private (menu);
3518 return priv->navigation_height && priv->navigation_width;
3522 gtk_menu_motion_notify (GtkWidget *widget,
3523 GdkEventMotion *event)
3525 GtkWidget *menu_item;
3527 GtkMenuShell *menu_shell;
3530 gboolean need_enter;
3532 if (GTK_IS_MENU (widget))
3534 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3536 if (priv->ignore_button_release)
3537 priv->ignore_button_release = FALSE;
3539 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3543 /* We received the event for one of two reasons:
3545 * a) We are the active menu, and did gtk_grab_add()
3546 * b) The widget is a child of ours, and the event was propagated
3548 * Since for computation of navigation regions, we want the menu which
3549 * is the parent of the menu item, for a), we need to find that menu,
3550 * which may be different from 'widget'.
3552 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3553 parent = gtk_widget_get_parent (menu_item);
3554 if (!GTK_IS_MENU_ITEM (menu_item) ||
3555 !GTK_IS_MENU (parent))
3558 menu_shell = GTK_MENU_SHELL (parent);
3559 menu = GTK_MENU (menu_shell);
3561 if (definitely_within_item (menu_item, event->x, event->y))
3562 menu_shell->activate_time = 0;
3564 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3566 /* Check to see if we are within an active submenu's navigation region
3568 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3571 /* Make sure we pop down if we enter a non-selectable menu item, so we
3572 * don't show a submenu when the cursor is outside the stay-up triangle.
3574 if (!_gtk_menu_item_is_selectable (menu_item))
3576 /* We really want to deselect, but this gives the menushell code
3577 * a chance to do some bookkeeping about the menuitem.
3579 gtk_menu_shell_select_item (menu_shell, menu_item);
3585 /* The menu is now sensitive to enter events on its items, but
3586 * was previously sensitive. So we fake an enter event.
3588 menu_shell->ignore_enter = FALSE;
3590 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3591 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3593 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3596 send_event->crossing.window = g_object_ref (event->window);
3597 send_event->crossing.time = event->time;
3598 send_event->crossing.send_event = TRUE;
3599 send_event->crossing.x_root = event->x_root;
3600 send_event->crossing.y_root = event->y_root;
3601 send_event->crossing.x = event->x;
3602 send_event->crossing.y = event->y;
3603 send_event->crossing.state = event->state;
3604 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3606 /* We send the event to 'widget', the currently active menu,
3607 * instead of 'menu', the menu that the pointer is in. This
3608 * will ensure that the event will be ignored unless the
3609 * menuitem is a child of the active menu or some parent
3610 * menu of the active menu.
3612 result = gtk_widget_event (widget, send_event);
3613 gdk_event_free (send_event);
3623 get_double_arrows (GtkMenu *menu)
3625 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3626 gboolean double_arrows;
3627 GtkArrowPlacement arrow_placement;
3629 gtk_widget_style_get (GTK_WIDGET (menu),
3630 "double-arrows", &double_arrows,
3631 "arrow-placement", &arrow_placement,
3634 if (arrow_placement != GTK_ARROWS_BOTH)
3637 return double_arrows || (priv->initially_pushed_in &&
3638 menu->scroll_offset != 0);
3642 gtk_menu_scroll_by (GtkMenu *menu,
3645 GtkMenuPrivate *priv;
3646 GtkBorder arrow_border;
3650 gboolean double_arrows;
3652 widget = GTK_WIDGET (menu);
3653 offset = menu->scroll_offset + step;
3654 priv = gtk_menu_get_private (menu);
3656 get_arrows_border (menu, &arrow_border);
3658 double_arrows = get_double_arrows (menu);
3660 /* If we scroll upward and the non-visible top part
3661 * is smaller than the scroll arrow it would be
3662 * pretty stupid to show the arrow and taking more
3663 * screen space than just scrolling to the top.
3666 if ((step < 0) && (offset < arrow_border.top))
3669 /* Don't scroll over the top if we weren't before: */
3670 if ((menu->scroll_offset >= 0) && (offset < 0))
3673 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3675 if (menu->scroll_offset == 0 &&
3676 view_height >= priv->requested_height)
3679 /* Don't scroll past the bottom if we weren't before: */
3680 if (menu->scroll_offset > 0)
3681 view_height -= arrow_border.top;
3683 /* When both arrows are always shown, reduce
3684 * view height even more.
3687 view_height -= arrow_border.bottom;
3689 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3690 (offset + view_height > priv->requested_height))
3691 offset = priv->requested_height - view_height;
3693 if (offset != menu->scroll_offset)
3694 gtk_menu_scroll_to (menu, offset);
3698 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3699 gboolean touchscreen_mode)
3701 gboolean upper_visible;
3702 gboolean lower_visible;
3704 upper_visible = menu->upper_arrow_visible;
3705 lower_visible = menu->lower_arrow_visible;
3707 gtk_menu_scroll_by (menu, menu->scroll_step);
3709 if (touchscreen_mode &&
3710 (upper_visible != menu->upper_arrow_visible ||
3711 lower_visible != menu->lower_arrow_visible))
3713 /* We are about to hide a scroll arrow while the mouse is pressed,
3714 * this would cause the uncovered menu item to be activated on button
3715 * release. Therefore we need to ignore button release here
3717 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3718 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3723 gtk_menu_scroll_timeout (gpointer data)
3726 gboolean touchscreen_mode;
3728 menu = GTK_MENU (data);
3730 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3731 "gtk-touchscreen-mode", &touchscreen_mode,
3734 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3740 gtk_menu_scroll_timeout_initial (gpointer data)
3744 gboolean touchscreen_mode;
3746 menu = GTK_MENU (data);
3748 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3749 "gtk-timeout-repeat", &timeout,
3750 "gtk-touchscreen-mode", &touchscreen_mode,
3753 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3755 gtk_menu_remove_scroll_timeout (menu);
3757 menu->timeout_id = gdk_threads_add_timeout (timeout,
3758 gtk_menu_scroll_timeout,
3765 gtk_menu_start_scrolling (GtkMenu *menu)
3768 gboolean touchscreen_mode;
3770 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3771 "gtk-timeout-repeat", &timeout,
3772 "gtk-touchscreen-mode", &touchscreen_mode,
3775 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3777 menu->timeout_id = gdk_threads_add_timeout (timeout,
3778 gtk_menu_scroll_timeout_initial,
3783 gtk_menu_scroll (GtkWidget *widget,
3784 GdkEventScroll *event)
3786 GtkMenu *menu = GTK_MENU (widget);
3788 switch (event->direction)
3790 case GDK_SCROLL_RIGHT:
3791 case GDK_SCROLL_DOWN:
3792 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3794 case GDK_SCROLL_LEFT:
3796 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3804 get_arrows_sensitive_area (GtkMenu *menu,
3805 GdkRectangle *upper,
3806 GdkRectangle *lower)
3808 GtkArrowPlacement arrow_placement;
3809 GtkWidget *widget = GTK_WIDGET (menu);
3813 guint vertical_padding;
3815 gint scroll_arrow_height;
3817 window = gtk_widget_get_window (widget);
3818 width = gdk_window_get_width (window);
3819 height = gdk_window_get_height (window);
3821 gtk_widget_style_get (widget,
3822 "vertical-padding", &vertical_padding,
3823 "scroll-arrow-vlength", &scroll_arrow_height,
3824 "arrow-placement", &arrow_placement,
3827 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3828 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3830 gdk_window_get_position (window, &win_x, &win_y);
3832 switch (arrow_placement)
3834 case GTK_ARROWS_BOTH:
3839 upper->width = width;
3840 upper->height = scroll_arrow_height + border;
3846 lower->y = win_y + height - border - scroll_arrow_height;
3847 lower->width = width;
3848 lower->height = scroll_arrow_height + border;
3852 case GTK_ARROWS_START:
3857 upper->width = width / 2;
3858 upper->height = scroll_arrow_height + border;
3863 lower->x = win_x + width / 2;
3865 lower->width = width / 2;
3866 lower->height = scroll_arrow_height + border;
3870 case GTK_ARROWS_END:
3874 upper->y = win_y + height - border - scroll_arrow_height;
3875 upper->width = width / 2;
3876 upper->height = scroll_arrow_height + border;
3881 lower->x = win_x + width / 2;
3882 lower->y = win_y + height - border - scroll_arrow_height;
3883 lower->width = width / 2;
3884 lower->height = scroll_arrow_height + border;
3892 gtk_menu_handle_scrolling (GtkMenu *menu,
3898 GtkMenuShell *menu_shell;
3899 GtkMenuPrivate *priv;
3902 gboolean scroll_fast = FALSE;
3904 gboolean touchscreen_mode;
3906 priv = gtk_menu_get_private (menu);
3908 menu_shell = GTK_MENU_SHELL (menu);
3910 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3911 "gtk-touchscreen-mode", &touchscreen_mode,
3914 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3919 /* upper arrow handling */
3921 get_arrows_sensitive_area (menu, &rect, NULL);
3924 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3925 (x >= rect.x) && (x < rect.x + rect.width) &&
3926 (y >= rect.y) && (y < rect.y + rect.height))
3931 if (touchscreen_mode)
3932 menu->upper_arrow_prelight = in_arrow;
3934 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3936 gboolean arrow_pressed = FALSE;
3938 if (menu->upper_arrow_visible && !menu->tearoff_active)
3940 if (touchscreen_mode)
3942 if (enter && menu->upper_arrow_prelight)
3944 if (menu->timeout_id == 0)
3946 /* Deselect the active item so that
3947 * any submenus are popped down
3949 gtk_menu_shell_deselect (menu_shell);
3951 gtk_menu_remove_scroll_timeout (menu);
3952 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3956 /* Only do stuff on click. */
3957 gtk_menu_start_scrolling (menu);
3958 arrow_pressed = TRUE;
3963 arrow_pressed = TRUE;
3968 gtk_menu_stop_scrolling (menu);
3971 else /* !touchscreen_mode */
3973 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3975 if (enter && in_arrow &&
3976 (!menu->upper_arrow_prelight ||
3977 menu->scroll_fast != scroll_fast))
3979 menu->upper_arrow_prelight = TRUE;
3980 menu->scroll_fast = scroll_fast;
3982 /* Deselect the active item so that
3983 * any submenus are popped down
3985 gtk_menu_shell_deselect (menu_shell);
3987 gtk_menu_remove_scroll_timeout (menu);
3988 menu->scroll_step = scroll_fast ?
3989 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3992 gdk_threads_add_timeout (scroll_fast ?
3993 MENU_SCROLL_TIMEOUT2 :
3994 MENU_SCROLL_TIMEOUT1,
3995 gtk_menu_scroll_timeout, menu);
3997 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3999 gtk_menu_stop_scrolling (menu);
4004 /* gtk_menu_start_scrolling() might have hit the top of the
4005 * menu, so check if the button isn't insensitive before
4006 * changing it to something else.
4008 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4010 GtkStateType arrow_state = GTK_STATE_NORMAL;
4013 arrow_state = GTK_STATE_ACTIVE;
4014 else if (menu->upper_arrow_prelight)
4015 arrow_state = GTK_STATE_PRELIGHT;
4017 if (arrow_state != priv->upper_arrow_state)
4019 priv->upper_arrow_state = arrow_state;
4021 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4027 /* lower arrow handling */
4029 get_arrows_sensitive_area (menu, NULL, &rect);
4032 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4033 (x >= rect.x) && (x < rect.x + rect.width) &&
4034 (y >= rect.y) && (y < rect.y + rect.height))
4039 if (touchscreen_mode)
4040 menu->lower_arrow_prelight = in_arrow;
4042 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4044 gboolean arrow_pressed = FALSE;
4046 if (menu->lower_arrow_visible && !menu->tearoff_active)
4048 if (touchscreen_mode)
4050 if (enter && menu->lower_arrow_prelight)
4052 if (menu->timeout_id == 0)
4054 /* Deselect the active item so that
4055 * any submenus are popped down
4057 gtk_menu_shell_deselect (menu_shell);
4059 gtk_menu_remove_scroll_timeout (menu);
4060 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4064 /* Only do stuff on click. */
4065 gtk_menu_start_scrolling (menu);
4066 arrow_pressed = TRUE;
4071 arrow_pressed = TRUE;
4076 gtk_menu_stop_scrolling (menu);
4079 else /* !touchscreen_mode */
4081 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4083 if (enter && in_arrow &&
4084 (!menu->lower_arrow_prelight ||
4085 menu->scroll_fast != scroll_fast))
4087 menu->lower_arrow_prelight = TRUE;
4088 menu->scroll_fast = scroll_fast;
4090 /* Deselect the active item so that
4091 * any submenus are popped down
4093 gtk_menu_shell_deselect (menu_shell);
4095 gtk_menu_remove_scroll_timeout (menu);
4096 menu->scroll_step = scroll_fast ?
4097 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4100 gdk_threads_add_timeout (scroll_fast ?
4101 MENU_SCROLL_TIMEOUT2 :
4102 MENU_SCROLL_TIMEOUT1,
4103 gtk_menu_scroll_timeout, menu);
4105 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4107 gtk_menu_stop_scrolling (menu);
4112 /* gtk_menu_start_scrolling() might have hit the bottom of the
4113 * menu, so check if the button isn't insensitive before
4114 * changing it to something else.
4116 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4118 GtkStateType arrow_state = GTK_STATE_NORMAL;
4121 arrow_state = GTK_STATE_ACTIVE;
4122 else if (menu->lower_arrow_prelight)
4123 arrow_state = GTK_STATE_PRELIGHT;
4125 if (arrow_state != priv->lower_arrow_state)
4127 priv->lower_arrow_state = arrow_state;
4129 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4137 gtk_menu_enter_notify (GtkWidget *widget,
4138 GdkEventCrossing *event)
4140 GtkWidget *menu_item;
4142 gboolean touchscreen_mode;
4144 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4145 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4146 event->mode == GDK_CROSSING_STATE_CHANGED)
4149 g_object_get (gtk_widget_get_settings (widget),
4150 "gtk-touchscreen-mode", &touchscreen_mode,
4153 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4154 if (GTK_IS_MENU (widget))
4156 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4158 if (!menu_shell->ignore_enter)
4159 gtk_menu_handle_scrolling (GTK_MENU (widget),
4160 event->x_root, event->y_root, TRUE, TRUE);
4163 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4165 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4167 if (GTK_IS_MENU (menu))
4169 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4170 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4172 if (priv->seen_item_enter)
4174 /* This is the second enter we see for an item
4175 * on this menu. This means a release should always
4178 menu_shell->activate_time = 0;
4180 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4181 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4183 if (definitely_within_item (menu_item, event->x, event->y))
4185 /* This is an actual user-enter (ie. not a pop-under)
4186 * In this case, the user must either have entered
4187 * sufficiently far enough into the item, or he must move
4188 * far enough away from the enter point. (see
4189 * gtk_menu_motion_notify())
4191 menu_shell->activate_time = 0;
4195 priv->seen_item_enter = TRUE;
4199 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4200 * will not correspond to the event widget's parent. Check to see
4201 * if we are in the parent's navigation region.
4203 parent = gtk_widget_get_parent (menu_item);
4204 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4205 gtk_menu_navigating_submenu (GTK_MENU (parent),
4206 event->x_root, event->y_root))
4209 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4213 gtk_menu_leave_notify (GtkWidget *widget,
4214 GdkEventCrossing *event)
4216 GtkMenuShell *menu_shell;
4218 GtkMenuItem *menu_item;
4219 GtkWidget *event_widget;
4221 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4222 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4223 event->mode == GDK_CROSSING_STATE_CHANGED)
4226 menu = GTK_MENU (widget);
4227 menu_shell = GTK_MENU_SHELL (widget);
4229 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4232 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4234 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4236 if (!GTK_IS_MENU_ITEM (event_widget))
4239 menu_item = GTK_MENU_ITEM (event_widget);
4241 /* Here we check to see if we're leaving an active menu item with a submenu,
4242 * in which case we enter submenu navigation mode.
4244 if (menu_shell->active_menu_item != NULL
4245 && menu_item->submenu != NULL
4246 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4248 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4250 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4253 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4255 /* We are leaving an active menu item with nonactive submenu.
4256 * Deselect it so we don't surprise the user with by popping
4257 * up a submenu _after_ he left the item.
4259 gtk_menu_shell_deselect (menu_shell);
4264 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4268 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4270 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4272 priv->navigation_x = 0;
4273 priv->navigation_y = 0;
4274 priv->navigation_width = 0;
4275 priv->navigation_height = 0;
4277 if (menu->navigation_timeout)
4279 g_source_remove (menu->navigation_timeout);
4280 menu->navigation_timeout = 0;
4284 /* When the timeout is elapsed, the navigation region is destroyed
4285 * and the menuitem under the pointer (if any) is selected.
4288 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4290 GtkMenuPopdownData *popdown_data = user_data;
4291 GtkMenu *menu = popdown_data->menu;
4292 GdkWindow *child_window;
4294 gtk_menu_stop_navigating_submenu (menu);
4296 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4298 child_window = gdk_window_get_device_position (menu->bin_window,
4299 popdown_data->device,
4304 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4306 send_event->crossing.window = g_object_ref (child_window);
4307 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4308 send_event->crossing.send_event = TRUE;
4309 gdk_event_set_device (send_event, popdown_data->device);
4311 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4313 gdk_event_free (send_event);
4321 gtk_menu_navigating_submenu (GtkMenu *menu,
4325 GtkMenuPrivate *priv;
4328 if (!gtk_menu_has_navigation_triangle (menu))
4331 priv = gtk_menu_get_private (menu);
4332 width = priv->navigation_width;
4333 height = priv->navigation_height;
4335 /* check if x/y are in the triangle spanned by the navigation parameters */
4337 /* 1) Move the coordinates so the triangle starts at 0,0 */
4338 event_x -= priv->navigation_x;
4339 event_y -= priv->navigation_y;
4341 /* 2) Ensure both legs move along the positive axis */
4353 /* 3) Check that the given coordinate is inside the triangle. The formula
4354 * is a transformed form of this formula: x/w + y/h <= 1
4356 if (event_x >= 0 && event_y >= 0 &&
4357 event_x * height + event_y * width <= width * height)
4363 gtk_menu_stop_navigating_submenu (menu);
4369 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4370 GtkMenuItem *menu_item,
4371 GdkEventCrossing *event)
4373 gint submenu_left = 0;
4374 gint submenu_right = 0;
4375 gint submenu_top = 0;
4376 gint submenu_bottom = 0;
4379 GtkWidget *event_widget;
4380 GtkMenuPopdownData *popdown_data;
4381 GtkMenuPrivate *priv;
4384 g_return_if_fail (menu_item->submenu != NULL);
4385 g_return_if_fail (event != NULL);
4387 priv = gtk_menu_get_private (menu);
4389 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4391 window = gtk_widget_get_window (menu_item->submenu);
4392 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4394 submenu_right = submenu_left + gdk_window_get_width (window);
4395 submenu_bottom = submenu_top + gdk_window_get_height (window);
4397 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4398 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4400 if (event->x >= 0 && event->x < width)
4404 gtk_menu_stop_navigating_submenu (menu);
4406 /* The navigation region is the triangle closest to the x/y
4407 * location of the rectangle. This is why the width or height
4411 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4414 priv->navigation_x = submenu_left;
4415 priv->navigation_width = event->x_root - submenu_left;
4420 priv->navigation_x = submenu_right;
4421 priv->navigation_width = event->x_root - submenu_right;
4427 priv->navigation_y = event->y_root;
4428 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4430 if (priv->navigation_height >= 0)
4436 priv->navigation_y = event->y_root;
4437 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4439 if (priv->navigation_height <= 0)
4443 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4444 "gtk-menu-popdown-delay", &popdown_delay,
4447 popdown_data = g_new (GtkMenuPopdownData, 1);
4448 popdown_data->menu = menu;
4449 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4451 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4453 gtk_menu_stop_navigating_submenu_cb,
4455 (GDestroyNotify) g_free);
4460 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4464 g_return_if_fail (GTK_IS_MENU (menu_shell));
4466 parent = menu_shell->parent_menu_shell;
4468 menu_shell->activate_time = 0;
4469 gtk_menu_popdown (GTK_MENU (menu_shell));
4472 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4476 gtk_menu_position (GtkMenu *menu,
4477 gboolean set_scroll_offset)
4480 GtkRequisition requisition;
4481 GtkMenuPrivate *private;
4486 GdkScreen *pointer_screen;
4487 GdkRectangle monitor;
4490 g_return_if_fail (GTK_IS_MENU (menu));
4492 widget = GTK_WIDGET (menu);
4494 screen = gtk_widget_get_screen (widget);
4495 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4496 gdk_display_get_device_state (gdk_screen_get_display (screen),
4497 pointer, &pointer_screen, &x, &y, NULL);
4499 /* Get the minimum height for minimum width to figure out
4500 * the right place to popup the menu.
4502 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4504 if (pointer_screen != screen)
4506 /* Pointer is on a different screen; roughly center the
4507 * menu on the screen. If someone was using multiscreen
4508 * + Xinerama together they'd probably want something
4509 * fancier; but that is likely to be vanishingly rare.
4511 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4512 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4515 private = gtk_menu_get_private (menu);
4516 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4518 private->initially_pushed_in = FALSE;
4520 /* Set the type hint here to allow custom position functions to set a different hint */
4521 if (!gtk_widget_get_visible (menu->toplevel))
4522 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4524 if (menu->position_func)
4526 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4527 menu->position_func_data);
4529 if (private->monitor_num < 0)
4530 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4532 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4536 GtkStyle *style = gtk_widget_get_style (widget);
4537 gint space_left, space_right, space_above, space_below;
4540 gint xthickness = style->xthickness;
4541 gint ythickness = style->ythickness;
4542 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4544 /* The placement of popup menus horizontally works like this (with
4545 * RTL in parentheses)
4547 * - If there is enough room to the right (left) of the mouse cursor,
4548 * position the menu there.
4550 * - Otherwise, if if there is enough room to the left (right) of the
4551 * mouse cursor, position the menu there.
4553 * - Otherwise if the menu is smaller than the monitor, position it
4554 * on the side of the mouse cursor that has the most space available
4556 * - Otherwise (if there is simply not enough room for the menu on the
4557 * monitor), position it as far left (right) as possible.
4559 * Positioning in the vertical direction is similar: first try below
4560 * mouse cursor, then above.
4562 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4564 space_left = x - monitor.x;
4565 space_right = monitor.x + monitor.width - x - 1;
4566 space_above = y - monitor.y;
4567 space_below = monitor.y + monitor.height - y - 1;
4569 /* position horizontally */
4571 /* the amount of space we need to position the menu. Note the
4572 * menu is offset "xthickness" pixels
4574 needed_width = requisition.width - xthickness;
4576 if (needed_width <= space_left ||
4577 needed_width <= space_right)
4579 if ((rtl && needed_width <= space_left) ||
4580 (!rtl && needed_width > space_right))
4583 x = x + xthickness - requisition.width + 1;
4587 /* position right */
4591 /* x is clamped on-screen further down */
4593 else if (requisition.width <= monitor.width)
4595 /* the menu is too big to fit on either side of the mouse
4596 * cursor, but smaller than the monitor. Position it on
4597 * the side that has the most space
4599 if (space_left > space_right)
4607 x = monitor.x + monitor.width - requisition.width;
4610 else /* menu is simply too big for the monitor */
4615 x = monitor.x + monitor.width - requisition.width;
4624 /* Position vertically. The algorithm is the same as above, but
4625 * simpler because we don't have to take RTL into account.
4627 needed_height = requisition.height - ythickness;
4629 if (needed_height <= space_above ||
4630 needed_height <= space_below)
4632 if (needed_height <= space_below)
4635 y = y + ythickness - requisition.height + 1;
4637 y = CLAMP (y, monitor.y,
4638 monitor.y + monitor.height - requisition.height);
4640 else if (needed_height > space_below && needed_height > space_above)
4642 if (space_below >= space_above)
4643 y = monitor.y + monitor.height - requisition.height;
4655 if (private->initially_pushed_in)
4657 menu_height = requisition.height;
4659 if (y + menu_height > monitor.y + monitor.height)
4661 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4662 y = (monitor.y + monitor.height) - menu_height;
4667 scroll_offset += monitor.y - y;
4672 /* FIXME: should this be done in the various position_funcs ? */
4673 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4675 if (GTK_MENU_SHELL (menu)->active)
4677 private->have_position = TRUE;
4682 if (y + requisition.height > monitor.y + monitor.height)
4683 requisition.height = (monitor.y + monitor.height) - y;
4687 scroll_offset += monitor.y - y;
4688 requisition.height -= monitor.y - y;
4692 if (scroll_offset > 0)
4694 GtkBorder arrow_border;
4696 get_arrows_border (menu, &arrow_border);
4697 scroll_offset += arrow_border.top;
4700 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4703 if (!GTK_MENU_SHELL (menu)->active)
4705 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4706 requisition.width, requisition.height);
4709 if (set_scroll_offset)
4710 menu->scroll_offset = scroll_offset;
4714 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4716 if (menu->timeout_id)
4718 g_source_remove (menu->timeout_id);
4719 menu->timeout_id = 0;
4724 gtk_menu_stop_scrolling (GtkMenu *menu)
4726 gboolean touchscreen_mode;
4728 gtk_menu_remove_scroll_timeout (menu);
4730 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4731 "gtk-touchscreen-mode", &touchscreen_mode,
4734 if (!touchscreen_mode)
4736 menu->upper_arrow_prelight = FALSE;
4737 menu->lower_arrow_prelight = FALSE;
4742 gtk_menu_scroll_to (GtkMenu *menu,
4745 GtkMenuPrivate *priv;
4746 GtkAllocation allocation;
4747 GtkBorder arrow_border;
4751 gint view_width, view_height;
4754 guint vertical_padding;
4755 guint horizontal_padding;
4756 gboolean double_arrows;
4758 widget = GTK_WIDGET (menu);
4759 priv = gtk_menu_get_private (menu);
4761 if (menu->tearoff_active &&
4762 menu->tearoff_adjustment &&
4763 (menu->tearoff_adjustment->value != offset))
4765 menu->tearoff_adjustment->value =
4767 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4768 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4771 /* Move/resize the viewport according to arrows: */
4772 gtk_widget_get_allocation (widget, &allocation);
4773 view_width = allocation.width;
4774 view_height = allocation.height;
4776 style = gtk_widget_get_style (widget);
4778 gtk_widget_style_get (GTK_WIDGET (menu),
4779 "vertical-padding", &vertical_padding,
4780 "horizontal-padding", &horizontal_padding,
4783 double_arrows = get_double_arrows (menu);
4785 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4787 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4788 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4789 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4791 x = border_width + style->xthickness + horizontal_padding;
4792 y = border_width + style->ythickness + vertical_padding;
4794 if (double_arrows && !menu->tearoff_active)
4796 if (view_height < menu_height ||
4797 (offset > 0 && menu->scroll_offset > 0) ||
4798 (offset < 0 && menu->scroll_offset < 0))
4800 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4801 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4802 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4804 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4805 gtk_widget_queue_draw (GTK_WIDGET (menu));
4807 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4809 get_arrows_border (menu, &arrow_border);
4810 y += arrow_border.top;
4811 view_height -= arrow_border.top;
4812 view_height -= arrow_border.bottom;
4815 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4816 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4817 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4818 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4820 if (offset >= menu_height - view_height)
4821 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4822 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4823 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4824 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4826 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4827 (priv->lower_arrow_state != lower_arrow_previous_state))
4828 gtk_widget_queue_draw (GTK_WIDGET (menu));
4830 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4831 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4833 /* At the upper border, possibly remove timeout */
4834 if (menu->scroll_step < 0)
4836 gtk_menu_stop_scrolling (menu);
4837 gtk_widget_queue_draw (GTK_WIDGET (menu));
4841 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4842 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4844 /* At the lower border, possibly remove timeout */
4845 if (menu->scroll_step > 0)
4847 gtk_menu_stop_scrolling (menu);
4848 gtk_widget_queue_draw (GTK_WIDGET (menu));
4852 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4856 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4857 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4859 gtk_menu_stop_scrolling (menu);
4860 gtk_widget_queue_draw (GTK_WIDGET (menu));
4863 else if (!menu->tearoff_active)
4865 gboolean last_visible;
4867 last_visible = menu->upper_arrow_visible;
4868 menu->upper_arrow_visible = offset > 0;
4870 /* upper_arrow_visible may have changed, so requery the border */
4871 get_arrows_border (menu, &arrow_border);
4872 view_height -= arrow_border.top;
4874 if ((last_visible != menu->upper_arrow_visible) &&
4875 !menu->upper_arrow_visible)
4877 menu->upper_arrow_prelight = FALSE;
4879 /* If we hid the upper arrow, possibly remove timeout */
4880 if (menu->scroll_step < 0)
4882 gtk_menu_stop_scrolling (menu);
4883 gtk_widget_queue_draw (GTK_WIDGET (menu));
4887 last_visible = menu->lower_arrow_visible;
4888 menu->lower_arrow_visible = offset < menu_height - view_height;
4890 /* lower_arrow_visible may have changed, so requery the border */
4891 get_arrows_border (menu, &arrow_border);
4892 view_height -= arrow_border.bottom;
4894 if ((last_visible != menu->lower_arrow_visible) &&
4895 !menu->lower_arrow_visible)
4897 menu->lower_arrow_prelight = FALSE;
4899 /* If we hid the lower arrow, possibly remove timeout */
4900 if (menu->scroll_step > 0)
4902 gtk_menu_stop_scrolling (menu);
4903 gtk_widget_queue_draw (GTK_WIDGET (menu));
4907 y += arrow_border.top;
4910 /* Scroll the menu: */
4911 if (gtk_widget_get_realized (widget))
4912 gdk_window_move (menu->bin_window, 0, -offset);
4914 if (gtk_widget_get_realized (widget))
4915 gdk_window_move_resize (menu->view_window,
4921 menu->scroll_offset = offset;
4925 compute_child_offset (GtkMenu *menu,
4926 GtkWidget *menu_item,
4929 gboolean *is_last_child)
4931 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4932 gint item_top_attach;
4933 gint item_bottom_attach;
4934 gint child_offset = 0;
4937 get_effective_child_attach (menu_item, NULL, NULL,
4938 &item_top_attach, &item_bottom_attach);
4940 /* there is a possibility that we get called before _size_request, so
4941 * check the height table for safety.
4943 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4946 /* when we have a row with only invisible children, it's height will
4947 * be zero, so there's no need to check WIDGET_VISIBLE here
4949 for (i = 0; i < item_top_attach; i++)
4950 child_offset += priv->heights[i];
4953 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4955 *offset = child_offset;
4957 *height = priv->heights[item_top_attach];
4963 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4964 GtkWidget *menu_item)
4966 GtkMenu *menu = GTK_MENU (menu_shell);
4967 GtkWidget *widget = GTK_WIDGET (menu_shell);
4968 gint child_offset, child_height;
4972 gboolean last_child = 0;
4974 /* We need to check if the selected item fully visible.
4975 * If not we need to scroll the menu so that it becomes fully
4979 if (compute_child_offset (menu, menu_item,
4980 &child_offset, &child_height, &last_child))
4982 guint vertical_padding;
4983 gboolean double_arrows;
4985 y = menu->scroll_offset;
4986 width = gdk_window_get_width (gtk_widget_get_window (widget));
4987 height = gdk_window_get_height (gtk_widget_get_window (widget));
4989 gtk_widget_style_get (widget,
4990 "vertical-padding", &vertical_padding,
4993 double_arrows = get_double_arrows (menu);
4995 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
4996 2 * gtk_widget_get_style (widget)->ythickness +
4997 2 * vertical_padding;
4998 if (child_offset < y)
5000 /* Ignore the enter event we might get if the pointer is on the menu
5002 menu_shell->ignore_enter = TRUE;
5003 gtk_menu_scroll_to (menu, child_offset);
5007 GtkBorder arrow_border;
5011 get_arrows_border (menu, &arrow_border);
5012 if (!menu->tearoff_active)
5013 arrow_height = arrow_border.top + arrow_border.bottom;
5015 if (child_offset + child_height > y + height - arrow_height)
5018 if ((!last_child && !menu->tearoff_active) || double_arrows)
5019 arrow_height += arrow_border.bottom;
5021 y = child_offset + child_height - height + arrow_height;
5022 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5024 /* Need upper arrow */
5025 arrow_height += arrow_border.top;
5026 y = child_offset + child_height - height + arrow_height;
5028 /* Ignore the enter event we might get if the pointer is on the menu
5030 menu_shell->ignore_enter = TRUE;
5031 gtk_menu_scroll_to (menu, y);
5039 gtk_menu_select_item (GtkMenuShell *menu_shell,
5040 GtkWidget *menu_item)
5042 GtkMenu *menu = GTK_MENU (menu_shell);
5044 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5045 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5047 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5051 /* Reparent the menu, taking care of the refcounting
5053 * If unrealize is true we force a unrealize while reparenting the parent.
5054 * This can help eliminate flicker in some cases.
5056 * What happens is that when the menu is unrealized and then re-realized,
5057 * the allocations are as follows:
5059 * parent - 1x1 at (0,0)
5060 * child1 - 100x20 at (0,0)
5061 * child2 - 100x20 at (0,20)
5062 * child3 - 100x20 at (0,40)
5064 * That is, the parent is small but the children are full sized. Then,
5065 * when the queued_resize gets processed, the parent gets resized to
5068 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5069 * contains the following logic:
5071 * - if a move or resize operation on a window would change the clip
5072 * region on the children, then before the window is resized
5073 * the background for children is temporarily set to None, the
5074 * move/resize done, and the background for the children restored.
5076 * So, at the point where the parent is resized to final size, the
5077 * background for the children is temporarily None, and thus they
5078 * are not cleared to the background color and the previous background
5079 * (the image of the menu) is left in place.
5082 gtk_menu_reparent (GtkMenu *menu,
5083 GtkWidget *new_parent,
5086 GObject *object = G_OBJECT (menu);
5087 GtkWidget *widget = GTK_WIDGET (menu);
5088 gboolean was_floating = g_object_is_floating (object);
5090 g_object_ref_sink (object);
5094 g_object_ref (object);
5095 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5096 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5097 g_object_unref (object);
5100 gtk_widget_reparent (widget, new_parent);
5103 g_object_force_floating (object);
5105 g_object_unref (object);
5109 gtk_menu_show_all (GtkWidget *widget)
5111 /* Show children, but not self. */
5112 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5116 * gtk_menu_set_screen:
5117 * @menu: a #GtkMenu.
5118 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5119 * determined by the widget the menu is attached to.
5121 * Sets the #GdkScreen on which the menu will be displayed.
5126 gtk_menu_set_screen (GtkMenu *menu,
5129 g_return_if_fail (GTK_IS_MENU (menu));
5130 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5132 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5136 menu_change_screen (menu, screen);
5140 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5142 attach_widget_screen_changed (attach_widget, NULL, menu);
5148 * @menu: a #GtkMenu.
5149 * @child: a #GtkMenuItem.
5150 * @left_attach: The column number to attach the left side of the item to.
5151 * @right_attach: The column number to attach the right side of the item to.
5152 * @top_attach: The row number to attach the top of the item to.
5153 * @bottom_attach: The row number to attach the bottom of the item to.
5155 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5156 * an item will occupy is specified by @left_attach, @right_attach,
5157 * @top_attach and @bottom_attach. These each represent the leftmost,
5158 * rightmost, uppermost and lower column and row numbers of the table.
5159 * (Columns and rows are indexed from zero).
5161 * Note that this function is not related to gtk_menu_detach().
5166 gtk_menu_attach (GtkMenu *menu,
5171 guint bottom_attach)
5173 GtkMenuShell *menu_shell;
5176 g_return_if_fail (GTK_IS_MENU (menu));
5177 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5178 parent = gtk_widget_get_parent (child);
5179 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5180 g_return_if_fail (left_attach < right_attach);
5181 g_return_if_fail (top_attach < bottom_attach);
5183 menu_shell = GTK_MENU_SHELL (menu);
5187 AttachInfo *ai = get_attach_info (child);
5189 ai->left_attach = left_attach;
5190 ai->right_attach = right_attach;
5191 ai->top_attach = top_attach;
5192 ai->bottom_attach = bottom_attach;
5194 menu_shell->children = g_list_append (menu_shell->children, child);
5196 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5198 menu_queue_resize (menu);
5202 gtk_container_child_set (GTK_CONTAINER (parent), child,
5203 "left-attach", left_attach,
5204 "right-attach", right_attach,
5205 "top-attach", top_attach,
5206 "bottom-attach", bottom_attach,
5212 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5216 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5217 "gtk-menu-popup-delay", &popup_delay,
5224 find_child_containing (GtkMenuShell *menu_shell,
5232 /* find a child which includes the area given by
5233 * left, right, top, bottom.
5236 for (list = menu_shell->children; list; list = list->next)
5240 if (!_gtk_menu_item_is_selectable (list->data))
5243 get_effective_child_attach (list->data, &l, &r, &t, &b);
5245 if (l <= left && right <= r
5246 && t <= top && bottom <= b)
5247 return GTK_WIDGET (list->data);
5254 gtk_menu_move_current (GtkMenuShell *menu_shell,
5255 GtkMenuDirectionType direction)
5257 GtkMenu *menu = GTK_MENU (menu_shell);
5260 GtkWidget *match = NULL;
5262 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5266 case GTK_MENU_DIR_CHILD:
5267 direction = GTK_MENU_DIR_PARENT;
5269 case GTK_MENU_DIR_PARENT:
5270 direction = GTK_MENU_DIR_CHILD;
5276 /* use special table menu key bindings */
5277 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5279 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5281 if (direction == GTK_MENU_DIR_NEXT)
5283 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5285 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5293 for (i = 0; i < t; i++)
5295 match = find_child_containing (menu_shell,
5296 l, l + 1, i, i + 1);
5302 else if (direction == GTK_MENU_DIR_PREV)
5304 for (i = t; i > 0; i--)
5306 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5314 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5316 match = find_child_containing (menu_shell,
5317 l, l + 1, i - 1, i);
5323 else if (direction == GTK_MENU_DIR_PARENT)
5325 /* we go one left if possible */
5327 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5331 GtkWidget *parent = menu_shell->parent_menu_shell;
5334 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5335 match = menu_shell->active_menu_item;
5338 else if (direction == GTK_MENU_DIR_CHILD)
5340 /* we go one right if possible */
5341 if (r < gtk_menu_get_n_columns (menu))
5342 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5346 GtkWidget *parent = menu_shell->parent_menu_shell;
5348 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5350 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5351 match = menu_shell->active_menu_item;
5357 gtk_menu_shell_select_item (menu_shell, match);
5362 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5366 get_visible_size (GtkMenu *menu)
5368 GtkAllocation allocation;
5369 GtkWidget *widget = GTK_WIDGET (menu);
5370 GtkContainer *container = GTK_CONTAINER (menu);
5373 gtk_widget_get_allocation (widget, &allocation);
5374 menu_height = (allocation.height
5375 - 2 * (gtk_container_get_border_width (container)
5376 + gtk_widget_get_style (widget)->ythickness));
5378 if (!menu->tearoff_active)
5380 GtkBorder arrow_border;
5382 get_arrows_border (menu, &arrow_border);
5383 menu_height -= arrow_border.top;
5384 menu_height -= arrow_border.bottom;
5390 /* Find the sensitive on-screen child containing @y, or if none,
5391 * the nearest selectable onscreen child. (%NULL if none)
5394 child_at (GtkMenu *menu,
5397 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5398 GtkWidget *child = NULL;
5399 gint child_offset = 0;
5402 gint lower, upper; /* Onscreen bounds */
5404 menu_height = get_visible_size (menu);
5405 lower = menu->scroll_offset;
5406 upper = menu->scroll_offset + menu_height;
5408 for (children = menu_shell->children; children; children = children->next)
5410 if (gtk_widget_get_visible (children->data))
5412 GtkRequisition child_requisition;
5414 gtk_widget_get_preferred_size (children->data,
5415 &child_requisition, NULL);
5417 if (_gtk_menu_item_is_selectable (children->data) &&
5418 child_offset >= lower &&
5419 child_offset + child_requisition.height <= upper)
5421 child = children->data;
5423 if (child_offset + child_requisition.height > y &&
5424 !GTK_IS_TEAROFF_MENU_ITEM (child))
5428 child_offset += child_requisition.height;
5436 get_menu_height (GtkMenu *menu)
5438 GtkAllocation allocation;
5439 GtkWidget *widget = GTK_WIDGET (menu);
5442 gtk_widget_get_allocation (widget, &allocation);
5444 height = allocation.height;
5445 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) + gtk_widget_get_style (widget)->ythickness) * 2;
5447 if (!menu->tearoff_active)
5449 GtkBorder arrow_border;
5451 get_arrows_border (menu, &arrow_border);
5452 height -= arrow_border.top;
5453 height -= arrow_border.bottom;
5460 gtk_menu_real_move_scroll (GtkMenu *menu,
5463 gint page_size = get_visible_size (menu);
5464 gint end_position = get_menu_height (menu);
5465 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5469 case GTK_SCROLL_PAGE_UP:
5470 case GTK_SCROLL_PAGE_DOWN:
5474 gint child_offset = 0;
5475 gboolean old_upper_arrow_visible;
5478 if (type == GTK_SCROLL_PAGE_UP)
5483 if (menu_shell->active_menu_item)
5487 compute_child_offset (menu, menu_shell->active_menu_item,
5488 &child_offset, &child_height, NULL);
5489 child_offset += child_height / 2;
5492 menu_shell->ignore_enter = TRUE;
5493 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5494 old_offset = menu->scroll_offset;
5496 new_offset = menu->scroll_offset + step;
5497 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5499 gtk_menu_scroll_to (menu, new_offset);
5501 if (menu_shell->active_menu_item)
5503 GtkWidget *new_child;
5504 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5505 GtkBorder arrow_border;
5507 get_arrows_border (menu, &arrow_border);
5509 if (menu->scroll_offset != old_offset)
5510 step = menu->scroll_offset - old_offset;
5512 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5514 new_child = child_at (menu, child_offset + step);
5516 gtk_menu_shell_select_item (menu_shell, new_child);
5520 case GTK_SCROLL_START:
5521 /* Ignore the enter event we might get if the pointer is on the menu
5523 menu_shell->ignore_enter = TRUE;
5524 gtk_menu_scroll_to (menu, 0);
5525 gtk_menu_shell_select_first (menu_shell, TRUE);
5527 case GTK_SCROLL_END:
5528 /* Ignore the enter event we might get if the pointer is on the menu
5530 menu_shell->ignore_enter = TRUE;
5531 gtk_menu_scroll_to (menu, end_position - page_size);
5532 _gtk_menu_shell_select_last (menu_shell, TRUE);
5541 * gtk_menu_set_monitor:
5543 * @monitor_num: the number of the monitor on which the menu should
5546 * Informs GTK+ on which monitor a menu should be popped up.
5547 * See gdk_screen_get_monitor_geometry().
5549 * This function should be called from a #GtkMenuPositionFunc if the
5550 * menu should not appear on the same monitor as the pointer. This
5551 * information can't be reliably inferred from the coordinates returned
5552 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5553 * may extend beyond the monitor boundaries or even the screen boundaries.
5558 gtk_menu_set_monitor (GtkMenu *menu,
5561 GtkMenuPrivate *priv;
5562 g_return_if_fail (GTK_IS_MENU (menu));
5564 priv = gtk_menu_get_private (menu);
5566 priv->monitor_num = monitor_num;
5570 * gtk_menu_get_monitor:
5573 * Retrieves the number of the monitor on which to show the menu.
5575 * Returns: the number of the monitor on which the menu should
5576 * be popped up or -1, if no monitor has been set
5581 gtk_menu_get_monitor (GtkMenu *menu)
5583 GtkMenuPrivate *priv;
5584 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5586 priv = gtk_menu_get_private (menu);
5588 return priv->monitor_num;
5592 * gtk_menu_get_for_attach_widget:
5593 * @widget: a #GtkWidget
5595 * Returns a list of the menus which are attached to this widget.
5596 * This list is owned by GTK+ and must not be modified.
5598 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5603 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5607 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5609 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5614 gtk_menu_grab_notify (GtkWidget *widget,
5615 gboolean was_grabbed)
5617 GtkWidget *toplevel;
5618 GtkWindowGroup *group;
5622 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5625 !gtk_widget_device_is_shadowed (widget, pointer))
5628 toplevel = gtk_widget_get_toplevel (widget);
5630 if (!GTK_IS_WINDOW (toplevel))
5633 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5634 grab = gtk_window_group_get_current_device_grab (group, pointer);
5636 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5637 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5641 * gtk_menu_set_reserve_toggle_size:
5643 * @reserve_toggle_size: whether to reserve size for toggles
5645 * Sets whether the menu should reserve space for drawing toggles
5646 * or icons, regardless of their actual presence.
5651 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5652 gboolean reserve_toggle_size)
5654 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5655 gboolean no_toggle_size;
5657 no_toggle_size = !reserve_toggle_size;
5659 if (priv->no_toggle_size != no_toggle_size)
5661 priv->no_toggle_size = no_toggle_size;
5663 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5668 * gtk_menu_get_reserve_toggle_size:
5671 * Returns whether the menu reserves space for toggles and
5672 * icons, regardless of their actual presence.
5674 * Returns: Whether the menu reserves toggle space
5679 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5681 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5683 return !priv->no_toggle_size;