1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #define GTK_MENU_INTERNALS
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
34 #include "gtkcheckmenuitem.h"
35 #include <gobject/gvaluecollector.h>
37 #include "gtkmarshalers.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
49 #define DEFAULT_POPUP_DELAY 225
50 #define DEFAULT_POPDOWN_DELAY 1000
52 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
53 * extends below the submenu
56 #define MENU_SCROLL_STEP1 8
57 #define MENU_SCROLL_STEP2 15
58 #define MENU_SCROLL_FAST_ZONE 8
59 #define MENU_SCROLL_TIMEOUT1 50
60 #define MENU_SCROLL_TIMEOUT2 20
62 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
63 #define ATTACHED_MENUS "gtk-attached-menus"
65 typedef struct _GtkMenuAttachData GtkMenuAttachData;
66 typedef struct _GtkMenuPrivate GtkMenuPrivate;
68 struct _GtkMenuAttachData
70 GtkWidget *attach_widget;
71 GtkMenuDetachFunc detacher;
74 struct _GtkMenuPrivate
78 gboolean initially_pushed_in;
80 /* info used for the table */
86 /* Cached layout information */
93 GtkStateType lower_arrow_state;
94 GtkStateType upper_arrow_state;
96 guint have_layout : 1;
97 guint seen_item_enter : 1;
98 guint have_position : 1;
99 guint ignore_button_release : 1;
100 guint no_toggle_size : 1;
109 gint effective_left_attach;
110 gint effective_right_attach;
111 gint effective_top_attach;
112 gint effective_bottom_attach;
129 PROP_RESERVE_TOGGLE_SIZE
134 CHILD_PROP_LEFT_ATTACH,
135 CHILD_PROP_RIGHT_ATTACH,
136 CHILD_PROP_TOP_ATTACH,
137 CHILD_PROP_BOTTOM_ATTACH
140 static void gtk_menu_set_property (GObject *object,
144 static void gtk_menu_get_property (GObject *object,
148 static void gtk_menu_set_child_property(GtkContainer *container,
153 static void gtk_menu_get_child_property(GtkContainer *container,
158 static void gtk_menu_destroy (GtkObject *object);
159 static void gtk_menu_realize (GtkWidget *widget);
160 static void gtk_menu_unrealize (GtkWidget *widget);
161 static void gtk_menu_size_request (GtkWidget *widget,
162 GtkRequisition *requisition);
163 static void gtk_menu_size_allocate (GtkWidget *widget,
164 GtkAllocation *allocation);
165 static void gtk_menu_paint (GtkWidget *widget,
166 GdkEventExpose *expose);
167 static void gtk_menu_show (GtkWidget *widget);
168 static gboolean gtk_menu_expose (GtkWidget *widget,
169 GdkEventExpose *event);
170 static gboolean gtk_menu_key_press (GtkWidget *widget,
172 static gboolean gtk_menu_scroll (GtkWidget *widget,
173 GdkEventScroll *event);
174 static gboolean gtk_menu_button_press (GtkWidget *widget,
175 GdkEventButton *event);
176 static gboolean gtk_menu_button_release (GtkWidget *widget,
177 GdkEventButton *event);
178 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
179 GdkEventMotion *event);
180 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
181 GdkEventCrossing *event);
182 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
183 GdkEventCrossing *event);
184 static void gtk_menu_scroll_to (GtkMenu *menu,
186 static void gtk_menu_grab_notify (GtkWidget *widget,
187 gboolean was_grabbed);
189 static void gtk_menu_stop_scrolling (GtkMenu *menu);
190 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
191 static gboolean gtk_menu_scroll_timeout (gpointer data);
192 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
193 static void gtk_menu_start_scrolling (GtkMenu *menu);
195 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
196 GtkWidget *menu_item);
197 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
198 GtkWidget *menu_item);
199 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
202 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
204 static void gtk_menu_handle_scrolling (GtkMenu *menu,
209 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
211 static void gtk_menu_style_set (GtkWidget *widget,
212 GtkStyle *previous_style);
213 static gboolean gtk_menu_focus (GtkWidget *widget,
214 GtkDirectionType direction);
215 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
216 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
217 GtkMenuDirectionType direction);
218 static void gtk_menu_real_move_scroll (GtkMenu *menu,
221 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
222 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
223 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
226 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
227 GtkMenuItem *menu_item,
228 GdkEventCrossing *event);
230 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
231 static void gtk_menu_show_all (GtkWidget *widget);
232 static void gtk_menu_hide_all (GtkWidget *widget);
233 static void gtk_menu_position (GtkMenu *menu);
234 static void gtk_menu_reparent (GtkMenu *menu,
235 GtkWidget *new_parent,
237 static void gtk_menu_remove (GtkContainer *menu,
240 static void gtk_menu_update_title (GtkMenu *menu);
242 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
243 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
245 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
247 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
248 gboolean group_changed);
250 static const gchar attach_data_key[] = "gtk-menu-attach-data";
252 static guint menu_signals[LAST_SIGNAL] = { 0 };
254 static GtkMenuPrivate *
255 gtk_menu_get_private (GtkMenu *menu)
257 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
260 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
263 menu_queue_resize (GtkMenu *menu)
265 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
267 priv->have_layout = FALSE;
268 gtk_widget_queue_resize (GTK_WIDGET (menu));
272 attach_info_free (AttachInfo *info)
274 g_slice_free (AttachInfo, info);
278 get_attach_info (GtkWidget *child)
280 GObject *object = G_OBJECT (child);
281 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
285 ai = g_slice_new0 (AttachInfo);
286 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
287 (GDestroyNotify) attach_info_free);
294 is_grid_attached (AttachInfo *ai)
296 return (ai->left_attach >= 0 &&
297 ai->right_attach >= 0 &&
298 ai->top_attach >= 0 &&
299 ai->bottom_attach >= 0);
303 menu_ensure_layout (GtkMenu *menu)
305 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
307 if (!priv->have_layout)
309 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
313 gint max_right_attach;
314 gint max_bottom_attach;
316 /* Find extents of gridded portion
318 max_right_attach = 1;
319 max_bottom_attach = 0;
321 for (l = menu_shell->children; l; l = l->next)
323 GtkWidget *child = l->data;
324 AttachInfo *ai = get_attach_info (child);
326 if (is_grid_attached (ai))
328 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
329 max_right_attach = MAX (max_right_attach, ai->right_attach);
335 row_occupied = g_malloc0 (max_bottom_attach);
337 for (l = menu_shell->children; l; l = l->next)
339 GtkWidget *child = l->data;
340 AttachInfo *ai = get_attach_info (child);
342 if (is_grid_attached (ai))
346 for (i = ai->top_attach; i < ai->bottom_attach; i++)
347 row_occupied[i] = TRUE;
351 /* Lay non-grid-items out in those rows
354 for (l = menu_shell->children; l; l = l->next)
356 GtkWidget *child = l->data;
357 AttachInfo *ai = get_attach_info (child);
359 if (!is_grid_attached (ai))
361 while (current_row < max_bottom_attach && row_occupied[current_row])
364 ai->effective_left_attach = 0;
365 ai->effective_right_attach = max_right_attach;
366 ai->effective_top_attach = current_row;
367 ai->effective_bottom_attach = current_row + 1;
373 ai->effective_left_attach = ai->left_attach;
374 ai->effective_right_attach = ai->right_attach;
375 ai->effective_top_attach = ai->top_attach;
376 ai->effective_bottom_attach = ai->bottom_attach;
380 g_free (row_occupied);
382 priv->n_rows = MAX (current_row, max_bottom_attach);
383 priv->n_columns = max_right_attach;
384 priv->have_layout = TRUE;
390 gtk_menu_get_n_columns (GtkMenu *menu)
392 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
394 menu_ensure_layout (menu);
396 return priv->n_columns;
400 gtk_menu_get_n_rows (GtkMenu *menu)
402 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
404 menu_ensure_layout (menu);
410 get_effective_child_attach (GtkWidget *child,
416 GtkMenu *menu = GTK_MENU (child->parent);
419 menu_ensure_layout (menu);
421 ai = get_attach_info (child);
424 *l = ai->effective_left_attach;
426 *r = ai->effective_right_attach;
428 *t = ai->effective_top_attach;
430 *b = ai->effective_bottom_attach;
435 gtk_menu_class_init (GtkMenuClass *class)
437 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
438 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
439 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
440 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
441 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
442 GtkBindingSet *binding_set;
444 gobject_class->set_property = gtk_menu_set_property;
445 gobject_class->get_property = gtk_menu_get_property;
447 object_class->destroy = gtk_menu_destroy;
449 widget_class->realize = gtk_menu_realize;
450 widget_class->unrealize = gtk_menu_unrealize;
451 widget_class->size_request = gtk_menu_size_request;
452 widget_class->size_allocate = gtk_menu_size_allocate;
453 widget_class->show = gtk_menu_show;
454 widget_class->expose_event = gtk_menu_expose;
455 widget_class->scroll_event = gtk_menu_scroll;
456 widget_class->key_press_event = gtk_menu_key_press;
457 widget_class->button_press_event = gtk_menu_button_press;
458 widget_class->button_release_event = gtk_menu_button_release;
459 widget_class->motion_notify_event = gtk_menu_motion_notify;
460 widget_class->show_all = gtk_menu_show_all;
461 widget_class->hide_all = gtk_menu_hide_all;
462 widget_class->enter_notify_event = gtk_menu_enter_notify;
463 widget_class->leave_notify_event = gtk_menu_leave_notify;
464 widget_class->style_set = gtk_menu_style_set;
465 widget_class->focus = gtk_menu_focus;
466 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
467 widget_class->grab_notify = gtk_menu_grab_notify;
469 container_class->remove = gtk_menu_remove;
470 container_class->get_child_property = gtk_menu_get_child_property;
471 container_class->set_child_property = gtk_menu_set_child_property;
473 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
474 menu_shell_class->deactivate = gtk_menu_deactivate;
475 menu_shell_class->select_item = gtk_menu_select_item;
476 menu_shell_class->insert = gtk_menu_real_insert;
477 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
478 menu_shell_class->move_current = gtk_menu_move_current;
480 menu_signals[MOVE_SCROLL] =
481 g_signal_new_class_handler (I_("move-scroll"),
482 G_OBJECT_CLASS_TYPE (object_class),
483 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
484 G_CALLBACK (gtk_menu_real_move_scroll),
486 _gtk_marshal_VOID__ENUM,
488 GTK_TYPE_SCROLL_TYPE);
493 * The index of the currently selected menu item, or -1 if no
494 * menu item is selected.
498 g_object_class_install_property (gobject_class,
500 g_param_spec_int ("active",
502 P_("The currently selected menu item"),
504 GTK_PARAM_READWRITE));
507 * GtkMenu:accel-group:
509 * The accel group holding accelerators for the menu.
513 g_object_class_install_property (gobject_class,
515 g_param_spec_object ("accel-group",
517 P_("The accel group holding accelerators for the menu"),
518 GTK_TYPE_ACCEL_GROUP,
519 GTK_PARAM_READWRITE));
522 * GtkMenu:accel-path:
524 * An accel path used to conveniently construct accel paths of child items.
528 g_object_class_install_property (gobject_class,
530 g_param_spec_string ("accel-path",
532 P_("An accel path used to conveniently construct accel paths of child items"),
534 GTK_PARAM_READWRITE));
537 * GtkMenu:attach-widget:
539 * The widget the menu is attached to. Setting this property attaches
540 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
541 * use gtk_menu_attach_to_widget() directly.
545 g_object_class_install_property (gobject_class,
547 g_param_spec_object ("attach-widget",
549 P_("The widget the menu is attached to"),
551 GTK_PARAM_READWRITE));
553 g_object_class_install_property (gobject_class,
555 g_param_spec_string ("tearoff-title",
557 P_("A title that may be displayed by the window manager when this menu is torn-off"),
559 GTK_PARAM_READWRITE));
562 * GtkMenu:tearoff-state:
564 * A boolean that indicates whether the menu is torn-off.
568 g_object_class_install_property (gobject_class,
570 g_param_spec_boolean ("tearoff-state",
572 P_("A boolean that indicates whether the menu is torn-off"),
574 GTK_PARAM_READWRITE));
579 * The monitor the menu will be popped up on.
583 g_object_class_install_property (gobject_class,
585 g_param_spec_int ("monitor",
587 P_("The monitor the menu will be popped up on"),
589 GTK_PARAM_READWRITE));
591 gtk_widget_class_install_style_property (widget_class,
592 g_param_spec_int ("vertical-padding",
593 P_("Vertical Padding"),
594 P_("Extra space at the top and bottom of the menu"),
598 GTK_PARAM_READABLE));
601 * GtkMenu:reserve-toggle-size:
603 * A boolean that indicates whether the menu reserves space for
604 * toggles and icons, regardless of their actual presence.
606 * This property should only be changed from its default value
607 * for special-purposes such as tabular menus. Regular menus that
608 * are connected to a menu bar or context menus should reserve
609 * toggle space for consistency.
613 g_object_class_install_property (gobject_class,
614 PROP_RESERVE_TOGGLE_SIZE,
615 g_param_spec_boolean ("reserve-toggle-size",
616 P_("Reserve Toggle Size"),
617 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
619 GTK_PARAM_READWRITE));
621 gtk_widget_class_install_style_property (widget_class,
622 g_param_spec_int ("horizontal-padding",
623 P_("Horizontal Padding"),
624 P_("Extra space at the left and right edges of the menu"),
628 GTK_PARAM_READABLE));
630 gtk_widget_class_install_style_property (widget_class,
631 g_param_spec_int ("vertical-offset",
632 P_("Vertical Offset"),
633 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
637 GTK_PARAM_READABLE));
639 gtk_widget_class_install_style_property (widget_class,
640 g_param_spec_int ("horizontal-offset",
641 P_("Horizontal Offset"),
642 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
646 GTK_PARAM_READABLE));
648 gtk_widget_class_install_style_property (widget_class,
649 g_param_spec_boolean ("double-arrows",
651 P_("When scrolling, always show both arrows."),
653 GTK_PARAM_READABLE));
656 * GtkMenu:arrow-placement:
658 * Indicates where scroll arrows should be placed.
662 gtk_widget_class_install_style_property (widget_class,
663 g_param_spec_enum ("arrow-placement",
664 P_("Arrow Placement"),
665 P_("Indicates where scroll arrows should be placed"),
666 GTK_TYPE_ARROW_PLACEMENT,
668 GTK_PARAM_READABLE));
670 gtk_container_class_install_child_property (container_class,
671 CHILD_PROP_LEFT_ATTACH,
672 g_param_spec_int ("left-attach",
674 P_("The column number to attach the left side of the child to"),
676 GTK_PARAM_READWRITE));
678 gtk_container_class_install_child_property (container_class,
679 CHILD_PROP_RIGHT_ATTACH,
680 g_param_spec_int ("right-attach",
682 P_("The column number to attach the right side of the child to"),
684 GTK_PARAM_READWRITE));
686 gtk_container_class_install_child_property (container_class,
687 CHILD_PROP_TOP_ATTACH,
688 g_param_spec_int ("top-attach",
690 P_("The row number to attach the top of the child to"),
692 GTK_PARAM_READWRITE));
694 gtk_container_class_install_child_property (container_class,
695 CHILD_PROP_BOTTOM_ATTACH,
696 g_param_spec_int ("bottom-attach",
698 P_("The row number to attach the bottom of the child to"),
700 GTK_PARAM_READWRITE));
703 * GtkMenu::arrow-scaling
705 * Arbitrary constant to scale down the size of the scroll arrow.
709 gtk_widget_class_install_style_property (widget_class,
710 g_param_spec_float ("arrow-scaling",
712 P_("Arbitrary constant to scale down the size of the scroll arrow"),
714 GTK_PARAM_READABLE));
716 binding_set = gtk_binding_set_by_class (class);
717 gtk_binding_entry_add_signal (binding_set,
719 I_("move-current"), 1,
720 GTK_TYPE_MENU_DIRECTION_TYPE,
722 gtk_binding_entry_add_signal (binding_set,
725 GTK_TYPE_MENU_DIRECTION_TYPE,
727 gtk_binding_entry_add_signal (binding_set,
730 GTK_TYPE_MENU_DIRECTION_TYPE,
732 gtk_binding_entry_add_signal (binding_set,
735 GTK_TYPE_MENU_DIRECTION_TYPE,
737 gtk_binding_entry_add_signal (binding_set,
740 GTK_TYPE_MENU_DIRECTION_TYPE,
741 GTK_MENU_DIR_PARENT);
742 gtk_binding_entry_add_signal (binding_set,
745 GTK_TYPE_MENU_DIRECTION_TYPE,
746 GTK_MENU_DIR_PARENT);
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_SCROLL_TYPE,
762 gtk_binding_entry_add_signal (binding_set,
765 GTK_TYPE_SCROLL_TYPE,
767 gtk_binding_entry_add_signal (binding_set,
770 GTK_TYPE_SCROLL_TYPE,
772 gtk_binding_entry_add_signal (binding_set,
775 GTK_TYPE_SCROLL_TYPE,
777 gtk_binding_entry_add_signal (binding_set,
780 GTK_TYPE_SCROLL_TYPE,
782 gtk_binding_entry_add_signal (binding_set,
785 GTK_TYPE_SCROLL_TYPE,
787 gtk_binding_entry_add_signal (binding_set,
790 GTK_TYPE_SCROLL_TYPE,
791 GTK_SCROLL_PAGE_DOWN);
792 gtk_binding_entry_add_signal (binding_set,
795 GTK_TYPE_SCROLL_TYPE,
796 GTK_SCROLL_PAGE_DOWN);
798 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
799 P_("Can change accelerators"),
800 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
802 GTK_PARAM_READWRITE));
804 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
805 P_("Delay before submenus appear"),
806 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
810 GTK_PARAM_READWRITE));
812 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
813 P_("Delay before hiding a submenu"),
814 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
817 DEFAULT_POPDOWN_DELAY,
818 GTK_PARAM_READWRITE));
820 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
825 gtk_menu_set_property (GObject *object,
830 GtkMenu *menu = GTK_MENU (object);
835 gtk_menu_set_active (menu, g_value_get_int (value));
837 case PROP_ACCEL_GROUP:
838 gtk_menu_set_accel_group (menu, g_value_get_object (value));
840 case PROP_ACCEL_PATH:
841 gtk_menu_set_accel_path (menu, g_value_get_string (value));
843 case PROP_ATTACH_WIDGET:
847 widget = gtk_menu_get_attach_widget (menu);
849 gtk_menu_detach (menu);
851 widget = (GtkWidget*) g_value_get_object (value);
853 gtk_menu_attach_to_widget (menu, widget, NULL);
856 case PROP_TEAROFF_STATE:
857 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
859 case PROP_TEAROFF_TITLE:
860 gtk_menu_set_title (menu, g_value_get_string (value));
863 gtk_menu_set_monitor (menu, g_value_get_int (value));
865 case PROP_RESERVE_TOGGLE_SIZE:
866 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
869 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
875 gtk_menu_get_property (GObject *object,
880 GtkMenu *menu = GTK_MENU (object);
885 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
887 case PROP_ACCEL_GROUP:
888 g_value_set_object (value, gtk_menu_get_accel_group (menu));
890 case PROP_ACCEL_PATH:
891 g_value_set_string (value, gtk_menu_get_accel_path (menu));
893 case PROP_ATTACH_WIDGET:
894 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
896 case PROP_TEAROFF_STATE:
897 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
899 case PROP_TEAROFF_TITLE:
900 g_value_set_string (value, gtk_menu_get_title (menu));
903 g_value_set_int (value, gtk_menu_get_monitor (menu));
905 case PROP_RESERVE_TOGGLE_SIZE:
906 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
909 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
915 gtk_menu_set_child_property (GtkContainer *container,
921 GtkMenu *menu = GTK_MENU (container);
922 AttachInfo *ai = get_attach_info (child);
926 case CHILD_PROP_LEFT_ATTACH:
927 ai->left_attach = g_value_get_int (value);
929 case CHILD_PROP_RIGHT_ATTACH:
930 ai->right_attach = g_value_get_int (value);
932 case CHILD_PROP_TOP_ATTACH:
933 ai->top_attach = g_value_get_int (value);
935 case CHILD_PROP_BOTTOM_ATTACH:
936 ai->bottom_attach = g_value_get_int (value);
940 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
944 menu_queue_resize (menu);
948 gtk_menu_get_child_property (GtkContainer *container,
954 AttachInfo *ai = get_attach_info (child);
958 case CHILD_PROP_LEFT_ATTACH:
959 g_value_set_int (value, ai->left_attach);
961 case CHILD_PROP_RIGHT_ATTACH:
962 g_value_set_int (value, ai->right_attach);
964 case CHILD_PROP_TOP_ATTACH:
965 g_value_set_int (value, ai->top_attach);
967 case CHILD_PROP_BOTTOM_ATTACH:
968 g_value_set_int (value, ai->bottom_attach);
972 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
978 gtk_menu_window_event (GtkWidget *window,
982 gboolean handled = FALSE;
984 g_object_ref (window);
990 case GDK_KEY_RELEASE:
991 handled = gtk_widget_event (menu, event);
997 g_object_unref (window);
998 g_object_unref (menu);
1004 gtk_menu_window_size_request (GtkWidget *window,
1005 GtkRequisition *requisition,
1008 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1010 if (private->have_position)
1012 GdkScreen *screen = gtk_widget_get_screen (window);
1013 GdkRectangle monitor;
1015 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
1017 if (private->y + requisition->height > monitor.y + monitor.height)
1018 requisition->height = monitor.y + monitor.height - private->y;
1020 if (private->y < monitor.y)
1021 requisition->height -= monitor.y - private->y;
1026 gtk_menu_init (GtkMenu *menu)
1028 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1030 menu->parent_menu_item = NULL;
1031 menu->old_active_menu_item = NULL;
1032 menu->accel_group = NULL;
1033 menu->position_func = NULL;
1034 menu->position_func_data = NULL;
1035 menu->toggle_size = 0;
1037 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1038 "type", GTK_WINDOW_POPUP,
1041 "signal::event", gtk_menu_window_event, menu,
1042 "signal::size-request", gtk_menu_window_size_request, menu,
1043 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1045 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1046 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1048 /* Refloat the menu, so that reference counting for the menu isn't
1049 * affected by it being a child of the toplevel
1051 g_object_force_floating (G_OBJECT (menu));
1052 menu->needs_destruction_ref_count = TRUE;
1054 menu->view_window = NULL;
1055 menu->bin_window = NULL;
1057 menu->scroll_offset = 0;
1058 menu->scroll_step = 0;
1059 menu->timeout_id = 0;
1060 menu->scroll_fast = FALSE;
1062 menu->tearoff_window = NULL;
1063 menu->tearoff_hbox = NULL;
1064 menu->torn_off = FALSE;
1065 menu->tearoff_active = FALSE;
1066 menu->tearoff_adjustment = NULL;
1067 menu->tearoff_scrollbar = NULL;
1069 menu->upper_arrow_visible = FALSE;
1070 menu->lower_arrow_visible = FALSE;
1071 menu->upper_arrow_prelight = FALSE;
1072 menu->lower_arrow_prelight = FALSE;
1074 priv->upper_arrow_state = GTK_STATE_NORMAL;
1075 priv->lower_arrow_state = GTK_STATE_NORMAL;
1077 priv->have_layout = FALSE;
1078 priv->monitor_num = -1;
1082 gtk_menu_destroy (GtkObject *object)
1084 GtkMenu *menu = GTK_MENU (object);
1085 GtkMenuAttachData *data;
1086 GtkMenuPrivate *priv;
1088 gtk_menu_remove_scroll_timeout (menu);
1090 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1092 gtk_menu_detach (menu);
1094 gtk_menu_stop_navigating_submenu (menu);
1096 if (menu->old_active_menu_item)
1098 g_object_unref (menu->old_active_menu_item);
1099 menu->old_active_menu_item = NULL;
1102 /* Add back the reference count for being a child */
1103 if (menu->needs_destruction_ref_count)
1105 menu->needs_destruction_ref_count = FALSE;
1106 g_object_ref (object);
1109 if (menu->accel_group)
1111 g_object_unref (menu->accel_group);
1112 menu->accel_group = NULL;
1116 gtk_widget_destroy (menu->toplevel);
1118 if (menu->tearoff_window)
1119 gtk_widget_destroy (menu->tearoff_window);
1121 priv = gtk_menu_get_private (menu);
1125 g_free (priv->heights);
1126 priv->heights = NULL;
1131 g_free (priv->title);
1135 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1139 menu_change_screen (GtkMenu *menu,
1140 GdkScreen *new_screen)
1142 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1144 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1146 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1152 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1153 gtk_menu_position (menu);
1156 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1157 private->monitor_num = -1;
1161 attach_widget_screen_changed (GtkWidget *attach_widget,
1162 GdkScreen *previous_screen,
1165 if (gtk_widget_has_screen (attach_widget) &&
1166 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1168 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1173 gtk_menu_attach_to_widget (GtkMenu *menu,
1174 GtkWidget *attach_widget,
1175 GtkMenuDetachFunc detacher)
1177 GtkMenuAttachData *data;
1180 g_return_if_fail (GTK_IS_MENU (menu));
1181 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1183 /* keep this function in sync with gtk_widget_set_parent()
1186 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1189 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1190 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1194 g_object_ref_sink (menu);
1196 data = g_slice_new (GtkMenuAttachData);
1197 data->attach_widget = attach_widget;
1199 g_signal_connect (attach_widget, "screen-changed",
1200 G_CALLBACK (attach_widget_screen_changed), menu);
1201 attach_widget_screen_changed (attach_widget, NULL, menu);
1203 data->detacher = detacher;
1204 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1205 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1206 if (!g_list_find (list, menu))
1208 list = g_list_prepend (list, menu);
1210 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1211 (GDestroyNotify) g_list_free);
1213 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1214 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1216 /* we don't need to set the style here, since
1217 * we are a toplevel widget.
1220 /* Fallback title for menu comes from attach widget */
1221 gtk_menu_update_title (menu);
1225 gtk_menu_get_attach_widget (GtkMenu *menu)
1227 GtkMenuAttachData *data;
1229 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1231 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1233 return data->attach_widget;
1238 gtk_menu_detach (GtkMenu *menu)
1240 GtkMenuAttachData *data;
1243 g_return_if_fail (GTK_IS_MENU (menu));
1245 /* keep this function in sync with gtk_widget_unparent()
1247 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1250 g_warning ("gtk_menu_detach(): menu is not attached");
1253 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1255 g_signal_handlers_disconnect_by_func (data->attach_widget,
1256 (gpointer) attach_widget_screen_changed,
1260 data->detacher (data->attach_widget, menu);
1261 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1262 list = g_list_remove (list, menu);
1264 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1265 (GDestroyNotify) g_list_free);
1267 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1269 if (GTK_WIDGET_REALIZED (menu))
1270 gtk_widget_unrealize (GTK_WIDGET (menu));
1272 g_slice_free (GtkMenuAttachData, data);
1274 /* Fallback title for menu comes from attach widget */
1275 gtk_menu_update_title (menu);
1277 g_object_unref (menu);
1281 gtk_menu_remove (GtkContainer *container,
1284 GtkMenu *menu = GTK_MENU (container);
1286 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1288 /* Clear out old_active_menu_item if it matches the item we are removing
1290 if (menu->old_active_menu_item == widget)
1292 g_object_unref (menu->old_active_menu_item);
1293 menu->old_active_menu_item = NULL;
1296 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1297 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1299 menu_queue_resize (menu);
1305 return g_object_new (GTK_TYPE_MENU, NULL);
1309 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1313 GtkMenu *menu = GTK_MENU (menu_shell);
1314 AttachInfo *ai = get_attach_info (child);
1316 ai->left_attach = -1;
1317 ai->right_attach = -1;
1318 ai->top_attach = -1;
1319 ai->bottom_attach = -1;
1321 if (GTK_WIDGET_REALIZED (menu_shell))
1322 gtk_widget_set_parent_window (child, menu->bin_window);
1324 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1326 menu_queue_resize (menu);
1330 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1335 widget = GTK_WIDGET (menu);
1341 GdkGCValues gc_values;
1343 menu->tearoff_active = FALSE;
1344 menu->saved_scroll_offset = menu->scroll_offset;
1346 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1347 gc = gdk_gc_new_with_values (widget->window,
1348 &gc_values, GDK_GC_SUBWINDOW);
1350 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1352 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1357 gdk_draw_drawable (pixmap, gc,
1358 menu->tearoff_window->window,
1359 0, 0, 0, 0, -1, -1);
1360 g_object_unref (gc);
1362 gtk_widget_set_size_request (menu->tearoff_window,
1366 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1367 g_object_unref (pixmap);
1372 popup_grab_on_window (GdkWindow *window,
1373 guint32 activate_time,
1374 gboolean grab_keyboard)
1376 if ((gdk_pointer_grab (window, TRUE,
1377 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1378 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1379 GDK_POINTER_MOTION_MASK,
1380 NULL, NULL, activate_time) == 0))
1382 if (!grab_keyboard ||
1383 gdk_keyboard_grab (window, TRUE,
1384 activate_time) == 0)
1388 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1399 * @menu: a #GtkMenu.
1400 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1401 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1402 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1403 * @data: (allow-none): user supplied data to be passed to @func.
1404 * @button: the mouse button which was pressed to initiate the event.
1405 * @activate_time: the time at which the activation event occurred.
1407 * Displays a menu and makes it available for selection. Applications can use
1408 * this function to display context-sensitive menus, and will typically supply
1409 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1410 * parameters. The default menu positioning function will position the menu
1411 * at the current mouse cursor position.
1413 * The @button parameter should be the mouse button pressed to initiate
1414 * the menu popup. If the menu popup was initiated by something other than
1415 * a mouse button press, such as a mouse button release or a keypress,
1416 * @button should be 0.
1418 * The @activate_time parameter is used to conflict-resolve initiation of
1419 * concurrent requests for mouse/keyboard grab requests. To function
1420 * properly, this needs to be the time stamp of the user event (such as
1421 * a mouse click or key press) that caused the initiation of the popup.
1422 * Only if no such event is available, gtk_get_current_event_time() can
1426 gtk_menu_popup (GtkMenu *menu,
1427 GtkWidget *parent_menu_shell,
1428 GtkWidget *parent_menu_item,
1429 GtkMenuPositionFunc func,
1432 guint32 activate_time)
1435 GtkWidget *xgrab_shell;
1437 GdkEvent *current_event;
1438 GtkMenuShell *menu_shell;
1439 gboolean grab_keyboard;
1440 GtkMenuPrivate *priv;
1441 GtkWidget *parent_toplevel;
1443 g_return_if_fail (GTK_IS_MENU (menu));
1445 widget = GTK_WIDGET (menu);
1446 menu_shell = GTK_MENU_SHELL (menu);
1447 priv = gtk_menu_get_private (menu);
1449 menu_shell->parent_menu_shell = parent_menu_shell;
1451 priv->seen_item_enter = FALSE;
1453 /* Find the last viewable ancestor, and make an X grab on it
1455 parent = GTK_WIDGET (menu);
1459 gboolean viewable = TRUE;
1460 GtkWidget *tmp = parent;
1464 if (!gtk_widget_get_mapped (tmp))
1473 xgrab_shell = parent;
1475 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1478 /* We want to receive events generated when we map the menu; unfortunately,
1479 * since there is probably already an implicit grab in place from the
1480 * button that the user used to pop up the menu, we won't receive then --
1481 * in particular, the EnterNotify when the menu pops up under the pointer.
1483 * If we are grabbing on a parent menu shell, no problem; just grab on
1484 * that menu shell first before popping up the window with owner_events = TRUE.
1486 * When grabbing on the menu itself, things get more convuluted - we
1487 * we do an explicit grab on a specially created window with
1488 * owner_events = TRUE, which we override further down with a grab
1489 * on the menu. (We can't grab on the menu until it is mapped; we
1490 * probably could just leave the grab on the other window, with a
1491 * little reorganization of the code in gtkmenu*).
1493 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1494 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1496 if (xgrab_shell && xgrab_shell != widget)
1498 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1499 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1503 GdkWindow *transfer_window;
1505 xgrab_shell = widget;
1506 transfer_window = menu_grab_transfer_window_get (menu);
1507 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1508 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1511 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1513 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1514 * with a stuck up window, we just abort here. Presumably the user will
1517 menu_shell->parent_menu_shell = NULL;
1518 menu_grab_transfer_window_destroy (menu);
1522 menu_shell->active = TRUE;
1523 menu_shell->button = button;
1525 /* If we are popping up the menu from something other than, a button
1526 * press then, as a heuristic, we ignore enter events for the menu
1527 * until we get a MOTION_NOTIFY.
1530 current_event = gtk_get_current_event ();
1533 if ((current_event->type != GDK_BUTTON_PRESS) &&
1534 (current_event->type != GDK_ENTER_NOTIFY))
1535 menu_shell->ignore_enter = TRUE;
1537 gdk_event_free (current_event);
1540 menu_shell->ignore_enter = TRUE;
1544 gtk_menu_tearoff_bg_copy (menu);
1546 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1549 parent_toplevel = NULL;
1550 if (parent_menu_shell)
1551 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1552 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1554 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1556 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1559 /* Set transient for to get the right window group and parent relationship */
1560 if (GTK_IS_WINDOW (parent_toplevel))
1561 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1562 GTK_WINDOW (parent_toplevel));
1564 menu->parent_menu_item = parent_menu_item;
1565 menu->position_func = func;
1566 menu->position_func_data = data;
1567 menu_shell->activate_time = activate_time;
1569 /* We need to show the menu here rather in the init function because
1570 * code expects to be able to tell if the menu is onscreen by
1571 * looking at the gtk_widget_get_visible (menu)
1573 gtk_widget_show (GTK_WIDGET (menu));
1575 /* Position the menu, possibly changing the size request
1577 gtk_menu_position (menu);
1579 /* Compute the size of the toplevel and realize it so we
1580 * can scroll correctly.
1583 GtkRequisition tmp_request;
1584 GtkAllocation tmp_allocation = { 0, };
1586 gtk_widget_size_request (menu->toplevel, &tmp_request);
1588 tmp_allocation.width = tmp_request.width;
1589 tmp_allocation.height = tmp_request.height;
1591 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1593 gtk_widget_realize (GTK_WIDGET (menu));
1596 gtk_menu_scroll_to (menu, menu->scroll_offset);
1598 /* if no item is selected, select the first one */
1599 if (!menu_shell->active_menu_item)
1601 gboolean touchscreen_mode;
1603 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1604 "gtk-touchscreen-mode", &touchscreen_mode,
1607 if (touchscreen_mode)
1608 gtk_menu_shell_select_first (menu_shell, TRUE);
1611 /* Once everything is set up correctly, map the toplevel window on
1614 gtk_widget_show (menu->toplevel);
1616 if (xgrab_shell == widget)
1617 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1618 gtk_grab_add (GTK_WIDGET (menu));
1620 if (parent_menu_shell)
1622 gboolean keyboard_mode;
1624 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1625 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1627 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1628 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1630 _gtk_menu_shell_update_mnemonics (menu_shell);
1634 gtk_menu_popdown (GtkMenu *menu)
1636 GtkMenuPrivate *private;
1637 GtkMenuShell *menu_shell;
1639 g_return_if_fail (GTK_IS_MENU (menu));
1641 menu_shell = GTK_MENU_SHELL (menu);
1642 private = gtk_menu_get_private (menu);
1644 menu_shell->parent_menu_shell = NULL;
1645 menu_shell->active = FALSE;
1646 menu_shell->ignore_enter = FALSE;
1648 private->have_position = FALSE;
1650 gtk_menu_stop_scrolling (menu);
1652 gtk_menu_stop_navigating_submenu (menu);
1654 if (menu_shell->active_menu_item)
1656 if (menu->old_active_menu_item)
1657 g_object_unref (menu->old_active_menu_item);
1658 menu->old_active_menu_item = menu_shell->active_menu_item;
1659 g_object_ref (menu->old_active_menu_item);
1662 gtk_menu_shell_deselect (menu_shell);
1664 /* The X Grab, if present, will automatically be removed when we hide
1666 gtk_widget_hide (menu->toplevel);
1667 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1671 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1673 if (GTK_BIN (menu->toplevel)->child)
1675 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1679 /* We popped up the menu from the tearoff, so we need to
1680 * release the grab - we aren't actually hiding the menu.
1682 if (menu_shell->have_xgrab)
1684 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1686 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1687 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1691 /* gtk_menu_popdown is called each time a menu item is selected from
1692 * a torn off menu. Only scroll back to the saved position if the
1693 * non-tearoff menu was popped down.
1695 if (!menu->tearoff_active)
1696 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1697 menu->tearoff_active = TRUE;
1700 gtk_widget_hide (GTK_WIDGET (menu));
1702 menu_shell->have_xgrab = FALSE;
1703 gtk_grab_remove (GTK_WIDGET (menu));
1705 menu_grab_transfer_window_destroy (menu);
1709 gtk_menu_get_active (GtkMenu *menu)
1714 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1716 if (!menu->old_active_menu_item)
1719 children = GTK_MENU_SHELL (menu)->children;
1723 child = children->data;
1724 children = children->next;
1726 if (GTK_BIN (child)->child)
1731 menu->old_active_menu_item = child;
1732 if (menu->old_active_menu_item)
1733 g_object_ref (menu->old_active_menu_item);
1736 return menu->old_active_menu_item;
1740 gtk_menu_set_active (GtkMenu *menu,
1746 g_return_if_fail (GTK_IS_MENU (menu));
1748 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1751 child = tmp_list->data;
1752 if (GTK_BIN (child)->child)
1754 if (menu->old_active_menu_item)
1755 g_object_unref (menu->old_active_menu_item);
1756 menu->old_active_menu_item = child;
1757 g_object_ref (menu->old_active_menu_item);
1764 * gtk_menu_set_accel_group:
1765 * @accel_group: (allow-none):
1768 gtk_menu_set_accel_group (GtkMenu *menu,
1769 GtkAccelGroup *accel_group)
1771 g_return_if_fail (GTK_IS_MENU (menu));
1773 if (menu->accel_group != accel_group)
1775 if (menu->accel_group)
1776 g_object_unref (menu->accel_group);
1777 menu->accel_group = accel_group;
1778 if (menu->accel_group)
1779 g_object_ref (menu->accel_group);
1780 _gtk_menu_refresh_accel_paths (menu, TRUE);
1785 gtk_menu_get_accel_group (GtkMenu *menu)
1787 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1789 return menu->accel_group;
1793 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1796 /* Menu items chain here to figure whether they can activate their
1797 * accelerators. Unlike ordinary widgets, menus allow accel
1798 * activation even if invisible since that's the usual case for
1799 * submenus/popup-menus. however, the state of the attach widget
1800 * affects the "activeness" of the menu.
1802 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1805 return gtk_widget_can_activate_accel (awidget, signal_id);
1807 return gtk_widget_is_sensitive (widget);
1811 * gtk_menu_set_accel_path
1812 * @menu: a valid #GtkMenu
1813 * @accel_path: (allow-none): a valid accelerator path
1815 * Sets an accelerator path for this menu from which accelerator paths
1816 * for its immediate children, its menu items, can be constructed.
1817 * The main purpose of this function is to spare the programmer the
1818 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1819 * each menu item that should support runtime user changable accelerators.
1820 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1821 * each menu item of this menu, that contains a label describing its purpose,
1822 * automatically gets an accel path assigned. For example, a menu containing
1823 * menu items "New" and "Exit", will, after
1824 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1825 * has been called, assign its items the accel paths:
1826 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1827 * Assigning accel paths to menu items then enables the user to change
1828 * their accelerators at runtime. More details about accelerator paths
1829 * and their default setups can be found at gtk_accel_map_add_entry().
1831 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1832 * pass a static string, you can save some memory by interning it first with
1833 * g_intern_static_string().
1836 gtk_menu_set_accel_path (GtkMenu *menu,
1837 const gchar *accel_path)
1839 g_return_if_fail (GTK_IS_MENU (menu));
1841 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1843 /* FIXME: accel_path should be defined as const gchar* */
1844 menu->accel_path = (gchar*)g_intern_string (accel_path);
1845 if (menu->accel_path)
1846 _gtk_menu_refresh_accel_paths (menu, FALSE);
1850 * gtk_menu_get_accel_path
1851 * @menu: a valid #GtkMenu
1853 * Retrieves the accelerator path set on the menu.
1855 * Returns: the accelerator path set on the menu.
1860 gtk_menu_get_accel_path (GtkMenu *menu)
1862 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1864 return menu->accel_path;
1869 gboolean group_changed;
1873 refresh_accel_paths_foreach (GtkWidget *widget,
1876 AccelPropagation *prop = data;
1878 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1879 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1880 prop->menu->accel_path,
1881 prop->menu->accel_group,
1882 prop->group_changed);
1886 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1887 gboolean group_changed)
1889 g_return_if_fail (GTK_IS_MENU (menu));
1891 if (menu->accel_path && menu->accel_group)
1893 AccelPropagation prop;
1896 prop.group_changed = group_changed;
1897 gtk_container_foreach (GTK_CONTAINER (menu),
1898 refresh_accel_paths_foreach,
1904 gtk_menu_reposition (GtkMenu *menu)
1906 g_return_if_fail (GTK_IS_MENU (menu));
1908 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
1909 gtk_menu_position (menu);
1913 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1916 g_return_if_fail (GTK_IS_MENU (menu));
1918 if (adjustment->value != menu->scroll_offset)
1919 gtk_menu_scroll_to (menu, adjustment->value);
1923 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1926 GdkGeometry geometry_hints;
1928 if (!menu->tearoff_window)
1931 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
1933 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1934 width += menu->tearoff_scrollbar->requisition.width;
1937 geometry_hints.min_width = width;
1938 geometry_hints.max_width = width;
1940 geometry_hints.min_height = 0;
1941 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1943 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1946 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1950 gtk_menu_update_title (GtkMenu *menu)
1952 if (menu->tearoff_window)
1955 GtkWidget *attach_widget;
1957 title = gtk_menu_get_title (menu);
1960 attach_widget = gtk_menu_get_attach_widget (menu);
1961 if (GTK_IS_MENU_ITEM (attach_widget))
1963 GtkWidget *child = GTK_BIN (attach_widget)->child;
1964 if (GTK_IS_LABEL (child))
1965 title = gtk_label_get_text (GTK_LABEL (child));
1970 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1975 gtk_menu_get_toplevel (GtkWidget *menu)
1977 GtkWidget *attach, *toplevel;
1979 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1981 if (GTK_IS_MENU_ITEM (attach))
1982 attach = attach->parent;
1984 if (GTK_IS_MENU (attach))
1985 return gtk_menu_get_toplevel (attach);
1986 else if (GTK_IS_WIDGET (attach))
1988 toplevel = gtk_widget_get_toplevel (attach);
1989 if (gtk_widget_is_toplevel (toplevel))
1997 tearoff_window_destroyed (GtkWidget *widget,
2000 gtk_menu_set_tearoff_state (menu, FALSE);
2004 gtk_menu_set_tearoff_state (GtkMenu *menu,
2009 g_return_if_fail (GTK_IS_MENU (menu));
2011 if (menu->torn_off != torn_off)
2013 menu->torn_off = torn_off;
2014 menu->tearoff_active = torn_off;
2018 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2019 gtk_menu_popdown (menu);
2021 if (!menu->tearoff_window)
2023 GtkWidget *toplevel;
2025 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2026 "type", GTK_WINDOW_TOPLEVEL,
2027 "screen", gtk_widget_get_screen (menu->toplevel),
2028 "app-paintable", TRUE,
2031 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2032 GDK_WINDOW_TYPE_HINT_MENU);
2033 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2034 g_signal_connect (menu->tearoff_window, "destroy",
2035 G_CALLBACK (tearoff_window_destroyed), menu);
2036 g_signal_connect (menu->tearoff_window, "event",
2037 G_CALLBACK (gtk_menu_window_event), menu);
2039 gtk_menu_update_title (menu);
2041 gtk_widget_realize (menu->tearoff_window);
2043 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2044 if (toplevel != NULL)
2045 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2046 GTK_WINDOW (toplevel));
2048 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2049 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2051 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2052 menu->tearoff_adjustment =
2053 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2055 GTK_WIDGET (menu)->requisition.height,
2059 g_object_connect (menu->tearoff_adjustment,
2060 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2062 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2064 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2065 menu->tearoff_scrollbar,
2068 if (menu->tearoff_adjustment->upper > height)
2069 gtk_widget_show (menu->tearoff_scrollbar);
2071 gtk_widget_show (menu->tearoff_hbox);
2074 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2076 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
2078 /* Update menu->requisition
2080 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2082 gtk_menu_set_tearoff_hints (menu, width);
2084 gtk_widget_realize (menu->tearoff_window);
2085 gtk_menu_position (menu);
2087 gtk_widget_show (GTK_WIDGET (menu));
2088 gtk_widget_show (menu->tearoff_window);
2090 gtk_menu_scroll_to (menu, 0);
2095 gtk_widget_hide (GTK_WIDGET (menu));
2096 gtk_widget_hide (menu->tearoff_window);
2097 if (GTK_IS_CONTAINER (menu->toplevel))
2098 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2099 gtk_widget_destroy (menu->tearoff_window);
2101 menu->tearoff_window = NULL;
2102 menu->tearoff_hbox = NULL;
2103 menu->tearoff_scrollbar = NULL;
2104 menu->tearoff_adjustment = NULL;
2107 g_object_notify (G_OBJECT (menu), "tearoff-state");
2112 * gtk_menu_get_tearoff_state:
2115 * Returns whether the menu is torn off. See
2116 * gtk_menu_set_tearoff_state ().
2118 * Return value: %TRUE if the menu is currently torn off.
2121 gtk_menu_get_tearoff_state (GtkMenu *menu)
2123 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2125 return menu->torn_off;
2129 * gtk_menu_set_title:
2131 * @title: a string containing the title for the menu.
2133 * Sets the title string for the menu. The title is displayed when the menu
2134 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2135 * attached to a parent menu item, and if so it will try to use the same text as
2136 * that menu item's label.
2139 gtk_menu_set_title (GtkMenu *menu,
2142 GtkMenuPrivate *priv;
2145 g_return_if_fail (GTK_IS_MENU (menu));
2147 priv = gtk_menu_get_private (menu);
2149 old_title = priv->title;
2150 priv->title = g_strdup (title);
2153 gtk_menu_update_title (menu);
2154 g_object_notify (G_OBJECT (menu), "tearoff-title");
2158 * gtk_menu_get_title:
2161 * Returns the title of the menu. See gtk_menu_set_title().
2163 * Return value: the title of the menu, or %NULL if the menu has no
2164 * title set on it. This string is owned by the widget and should
2165 * not be modified or freed.
2167 G_CONST_RETURN gchar *
2168 gtk_menu_get_title (GtkMenu *menu)
2170 GtkMenuPrivate *priv;
2172 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2174 priv = gtk_menu_get_private (menu);
2180 gtk_menu_reorder_child (GtkMenu *menu,
2184 GtkMenuShell *menu_shell;
2186 g_return_if_fail (GTK_IS_MENU (menu));
2187 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2189 menu_shell = GTK_MENU_SHELL (menu);
2191 if (g_list_find (menu_shell->children, child))
2193 menu_shell->children = g_list_remove (menu_shell->children, child);
2194 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2196 menu_queue_resize (menu);
2201 gtk_menu_style_set (GtkWidget *widget,
2202 GtkStyle *previous_style)
2204 if (GTK_WIDGET_REALIZED (widget))
2206 GtkMenu *menu = GTK_MENU (widget);
2208 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2209 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2210 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2215 get_arrows_border (GtkMenu *menu,
2218 guint scroll_arrow_height;
2219 GtkArrowPlacement arrow_placement;
2221 gtk_widget_style_get (GTK_WIDGET (menu),
2222 "scroll-arrow-vlength", &scroll_arrow_height,
2223 "arrow_placement", &arrow_placement,
2226 switch (arrow_placement)
2228 case GTK_ARROWS_BOTH:
2229 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2230 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2233 case GTK_ARROWS_START:
2234 border->top = (menu->upper_arrow_visible ||
2235 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2239 case GTK_ARROWS_END:
2241 border->bottom = (menu->upper_arrow_visible ||
2242 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2246 border->left = border->right = 0;
2250 gtk_menu_realize (GtkWidget *widget)
2252 GdkWindowAttr attributes;
2253 gint attributes_mask;
2258 guint vertical_padding;
2259 guint horizontal_padding;
2260 GtkBorder arrow_border;
2262 g_return_if_fail (GTK_IS_MENU (widget));
2264 menu = GTK_MENU (widget);
2266 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2268 attributes.window_type = GDK_WINDOW_CHILD;
2269 attributes.x = widget->allocation.x;
2270 attributes.y = widget->allocation.y;
2271 attributes.width = widget->allocation.width;
2272 attributes.height = widget->allocation.height;
2273 attributes.wclass = GDK_INPUT_OUTPUT;
2274 attributes.visual = gtk_widget_get_visual (widget);
2275 attributes.colormap = gtk_widget_get_colormap (widget);
2277 attributes.event_mask = gtk_widget_get_events (widget);
2279 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2280 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2282 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2283 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2284 gdk_window_set_user_data (widget->window, widget);
2286 border_width = GTK_CONTAINER (widget)->border_width;
2288 gtk_widget_style_get (GTK_WIDGET (menu),
2289 "vertical-padding", &vertical_padding,
2290 "horizontal-padding", &horizontal_padding,
2293 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2294 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2295 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2296 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2298 get_arrows_border (menu, &arrow_border);
2299 attributes.y += arrow_border.top;
2300 attributes.height -= arrow_border.top;
2301 attributes.height -= arrow_border.bottom;
2303 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2304 gdk_window_set_user_data (menu->view_window, menu);
2308 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2309 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2311 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2312 gdk_window_set_user_data (menu->bin_window, menu);
2314 children = GTK_MENU_SHELL (menu)->children;
2317 child = children->data;
2318 children = children->next;
2320 gtk_widget_set_parent_window (child, menu->bin_window);
2323 widget->style = gtk_style_attach (widget->style, widget->window);
2324 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2325 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2326 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2328 if (GTK_MENU_SHELL (widget)->active_menu_item)
2329 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2330 GTK_MENU_SHELL (widget)->active_menu_item);
2332 gdk_window_show (menu->bin_window);
2333 gdk_window_show (menu->view_window);
2337 gtk_menu_focus (GtkWidget *widget,
2338 GtkDirectionType direction)
2341 * A menu or its menu items cannot have focus
2346 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2349 menu_grab_transfer_window_get (GtkMenu *menu)
2351 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2354 GdkWindowAttr attributes;
2355 gint attributes_mask;
2357 attributes.x = -100;
2358 attributes.y = -100;
2359 attributes.width = 10;
2360 attributes.height = 10;
2361 attributes.window_type = GDK_WINDOW_TEMP;
2362 attributes.wclass = GDK_INPUT_ONLY;
2363 attributes.override_redirect = TRUE;
2364 attributes.event_mask = 0;
2366 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2368 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2369 &attributes, attributes_mask);
2370 gdk_window_set_user_data (window, menu);
2372 gdk_window_show (window);
2374 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2381 menu_grab_transfer_window_destroy (GtkMenu *menu)
2383 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2386 gdk_window_set_user_data (window, NULL);
2387 gdk_window_destroy (window);
2388 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2393 gtk_menu_unrealize (GtkWidget *widget)
2395 GtkMenu *menu = GTK_MENU (widget);
2397 menu_grab_transfer_window_destroy (menu);
2399 gdk_window_set_user_data (menu->view_window, NULL);
2400 gdk_window_destroy (menu->view_window);
2401 menu->view_window = NULL;
2403 gdk_window_set_user_data (menu->bin_window, NULL);
2404 gdk_window_destroy (menu->bin_window);
2405 menu->bin_window = NULL;
2407 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2411 gtk_menu_size_request (GtkWidget *widget,
2412 GtkRequisition *requisition)
2416 GtkMenuShell *menu_shell;
2419 guint max_toggle_size;
2420 guint max_accel_width;
2421 guint vertical_padding;
2422 guint horizontal_padding;
2423 GtkRequisition child_requisition;
2424 GtkMenuPrivate *priv;
2426 g_return_if_fail (GTK_IS_MENU (widget));
2427 g_return_if_fail (requisition != NULL);
2429 menu = GTK_MENU (widget);
2430 menu_shell = GTK_MENU_SHELL (widget);
2431 priv = gtk_menu_get_private (menu);
2433 requisition->width = 0;
2434 requisition->height = 0;
2436 max_toggle_size = 0;
2437 max_accel_width = 0;
2439 g_free (priv->heights);
2440 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2441 priv->heights_length = gtk_menu_get_n_rows (menu);
2443 children = menu_shell->children;
2450 child = children->data;
2451 children = children->next;
2453 if (! gtk_widget_get_visible (child))
2456 get_effective_child_attach (child, &l, &r, &t, &b);
2458 /* It's important to size_request the child
2459 * before doing the toggle size request, in
2460 * case the toggle size request depends on the size
2461 * request of a child of the child (e.g. for ImageMenuItem)
2464 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2465 gtk_widget_size_request (child, &child_requisition);
2467 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2468 max_toggle_size = MAX (max_toggle_size, toggle_size);
2469 max_accel_width = MAX (max_accel_width,
2470 GTK_MENU_ITEM (child)->accelerator_width);
2472 part = child_requisition.width / (r - l);
2473 requisition->width = MAX (requisition->width, part);
2475 part = MAX (child_requisition.height, toggle_size) / (b - t);
2476 priv->heights[t] = MAX (priv->heights[t], part);
2479 /* If the menu doesn't include any images or check items
2480 * reserve the space so that all menus are consistent.
2481 * We only do this for 'ordinary' menus, not for combobox
2482 * menus or multi-column menus
2484 if (max_toggle_size == 0 &&
2485 gtk_menu_get_n_columns (menu) == 1 &&
2486 !priv->no_toggle_size)
2488 guint toggle_spacing;
2489 guint indicator_size;
2491 gtk_style_get (widget->style,
2492 GTK_TYPE_CHECK_MENU_ITEM,
2493 "toggle-spacing", &toggle_spacing,
2494 "indicator-size", &indicator_size,
2497 max_toggle_size = indicator_size + toggle_spacing;
2500 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2501 requisition->height += priv->heights[i];
2503 requisition->width += 2 * max_toggle_size + max_accel_width;
2504 requisition->width *= gtk_menu_get_n_columns (menu);
2506 gtk_widget_style_get (GTK_WIDGET (menu),
2507 "vertical-padding", &vertical_padding,
2508 "horizontal-padding", &horizontal_padding,
2511 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2512 widget->style->xthickness) * 2;
2513 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2514 widget->style->ythickness) * 2;
2516 menu->toggle_size = max_toggle_size;
2518 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2520 if (menu->tearoff_active)
2521 gtk_menu_set_tearoff_hints (menu, requisition->width);
2525 gtk_menu_size_allocate (GtkWidget *widget,
2526 GtkAllocation *allocation)
2529 GtkMenuShell *menu_shell;
2531 GtkAllocation child_allocation;
2532 GtkRequisition child_requisition;
2533 GtkMenuPrivate *priv;
2537 guint vertical_padding;
2538 guint horizontal_padding;
2540 g_return_if_fail (GTK_IS_MENU (widget));
2541 g_return_if_fail (allocation != NULL);
2543 menu = GTK_MENU (widget);
2544 menu_shell = GTK_MENU_SHELL (widget);
2545 priv = gtk_menu_get_private (menu);
2547 widget->allocation = *allocation;
2548 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2550 gtk_widget_style_get (GTK_WIDGET (menu),
2551 "vertical-padding", &vertical_padding,
2552 "horizontal-padding", &horizontal_padding,
2555 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2556 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2558 width = MAX (1, allocation->width - x * 2);
2559 height = MAX (1, allocation->height - y * 2);
2561 child_requisition.width -= x * 2;
2562 child_requisition.height -= y * 2;
2564 if (menu_shell->active)
2565 gtk_menu_scroll_to (menu, menu->scroll_offset);
2567 if (!menu->tearoff_active)
2569 GtkBorder arrow_border;
2571 get_arrows_border (menu, &arrow_border);
2572 y += arrow_border.top;
2573 height -= arrow_border.top;
2574 height -= arrow_border.bottom;
2577 if (GTK_WIDGET_REALIZED (widget))
2579 gdk_window_move_resize (widget->window,
2580 allocation->x, allocation->y,
2581 allocation->width, allocation->height);
2583 gdk_window_move_resize (menu->view_window,
2590 if (menu_shell->children)
2592 gint base_width = width / gtk_menu_get_n_columns (menu);
2594 children = menu_shell->children;
2597 child = children->data;
2598 children = children->next;
2600 if (gtk_widget_get_visible (child))
2605 get_effective_child_attach (child, &l, &r, &t, &b);
2607 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2610 tmp = gtk_menu_get_n_columns (menu) - l;
2611 l = gtk_menu_get_n_columns (menu) - r;
2615 child_allocation.width = (r - l) * base_width;
2616 child_allocation.height = 0;
2617 child_allocation.x = l * base_width;
2618 child_allocation.y = 0;
2620 for (i = 0; i < b; i++)
2623 child_allocation.y += priv->heights[i];
2625 child_allocation.height += priv->heights[i];
2628 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2631 gtk_widget_size_allocate (child, &child_allocation);
2632 gtk_widget_queue_draw (child);
2636 /* Resize the item window */
2637 if (GTK_WIDGET_REALIZED (widget))
2643 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2644 height += priv->heights[i];
2646 width = gtk_menu_get_n_columns (menu) * base_width;
2647 gdk_window_resize (menu->bin_window, width, height);
2650 if (menu->tearoff_active)
2652 if (allocation->height >= widget->requisition.height)
2654 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2656 gtk_widget_hide (menu->tearoff_scrollbar);
2657 gtk_menu_set_tearoff_hints (menu, allocation->width);
2659 gtk_menu_scroll_to (menu, 0);
2664 menu->tearoff_adjustment->upper = widget->requisition.height;
2665 menu->tearoff_adjustment->page_size = allocation->height;
2667 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2668 menu->tearoff_adjustment->upper)
2671 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2674 gtk_menu_scroll_to (menu, value);
2677 gtk_adjustment_changed (menu->tearoff_adjustment);
2679 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2681 gtk_widget_show (menu->tearoff_scrollbar);
2682 gtk_menu_set_tearoff_hints (menu, allocation->width);
2690 get_arrows_visible_area (GtkMenu *menu,
2691 GdkRectangle *border,
2692 GdkRectangle *upper,
2693 GdkRectangle *lower,
2696 GtkWidget *widget = GTK_WIDGET (menu);
2697 guint vertical_padding;
2698 guint horizontal_padding;
2699 gint scroll_arrow_height;
2700 GtkArrowPlacement arrow_placement;
2702 gtk_widget_style_get (widget,
2703 "vertical-padding", &vertical_padding,
2704 "horizontal-padding", &horizontal_padding,
2705 "scroll-arrow-vlength", &scroll_arrow_height,
2706 "arrow-placement", &arrow_placement,
2709 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2710 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2711 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2713 switch (arrow_placement)
2715 case GTK_ARROWS_BOTH:
2716 upper->x = border->x;
2717 upper->y = border->y;
2718 upper->width = border->width - 2 * border->x;
2719 upper->height = scroll_arrow_height;
2721 lower->x = border->x;
2722 lower->y = border->height - border->y - scroll_arrow_height;
2723 lower->width = border->width - 2 * border->x;
2724 lower->height = scroll_arrow_height;
2727 case GTK_ARROWS_START:
2728 upper->x = border->x;
2729 upper->y = border->y;
2730 upper->width = (border->width - 2 * border->x) / 2;
2731 upper->height = scroll_arrow_height;
2733 lower->x = border->x + upper->width;
2734 lower->y = border->y;
2735 lower->width = (border->width - 2 * border->x) / 2;
2736 lower->height = scroll_arrow_height;
2739 case GTK_ARROWS_END:
2740 upper->x = border->x;
2741 upper->y = border->height - border->y - scroll_arrow_height;
2742 upper->width = (border->width - 2 * border->x) / 2;
2743 upper->height = scroll_arrow_height;
2745 lower->x = border->x + upper->width;
2746 lower->y = border->height - border->y - scroll_arrow_height;
2747 lower->width = (border->width - 2 * border->x) / 2;
2748 lower->height = scroll_arrow_height;
2752 g_assert_not_reached();
2753 upper->x = upper->y = upper->width = upper->height = 0;
2754 lower->x = lower->y = lower->width = lower->height = 0;
2757 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2761 gtk_menu_paint (GtkWidget *widget,
2762 GdkEventExpose *event)
2765 GtkMenuPrivate *priv;
2766 GdkRectangle border;
2771 g_return_if_fail (GTK_IS_MENU (widget));
2773 menu = GTK_MENU (widget);
2774 priv = gtk_menu_get_private (menu);
2776 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2778 if (event->window == widget->window)
2780 gfloat arrow_scaling;
2783 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2784 arrow_size = arrow_scaling * arrow_space;
2786 gtk_paint_box (widget->style,
2790 &event->area, widget, "menu",
2793 if (menu->upper_arrow_visible && !menu->tearoff_active)
2795 gtk_paint_box (widget->style,
2797 priv->upper_arrow_state,
2799 &event->area, widget, "menu_scroll_arrow_up",
2805 gtk_paint_arrow (widget->style,
2807 priv->upper_arrow_state,
2809 &event->area, widget, "menu_scroll_arrow_up",
2812 upper.x + (upper.width - arrow_size) / 2,
2813 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2814 arrow_size, arrow_size);
2817 if (menu->lower_arrow_visible && !menu->tearoff_active)
2819 gtk_paint_box (widget->style,
2821 priv->lower_arrow_state,
2823 &event->area, widget, "menu_scroll_arrow_down",
2829 gtk_paint_arrow (widget->style,
2831 priv->lower_arrow_state,
2833 &event->area, widget, "menu_scroll_arrow_down",
2836 lower.x + (lower.width - arrow_size) / 2,
2837 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2838 arrow_size, arrow_size);
2841 else if (event->window == menu->bin_window)
2843 gint y = -border.y + menu->scroll_offset;
2845 if (!menu->tearoff_active)
2847 GtkBorder arrow_border;
2849 get_arrows_border (menu, &arrow_border);
2850 y -= arrow_border.top;
2853 gtk_paint_box (widget->style,
2857 &event->area, widget, "menu",
2859 border.width, border.height);
2864 gtk_menu_expose (GtkWidget *widget,
2865 GdkEventExpose *event)
2867 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2868 g_return_val_if_fail (event != NULL, FALSE);
2870 if (gtk_widget_is_drawable (widget))
2872 gtk_menu_paint (widget, event);
2874 GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
2881 gtk_menu_show (GtkWidget *widget)
2883 GtkMenu *menu = GTK_MENU (widget);
2885 _gtk_menu_refresh_accel_paths (menu, FALSE);
2887 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2891 gtk_menu_button_scroll (GtkMenu *menu,
2892 GdkEventButton *event)
2894 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2896 gboolean touchscreen_mode;
2898 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
2899 "gtk-touchscreen-mode", &touchscreen_mode,
2902 if (touchscreen_mode)
2903 gtk_menu_handle_scrolling (menu,
2904 event->x_root, event->y_root,
2905 event->type == GDK_BUTTON_PRESS,
2915 pointer_in_menu_window (GtkWidget *widget,
2919 GtkMenu *menu = GTK_MENU (widget);
2921 if (gtk_widget_get_mapped (menu->toplevel))
2923 GtkMenuShell *menu_shell;
2924 gint window_x, window_y;
2926 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
2928 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
2929 y_root >= window_y && y_root < window_y + widget->allocation.height)
2932 menu_shell = GTK_MENU_SHELL (widget);
2934 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
2935 return pointer_in_menu_window (menu_shell->parent_menu_shell,
2943 gtk_menu_button_press (GtkWidget *widget,
2944 GdkEventButton *event)
2946 if (event->type != GDK_BUTTON_PRESS)
2949 /* Don't pass down to menu shell for presses over scroll arrows
2951 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2954 /* Don't pass down to menu shell if a non-menuitem part of the menu
2955 * was clicked. The check for the event_widget being a GtkMenuShell
2956 * works because we have the pointer grabbed on menu_shell->window
2957 * with owner_events=TRUE, so all events that are either outside
2958 * the menu or on its border are delivered relative to
2959 * menu_shell->window.
2961 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2962 pointer_in_menu_window (widget, event->x_root, event->y_root))
2965 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2969 gtk_menu_button_release (GtkWidget *widget,
2970 GdkEventButton *event)
2972 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2974 if (priv->ignore_button_release)
2976 priv->ignore_button_release = FALSE;
2980 if (event->type != GDK_BUTTON_RELEASE)
2983 /* Don't pass down to menu shell for releases over scroll arrows
2985 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2988 /* Don't pass down to menu shell if a non-menuitem part of the menu
2989 * was clicked (see comment in button_press()).
2991 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2992 pointer_in_menu_window (widget, event->x_root, event->y_root))
2994 /* Ugly: make sure menu_shell->button gets reset to 0 when we
2995 * bail out early here so it is in a consistent state for the
2996 * next button_press/button_release in GtkMenuShell.
2999 if (GTK_MENU_SHELL (widget)->active)
3000 GTK_MENU_SHELL (widget)->button = 0;
3005 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3008 static const gchar *
3009 get_accel_path (GtkWidget *menu_item,
3014 GClosure *accel_closure;
3015 GtkAccelGroup *accel_group;
3017 path = _gtk_widget_get_accel_path (menu_item, locked);
3020 path = GTK_MENU_ITEM (menu_item)->accel_path;
3026 label = GTK_BIN (menu_item)->child;
3028 if (GTK_IS_ACCEL_LABEL (label))
3030 g_object_get (label,
3031 "accel-closure", &accel_closure,
3035 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3037 *locked = accel_group->lock_count > 0;
3047 gtk_menu_key_press (GtkWidget *widget,
3050 GtkMenuShell *menu_shell;
3052 gboolean delete = FALSE;
3053 gboolean can_change_accels;
3054 gchar *accel = NULL;
3055 guint accel_key, accel_mods;
3056 GdkModifierType consumed_modifiers;
3057 GdkDisplay *display;
3059 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3060 g_return_val_if_fail (event != NULL, FALSE);
3062 menu_shell = GTK_MENU_SHELL (widget);
3063 menu = GTK_MENU (widget);
3065 gtk_menu_stop_navigating_submenu (menu);
3067 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3070 display = gtk_widget_get_display (widget);
3072 g_object_get (gtk_widget_get_settings (widget),
3073 "gtk-menu-bar-accel", &accel,
3074 "gtk-can-change-accels", &can_change_accels,
3077 if (accel && *accel)
3080 GdkModifierType mods = 0;
3082 gtk_accelerator_parse (accel, &keyval, &mods);
3085 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3087 /* FIXME this is wrong, needs to be in the global accel resolution
3088 * thing, to properly consider i18n etc., but that probably requires
3089 * AccelGroup changes etc.
3091 if (event->keyval == keyval && (mods & event->state) == mods)
3093 gtk_menu_shell_cancel (menu_shell);
3101 switch (event->keyval)
3112 /* Figure out what modifiers went into determining the key symbol */
3113 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3114 event->hardware_keycode, event->state, event->group,
3115 NULL, NULL, NULL, &consumed_modifiers);
3117 accel_key = gdk_keyval_to_lower (event->keyval);
3118 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3120 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3121 * We re-upper case when we match against the keyval, but display and save in caseless form.
3123 if (accel_key != event->keyval)
3124 accel_mods |= GDK_SHIFT_MASK;
3126 /* Modify the accelerators */
3127 if (can_change_accels &&
3128 menu_shell->active_menu_item &&
3129 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
3130 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3131 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3133 GtkWidget *menu_item = menu_shell->active_menu_item;
3134 gboolean locked, replace_accels = TRUE;
3137 path = get_accel_path (menu_item, &locked);
3138 if (!path || locked)
3140 /* can't change accelerators on menu_items without paths
3141 * (basically, those items are accelerator-locked).
3143 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3144 gtk_widget_error_bell (widget);
3150 /* For the keys that act to delete the current setting, we delete
3151 * the current setting if there is one, otherwise, we set the
3152 * key as the accelerator.
3158 if (gtk_accel_map_lookup_entry (path, &key) &&
3159 (key.accel_key || key.accel_mods))
3165 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3169 /* we failed, probably because this key is in use and
3172 /* g_print("failed to change\n"); */
3173 gtk_widget_error_bell (widget);
3182 check_threshold (GtkWidget *widget,
3191 ABS (start_x - x) > THRESHOLD ||
3192 ABS (start_y - y) > THRESHOLD;
3196 definitely_within_item (GtkWidget *widget,
3200 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3203 gdk_drawable_get_size (window, &w, &h);
3206 check_threshold (widget, 0, 0, x, y) &&
3207 check_threshold (widget, w - 1, 0, x, y) &&
3208 check_threshold (widget, w - 1, h - 1, x, y) &&
3209 check_threshold (widget, 0, h - 1, x, y);
3213 gtk_menu_motion_notify (GtkWidget *widget,
3214 GdkEventMotion *event)
3216 GtkWidget *menu_item;
3218 GtkMenuShell *menu_shell;
3220 gboolean need_enter;
3222 if (GTK_IS_MENU (widget))
3224 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3226 if (priv->ignore_button_release)
3227 priv->ignore_button_release = FALSE;
3229 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3233 /* We received the event for one of two reasons:
3235 * a) We are the active menu, and did gtk_grab_add()
3236 * b) The widget is a child of ours, and the event was propagated
3238 * Since for computation of navigation regions, we want the menu which
3239 * is the parent of the menu item, for a), we need to find that menu,
3240 * which may be different from 'widget'.
3242 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3243 if (!GTK_IS_MENU_ITEM (menu_item) ||
3244 !GTK_IS_MENU (menu_item->parent))
3247 menu_shell = GTK_MENU_SHELL (menu_item->parent);
3248 menu = GTK_MENU (menu_shell);
3250 if (definitely_within_item (menu_item, event->x, event->y))
3251 menu_shell->activate_time = 0;
3253 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
3255 /* Check to see if we are within an active submenu's navigation region
3257 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3260 /* Make sure we pop down if we enter a non-selectable menu item, so we
3261 * don't show a submenu when the cursor is outside the stay-up triangle.
3263 if (!_gtk_menu_item_is_selectable (menu_item))
3265 gtk_menu_shell_deselect (menu_shell);
3271 /* The menu is now sensitive to enter events on its items, but
3272 * was previously sensitive. So we fake an enter event.
3276 menu_shell->ignore_enter = FALSE;
3278 gdk_drawable_get_size (event->window, &width, &height);
3279 if (event->x >= 0 && event->x < width &&
3280 event->y >= 0 && event->y < height)
3282 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3285 send_event->crossing.window = g_object_ref (event->window);
3286 send_event->crossing.time = event->time;
3287 send_event->crossing.send_event = TRUE;
3288 send_event->crossing.x_root = event->x_root;
3289 send_event->crossing.y_root = event->y_root;
3290 send_event->crossing.x = event->x;
3291 send_event->crossing.y = event->y;
3292 send_event->crossing.state = event->state;
3294 /* We send the event to 'widget', the currently active menu,
3295 * instead of 'menu', the menu that the pointer is in. This
3296 * will ensure that the event will be ignored unless the
3297 * menuitem is a child of the active menu or some parent
3298 * menu of the active menu.
3300 result = gtk_widget_event (widget, send_event);
3301 gdk_event_free (send_event);
3311 get_double_arrows (GtkMenu *menu)
3313 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3314 gboolean double_arrows;
3315 GtkArrowPlacement arrow_placement;
3317 gtk_widget_style_get (GTK_WIDGET (menu),
3318 "double-arrows", &double_arrows,
3319 "arrow-placement", &arrow_placement,
3322 if (arrow_placement != GTK_ARROWS_BOTH)
3325 return double_arrows || (priv->initially_pushed_in &&
3326 menu->scroll_offset != 0);
3330 gtk_menu_scroll_by (GtkMenu *menu,
3335 gint view_width, view_height;
3336 gboolean double_arrows;
3337 GtkBorder arrow_border;
3339 widget = GTK_WIDGET (menu);
3340 offset = menu->scroll_offset + step;
3342 get_arrows_border (menu, &arrow_border);
3344 double_arrows = get_double_arrows (menu);
3346 /* If we scroll upward and the non-visible top part
3347 * is smaller than the scroll arrow it would be
3348 * pretty stupid to show the arrow and taking more
3349 * screen space than just scrolling to the top.
3352 if ((step < 0) && (offset < arrow_border.top))
3355 /* Don't scroll over the top if we weren't before: */
3356 if ((menu->scroll_offset >= 0) && (offset < 0))
3359 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3361 if (menu->scroll_offset == 0 &&
3362 view_height >= widget->requisition.height)
3365 /* Don't scroll past the bottom if we weren't before: */
3366 if (menu->scroll_offset > 0)
3367 view_height -= arrow_border.top;
3369 /* When both arrows are always shown, reduce
3370 * view height even more.
3373 view_height -= arrow_border.bottom;
3375 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3376 (offset + view_height > widget->requisition.height))
3377 offset = widget->requisition.height - view_height;
3379 if (offset != menu->scroll_offset)
3380 gtk_menu_scroll_to (menu, offset);
3384 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3385 gboolean touchscreen_mode)
3387 gboolean upper_visible;
3388 gboolean lower_visible;
3390 upper_visible = menu->upper_arrow_visible;
3391 lower_visible = menu->lower_arrow_visible;
3393 gtk_menu_scroll_by (menu, menu->scroll_step);
3395 if (touchscreen_mode &&
3396 (upper_visible != menu->upper_arrow_visible ||
3397 lower_visible != menu->lower_arrow_visible))
3399 /* We are about to hide a scroll arrow while the mouse is pressed,
3400 * this would cause the uncovered menu item to be activated on button
3401 * release. Therefore we need to ignore button release here
3403 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3404 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3409 gtk_menu_scroll_timeout (gpointer data)
3412 gboolean touchscreen_mode;
3414 menu = GTK_MENU (data);
3416 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3417 "gtk-touchscreen-mode", &touchscreen_mode,
3420 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3426 gtk_menu_scroll_timeout_initial (gpointer data)
3430 gboolean touchscreen_mode;
3432 menu = GTK_MENU (data);
3434 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3435 "gtk-timeout-repeat", &timeout,
3436 "gtk-touchscreen-mode", &touchscreen_mode,
3439 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3441 gtk_menu_remove_scroll_timeout (menu);
3443 menu->timeout_id = gdk_threads_add_timeout (timeout,
3444 gtk_menu_scroll_timeout,
3451 gtk_menu_start_scrolling (GtkMenu *menu)
3454 gboolean touchscreen_mode;
3456 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3457 "gtk-timeout-repeat", &timeout,
3458 "gtk-touchscreen-mode", &touchscreen_mode,
3461 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3463 menu->timeout_id = gdk_threads_add_timeout (timeout,
3464 gtk_menu_scroll_timeout_initial,
3469 gtk_menu_scroll (GtkWidget *widget,
3470 GdkEventScroll *event)
3472 GtkMenu *menu = GTK_MENU (widget);
3474 switch (event->direction)
3476 case GDK_SCROLL_RIGHT:
3477 case GDK_SCROLL_DOWN:
3478 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3480 case GDK_SCROLL_LEFT:
3482 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3490 get_arrows_sensitive_area (GtkMenu *menu,
3491 GdkRectangle *upper,
3492 GdkRectangle *lower)
3496 guint vertical_padding;
3498 gint scroll_arrow_height;
3499 GtkArrowPlacement arrow_placement;
3501 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3503 gtk_widget_style_get (GTK_WIDGET (menu),
3504 "vertical-padding", &vertical_padding,
3505 "scroll-arrow-vlength", &scroll_arrow_height,
3506 "arrow-placement", &arrow_placement,
3509 border = GTK_CONTAINER (menu)->border_width +
3510 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3512 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3514 switch (arrow_placement)
3516 case GTK_ARROWS_BOTH:
3521 upper->width = width;
3522 upper->height = scroll_arrow_height + border;
3528 lower->y = win_y + height - border - scroll_arrow_height;
3529 lower->width = width;
3530 lower->height = scroll_arrow_height + border;
3534 case GTK_ARROWS_START:
3539 upper->width = width / 2;
3540 upper->height = scroll_arrow_height + border;
3545 lower->x = win_x + width / 2;
3547 lower->width = width / 2;
3548 lower->height = scroll_arrow_height + border;
3552 case GTK_ARROWS_END:
3556 upper->y = win_y + height - border - scroll_arrow_height;
3557 upper->width = width / 2;
3558 upper->height = scroll_arrow_height + border;
3563 lower->x = win_x + width / 2;
3564 lower->y = win_y + height - border - scroll_arrow_height;
3565 lower->width = width / 2;
3566 lower->height = scroll_arrow_height + border;
3574 gtk_menu_handle_scrolling (GtkMenu *menu,
3580 GtkMenuShell *menu_shell;
3581 GtkMenuPrivate *priv;
3584 gboolean scroll_fast = FALSE;
3586 gboolean touchscreen_mode;
3588 priv = gtk_menu_get_private (menu);
3590 menu_shell = GTK_MENU_SHELL (menu);
3592 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3593 "gtk-touchscreen-mode", &touchscreen_mode,
3596 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3600 /* upper arrow handling */
3602 get_arrows_sensitive_area (menu, &rect, NULL);
3605 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3606 (x >= rect.x) && (x < rect.x + rect.width) &&
3607 (y >= rect.y) && (y < rect.y + rect.height))
3612 if (touchscreen_mode)
3613 menu->upper_arrow_prelight = in_arrow;
3615 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3617 gboolean arrow_pressed = FALSE;
3619 if (menu->upper_arrow_visible && !menu->tearoff_active)
3621 if (touchscreen_mode)
3623 if (enter && menu->upper_arrow_prelight)
3625 if (menu->timeout_id == 0)
3627 /* Deselect the active item so that
3628 * any submenus are popped down
3630 gtk_menu_shell_deselect (menu_shell);
3632 gtk_menu_remove_scroll_timeout (menu);
3633 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3637 /* Only do stuff on click. */
3638 gtk_menu_start_scrolling (menu);
3639 arrow_pressed = TRUE;
3644 arrow_pressed = TRUE;
3649 gtk_menu_stop_scrolling (menu);
3652 else /* !touchscreen_mode */
3654 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3656 if (enter && in_arrow &&
3657 (!menu->upper_arrow_prelight ||
3658 menu->scroll_fast != scroll_fast))
3660 menu->upper_arrow_prelight = TRUE;
3661 menu->scroll_fast = scroll_fast;
3663 /* Deselect the active item so that
3664 * any submenus are popped down
3666 gtk_menu_shell_deselect (menu_shell);
3668 gtk_menu_remove_scroll_timeout (menu);
3669 menu->scroll_step = scroll_fast ?
3670 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3673 gdk_threads_add_timeout (scroll_fast ?
3674 MENU_SCROLL_TIMEOUT2 :
3675 MENU_SCROLL_TIMEOUT1,
3676 gtk_menu_scroll_timeout, menu);
3678 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3680 gtk_menu_stop_scrolling (menu);
3685 /* gtk_menu_start_scrolling() might have hit the top of the
3686 * menu, so check if the button isn't insensitive before
3687 * changing it to something else.
3689 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3691 GtkStateType arrow_state = GTK_STATE_NORMAL;
3694 arrow_state = GTK_STATE_ACTIVE;
3695 else if (menu->upper_arrow_prelight)
3696 arrow_state = GTK_STATE_PRELIGHT;
3698 if (arrow_state != priv->upper_arrow_state)
3700 priv->upper_arrow_state = arrow_state;
3702 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3708 /* lower arrow handling */
3710 get_arrows_sensitive_area (menu, NULL, &rect);
3713 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3714 (x >= rect.x) && (x < rect.x + rect.width) &&
3715 (y >= rect.y) && (y < rect.y + rect.height))
3720 if (touchscreen_mode)
3721 menu->lower_arrow_prelight = in_arrow;
3723 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3725 gboolean arrow_pressed = FALSE;
3727 if (menu->lower_arrow_visible && !menu->tearoff_active)
3729 if (touchscreen_mode)
3731 if (enter && menu->lower_arrow_prelight)
3733 if (menu->timeout_id == 0)
3735 /* Deselect the active item so that
3736 * any submenus are popped down
3738 gtk_menu_shell_deselect (menu_shell);
3740 gtk_menu_remove_scroll_timeout (menu);
3741 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3745 /* Only do stuff on click. */
3746 gtk_menu_start_scrolling (menu);
3747 arrow_pressed = TRUE;
3752 arrow_pressed = TRUE;
3757 gtk_menu_stop_scrolling (menu);
3760 else /* !touchscreen_mode */
3762 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3764 if (enter && in_arrow &&
3765 (!menu->lower_arrow_prelight ||
3766 menu->scroll_fast != scroll_fast))
3768 menu->lower_arrow_prelight = TRUE;
3769 menu->scroll_fast = scroll_fast;
3771 /* Deselect the active item so that
3772 * any submenus are popped down
3774 gtk_menu_shell_deselect (menu_shell);
3776 gtk_menu_remove_scroll_timeout (menu);
3777 menu->scroll_step = scroll_fast ?
3778 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3781 gdk_threads_add_timeout (scroll_fast ?
3782 MENU_SCROLL_TIMEOUT2 :
3783 MENU_SCROLL_TIMEOUT1,
3784 gtk_menu_scroll_timeout, menu);
3786 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3788 gtk_menu_stop_scrolling (menu);
3793 /* gtk_menu_start_scrolling() might have hit the bottom of the
3794 * menu, so check if the button isn't insensitive before
3795 * changing it to something else.
3797 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3799 GtkStateType arrow_state = GTK_STATE_NORMAL;
3802 arrow_state = GTK_STATE_ACTIVE;
3803 else if (menu->lower_arrow_prelight)
3804 arrow_state = GTK_STATE_PRELIGHT;
3806 if (arrow_state != priv->lower_arrow_state)
3808 priv->lower_arrow_state = arrow_state;
3810 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3818 gtk_menu_enter_notify (GtkWidget *widget,
3819 GdkEventCrossing *event)
3821 GtkWidget *menu_item;
3822 gboolean touchscreen_mode;
3824 if (event->mode == GDK_CROSSING_GTK_GRAB ||
3825 event->mode == GDK_CROSSING_GTK_UNGRAB ||
3826 event->mode == GDK_CROSSING_STATE_CHANGED)
3829 g_object_get (gtk_widget_get_settings (widget),
3830 "gtk-touchscreen-mode", &touchscreen_mode,
3833 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3834 if (GTK_IS_MENU (widget))
3836 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3838 if (!menu_shell->ignore_enter)
3839 gtk_menu_handle_scrolling (GTK_MENU (widget),
3840 event->x_root, event->y_root, TRUE, TRUE);
3843 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3845 GtkWidget *menu = menu_item->parent;
3847 if (GTK_IS_MENU (menu))
3849 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3850 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3852 if (priv->seen_item_enter)
3854 /* This is the second enter we see for an item
3855 * on this menu. This means a release should always
3858 menu_shell->activate_time = 0;
3860 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3861 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3863 if (definitely_within_item (menu_item, event->x, event->y))
3865 /* This is an actual user-enter (ie. not a pop-under)
3866 * In this case, the user must either have entered
3867 * sufficiently far enough into the item, or he must move
3868 * far enough away from the enter point. (see
3869 * gtk_menu_motion_notify())
3871 menu_shell->activate_time = 0;
3875 priv->seen_item_enter = TRUE;
3879 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3880 * will not correspond to the event widget's parent. Check to see
3881 * if we are in the parent's navigation region.
3883 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3884 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3885 event->x_root, event->y_root))
3888 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3892 gtk_menu_leave_notify (GtkWidget *widget,
3893 GdkEventCrossing *event)
3895 GtkMenuShell *menu_shell;
3897 GtkMenuItem *menu_item;
3898 GtkWidget *event_widget;
3900 if (event->mode == GDK_CROSSING_GTK_GRAB ||
3901 event->mode == GDK_CROSSING_GTK_UNGRAB ||
3902 event->mode == GDK_CROSSING_STATE_CHANGED)
3905 menu = GTK_MENU (widget);
3906 menu_shell = GTK_MENU_SHELL (widget);
3908 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3911 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3913 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3915 if (!GTK_IS_MENU_ITEM (event_widget))
3918 menu_item = GTK_MENU_ITEM (event_widget);
3920 /* Here we check to see if we're leaving an active menu item with a submenu,
3921 * in which case we enter submenu navigation mode.
3923 if (menu_shell->active_menu_item != NULL
3924 && menu_item->submenu != NULL
3925 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3927 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3929 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3932 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3934 /* We are leaving an active menu item with nonactive submenu.
3935 * Deselect it so we don't surprise the user with by popping
3936 * up a submenu _after_ he left the item.
3938 gtk_menu_shell_deselect (menu_shell);
3943 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3947 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3949 if (menu->navigation_region)
3951 gdk_region_destroy (menu->navigation_region);
3952 menu->navigation_region = NULL;
3954 if (menu->navigation_timeout)
3956 g_source_remove (menu->navigation_timeout);
3957 menu->navigation_timeout = 0;
3961 /* When the timeout is elapsed, the navigation region is destroyed
3962 * and the menuitem under the pointer (if any) is selected.
3965 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3967 GtkMenu *menu = user_data;
3968 GdkWindow *child_window;
3970 gtk_menu_stop_navigating_submenu (menu);
3972 if (GTK_WIDGET_REALIZED (menu))
3974 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3978 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3980 send_event->crossing.window = g_object_ref (child_window);
3981 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3982 send_event->crossing.send_event = TRUE;
3984 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3986 gdk_event_free (send_event);
3994 gtk_menu_navigating_submenu (GtkMenu *menu,
3998 if (menu->navigation_region)
4000 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
4004 gtk_menu_stop_navigating_submenu (menu);
4011 #undef DRAW_STAY_UP_TRIANGLE
4013 #ifdef DRAW_STAY_UP_TRIANGLE
4016 draw_stay_up_triangle (GdkWindow *window,
4019 /* Draw ugly color all over the stay-up triangle */
4020 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
4021 GdkGCValues gc_values;
4023 GdkRectangle clipbox;
4025 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
4026 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
4027 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
4028 gdk_gc_set_clip_region (ugly_gc, region);
4030 gdk_region_get_clipbox (region, &clipbox);
4032 gdk_draw_rectangle (window,
4035 clipbox.x, clipbox.y,
4036 clipbox.width, clipbox.height);
4038 g_object_unref (ugly_gc);
4043 flip_region (GdkRegion *region,
4048 GdkRectangle *rectangles;
4049 GdkRectangle clipbox;
4050 GdkRegion *new_region;
4053 new_region = gdk_region_new ();
4055 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
4056 gdk_region_get_clipbox (region, &clipbox);
4058 for (i = 0; i < n_rectangles; ++i)
4060 GdkRectangle rect = rectangles[i];
4063 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
4066 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
4068 gdk_region_union_with_rect (new_region, &rect);
4071 g_free (rectangles);
4077 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4078 GtkMenuItem *menu_item,
4079 GdkEventCrossing *event)
4081 gint submenu_left = 0;
4082 gint submenu_right = 0;
4083 gint submenu_top = 0;
4084 gint submenu_bottom = 0;
4088 GtkWidget *event_widget;
4090 g_return_if_fail (menu_item->submenu != NULL);
4091 g_return_if_fail (event != NULL);
4093 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4095 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
4096 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
4098 submenu_right = submenu_left + width;
4099 submenu_bottom = submenu_top + height;
4101 gdk_drawable_get_size (event_widget->window, &width, &height);
4103 if (event->x >= 0 && event->x < width)
4106 gboolean flip_y = FALSE;
4107 gboolean flip_x = FALSE;
4109 gtk_menu_stop_navigating_submenu (menu);
4111 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4114 point[0].x = event->x_root;
4115 point[1].x = submenu_left;
4120 point[0].x = event->x_root + 1;
4121 point[1].x = 2 * (event->x_root + 1) - submenu_right;
4129 point[0].y = event->y_root + 1;
4130 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
4132 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
4140 point[0].y = event->y_root;
4141 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
4143 if (point[0].y >= submenu_bottom)
4147 point[2].x = point[1].x;
4148 point[2].y = point[0].y;
4150 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
4152 if (flip_x || flip_y)
4154 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
4155 gdk_region_destroy (menu->navigation_region);
4156 menu->navigation_region = new_region;
4159 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4160 "gtk-menu-popdown-delay", &popdown_delay,
4163 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
4164 gtk_menu_stop_navigating_submenu_cb,
4167 #ifdef DRAW_STAY_UP_TRIANGLE
4168 draw_stay_up_triangle (gdk_get_default_root_window(),
4169 menu->navigation_region);
4175 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4179 g_return_if_fail (GTK_IS_MENU (menu_shell));
4181 parent = menu_shell->parent_menu_shell;
4183 menu_shell->activate_time = 0;
4184 gtk_menu_popdown (GTK_MENU (menu_shell));
4187 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4191 gtk_menu_position (GtkMenu *menu)
4194 GtkRequisition requisition;
4195 GtkMenuPrivate *private;
4200 GdkScreen *pointer_screen;
4201 GdkRectangle monitor;
4203 g_return_if_fail (GTK_IS_MENU (menu));
4205 widget = GTK_WIDGET (menu);
4207 screen = gtk_widget_get_screen (widget);
4208 gdk_display_get_pointer (gdk_screen_get_display (screen),
4209 &pointer_screen, &x, &y, NULL);
4211 /* We need the requisition to figure out the right place to
4212 * popup the menu. In fact, we always need to ask here, since
4213 * if a size_request was queued while we weren't popped up,
4214 * the requisition won't have been recomputed yet.
4216 gtk_widget_size_request (widget, &requisition);
4218 if (pointer_screen != screen)
4220 /* Pointer is on a different screen; roughly center the
4221 * menu on the screen. If someone was using multiscreen
4222 * + Xinerama together they'd probably want something
4223 * fancier; but that is likely to be vanishingly rare.
4225 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4226 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4229 private = gtk_menu_get_private (menu);
4230 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4232 private->initially_pushed_in = FALSE;
4234 /* Set the type hint here to allow custom position functions to set a different hint */
4235 if (!gtk_widget_get_visible (menu->toplevel))
4236 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4238 if (menu->position_func)
4240 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4241 menu->position_func_data);
4243 if (private->monitor_num < 0)
4244 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4246 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4250 gint space_left, space_right, space_above, space_below;
4253 gint xthickness = widget->style->xthickness;
4254 gint ythickness = widget->style->ythickness;
4255 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4257 /* The placement of popup menus horizontally works like this (with
4258 * RTL in parentheses)
4260 * - If there is enough room to the right (left) of the mouse cursor,
4261 * position the menu there.
4263 * - Otherwise, if if there is enough room to the left (right) of the
4264 * mouse cursor, position the menu there.
4266 * - Otherwise if the menu is smaller than the monitor, position it
4267 * on the side of the mouse cursor that has the most space available
4269 * - Otherwise (if there is simply not enough room for the menu on the
4270 * monitor), position it as far left (right) as possible.
4272 * Positioning in the vertical direction is similar: first try below
4273 * mouse cursor, then above.
4275 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4277 space_left = x - monitor.x;
4278 space_right = monitor.x + monitor.width - x - 1;
4279 space_above = y - monitor.y;
4280 space_below = monitor.y + monitor.height - y - 1;
4282 /* position horizontally */
4284 /* the amount of space we need to position the menu. Note the
4285 * menu is offset "xthickness" pixels
4287 needed_width = requisition.width - xthickness;
4289 if (needed_width <= space_left ||
4290 needed_width <= space_right)
4292 if ((rtl && needed_width <= space_left) ||
4293 (!rtl && needed_width > space_right))
4296 x = x + xthickness - requisition.width + 1;
4300 /* position right */
4304 /* x is clamped on-screen further down */
4306 else if (requisition.width <= monitor.width)
4308 /* the menu is too big to fit on either side of the mouse
4309 * cursor, but smaller than the monitor. Position it on
4310 * the side that has the most space
4312 if (space_left > space_right)
4320 x = monitor.x + monitor.width - requisition.width;
4323 else /* menu is simply too big for the monitor */
4328 x = monitor.x + monitor.width - requisition.width;
4337 /* Position vertically. The algorithm is the same as above, but
4338 * simpler because we don't have to take RTL into account.
4340 needed_height = requisition.height - ythickness;
4342 if (needed_height <= space_above ||
4343 needed_height <= space_below)
4345 if (needed_height <= space_below)
4348 y = y + ythickness - requisition.height + 1;
4350 y = CLAMP (y, monitor.y,
4351 monitor.y + monitor.height - requisition.height);
4353 else if (needed_height > space_below && needed_height > space_above)
4355 if (space_below >= space_above)
4356 y = monitor.y + monitor.height - requisition.height;
4368 if (private->initially_pushed_in)
4370 menu_height = GTK_WIDGET (menu)->requisition.height;
4372 if (y + menu_height > monitor.y + monitor.height)
4374 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4375 y = (monitor.y + monitor.height) - menu_height;
4380 scroll_offset += monitor.y - y;
4385 /* FIXME: should this be done in the various position_funcs ? */
4386 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4388 if (GTK_MENU_SHELL (menu)->active)
4390 private->have_position = TRUE;
4395 if (y + requisition.height > monitor.y + monitor.height)
4396 requisition.height = (monitor.y + monitor.height) - y;
4400 scroll_offset += monitor.y - y;
4401 requisition.height -= monitor.y - y;
4405 if (scroll_offset > 0)
4407 GtkBorder arrow_border;
4409 get_arrows_border (menu, &arrow_border);
4410 scroll_offset += arrow_border.top;
4413 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4416 if (!GTK_MENU_SHELL (menu)->active)
4418 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4419 requisition.width, requisition.height);
4422 menu->scroll_offset = scroll_offset;
4426 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4428 if (menu->timeout_id)
4430 g_source_remove (menu->timeout_id);
4431 menu->timeout_id = 0;
4436 gtk_menu_stop_scrolling (GtkMenu *menu)
4438 gboolean touchscreen_mode;
4440 gtk_menu_remove_scroll_timeout (menu);
4442 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4443 "gtk-touchscreen-mode", &touchscreen_mode,
4446 if (!touchscreen_mode)
4448 menu->upper_arrow_prelight = FALSE;
4449 menu->lower_arrow_prelight = FALSE;
4454 gtk_menu_scroll_to (GtkMenu *menu,
4459 gint view_width, view_height;
4462 guint vertical_padding;
4463 guint horizontal_padding;
4464 gboolean double_arrows;
4465 GtkBorder arrow_border;
4467 widget = GTK_WIDGET (menu);
4469 if (menu->tearoff_active &&
4470 menu->tearoff_adjustment &&
4471 (menu->tearoff_adjustment->value != offset))
4473 menu->tearoff_adjustment->value =
4475 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4476 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4479 /* Move/resize the viewport according to arrows: */
4480 view_width = widget->allocation.width;
4481 view_height = widget->allocation.height;
4483 gtk_widget_style_get (GTK_WIDGET (menu),
4484 "vertical-padding", &vertical_padding,
4485 "horizontal-padding", &horizontal_padding,
4488 double_arrows = get_double_arrows (menu);
4490 border_width = GTK_CONTAINER (menu)->border_width;
4491 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4492 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4493 menu_height = widget->requisition.height -
4494 (border_width + widget->style->ythickness + vertical_padding) * 2;
4496 x = border_width + widget->style->xthickness + horizontal_padding;
4497 y = border_width + widget->style->ythickness + vertical_padding;
4499 if (double_arrows && !menu->tearoff_active)
4501 if (view_height < menu_height ||
4502 (offset > 0 && menu->scroll_offset > 0) ||
4503 (offset < 0 && menu->scroll_offset < 0))
4505 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4506 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4507 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4509 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4510 gtk_widget_queue_draw (GTK_WIDGET (menu));
4512 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4514 get_arrows_border (menu, &arrow_border);
4515 y += arrow_border.top;
4516 view_height -= arrow_border.top;
4517 view_height -= arrow_border.bottom;
4520 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4521 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4522 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4523 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4525 if (offset >= menu_height - view_height)
4526 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4527 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4528 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4529 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4531 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4532 (priv->lower_arrow_state != lower_arrow_previous_state))
4533 gtk_widget_queue_draw (GTK_WIDGET (menu));
4535 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4536 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4538 /* At the upper border, possibly remove timeout */
4539 if (menu->scroll_step < 0)
4541 gtk_menu_stop_scrolling (menu);
4542 gtk_widget_queue_draw (GTK_WIDGET (menu));
4546 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4547 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4549 /* At the lower border, possibly remove timeout */
4550 if (menu->scroll_step > 0)
4552 gtk_menu_stop_scrolling (menu);
4553 gtk_widget_queue_draw (GTK_WIDGET (menu));
4557 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4561 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4562 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4564 gtk_menu_stop_scrolling (menu);
4565 gtk_widget_queue_draw (GTK_WIDGET (menu));
4568 else if (!menu->tearoff_active)
4570 gboolean last_visible;
4572 last_visible = menu->upper_arrow_visible;
4573 menu->upper_arrow_visible = offset > 0;
4575 /* upper_arrow_visible may have changed, so requery the border */
4576 get_arrows_border (menu, &arrow_border);
4577 view_height -= arrow_border.top;
4579 if ((last_visible != menu->upper_arrow_visible) &&
4580 !menu->upper_arrow_visible)
4582 menu->upper_arrow_prelight = FALSE;
4584 /* If we hid the upper arrow, possibly remove timeout */
4585 if (menu->scroll_step < 0)
4587 gtk_menu_stop_scrolling (menu);
4588 gtk_widget_queue_draw (GTK_WIDGET (menu));
4592 last_visible = menu->lower_arrow_visible;
4593 menu->lower_arrow_visible = offset < menu_height - view_height;
4595 /* lower_arrow_visible may have changed, so requery the border */
4596 get_arrows_border (menu, &arrow_border);
4597 view_height -= arrow_border.bottom;
4599 if ((last_visible != menu->lower_arrow_visible) &&
4600 !menu->lower_arrow_visible)
4602 menu->lower_arrow_prelight = FALSE;
4604 /* If we hid the lower arrow, possibly remove timeout */
4605 if (menu->scroll_step > 0)
4607 gtk_menu_stop_scrolling (menu);
4608 gtk_widget_queue_draw (GTK_WIDGET (menu));
4612 y += arrow_border.top;
4615 /* Scroll the menu: */
4616 if (GTK_WIDGET_REALIZED (menu))
4617 gdk_window_move (menu->bin_window, 0, -offset);
4619 if (GTK_WIDGET_REALIZED (menu))
4620 gdk_window_move_resize (menu->view_window,
4626 menu->scroll_offset = offset;
4630 compute_child_offset (GtkMenu *menu,
4631 GtkWidget *menu_item,
4634 gboolean *is_last_child)
4636 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4637 gint item_top_attach;
4638 gint item_bottom_attach;
4639 gint child_offset = 0;
4642 get_effective_child_attach (menu_item, NULL, NULL,
4643 &item_top_attach, &item_bottom_attach);
4645 /* there is a possibility that we get called before _size_request, so
4646 * check the height table for safety.
4648 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4651 /* when we have a row with only invisible children, it's height will
4652 * be zero, so there's no need to check WIDGET_VISIBLE here
4654 for (i = 0; i < item_top_attach; i++)
4655 child_offset += priv->heights[i];
4658 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4660 *offset = child_offset;
4662 *height = priv->heights[item_top_attach];
4668 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4669 GtkWidget *menu_item)
4672 gint child_offset, child_height;
4676 gboolean last_child = 0;
4678 menu = GTK_MENU (menu_shell);
4680 /* We need to check if the selected item fully visible.
4681 * If not we need to scroll the menu so that it becomes fully
4685 if (compute_child_offset (menu, menu_item,
4686 &child_offset, &child_height, &last_child))
4688 guint vertical_padding;
4689 gboolean double_arrows;
4691 y = menu->scroll_offset;
4692 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4694 gtk_widget_style_get (GTK_WIDGET (menu),
4695 "vertical-padding", &vertical_padding,
4698 double_arrows = get_double_arrows (menu);
4700 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4702 if (child_offset < y)
4704 /* Ignore the enter event we might get if the pointer is on the menu
4706 menu_shell->ignore_enter = TRUE;
4707 gtk_menu_scroll_to (menu, child_offset);
4711 GtkBorder arrow_border;
4715 get_arrows_border (menu, &arrow_border);
4716 if (!menu->tearoff_active)
4717 arrow_height = arrow_border.top + arrow_border.bottom;
4719 if (child_offset + child_height > y + height - arrow_height)
4722 if ((!last_child && !menu->tearoff_active) || double_arrows)
4723 arrow_height += arrow_border.bottom;
4725 y = child_offset + child_height - height + arrow_height;
4726 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4728 /* Need upper arrow */
4729 arrow_height += arrow_border.top;
4730 y = child_offset + child_height - height + arrow_height;
4732 /* Ignore the enter event we might get if the pointer is on the menu
4734 menu_shell->ignore_enter = TRUE;
4735 gtk_menu_scroll_to (menu, y);
4743 gtk_menu_select_item (GtkMenuShell *menu_shell,
4744 GtkWidget *menu_item)
4746 GtkMenu *menu = GTK_MENU (menu_shell);
4748 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4749 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4751 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4755 /* Reparent the menu, taking care of the refcounting
4757 * If unrealize is true we force a unrealize while reparenting the parent.
4758 * This can help eliminate flicker in some cases.
4760 * What happens is that when the menu is unrealized and then re-realized,
4761 * the allocations are as follows:
4763 * parent - 1x1 at (0,0)
4764 * child1 - 100x20 at (0,0)
4765 * child2 - 100x20 at (0,20)
4766 * child3 - 100x20 at (0,40)
4768 * That is, the parent is small but the children are full sized. Then,
4769 * when the queued_resize gets processed, the parent gets resized to
4772 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4773 * contains the following logic:
4775 * - if a move or resize operation on a window would change the clip
4776 * region on the children, then before the window is resized
4777 * the background for children is temporarily set to None, the
4778 * move/resize done, and the background for the children restored.
4780 * So, at the point where the parent is resized to final size, the
4781 * background for the children is temporarily None, and thus they
4782 * are not cleared to the background color and the previous background
4783 * (the image of the menu) is left in place.
4786 gtk_menu_reparent (GtkMenu *menu,
4787 GtkWidget *new_parent,
4790 GtkObject *object = GTK_OBJECT (menu);
4791 GtkWidget *widget = GTK_WIDGET (menu);
4792 gboolean was_floating = g_object_is_floating (object);
4794 g_object_ref_sink (object);
4798 g_object_ref (object);
4799 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4800 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4801 g_object_unref (object);
4804 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4807 g_object_force_floating (G_OBJECT (object));
4809 g_object_unref (object);
4813 gtk_menu_show_all (GtkWidget *widget)
4815 /* Show children, but not self. */
4816 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4821 gtk_menu_hide_all (GtkWidget *widget)
4823 /* Hide children, but not self. */
4824 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4828 * gtk_menu_set_screen:
4829 * @menu: a #GtkMenu.
4830 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
4831 * determined by the widget the menu is attached to.
4833 * Sets the #GdkScreen on which the menu will be displayed.
4838 gtk_menu_set_screen (GtkMenu *menu,
4841 g_return_if_fail (GTK_IS_MENU (menu));
4842 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4844 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4848 menu_change_screen (menu, screen);
4852 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4854 attach_widget_screen_changed (attach_widget, NULL, menu);
4860 * @menu: a #GtkMenu.
4861 * @child: a #GtkMenuItem.
4862 * @left_attach: The column number to attach the left side of the item to.
4863 * @right_attach: The column number to attach the right side of the item to.
4864 * @top_attach: The row number to attach the top of the item to.
4865 * @bottom_attach: The row number to attach the bottom of the item to.
4867 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4868 * an item will occupy is specified by @left_attach, @right_attach,
4869 * @top_attach and @bottom_attach. These each represent the leftmost,
4870 * rightmost, uppermost and lower column and row numbers of the table.
4871 * (Columns and rows are indexed from zero).
4873 * Note that this function is not related to gtk_menu_detach().
4878 gtk_menu_attach (GtkMenu *menu,
4883 guint bottom_attach)
4885 GtkMenuShell *menu_shell;
4887 g_return_if_fail (GTK_IS_MENU (menu));
4888 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4889 g_return_if_fail (child->parent == NULL ||
4890 child->parent == GTK_WIDGET (menu));
4891 g_return_if_fail (left_attach < right_attach);
4892 g_return_if_fail (top_attach < bottom_attach);
4894 menu_shell = GTK_MENU_SHELL (menu);
4898 AttachInfo *ai = get_attach_info (child);
4900 ai->left_attach = left_attach;
4901 ai->right_attach = right_attach;
4902 ai->top_attach = top_attach;
4903 ai->bottom_attach = bottom_attach;
4905 menu_shell->children = g_list_append (menu_shell->children, child);
4907 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4909 menu_queue_resize (menu);
4913 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4914 "left-attach", left_attach,
4915 "right-attach", right_attach,
4916 "top-attach", top_attach,
4917 "bottom-attach", bottom_attach,
4923 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4927 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4928 "gtk-menu-popup-delay", &popup_delay,
4935 find_child_containing (GtkMenuShell *menu_shell,
4943 /* find a child which includes the area given by
4944 * left, right, top, bottom.
4947 for (list = menu_shell->children; list; list = list->next)
4951 if (!_gtk_menu_item_is_selectable (list->data))
4954 get_effective_child_attach (list->data, &l, &r, &t, &b);
4956 if (l <= left && right <= r
4957 && t <= top && bottom <= b)
4958 return GTK_WIDGET (list->data);
4965 gtk_menu_move_current (GtkMenuShell *menu_shell,
4966 GtkMenuDirectionType direction)
4968 GtkMenu *menu = GTK_MENU (menu_shell);
4971 GtkWidget *match = NULL;
4973 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4977 case GTK_MENU_DIR_CHILD:
4978 direction = GTK_MENU_DIR_PARENT;
4980 case GTK_MENU_DIR_PARENT:
4981 direction = GTK_MENU_DIR_CHILD;
4987 /* use special table menu key bindings */
4988 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4990 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4992 if (direction == GTK_MENU_DIR_NEXT)
4994 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4996 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5004 for (i = 0; i < t; i++)
5006 match = find_child_containing (menu_shell,
5007 l, l + 1, i, i + 1);
5013 else if (direction == GTK_MENU_DIR_PREV)
5015 for (i = t; i > 0; i--)
5017 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5025 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5027 match = find_child_containing (menu_shell,
5028 l, l + 1, i - 1, i);
5034 else if (direction == GTK_MENU_DIR_PARENT)
5036 /* we go one left if possible */
5038 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5042 GtkWidget *parent = menu_shell->parent_menu_shell;
5045 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5046 match = menu_shell->active_menu_item;
5049 else if (direction == GTK_MENU_DIR_CHILD)
5051 /* we go one right if possible */
5052 if (r < gtk_menu_get_n_columns (menu))
5053 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5057 GtkWidget *parent = menu_shell->parent_menu_shell;
5059 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5061 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5062 match = menu_shell->active_menu_item;
5068 gtk_menu_shell_select_item (menu_shell, match);
5073 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5077 get_visible_size (GtkMenu *menu)
5079 GtkWidget *widget = GTK_WIDGET (menu);
5080 GtkContainer *container = GTK_CONTAINER (menu);
5082 gint menu_height = (widget->allocation.height
5083 - 2 * (container->border_width
5084 + widget->style->ythickness));
5086 if (!menu->tearoff_active)
5088 GtkBorder arrow_border;
5090 get_arrows_border (menu, &arrow_border);
5091 menu_height -= arrow_border.top;
5092 menu_height -= arrow_border.bottom;
5098 /* Find the sensitive on-screen child containing @y, or if none,
5099 * the nearest selectable onscreen child. (%NULL if none)
5102 child_at (GtkMenu *menu,
5105 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5106 GtkWidget *child = NULL;
5107 gint child_offset = 0;
5110 gint lower, upper; /* Onscreen bounds */
5112 menu_height = get_visible_size (menu);
5113 lower = menu->scroll_offset;
5114 upper = menu->scroll_offset + menu_height;
5116 for (children = menu_shell->children; children; children = children->next)
5118 if (gtk_widget_get_visible (children->data))
5120 GtkRequisition child_requisition;
5122 gtk_widget_size_request (children->data, &child_requisition);
5124 if (_gtk_menu_item_is_selectable (children->data) &&
5125 child_offset >= lower &&
5126 child_offset + child_requisition.height <= upper)
5128 child = children->data;
5130 if (child_offset + child_requisition.height > y &&
5131 !GTK_IS_TEAROFF_MENU_ITEM (child))
5135 child_offset += child_requisition.height;
5143 get_menu_height (GtkMenu *menu)
5146 GtkWidget *widget = GTK_WIDGET (menu);
5148 height = widget->requisition.height;
5149 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
5151 if (!menu->tearoff_active)
5153 GtkBorder arrow_border;
5155 get_arrows_border (menu, &arrow_border);
5156 height -= arrow_border.top;
5157 height -= arrow_border.bottom;
5164 gtk_menu_real_move_scroll (GtkMenu *menu,
5167 gint page_size = get_visible_size (menu);
5168 gint end_position = get_menu_height (menu);
5169 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5173 case GTK_SCROLL_PAGE_UP:
5174 case GTK_SCROLL_PAGE_DOWN:
5178 gint child_offset = 0;
5179 gboolean old_upper_arrow_visible;
5182 if (type == GTK_SCROLL_PAGE_UP)
5187 if (menu_shell->active_menu_item)
5191 compute_child_offset (menu, menu_shell->active_menu_item,
5192 &child_offset, &child_height, NULL);
5193 child_offset += child_height / 2;
5196 menu_shell->ignore_enter = TRUE;
5197 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5198 old_offset = menu->scroll_offset;
5200 new_offset = menu->scroll_offset + step;
5201 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5203 gtk_menu_scroll_to (menu, new_offset);
5205 if (menu_shell->active_menu_item)
5207 GtkWidget *new_child;
5208 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5209 GtkBorder arrow_border;
5211 get_arrows_border (menu, &arrow_border);
5213 if (menu->scroll_offset != old_offset)
5214 step = menu->scroll_offset - old_offset;
5216 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5218 new_child = child_at (menu, child_offset + step);
5220 gtk_menu_shell_select_item (menu_shell, new_child);
5224 case GTK_SCROLL_START:
5225 /* Ignore the enter event we might get if the pointer is on the menu
5227 menu_shell->ignore_enter = TRUE;
5228 gtk_menu_scroll_to (menu, 0);
5229 gtk_menu_shell_select_first (menu_shell, TRUE);
5231 case GTK_SCROLL_END:
5232 /* Ignore the enter event we might get if the pointer is on the menu
5234 menu_shell->ignore_enter = TRUE;
5235 gtk_menu_scroll_to (menu, end_position - page_size);
5236 _gtk_menu_shell_select_last (menu_shell, TRUE);
5245 * gtk_menu_set_monitor:
5247 * @monitor_num: the number of the monitor on which the menu should
5250 * Informs GTK+ on which monitor a menu should be popped up.
5251 * See gdk_screen_get_monitor_geometry().
5253 * This function should be called from a #GtkMenuPositionFunc if the
5254 * menu should not appear on the same monitor as the pointer. This
5255 * information can't be reliably inferred from the coordinates returned
5256 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5257 * may extend beyond the monitor boundaries or even the screen boundaries.
5262 gtk_menu_set_monitor (GtkMenu *menu,
5265 GtkMenuPrivate *priv;
5266 g_return_if_fail (GTK_IS_MENU (menu));
5268 priv = gtk_menu_get_private (menu);
5270 priv->monitor_num = monitor_num;
5274 * gtk_menu_get_monitor:
5277 * Retrieves the number of the monitor on which to show the menu.
5279 * Returns: the number of the monitor on which the menu should
5280 * be popped up or -1, if no monitor has been set
5285 gtk_menu_get_monitor (GtkMenu *menu)
5287 GtkMenuPrivate *priv;
5288 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5290 priv = gtk_menu_get_private (menu);
5292 return priv->monitor_num;
5296 * gtk_menu_get_for_attach_widget:
5297 * @widget: a #GtkWidget
5299 * Returns a list of the menus which are attached to this widget.
5300 * This list is owned by GTK+ and must not be modified.
5302 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5307 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5311 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5313 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5318 gtk_menu_grab_notify (GtkWidget *widget,
5319 gboolean was_grabbed)
5321 GtkWidget *toplevel;
5322 GtkWindowGroup *group;
5325 toplevel = gtk_widget_get_toplevel (widget);
5326 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5327 grab = _gtk_window_group_get_current_grab (group);
5331 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5332 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5337 * gtk_menu_set_reserve_toggle_size:
5339 * @reserve_toggle_size: whether to reserve size for toggles
5341 * Sets whether the menu should reserve space for drawing toggles
5342 * or icons, regardless of their actual presence.
5347 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5348 gboolean reserve_toggle_size)
5350 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5351 gboolean no_toggle_size;
5353 no_toggle_size = !reserve_toggle_size;
5355 if (priv->no_toggle_size != no_toggle_size)
5357 priv->no_toggle_size = no_toggle_size;
5359 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5364 * gtk_menu_get_reserve_toggle_size:
5367 * Returns whether the menu reserves space for toggles and
5368 * icons, regardless of their actual presence.
5370 * Returns: Whether the menu reserves toggle space
5375 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5377 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5379 return !priv->no_toggle_size;
5382 #define __GTK_MENU_C__
5383 #include "gtkaliasdef.c"