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
29 #include <string.h> /* memset */
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
36 #include "gtkmarshalers.h"
38 #include "gtkmenuitem.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
49 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
51 #define DEFAULT_POPUP_DELAY 225
52 #define DEFAULT_POPDOWN_DELAY 1000
54 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
55 * extends below the submenu
58 #define MENU_SCROLL_STEP1 8
59 #define MENU_SCROLL_STEP2 15
60 #define MENU_SCROLL_FAST_ZONE 8
61 #define MENU_SCROLL_TIMEOUT1 50
62 #define MENU_SCROLL_TIMEOUT2 20
64 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
65 #define ATTACHED_MENUS "gtk-attached-menus"
67 typedef struct _GtkMenuAttachData GtkMenuAttachData;
68 typedef struct _GtkMenuPrivate GtkMenuPrivate;
70 struct _GtkMenuAttachData
72 GtkWidget *attach_widget;
73 GtkMenuDetachFunc detacher;
76 struct _GtkMenuPrivate
78 gboolean seen_item_enter;
80 gboolean have_position;
84 /* info used for the table */
90 /* Cached layout information */
98 GtkStateType lower_arrow_state;
99 GtkStateType upper_arrow_state;
101 gboolean ignore_button_release;
102 gboolean initially_pushed_in;
111 gint effective_left_attach;
112 gint effective_right_attach;
113 gint effective_top_attach;
114 gint effective_bottom_attach;
135 CHILD_PROP_LEFT_ATTACH,
136 CHILD_PROP_RIGHT_ATTACH,
137 CHILD_PROP_TOP_ATTACH,
138 CHILD_PROP_BOTTOM_ATTACH
141 static void gtk_menu_set_property (GObject *object,
145 static void gtk_menu_get_property (GObject *object,
149 static void gtk_menu_set_child_property(GtkContainer *container,
154 static void gtk_menu_get_child_property(GtkContainer *container,
159 static void gtk_menu_destroy (GtkObject *object);
160 static void gtk_menu_realize (GtkWidget *widget);
161 static void gtk_menu_unrealize (GtkWidget *widget);
162 static void gtk_menu_size_request (GtkWidget *widget,
163 GtkRequisition *requisition);
164 static void gtk_menu_size_allocate (GtkWidget *widget,
165 GtkAllocation *allocation);
166 static void gtk_menu_paint (GtkWidget *widget,
167 GdkEventExpose *expose);
168 static void gtk_menu_show (GtkWidget *widget);
169 static gboolean gtk_menu_expose (GtkWidget *widget,
170 GdkEventExpose *event);
171 static gboolean gtk_menu_key_press (GtkWidget *widget,
173 static gboolean gtk_menu_scroll (GtkWidget *widget,
174 GdkEventScroll *event);
175 static gboolean gtk_menu_button_press (GtkWidget *widget,
176 GdkEventButton *event);
177 static gboolean gtk_menu_button_release (GtkWidget *widget,
178 GdkEventButton *event);
179 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
180 GdkEventMotion *event);
181 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
182 GdkEventCrossing *event);
183 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
184 GdkEventCrossing *event);
185 static void gtk_menu_scroll_to (GtkMenu *menu,
187 static void gtk_menu_grab_notify (GtkWidget *widget,
188 gboolean was_grabbed);
190 static void gtk_menu_stop_scrolling (GtkMenu *menu);
191 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
192 static gboolean gtk_menu_scroll_timeout (gpointer data);
193 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
194 static void gtk_menu_start_scrolling (GtkMenu *menu);
196 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
197 GtkWidget *menu_item);
198 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
199 GtkWidget *menu_item);
200 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
203 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
205 static void gtk_menu_handle_scrolling (GtkMenu *menu,
210 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
212 static void gtk_menu_style_set (GtkWidget *widget,
213 GtkStyle *previous_style);
214 static gboolean gtk_menu_focus (GtkWidget *widget,
215 GtkDirectionType direction);
216 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
217 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
218 GtkMenuDirectionType direction);
219 static void gtk_menu_real_move_scroll (GtkMenu *menu,
222 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
223 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
224 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
227 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
228 GtkMenuItem *menu_item,
229 GdkEventCrossing *event);
231 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
232 static void gtk_menu_show_all (GtkWidget *widget);
233 static void gtk_menu_hide_all (GtkWidget *widget);
234 static void gtk_menu_position (GtkMenu *menu);
235 static void gtk_menu_reparent (GtkMenu *menu,
236 GtkWidget *new_parent,
238 static void gtk_menu_remove (GtkContainer *menu,
241 static void gtk_menu_update_title (GtkMenu *menu);
243 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
244 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
246 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
248 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
249 gboolean group_changed);
251 static const gchar attach_data_key[] = "gtk-menu-attach-data";
253 static guint menu_signals[LAST_SIGNAL] = { 0 };
255 static GtkMenuPrivate *
256 gtk_menu_get_private (GtkMenu *menu)
258 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
261 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
264 menu_queue_resize (GtkMenu *menu)
266 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
268 priv->have_layout = FALSE;
269 gtk_widget_queue_resize (GTK_WIDGET (menu));
273 attach_info_free (AttachInfo *info)
275 g_slice_free (AttachInfo, info);
279 get_attach_info (GtkWidget *child)
281 GObject *object = G_OBJECT (child);
282 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
286 ai = g_slice_new0 (AttachInfo);
287 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
288 (GDestroyNotify) attach_info_free);
295 is_grid_attached (AttachInfo *ai)
297 return (ai->left_attach >= 0 &&
298 ai->right_attach >= 0 &&
299 ai->top_attach >= 0 &&
300 ai->bottom_attach >= 0);
304 menu_ensure_layout (GtkMenu *menu)
306 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
308 if (!priv->have_layout)
310 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
314 gint max_right_attach;
315 gint max_bottom_attach;
317 /* Find extents of gridded portion
319 max_right_attach = 1;
320 max_bottom_attach = 0;
322 for (l = menu_shell->children; l; l = l->next)
324 GtkWidget *child = l->data;
325 AttachInfo *ai = get_attach_info (child);
327 if (is_grid_attached (ai))
329 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
330 max_right_attach = MAX (max_right_attach, ai->right_attach);
336 row_occupied = g_malloc0 (max_bottom_attach);
338 for (l = menu_shell->children; l; l = l->next)
340 GtkWidget *child = l->data;
341 AttachInfo *ai = get_attach_info (child);
343 if (is_grid_attached (ai))
347 for (i = ai->top_attach; i < ai->bottom_attach; i++)
348 row_occupied[i] = TRUE;
352 /* Lay non-grid-items out in those rows
355 for (l = menu_shell->children; l; l = l->next)
357 GtkWidget *child = l->data;
358 AttachInfo *ai = get_attach_info (child);
360 if (!is_grid_attached (ai))
362 while (current_row < max_bottom_attach && row_occupied[current_row])
365 ai->effective_left_attach = 0;
366 ai->effective_right_attach = max_right_attach;
367 ai->effective_top_attach = current_row;
368 ai->effective_bottom_attach = current_row + 1;
374 ai->effective_left_attach = ai->left_attach;
375 ai->effective_right_attach = ai->right_attach;
376 ai->effective_top_attach = ai->top_attach;
377 ai->effective_bottom_attach = ai->bottom_attach;
381 g_free (row_occupied);
383 priv->n_rows = MAX (current_row, max_bottom_attach);
384 priv->n_columns = max_right_attach;
385 priv->have_layout = TRUE;
391 gtk_menu_get_n_columns (GtkMenu *menu)
393 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
395 menu_ensure_layout (menu);
397 return priv->n_columns;
401 gtk_menu_get_n_rows (GtkMenu *menu)
403 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
405 menu_ensure_layout (menu);
411 get_effective_child_attach (GtkWidget *child,
417 GtkMenu *menu = GTK_MENU (child->parent);
420 menu_ensure_layout (menu);
422 ai = get_attach_info (child);
425 *l = ai->effective_left_attach;
427 *r = ai->effective_right_attach;
429 *t = ai->effective_top_attach;
431 *b = ai->effective_bottom_attach;
436 gtk_menu_class_init (GtkMenuClass *class)
438 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
439 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
440 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
441 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
442 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
443 GtkBindingSet *binding_set;
445 gobject_class->set_property = gtk_menu_set_property;
446 gobject_class->get_property = gtk_menu_get_property;
448 object_class->destroy = gtk_menu_destroy;
450 widget_class->realize = gtk_menu_realize;
451 widget_class->unrealize = gtk_menu_unrealize;
452 widget_class->size_request = gtk_menu_size_request;
453 widget_class->size_allocate = gtk_menu_size_allocate;
454 widget_class->show = gtk_menu_show;
455 widget_class->expose_event = gtk_menu_expose;
456 widget_class->scroll_event = gtk_menu_scroll;
457 widget_class->key_press_event = gtk_menu_key_press;
458 widget_class->button_press_event = gtk_menu_button_press;
459 widget_class->button_release_event = gtk_menu_button_release;
460 widget_class->motion_notify_event = gtk_menu_motion_notify;
461 widget_class->show_all = gtk_menu_show_all;
462 widget_class->hide_all = gtk_menu_hide_all;
463 widget_class->enter_notify_event = gtk_menu_enter_notify;
464 widget_class->leave_notify_event = gtk_menu_leave_notify;
465 widget_class->style_set = gtk_menu_style_set;
466 widget_class->focus = gtk_menu_focus;
467 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
468 widget_class->grab_notify = gtk_menu_grab_notify;
470 container_class->remove = gtk_menu_remove;
471 container_class->get_child_property = gtk_menu_get_child_property;
472 container_class->set_child_property = gtk_menu_set_child_property;
474 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
475 menu_shell_class->deactivate = gtk_menu_deactivate;
476 menu_shell_class->select_item = gtk_menu_select_item;
477 menu_shell_class->insert = gtk_menu_real_insert;
478 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
479 menu_shell_class->move_current = gtk_menu_move_current;
481 menu_signals[MOVE_SCROLL] =
482 _gtk_binding_signal_new (I_("move_scroll"),
483 G_OBJECT_CLASS_TYPE (object_class),
484 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
485 G_CALLBACK (gtk_menu_real_move_scroll),
487 _gtk_marshal_VOID__ENUM,
489 GTK_TYPE_SCROLL_TYPE);
494 * The currently selected menu item.
498 g_object_class_install_property (gobject_class,
500 g_param_spec_uint ("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));
600 gtk_widget_class_install_style_property (widget_class,
601 g_param_spec_int ("horizontal-padding",
602 P_("Horizontal Padding"),
603 P_("Extra space at the left and right edges of the menu"),
607 GTK_PARAM_READABLE));
609 gtk_widget_class_install_style_property (widget_class,
610 g_param_spec_int ("vertical-offset",
611 P_("Vertical Offset"),
612 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
616 GTK_PARAM_READABLE));
618 gtk_widget_class_install_style_property (widget_class,
619 g_param_spec_int ("horizontal-offset",
620 P_("Horizontal Offset"),
621 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
625 GTK_PARAM_READABLE));
627 gtk_widget_class_install_style_property (widget_class,
628 g_param_spec_boolean ("double-arrows",
630 P_("When scrolling, always show both arrows."),
632 GTK_PARAM_READABLE));
635 gtk_container_class_install_child_property (container_class,
636 CHILD_PROP_LEFT_ATTACH,
637 g_param_spec_int ("left-attach",
639 P_("The column number to attach the left side of the child to"),
641 GTK_PARAM_READWRITE));
643 gtk_container_class_install_child_property (container_class,
644 CHILD_PROP_RIGHT_ATTACH,
645 g_param_spec_int ("right-attach",
647 P_("The column number to attach the right side of the child to"),
649 GTK_PARAM_READWRITE));
651 gtk_container_class_install_child_property (container_class,
652 CHILD_PROP_TOP_ATTACH,
653 g_param_spec_int ("top-attach",
655 P_("The row number to attach the top of the child to"),
657 GTK_PARAM_READWRITE));
659 gtk_container_class_install_child_property (container_class,
660 CHILD_PROP_BOTTOM_ATTACH,
661 g_param_spec_int ("bottom-attach",
663 P_("The row number to attach the bottom of the child to"),
665 GTK_PARAM_READWRITE));
667 binding_set = gtk_binding_set_by_class (class);
668 gtk_binding_entry_add_signal (binding_set,
670 I_("move_current"), 1,
671 GTK_TYPE_MENU_DIRECTION_TYPE,
673 gtk_binding_entry_add_signal (binding_set,
676 GTK_TYPE_MENU_DIRECTION_TYPE,
678 gtk_binding_entry_add_signal (binding_set,
681 GTK_TYPE_MENU_DIRECTION_TYPE,
683 gtk_binding_entry_add_signal (binding_set,
686 GTK_TYPE_MENU_DIRECTION_TYPE,
688 gtk_binding_entry_add_signal (binding_set,
691 GTK_TYPE_MENU_DIRECTION_TYPE,
692 GTK_MENU_DIR_PARENT);
693 gtk_binding_entry_add_signal (binding_set,
696 GTK_TYPE_MENU_DIRECTION_TYPE,
697 GTK_MENU_DIR_PARENT);
698 gtk_binding_entry_add_signal (binding_set,
701 GTK_TYPE_MENU_DIRECTION_TYPE,
703 gtk_binding_entry_add_signal (binding_set,
706 GTK_TYPE_MENU_DIRECTION_TYPE,
708 gtk_binding_entry_add_signal (binding_set,
711 GTK_TYPE_SCROLL_TYPE,
713 gtk_binding_entry_add_signal (binding_set,
716 GTK_TYPE_SCROLL_TYPE,
718 gtk_binding_entry_add_signal (binding_set,
721 GTK_TYPE_SCROLL_TYPE,
723 gtk_binding_entry_add_signal (binding_set,
726 GTK_TYPE_SCROLL_TYPE,
728 gtk_binding_entry_add_signal (binding_set,
731 GTK_TYPE_SCROLL_TYPE,
733 gtk_binding_entry_add_signal (binding_set,
736 GTK_TYPE_SCROLL_TYPE,
738 gtk_binding_entry_add_signal (binding_set,
741 GTK_TYPE_SCROLL_TYPE,
742 GTK_SCROLL_PAGE_DOWN);
743 gtk_binding_entry_add_signal (binding_set,
746 GTK_TYPE_SCROLL_TYPE,
747 GTK_SCROLL_PAGE_DOWN);
749 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
750 P_("Can change accelerators"),
751 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
753 GTK_PARAM_READWRITE));
755 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
756 P_("Delay before submenus appear"),
757 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
761 GTK_PARAM_READWRITE));
763 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
764 P_("Delay before hiding a submenu"),
765 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
768 DEFAULT_POPDOWN_DELAY,
769 GTK_PARAM_READWRITE));
771 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
776 gtk_menu_set_property (GObject *object,
783 menu = GTK_MENU (object);
788 gtk_menu_set_active (menu, g_value_get_uint (value));
790 case PROP_ACCEL_GROUP:
791 gtk_menu_set_accel_group (menu, g_value_get_object (value));
793 case PROP_ACCEL_PATH:
794 gtk_menu_set_accel_path (menu, g_value_get_string (value));
796 case PROP_ATTACH_WIDGET:
797 gtk_menu_attach_to_widget (menu, g_value_get_object (value), NULL);
799 case PROP_TEAROFF_STATE:
800 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
802 case PROP_TEAROFF_TITLE:
803 gtk_menu_set_title (menu, g_value_get_string (value));
806 gtk_menu_set_monitor (menu, g_value_get_int (value));
809 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
815 gtk_menu_get_property (GObject *object,
822 menu = GTK_MENU (object);
827 g_value_set_uint (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
829 case PROP_ACCEL_GROUP:
830 g_value_set_object (value, gtk_menu_get_accel_group (menu));
832 case PROP_ACCEL_PATH:
833 g_value_set_string (value, gtk_menu_get_accel_path (menu));
835 case PROP_ATTACH_WIDGET:
836 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
838 case PROP_TEAROFF_STATE:
839 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
841 case PROP_TEAROFF_TITLE:
842 g_value_set_string (value, gtk_menu_get_title (menu));
845 g_value_set_int (value, gtk_menu_get_monitor (menu));
848 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
854 gtk_menu_set_child_property (GtkContainer *container,
860 GtkMenu *menu = GTK_MENU (container);
861 AttachInfo *ai = get_attach_info (child);
865 case CHILD_PROP_LEFT_ATTACH:
866 ai->left_attach = g_value_get_int (value);
868 case CHILD_PROP_RIGHT_ATTACH:
869 ai->right_attach = g_value_get_int (value);
871 case CHILD_PROP_TOP_ATTACH:
872 ai->top_attach = g_value_get_int (value);
874 case CHILD_PROP_BOTTOM_ATTACH:
875 ai->bottom_attach = g_value_get_int (value);
879 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
883 menu_queue_resize (menu);
887 gtk_menu_get_child_property (GtkContainer *container,
893 AttachInfo *ai = get_attach_info (child);
897 case CHILD_PROP_LEFT_ATTACH:
898 g_value_set_int (value, ai->left_attach);
900 case CHILD_PROP_RIGHT_ATTACH:
901 g_value_set_int (value, ai->right_attach);
903 case CHILD_PROP_TOP_ATTACH:
904 g_value_set_int (value, ai->top_attach);
906 case CHILD_PROP_BOTTOM_ATTACH:
907 g_value_set_int (value, ai->bottom_attach);
911 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
917 gtk_menu_window_event (GtkWidget *window,
921 gboolean handled = FALSE;
923 g_object_ref (window);
929 case GDK_KEY_RELEASE:
930 handled = gtk_widget_event (menu, event);
936 g_object_unref (window);
937 g_object_unref (menu);
943 gtk_menu_window_size_request (GtkWidget *window,
944 GtkRequisition *requisition,
947 GtkMenuPrivate *private = gtk_menu_get_private (menu);
949 if (private->have_position)
951 GdkScreen *screen = gtk_widget_get_screen (window);
952 GdkRectangle monitor;
954 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
956 if (private->y + requisition->height > monitor.y + monitor.height)
957 requisition->height = monitor.y + monitor.height - private->y;
959 if (private->y < monitor.y)
960 requisition->height -= monitor.y - private->y;
965 gtk_menu_init (GtkMenu *menu)
967 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
969 menu->parent_menu_item = NULL;
970 menu->old_active_menu_item = NULL;
971 menu->accel_group = NULL;
972 menu->position_func = NULL;
973 menu->position_func_data = NULL;
974 menu->toggle_size = 0;
976 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
977 "type", GTK_WINDOW_POPUP,
980 "signal::event", gtk_menu_window_event, menu,
981 "signal::size_request", gtk_menu_window_size_request, menu,
982 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
984 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
985 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
987 /* Refloat the menu, so that reference counting for the menu isn't
988 * affected by it being a child of the toplevel
990 g_object_force_floating (G_OBJECT (menu));
991 menu->needs_destruction_ref_count = TRUE;
993 menu->view_window = NULL;
994 menu->bin_window = NULL;
996 menu->scroll_offset = 0;
997 menu->scroll_step = 0;
998 menu->timeout_id = 0;
999 menu->scroll_fast = FALSE;
1001 menu->tearoff_window = NULL;
1002 menu->tearoff_hbox = NULL;
1003 menu->torn_off = FALSE;
1004 menu->tearoff_active = FALSE;
1005 menu->tearoff_adjustment = NULL;
1006 menu->tearoff_scrollbar = NULL;
1008 menu->upper_arrow_visible = FALSE;
1009 menu->lower_arrow_visible = FALSE;
1010 menu->upper_arrow_prelight = FALSE;
1011 menu->lower_arrow_prelight = FALSE;
1013 priv->upper_arrow_state = GTK_STATE_NORMAL;
1014 priv->lower_arrow_state = GTK_STATE_NORMAL;
1016 priv->have_layout = FALSE;
1020 gtk_menu_destroy (GtkObject *object)
1022 GtkMenu *menu = GTK_MENU (object);
1023 GtkMenuAttachData *data;
1024 GtkMenuPrivate *priv;
1026 gtk_menu_remove_scroll_timeout (menu);
1028 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1030 gtk_menu_detach (menu);
1032 gtk_menu_stop_navigating_submenu (menu);
1034 if (menu->old_active_menu_item)
1036 g_object_unref (menu->old_active_menu_item);
1037 menu->old_active_menu_item = NULL;
1040 /* Add back the reference count for being a child */
1041 if (menu->needs_destruction_ref_count)
1043 menu->needs_destruction_ref_count = FALSE;
1044 g_object_ref (object);
1047 if (menu->accel_group)
1049 g_object_unref (menu->accel_group);
1050 menu->accel_group = NULL;
1054 gtk_widget_destroy (menu->toplevel);
1056 if (menu->tearoff_window)
1057 gtk_widget_destroy (menu->tearoff_window);
1059 priv = gtk_menu_get_private (menu);
1063 g_free (priv->heights);
1064 priv->heights = NULL;
1069 g_free (priv->title);
1073 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1077 menu_change_screen (GtkMenu *menu,
1078 GdkScreen *new_screen)
1080 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1082 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1084 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1090 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1091 gtk_menu_position (menu);
1094 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1095 private->monitor_num = -1;
1099 attach_widget_screen_changed (GtkWidget *attach_widget,
1100 GdkScreen *previous_screen,
1103 if (gtk_widget_has_screen (attach_widget) &&
1104 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1106 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1111 gtk_menu_attach_to_widget (GtkMenu *menu,
1112 GtkWidget *attach_widget,
1113 GtkMenuDetachFunc detacher)
1115 GtkMenuAttachData *data;
1118 g_return_if_fail (GTK_IS_MENU (menu));
1119 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1121 /* keep this function in sync with gtk_widget_set_parent()
1124 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1127 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1128 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1132 g_object_ref_sink (menu);
1134 data = g_slice_new (GtkMenuAttachData);
1135 data->attach_widget = attach_widget;
1137 g_signal_connect (attach_widget, "screen_changed",
1138 G_CALLBACK (attach_widget_screen_changed), menu);
1139 attach_widget_screen_changed (attach_widget, NULL, menu);
1141 data->detacher = detacher;
1142 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1143 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1144 if (!g_list_find (list, menu))
1146 list = g_list_prepend (list, menu);
1148 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1149 (GDestroyNotify) g_list_free);
1151 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1152 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1154 /* we don't need to set the style here, since
1155 * we are a toplevel widget.
1158 /* Fallback title for menu comes from attach widget */
1159 gtk_menu_update_title (menu);
1163 gtk_menu_get_attach_widget (GtkMenu *menu)
1165 GtkMenuAttachData *data;
1167 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1169 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1171 return data->attach_widget;
1176 gtk_menu_detach (GtkMenu *menu)
1178 GtkMenuAttachData *data;
1181 g_return_if_fail (GTK_IS_MENU (menu));
1183 /* keep this function in sync with gtk_widget_unparent()
1185 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1188 g_warning ("gtk_menu_detach(): menu is not attached");
1191 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1193 g_signal_handlers_disconnect_by_func (data->attach_widget,
1194 (gpointer) attach_widget_screen_changed,
1198 data->detacher (data->attach_widget, menu);
1199 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1200 list = g_list_remove (list, menu);
1202 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1203 (GDestroyNotify) g_list_free);
1205 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1207 if (GTK_WIDGET_REALIZED (menu))
1208 gtk_widget_unrealize (GTK_WIDGET (menu));
1210 g_slice_free (GtkMenuAttachData, data);
1212 /* Fallback title for menu comes from attach widget */
1213 gtk_menu_update_title (menu);
1215 g_object_unref (menu);
1219 gtk_menu_remove (GtkContainer *container,
1222 GtkMenu *menu = GTK_MENU (container);
1224 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1226 /* Clear out old_active_menu_item if it matches the item we are removing
1228 if (menu->old_active_menu_item == widget)
1230 g_object_unref (menu->old_active_menu_item);
1231 menu->old_active_menu_item = NULL;
1234 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1235 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1237 menu_queue_resize (menu);
1243 return g_object_new (GTK_TYPE_MENU, NULL);
1247 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1251 GtkMenu *menu = GTK_MENU (menu_shell);
1252 AttachInfo *ai = get_attach_info (child);
1254 ai->left_attach = -1;
1255 ai->right_attach = -1;
1256 ai->top_attach = -1;
1257 ai->bottom_attach = -1;
1259 if (GTK_WIDGET_REALIZED (menu_shell))
1260 gtk_widget_set_parent_window (child, menu->bin_window);
1262 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1264 menu_queue_resize (menu);
1268 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1273 widget = GTK_WIDGET (menu);
1279 GdkGCValues gc_values;
1281 menu->tearoff_active = FALSE;
1282 menu->saved_scroll_offset = menu->scroll_offset;
1284 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1285 gc = gdk_gc_new_with_values (widget->window,
1286 &gc_values, GDK_GC_SUBWINDOW);
1288 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1290 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1295 gdk_draw_drawable (pixmap, gc,
1296 menu->tearoff_window->window,
1297 0, 0, 0, 0, -1, -1);
1298 g_object_unref (gc);
1300 gtk_widget_set_size_request (menu->tearoff_window,
1304 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1305 g_object_unref (pixmap);
1310 popup_grab_on_window (GdkWindow *window,
1311 guint32 activate_time,
1312 gboolean grab_keyboard)
1314 if ((gdk_pointer_grab (window, TRUE,
1315 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1316 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1317 GDK_POINTER_MOTION_MASK,
1318 NULL, NULL, activate_time) == 0))
1320 if (!grab_keyboard ||
1321 gdk_keyboard_grab (window, TRUE,
1322 activate_time) == 0)
1326 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1337 * @menu: a #GtkMenu.
1338 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1339 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1340 * @func: a user supplied function used to position the menu, or %NULL
1341 * @data: user supplied data to be passed to @func.
1342 * @button: the mouse button which was pressed to initiate the event.
1343 * @activate_time: the time at which the activation event occurred.
1345 * Displays a menu and makes it available for selection. Applications can use
1346 * this function to display context-sensitive menus, and will typically supply
1347 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1348 * parameters. The default menu positioning function will position the menu
1349 * at the current mouse cursor position.
1351 * The @button parameter should be the mouse button pressed to initiate
1352 * the menu popup. If the menu popup was initiated by something other than
1353 * a mouse button press, such as a mouse button release or a keypress,
1354 * @button should be 0.
1356 * The @activate_time parameter is used to conflict-resolve initiation of
1357 * concurrent requests for mouse/keyboard grab requests. To function
1358 * properly, this needs to be the time stamp of the user event (such as
1359 * a mouse click or key press) that caused the initiation of the popup.
1360 * Only if no such event is available, gtk_get_current_event_time() can
1364 gtk_menu_popup (GtkMenu *menu,
1365 GtkWidget *parent_menu_shell,
1366 GtkWidget *parent_menu_item,
1367 GtkMenuPositionFunc func,
1370 guint32 activate_time)
1373 GtkWidget *xgrab_shell;
1375 GdkEvent *current_event;
1376 GtkMenuShell *menu_shell;
1377 gboolean grab_keyboard;
1378 GtkMenuPrivate *priv;
1379 GtkWidget *parent_toplevel;
1381 g_return_if_fail (GTK_IS_MENU (menu));
1383 widget = GTK_WIDGET (menu);
1384 menu_shell = GTK_MENU_SHELL (menu);
1385 priv = gtk_menu_get_private (menu);
1387 menu_shell->parent_menu_shell = parent_menu_shell;
1389 priv->seen_item_enter = FALSE;
1391 /* Find the last viewable ancestor, and make an X grab on it
1393 parent = GTK_WIDGET (menu);
1397 gboolean viewable = TRUE;
1398 GtkWidget *tmp = parent;
1402 if (!GTK_WIDGET_MAPPED (tmp))
1411 xgrab_shell = parent;
1413 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1416 /* We want to receive events generated when we map the menu; unfortunately,
1417 * since there is probably already an implicit grab in place from the
1418 * button that the user used to pop up the menu, we won't receive then --
1419 * in particular, the EnterNotify when the menu pops up under the pointer.
1421 * If we are grabbing on a parent menu shell, no problem; just grab on
1422 * that menu shell first before popping up the window with owner_events = TRUE.
1424 * When grabbing on the menu itself, things get more convuluted - we
1425 * we do an explicit grab on a specially created window with
1426 * owner_events = TRUE, which we override further down with a grab
1427 * on the menu. (We can't grab on the menu until it is mapped; we
1428 * probably could just leave the grab on the other window, with a
1429 * little reorganization of the code in gtkmenu*).
1431 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1432 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1434 if (xgrab_shell && xgrab_shell != widget)
1436 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1437 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1441 GdkWindow *transfer_window;
1443 xgrab_shell = widget;
1444 transfer_window = menu_grab_transfer_window_get (menu);
1445 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1446 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1449 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1451 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1452 * with a stuck up window, we just abort here. Presumably the user will
1455 menu_shell->parent_menu_shell = NULL;
1456 menu_grab_transfer_window_destroy (menu);
1460 menu_shell->active = TRUE;
1461 menu_shell->button = button;
1463 /* If we are popping up the menu from something other than, a button
1464 * press then, as a heuristic, we ignore enter events for the menu
1465 * until we get a MOTION_NOTIFY.
1468 current_event = gtk_get_current_event ();
1471 if ((current_event->type != GDK_BUTTON_PRESS) &&
1472 (current_event->type != GDK_ENTER_NOTIFY))
1473 menu_shell->ignore_enter = TRUE;
1475 gdk_event_free (current_event);
1478 menu_shell->ignore_enter = TRUE;
1482 gtk_menu_tearoff_bg_copy (menu);
1484 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1487 parent_toplevel = NULL;
1488 if (parent_menu_shell)
1489 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1490 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1492 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1494 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1497 /* Set transient for to get the right window group and parent relationship */
1498 if (GTK_IS_WINDOW (parent_toplevel))
1499 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1500 GTK_WINDOW (parent_toplevel));
1502 menu->parent_menu_item = parent_menu_item;
1503 menu->position_func = func;
1504 menu->position_func_data = data;
1505 menu_shell->activate_time = activate_time;
1507 /* We need to show the menu here rather in the init function because
1508 * code expects to be able to tell if the menu is onscreen by
1509 * looking at the GTK_WIDGET_VISIBLE (menu)
1511 gtk_widget_show (GTK_WIDGET (menu));
1513 /* Position the menu, possibly changing the size request
1515 gtk_menu_position (menu);
1517 /* Compute the size of the toplevel and realize it so we
1518 * can scroll correctly.
1521 GtkRequisition tmp_request;
1522 GtkAllocation tmp_allocation = { 0, };
1524 gtk_widget_size_request (menu->toplevel, &tmp_request);
1526 tmp_allocation.width = tmp_request.width;
1527 tmp_allocation.height = tmp_request.height;
1529 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1531 gtk_widget_realize (GTK_WIDGET (menu));
1534 gtk_menu_scroll_to (menu, menu->scroll_offset);
1536 /* if no item is selected, select the first one */
1537 if (!menu_shell->active_menu_item)
1539 gboolean touchscreen_mode;
1541 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1542 "gtk-touchscreen-mode", &touchscreen_mode,
1545 if (touchscreen_mode)
1546 gtk_menu_shell_select_first (menu_shell, TRUE);
1549 /* Once everything is set up correctly, map the toplevel window on
1552 gtk_widget_show (menu->toplevel);
1554 if (xgrab_shell == widget)
1555 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1556 gtk_grab_add (GTK_WIDGET (menu));
1560 gtk_menu_popdown (GtkMenu *menu)
1562 GtkMenuPrivate *private;
1563 GtkMenuShell *menu_shell;
1565 g_return_if_fail (GTK_IS_MENU (menu));
1567 menu_shell = GTK_MENU_SHELL (menu);
1568 private = gtk_menu_get_private (menu);
1570 menu_shell->parent_menu_shell = NULL;
1571 menu_shell->active = FALSE;
1572 menu_shell->ignore_enter = FALSE;
1574 private->have_position = FALSE;
1576 gtk_menu_stop_scrolling (menu);
1578 gtk_menu_stop_navigating_submenu (menu);
1580 if (menu_shell->active_menu_item)
1582 if (menu->old_active_menu_item)
1583 g_object_unref (menu->old_active_menu_item);
1584 menu->old_active_menu_item = menu_shell->active_menu_item;
1585 g_object_ref (menu->old_active_menu_item);
1588 gtk_menu_shell_deselect (menu_shell);
1590 /* The X Grab, if present, will automatically be removed when we hide
1592 gtk_widget_hide (menu->toplevel);
1593 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1597 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1599 if (GTK_BIN (menu->toplevel)->child)
1601 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1605 /* We popped up the menu from the tearoff, so we need to
1606 * release the grab - we aren't actually hiding the menu.
1608 if (menu_shell->have_xgrab)
1610 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1612 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1613 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1617 /* gtk_menu_popdown is called each time a menu item is selected from
1618 * a torn off menu. Only scroll back to the saved position if the
1619 * non-tearoff menu was popped down.
1621 if (!menu->tearoff_active)
1622 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1623 menu->tearoff_active = TRUE;
1626 gtk_widget_hide (GTK_WIDGET (menu));
1628 menu_shell->have_xgrab = FALSE;
1629 gtk_grab_remove (GTK_WIDGET (menu));
1631 menu_grab_transfer_window_destroy (menu);
1635 gtk_menu_get_active (GtkMenu *menu)
1640 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1642 if (!menu->old_active_menu_item)
1645 children = GTK_MENU_SHELL (menu)->children;
1649 child = children->data;
1650 children = children->next;
1652 if (GTK_BIN (child)->child)
1657 menu->old_active_menu_item = child;
1658 if (menu->old_active_menu_item)
1659 g_object_ref (menu->old_active_menu_item);
1662 return menu->old_active_menu_item;
1666 gtk_menu_set_active (GtkMenu *menu,
1672 g_return_if_fail (GTK_IS_MENU (menu));
1674 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1677 child = tmp_list->data;
1678 if (GTK_BIN (child)->child)
1680 if (menu->old_active_menu_item)
1681 g_object_unref (menu->old_active_menu_item);
1682 menu->old_active_menu_item = child;
1683 g_object_ref (menu->old_active_menu_item);
1689 gtk_menu_set_accel_group (GtkMenu *menu,
1690 GtkAccelGroup *accel_group)
1692 g_return_if_fail (GTK_IS_MENU (menu));
1694 if (menu->accel_group != accel_group)
1696 if (menu->accel_group)
1697 g_object_unref (menu->accel_group);
1698 menu->accel_group = accel_group;
1699 if (menu->accel_group)
1700 g_object_ref (menu->accel_group);
1701 _gtk_menu_refresh_accel_paths (menu, TRUE);
1706 gtk_menu_get_accel_group (GtkMenu *menu)
1708 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1710 return menu->accel_group;
1714 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1717 /* Menu items chain here to figure whether they can activate their
1718 * accelerators. Unlike ordinary widgets, menus allow accel
1719 * activation even if invisible since that's the usual case for
1720 * submenus/popup-menus. however, the state of the attach widget
1721 * affects the "activeness" of the menu.
1723 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1726 return gtk_widget_can_activate_accel (awidget, signal_id);
1728 return GTK_WIDGET_IS_SENSITIVE (widget);
1732 * gtk_menu_set_accel_path
1733 * @menu: a valid #GtkMenu
1734 * @accel_path: a valid accelerator path
1736 * Sets an accelerator path for this menu from which accelerator paths
1737 * for its immediate children, its menu items, can be constructed.
1738 * The main purpose of this function is to spare the programmer the
1739 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1740 * each menu item that should support runtime user changable accelerators.
1741 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1742 * each menu item of this menu, that contains a label describing its purpose,
1743 * automatically gets an accel path assigned. For example, a menu containing
1744 * menu items "New" and "Exit", will, after
1745 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1746 * has been called, assign its items the accel paths:
1747 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1748 * Assigning accel paths to menu items then enables the user to change
1749 * their accelerators at runtime. More details about accelerator paths
1750 * and their default setups can be found at gtk_accel_map_add_entry().
1752 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1753 * pass a static string, you can save some memory by interning it first with
1754 * g_intern_static_string().
1757 gtk_menu_set_accel_path (GtkMenu *menu,
1758 const gchar *accel_path)
1760 g_return_if_fail (GTK_IS_MENU (menu));
1762 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1764 /* FIXME: accel_path should be defined as const gchar* */
1765 menu->accel_path = (gchar*)g_intern_string (accel_path);
1766 if (menu->accel_path)
1767 _gtk_menu_refresh_accel_paths (menu, FALSE);
1771 * gtk_menu_get_accel_path
1772 * @menu: a valid #GtkMenu
1774 * Retrieves the accelerator path set on the menu.
1779 gtk_menu_get_accel_path (GtkMenu *menu)
1781 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1783 return menu->accel_path;
1788 gboolean group_changed;
1792 refresh_accel_paths_foreach (GtkWidget *widget,
1795 AccelPropagation *prop = data;
1797 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1798 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1799 prop->menu->accel_path,
1800 prop->menu->accel_group,
1801 prop->group_changed);
1805 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1806 gboolean group_changed)
1808 g_return_if_fail (GTK_IS_MENU (menu));
1810 if (menu->accel_path && menu->accel_group)
1812 AccelPropagation prop;
1815 prop.group_changed = group_changed;
1816 gtk_container_foreach (GTK_CONTAINER (menu),
1817 refresh_accel_paths_foreach,
1823 gtk_menu_reposition (GtkMenu *menu)
1825 g_return_if_fail (GTK_IS_MENU (menu));
1827 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1828 gtk_menu_position (menu);
1832 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1835 g_return_if_fail (GTK_IS_MENU (menu));
1837 if (adjustment->value != menu->scroll_offset)
1838 gtk_menu_scroll_to (menu, adjustment->value);
1842 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1845 GdkGeometry geometry_hints;
1847 if (!menu->tearoff_window)
1850 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1852 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1853 width += menu->tearoff_scrollbar->requisition.width;
1856 geometry_hints.min_width = width;
1857 geometry_hints.max_width = width;
1859 geometry_hints.min_height = 0;
1860 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1862 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1865 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1869 gtk_menu_update_title (GtkMenu *menu)
1871 if (menu->tearoff_window)
1874 GtkWidget *attach_widget;
1876 title = gtk_menu_get_title (menu);
1879 attach_widget = gtk_menu_get_attach_widget (menu);
1880 if (GTK_IS_MENU_ITEM (attach_widget))
1882 GtkWidget *child = GTK_BIN (attach_widget)->child;
1883 if (GTK_IS_LABEL (child))
1884 title = gtk_label_get_text (GTK_LABEL (child));
1889 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1894 gtk_menu_get_toplevel (GtkWidget *menu)
1896 GtkWidget *attach, *toplevel;
1898 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1900 if (GTK_IS_MENU_ITEM (attach))
1901 attach = attach->parent;
1903 if (GTK_IS_MENU (attach))
1904 return gtk_menu_get_toplevel (attach);
1905 else if (GTK_IS_WIDGET (attach))
1907 toplevel = gtk_widget_get_toplevel (attach);
1908 if (GTK_WIDGET_TOPLEVEL (toplevel))
1916 tearoff_window_destroyed (GtkWidget *widget,
1919 gtk_menu_set_tearoff_state (menu, FALSE);
1923 gtk_menu_set_tearoff_state (GtkMenu *menu,
1928 g_return_if_fail (GTK_IS_MENU (menu));
1930 if (menu->torn_off != torn_off)
1932 menu->torn_off = torn_off;
1933 menu->tearoff_active = torn_off;
1937 if (GTK_WIDGET_VISIBLE (menu))
1938 gtk_menu_popdown (menu);
1940 if (!menu->tearoff_window)
1942 GtkWidget *toplevel;
1944 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
1945 "type", GTK_WINDOW_TOPLEVEL,
1946 "screen", gtk_widget_get_screen (menu->toplevel),
1947 "app-paintable", TRUE,
1950 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1951 GDK_WINDOW_TYPE_HINT_MENU);
1952 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1953 g_signal_connect (menu->tearoff_window, "destroy",
1954 G_CALLBACK (tearoff_window_destroyed), menu);
1955 g_signal_connect (menu->tearoff_window, "event",
1956 G_CALLBACK (gtk_menu_window_event), menu);
1958 gtk_menu_update_title (menu);
1960 gtk_widget_realize (menu->tearoff_window);
1962 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1963 if (toplevel != NULL)
1964 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1965 GTK_WINDOW (toplevel));
1967 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1968 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1970 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1971 menu->tearoff_adjustment =
1972 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1974 GTK_WIDGET (menu)->requisition.height,
1978 g_object_connect (menu->tearoff_adjustment,
1979 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1981 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1983 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1984 menu->tearoff_scrollbar,
1987 if (menu->tearoff_adjustment->upper > height)
1988 gtk_widget_show (menu->tearoff_scrollbar);
1990 gtk_widget_show (menu->tearoff_hbox);
1993 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1995 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1997 /* Update menu->requisition
1999 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2001 gtk_menu_set_tearoff_hints (menu, width);
2003 gtk_widget_realize (menu->tearoff_window);
2004 gtk_menu_position (menu);
2006 gtk_widget_show (GTK_WIDGET (menu));
2007 gtk_widget_show (menu->tearoff_window);
2009 gtk_menu_scroll_to (menu, 0);
2014 gtk_widget_hide (GTK_WIDGET (menu));
2015 gtk_widget_hide (menu->tearoff_window);
2016 if (GTK_IS_CONTAINER (menu->toplevel))
2017 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2018 gtk_widget_destroy (menu->tearoff_window);
2020 menu->tearoff_window = NULL;
2021 menu->tearoff_hbox = NULL;
2022 menu->tearoff_scrollbar = NULL;
2023 menu->tearoff_adjustment = NULL;
2026 g_object_notify (G_OBJECT (menu), "tearoff-state");
2031 * gtk_menu_get_tearoff_state:
2034 * Returns whether the menu is torn off. See
2035 * gtk_menu_set_tearoff_state ().
2037 * Return value: %TRUE if the menu is currently torn off.
2040 gtk_menu_get_tearoff_state (GtkMenu *menu)
2042 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2044 return menu->torn_off;
2048 * gtk_menu_set_title:
2050 * @title: a string containing the title for the menu.
2052 * Sets the title string for the menu. The title is displayed when the menu
2053 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2054 * attached to a parent menu item, and if so it will try to use the same text as
2055 * that menu item's label.
2058 gtk_menu_set_title (GtkMenu *menu,
2061 GtkMenuPrivate *priv;
2064 g_return_if_fail (GTK_IS_MENU (menu));
2066 priv = gtk_menu_get_private (menu);
2068 old_title = priv->title;
2069 priv->title = g_strdup (title);
2072 gtk_menu_update_title (menu);
2073 g_object_notify (G_OBJECT (menu), "tearoff-title");
2077 * gtk_menu_get_title:
2080 * Returns the title of the menu. See gtk_menu_set_title().
2082 * Return value: the title of the menu, or %NULL if the menu has no
2083 * title set on it. This string is owned by the widget and should
2084 * not be modified or freed.
2086 G_CONST_RETURN gchar *
2087 gtk_menu_get_title (GtkMenu *menu)
2089 GtkMenuPrivate *priv;
2091 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2093 priv = gtk_menu_get_private (menu);
2099 gtk_menu_reorder_child (GtkMenu *menu,
2103 GtkMenuShell *menu_shell;
2105 g_return_if_fail (GTK_IS_MENU (menu));
2106 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2108 menu_shell = GTK_MENU_SHELL (menu);
2110 if (g_list_find (menu_shell->children, child))
2112 menu_shell->children = g_list_remove (menu_shell->children, child);
2113 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2115 menu_queue_resize (menu);
2120 gtk_menu_style_set (GtkWidget *widget,
2121 GtkStyle *previous_style)
2123 if (GTK_WIDGET_REALIZED (widget))
2125 GtkMenu *menu = GTK_MENU (widget);
2127 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2128 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2129 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2134 get_arrows_border (GtkMenu *menu, GtkBorder *border)
2136 guint scroll_arrow_height;
2138 gtk_widget_style_get (GTK_WIDGET (menu),
2139 "scroll-arrow-vlength", &scroll_arrow_height,
2142 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2143 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2145 border->left = border->right = 0;
2149 gtk_menu_realize (GtkWidget *widget)
2151 GdkWindowAttr attributes;
2152 gint attributes_mask;
2157 guint vertical_padding;
2158 guint horizontal_padding;
2159 GtkBorder arrow_border;
2161 g_return_if_fail (GTK_IS_MENU (widget));
2163 menu = GTK_MENU (widget);
2165 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2167 attributes.window_type = GDK_WINDOW_CHILD;
2168 attributes.x = widget->allocation.x;
2169 attributes.y = widget->allocation.y;
2170 attributes.width = widget->allocation.width;
2171 attributes.height = widget->allocation.height;
2172 attributes.wclass = GDK_INPUT_OUTPUT;
2173 attributes.visual = gtk_widget_get_visual (widget);
2174 attributes.colormap = gtk_widget_get_colormap (widget);
2176 attributes.event_mask = gtk_widget_get_events (widget);
2178 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2179 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2181 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2182 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2183 gdk_window_set_user_data (widget->window, widget);
2185 border_width = GTK_CONTAINER (widget)->border_width;
2187 gtk_widget_style_get (GTK_WIDGET (menu),
2188 "vertical-padding", &vertical_padding,
2189 "horizontal-padding", &horizontal_padding,
2192 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2193 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2194 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2195 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2197 get_arrows_border (menu, &arrow_border);
2198 attributes.y += arrow_border.top;
2199 attributes.height -= arrow_border.top;
2200 attributes.height -= arrow_border.bottom;
2202 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2203 gdk_window_set_user_data (menu->view_window, menu);
2207 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2208 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2210 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2211 gdk_window_set_user_data (menu->bin_window, menu);
2213 children = GTK_MENU_SHELL (menu)->children;
2216 child = children->data;
2217 children = children->next;
2219 gtk_widget_set_parent_window (child, menu->bin_window);
2222 widget->style = gtk_style_attach (widget->style, widget->window);
2223 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2224 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2225 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2227 if (GTK_MENU_SHELL (widget)->active_menu_item)
2228 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2229 GTK_MENU_SHELL (widget)->active_menu_item);
2231 gdk_window_show (menu->bin_window);
2232 gdk_window_show (menu->view_window);
2236 gtk_menu_focus (GtkWidget *widget,
2237 GtkDirectionType direction)
2240 * A menu or its menu items cannot have focus
2245 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2248 menu_grab_transfer_window_get (GtkMenu *menu)
2250 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2253 GdkWindowAttr attributes;
2254 gint attributes_mask;
2256 attributes.x = -100;
2257 attributes.y = -100;
2258 attributes.width = 10;
2259 attributes.height = 10;
2260 attributes.window_type = GDK_WINDOW_TEMP;
2261 attributes.wclass = GDK_INPUT_ONLY;
2262 attributes.override_redirect = TRUE;
2263 attributes.event_mask = 0;
2265 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2267 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2268 &attributes, attributes_mask);
2269 gdk_window_set_user_data (window, menu);
2271 gdk_window_show (window);
2273 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2280 menu_grab_transfer_window_destroy (GtkMenu *menu)
2282 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2285 gdk_window_set_user_data (window, NULL);
2286 gdk_window_destroy (window);
2287 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2292 gtk_menu_unrealize (GtkWidget *widget)
2294 GtkMenu *menu = GTK_MENU (widget);
2296 menu_grab_transfer_window_destroy (menu);
2298 gdk_window_set_user_data (menu->view_window, NULL);
2299 gdk_window_destroy (menu->view_window);
2300 menu->view_window = NULL;
2302 gdk_window_set_user_data (menu->bin_window, NULL);
2303 gdk_window_destroy (menu->bin_window);
2304 menu->bin_window = NULL;
2306 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize) (widget);
2310 gtk_menu_size_request (GtkWidget *widget,
2311 GtkRequisition *requisition)
2315 GtkMenuShell *menu_shell;
2318 guint max_toggle_size;
2319 guint max_accel_width;
2320 guint vertical_padding;
2321 guint horizontal_padding;
2322 GtkRequisition child_requisition;
2323 GtkMenuPrivate *priv;
2325 g_return_if_fail (GTK_IS_MENU (widget));
2326 g_return_if_fail (requisition != NULL);
2328 menu = GTK_MENU (widget);
2329 menu_shell = GTK_MENU_SHELL (widget);
2330 priv = gtk_menu_get_private (menu);
2332 requisition->width = 0;
2333 requisition->height = 0;
2335 max_toggle_size = 0;
2336 max_accel_width = 0;
2338 g_free (priv->heights);
2339 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2340 priv->heights_length = gtk_menu_get_n_rows (menu);
2342 children = menu_shell->children;
2349 child = children->data;
2350 children = children->next;
2352 if (! GTK_WIDGET_VISIBLE (child))
2355 get_effective_child_attach (child, &l, &r, &t, &b);
2357 /* It's important to size_request the child
2358 * before doing the toggle size request, in
2359 * case the toggle size request depends on the size
2360 * request of a child of the child (e.g. for ImageMenuItem)
2363 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2364 gtk_widget_size_request (child, &child_requisition);
2366 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2367 max_toggle_size = MAX (max_toggle_size, toggle_size);
2368 max_accel_width = MAX (max_accel_width,
2369 GTK_MENU_ITEM (child)->accelerator_width);
2371 part = child_requisition.width / (r - l);
2372 requisition->width = MAX (requisition->width, part);
2374 part = MAX (child_requisition.height, toggle_size) / (b - t);
2375 priv->heights[t] = MAX (priv->heights[t], part);
2378 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2379 requisition->height += priv->heights[i];
2381 requisition->width += max_toggle_size + max_accel_width;
2382 requisition->width *= gtk_menu_get_n_columns (menu);
2384 gtk_widget_style_get (GTK_WIDGET (menu),
2385 "vertical-padding", &vertical_padding,
2386 "horizontal-padding", &horizontal_padding,
2389 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2390 widget->style->xthickness) * 2;
2391 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2392 widget->style->ythickness) * 2;
2394 menu->toggle_size = max_toggle_size;
2396 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2398 if (menu->tearoff_active)
2399 gtk_menu_set_tearoff_hints (menu, requisition->width);
2403 gtk_menu_size_allocate (GtkWidget *widget,
2404 GtkAllocation *allocation)
2407 GtkMenuShell *menu_shell;
2409 GtkAllocation child_allocation;
2410 GtkRequisition child_requisition;
2411 GtkMenuPrivate *priv;
2415 guint vertical_padding;
2416 guint horizontal_padding;
2418 g_return_if_fail (GTK_IS_MENU (widget));
2419 g_return_if_fail (allocation != NULL);
2421 menu = GTK_MENU (widget);
2422 menu_shell = GTK_MENU_SHELL (widget);
2423 priv = gtk_menu_get_private (menu);
2425 widget->allocation = *allocation;
2426 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2428 gtk_widget_style_get (GTK_WIDGET (menu),
2429 "vertical-padding", &vertical_padding,
2430 "horizontal-padding", &horizontal_padding,
2433 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2434 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2436 width = MAX (1, allocation->width - x * 2);
2437 height = MAX (1, allocation->height - y * 2);
2439 child_requisition.width -= x * 2;
2440 child_requisition.height -= y * 2;
2442 if (menu_shell->active)
2443 gtk_menu_scroll_to (menu, menu->scroll_offset);
2445 if (!menu->tearoff_active)
2447 GtkBorder arrow_border;
2449 get_arrows_border (menu, &arrow_border);
2450 y += arrow_border.top;
2451 height -= arrow_border.top;
2452 height -= arrow_border.bottom;
2455 if (GTK_WIDGET_REALIZED (widget))
2457 gdk_window_move_resize (widget->window,
2458 allocation->x, allocation->y,
2459 allocation->width, allocation->height);
2461 gdk_window_move_resize (menu->view_window,
2468 if (menu_shell->children)
2470 gint base_width = width / gtk_menu_get_n_columns (menu);
2472 children = menu_shell->children;
2475 child = children->data;
2476 children = children->next;
2478 if (GTK_WIDGET_VISIBLE (child))
2483 get_effective_child_attach (child, &l, &r, &t, &b);
2485 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2488 tmp = gtk_menu_get_n_columns (menu) - l;
2489 l = gtk_menu_get_n_columns (menu) - r;
2493 child_allocation.width = (r - l) * base_width;
2494 child_allocation.height = 0;
2495 child_allocation.x = l * base_width;
2496 child_allocation.y = 0;
2498 for (i = 0; i < b; i++)
2501 child_allocation.y += priv->heights[i];
2503 child_allocation.height += priv->heights[i];
2506 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2509 gtk_widget_size_allocate (child, &child_allocation);
2510 gtk_widget_queue_draw (child);
2514 /* Resize the item window */
2515 if (GTK_WIDGET_REALIZED (widget))
2521 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2522 height += priv->heights[i];
2524 width = gtk_menu_get_n_columns (menu) * base_width;
2525 gdk_window_resize (menu->bin_window, width, height);
2528 if (menu->tearoff_active)
2530 if (allocation->height >= widget->requisition.height)
2532 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2534 gtk_widget_hide (menu->tearoff_scrollbar);
2535 gtk_menu_set_tearoff_hints (menu, allocation->width);
2537 gtk_menu_scroll_to (menu, 0);
2542 menu->tearoff_adjustment->upper = widget->requisition.height;
2543 menu->tearoff_adjustment->page_size = allocation->height;
2545 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2546 menu->tearoff_adjustment->upper)
2549 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2552 gtk_menu_scroll_to (menu, value);
2555 gtk_adjustment_changed (menu->tearoff_adjustment);
2557 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2559 gtk_widget_show (menu->tearoff_scrollbar);
2560 gtk_menu_set_tearoff_hints (menu, allocation->width);
2568 get_arrows_visible_area (GtkMenu *menu,
2569 GdkRectangle *border,
2570 GdkRectangle *upper,
2571 GdkRectangle *lower,
2574 GtkWidget *widget = GTK_WIDGET (menu);
2575 guint vertical_padding;
2576 guint horizontal_padding;
2577 gint scroll_arrow_height;
2579 gtk_widget_style_get (widget,
2580 "vertical-padding", &vertical_padding,
2581 "horizontal-padding", &horizontal_padding,
2582 "scroll-arrow-vlength", &scroll_arrow_height,
2585 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2586 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2587 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2589 upper->x = border->x;
2590 upper->y = border->y;
2591 upper->width = border->width - 2 * border->x;
2592 upper->height = scroll_arrow_height;
2594 lower->x = border->x;
2595 lower->y = border->height - border->y - scroll_arrow_height;
2596 lower->width = border->width - 2 * border->x;
2597 lower->height = scroll_arrow_height;
2599 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2603 gtk_menu_paint (GtkWidget *widget,
2604 GdkEventExpose *event)
2607 GtkMenuPrivate *priv;
2608 GdkRectangle border;
2613 g_return_if_fail (GTK_IS_MENU (widget));
2615 menu = GTK_MENU (widget);
2616 priv = gtk_menu_get_private (menu);
2618 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2620 if (event->window == widget->window)
2622 gint arrow_size = 0.7 * arrow_space;
2624 gtk_paint_box (widget->style,
2628 &event->area, widget, "menu",
2631 if (menu->upper_arrow_visible && !menu->tearoff_active)
2633 gtk_paint_box (widget->style,
2635 priv->upper_arrow_state,
2637 &event->area, widget, "menu_scroll_arrow_up",
2643 gtk_paint_arrow (widget->style,
2645 priv->upper_arrow_state,
2647 &event->area, widget, "menu_scroll_arrow_up",
2650 upper.x + (upper.width - arrow_size) / 2,
2651 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2652 arrow_size, arrow_size);
2655 if (menu->lower_arrow_visible && !menu->tearoff_active)
2657 gtk_paint_box (widget->style,
2659 priv->lower_arrow_state,
2661 &event->area, widget, "menu_scroll_arrow_down",
2667 gtk_paint_arrow (widget->style,
2669 priv->lower_arrow_state,
2671 &event->area, widget, "menu_scroll_arrow_down",
2674 lower.x + (lower.width - arrow_size) / 2,
2675 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2676 arrow_size, arrow_size);
2679 else if (event->window == menu->bin_window)
2681 gint y = -border.y + menu->scroll_offset;
2683 if (!menu->tearoff_active)
2685 GtkBorder arrow_border;
2687 get_arrows_border (menu, &arrow_border);
2688 y -= arrow_border.top;
2691 gtk_paint_box (widget->style,
2695 &event->area, widget, "menu",
2697 border.width, border.height);
2702 gtk_menu_expose (GtkWidget *widget,
2703 GdkEventExpose *event)
2705 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2706 g_return_val_if_fail (event != NULL, FALSE);
2708 if (GTK_WIDGET_DRAWABLE (widget))
2710 gtk_menu_paint (widget, event);
2712 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event) (widget, event);
2719 gtk_menu_show (GtkWidget *widget)
2721 GtkMenu *menu = GTK_MENU (widget);
2723 _gtk_menu_refresh_accel_paths (menu, FALSE);
2725 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2729 gtk_menu_button_scroll (GtkMenu *menu,
2730 GdkEventButton *event)
2732 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2734 gboolean touchscreen_mode;
2736 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
2737 "gtk-touchscreen-mode", &touchscreen_mode,
2740 if (touchscreen_mode)
2741 gtk_menu_handle_scrolling (menu,
2742 event->x_root, event->y_root,
2743 event->type == GDK_BUTTON_PRESS,
2753 pointer_in_menu_window (GtkWidget *widget,
2757 GtkMenu *menu = GTK_MENU (widget);
2759 if (GTK_WIDGET_MAPPED (menu->toplevel))
2761 GtkMenuShell *menu_shell;
2762 gint window_x, window_y;
2764 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
2766 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
2767 y_root >= window_y && y_root < window_y + widget->allocation.height)
2770 menu_shell = GTK_MENU_SHELL (widget);
2772 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
2773 return pointer_in_menu_window (menu_shell->parent_menu_shell,
2781 gtk_menu_button_press (GtkWidget *widget,
2782 GdkEventButton *event)
2784 if (event->type != GDK_BUTTON_PRESS)
2787 /* Don't pass down to menu shell for presses over scroll arrows
2789 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2792 /* Don't pass down to menu shell if a non-menuitem part of the menu
2793 * was clicked. The check for the event_widget being a GtkMenuShell
2794 * works because we have the pointer grabbed on menu_shell->window
2795 * with owner_events=TRUE, so all events that are either outside
2796 * the menu or on its border are delivered relative to
2797 * menu_shell->window.
2799 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2800 pointer_in_menu_window (widget, event->x_root, event->y_root))
2803 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2807 gtk_menu_button_release (GtkWidget *widget,
2808 GdkEventButton *event)
2810 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2812 if (priv->ignore_button_release)
2814 priv->ignore_button_release = FALSE;
2818 if (event->type != GDK_BUTTON_RELEASE)
2821 /* Don't pass down to menu shell for releases over scroll arrows
2823 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2826 /* Don't pass down to menu shell if a non-menuitem part of the menu
2827 * was clicked (see comment in button_press()).
2829 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2830 pointer_in_menu_window (widget, event->x_root, event->y_root))
2832 /* Ugly: make sure menu_shell->button gets reset to 0 when we
2833 * bail out early here so it is in a consistent state for the
2834 * next button_press/button_release in GtkMenuShell.
2837 if (GTK_MENU_SHELL (widget)->active)
2838 GTK_MENU_SHELL (widget)->button = 0;
2843 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
2846 static const gchar *
2847 get_accel_path (GtkWidget *menu_item,
2852 GClosure *accel_closure;
2853 GtkAccelGroup *accel_group;
2855 path = _gtk_widget_get_accel_path (menu_item, locked);
2858 path = GTK_MENU_ITEM (menu_item)->accel_path;
2864 label = GTK_BIN (menu_item)->child;
2866 if (GTK_IS_ACCEL_LABEL (label))
2868 g_object_get (label,
2869 "accel-closure", &accel_closure,
2873 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2875 *locked = accel_group->lock_count > 0;
2885 gtk_menu_key_press (GtkWidget *widget,
2888 GtkMenuShell *menu_shell;
2890 gboolean delete = FALSE;
2891 gboolean can_change_accels;
2892 gchar *accel = NULL;
2893 guint accel_key, accel_mods;
2894 GdkModifierType consumed_modifiers;
2895 GdkDisplay *display;
2897 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2898 g_return_val_if_fail (event != NULL, FALSE);
2900 menu_shell = GTK_MENU_SHELL (widget);
2901 menu = GTK_MENU (widget);
2903 gtk_menu_stop_navigating_submenu (menu);
2905 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
2908 display = gtk_widget_get_display (widget);
2910 g_object_get (gtk_widget_get_settings (widget),
2911 "gtk-menu-bar-accel", &accel,
2912 "gtk-can-change-accels", &can_change_accels,
2915 if (accel && *accel)
2918 GdkModifierType mods = 0;
2920 gtk_accelerator_parse (accel, &keyval, &mods);
2923 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2925 /* FIXME this is wrong, needs to be in the global accel resolution
2926 * thing, to properly consider i18n etc., but that probably requires
2927 * AccelGroup changes etc.
2929 if (event->keyval == keyval && (mods & event->state) == mods)
2931 gtk_menu_shell_cancel (menu_shell);
2939 switch (event->keyval)
2950 /* Figure out what modifiers went into determining the key symbol */
2951 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2952 event->hardware_keycode, event->state, event->group,
2953 NULL, NULL, NULL, &consumed_modifiers);
2955 accel_key = gdk_keyval_to_lower (event->keyval);
2956 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2958 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2959 * We re-upper case when we match against the keyval, but display and save in caseless form.
2961 if (accel_key != event->keyval)
2962 accel_mods |= GDK_SHIFT_MASK;
2964 /* Modify the accelerators */
2965 if (can_change_accels &&
2966 menu_shell->active_menu_item &&
2967 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2968 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2969 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2971 GtkWidget *menu_item = menu_shell->active_menu_item;
2972 gboolean locked, replace_accels = TRUE;
2975 path = get_accel_path (menu_item, &locked);
2976 if (!path || locked)
2978 /* can't change accelerators on menu_items without paths
2979 * (basically, those items are accelerator-locked).
2981 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2982 gtk_widget_error_bell (widget);
2988 /* For the keys that act to delete the current setting, we delete
2989 * the current setting if there is one, otherwise, we set the
2990 * key as the accelerator.
2996 if (gtk_accel_map_lookup_entry (path, &key) &&
2997 (key.accel_key || key.accel_mods))
3003 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3007 /* we failed, probably because this key is in use and
3010 /* g_print("failed to change\n"); */
3011 gtk_widget_error_bell (widget);
3020 check_threshold (GtkWidget *widget,
3021 int start_x, int start_y,
3027 ABS (start_x - x) > THRESHOLD ||
3028 ABS (start_y - y) > THRESHOLD;
3032 definitely_within_item (GtkWidget *widget,
3036 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3039 gdk_drawable_get_size (window, &w, &h);
3042 check_threshold (widget, 0, 0, x, y) &&
3043 check_threshold (widget, w - 1, 0, x, y) &&
3044 check_threshold (widget, w - 1, h - 1, x, y) &&
3045 check_threshold (widget, 0, h - 1, x, y);
3049 gtk_menu_motion_notify (GtkWidget *widget,
3050 GdkEventMotion *event)
3052 GtkWidget *menu_item;
3054 GtkMenuShell *menu_shell;
3056 gboolean need_enter;
3058 if (GTK_IS_MENU (widget))
3060 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3062 if (priv->ignore_button_release)
3063 priv->ignore_button_release = FALSE;
3065 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3069 /* We received the event for one of two reasons:
3071 * a) We are the active menu, and did gtk_grab_add()
3072 * b) The widget is a child of ours, and the event was propagated
3074 * Since for computation of navigation regions, we want the menu which
3075 * is the parent of the menu item, for a), we need to find that menu,
3076 * which may be different from 'widget'.
3078 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3079 if (!GTK_IS_MENU_ITEM (menu_item) ||
3080 !GTK_IS_MENU (menu_item->parent))
3083 menu_shell = GTK_MENU_SHELL (menu_item->parent);
3084 menu = GTK_MENU (menu_shell);
3086 if (definitely_within_item (menu_item, event->x, event->y))
3087 menu_shell->activate_time = 0;
3089 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
3091 /* Check to see if we are within an active submenu's navigation region
3093 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3096 /* Make sure we pop down if we enter a non-selectable menu item, so we
3097 * don't show a submenu when the cursor is outside the stay-up triangle.
3099 if (!_gtk_menu_item_is_selectable (menu_item))
3101 gtk_menu_shell_deselect (menu_shell);
3107 /* The menu is now sensitive to enter events on its items, but
3108 * was previously sensitive. So we fake an enter event.
3112 menu_shell->ignore_enter = FALSE;
3114 gdk_drawable_get_size (event->window, &width, &height);
3115 if (event->x >= 0 && event->x < width &&
3116 event->y >= 0 && event->y < height)
3118 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3121 send_event->crossing.window = g_object_ref (event->window);
3122 send_event->crossing.time = event->time;
3123 send_event->crossing.send_event = TRUE;
3124 send_event->crossing.x_root = event->x_root;
3125 send_event->crossing.y_root = event->y_root;
3126 send_event->crossing.x = event->x;
3127 send_event->crossing.y = event->y;
3128 send_event->crossing.state = event->state;
3130 /* We send the event to 'widget', the currently active menu,
3131 * instead of 'menu', the menu that the pointer is in. This
3132 * will ensure that the event will be ignored unless the
3133 * menuitem is a child of the active menu or some parent
3134 * menu of the active menu.
3136 result = gtk_widget_event (widget, send_event);
3137 gdk_event_free (send_event);
3147 get_double_arrows (GtkMenu *menu)
3149 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3150 gboolean double_arrows;
3152 gtk_widget_style_get (GTK_WIDGET (menu),
3153 "double-arrows", &double_arrows,
3156 return double_arrows || (priv->initially_pushed_in &&
3157 menu->scroll_offset != 0);
3161 gtk_menu_scroll_by (GtkMenu *menu,
3166 gint view_width, view_height;
3167 gboolean double_arrows;
3168 GtkBorder arrow_border;
3170 widget = GTK_WIDGET (menu);
3171 offset = menu->scroll_offset + step;
3173 get_arrows_border (menu, &arrow_border);
3175 double_arrows = get_double_arrows (menu);
3177 /* If we scroll upward and the non-visible top part
3178 * is smaller than the scroll arrow it would be
3179 * pretty stupid to show the arrow and taking more
3180 * screen space than just scrolling to the top.
3183 if ((step < 0) && (offset < arrow_border.top))
3186 /* Don't scroll over the top if we weren't before: */
3187 if ((menu->scroll_offset >= 0) && (offset < 0))
3190 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3192 if (menu->scroll_offset == 0 &&
3193 view_height >= widget->requisition.height)
3196 /* Don't scroll past the bottom if we weren't before: */
3197 if (menu->scroll_offset > 0)
3198 view_height -= arrow_border.top;
3200 /* When both arrows are always shown, reduce
3201 * view height even more.
3204 view_height -= arrow_border.bottom;
3206 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3207 (offset + view_height > widget->requisition.height))
3208 offset = widget->requisition.height - view_height;
3210 if (offset != menu->scroll_offset)
3211 gtk_menu_scroll_to (menu, offset);
3215 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3216 gboolean touchscreen_mode)
3218 gboolean upper_visible;
3219 gboolean lower_visible;
3221 upper_visible = menu->upper_arrow_visible;
3222 lower_visible = menu->lower_arrow_visible;
3224 gtk_menu_scroll_by (menu, menu->scroll_step);
3226 if (touchscreen_mode &&
3227 (upper_visible != menu->upper_arrow_visible ||
3228 lower_visible != menu->lower_arrow_visible))
3230 /* We are about to hide a scroll arrow while the mouse is pressed,
3231 * this would cause the uncovered menu item to be activated on button
3232 * release. Therefore we need to ignore button release here
3234 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3235 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3240 gtk_menu_scroll_timeout (gpointer data)
3243 gboolean touchscreen_mode;
3245 menu = GTK_MENU (data);
3247 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3248 "gtk-touchscreen-mode", &touchscreen_mode,
3251 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3257 gtk_menu_scroll_timeout_initial (gpointer data)
3261 gboolean touchscreen_mode;
3263 menu = GTK_MENU (data);
3265 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3266 "gtk-timeout-repeat", &timeout,
3267 "gtk-touchscreen-mode", &touchscreen_mode,
3270 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3272 gtk_menu_remove_scroll_timeout (menu);
3274 menu->timeout_id = gdk_threads_add_timeout (timeout,
3275 gtk_menu_scroll_timeout,
3282 gtk_menu_start_scrolling (GtkMenu *menu)
3285 gboolean touchscreen_mode;
3287 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3288 "gtk-timeout-repeat", &timeout,
3289 "gtk-touchscreen-mode", &touchscreen_mode,
3292 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3294 menu->timeout_id = gdk_threads_add_timeout (timeout,
3295 gtk_menu_scroll_timeout_initial,
3300 gtk_menu_scroll (GtkWidget *widget,
3301 GdkEventScroll *event)
3303 GtkMenu *menu = GTK_MENU (widget);
3305 switch (event->direction)
3307 case GDK_SCROLL_RIGHT:
3308 case GDK_SCROLL_DOWN:
3309 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3311 case GDK_SCROLL_LEFT:
3313 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3321 get_arrows_sensitive_area (GtkMenu *menu,
3322 GdkRectangle *upper,
3323 GdkRectangle *lower)
3327 guint vertical_padding;
3329 gint scroll_arrow_height;
3331 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3333 gtk_widget_style_get (GTK_WIDGET (menu),
3334 "vertical-padding", &vertical_padding,
3335 "scroll-arrow-vlength", &scroll_arrow_height,
3338 border = GTK_CONTAINER (menu)->border_width +
3339 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3341 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3347 upper->width = width;
3348 upper->height = scroll_arrow_height + border;
3354 lower->y = win_y + height - border - scroll_arrow_height;
3355 lower->width = width;
3356 lower->height = scroll_arrow_height + border;
3362 gtk_menu_handle_scrolling (GtkMenu *menu,
3368 GtkMenuShell *menu_shell;
3369 GtkMenuPrivate *priv;
3372 gboolean scroll_fast = FALSE;
3374 gboolean touchscreen_mode;
3376 priv = gtk_menu_get_private (menu);
3378 menu_shell = GTK_MENU_SHELL (menu);
3380 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3381 "gtk-touchscreen-mode", &touchscreen_mode,
3384 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3388 /* upper arrow handling */
3390 get_arrows_sensitive_area (menu, &rect, NULL);
3393 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3394 (x >= rect.x) && (x < rect.x + rect.width) &&
3395 (y >= rect.y) && (y < rect.y + rect.height))
3400 if (touchscreen_mode)
3401 menu->upper_arrow_prelight = in_arrow;
3403 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3405 gboolean arrow_pressed = FALSE;
3407 if (menu->upper_arrow_visible && !menu->tearoff_active)
3409 if (touchscreen_mode)
3411 if (enter && menu->upper_arrow_prelight)
3413 if (menu->timeout_id == 0)
3415 /* Deselect the active item so that
3416 * any submenus are popped down
3418 gtk_menu_shell_deselect (menu_shell);
3420 gtk_menu_remove_scroll_timeout (menu);
3421 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3425 /* Only do stuff on click. */
3426 gtk_menu_start_scrolling (menu);
3427 arrow_pressed = TRUE;
3432 arrow_pressed = TRUE;
3437 gtk_menu_stop_scrolling (menu);
3440 else /* !touchscreen_mode */
3442 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3444 if (enter && in_arrow &&
3445 (!menu->upper_arrow_prelight ||
3446 menu->scroll_fast != scroll_fast))
3448 menu->upper_arrow_prelight = TRUE;
3449 menu->scroll_fast = scroll_fast;
3451 /* Deselect the active item so that
3452 * any submenus are popped down
3454 gtk_menu_shell_deselect (menu_shell);
3456 gtk_menu_remove_scroll_timeout (menu);
3457 menu->scroll_step = scroll_fast ?
3458 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3461 gdk_threads_add_timeout (scroll_fast ?
3462 MENU_SCROLL_TIMEOUT2 :
3463 MENU_SCROLL_TIMEOUT1,
3464 gtk_menu_scroll_timeout, menu);
3466 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3468 gtk_menu_stop_scrolling (menu);
3473 /* gtk_menu_start_scrolling() might have hit the top of the
3474 * menu, so check if the button isn't insensitive before
3475 * changing it to something else.
3477 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3479 GtkStateType arrow_state = GTK_STATE_NORMAL;
3482 arrow_state = GTK_STATE_ACTIVE;
3483 else if (menu->upper_arrow_prelight)
3484 arrow_state = GTK_STATE_PRELIGHT;
3486 if (arrow_state != priv->upper_arrow_state)
3488 priv->upper_arrow_state = arrow_state;
3490 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3496 /* lower arrow handling */
3498 get_arrows_sensitive_area (menu, NULL, &rect);
3501 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3502 (x >= rect.x) && (x < rect.x + rect.width) &&
3503 (y >= rect.y) && (y < rect.y + rect.height))
3508 if (touchscreen_mode)
3509 menu->lower_arrow_prelight = in_arrow;
3511 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3513 gboolean arrow_pressed = FALSE;
3515 if (menu->lower_arrow_visible && !menu->tearoff_active)
3517 if (touchscreen_mode)
3519 if (enter && menu->lower_arrow_prelight)
3521 if (menu->timeout_id == 0)
3523 /* Deselect the active item so that
3524 * any submenus are popped down
3526 gtk_menu_shell_deselect (menu_shell);
3528 gtk_menu_remove_scroll_timeout (menu);
3529 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3533 /* Only do stuff on click. */
3534 gtk_menu_start_scrolling (menu);
3535 arrow_pressed = TRUE;
3540 arrow_pressed = TRUE;
3545 gtk_menu_stop_scrolling (menu);
3548 else /* !touchscreen_mode */
3550 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3552 if (enter && in_arrow &&
3553 (!menu->lower_arrow_prelight ||
3554 menu->scroll_fast != scroll_fast))
3556 menu->lower_arrow_prelight = TRUE;
3557 menu->scroll_fast = scroll_fast;
3559 /* Deselect the active item so that
3560 * any submenus are popped down
3562 gtk_menu_shell_deselect (menu_shell);
3564 gtk_menu_remove_scroll_timeout (menu);
3565 menu->scroll_step = scroll_fast ?
3566 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3569 gdk_threads_add_timeout (scroll_fast ?
3570 MENU_SCROLL_TIMEOUT2 :
3571 MENU_SCROLL_TIMEOUT1,
3572 gtk_menu_scroll_timeout, menu);
3574 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3576 gtk_menu_stop_scrolling (menu);
3581 /* gtk_menu_start_scrolling() might have hit the bottom of the
3582 * menu, so check if the button isn't insensitive before
3583 * changing it to something else.
3585 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3587 GtkStateType arrow_state = GTK_STATE_NORMAL;
3590 arrow_state = GTK_STATE_ACTIVE;
3591 else if (menu->lower_arrow_prelight)
3592 arrow_state = GTK_STATE_PRELIGHT;
3594 if (arrow_state != priv->lower_arrow_state)
3596 priv->lower_arrow_state = arrow_state;
3598 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3606 gtk_menu_enter_notify (GtkWidget *widget,
3607 GdkEventCrossing *event)
3609 GtkWidget *menu_item;
3610 gboolean touchscreen_mode;
3612 g_object_get (gtk_widget_get_settings (widget),
3613 "gtk-touchscreen-mode", &touchscreen_mode,
3616 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3617 if (GTK_IS_MENU (widget))
3619 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3621 if (!menu_shell->ignore_enter)
3622 gtk_menu_handle_scrolling (GTK_MENU (widget),
3623 event->x_root, event->y_root, TRUE, TRUE);
3626 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3628 GtkWidget *menu = menu_item->parent;
3630 if (GTK_IS_MENU (menu))
3632 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3633 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3635 if (priv->seen_item_enter)
3637 /* This is the second enter we see for an item
3638 * on this menu. This means a release should always
3641 menu_shell->activate_time = 0;
3643 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3644 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3646 if (definitely_within_item (menu_item, event->x, event->y))
3648 /* This is an actual user-enter (ie. not a pop-under)
3649 * In this case, the user must either have entered
3650 * sufficiently far enough into the item, or he must move
3651 * far enough away from the enter point. (see
3652 * gtk_menu_motion_notify())
3654 menu_shell->activate_time = 0;
3658 priv->seen_item_enter = TRUE;
3662 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3663 * will not correspond to the event widget's parent. Check to see
3664 * if we are in the parent's navigation region.
3666 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3667 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3668 event->x_root, event->y_root))
3671 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3675 gtk_menu_leave_notify (GtkWidget *widget,
3676 GdkEventCrossing *event)
3678 GtkMenuShell *menu_shell;
3680 GtkMenuItem *menu_item;
3681 GtkWidget *event_widget;
3683 menu = GTK_MENU (widget);
3684 menu_shell = GTK_MENU_SHELL (widget);
3686 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3689 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3691 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3693 if (!GTK_IS_MENU_ITEM (event_widget))
3696 menu_item = GTK_MENU_ITEM (event_widget);
3698 /* Here we check to see if we're leaving an active menu item with a submenu,
3699 * in which case we enter submenu navigation mode.
3701 if (menu_shell->active_menu_item != NULL
3702 && menu_item->submenu != NULL
3703 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3705 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3707 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3710 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3712 /* We are leaving an active menu item with nonactive submenu.
3713 * Deselect it so we don't surprise the user with by popping
3714 * up a submenu _after_ he left the item.
3716 gtk_menu_shell_deselect (menu_shell);
3721 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3725 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3727 if (menu->navigation_region)
3729 gdk_region_destroy (menu->navigation_region);
3730 menu->navigation_region = NULL;
3732 if (menu->navigation_timeout)
3734 g_source_remove (menu->navigation_timeout);
3735 menu->navigation_timeout = 0;
3739 /* When the timeout is elapsed, the navigation region is destroyed
3740 * and the menuitem under the pointer (if any) is selected.
3743 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3745 GtkMenu *menu = user_data;
3746 GdkWindow *child_window;
3748 gtk_menu_stop_navigating_submenu (menu);
3750 if (GTK_WIDGET_REALIZED (menu))
3752 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3756 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3758 send_event->crossing.window = g_object_ref (child_window);
3759 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3760 send_event->crossing.send_event = TRUE;
3762 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3764 gdk_event_free (send_event);
3772 gtk_menu_navigating_submenu (GtkMenu *menu,
3776 if (menu->navigation_region)
3778 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3782 gtk_menu_stop_navigating_submenu (menu);
3789 #undef DRAW_STAY_UP_TRIANGLE
3791 #ifdef DRAW_STAY_UP_TRIANGLE
3794 draw_stay_up_triangle (GdkWindow *window,
3797 /* Draw ugly color all over the stay-up triangle */
3798 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3799 GdkGCValues gc_values;
3801 GdkRectangle clipbox;
3803 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3804 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3805 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3806 gdk_gc_set_clip_region (ugly_gc, region);
3808 gdk_region_get_clipbox (region, &clipbox);
3810 gdk_draw_rectangle (window,
3813 clipbox.x, clipbox.y,
3814 clipbox.width, clipbox.height);
3816 g_object_unref (ugly_gc);
3821 flip_region (GdkRegion *region,
3826 GdkRectangle *rectangles;
3827 GdkRectangle clipbox;
3828 GdkRegion *new_region;
3831 new_region = gdk_region_new ();
3833 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3834 gdk_region_get_clipbox (region, &clipbox);
3836 for (i = 0; i < n_rectangles; ++i)
3838 GdkRectangle rect = rectangles[i];
3841 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3844 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3846 gdk_region_union_with_rect (new_region, &rect);
3849 g_free (rectangles);
3855 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3856 GtkMenuItem *menu_item,
3857 GdkEventCrossing *event)
3859 gint submenu_left = 0;
3860 gint submenu_right = 0;
3861 gint submenu_top = 0;
3862 gint submenu_bottom = 0;
3866 GtkWidget *event_widget;
3868 g_return_if_fail (menu_item->submenu != NULL);
3869 g_return_if_fail (event != NULL);
3871 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3873 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3874 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3876 submenu_right = submenu_left + width;
3877 submenu_bottom = submenu_top + height;
3879 gdk_drawable_get_size (event_widget->window, &width, &height);
3881 if (event->x >= 0 && event->x < width)
3884 gboolean flip_y = FALSE;
3885 gboolean flip_x = FALSE;
3887 gtk_menu_stop_navigating_submenu (menu);
3889 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3892 point[0].x = event->x_root;
3893 point[1].x = submenu_left;
3898 point[0].x = event->x_root + 1;
3899 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3907 point[0].y = event->y_root + 1;
3908 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3910 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3918 point[0].y = event->y_root;
3919 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3921 if (point[0].y >= submenu_bottom)
3925 point[2].x = point[1].x;
3926 point[2].y = point[0].y;
3928 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3930 if (flip_x || flip_y)
3932 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3933 gdk_region_destroy (menu->navigation_region);
3934 menu->navigation_region = new_region;
3937 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3938 "gtk-menu-popdown-delay", &popdown_delay,
3941 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
3942 gtk_menu_stop_navigating_submenu_cb,
3945 #ifdef DRAW_STAY_UP_TRIANGLE
3946 draw_stay_up_triangle (gdk_get_default_root_window(),
3947 menu->navigation_region);
3953 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3957 g_return_if_fail (GTK_IS_MENU (menu_shell));
3959 parent = menu_shell->parent_menu_shell;
3961 menu_shell->activate_time = 0;
3962 gtk_menu_popdown (GTK_MENU (menu_shell));
3965 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3969 gtk_menu_position (GtkMenu *menu)
3972 GtkRequisition requisition;
3973 GtkMenuPrivate *private;
3978 GdkScreen *pointer_screen;
3979 GdkRectangle monitor;
3981 g_return_if_fail (GTK_IS_MENU (menu));
3983 widget = GTK_WIDGET (menu);
3985 screen = gtk_widget_get_screen (widget);
3986 gdk_display_get_pointer (gdk_screen_get_display (screen),
3987 &pointer_screen, &x, &y, NULL);
3989 /* We need the requisition to figure out the right place to
3990 * popup the menu. In fact, we always need to ask here, since
3991 * if a size_request was queued while we weren't popped up,
3992 * the requisition won't have been recomputed yet.
3994 gtk_widget_size_request (widget, &requisition);
3996 if (pointer_screen != screen)
3998 /* Pointer is on a different screen; roughly center the
3999 * menu on the screen. If someone was using multiscreen
4000 * + Xinerama together they'd probably want something
4001 * fancier; but that is likely to be vanishingly rare.
4003 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4004 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4007 private = gtk_menu_get_private (menu);
4008 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4010 private->initially_pushed_in = FALSE;
4012 /* Set the type hint here to allow custom position functions to set a different hint */
4013 if (!GTK_WIDGET_VISIBLE (menu->toplevel))
4014 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4016 if (menu->position_func)
4018 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4019 menu->position_func_data);
4020 if (private->monitor_num < 0)
4021 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4023 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4027 gint space_left, space_right, space_above, space_below;
4030 gint xthickness = widget->style->xthickness;
4031 gint ythickness = widget->style->ythickness;
4032 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4034 /* The placement of popup menus horizontally works like this (with
4035 * RTL in parentheses)
4037 * - If there is enough room to the right (left) of the mouse cursor,
4038 * position the menu there.
4040 * - Otherwise, if if there is enough room to the left (right) of the
4041 * mouse cursor, position the menu there.
4043 * - Otherwise if the menu is smaller than the monitor, position it
4044 * on the side of the mouse cursor that has the most space available
4046 * - Otherwise (if there is simply not enough room for the menu on the
4047 * monitor), position it as far left (right) as possible.
4049 * Positioning in the vertical direction is similar: first try below
4050 * mouse cursor, then above.
4052 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4054 space_left = x - monitor.x;
4055 space_right = monitor.x + monitor.width - x - 1;
4056 space_above = y - monitor.y;
4057 space_below = monitor.y + monitor.height - y - 1;
4059 /* position horizontally */
4061 /* the amount of space we need to position the menu. Note the
4062 * menu is offset "xthickness" pixels
4064 needed_width = requisition.width - xthickness;
4066 if (needed_width <= space_left ||
4067 needed_width <= space_right)
4069 if ((rtl && needed_width <= space_left) ||
4070 (!rtl && needed_width > space_right))
4073 x = x + xthickness - requisition.width + 1;
4077 /* position right */
4081 /* x is clamped on-screen further down */
4083 else if (requisition.width <= monitor.width)
4085 /* the menu is too big to fit on either side of the mouse
4086 * cursor, but smaller than the monitor. Position it on
4087 * the side that has the most space
4089 if (space_left > space_right)
4097 x = monitor.x + monitor.width - requisition.width;
4100 else /* menu is simply too big for the monitor */
4105 x = monitor.x + monitor.width - requisition.width;
4114 /* Position vertically. The algorithm is the same as above, but
4115 * simpler because we don't have to take RTL into account.
4117 needed_height = requisition.height - ythickness;
4119 if (needed_height <= space_above ||
4120 needed_height <= space_below)
4122 if (needed_height <= space_below)
4125 y = y + ythickness - requisition.height + 1;
4127 y = CLAMP (y, monitor.y,
4128 monitor.y + monitor.height - requisition.height);
4130 else if (needed_height > space_below && needed_height > space_above)
4132 if (space_below >= space_above)
4133 y = monitor.y + monitor.height - requisition.height;
4145 if (private->initially_pushed_in)
4147 menu_height = GTK_WIDGET (menu)->requisition.height;
4149 if (y + menu_height > monitor.y + monitor.height)
4151 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4152 y = (monitor.y + monitor.height) - menu_height;
4157 scroll_offset += monitor.y - y;
4162 /* FIXME: should this be done in the various position_funcs ? */
4163 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4165 if (GTK_MENU_SHELL (menu)->active)
4167 private->have_position = TRUE;
4172 if (y + requisition.height > monitor.y + monitor.height)
4173 requisition.height = (monitor.y + monitor.height) - y;
4177 scroll_offset += monitor.y - y;
4178 requisition.height -= monitor.y - y;
4182 if (scroll_offset > 0)
4184 GtkBorder arrow_border;
4186 get_arrows_border (menu, &arrow_border);
4187 scroll_offset += arrow_border.top;
4190 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4193 if (!GTK_MENU_SHELL (menu)->active)
4195 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4196 requisition.width, requisition.height);
4199 menu->scroll_offset = scroll_offset;
4203 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4205 if (menu->timeout_id)
4207 g_source_remove (menu->timeout_id);
4208 menu->timeout_id = 0;
4213 gtk_menu_stop_scrolling (GtkMenu *menu)
4215 gboolean touchscreen_mode;
4217 gtk_menu_remove_scroll_timeout (menu);
4219 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4220 "gtk-touchscreen-mode", &touchscreen_mode,
4223 if (!touchscreen_mode)
4225 menu->upper_arrow_prelight = FALSE;
4226 menu->lower_arrow_prelight = FALSE;
4231 gtk_menu_scroll_to (GtkMenu *menu,
4236 gint view_width, view_height;
4239 guint vertical_padding;
4240 guint horizontal_padding;
4241 gboolean double_arrows;
4242 GtkBorder arrow_border;
4244 widget = GTK_WIDGET (menu);
4246 if (menu->tearoff_active &&
4247 menu->tearoff_adjustment &&
4248 (menu->tearoff_adjustment->value != offset))
4250 menu->tearoff_adjustment->value =
4252 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4253 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4256 /* Move/resize the viewport according to arrows: */
4257 view_width = widget->allocation.width;
4258 view_height = widget->allocation.height;
4260 gtk_widget_style_get (GTK_WIDGET (menu),
4261 "vertical-padding", &vertical_padding,
4262 "horizontal-padding", &horizontal_padding,
4265 double_arrows = get_double_arrows (menu);
4267 border_width = GTK_CONTAINER (menu)->border_width;
4268 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4269 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4270 menu_height = widget->requisition.height -
4271 (border_width + widget->style->ythickness + vertical_padding) * 2;
4273 x = border_width + widget->style->xthickness + horizontal_padding;
4274 y = border_width + widget->style->ythickness + vertical_padding;
4276 if (double_arrows && !menu->tearoff_active)
4278 if (view_height < menu_height ||
4279 (offset > 0 && menu->scroll_offset > 0) ||
4280 (offset < 0 && menu->scroll_offset < 0))
4282 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4283 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4284 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4286 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4287 gtk_widget_queue_draw (GTK_WIDGET (menu));
4289 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4291 get_arrows_border (menu, &arrow_border);
4292 y += arrow_border.top;
4293 view_height -= arrow_border.top;
4294 view_height -= arrow_border.bottom;
4297 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4298 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4299 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4300 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4302 if (offset >= menu_height - view_height)
4303 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4304 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4305 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4306 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4308 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4309 (priv->lower_arrow_state != lower_arrow_previous_state))
4310 gtk_widget_queue_draw (GTK_WIDGET (menu));
4312 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4313 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4315 /* At the upper border, possibly remove timeout */
4316 if (menu->scroll_step < 0)
4318 gtk_menu_stop_scrolling (menu);
4319 gtk_widget_queue_draw (GTK_WIDGET (menu));
4323 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4324 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4326 /* At the lower border, possibly remove timeout */
4327 if (menu->scroll_step > 0)
4329 gtk_menu_stop_scrolling (menu);
4330 gtk_widget_queue_draw (GTK_WIDGET (menu));
4334 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4338 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4339 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4341 gtk_menu_stop_scrolling (menu);
4342 gtk_widget_queue_draw (GTK_WIDGET (menu));
4345 else if (!menu->tearoff_active)
4347 gboolean last_visible;
4349 last_visible = menu->upper_arrow_visible;
4350 menu->upper_arrow_visible = offset > 0;
4352 /* upper_arrow_visible may have changed, so requery the border */
4353 get_arrows_border (menu, &arrow_border);
4354 view_height -= arrow_border.top;
4356 if ((last_visible != menu->upper_arrow_visible) &&
4357 !menu->upper_arrow_visible)
4359 menu->upper_arrow_prelight = FALSE;
4361 /* If we hid the upper arrow, possibly remove timeout */
4362 if (menu->scroll_step < 0)
4364 gtk_menu_stop_scrolling (menu);
4365 gtk_widget_queue_draw (GTK_WIDGET (menu));
4369 last_visible = menu->lower_arrow_visible;
4370 menu->lower_arrow_visible = offset < menu_height - view_height;
4372 /* lower_arrow_visible may have changed, so requery the border */
4373 get_arrows_border (menu, &arrow_border);
4374 view_height -= arrow_border.bottom;
4376 if ((last_visible != menu->lower_arrow_visible) &&
4377 !menu->lower_arrow_visible)
4379 menu->lower_arrow_prelight = FALSE;
4381 /* If we hid the lower arrow, possibly remove timeout */
4382 if (menu->scroll_step > 0)
4384 gtk_menu_stop_scrolling (menu);
4385 gtk_widget_queue_draw (GTK_WIDGET (menu));
4389 y += arrow_border.top;
4392 /* Scroll the menu: */
4393 if (GTK_WIDGET_REALIZED (menu))
4394 gdk_window_move (menu->bin_window, 0, -offset);
4396 if (GTK_WIDGET_REALIZED (menu))
4397 gdk_window_move_resize (menu->view_window,
4403 menu->scroll_offset = offset;
4407 compute_child_offset (GtkMenu *menu,
4408 GtkWidget *menu_item,
4411 gboolean *is_last_child)
4413 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4414 gint item_top_attach;
4415 gint item_bottom_attach;
4416 gint child_offset = 0;
4419 get_effective_child_attach (menu_item, NULL, NULL,
4420 &item_top_attach, &item_bottom_attach);
4422 /* there is a possibility that we get called before _size_request, so
4423 * check the height table for safety.
4425 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4428 /* when we have a row with only invisible children, it's height will
4429 * be zero, so there's no need to check WIDGET_VISIBLE here
4431 for (i = 0; i < item_top_attach; i++)
4432 child_offset += priv->heights[i];
4435 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4437 *offset = child_offset;
4439 *height = priv->heights[item_top_attach];
4445 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4446 GtkWidget *menu_item)
4449 gint child_offset, child_height;
4453 gboolean last_child = 0;
4455 menu = GTK_MENU (menu_shell);
4457 /* We need to check if the selected item fully visible.
4458 * If not we need to scroll the menu so that it becomes fully
4462 if (compute_child_offset (menu, menu_item,
4463 &child_offset, &child_height, &last_child))
4465 guint vertical_padding;
4466 gboolean double_arrows;
4468 y = menu->scroll_offset;
4469 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4471 gtk_widget_style_get (GTK_WIDGET (menu),
4472 "vertical-padding", &vertical_padding,
4475 double_arrows = get_double_arrows (menu);
4477 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4479 if (child_offset < y)
4481 /* Ignore the enter event we might get if the pointer is on the menu
4483 menu_shell->ignore_enter = TRUE;
4484 gtk_menu_scroll_to (menu, child_offset);
4488 GtkBorder arrow_border;
4492 get_arrows_border (menu, &arrow_border);
4493 if (!menu->tearoff_active)
4494 arrow_height = arrow_border.top + arrow_border.bottom;
4496 if (child_offset + child_height > y + height - arrow_height)
4499 if ((!last_child && !menu->tearoff_active) || double_arrows)
4500 arrow_height += arrow_border.bottom;
4502 y = child_offset + child_height - height + arrow_height;
4503 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4505 /* Need upper arrow */
4506 arrow_height += arrow_border.top;
4507 y = child_offset + child_height - height + arrow_height;
4509 /* Ignore the enter event we might get if the pointer is on the menu
4511 menu_shell->ignore_enter = TRUE;
4512 gtk_menu_scroll_to (menu, y);
4520 gtk_menu_select_item (GtkMenuShell *menu_shell,
4521 GtkWidget *menu_item)
4523 GtkMenu *menu = GTK_MENU (menu_shell);
4525 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4526 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4528 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4532 /* Reparent the menu, taking care of the refcounting
4534 * If unrealize is true we force a unrealize while reparenting the parent.
4535 * This can help eliminate flicker in some cases.
4537 * What happens is that when the menu is unrealized and then re-realized,
4538 * the allocations are as follows:
4540 * parent - 1x1 at (0,0)
4541 * child1 - 100x20 at (0,0)
4542 * child2 - 100x20 at (0,20)
4543 * child3 - 100x20 at (0,40)
4545 * That is, the parent is small but the children are full sized. Then,
4546 * when the queued_resize gets processed, the parent gets resized to
4549 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4550 * contains the following logic:
4552 * - if a move or resize operation on a window would change the clip
4553 * region on the children, then before the window is resized
4554 * the background for children is temporarily set to None, the
4555 * move/resize done, and the background for the children restored.
4557 * So, at the point where the parent is resized to final size, the
4558 * background for the children is temporarily None, and thus they
4559 * are not cleared to the background color and the previous background
4560 * (the image of the menu) is left in place.
4563 gtk_menu_reparent (GtkMenu *menu,
4564 GtkWidget *new_parent,
4567 GtkObject *object = GTK_OBJECT (menu);
4568 GtkWidget *widget = GTK_WIDGET (menu);
4569 gboolean was_floating = g_object_is_floating (object);
4571 g_object_ref_sink (object);
4575 g_object_ref (object);
4576 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4577 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4578 g_object_unref (object);
4581 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4584 g_object_force_floating (G_OBJECT (object));
4586 g_object_unref (object);
4590 gtk_menu_show_all (GtkWidget *widget)
4592 /* Show children, but not self. */
4593 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4598 gtk_menu_hide_all (GtkWidget *widget)
4600 /* Hide children, but not self. */
4601 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4605 * gtk_menu_set_screen:
4606 * @menu: a #GtkMenu.
4607 * @screen: a #GdkScreen, or %NULL if the screen should be
4608 * determined by the widget the menu is attached to.
4610 * Sets the #GdkScreen on which the menu will be displayed.
4615 gtk_menu_set_screen (GtkMenu *menu,
4618 g_return_if_fail (GTK_IS_MENU (menu));
4619 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4621 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4625 menu_change_screen (menu, screen);
4629 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4631 attach_widget_screen_changed (attach_widget, NULL, menu);
4637 * @menu: a #GtkMenu.
4638 * @child: a #GtkMenuItem.
4639 * @left_attach: The column number to attach the left side of the item to.
4640 * @right_attach: The column number to attach the right side of the item to.
4641 * @top_attach: The row number to attach the top of the item to.
4642 * @bottom_attach: The row number to attach the bottom of the item to.
4644 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4645 * an item will occupy is specified by @left_attach, @right_attach,
4646 * @top_attach and @bottom_attach. These each represent the leftmost,
4647 * rightmost, uppermost and lower column and row numbers of the table.
4648 * (Columns and rows are indexed from zero).
4650 * Note that this function is not related to gtk_menu_detach().
4655 gtk_menu_attach (GtkMenu *menu,
4660 guint bottom_attach)
4662 GtkMenuShell *menu_shell;
4664 g_return_if_fail (GTK_IS_MENU (menu));
4665 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4666 g_return_if_fail (child->parent == NULL ||
4667 child->parent == GTK_WIDGET (menu));
4668 g_return_if_fail (left_attach < right_attach);
4669 g_return_if_fail (top_attach < bottom_attach);
4671 menu_shell = GTK_MENU_SHELL (menu);
4675 AttachInfo *ai = get_attach_info (child);
4677 ai->left_attach = left_attach;
4678 ai->right_attach = right_attach;
4679 ai->top_attach = top_attach;
4680 ai->bottom_attach = bottom_attach;
4682 menu_shell->children = g_list_append (menu_shell->children, child);
4684 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4686 menu_queue_resize (menu);
4690 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4691 "left-attach", left_attach,
4692 "right-attach", right_attach,
4693 "top-attach", top_attach,
4694 "bottom-attach", bottom_attach,
4700 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4704 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4705 "gtk-menu-popup-delay", &popup_delay,
4712 find_child_containing (GtkMenuShell *menu_shell,
4720 /* find a child which includes the area given by
4721 * left, right, top, bottom.
4724 for (list = menu_shell->children; list; list = list->next)
4728 if (!_gtk_menu_item_is_selectable (list->data))
4731 get_effective_child_attach (list->data, &l, &r, &t, &b);
4733 if (l <= left && right <= r
4734 && t <= top && bottom <= b)
4735 return GTK_WIDGET (list->data);
4742 gtk_menu_move_current (GtkMenuShell *menu_shell,
4743 GtkMenuDirectionType direction)
4745 GtkMenu *menu = GTK_MENU (menu_shell);
4748 GtkWidget *match = NULL;
4750 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4754 case GTK_MENU_DIR_CHILD:
4755 direction = GTK_MENU_DIR_PARENT;
4757 case GTK_MENU_DIR_PARENT:
4758 direction = GTK_MENU_DIR_CHILD;
4764 /* use special table menu key bindings */
4765 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4767 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4769 if (direction == GTK_MENU_DIR_NEXT)
4771 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4773 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4781 for (i = 0; i < t; i++)
4783 match = find_child_containing (menu_shell,
4784 l, l + 1, i, i + 1);
4790 else if (direction == GTK_MENU_DIR_PREV)
4792 for (i = t; i > 0; i--)
4794 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4802 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4804 match = find_child_containing (menu_shell,
4805 l, l + 1, i - 1, i);
4811 else if (direction == GTK_MENU_DIR_PARENT)
4813 /* we go one left if possible */
4815 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4819 GtkWidget *parent = menu_shell->parent_menu_shell;
4822 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4823 match = menu_shell->active_menu_item;
4826 else if (direction == GTK_MENU_DIR_CHILD)
4828 /* we go one right if possible */
4829 if (r < gtk_menu_get_n_columns (menu))
4830 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4834 GtkWidget *parent = menu_shell->parent_menu_shell;
4836 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4838 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4839 match = menu_shell->active_menu_item;
4845 gtk_menu_shell_select_item (menu_shell, match);
4850 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
4854 get_visible_size (GtkMenu *menu)
4856 GtkWidget *widget = GTK_WIDGET (menu);
4857 GtkContainer *container = GTK_CONTAINER (menu);
4859 gint menu_height = (widget->allocation.height
4860 - 2 * (container->border_width
4861 + widget->style->ythickness));
4863 if (!menu->tearoff_active)
4865 GtkBorder arrow_border;
4867 get_arrows_border (menu, &arrow_border);
4868 menu_height -= arrow_border.top;
4869 menu_height -= arrow_border.bottom;
4875 /* Find the sensitive on-screen child containing @y, or if none,
4876 * the nearest selectable onscreen child. (%NULL if none)
4879 child_at (GtkMenu *menu,
4882 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4883 GtkWidget *child = NULL;
4884 gint child_offset = 0;
4887 gint lower, upper; /* Onscreen bounds */
4889 menu_height = get_visible_size (menu);
4890 lower = menu->scroll_offset;
4891 upper = menu->scroll_offset + menu_height;
4893 for (children = menu_shell->children; children; children = children->next)
4895 if (GTK_WIDGET_VISIBLE (children->data))
4897 GtkRequisition child_requisition;
4899 gtk_widget_size_request (children->data, &child_requisition);
4901 if (_gtk_menu_item_is_selectable (children->data) &&
4902 child_offset >= lower &&
4903 child_offset + child_requisition.height <= upper)
4905 child = children->data;
4907 if (child_offset + child_requisition.height > y &&
4908 !GTK_IS_TEAROFF_MENU_ITEM (child))
4912 child_offset += child_requisition.height;
4920 get_menu_height (GtkMenu *menu)
4923 GtkWidget *widget = GTK_WIDGET (menu);
4925 height = widget->requisition.height;
4926 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4928 if (!menu->tearoff_active)
4930 GtkBorder arrow_border;
4932 get_arrows_border (menu, &arrow_border);
4933 height -= arrow_border.top;
4934 height -= arrow_border.bottom;
4941 gtk_menu_real_move_scroll (GtkMenu *menu,
4944 gint page_size = get_visible_size (menu);
4945 gint end_position = get_menu_height (menu);
4946 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4950 case GTK_SCROLL_PAGE_UP:
4951 case GTK_SCROLL_PAGE_DOWN:
4955 gint child_offset = 0;
4956 gboolean old_upper_arrow_visible;
4959 if (type == GTK_SCROLL_PAGE_UP)
4964 if (menu_shell->active_menu_item)
4968 compute_child_offset (menu, menu_shell->active_menu_item,
4969 &child_offset, &child_height, NULL);
4970 child_offset += child_height / 2;
4973 menu_shell->ignore_enter = TRUE;
4974 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4975 old_offset = menu->scroll_offset;
4977 new_offset = menu->scroll_offset + step;
4978 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4980 gtk_menu_scroll_to (menu, new_offset);
4982 if (menu_shell->active_menu_item)
4984 GtkWidget *new_child;
4985 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4986 GtkBorder arrow_border;
4988 get_arrows_border (menu, &arrow_border);
4990 if (menu->scroll_offset != old_offset)
4991 step = menu->scroll_offset - old_offset;
4993 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
4995 new_child = child_at (menu, child_offset + step);
4997 gtk_menu_shell_select_item (menu_shell, new_child);
5001 case GTK_SCROLL_START:
5002 /* Ignore the enter event we might get if the pointer is on the menu
5004 menu_shell->ignore_enter = TRUE;
5005 gtk_menu_scroll_to (menu, 0);
5006 gtk_menu_shell_select_first (menu_shell, TRUE);
5008 case GTK_SCROLL_END:
5009 /* Ignore the enter event we might get if the pointer is on the menu
5011 menu_shell->ignore_enter = TRUE;
5012 gtk_menu_scroll_to (menu, end_position - page_size);
5013 _gtk_menu_shell_select_last (menu_shell, TRUE);
5022 * gtk_menu_set_monitor:
5024 * @monitor_num: the number of the monitor on which the menu should
5027 * Informs GTK+ on which monitor a menu should be popped up.
5028 * See gdk_screen_get_monitor_geometry().
5030 * This function should be called from a #GtkMenuPositionFunc if the
5031 * menu should not appear on the same monitor as the pointer. This
5032 * information can't be reliably inferred from the coordinates returned
5033 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5034 * may extend beyond the monitor boundaries or even the screen boundaries.
5039 gtk_menu_set_monitor (GtkMenu *menu,
5042 GtkMenuPrivate *priv;
5043 g_return_if_fail (GTK_IS_MENU (menu));
5045 priv = gtk_menu_get_private (menu);
5047 priv->monitor_num = monitor_num;
5051 * gtk_menu_get_monitor:
5054 * Retrieves the number of the monitor on which to show the menu.
5056 * Returns: the number of the monitor on which the menu should
5057 * be popped up or -1
5062 gtk_menu_get_monitor (GtkMenu *menu)
5064 GtkMenuPrivate *priv;
5065 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5067 priv = gtk_menu_get_private (menu);
5069 return priv->monitor_num;
5073 * gtk_menu_get_for_attach_widget:
5074 * @widget: a #GtkWidget
5076 * Returns a list of the menus which are attached to this widget.
5077 * This list is owned by GTK+ and must not be modified.
5079 * Return value: the list of menus attached to his widget.
5084 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5088 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5090 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5095 gtk_menu_grab_notify (GtkWidget *widget,
5096 gboolean was_grabbed)
5098 GtkWidget *toplevel;
5099 GtkWindowGroup *group;
5102 toplevel = gtk_widget_get_toplevel (widget);
5103 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5104 grab = _gtk_window_group_get_current_grab (group);
5108 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5109 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5113 #define __GTK_MENU_C__
5114 #include "gtkaliasdef.c"