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.
496 * Since: GSEAL-branch
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.
511 * Since: GSEAL-branch
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.
526 * Since: GSEAL-branch
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.
541 * Since: GSEAL-branch
543 g_object_class_install_property (gobject_class,
545 g_param_spec_string ("attach-widget",
547 P_("The widget the menu is attached to"),
549 GTK_PARAM_READWRITE));
551 g_object_class_install_property (gobject_class,
553 g_param_spec_string ("tearoff-title",
555 P_("A title that may be displayed by the window manager when this menu is torn-off"),
557 GTK_PARAM_READWRITE));
560 * GtkMenu:tearoff-state:
562 * A boolean that indicates whether the menu is torn-off.
566 g_object_class_install_property (gobject_class,
568 g_param_spec_boolean ("tearoff-state",
570 P_("A boolean that indicates whether the menu is torn-off"),
572 GTK_PARAM_READWRITE));
577 * The monitor the menu will be popped up on.
579 * Since: GSEAL-branch
581 g_object_class_install_property (gobject_class,
583 g_param_spec_int ("monitor",
585 P_("The monitor the menu will be popped up on"),
587 GTK_PARAM_READWRITE));
589 gtk_widget_class_install_style_property (widget_class,
590 g_param_spec_int ("vertical-padding",
591 P_("Vertical Padding"),
592 P_("Extra space at the top and bottom of the menu"),
596 GTK_PARAM_READABLE));
598 gtk_widget_class_install_style_property (widget_class,
599 g_param_spec_int ("horizontal-padding",
600 P_("Horizontal Padding"),
601 P_("Extra space at the left and right edges of the menu"),
605 GTK_PARAM_READABLE));
607 gtk_widget_class_install_style_property (widget_class,
608 g_param_spec_int ("vertical-offset",
609 P_("Vertical Offset"),
610 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
614 GTK_PARAM_READABLE));
616 gtk_widget_class_install_style_property (widget_class,
617 g_param_spec_int ("horizontal-offset",
618 P_("Horizontal Offset"),
619 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
623 GTK_PARAM_READABLE));
625 gtk_widget_class_install_style_property (widget_class,
626 g_param_spec_boolean ("double-arrows",
628 P_("When scrolling, always show both arrows."),
630 GTK_PARAM_READABLE));
633 gtk_container_class_install_child_property (container_class,
634 CHILD_PROP_LEFT_ATTACH,
635 g_param_spec_int ("left-attach",
637 P_("The column number to attach the left side of the child to"),
639 GTK_PARAM_READWRITE));
641 gtk_container_class_install_child_property (container_class,
642 CHILD_PROP_RIGHT_ATTACH,
643 g_param_spec_int ("right-attach",
645 P_("The column number to attach the right side of the child to"),
647 GTK_PARAM_READWRITE));
649 gtk_container_class_install_child_property (container_class,
650 CHILD_PROP_TOP_ATTACH,
651 g_param_spec_int ("top-attach",
653 P_("The row number to attach the top of the child to"),
655 GTK_PARAM_READWRITE));
657 gtk_container_class_install_child_property (container_class,
658 CHILD_PROP_BOTTOM_ATTACH,
659 g_param_spec_int ("bottom-attach",
661 P_("The row number to attach the bottom of the child to"),
663 GTK_PARAM_READWRITE));
665 binding_set = gtk_binding_set_by_class (class);
666 gtk_binding_entry_add_signal (binding_set,
668 I_("move_current"), 1,
669 GTK_TYPE_MENU_DIRECTION_TYPE,
671 gtk_binding_entry_add_signal (binding_set,
674 GTK_TYPE_MENU_DIRECTION_TYPE,
676 gtk_binding_entry_add_signal (binding_set,
679 GTK_TYPE_MENU_DIRECTION_TYPE,
681 gtk_binding_entry_add_signal (binding_set,
684 GTK_TYPE_MENU_DIRECTION_TYPE,
686 gtk_binding_entry_add_signal (binding_set,
689 GTK_TYPE_MENU_DIRECTION_TYPE,
690 GTK_MENU_DIR_PARENT);
691 gtk_binding_entry_add_signal (binding_set,
694 GTK_TYPE_MENU_DIRECTION_TYPE,
695 GTK_MENU_DIR_PARENT);
696 gtk_binding_entry_add_signal (binding_set,
699 GTK_TYPE_MENU_DIRECTION_TYPE,
701 gtk_binding_entry_add_signal (binding_set,
704 GTK_TYPE_MENU_DIRECTION_TYPE,
706 gtk_binding_entry_add_signal (binding_set,
709 GTK_TYPE_SCROLL_TYPE,
711 gtk_binding_entry_add_signal (binding_set,
714 GTK_TYPE_SCROLL_TYPE,
716 gtk_binding_entry_add_signal (binding_set,
719 GTK_TYPE_SCROLL_TYPE,
721 gtk_binding_entry_add_signal (binding_set,
724 GTK_TYPE_SCROLL_TYPE,
726 gtk_binding_entry_add_signal (binding_set,
729 GTK_TYPE_SCROLL_TYPE,
731 gtk_binding_entry_add_signal (binding_set,
734 GTK_TYPE_SCROLL_TYPE,
736 gtk_binding_entry_add_signal (binding_set,
739 GTK_TYPE_SCROLL_TYPE,
740 GTK_SCROLL_PAGE_DOWN);
741 gtk_binding_entry_add_signal (binding_set,
744 GTK_TYPE_SCROLL_TYPE,
745 GTK_SCROLL_PAGE_DOWN);
747 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
748 P_("Can change accelerators"),
749 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
751 GTK_PARAM_READWRITE));
753 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
754 P_("Delay before submenus appear"),
755 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
759 GTK_PARAM_READWRITE));
761 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
762 P_("Delay before hiding a submenu"),
763 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
766 DEFAULT_POPDOWN_DELAY,
767 GTK_PARAM_READWRITE));
769 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
774 gtk_menu_set_property (GObject *object,
781 menu = GTK_MENU (object);
786 gtk_menu_set_active (menu, g_value_get_uint (value));
788 case PROP_ACCEL_GROUP:
789 gtk_menu_set_accel_group (menu, g_value_get_object (value));
791 case PROP_ACCEL_PATH:
792 gtk_menu_set_accel_path (menu, g_value_get_string (value));
794 case PROP_ATTACH_WIDGET:
795 gtk_menu_attach (menu, g_value_get_object (value), 0, 0, 0, 0);
797 case PROP_TEAROFF_STATE:
798 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
800 case PROP_TEAROFF_TITLE:
801 gtk_menu_set_title (menu, g_value_get_string (value));
804 gtk_menu_set_monitor (menu, g_value_get_int (value));
807 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
813 gtk_menu_get_property (GObject *object,
820 menu = GTK_MENU (object);
825 g_value_set_boolean (value, gtk_menu_get_active (menu));
827 case PROP_ACCEL_GROUP:
828 g_value_set_object (value, gtk_menu_get_accel_group (menu));
830 case PROP_ACCEL_PATH:
831 g_value_set_string (value, gtk_menu_get_accel_path (menu));
833 case PROP_ATTACH_WIDGET:
834 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
836 case PROP_TEAROFF_STATE:
837 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
839 case PROP_TEAROFF_TITLE:
840 g_value_set_string (value, gtk_menu_get_title (menu));
843 g_value_set_int (value, gtk_menu_get_monitor (menu));
846 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
852 gtk_menu_set_child_property (GtkContainer *container,
858 GtkMenu *menu = GTK_MENU (container);
859 AttachInfo *ai = get_attach_info (child);
863 case CHILD_PROP_LEFT_ATTACH:
864 ai->left_attach = g_value_get_int (value);
866 case CHILD_PROP_RIGHT_ATTACH:
867 ai->right_attach = g_value_get_int (value);
869 case CHILD_PROP_TOP_ATTACH:
870 ai->top_attach = g_value_get_int (value);
872 case CHILD_PROP_BOTTOM_ATTACH:
873 ai->bottom_attach = g_value_get_int (value);
877 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
881 menu_queue_resize (menu);
885 gtk_menu_get_child_property (GtkContainer *container,
891 AttachInfo *ai = get_attach_info (child);
895 case CHILD_PROP_LEFT_ATTACH:
896 g_value_set_int (value, ai->left_attach);
898 case CHILD_PROP_RIGHT_ATTACH:
899 g_value_set_int (value, ai->right_attach);
901 case CHILD_PROP_TOP_ATTACH:
902 g_value_set_int (value, ai->top_attach);
904 case CHILD_PROP_BOTTOM_ATTACH:
905 g_value_set_int (value, ai->bottom_attach);
909 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
915 gtk_menu_window_event (GtkWidget *window,
919 gboolean handled = FALSE;
921 g_object_ref (window);
927 case GDK_KEY_RELEASE:
928 handled = gtk_widget_event (menu, event);
934 g_object_unref (window);
935 g_object_unref (menu);
941 gtk_menu_window_size_request (GtkWidget *window,
942 GtkRequisition *requisition,
945 GtkMenuPrivate *private = gtk_menu_get_private (menu);
947 if (private->have_position)
949 GdkScreen *screen = gtk_widget_get_screen (window);
950 GdkRectangle monitor;
952 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
954 if (private->y + requisition->height > monitor.y + monitor.height)
955 requisition->height = monitor.y + monitor.height - private->y;
957 if (private->y < monitor.y)
958 requisition->height -= monitor.y - private->y;
963 gtk_menu_init (GtkMenu *menu)
965 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
967 menu->parent_menu_item = NULL;
968 menu->old_active_menu_item = NULL;
969 menu->accel_group = NULL;
970 menu->position_func = NULL;
971 menu->position_func_data = NULL;
972 menu->toggle_size = 0;
974 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
975 "type", GTK_WINDOW_POPUP,
978 "signal::event", gtk_menu_window_event, menu,
979 "signal::size_request", gtk_menu_window_size_request, menu,
980 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
982 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
983 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
985 /* Refloat the menu, so that reference counting for the menu isn't
986 * affected by it being a child of the toplevel
988 g_object_force_floating (G_OBJECT (menu));
989 menu->needs_destruction_ref_count = TRUE;
991 menu->view_window = NULL;
992 menu->bin_window = NULL;
994 menu->scroll_offset = 0;
995 menu->scroll_step = 0;
996 menu->timeout_id = 0;
997 menu->scroll_fast = FALSE;
999 menu->tearoff_window = NULL;
1000 menu->tearoff_hbox = NULL;
1001 menu->torn_off = FALSE;
1002 menu->tearoff_active = FALSE;
1003 menu->tearoff_adjustment = NULL;
1004 menu->tearoff_scrollbar = NULL;
1006 menu->upper_arrow_visible = FALSE;
1007 menu->lower_arrow_visible = FALSE;
1008 menu->upper_arrow_prelight = FALSE;
1009 menu->lower_arrow_prelight = FALSE;
1011 priv->upper_arrow_state = GTK_STATE_NORMAL;
1012 priv->lower_arrow_state = GTK_STATE_NORMAL;
1014 priv->have_layout = FALSE;
1018 gtk_menu_destroy (GtkObject *object)
1020 GtkMenu *menu = GTK_MENU (object);
1021 GtkMenuAttachData *data;
1022 GtkMenuPrivate *priv;
1024 gtk_menu_remove_scroll_timeout (menu);
1026 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1028 gtk_menu_detach (menu);
1030 gtk_menu_stop_navigating_submenu (menu);
1032 if (menu->old_active_menu_item)
1034 g_object_unref (menu->old_active_menu_item);
1035 menu->old_active_menu_item = NULL;
1038 /* Add back the reference count for being a child */
1039 if (menu->needs_destruction_ref_count)
1041 menu->needs_destruction_ref_count = FALSE;
1042 g_object_ref (object);
1045 if (menu->accel_group)
1047 g_object_unref (menu->accel_group);
1048 menu->accel_group = NULL;
1052 gtk_widget_destroy (menu->toplevel);
1054 if (menu->tearoff_window)
1055 gtk_widget_destroy (menu->tearoff_window);
1057 priv = gtk_menu_get_private (menu);
1061 g_free (priv->heights);
1062 priv->heights = NULL;
1067 g_free (priv->title);
1071 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1075 menu_change_screen (GtkMenu *menu,
1076 GdkScreen *new_screen)
1078 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1080 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1082 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1088 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1089 gtk_menu_position (menu);
1092 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1093 private->monitor_num = -1;
1097 attach_widget_screen_changed (GtkWidget *attach_widget,
1098 GdkScreen *previous_screen,
1101 if (gtk_widget_has_screen (attach_widget) &&
1102 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1104 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1109 gtk_menu_attach_to_widget (GtkMenu *menu,
1110 GtkWidget *attach_widget,
1111 GtkMenuDetachFunc detacher)
1113 GtkMenuAttachData *data;
1116 g_return_if_fail (GTK_IS_MENU (menu));
1117 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1119 /* keep this function in sync with gtk_widget_set_parent()
1122 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1125 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1126 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1130 g_object_ref_sink (menu);
1132 data = g_slice_new (GtkMenuAttachData);
1133 data->attach_widget = attach_widget;
1135 g_signal_connect (attach_widget, "screen_changed",
1136 G_CALLBACK (attach_widget_screen_changed), menu);
1137 attach_widget_screen_changed (attach_widget, NULL, menu);
1139 data->detacher = detacher;
1140 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1141 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1142 if (!g_list_find (list, menu))
1144 list = g_list_prepend (list, menu);
1146 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1147 (GDestroyNotify) g_list_free);
1149 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1150 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1152 /* we don't need to set the style here, since
1153 * we are a toplevel widget.
1156 /* Fallback title for menu comes from attach widget */
1157 gtk_menu_update_title (menu);
1161 gtk_menu_get_attach_widget (GtkMenu *menu)
1163 GtkMenuAttachData *data;
1165 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1167 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1169 return data->attach_widget;
1174 gtk_menu_detach (GtkMenu *menu)
1176 GtkMenuAttachData *data;
1179 g_return_if_fail (GTK_IS_MENU (menu));
1181 /* keep this function in sync with gtk_widget_unparent()
1183 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1186 g_warning ("gtk_menu_detach(): menu is not attached");
1189 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1191 g_signal_handlers_disconnect_by_func (data->attach_widget,
1192 (gpointer) attach_widget_screen_changed,
1196 data->detacher (data->attach_widget, menu);
1197 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1198 list = g_list_remove (list, menu);
1200 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1201 (GDestroyNotify) g_list_free);
1203 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1205 if (GTK_WIDGET_REALIZED (menu))
1206 gtk_widget_unrealize (GTK_WIDGET (menu));
1208 g_slice_free (GtkMenuAttachData, data);
1210 /* Fallback title for menu comes from attach widget */
1211 gtk_menu_update_title (menu);
1213 g_object_unref (menu);
1217 gtk_menu_remove (GtkContainer *container,
1220 GtkMenu *menu = GTK_MENU (container);
1222 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1224 /* Clear out old_active_menu_item if it matches the item we are removing
1226 if (menu->old_active_menu_item == widget)
1228 g_object_unref (menu->old_active_menu_item);
1229 menu->old_active_menu_item = NULL;
1232 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1233 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1235 menu_queue_resize (menu);
1241 return g_object_new (GTK_TYPE_MENU, NULL);
1245 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1249 GtkMenu *menu = GTK_MENU (menu_shell);
1250 AttachInfo *ai = get_attach_info (child);
1252 ai->left_attach = -1;
1253 ai->right_attach = -1;
1254 ai->top_attach = -1;
1255 ai->bottom_attach = -1;
1257 if (GTK_WIDGET_REALIZED (menu_shell))
1258 gtk_widget_set_parent_window (child, menu->bin_window);
1260 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1262 menu_queue_resize (menu);
1266 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1271 widget = GTK_WIDGET (menu);
1277 GdkGCValues gc_values;
1279 menu->tearoff_active = FALSE;
1280 menu->saved_scroll_offset = menu->scroll_offset;
1282 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1283 gc = gdk_gc_new_with_values (widget->window,
1284 &gc_values, GDK_GC_SUBWINDOW);
1286 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1288 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1293 gdk_draw_drawable (pixmap, gc,
1294 menu->tearoff_window->window,
1295 0, 0, 0, 0, -1, -1);
1296 g_object_unref (gc);
1298 gtk_widget_set_size_request (menu->tearoff_window,
1302 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1303 g_object_unref (pixmap);
1308 popup_grab_on_window (GdkWindow *window,
1309 guint32 activate_time,
1310 gboolean grab_keyboard)
1312 if ((gdk_pointer_grab (window, TRUE,
1313 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1314 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1315 GDK_POINTER_MOTION_MASK,
1316 NULL, NULL, activate_time) == 0))
1318 if (!grab_keyboard ||
1319 gdk_keyboard_grab (window, TRUE,
1320 activate_time) == 0)
1324 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1335 * @menu: a #GtkMenu.
1336 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1337 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1338 * @func: a user supplied function used to position the menu, or %NULL
1339 * @data: user supplied data to be passed to @func.
1340 * @button: the mouse button which was pressed to initiate the event.
1341 * @activate_time: the time at which the activation event occurred.
1343 * Displays a menu and makes it available for selection. Applications can use
1344 * this function to display context-sensitive menus, and will typically supply
1345 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1346 * parameters. The default menu positioning function will position the menu
1347 * at the current mouse cursor position.
1349 * The @button parameter should be the mouse button pressed to initiate
1350 * the menu popup. If the menu popup was initiated by something other than
1351 * a mouse button press, such as a mouse button release or a keypress,
1352 * @button should be 0.
1354 * The @activate_time parameter is used to conflict-resolve initiation of
1355 * concurrent requests for mouse/keyboard grab requests. To function
1356 * properly, this needs to be the time stamp of the user event (such as
1357 * a mouse click or key press) that caused the initiation of the popup.
1358 * Only if no such event is available, gtk_get_current_event_time() can
1362 gtk_menu_popup (GtkMenu *menu,
1363 GtkWidget *parent_menu_shell,
1364 GtkWidget *parent_menu_item,
1365 GtkMenuPositionFunc func,
1368 guint32 activate_time)
1371 GtkWidget *xgrab_shell;
1373 GdkEvent *current_event;
1374 GtkMenuShell *menu_shell;
1375 gboolean grab_keyboard;
1376 GtkMenuPrivate *priv;
1377 GtkWidget *parent_toplevel;
1379 g_return_if_fail (GTK_IS_MENU (menu));
1381 widget = GTK_WIDGET (menu);
1382 menu_shell = GTK_MENU_SHELL (menu);
1383 priv = gtk_menu_get_private (menu);
1385 menu_shell->parent_menu_shell = parent_menu_shell;
1387 priv->seen_item_enter = FALSE;
1389 /* Find the last viewable ancestor, and make an X grab on it
1391 parent = GTK_WIDGET (menu);
1395 gboolean viewable = TRUE;
1396 GtkWidget *tmp = parent;
1400 if (!GTK_WIDGET_MAPPED (tmp))
1409 xgrab_shell = parent;
1411 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1414 /* We want to receive events generated when we map the menu; unfortunately,
1415 * since there is probably already an implicit grab in place from the
1416 * button that the user used to pop up the menu, we won't receive then --
1417 * in particular, the EnterNotify when the menu pops up under the pointer.
1419 * If we are grabbing on a parent menu shell, no problem; just grab on
1420 * that menu shell first before popping up the window with owner_events = TRUE.
1422 * When grabbing on the menu itself, things get more convuluted - we
1423 * we do an explicit grab on a specially created window with
1424 * owner_events = TRUE, which we override further down with a grab
1425 * on the menu. (We can't grab on the menu until it is mapped; we
1426 * probably could just leave the grab on the other window, with a
1427 * little reorganization of the code in gtkmenu*).
1429 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1430 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1432 if (xgrab_shell && xgrab_shell != widget)
1434 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1435 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1439 GdkWindow *transfer_window;
1441 xgrab_shell = widget;
1442 transfer_window = menu_grab_transfer_window_get (menu);
1443 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1444 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1447 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1449 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1450 * with a stuck up window, we just abort here. Presumably the user will
1453 menu_shell->parent_menu_shell = NULL;
1454 menu_grab_transfer_window_destroy (menu);
1458 menu_shell->active = TRUE;
1459 menu_shell->button = button;
1461 /* If we are popping up the menu from something other than, a button
1462 * press then, as a heuristic, we ignore enter events for the menu
1463 * until we get a MOTION_NOTIFY.
1466 current_event = gtk_get_current_event ();
1469 if ((current_event->type != GDK_BUTTON_PRESS) &&
1470 (current_event->type != GDK_ENTER_NOTIFY))
1471 menu_shell->ignore_enter = TRUE;
1473 gdk_event_free (current_event);
1476 menu_shell->ignore_enter = TRUE;
1480 gtk_menu_tearoff_bg_copy (menu);
1482 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1485 parent_toplevel = NULL;
1486 if (parent_menu_shell)
1487 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1488 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1490 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1492 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1495 /* Set transient for to get the right window group and parent relationship */
1496 if (GTK_IS_WINDOW (parent_toplevel))
1497 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1498 GTK_WINDOW (parent_toplevel));
1500 menu->parent_menu_item = parent_menu_item;
1501 menu->position_func = func;
1502 menu->position_func_data = data;
1503 menu_shell->activate_time = activate_time;
1505 /* We need to show the menu here rather in the init function because
1506 * code expects to be able to tell if the menu is onscreen by
1507 * looking at the GTK_WIDGET_VISIBLE (menu)
1509 gtk_widget_show (GTK_WIDGET (menu));
1511 /* Position the menu, possibly changing the size request
1513 gtk_menu_position (menu);
1515 /* Compute the size of the toplevel and realize it so we
1516 * can scroll correctly.
1519 GtkRequisition tmp_request;
1520 GtkAllocation tmp_allocation = { 0, };
1522 gtk_widget_size_request (menu->toplevel, &tmp_request);
1524 tmp_allocation.width = tmp_request.width;
1525 tmp_allocation.height = tmp_request.height;
1527 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1529 gtk_widget_realize (GTK_WIDGET (menu));
1532 gtk_menu_scroll_to (menu, menu->scroll_offset);
1534 /* if no item is selected, select the first one */
1535 if (!menu_shell->active_menu_item)
1537 gboolean touchscreen_mode;
1539 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1540 "gtk-touchscreen-mode", &touchscreen_mode,
1543 if (touchscreen_mode)
1544 gtk_menu_shell_select_first (menu_shell, TRUE);
1547 /* Once everything is set up correctly, map the toplevel window on
1550 gtk_widget_show (menu->toplevel);
1552 if (xgrab_shell == widget)
1553 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1554 gtk_grab_add (GTK_WIDGET (menu));
1558 gtk_menu_popdown (GtkMenu *menu)
1560 GtkMenuPrivate *private;
1561 GtkMenuShell *menu_shell;
1563 g_return_if_fail (GTK_IS_MENU (menu));
1565 menu_shell = GTK_MENU_SHELL (menu);
1566 private = gtk_menu_get_private (menu);
1568 menu_shell->parent_menu_shell = NULL;
1569 menu_shell->active = FALSE;
1570 menu_shell->ignore_enter = FALSE;
1572 private->have_position = FALSE;
1574 gtk_menu_stop_scrolling (menu);
1576 gtk_menu_stop_navigating_submenu (menu);
1578 if (menu_shell->active_menu_item)
1580 if (menu->old_active_menu_item)
1581 g_object_unref (menu->old_active_menu_item);
1582 menu->old_active_menu_item = menu_shell->active_menu_item;
1583 g_object_ref (menu->old_active_menu_item);
1586 gtk_menu_shell_deselect (menu_shell);
1588 /* The X Grab, if present, will automatically be removed when we hide
1590 gtk_widget_hide (menu->toplevel);
1591 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1595 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1597 if (GTK_BIN (menu->toplevel)->child)
1599 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1603 /* We popped up the menu from the tearoff, so we need to
1604 * release the grab - we aren't actually hiding the menu.
1606 if (menu_shell->have_xgrab)
1608 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1610 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1611 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1615 /* gtk_menu_popdown is called each time a menu item is selected from
1616 * a torn off menu. Only scroll back to the saved position if the
1617 * non-tearoff menu was popped down.
1619 if (!menu->tearoff_active)
1620 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1621 menu->tearoff_active = TRUE;
1624 gtk_widget_hide (GTK_WIDGET (menu));
1626 menu_shell->have_xgrab = FALSE;
1627 gtk_grab_remove (GTK_WIDGET (menu));
1629 menu_grab_transfer_window_destroy (menu);
1633 gtk_menu_get_active (GtkMenu *menu)
1638 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1640 if (!menu->old_active_menu_item)
1643 children = GTK_MENU_SHELL (menu)->children;
1647 child = children->data;
1648 children = children->next;
1650 if (GTK_BIN (child)->child)
1655 menu->old_active_menu_item = child;
1656 if (menu->old_active_menu_item)
1657 g_object_ref (menu->old_active_menu_item);
1660 return menu->old_active_menu_item;
1664 gtk_menu_set_active (GtkMenu *menu,
1670 g_return_if_fail (GTK_IS_MENU (menu));
1672 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1675 child = tmp_list->data;
1676 if (GTK_BIN (child)->child)
1678 if (menu->old_active_menu_item)
1679 g_object_unref (menu->old_active_menu_item);
1680 menu->old_active_menu_item = child;
1681 g_object_ref (menu->old_active_menu_item);
1687 gtk_menu_set_accel_group (GtkMenu *menu,
1688 GtkAccelGroup *accel_group)
1690 g_return_if_fail (GTK_IS_MENU (menu));
1692 if (menu->accel_group != accel_group)
1694 if (menu->accel_group)
1695 g_object_unref (menu->accel_group);
1696 menu->accel_group = accel_group;
1697 if (menu->accel_group)
1698 g_object_ref (menu->accel_group);
1699 _gtk_menu_refresh_accel_paths (menu, TRUE);
1704 gtk_menu_get_accel_group (GtkMenu *menu)
1706 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1708 return menu->accel_group;
1712 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1715 /* Menu items chain here to figure whether they can activate their
1716 * accelerators. Unlike ordinary widgets, menus allow accel
1717 * activation even if invisible since that's the usual case for
1718 * submenus/popup-menus. however, the state of the attach widget
1719 * affects the "activeness" of the menu.
1721 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1724 return gtk_widget_can_activate_accel (awidget, signal_id);
1726 return GTK_WIDGET_IS_SENSITIVE (widget);
1730 * gtk_menu_set_accel_path
1731 * @menu: a valid #GtkMenu
1732 * @accel_path: a valid accelerator path
1734 * Sets an accelerator path for this menu from which accelerator paths
1735 * for its immediate children, its menu items, can be constructed.
1736 * The main purpose of this function is to spare the programmer the
1737 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1738 * each menu item that should support runtime user changable accelerators.
1739 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1740 * each menu item of this menu, that contains a label describing its purpose,
1741 * automatically gets an accel path assigned. For example, a menu containing
1742 * menu items "New" and "Exit", will, after
1743 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1744 * has been called, assign its items the accel paths:
1745 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1746 * Assigning accel paths to menu items then enables the user to change
1747 * their accelerators at runtime. More details about accelerator paths
1748 * and their default setups can be found at gtk_accel_map_add_entry().
1750 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1751 * pass a static string, you can save some memory by interning it first with
1752 * g_intern_static_string().
1755 gtk_menu_set_accel_path (GtkMenu *menu,
1756 const gchar *accel_path)
1758 g_return_if_fail (GTK_IS_MENU (menu));
1760 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1762 /* FIXME: accel_path should be defined as const gchar* */
1763 menu->accel_path = (gchar*)g_intern_string (accel_path);
1764 if (menu->accel_path)
1765 _gtk_menu_refresh_accel_paths (menu, FALSE);
1769 * gtk_menu_get_accel_path
1770 * @menu: a valid #GtkMenu
1772 * Retrieves the accelerator path set on the menu.
1774 * Since: GSEAL-branch
1777 gtk_menu_get_accel_path (GtkMenu *menu)
1779 g_return_if_fail (GTK_IS_MENU (menu));
1781 return menu->accel_path;
1786 gboolean group_changed;
1790 refresh_accel_paths_foreach (GtkWidget *widget,
1793 AccelPropagation *prop = data;
1795 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1796 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1797 prop->menu->accel_path,
1798 prop->menu->accel_group,
1799 prop->group_changed);
1803 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1804 gboolean group_changed)
1806 g_return_if_fail (GTK_IS_MENU (menu));
1808 if (menu->accel_path && menu->accel_group)
1810 AccelPropagation prop;
1813 prop.group_changed = group_changed;
1814 gtk_container_foreach (GTK_CONTAINER (menu),
1815 refresh_accel_paths_foreach,
1821 gtk_menu_reposition (GtkMenu *menu)
1823 g_return_if_fail (GTK_IS_MENU (menu));
1825 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1826 gtk_menu_position (menu);
1830 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1833 g_return_if_fail (GTK_IS_MENU (menu));
1835 if (adjustment->value != menu->scroll_offset)
1836 gtk_menu_scroll_to (menu, adjustment->value);
1840 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1843 GdkGeometry geometry_hints;
1845 if (!menu->tearoff_window)
1848 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1850 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1851 width += menu->tearoff_scrollbar->requisition.width;
1854 geometry_hints.min_width = width;
1855 geometry_hints.max_width = width;
1857 geometry_hints.min_height = 0;
1858 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1860 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1863 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1867 gtk_menu_update_title (GtkMenu *menu)
1869 if (menu->tearoff_window)
1872 GtkWidget *attach_widget;
1874 title = gtk_menu_get_title (menu);
1877 attach_widget = gtk_menu_get_attach_widget (menu);
1878 if (GTK_IS_MENU_ITEM (attach_widget))
1880 GtkWidget *child = GTK_BIN (attach_widget)->child;
1881 if (GTK_IS_LABEL (child))
1882 title = gtk_label_get_text (GTK_LABEL (child));
1887 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1892 gtk_menu_get_toplevel (GtkWidget *menu)
1894 GtkWidget *attach, *toplevel;
1896 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1898 if (GTK_IS_MENU_ITEM (attach))
1899 attach = attach->parent;
1901 if (GTK_IS_MENU (attach))
1902 return gtk_menu_get_toplevel (attach);
1903 else if (GTK_IS_WIDGET (attach))
1905 toplevel = gtk_widget_get_toplevel (attach);
1906 if (GTK_WIDGET_TOPLEVEL (toplevel))
1914 tearoff_window_destroyed (GtkWidget *widget,
1917 gtk_menu_set_tearoff_state (menu, FALSE);
1921 gtk_menu_set_tearoff_state (GtkMenu *menu,
1926 g_return_if_fail (GTK_IS_MENU (menu));
1928 if (menu->torn_off != torn_off)
1930 menu->torn_off = torn_off;
1931 menu->tearoff_active = torn_off;
1935 if (GTK_WIDGET_VISIBLE (menu))
1936 gtk_menu_popdown (menu);
1938 if (!menu->tearoff_window)
1940 GtkWidget *toplevel;
1942 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
1943 "type", GTK_WINDOW_TOPLEVEL,
1944 "screen", gtk_widget_get_screen (menu->toplevel),
1945 "app-paintable", TRUE,
1948 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1949 GDK_WINDOW_TYPE_HINT_MENU);
1950 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1951 g_signal_connect (menu->tearoff_window, "destroy",
1952 G_CALLBACK (tearoff_window_destroyed), menu);
1953 g_signal_connect (menu->tearoff_window, "event",
1954 G_CALLBACK (gtk_menu_window_event), menu);
1956 gtk_menu_update_title (menu);
1958 gtk_widget_realize (menu->tearoff_window);
1960 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1961 if (toplevel != NULL)
1962 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1963 GTK_WINDOW (toplevel));
1965 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1966 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1968 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1969 menu->tearoff_adjustment =
1970 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1972 GTK_WIDGET (menu)->requisition.height,
1976 g_object_connect (menu->tearoff_adjustment,
1977 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1979 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1981 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1982 menu->tearoff_scrollbar,
1985 if (menu->tearoff_adjustment->upper > height)
1986 gtk_widget_show (menu->tearoff_scrollbar);
1988 gtk_widget_show (menu->tearoff_hbox);
1991 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1993 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1995 /* Update menu->requisition
1997 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1999 gtk_menu_set_tearoff_hints (menu, width);
2001 gtk_widget_realize (menu->tearoff_window);
2002 gtk_menu_position (menu);
2004 gtk_widget_show (GTK_WIDGET (menu));
2005 gtk_widget_show (menu->tearoff_window);
2007 gtk_menu_scroll_to (menu, 0);
2012 gtk_widget_hide (GTK_WIDGET (menu));
2013 gtk_widget_hide (menu->tearoff_window);
2014 if (GTK_IS_CONTAINER (menu->toplevel))
2015 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2016 gtk_widget_destroy (menu->tearoff_window);
2018 menu->tearoff_window = NULL;
2019 menu->tearoff_hbox = NULL;
2020 menu->tearoff_scrollbar = NULL;
2021 menu->tearoff_adjustment = NULL;
2024 g_object_notify (G_OBJECT (menu), "tearoff-state");
2029 * gtk_menu_get_tearoff_state:
2032 * Returns whether the menu is torn off. See
2033 * gtk_menu_set_tearoff_state ().
2035 * Return value: %TRUE if the menu is currently torn off.
2038 gtk_menu_get_tearoff_state (GtkMenu *menu)
2040 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2042 return menu->torn_off;
2046 * gtk_menu_set_title:
2048 * @title: a string containing the title for the menu.
2050 * Sets the title string for the menu. The title is displayed when the menu
2051 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2052 * attached to a parent menu item, and if so it will try to use the same text as
2053 * that menu item's label.
2056 gtk_menu_set_title (GtkMenu *menu,
2059 GtkMenuPrivate *priv;
2062 g_return_if_fail (GTK_IS_MENU (menu));
2064 priv = gtk_menu_get_private (menu);
2066 old_title = priv->title;
2067 priv->title = g_strdup (title);
2070 gtk_menu_update_title (menu);
2071 g_object_notify (G_OBJECT (menu), "tearoff-title");
2075 * gtk_menu_get_title:
2078 * Returns the title of the menu. See gtk_menu_set_title().
2080 * Return value: the title of the menu, or %NULL if the menu has no
2081 * title set on it. This string is owned by the widget and should
2082 * not be modified or freed.
2084 G_CONST_RETURN gchar *
2085 gtk_menu_get_title (GtkMenu *menu)
2087 GtkMenuPrivate *priv;
2089 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2091 priv = gtk_menu_get_private (menu);
2097 gtk_menu_reorder_child (GtkMenu *menu,
2101 GtkMenuShell *menu_shell;
2103 g_return_if_fail (GTK_IS_MENU (menu));
2104 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2106 menu_shell = GTK_MENU_SHELL (menu);
2108 if (g_list_find (menu_shell->children, child))
2110 menu_shell->children = g_list_remove (menu_shell->children, child);
2111 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2113 menu_queue_resize (menu);
2118 gtk_menu_style_set (GtkWidget *widget,
2119 GtkStyle *previous_style)
2121 if (GTK_WIDGET_REALIZED (widget))
2123 GtkMenu *menu = GTK_MENU (widget);
2125 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2126 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2127 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2132 get_arrows_border (GtkMenu *menu, GtkBorder *border)
2134 guint scroll_arrow_height;
2136 gtk_widget_style_get (GTK_WIDGET (menu),
2137 "scroll-arrow-vlength", &scroll_arrow_height,
2140 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2141 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2143 border->left = border->right = 0;
2147 gtk_menu_realize (GtkWidget *widget)
2149 GdkWindowAttr attributes;
2150 gint attributes_mask;
2155 guint vertical_padding;
2156 guint horizontal_padding;
2157 GtkBorder arrow_border;
2159 g_return_if_fail (GTK_IS_MENU (widget));
2161 menu = GTK_MENU (widget);
2163 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2165 attributes.window_type = GDK_WINDOW_CHILD;
2166 attributes.x = widget->allocation.x;
2167 attributes.y = widget->allocation.y;
2168 attributes.width = widget->allocation.width;
2169 attributes.height = widget->allocation.height;
2170 attributes.wclass = GDK_INPUT_OUTPUT;
2171 attributes.visual = gtk_widget_get_visual (widget);
2172 attributes.colormap = gtk_widget_get_colormap (widget);
2174 attributes.event_mask = gtk_widget_get_events (widget);
2176 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2177 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2179 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2180 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2181 gdk_window_set_user_data (widget->window, widget);
2183 border_width = GTK_CONTAINER (widget)->border_width;
2185 gtk_widget_style_get (GTK_WIDGET (menu),
2186 "vertical-padding", &vertical_padding,
2187 "horizontal-padding", &horizontal_padding,
2190 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2191 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2192 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2193 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2195 get_arrows_border (menu, &arrow_border);
2196 attributes.y += arrow_border.top;
2197 attributes.height -= arrow_border.top;
2198 attributes.height -= arrow_border.bottom;
2200 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2201 gdk_window_set_user_data (menu->view_window, menu);
2205 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2206 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2208 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2209 gdk_window_set_user_data (menu->bin_window, menu);
2211 children = GTK_MENU_SHELL (menu)->children;
2214 child = children->data;
2215 children = children->next;
2217 gtk_widget_set_parent_window (child, menu->bin_window);
2220 widget->style = gtk_style_attach (widget->style, widget->window);
2221 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2222 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2223 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2225 if (GTK_MENU_SHELL (widget)->active_menu_item)
2226 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2227 GTK_MENU_SHELL (widget)->active_menu_item);
2229 gdk_window_show (menu->bin_window);
2230 gdk_window_show (menu->view_window);
2234 gtk_menu_focus (GtkWidget *widget,
2235 GtkDirectionType direction)
2238 * A menu or its menu items cannot have focus
2243 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2246 menu_grab_transfer_window_get (GtkMenu *menu)
2248 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2251 GdkWindowAttr attributes;
2252 gint attributes_mask;
2254 attributes.x = -100;
2255 attributes.y = -100;
2256 attributes.width = 10;
2257 attributes.height = 10;
2258 attributes.window_type = GDK_WINDOW_TEMP;
2259 attributes.wclass = GDK_INPUT_ONLY;
2260 attributes.override_redirect = TRUE;
2261 attributes.event_mask = 0;
2263 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2265 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2266 &attributes, attributes_mask);
2267 gdk_window_set_user_data (window, menu);
2269 gdk_window_show (window);
2271 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2278 menu_grab_transfer_window_destroy (GtkMenu *menu)
2280 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2283 gdk_window_set_user_data (window, NULL);
2284 gdk_window_destroy (window);
2285 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2290 gtk_menu_unrealize (GtkWidget *widget)
2292 GtkMenu *menu = GTK_MENU (widget);
2294 menu_grab_transfer_window_destroy (menu);
2296 gdk_window_set_user_data (menu->view_window, NULL);
2297 gdk_window_destroy (menu->view_window);
2298 menu->view_window = NULL;
2300 gdk_window_set_user_data (menu->bin_window, NULL);
2301 gdk_window_destroy (menu->bin_window);
2302 menu->bin_window = NULL;
2304 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize) (widget);
2308 gtk_menu_size_request (GtkWidget *widget,
2309 GtkRequisition *requisition)
2313 GtkMenuShell *menu_shell;
2316 guint max_toggle_size;
2317 guint max_accel_width;
2318 guint vertical_padding;
2319 guint horizontal_padding;
2320 GtkRequisition child_requisition;
2321 GtkMenuPrivate *priv;
2323 g_return_if_fail (GTK_IS_MENU (widget));
2324 g_return_if_fail (requisition != NULL);
2326 menu = GTK_MENU (widget);
2327 menu_shell = GTK_MENU_SHELL (widget);
2328 priv = gtk_menu_get_private (menu);
2330 requisition->width = 0;
2331 requisition->height = 0;
2333 max_toggle_size = 0;
2334 max_accel_width = 0;
2336 g_free (priv->heights);
2337 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2338 priv->heights_length = gtk_menu_get_n_rows (menu);
2340 children = menu_shell->children;
2347 child = children->data;
2348 children = children->next;
2350 if (! GTK_WIDGET_VISIBLE (child))
2353 get_effective_child_attach (child, &l, &r, &t, &b);
2355 /* It's important to size_request the child
2356 * before doing the toggle size request, in
2357 * case the toggle size request depends on the size
2358 * request of a child of the child (e.g. for ImageMenuItem)
2361 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2362 gtk_widget_size_request (child, &child_requisition);
2364 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2365 max_toggle_size = MAX (max_toggle_size, toggle_size);
2366 max_accel_width = MAX (max_accel_width,
2367 GTK_MENU_ITEM (child)->accelerator_width);
2369 part = child_requisition.width / (r - l);
2370 requisition->width = MAX (requisition->width, part);
2372 part = MAX (child_requisition.height, toggle_size) / (b - t);
2373 priv->heights[t] = MAX (priv->heights[t], part);
2376 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2377 requisition->height += priv->heights[i];
2379 requisition->width += max_toggle_size + max_accel_width;
2380 requisition->width *= gtk_menu_get_n_columns (menu);
2382 gtk_widget_style_get (GTK_WIDGET (menu),
2383 "vertical-padding", &vertical_padding,
2384 "horizontal-padding", &horizontal_padding,
2387 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2388 widget->style->xthickness) * 2;
2389 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2390 widget->style->ythickness) * 2;
2392 menu->toggle_size = max_toggle_size;
2394 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2396 if (menu->tearoff_active)
2397 gtk_menu_set_tearoff_hints (menu, requisition->width);
2401 gtk_menu_size_allocate (GtkWidget *widget,
2402 GtkAllocation *allocation)
2405 GtkMenuShell *menu_shell;
2407 GtkAllocation child_allocation;
2408 GtkRequisition child_requisition;
2409 GtkMenuPrivate *priv;
2413 guint vertical_padding;
2414 guint horizontal_padding;
2416 g_return_if_fail (GTK_IS_MENU (widget));
2417 g_return_if_fail (allocation != NULL);
2419 menu = GTK_MENU (widget);
2420 menu_shell = GTK_MENU_SHELL (widget);
2421 priv = gtk_menu_get_private (menu);
2423 widget->allocation = *allocation;
2424 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2426 gtk_widget_style_get (GTK_WIDGET (menu),
2427 "vertical-padding", &vertical_padding,
2428 "horizontal-padding", &horizontal_padding,
2431 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2432 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2434 width = MAX (1, allocation->width - x * 2);
2435 height = MAX (1, allocation->height - y * 2);
2437 child_requisition.width -= x * 2;
2438 child_requisition.height -= y * 2;
2440 if (menu_shell->active)
2441 gtk_menu_scroll_to (menu, menu->scroll_offset);
2443 if (!menu->tearoff_active)
2445 GtkBorder arrow_border;
2447 get_arrows_border (menu, &arrow_border);
2448 y += arrow_border.top;
2449 height -= arrow_border.top;
2450 height -= arrow_border.bottom;
2453 if (GTK_WIDGET_REALIZED (widget))
2455 gdk_window_move_resize (widget->window,
2456 allocation->x, allocation->y,
2457 allocation->width, allocation->height);
2459 gdk_window_move_resize (menu->view_window,
2466 if (menu_shell->children)
2468 gint base_width = width / gtk_menu_get_n_columns (menu);
2470 children = menu_shell->children;
2473 child = children->data;
2474 children = children->next;
2476 if (GTK_WIDGET_VISIBLE (child))
2481 get_effective_child_attach (child, &l, &r, &t, &b);
2483 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2486 tmp = gtk_menu_get_n_columns (menu) - l;
2487 l = gtk_menu_get_n_columns (menu) - r;
2491 child_allocation.width = (r - l) * base_width;
2492 child_allocation.height = 0;
2493 child_allocation.x = l * base_width;
2494 child_allocation.y = 0;
2496 for (i = 0; i < b; i++)
2499 child_allocation.y += priv->heights[i];
2501 child_allocation.height += priv->heights[i];
2504 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2507 gtk_widget_size_allocate (child, &child_allocation);
2508 gtk_widget_queue_draw (child);
2512 /* Resize the item window */
2513 if (GTK_WIDGET_REALIZED (widget))
2519 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2520 height += priv->heights[i];
2522 width = gtk_menu_get_n_columns (menu) * base_width;
2523 gdk_window_resize (menu->bin_window, width, height);
2526 if (menu->tearoff_active)
2528 if (allocation->height >= widget->requisition.height)
2530 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2532 gtk_widget_hide (menu->tearoff_scrollbar);
2533 gtk_menu_set_tearoff_hints (menu, allocation->width);
2535 gtk_menu_scroll_to (menu, 0);
2540 menu->tearoff_adjustment->upper = widget->requisition.height;
2541 menu->tearoff_adjustment->page_size = allocation->height;
2543 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2544 menu->tearoff_adjustment->upper)
2547 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2550 gtk_menu_scroll_to (menu, value);
2553 gtk_adjustment_changed (menu->tearoff_adjustment);
2555 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2557 gtk_widget_show (menu->tearoff_scrollbar);
2558 gtk_menu_set_tearoff_hints (menu, allocation->width);
2566 get_arrows_visible_area (GtkMenu *menu,
2567 GdkRectangle *border,
2568 GdkRectangle *upper,
2569 GdkRectangle *lower,
2572 GtkWidget *widget = GTK_WIDGET (menu);
2573 guint vertical_padding;
2574 guint horizontal_padding;
2575 gint scroll_arrow_height;
2577 gtk_widget_style_get (widget,
2578 "vertical-padding", &vertical_padding,
2579 "horizontal-padding", &horizontal_padding,
2580 "scroll-arrow-vlength", &scroll_arrow_height,
2583 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2584 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2585 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2587 upper->x = border->x;
2588 upper->y = border->y;
2589 upper->width = border->width - 2 * border->x;
2590 upper->height = scroll_arrow_height;
2592 lower->x = border->x;
2593 lower->y = border->height - border->y - scroll_arrow_height;
2594 lower->width = border->width - 2 * border->x;
2595 lower->height = scroll_arrow_height;
2597 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2601 gtk_menu_paint (GtkWidget *widget,
2602 GdkEventExpose *event)
2605 GtkMenuPrivate *priv;
2606 GdkRectangle border;
2611 g_return_if_fail (GTK_IS_MENU (widget));
2613 menu = GTK_MENU (widget);
2614 priv = gtk_menu_get_private (menu);
2616 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2618 if (event->window == widget->window)
2620 gint arrow_size = 0.7 * arrow_space;
2622 gtk_paint_box (widget->style,
2626 &event->area, widget, "menu",
2629 if (menu->upper_arrow_visible && !menu->tearoff_active)
2631 gtk_paint_box (widget->style,
2633 priv->upper_arrow_state,
2635 &event->area, widget, "menu_scroll_arrow_up",
2641 gtk_paint_arrow (widget->style,
2643 priv->upper_arrow_state,
2645 &event->area, widget, "menu_scroll_arrow_up",
2648 upper.x + (upper.width - arrow_size) / 2,
2649 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2650 arrow_size, arrow_size);
2653 if (menu->lower_arrow_visible && !menu->tearoff_active)
2655 gtk_paint_box (widget->style,
2657 priv->lower_arrow_state,
2659 &event->area, widget, "menu_scroll_arrow_down",
2665 gtk_paint_arrow (widget->style,
2667 priv->lower_arrow_state,
2669 &event->area, widget, "menu_scroll_arrow_down",
2672 lower.x + (lower.width - arrow_size) / 2,
2673 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2674 arrow_size, arrow_size);
2677 else if (event->window == menu->bin_window)
2679 gint y = -border.y + menu->scroll_offset;
2681 if (!menu->tearoff_active)
2683 GtkBorder arrow_border;
2685 get_arrows_border (menu, &arrow_border);
2686 y -= arrow_border.top;
2689 gtk_paint_box (widget->style,
2693 &event->area, widget, "menu",
2695 border.width, border.height);
2700 gtk_menu_expose (GtkWidget *widget,
2701 GdkEventExpose *event)
2703 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2704 g_return_val_if_fail (event != NULL, FALSE);
2706 if (GTK_WIDGET_DRAWABLE (widget))
2708 gtk_menu_paint (widget, event);
2710 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event) (widget, event);
2717 gtk_menu_show (GtkWidget *widget)
2719 GtkMenu *menu = GTK_MENU (widget);
2721 _gtk_menu_refresh_accel_paths (menu, FALSE);
2723 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2727 gtk_menu_button_scroll (GtkMenu *menu,
2728 GdkEventButton *event)
2730 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2732 gboolean touchscreen_mode;
2734 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
2735 "gtk-touchscreen-mode", &touchscreen_mode,
2738 if (touchscreen_mode)
2739 gtk_menu_handle_scrolling (menu,
2740 event->x_root, event->y_root,
2741 event->type == GDK_BUTTON_PRESS,
2751 pointer_in_menu_window (GtkWidget *widget,
2755 GtkMenu *menu = GTK_MENU (widget);
2757 if (GTK_WIDGET_MAPPED (menu->toplevel))
2759 GtkMenuShell *menu_shell;
2760 gint window_x, window_y;
2762 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
2764 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
2765 y_root >= window_y && y_root < window_y + widget->allocation.height)
2768 menu_shell = GTK_MENU_SHELL (widget);
2770 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
2771 return pointer_in_menu_window (menu_shell->parent_menu_shell,
2779 gtk_menu_button_press (GtkWidget *widget,
2780 GdkEventButton *event)
2782 if (event->type != GDK_BUTTON_PRESS)
2785 /* Don't pass down to menu shell for presses over scroll arrows
2787 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2790 /* Don't pass down to menu shell if a non-menuitem part of the menu
2791 * was clicked. The check for the event_widget being a GtkMenuShell
2792 * works because we have the pointer grabbed on menu_shell->window
2793 * with owner_events=TRUE, so all events that are either outside
2794 * the menu or on its border are delivered relative to
2795 * menu_shell->window.
2797 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2798 pointer_in_menu_window (widget, event->x_root, event->y_root))
2801 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2805 gtk_menu_button_release (GtkWidget *widget,
2806 GdkEventButton *event)
2808 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2810 if (priv->ignore_button_release)
2812 priv->ignore_button_release = FALSE;
2816 if (event->type != GDK_BUTTON_RELEASE)
2819 /* Don't pass down to menu shell for releases over scroll arrows
2821 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2824 /* Don't pass down to menu shell if a non-menuitem part of the menu
2825 * was clicked (see comment in button_press()).
2827 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2828 pointer_in_menu_window (widget, event->x_root, event->y_root))
2830 /* Ugly: make sure menu_shell->button gets reset to 0 when we
2831 * bail out early here so it is in a consistent state for the
2832 * next button_press/button_release in GtkMenuShell.
2835 if (GTK_MENU_SHELL (widget)->active)
2836 GTK_MENU_SHELL (widget)->button = 0;
2841 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
2844 static const gchar *
2845 get_accel_path (GtkWidget *menu_item,
2850 GClosure *accel_closure;
2851 GtkAccelGroup *accel_group;
2853 path = _gtk_widget_get_accel_path (menu_item, locked);
2856 path = GTK_MENU_ITEM (menu_item)->accel_path;
2862 label = GTK_BIN (menu_item)->child;
2864 if (GTK_IS_ACCEL_LABEL (label))
2866 g_object_get (label,
2867 "accel-closure", &accel_closure,
2871 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2873 *locked = accel_group->lock_count > 0;
2883 gtk_menu_key_press (GtkWidget *widget,
2886 GtkMenuShell *menu_shell;
2888 gboolean delete = FALSE;
2889 gboolean can_change_accels;
2890 gchar *accel = NULL;
2891 guint accel_key, accel_mods;
2892 GdkModifierType consumed_modifiers;
2893 GdkDisplay *display;
2895 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2896 g_return_val_if_fail (event != NULL, FALSE);
2898 menu_shell = GTK_MENU_SHELL (widget);
2899 menu = GTK_MENU (widget);
2901 gtk_menu_stop_navigating_submenu (menu);
2903 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
2906 display = gtk_widget_get_display (widget);
2908 g_object_get (gtk_widget_get_settings (widget),
2909 "gtk-menu-bar-accel", &accel,
2910 "gtk-can-change-accels", &can_change_accels,
2913 if (accel && *accel)
2916 GdkModifierType mods = 0;
2918 gtk_accelerator_parse (accel, &keyval, &mods);
2921 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2923 /* FIXME this is wrong, needs to be in the global accel resolution
2924 * thing, to properly consider i18n etc., but that probably requires
2925 * AccelGroup changes etc.
2927 if (event->keyval == keyval && (mods & event->state) == mods)
2929 gtk_menu_shell_cancel (menu_shell);
2937 switch (event->keyval)
2948 /* Figure out what modifiers went into determining the key symbol */
2949 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2950 event->hardware_keycode, event->state, event->group,
2951 NULL, NULL, NULL, &consumed_modifiers);
2953 accel_key = gdk_keyval_to_lower (event->keyval);
2954 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2956 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2957 * We re-upper case when we match against the keyval, but display and save in caseless form.
2959 if (accel_key != event->keyval)
2960 accel_mods |= GDK_SHIFT_MASK;
2962 /* Modify the accelerators */
2963 if (can_change_accels &&
2964 menu_shell->active_menu_item &&
2965 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2966 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2967 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2969 GtkWidget *menu_item = menu_shell->active_menu_item;
2970 gboolean locked, replace_accels = TRUE;
2973 path = get_accel_path (menu_item, &locked);
2974 if (!path || locked)
2976 /* can't change accelerators on menu_items without paths
2977 * (basically, those items are accelerator-locked).
2979 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2980 gtk_widget_error_bell (widget);
2986 /* For the keys that act to delete the current setting, we delete
2987 * the current setting if there is one, otherwise, we set the
2988 * key as the accelerator.
2994 if (gtk_accel_map_lookup_entry (path, &key) &&
2995 (key.accel_key || key.accel_mods))
3001 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3005 /* we failed, probably because this key is in use and
3008 /* g_print("failed to change\n"); */
3009 gtk_widget_error_bell (widget);
3018 check_threshold (GtkWidget *widget,
3019 int start_x, int start_y,
3025 ABS (start_x - x) > THRESHOLD ||
3026 ABS (start_y - y) > THRESHOLD;
3030 definitely_within_item (GtkWidget *widget,
3034 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3037 gdk_drawable_get_size (window, &w, &h);
3040 check_threshold (widget, 0, 0, x, y) &&
3041 check_threshold (widget, w - 1, 0, x, y) &&
3042 check_threshold (widget, w - 1, h - 1, x, y) &&
3043 check_threshold (widget, 0, h - 1, x, y);
3047 gtk_menu_motion_notify (GtkWidget *widget,
3048 GdkEventMotion *event)
3050 GtkWidget *menu_item;
3052 GtkMenuShell *menu_shell;
3054 gboolean need_enter;
3056 if (GTK_IS_MENU (widget))
3058 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3060 if (priv->ignore_button_release)
3061 priv->ignore_button_release = FALSE;
3063 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3067 /* We received the event for one of two reasons:
3069 * a) We are the active menu, and did gtk_grab_add()
3070 * b) The widget is a child of ours, and the event was propagated
3072 * Since for computation of navigation regions, we want the menu which
3073 * is the parent of the menu item, for a), we need to find that menu,
3074 * which may be different from 'widget'.
3076 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3077 if (!GTK_IS_MENU_ITEM (menu_item) ||
3078 !GTK_IS_MENU (menu_item->parent))
3081 menu_shell = GTK_MENU_SHELL (menu_item->parent);
3082 menu = GTK_MENU (menu_shell);
3084 if (definitely_within_item (menu_item, event->x, event->y))
3085 menu_shell->activate_time = 0;
3087 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
3089 /* Check to see if we are within an active submenu's navigation region
3091 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3094 /* Make sure we pop down if we enter a non-selectable menu item, so we
3095 * don't show a submenu when the cursor is outside the stay-up triangle.
3097 if (!_gtk_menu_item_is_selectable (menu_item))
3099 gtk_menu_shell_deselect (menu_shell);
3105 /* The menu is now sensitive to enter events on its items, but
3106 * was previously sensitive. So we fake an enter event.
3110 menu_shell->ignore_enter = FALSE;
3112 gdk_drawable_get_size (event->window, &width, &height);
3113 if (event->x >= 0 && event->x < width &&
3114 event->y >= 0 && event->y < height)
3116 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3119 send_event->crossing.window = g_object_ref (event->window);
3120 send_event->crossing.time = event->time;
3121 send_event->crossing.send_event = TRUE;
3122 send_event->crossing.x_root = event->x_root;
3123 send_event->crossing.y_root = event->y_root;
3124 send_event->crossing.x = event->x;
3125 send_event->crossing.y = event->y;
3126 send_event->crossing.state = event->state;
3128 /* We send the event to 'widget', the currently active menu,
3129 * instead of 'menu', the menu that the pointer is in. This
3130 * will ensure that the event will be ignored unless the
3131 * menuitem is a child of the active menu or some parent
3132 * menu of the active menu.
3134 result = gtk_widget_event (widget, send_event);
3135 gdk_event_free (send_event);
3145 get_double_arrows (GtkMenu *menu)
3147 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3148 gboolean double_arrows;
3150 gtk_widget_style_get (GTK_WIDGET (menu),
3151 "double-arrows", &double_arrows,
3154 return double_arrows || (priv->initially_pushed_in &&
3155 menu->scroll_offset != 0);
3159 gtk_menu_scroll_by (GtkMenu *menu,
3164 gint view_width, view_height;
3165 gboolean double_arrows;
3166 GtkBorder arrow_border;
3168 widget = GTK_WIDGET (menu);
3169 offset = menu->scroll_offset + step;
3171 get_arrows_border (menu, &arrow_border);
3173 double_arrows = get_double_arrows (menu);
3175 /* If we scroll upward and the non-visible top part
3176 * is smaller than the scroll arrow it would be
3177 * pretty stupid to show the arrow and taking more
3178 * screen space than just scrolling to the top.
3181 if ((step < 0) && (offset < arrow_border.top))
3184 /* Don't scroll over the top if we weren't before: */
3185 if ((menu->scroll_offset >= 0) && (offset < 0))
3188 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3190 if (menu->scroll_offset == 0 &&
3191 view_height >= widget->requisition.height)
3194 /* Don't scroll past the bottom if we weren't before: */
3195 if (menu->scroll_offset > 0)
3196 view_height -= arrow_border.top;
3198 /* When both arrows are always shown, reduce
3199 * view height even more.
3202 view_height -= arrow_border.bottom;
3204 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3205 (offset + view_height > widget->requisition.height))
3206 offset = widget->requisition.height - view_height;
3208 if (offset != menu->scroll_offset)
3209 gtk_menu_scroll_to (menu, offset);
3213 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3214 gboolean touchscreen_mode)
3216 gboolean upper_visible;
3217 gboolean lower_visible;
3219 upper_visible = menu->upper_arrow_visible;
3220 lower_visible = menu->lower_arrow_visible;
3222 gtk_menu_scroll_by (menu, menu->scroll_step);
3224 if (touchscreen_mode &&
3225 (upper_visible != menu->upper_arrow_visible ||
3226 lower_visible != menu->lower_arrow_visible))
3228 /* We are about to hide a scroll arrow while the mouse is pressed,
3229 * this would cause the uncovered menu item to be activated on button
3230 * release. Therefore we need to ignore button release here
3232 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3233 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3238 gtk_menu_scroll_timeout (gpointer data)
3241 gboolean touchscreen_mode;
3243 menu = GTK_MENU (data);
3245 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3246 "gtk-touchscreen-mode", &touchscreen_mode,
3249 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3255 gtk_menu_scroll_timeout_initial (gpointer data)
3259 gboolean touchscreen_mode;
3261 menu = GTK_MENU (data);
3263 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3264 "gtk-timeout-repeat", &timeout,
3265 "gtk-touchscreen-mode", &touchscreen_mode,
3268 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3270 gtk_menu_remove_scroll_timeout (menu);
3272 menu->timeout_id = gdk_threads_add_timeout (timeout,
3273 gtk_menu_scroll_timeout,
3280 gtk_menu_start_scrolling (GtkMenu *menu)
3283 gboolean touchscreen_mode;
3285 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3286 "gtk-timeout-repeat", &timeout,
3287 "gtk-touchscreen-mode", &touchscreen_mode,
3290 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3292 menu->timeout_id = gdk_threads_add_timeout (timeout,
3293 gtk_menu_scroll_timeout_initial,
3298 gtk_menu_scroll (GtkWidget *widget,
3299 GdkEventScroll *event)
3301 GtkMenu *menu = GTK_MENU (widget);
3303 switch (event->direction)
3305 case GDK_SCROLL_RIGHT:
3306 case GDK_SCROLL_DOWN:
3307 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3309 case GDK_SCROLL_LEFT:
3311 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3319 get_arrows_sensitive_area (GtkMenu *menu,
3320 GdkRectangle *upper,
3321 GdkRectangle *lower)
3325 guint vertical_padding;
3327 gint scroll_arrow_height;
3329 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3331 gtk_widget_style_get (GTK_WIDGET (menu),
3332 "vertical-padding", &vertical_padding,
3333 "scroll-arrow-vlength", &scroll_arrow_height,
3336 border = GTK_CONTAINER (menu)->border_width +
3337 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3339 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3345 upper->width = width;
3346 upper->height = scroll_arrow_height + border;
3352 lower->y = win_y + height - border - scroll_arrow_height;
3353 lower->width = width;
3354 lower->height = scroll_arrow_height + border;
3360 gtk_menu_handle_scrolling (GtkMenu *menu,
3366 GtkMenuShell *menu_shell;
3367 GtkMenuPrivate *priv;
3370 gboolean scroll_fast = FALSE;
3372 gboolean touchscreen_mode;
3374 priv = gtk_menu_get_private (menu);
3376 menu_shell = GTK_MENU_SHELL (menu);
3378 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3379 "gtk-touchscreen-mode", &touchscreen_mode,
3382 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3386 /* upper arrow handling */
3388 get_arrows_sensitive_area (menu, &rect, NULL);
3391 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3392 (x >= rect.x) && (x < rect.x + rect.width) &&
3393 (y >= rect.y) && (y < rect.y + rect.height))
3398 if (touchscreen_mode)
3399 menu->upper_arrow_prelight = in_arrow;
3401 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3403 gboolean arrow_pressed = FALSE;
3405 if (menu->upper_arrow_visible && !menu->tearoff_active)
3407 if (touchscreen_mode)
3409 if (enter && menu->upper_arrow_prelight)
3411 if (menu->timeout_id == 0)
3413 /* Deselect the active item so that
3414 * any submenus are popped down
3416 gtk_menu_shell_deselect (menu_shell);
3418 gtk_menu_remove_scroll_timeout (menu);
3419 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3423 /* Only do stuff on click. */
3424 gtk_menu_start_scrolling (menu);
3425 arrow_pressed = TRUE;
3430 arrow_pressed = TRUE;
3435 gtk_menu_stop_scrolling (menu);
3438 else /* !touchscreen_mode */
3440 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3442 if (enter && in_arrow &&
3443 (!menu->upper_arrow_prelight ||
3444 menu->scroll_fast != scroll_fast))
3446 menu->upper_arrow_prelight = TRUE;
3447 menu->scroll_fast = scroll_fast;
3449 /* Deselect the active item so that
3450 * any submenus are popped down
3452 gtk_menu_shell_deselect (menu_shell);
3454 gtk_menu_remove_scroll_timeout (menu);
3455 menu->scroll_step = scroll_fast ?
3456 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3459 gdk_threads_add_timeout (scroll_fast ?
3460 MENU_SCROLL_TIMEOUT2 :
3461 MENU_SCROLL_TIMEOUT1,
3462 gtk_menu_scroll_timeout, menu);
3464 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3466 gtk_menu_stop_scrolling (menu);
3471 /* gtk_menu_start_scrolling() might have hit the top of the
3472 * menu, so check if the button isn't insensitive before
3473 * changing it to something else.
3475 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3477 GtkStateType arrow_state = GTK_STATE_NORMAL;
3480 arrow_state = GTK_STATE_ACTIVE;
3481 else if (menu->upper_arrow_prelight)
3482 arrow_state = GTK_STATE_PRELIGHT;
3484 if (arrow_state != priv->upper_arrow_state)
3486 priv->upper_arrow_state = arrow_state;
3488 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3494 /* lower arrow handling */
3496 get_arrows_sensitive_area (menu, NULL, &rect);
3499 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3500 (x >= rect.x) && (x < rect.x + rect.width) &&
3501 (y >= rect.y) && (y < rect.y + rect.height))
3506 if (touchscreen_mode)
3507 menu->lower_arrow_prelight = in_arrow;
3509 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3511 gboolean arrow_pressed = FALSE;
3513 if (menu->lower_arrow_visible && !menu->tearoff_active)
3515 if (touchscreen_mode)
3517 if (enter && menu->lower_arrow_prelight)
3519 if (menu->timeout_id == 0)
3521 /* Deselect the active item so that
3522 * any submenus are popped down
3524 gtk_menu_shell_deselect (menu_shell);
3526 gtk_menu_remove_scroll_timeout (menu);
3527 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3531 /* Only do stuff on click. */
3532 gtk_menu_start_scrolling (menu);
3533 arrow_pressed = TRUE;
3538 arrow_pressed = TRUE;
3543 gtk_menu_stop_scrolling (menu);
3546 else /* !touchscreen_mode */
3548 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3550 if (enter && in_arrow &&
3551 (!menu->lower_arrow_prelight ||
3552 menu->scroll_fast != scroll_fast))
3554 menu->lower_arrow_prelight = TRUE;
3555 menu->scroll_fast = scroll_fast;
3557 /* Deselect the active item so that
3558 * any submenus are popped down
3560 gtk_menu_shell_deselect (menu_shell);
3562 gtk_menu_remove_scroll_timeout (menu);
3563 menu->scroll_step = scroll_fast ?
3564 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3567 gdk_threads_add_timeout (scroll_fast ?
3568 MENU_SCROLL_TIMEOUT2 :
3569 MENU_SCROLL_TIMEOUT1,
3570 gtk_menu_scroll_timeout, menu);
3572 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3574 gtk_menu_stop_scrolling (menu);
3579 /* gtk_menu_start_scrolling() might have hit the bottom of the
3580 * menu, so check if the button isn't insensitive before
3581 * changing it to something else.
3583 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3585 GtkStateType arrow_state = GTK_STATE_NORMAL;
3588 arrow_state = GTK_STATE_ACTIVE;
3589 else if (menu->lower_arrow_prelight)
3590 arrow_state = GTK_STATE_PRELIGHT;
3592 if (arrow_state != priv->lower_arrow_state)
3594 priv->lower_arrow_state = arrow_state;
3596 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3604 gtk_menu_enter_notify (GtkWidget *widget,
3605 GdkEventCrossing *event)
3607 GtkWidget *menu_item;
3608 gboolean touchscreen_mode;
3610 g_object_get (gtk_widget_get_settings (widget),
3611 "gtk-touchscreen-mode", &touchscreen_mode,
3614 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3615 if (GTK_IS_MENU (widget))
3617 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3619 if (!menu_shell->ignore_enter)
3620 gtk_menu_handle_scrolling (GTK_MENU (widget),
3621 event->x_root, event->y_root, TRUE, TRUE);
3624 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3626 GtkWidget *menu = menu_item->parent;
3628 if (GTK_IS_MENU (menu))
3630 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3631 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3633 if (priv->seen_item_enter)
3635 /* This is the second enter we see for an item
3636 * on this menu. This means a release should always
3639 menu_shell->activate_time = 0;
3641 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3642 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3644 if (definitely_within_item (menu_item, event->x, event->y))
3646 /* This is an actual user-enter (ie. not a pop-under)
3647 * In this case, the user must either have entered
3648 * sufficiently far enough into the item, or he must move
3649 * far enough away from the enter point. (see
3650 * gtk_menu_motion_notify())
3652 menu_shell->activate_time = 0;
3656 priv->seen_item_enter = TRUE;
3660 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3661 * will not correspond to the event widget's parent. Check to see
3662 * if we are in the parent's navigation region.
3664 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3665 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3666 event->x_root, event->y_root))
3669 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3673 gtk_menu_leave_notify (GtkWidget *widget,
3674 GdkEventCrossing *event)
3676 GtkMenuShell *menu_shell;
3678 GtkMenuItem *menu_item;
3679 GtkWidget *event_widget;
3681 menu = GTK_MENU (widget);
3682 menu_shell = GTK_MENU_SHELL (widget);
3684 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3687 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3689 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3691 if (!GTK_IS_MENU_ITEM (event_widget))
3694 menu_item = GTK_MENU_ITEM (event_widget);
3696 /* Here we check to see if we're leaving an active menu item with a submenu,
3697 * in which case we enter submenu navigation mode.
3699 if (menu_shell->active_menu_item != NULL
3700 && menu_item->submenu != NULL
3701 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3703 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3705 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3708 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3710 /* We are leaving an active menu item with nonactive submenu.
3711 * Deselect it so we don't surprise the user with by popping
3712 * up a submenu _after_ he left the item.
3714 gtk_menu_shell_deselect (menu_shell);
3719 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3723 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3725 if (menu->navigation_region)
3727 gdk_region_destroy (menu->navigation_region);
3728 menu->navigation_region = NULL;
3730 if (menu->navigation_timeout)
3732 g_source_remove (menu->navigation_timeout);
3733 menu->navigation_timeout = 0;
3737 /* When the timeout is elapsed, the navigation region is destroyed
3738 * and the menuitem under the pointer (if any) is selected.
3741 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3743 GtkMenu *menu = user_data;
3744 GdkWindow *child_window;
3746 gtk_menu_stop_navigating_submenu (menu);
3748 if (GTK_WIDGET_REALIZED (menu))
3750 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3754 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3756 send_event->crossing.window = g_object_ref (child_window);
3757 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3758 send_event->crossing.send_event = TRUE;
3760 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3762 gdk_event_free (send_event);
3770 gtk_menu_navigating_submenu (GtkMenu *menu,
3774 if (menu->navigation_region)
3776 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3780 gtk_menu_stop_navigating_submenu (menu);
3787 #undef DRAW_STAY_UP_TRIANGLE
3789 #ifdef DRAW_STAY_UP_TRIANGLE
3792 draw_stay_up_triangle (GdkWindow *window,
3795 /* Draw ugly color all over the stay-up triangle */
3796 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3797 GdkGCValues gc_values;
3799 GdkRectangle clipbox;
3801 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3802 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3803 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3804 gdk_gc_set_clip_region (ugly_gc, region);
3806 gdk_region_get_clipbox (region, &clipbox);
3808 gdk_draw_rectangle (window,
3811 clipbox.x, clipbox.y,
3812 clipbox.width, clipbox.height);
3814 g_object_unref (ugly_gc);
3819 flip_region (GdkRegion *region,
3824 GdkRectangle *rectangles;
3825 GdkRectangle clipbox;
3826 GdkRegion *new_region;
3829 new_region = gdk_region_new ();
3831 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3832 gdk_region_get_clipbox (region, &clipbox);
3834 for (i = 0; i < n_rectangles; ++i)
3836 GdkRectangle rect = rectangles[i];
3839 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3842 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3844 gdk_region_union_with_rect (new_region, &rect);
3847 g_free (rectangles);
3853 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3854 GtkMenuItem *menu_item,
3855 GdkEventCrossing *event)
3857 gint submenu_left = 0;
3858 gint submenu_right = 0;
3859 gint submenu_top = 0;
3860 gint submenu_bottom = 0;
3864 GtkWidget *event_widget;
3866 g_return_if_fail (menu_item->submenu != NULL);
3867 g_return_if_fail (event != NULL);
3869 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3871 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3872 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3874 submenu_right = submenu_left + width;
3875 submenu_bottom = submenu_top + height;
3877 gdk_drawable_get_size (event_widget->window, &width, &height);
3879 if (event->x >= 0 && event->x < width)
3882 gboolean flip_y = FALSE;
3883 gboolean flip_x = FALSE;
3885 gtk_menu_stop_navigating_submenu (menu);
3887 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3890 point[0].x = event->x_root;
3891 point[1].x = submenu_left;
3896 point[0].x = event->x_root + 1;
3897 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3905 point[0].y = event->y_root + 1;
3906 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3908 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3916 point[0].y = event->y_root;
3917 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3919 if (point[0].y >= submenu_bottom)
3923 point[2].x = point[1].x;
3924 point[2].y = point[0].y;
3926 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3928 if (flip_x || flip_y)
3930 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3931 gdk_region_destroy (menu->navigation_region);
3932 menu->navigation_region = new_region;
3935 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3936 "gtk-menu-popdown-delay", &popdown_delay,
3939 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
3940 gtk_menu_stop_navigating_submenu_cb,
3943 #ifdef DRAW_STAY_UP_TRIANGLE
3944 draw_stay_up_triangle (gdk_get_default_root_window(),
3945 menu->navigation_region);
3951 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3955 g_return_if_fail (GTK_IS_MENU (menu_shell));
3957 parent = menu_shell->parent_menu_shell;
3959 menu_shell->activate_time = 0;
3960 gtk_menu_popdown (GTK_MENU (menu_shell));
3963 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3967 gtk_menu_position (GtkMenu *menu)
3970 GtkRequisition requisition;
3971 GtkMenuPrivate *private;
3976 GdkScreen *pointer_screen;
3977 GdkRectangle monitor;
3979 g_return_if_fail (GTK_IS_MENU (menu));
3981 widget = GTK_WIDGET (menu);
3983 screen = gtk_widget_get_screen (widget);
3984 gdk_display_get_pointer (gdk_screen_get_display (screen),
3985 &pointer_screen, &x, &y, NULL);
3987 /* We need the requisition to figure out the right place to
3988 * popup the menu. In fact, we always need to ask here, since
3989 * if a size_request was queued while we weren't popped up,
3990 * the requisition won't have been recomputed yet.
3992 gtk_widget_size_request (widget, &requisition);
3994 if (pointer_screen != screen)
3996 /* Pointer is on a different screen; roughly center the
3997 * menu on the screen. If someone was using multiscreen
3998 * + Xinerama together they'd probably want something
3999 * fancier; but that is likely to be vanishingly rare.
4001 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4002 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4005 private = gtk_menu_get_private (menu);
4006 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4008 private->initially_pushed_in = FALSE;
4010 /* Set the type hint here to allow custom position functions to set a different hint */
4011 if (!GTK_WIDGET_VISIBLE (menu->toplevel))
4012 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4014 if (menu->position_func)
4016 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4017 menu->position_func_data);
4018 if (private->monitor_num < 0)
4019 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4021 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4025 gint space_left, space_right, space_above, space_below;
4028 gint xthickness = widget->style->xthickness;
4029 gint ythickness = widget->style->ythickness;
4030 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4032 /* The placement of popup menus horizontally works like this (with
4033 * RTL in parentheses)
4035 * - If there is enough room to the right (left) of the mouse cursor,
4036 * position the menu there.
4038 * - Otherwise, if if there is enough room to the left (right) of the
4039 * mouse cursor, position the menu there.
4041 * - Otherwise if the menu is smaller than the monitor, position it
4042 * on the side of the mouse cursor that has the most space available
4044 * - Otherwise (if there is simply not enough room for the menu on the
4045 * monitor), position it as far left (right) as possible.
4047 * Positioning in the vertical direction is similar: first try below
4048 * mouse cursor, then above.
4050 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4052 space_left = x - monitor.x;
4053 space_right = monitor.x + monitor.width - x - 1;
4054 space_above = y - monitor.y;
4055 space_below = monitor.y + monitor.height - y - 1;
4057 /* position horizontally */
4059 /* the amount of space we need to position the menu. Note the
4060 * menu is offset "xthickness" pixels
4062 needed_width = requisition.width - xthickness;
4064 if (needed_width <= space_left ||
4065 needed_width <= space_right)
4067 if ((rtl && needed_width <= space_left) ||
4068 (!rtl && needed_width > space_right))
4071 x = x + xthickness - requisition.width + 1;
4075 /* position right */
4079 /* x is clamped on-screen further down */
4081 else if (requisition.width <= monitor.width)
4083 /* the menu is too big to fit on either side of the mouse
4084 * cursor, but smaller than the monitor. Position it on
4085 * the side that has the most space
4087 if (space_left > space_right)
4095 x = monitor.x + monitor.width - requisition.width;
4098 else /* menu is simply too big for the monitor */
4103 x = monitor.x + monitor.width - requisition.width;
4112 /* Position vertically. The algorithm is the same as above, but
4113 * simpler because we don't have to take RTL into account.
4115 needed_height = requisition.height - ythickness;
4117 if (needed_height <= space_above ||
4118 needed_height <= space_below)
4120 if (needed_height <= space_below)
4123 y = y + ythickness - requisition.height + 1;
4125 y = CLAMP (y, monitor.y,
4126 monitor.y + monitor.height - requisition.height);
4128 else if (needed_height > space_below && needed_height > space_above)
4130 if (space_below >= space_above)
4131 y = monitor.y + monitor.height - requisition.height;
4143 if (private->initially_pushed_in)
4145 menu_height = GTK_WIDGET (menu)->requisition.height;
4147 if (y + menu_height > monitor.y + monitor.height)
4149 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4150 y = (monitor.y + monitor.height) - menu_height;
4155 scroll_offset += monitor.y - y;
4160 /* FIXME: should this be done in the various position_funcs ? */
4161 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4163 if (GTK_MENU_SHELL (menu)->active)
4165 private->have_position = TRUE;
4170 if (y + requisition.height > monitor.y + monitor.height)
4171 requisition.height = (monitor.y + monitor.height) - y;
4175 scroll_offset += monitor.y - y;
4176 requisition.height -= monitor.y - y;
4180 if (scroll_offset > 0)
4182 GtkBorder arrow_border;
4184 get_arrows_border (menu, &arrow_border);
4185 scroll_offset += arrow_border.top;
4188 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4191 if (!GTK_MENU_SHELL (menu)->active)
4193 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4194 requisition.width, requisition.height);
4197 menu->scroll_offset = scroll_offset;
4201 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4203 if (menu->timeout_id)
4205 g_source_remove (menu->timeout_id);
4206 menu->timeout_id = 0;
4211 gtk_menu_stop_scrolling (GtkMenu *menu)
4213 gboolean touchscreen_mode;
4215 gtk_menu_remove_scroll_timeout (menu);
4217 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4218 "gtk-touchscreen-mode", &touchscreen_mode,
4221 if (!touchscreen_mode)
4223 menu->upper_arrow_prelight = FALSE;
4224 menu->lower_arrow_prelight = FALSE;
4229 gtk_menu_scroll_to (GtkMenu *menu,
4234 gint view_width, view_height;
4237 guint vertical_padding;
4238 guint horizontal_padding;
4239 gboolean double_arrows;
4240 GtkBorder arrow_border;
4242 widget = GTK_WIDGET (menu);
4244 if (menu->tearoff_active &&
4245 menu->tearoff_adjustment &&
4246 (menu->tearoff_adjustment->value != offset))
4248 menu->tearoff_adjustment->value =
4250 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4251 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4254 /* Move/resize the viewport according to arrows: */
4255 view_width = widget->allocation.width;
4256 view_height = widget->allocation.height;
4258 gtk_widget_style_get (GTK_WIDGET (menu),
4259 "vertical-padding", &vertical_padding,
4260 "horizontal-padding", &horizontal_padding,
4263 double_arrows = get_double_arrows (menu);
4265 border_width = GTK_CONTAINER (menu)->border_width;
4266 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4267 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4268 menu_height = widget->requisition.height -
4269 (border_width + widget->style->ythickness + vertical_padding) * 2;
4271 x = border_width + widget->style->xthickness + horizontal_padding;
4272 y = border_width + widget->style->ythickness + vertical_padding;
4274 if (double_arrows && !menu->tearoff_active)
4276 if (view_height < menu_height ||
4277 (offset > 0 && menu->scroll_offset > 0) ||
4278 (offset < 0 && menu->scroll_offset < 0))
4280 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4281 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4282 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4284 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4285 gtk_widget_queue_draw (GTK_WIDGET (menu));
4287 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4289 get_arrows_border (menu, &arrow_border);
4290 y += arrow_border.top;
4291 view_height -= arrow_border.top;
4292 view_height -= arrow_border.bottom;
4295 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4296 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4297 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4298 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4300 if (offset >= menu_height - view_height)
4301 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4302 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4303 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4304 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4306 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4307 (priv->lower_arrow_state != lower_arrow_previous_state))
4308 gtk_widget_queue_draw (GTK_WIDGET (menu));
4310 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4311 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4313 /* At the upper border, possibly remove timeout */
4314 if (menu->scroll_step < 0)
4316 gtk_menu_stop_scrolling (menu);
4317 gtk_widget_queue_draw (GTK_WIDGET (menu));
4321 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4322 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4324 /* At the lower border, possibly remove timeout */
4325 if (menu->scroll_step > 0)
4327 gtk_menu_stop_scrolling (menu);
4328 gtk_widget_queue_draw (GTK_WIDGET (menu));
4332 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4336 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4337 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4339 gtk_menu_stop_scrolling (menu);
4340 gtk_widget_queue_draw (GTK_WIDGET (menu));
4343 else if (!menu->tearoff_active)
4345 gboolean last_visible;
4347 last_visible = menu->upper_arrow_visible;
4348 menu->upper_arrow_visible = offset > 0;
4350 /* upper_arrow_visible may have changed, so requery the border */
4351 get_arrows_border (menu, &arrow_border);
4352 view_height -= arrow_border.top;
4354 if ((last_visible != menu->upper_arrow_visible) &&
4355 !menu->upper_arrow_visible)
4357 menu->upper_arrow_prelight = FALSE;
4359 /* If we hid the upper arrow, possibly remove timeout */
4360 if (menu->scroll_step < 0)
4362 gtk_menu_stop_scrolling (menu);
4363 gtk_widget_queue_draw (GTK_WIDGET (menu));
4367 last_visible = menu->lower_arrow_visible;
4368 menu->lower_arrow_visible = offset < menu_height - view_height;
4370 /* lower_arrow_visible may have changed, so requery the border */
4371 get_arrows_border (menu, &arrow_border);
4372 view_height -= arrow_border.bottom;
4374 if ((last_visible != menu->lower_arrow_visible) &&
4375 !menu->lower_arrow_visible)
4377 menu->lower_arrow_prelight = FALSE;
4379 /* If we hid the lower arrow, possibly remove timeout */
4380 if (menu->scroll_step > 0)
4382 gtk_menu_stop_scrolling (menu);
4383 gtk_widget_queue_draw (GTK_WIDGET (menu));
4387 y += arrow_border.top;
4390 /* Scroll the menu: */
4391 if (GTK_WIDGET_REALIZED (menu))
4392 gdk_window_move (menu->bin_window, 0, -offset);
4394 if (GTK_WIDGET_REALIZED (menu))
4395 gdk_window_move_resize (menu->view_window,
4401 menu->scroll_offset = offset;
4405 compute_child_offset (GtkMenu *menu,
4406 GtkWidget *menu_item,
4409 gboolean *is_last_child)
4411 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4412 gint item_top_attach;
4413 gint item_bottom_attach;
4414 gint child_offset = 0;
4417 get_effective_child_attach (menu_item, NULL, NULL,
4418 &item_top_attach, &item_bottom_attach);
4420 /* there is a possibility that we get called before _size_request, so
4421 * check the height table for safety.
4423 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4426 /* when we have a row with only invisible children, it's height will
4427 * be zero, so there's no need to check WIDGET_VISIBLE here
4429 for (i = 0; i < item_top_attach; i++)
4430 child_offset += priv->heights[i];
4433 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4435 *offset = child_offset;
4437 *height = priv->heights[item_top_attach];
4443 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4444 GtkWidget *menu_item)
4447 gint child_offset, child_height;
4451 gboolean last_child = 0;
4453 menu = GTK_MENU (menu_shell);
4455 /* We need to check if the selected item fully visible.
4456 * If not we need to scroll the menu so that it becomes fully
4460 if (compute_child_offset (menu, menu_item,
4461 &child_offset, &child_height, &last_child))
4463 guint vertical_padding;
4464 gboolean double_arrows;
4466 y = menu->scroll_offset;
4467 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4469 gtk_widget_style_get (GTK_WIDGET (menu),
4470 "vertical-padding", &vertical_padding,
4473 double_arrows = get_double_arrows (menu);
4475 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4477 if (child_offset < y)
4479 /* Ignore the enter event we might get if the pointer is on the menu
4481 menu_shell->ignore_enter = TRUE;
4482 gtk_menu_scroll_to (menu, child_offset);
4486 GtkBorder arrow_border;
4490 get_arrows_border (menu, &arrow_border);
4491 if (!menu->tearoff_active)
4492 arrow_height = arrow_border.top + arrow_border.bottom;
4494 if (child_offset + child_height > y + height - arrow_height)
4497 if ((!last_child && !menu->tearoff_active) || double_arrows)
4498 arrow_height += arrow_border.bottom;
4500 y = child_offset + child_height - height + arrow_height;
4501 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4503 /* Need upper arrow */
4504 arrow_height += arrow_border.top;
4505 y = child_offset + child_height - height + arrow_height;
4507 /* Ignore the enter event we might get if the pointer is on the menu
4509 menu_shell->ignore_enter = TRUE;
4510 gtk_menu_scroll_to (menu, y);
4518 gtk_menu_select_item (GtkMenuShell *menu_shell,
4519 GtkWidget *menu_item)
4521 GtkMenu *menu = GTK_MENU (menu_shell);
4523 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4524 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4526 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4530 /* Reparent the menu, taking care of the refcounting
4532 * If unrealize is true we force a unrealize while reparenting the parent.
4533 * This can help eliminate flicker in some cases.
4535 * What happens is that when the menu is unrealized and then re-realized,
4536 * the allocations are as follows:
4538 * parent - 1x1 at (0,0)
4539 * child1 - 100x20 at (0,0)
4540 * child2 - 100x20 at (0,20)
4541 * child3 - 100x20 at (0,40)
4543 * That is, the parent is small but the children are full sized. Then,
4544 * when the queued_resize gets processed, the parent gets resized to
4547 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4548 * contains the following logic:
4550 * - if a move or resize operation on a window would change the clip
4551 * region on the children, then before the window is resized
4552 * the background for children is temporarily set to None, the
4553 * move/resize done, and the background for the children restored.
4555 * So, at the point where the parent is resized to final size, the
4556 * background for the children is temporarily None, and thus they
4557 * are not cleared to the background color and the previous background
4558 * (the image of the menu) is left in place.
4561 gtk_menu_reparent (GtkMenu *menu,
4562 GtkWidget *new_parent,
4565 GtkObject *object = GTK_OBJECT (menu);
4566 GtkWidget *widget = GTK_WIDGET (menu);
4567 gboolean was_floating = g_object_is_floating (object);
4569 g_object_ref_sink (object);
4573 g_object_ref (object);
4574 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4575 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4576 g_object_unref (object);
4579 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4582 g_object_force_floating (G_OBJECT (object));
4584 g_object_unref (object);
4588 gtk_menu_show_all (GtkWidget *widget)
4590 /* Show children, but not self. */
4591 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4596 gtk_menu_hide_all (GtkWidget *widget)
4598 /* Hide children, but not self. */
4599 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4603 * gtk_menu_set_screen:
4604 * @menu: a #GtkMenu.
4605 * @screen: a #GdkScreen, or %NULL if the screen should be
4606 * determined by the widget the menu is attached to.
4608 * Sets the #GdkScreen on which the menu will be displayed.
4613 gtk_menu_set_screen (GtkMenu *menu,
4616 g_return_if_fail (GTK_IS_MENU (menu));
4617 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4619 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4623 menu_change_screen (menu, screen);
4627 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4629 attach_widget_screen_changed (attach_widget, NULL, menu);
4635 * @menu: a #GtkMenu.
4636 * @child: a #GtkMenuItem.
4637 * @left_attach: The column number to attach the left side of the item to.
4638 * @right_attach: The column number to attach the right side of the item to.
4639 * @top_attach: The row number to attach the top of the item to.
4640 * @bottom_attach: The row number to attach the bottom of the item to.
4642 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4643 * an item will occupy is specified by @left_attach, @right_attach,
4644 * @top_attach and @bottom_attach. These each represent the leftmost,
4645 * rightmost, uppermost and lower column and row numbers of the table.
4646 * (Columns and rows are indexed from zero).
4648 * Note that this function is not related to gtk_menu_detach().
4653 gtk_menu_attach (GtkMenu *menu,
4658 guint bottom_attach)
4660 GtkMenuShell *menu_shell;
4662 g_return_if_fail (GTK_IS_MENU (menu));
4663 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4664 g_return_if_fail (child->parent == NULL ||
4665 child->parent == GTK_WIDGET (menu));
4666 g_return_if_fail (left_attach < right_attach);
4667 g_return_if_fail (top_attach < bottom_attach);
4669 menu_shell = GTK_MENU_SHELL (menu);
4673 AttachInfo *ai = get_attach_info (child);
4675 ai->left_attach = left_attach;
4676 ai->right_attach = right_attach;
4677 ai->top_attach = top_attach;
4678 ai->bottom_attach = bottom_attach;
4680 menu_shell->children = g_list_append (menu_shell->children, child);
4682 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4684 menu_queue_resize (menu);
4688 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4689 "left-attach", left_attach,
4690 "right-attach", right_attach,
4691 "top-attach", top_attach,
4692 "bottom-attach", bottom_attach,
4698 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4702 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4703 "gtk-menu-popup-delay", &popup_delay,
4710 find_child_containing (GtkMenuShell *menu_shell,
4718 /* find a child which includes the area given by
4719 * left, right, top, bottom.
4722 for (list = menu_shell->children; list; list = list->next)
4726 if (!_gtk_menu_item_is_selectable (list->data))
4729 get_effective_child_attach (list->data, &l, &r, &t, &b);
4731 if (l <= left && right <= r
4732 && t <= top && bottom <= b)
4733 return GTK_WIDGET (list->data);
4740 gtk_menu_move_current (GtkMenuShell *menu_shell,
4741 GtkMenuDirectionType direction)
4743 GtkMenu *menu = GTK_MENU (menu_shell);
4746 GtkWidget *match = NULL;
4748 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4752 case GTK_MENU_DIR_CHILD:
4753 direction = GTK_MENU_DIR_PARENT;
4755 case GTK_MENU_DIR_PARENT:
4756 direction = GTK_MENU_DIR_CHILD;
4762 /* use special table menu key bindings */
4763 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4765 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4767 if (direction == GTK_MENU_DIR_NEXT)
4769 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4771 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4779 for (i = 0; i < t; i++)
4781 match = find_child_containing (menu_shell,
4782 l, l + 1, i, i + 1);
4788 else if (direction == GTK_MENU_DIR_PREV)
4790 for (i = t; i > 0; i--)
4792 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4800 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4802 match = find_child_containing (menu_shell,
4803 l, l + 1, i - 1, i);
4809 else if (direction == GTK_MENU_DIR_PARENT)
4811 /* we go one left if possible */
4813 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4817 GtkWidget *parent = menu_shell->parent_menu_shell;
4820 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4821 match = menu_shell->active_menu_item;
4824 else if (direction == GTK_MENU_DIR_CHILD)
4826 /* we go one right if possible */
4827 if (r < gtk_menu_get_n_columns (menu))
4828 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4832 GtkWidget *parent = menu_shell->parent_menu_shell;
4834 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4836 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4837 match = menu_shell->active_menu_item;
4843 gtk_menu_shell_select_item (menu_shell, match);
4848 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
4852 get_visible_size (GtkMenu *menu)
4854 GtkWidget *widget = GTK_WIDGET (menu);
4855 GtkContainer *container = GTK_CONTAINER (menu);
4857 gint menu_height = (widget->allocation.height
4858 - 2 * (container->border_width
4859 + widget->style->ythickness));
4861 if (!menu->tearoff_active)
4863 GtkBorder arrow_border;
4865 get_arrows_border (menu, &arrow_border);
4866 menu_height -= arrow_border.top;
4867 menu_height -= arrow_border.bottom;
4873 /* Find the sensitive on-screen child containing @y, or if none,
4874 * the nearest selectable onscreen child. (%NULL if none)
4877 child_at (GtkMenu *menu,
4880 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4881 GtkWidget *child = NULL;
4882 gint child_offset = 0;
4885 gint lower, upper; /* Onscreen bounds */
4887 menu_height = get_visible_size (menu);
4888 lower = menu->scroll_offset;
4889 upper = menu->scroll_offset + menu_height;
4891 for (children = menu_shell->children; children; children = children->next)
4893 if (GTK_WIDGET_VISIBLE (children->data))
4895 GtkRequisition child_requisition;
4897 gtk_widget_size_request (children->data, &child_requisition);
4899 if (_gtk_menu_item_is_selectable (children->data) &&
4900 child_offset >= lower &&
4901 child_offset + child_requisition.height <= upper)
4903 child = children->data;
4905 if (child_offset + child_requisition.height > y &&
4906 !GTK_IS_TEAROFF_MENU_ITEM (child))
4910 child_offset += child_requisition.height;
4918 get_menu_height (GtkMenu *menu)
4921 GtkWidget *widget = GTK_WIDGET (menu);
4923 height = widget->requisition.height;
4924 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4926 if (!menu->tearoff_active)
4928 GtkBorder arrow_border;
4930 get_arrows_border (menu, &arrow_border);
4931 height -= arrow_border.top;
4932 height -= arrow_border.bottom;
4939 gtk_menu_real_move_scroll (GtkMenu *menu,
4942 gint page_size = get_visible_size (menu);
4943 gint end_position = get_menu_height (menu);
4944 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4948 case GTK_SCROLL_PAGE_UP:
4949 case GTK_SCROLL_PAGE_DOWN:
4953 gint child_offset = 0;
4954 gboolean old_upper_arrow_visible;
4957 if (type == GTK_SCROLL_PAGE_UP)
4962 if (menu_shell->active_menu_item)
4966 compute_child_offset (menu, menu_shell->active_menu_item,
4967 &child_offset, &child_height, NULL);
4968 child_offset += child_height / 2;
4971 menu_shell->ignore_enter = TRUE;
4972 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4973 old_offset = menu->scroll_offset;
4975 new_offset = menu->scroll_offset + step;
4976 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4978 gtk_menu_scroll_to (menu, new_offset);
4980 if (menu_shell->active_menu_item)
4982 GtkWidget *new_child;
4983 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4984 GtkBorder arrow_border;
4986 get_arrows_border (menu, &arrow_border);
4988 if (menu->scroll_offset != old_offset)
4989 step = menu->scroll_offset - old_offset;
4991 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
4993 new_child = child_at (menu, child_offset + step);
4995 gtk_menu_shell_select_item (menu_shell, new_child);
4999 case GTK_SCROLL_START:
5000 /* Ignore the enter event we might get if the pointer is on the menu
5002 menu_shell->ignore_enter = TRUE;
5003 gtk_menu_scroll_to (menu, 0);
5004 gtk_menu_shell_select_first (menu_shell, TRUE);
5006 case GTK_SCROLL_END:
5007 /* Ignore the enter event we might get if the pointer is on the menu
5009 menu_shell->ignore_enter = TRUE;
5010 gtk_menu_scroll_to (menu, end_position - page_size);
5011 _gtk_menu_shell_select_last (menu_shell, TRUE);
5020 * gtk_menu_set_monitor:
5022 * @monitor_num: the number of the monitor on which the menu should
5025 * Informs GTK+ on which monitor a menu should be popped up.
5026 * See gdk_screen_get_monitor_geometry().
5028 * This function should be called from a #GtkMenuPositionFunc if the
5029 * menu should not appear on the same monitor as the pointer. This
5030 * information can't be reliably inferred from the coordinates returned
5031 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5032 * may extend beyond the monitor boundaries or even the screen boundaries.
5037 gtk_menu_set_monitor (GtkMenu *menu,
5040 GtkMenuPrivate *priv;
5041 g_return_if_fail (GTK_IS_MENU (menu));
5043 priv = gtk_menu_get_private (menu);
5045 priv->monitor_num = monitor_num;
5049 * gtk_menu_get_monitor:
5052 * Retrieves the number of the monitor on which to show the menu.
5054 * Since: GSEAL-branch
5056 * Returns: the number of the monitor on which the menu should
5057 * be popped up or -1
5060 gtk_menu_get_monitor (GtkMenu *menu)
5062 GtkMenuPrivate *priv;
5063 g_return_if_fail (GTK_IS_MENU (menu));
5065 priv = gtk_menu_get_private (menu);
5067 return priv->monitor_num;
5071 * gtk_menu_get_for_attach_widget:
5072 * @widget: a #GtkWidget
5074 * Returns a list of the menus which are attached to this widget.
5075 * This list is owned by GTK+ and must not be modified.
5077 * Return value: the list of menus attached to his widget.
5082 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5086 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5088 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5093 gtk_menu_grab_notify (GtkWidget *widget,
5094 gboolean was_grabbed)
5096 GtkWidget *toplevel;
5097 GtkWindowGroup *group;
5100 toplevel = gtk_widget_get_toplevel (widget);
5101 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5102 grab = _gtk_window_group_get_current_grab (group);
5106 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5107 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5111 #define __GTK_MENU_C__
5112 #include "gtkaliasdef.c"