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;
130 CHILD_PROP_LEFT_ATTACH,
131 CHILD_PROP_RIGHT_ATTACH,
132 CHILD_PROP_TOP_ATTACH,
133 CHILD_PROP_BOTTOM_ATTACH
136 static void gtk_menu_set_property (GObject *object,
140 static void gtk_menu_get_property (GObject *object,
144 static void gtk_menu_set_child_property(GtkContainer *container,
149 static void gtk_menu_get_child_property(GtkContainer *container,
154 static void gtk_menu_destroy (GtkObject *object);
155 static void gtk_menu_finalize (GObject *object);
156 static void gtk_menu_realize (GtkWidget *widget);
157 static void gtk_menu_unrealize (GtkWidget *widget);
158 static void gtk_menu_size_request (GtkWidget *widget,
159 GtkRequisition *requisition);
160 static void gtk_menu_size_allocate (GtkWidget *widget,
161 GtkAllocation *allocation);
162 static void gtk_menu_paint (GtkWidget *widget,
163 GdkEventExpose *expose);
164 static void gtk_menu_show (GtkWidget *widget);
165 static gboolean gtk_menu_expose (GtkWidget *widget,
166 GdkEventExpose *event);
167 static gboolean gtk_menu_key_press (GtkWidget *widget,
169 static gboolean gtk_menu_scroll (GtkWidget *widget,
170 GdkEventScroll *event);
171 static gboolean gtk_menu_button_press (GtkWidget *widget,
172 GdkEventButton *event);
173 static gboolean gtk_menu_button_release (GtkWidget *widget,
174 GdkEventButton *event);
175 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
176 GdkEventMotion *event);
177 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
178 GdkEventCrossing *event);
179 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
180 GdkEventCrossing *event);
181 static void gtk_menu_scroll_to (GtkMenu *menu,
183 static void gtk_menu_grab_notify (GtkWidget *widget,
184 gboolean was_grabbed);
186 static void gtk_menu_stop_scrolling (GtkMenu *menu);
187 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
188 static gboolean gtk_menu_scroll_timeout (gpointer data);
189 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
190 static void gtk_menu_start_scrolling (GtkMenu *menu);
192 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
193 GtkWidget *menu_item);
194 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
195 GtkWidget *menu_item);
196 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
199 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
201 static void gtk_menu_handle_scrolling (GtkMenu *menu,
206 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
208 static void gtk_menu_style_set (GtkWidget *widget,
209 GtkStyle *previous_style);
210 static gboolean gtk_menu_focus (GtkWidget *widget,
211 GtkDirectionType direction);
212 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
213 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
214 GtkMenuDirectionType direction);
215 static void gtk_menu_real_move_scroll (GtkMenu *menu,
218 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
219 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
220 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
223 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
224 GtkMenuItem *menu_item,
225 GdkEventCrossing *event);
227 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
228 static void gtk_menu_show_all (GtkWidget *widget);
229 static void gtk_menu_hide_all (GtkWidget *widget);
230 static void gtk_menu_position (GtkMenu *menu);
231 static void gtk_menu_reparent (GtkMenu *menu,
232 GtkWidget *new_parent,
234 static void gtk_menu_remove (GtkContainer *menu,
237 static void gtk_menu_update_title (GtkMenu *menu);
239 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
240 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
242 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
244 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
245 gboolean group_changed);
247 static const gchar attach_data_key[] = "gtk-menu-attach-data";
249 static guint menu_signals[LAST_SIGNAL] = { 0 };
251 static GtkMenuPrivate *
252 gtk_menu_get_private (GtkMenu *menu)
254 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
257 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
260 menu_queue_resize (GtkMenu *menu)
262 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
264 priv->have_layout = FALSE;
265 gtk_widget_queue_resize (GTK_WIDGET (menu));
269 attach_info_free (AttachInfo *info)
271 g_slice_free (AttachInfo, info);
275 get_attach_info (GtkWidget *child)
277 GObject *object = G_OBJECT (child);
278 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
282 ai = g_slice_new0 (AttachInfo);
283 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai, attach_info_free);
290 is_grid_attached (AttachInfo *ai)
292 return (ai->left_attach >= 0 &&
293 ai->right_attach >= 0 &&
294 ai->top_attach >= 0 &&
295 ai->bottom_attach >= 0);
299 menu_ensure_layout (GtkMenu *menu)
301 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
303 if (!priv->have_layout)
305 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
309 gint max_right_attach;
310 gint max_bottom_attach;
312 /* Find extents of gridded portion
314 max_right_attach = 1;
315 max_bottom_attach = 0;
317 for (l = menu_shell->children; l; l = l->next)
319 GtkWidget *child = l->data;
320 AttachInfo *ai = get_attach_info (child);
322 if (is_grid_attached (ai))
324 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
325 max_right_attach = MAX (max_right_attach, ai->right_attach);
331 row_occupied = g_malloc0 (max_bottom_attach);
333 for (l = menu_shell->children; l; l = l->next)
335 GtkWidget *child = l->data;
336 AttachInfo *ai = get_attach_info (child);
338 if (is_grid_attached (ai))
342 for (i = ai->top_attach; i < ai->bottom_attach; i++)
343 row_occupied[i] = TRUE;
347 /* Lay non-grid-items out in those rows
350 for (l = menu_shell->children; l; l = l->next)
352 GtkWidget *child = l->data;
353 AttachInfo *ai = get_attach_info (child);
355 if (!is_grid_attached (ai))
357 while (current_row < max_bottom_attach && row_occupied[current_row])
360 ai->effective_left_attach = 0;
361 ai->effective_right_attach = max_right_attach;
362 ai->effective_top_attach = current_row;
363 ai->effective_bottom_attach = current_row + 1;
369 ai->effective_left_attach = ai->left_attach;
370 ai->effective_right_attach = ai->right_attach;
371 ai->effective_top_attach = ai->top_attach;
372 ai->effective_bottom_attach = ai->bottom_attach;
376 g_free (row_occupied);
378 priv->n_rows = MAX (current_row, max_bottom_attach);
379 priv->n_columns = max_right_attach;
380 priv->have_layout = TRUE;
386 gtk_menu_get_n_columns (GtkMenu *menu)
388 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
390 menu_ensure_layout (menu);
392 return priv->n_columns;
396 gtk_menu_get_n_rows (GtkMenu *menu)
398 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
400 menu_ensure_layout (menu);
406 get_effective_child_attach (GtkWidget *child,
412 GtkMenu *menu = GTK_MENU (child->parent);
415 menu_ensure_layout (menu);
417 ai = get_attach_info (child);
420 *l = ai->effective_left_attach;
422 *r = ai->effective_right_attach;
424 *t = ai->effective_top_attach;
426 *b = ai->effective_bottom_attach;
431 gtk_menu_class_init (GtkMenuClass *class)
433 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
434 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
435 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
436 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
437 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
438 GtkBindingSet *binding_set;
440 gobject_class->finalize = gtk_menu_finalize;
441 gobject_class->set_property = gtk_menu_set_property;
442 gobject_class->get_property = gtk_menu_get_property;
444 object_class->destroy = gtk_menu_destroy;
446 widget_class->realize = gtk_menu_realize;
447 widget_class->unrealize = gtk_menu_unrealize;
448 widget_class->size_request = gtk_menu_size_request;
449 widget_class->size_allocate = gtk_menu_size_allocate;
450 widget_class->show = gtk_menu_show;
451 widget_class->expose_event = gtk_menu_expose;
452 widget_class->scroll_event = gtk_menu_scroll;
453 widget_class->key_press_event = gtk_menu_key_press;
454 widget_class->button_press_event = gtk_menu_button_press;
455 widget_class->button_release_event = gtk_menu_button_release;
456 widget_class->motion_notify_event = gtk_menu_motion_notify;
457 widget_class->show_all = gtk_menu_show_all;
458 widget_class->hide_all = gtk_menu_hide_all;
459 widget_class->enter_notify_event = gtk_menu_enter_notify;
460 widget_class->leave_notify_event = gtk_menu_leave_notify;
461 widget_class->style_set = gtk_menu_style_set;
462 widget_class->focus = gtk_menu_focus;
463 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
464 widget_class->grab_notify = gtk_menu_grab_notify;
466 container_class->remove = gtk_menu_remove;
467 container_class->get_child_property = gtk_menu_get_child_property;
468 container_class->set_child_property = gtk_menu_set_child_property;
470 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
471 menu_shell_class->deactivate = gtk_menu_deactivate;
472 menu_shell_class->select_item = gtk_menu_select_item;
473 menu_shell_class->insert = gtk_menu_real_insert;
474 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
475 menu_shell_class->move_current = gtk_menu_move_current;
477 menu_signals[MOVE_SCROLL] =
478 _gtk_binding_signal_new (I_("move_scroll"),
479 G_OBJECT_CLASS_TYPE (object_class),
480 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
481 G_CALLBACK (gtk_menu_real_move_scroll),
483 _gtk_marshal_VOID__ENUM,
485 GTK_TYPE_SCROLL_TYPE);
487 g_object_class_install_property (gobject_class,
489 g_param_spec_string ("tearoff-title",
491 P_("A title that may be displayed by the window manager when this menu is torn-off"),
493 GTK_PARAM_READWRITE));
496 * GtkMenu:tearoff-state:
498 * A boolean that indicates whether the menu is torn-off.
502 g_object_class_install_property (gobject_class,
504 g_param_spec_boolean ("tearoff-state",
506 P_("A boolean that indicates whether the menu is torn-off"),
508 GTK_PARAM_READWRITE));
510 gtk_widget_class_install_style_property (widget_class,
511 g_param_spec_int ("vertical-padding",
512 P_("Vertical Padding"),
513 P_("Extra space at the top and bottom of the menu"),
517 GTK_PARAM_READABLE));
519 gtk_widget_class_install_style_property (widget_class,
520 g_param_spec_int ("horizontal-padding",
521 P_("Horizontal Padding"),
522 P_("Extra space at the left and right edges of the menu"),
526 GTK_PARAM_READABLE));
528 gtk_widget_class_install_style_property (widget_class,
529 g_param_spec_int ("vertical-offset",
530 P_("Vertical Offset"),
531 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
535 GTK_PARAM_READABLE));
537 gtk_widget_class_install_style_property (widget_class,
538 g_param_spec_int ("horizontal-offset",
539 P_("Horizontal Offset"),
540 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
544 GTK_PARAM_READABLE));
546 gtk_widget_class_install_style_property (widget_class,
547 g_param_spec_boolean ("double-arrows",
549 P_("When scrolling, always show both arrows."),
551 GTK_PARAM_READABLE));
554 gtk_container_class_install_child_property (container_class,
555 CHILD_PROP_LEFT_ATTACH,
556 g_param_spec_int ("left-attach",
558 P_("The column number to attach the left side of the child to"),
560 GTK_PARAM_READWRITE));
562 gtk_container_class_install_child_property (container_class,
563 CHILD_PROP_RIGHT_ATTACH,
564 g_param_spec_int ("right-attach",
566 P_("The column number to attach the right side of the child to"),
568 GTK_PARAM_READWRITE));
570 gtk_container_class_install_child_property (container_class,
571 CHILD_PROP_TOP_ATTACH,
572 g_param_spec_int ("top-attach",
574 P_("The row number to attach the top of the child to"),
576 GTK_PARAM_READWRITE));
578 gtk_container_class_install_child_property (container_class,
579 CHILD_PROP_BOTTOM_ATTACH,
580 g_param_spec_int ("bottom-attach",
582 P_("The row number to attach the bottom of the child to"),
584 GTK_PARAM_READWRITE));
586 binding_set = gtk_binding_set_by_class (class);
587 gtk_binding_entry_add_signal (binding_set,
589 I_("move_current"), 1,
590 GTK_TYPE_MENU_DIRECTION_TYPE,
592 gtk_binding_entry_add_signal (binding_set,
595 GTK_TYPE_MENU_DIRECTION_TYPE,
597 gtk_binding_entry_add_signal (binding_set,
600 GTK_TYPE_MENU_DIRECTION_TYPE,
602 gtk_binding_entry_add_signal (binding_set,
605 GTK_TYPE_MENU_DIRECTION_TYPE,
607 gtk_binding_entry_add_signal (binding_set,
610 GTK_TYPE_MENU_DIRECTION_TYPE,
611 GTK_MENU_DIR_PARENT);
612 gtk_binding_entry_add_signal (binding_set,
615 GTK_TYPE_MENU_DIRECTION_TYPE,
616 GTK_MENU_DIR_PARENT);
617 gtk_binding_entry_add_signal (binding_set,
620 GTK_TYPE_MENU_DIRECTION_TYPE,
622 gtk_binding_entry_add_signal (binding_set,
625 GTK_TYPE_MENU_DIRECTION_TYPE,
627 gtk_binding_entry_add_signal (binding_set,
630 GTK_TYPE_SCROLL_TYPE,
632 gtk_binding_entry_add_signal (binding_set,
635 GTK_TYPE_SCROLL_TYPE,
637 gtk_binding_entry_add_signal (binding_set,
640 GTK_TYPE_SCROLL_TYPE,
642 gtk_binding_entry_add_signal (binding_set,
645 GTK_TYPE_SCROLL_TYPE,
647 gtk_binding_entry_add_signal (binding_set,
650 GTK_TYPE_SCROLL_TYPE,
652 gtk_binding_entry_add_signal (binding_set,
655 GTK_TYPE_SCROLL_TYPE,
657 gtk_binding_entry_add_signal (binding_set,
660 GTK_TYPE_SCROLL_TYPE,
661 GTK_SCROLL_PAGE_DOWN);
662 gtk_binding_entry_add_signal (binding_set,
665 GTK_TYPE_SCROLL_TYPE,
666 GTK_SCROLL_PAGE_DOWN);
668 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
669 P_("Can change accelerators"),
670 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
672 GTK_PARAM_READWRITE));
674 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
675 P_("Delay before submenus appear"),
676 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
680 GTK_PARAM_READWRITE));
682 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
683 P_("Delay before hiding a submenu"),
684 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
687 DEFAULT_POPDOWN_DELAY,
688 GTK_PARAM_READWRITE));
690 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
695 gtk_menu_set_property (GObject *object,
702 menu = GTK_MENU (object);
706 case PROP_TEAROFF_STATE:
707 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
709 case PROP_TEAROFF_TITLE:
710 gtk_menu_set_title (menu, g_value_get_string (value));
713 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
719 gtk_menu_get_property (GObject *object,
726 menu = GTK_MENU (object);
730 case PROP_TEAROFF_STATE:
731 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
733 case PROP_TEAROFF_TITLE:
734 g_value_set_string (value, gtk_menu_get_title (menu));
737 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
743 gtk_menu_set_child_property (GtkContainer *container,
749 GtkMenu *menu = GTK_MENU (container);
750 AttachInfo *ai = get_attach_info (child);
754 case CHILD_PROP_LEFT_ATTACH:
755 ai->left_attach = g_value_get_int (value);
757 case CHILD_PROP_RIGHT_ATTACH:
758 ai->right_attach = g_value_get_int (value);
760 case CHILD_PROP_TOP_ATTACH:
761 ai->top_attach = g_value_get_int (value);
763 case CHILD_PROP_BOTTOM_ATTACH:
764 ai->bottom_attach = g_value_get_int (value);
768 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
772 menu_queue_resize (menu);
776 gtk_menu_get_child_property (GtkContainer *container,
782 AttachInfo *ai = get_attach_info (child);
786 case CHILD_PROP_LEFT_ATTACH:
787 g_value_set_int (value, ai->left_attach);
789 case CHILD_PROP_RIGHT_ATTACH:
790 g_value_set_int (value, ai->right_attach);
792 case CHILD_PROP_TOP_ATTACH:
793 g_value_set_int (value, ai->top_attach);
795 case CHILD_PROP_BOTTOM_ATTACH:
796 g_value_set_int (value, ai->bottom_attach);
800 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
806 gtk_menu_window_event (GtkWidget *window,
810 gboolean handled = FALSE;
812 g_object_ref (window);
818 case GDK_KEY_RELEASE:
819 handled = gtk_widget_event (menu, event);
825 g_object_unref (window);
826 g_object_unref (menu);
832 gtk_menu_window_size_request (GtkWidget *window,
833 GtkRequisition *requisition,
836 GtkMenuPrivate *private = gtk_menu_get_private (menu);
838 if (private->have_position)
840 GdkScreen *screen = gtk_widget_get_screen (window);
841 GdkRectangle monitor;
843 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
845 if (private->y + requisition->height > monitor.y + monitor.height)
846 requisition->height = monitor.y + monitor.height - private->y;
848 if (private->y < monitor.y)
849 requisition->height -= monitor.y - private->y;
854 gtk_menu_init (GtkMenu *menu)
856 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
858 menu->parent_menu_item = NULL;
859 menu->old_active_menu_item = NULL;
860 menu->accel_group = NULL;
861 menu->position_func = NULL;
862 menu->position_func_data = NULL;
863 menu->toggle_size = 0;
865 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
866 "type", GTK_WINDOW_POPUP,
869 "signal::event", gtk_menu_window_event, menu,
870 "signal::size_request", gtk_menu_window_size_request, menu,
871 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
873 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
874 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
876 /* Refloat the menu, so that reference counting for the menu isn't
877 * affected by it being a child of the toplevel
879 g_object_force_floating (G_OBJECT (menu));
880 menu->needs_destruction_ref_count = TRUE;
882 menu->view_window = NULL;
883 menu->bin_window = NULL;
885 menu->scroll_offset = 0;
886 menu->scroll_step = 0;
887 menu->timeout_id = 0;
888 menu->scroll_fast = FALSE;
890 menu->tearoff_window = NULL;
891 menu->tearoff_hbox = NULL;
892 menu->torn_off = FALSE;
893 menu->tearoff_active = FALSE;
894 menu->tearoff_adjustment = NULL;
895 menu->tearoff_scrollbar = NULL;
897 menu->upper_arrow_visible = FALSE;
898 menu->lower_arrow_visible = FALSE;
899 menu->upper_arrow_prelight = FALSE;
900 menu->lower_arrow_prelight = FALSE;
902 priv->upper_arrow_state = GTK_STATE_NORMAL;
903 priv->lower_arrow_state = GTK_STATE_NORMAL;
905 priv->have_layout = FALSE;
909 gtk_menu_destroy (GtkObject *object)
911 GtkMenu *menu = GTK_MENU (object);
912 GtkMenuAttachData *data;
913 GtkMenuPrivate *priv;
915 gtk_menu_remove_scroll_timeout (menu);
917 data = g_object_get_data (G_OBJECT (object), attach_data_key);
919 gtk_menu_detach (menu);
921 gtk_menu_stop_navigating_submenu (menu);
923 if (menu->old_active_menu_item)
925 g_object_unref (menu->old_active_menu_item);
926 menu->old_active_menu_item = NULL;
929 /* Add back the reference count for being a child */
930 if (menu->needs_destruction_ref_count)
932 menu->needs_destruction_ref_count = FALSE;
933 g_object_ref (object);
936 if (menu->accel_group)
938 g_object_unref (menu->accel_group);
939 menu->accel_group = NULL;
943 gtk_widget_destroy (menu->toplevel);
945 if (menu->tearoff_window)
946 gtk_widget_destroy (menu->tearoff_window);
948 priv = gtk_menu_get_private (menu);
952 g_free (priv->heights);
953 priv->heights = NULL;
958 g_free (priv->title);
962 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
966 gtk_menu_finalize (GObject *object)
968 GtkMenu *menu = GTK_MENU (object);
970 g_free (menu->accel_path);
972 G_OBJECT_CLASS (gtk_menu_parent_class)->finalize (object);
976 menu_change_screen (GtkMenu *menu,
977 GdkScreen *new_screen)
979 GtkMenuPrivate *private = gtk_menu_get_private (menu);
981 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
983 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
989 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
990 gtk_menu_position (menu);
993 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
994 private->monitor_num = -1;
998 attach_widget_screen_changed (GtkWidget *attach_widget,
999 GdkScreen *previous_screen,
1002 if (gtk_widget_has_screen (attach_widget) &&
1003 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1005 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1010 gtk_menu_attach_to_widget (GtkMenu *menu,
1011 GtkWidget *attach_widget,
1012 GtkMenuDetachFunc detacher)
1014 GtkMenuAttachData *data;
1017 g_return_if_fail (GTK_IS_MENU (menu));
1018 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1020 /* keep this function in sync with gtk_widget_set_parent()
1023 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1026 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1027 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1031 g_object_ref_sink (menu);
1033 data = g_slice_new (GtkMenuAttachData);
1034 data->attach_widget = attach_widget;
1036 g_signal_connect (attach_widget, "screen_changed",
1037 G_CALLBACK (attach_widget_screen_changed), menu);
1038 attach_widget_screen_changed (attach_widget, NULL, menu);
1040 data->detacher = detacher;
1041 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1042 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1043 if (!g_list_find (list, menu))
1045 list = g_list_prepend (list, menu);
1047 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
1049 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1050 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1052 /* we don't need to set the style here, since
1053 * we are a toplevel widget.
1056 /* Fallback title for menu comes from attach widget */
1057 gtk_menu_update_title (menu);
1061 gtk_menu_get_attach_widget (GtkMenu *menu)
1063 GtkMenuAttachData *data;
1065 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1067 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1069 return data->attach_widget;
1074 gtk_menu_detach (GtkMenu *menu)
1076 GtkMenuAttachData *data;
1079 g_return_if_fail (GTK_IS_MENU (menu));
1081 /* keep this function in sync with gtk_widget_unparent()
1083 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1086 g_warning ("gtk_menu_detach(): menu is not attached");
1089 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1091 g_signal_handlers_disconnect_by_func (data->attach_widget,
1092 (gpointer) attach_widget_screen_changed,
1096 data->detacher (data->attach_widget, menu);
1097 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1098 list = g_list_remove (list, menu);
1100 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
1102 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1104 if (GTK_WIDGET_REALIZED (menu))
1105 gtk_widget_unrealize (GTK_WIDGET (menu));
1107 g_slice_free (GtkMenuAttachData, data);
1109 /* Fallback title for menu comes from attach widget */
1110 gtk_menu_update_title (menu);
1112 g_object_unref (menu);
1116 gtk_menu_remove (GtkContainer *container,
1119 GtkMenu *menu = GTK_MENU (container);
1121 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1123 /* Clear out old_active_menu_item if it matches the item we are removing
1125 if (menu->old_active_menu_item == widget)
1127 g_object_unref (menu->old_active_menu_item);
1128 menu->old_active_menu_item = NULL;
1131 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1132 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1134 menu_queue_resize (menu);
1140 return g_object_new (GTK_TYPE_MENU, NULL);
1144 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1148 GtkMenu *menu = GTK_MENU (menu_shell);
1149 AttachInfo *ai = get_attach_info (child);
1151 ai->left_attach = -1;
1152 ai->right_attach = -1;
1153 ai->top_attach = -1;
1154 ai->bottom_attach = -1;
1156 if (GTK_WIDGET_REALIZED (menu_shell))
1157 gtk_widget_set_parent_window (child, menu->bin_window);
1159 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1161 menu_queue_resize (menu);
1165 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1170 widget = GTK_WIDGET (menu);
1176 GdkGCValues gc_values;
1178 menu->tearoff_active = FALSE;
1179 menu->saved_scroll_offset = menu->scroll_offset;
1181 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1182 gc = gdk_gc_new_with_values (widget->window,
1183 &gc_values, GDK_GC_SUBWINDOW);
1185 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1187 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1192 gdk_draw_drawable (pixmap, gc,
1193 menu->tearoff_window->window,
1194 0, 0, 0, 0, -1, -1);
1195 g_object_unref (gc);
1197 gtk_widget_set_size_request (menu->tearoff_window,
1201 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1202 g_object_unref (pixmap);
1207 popup_grab_on_window (GdkWindow *window,
1208 guint32 activate_time,
1209 gboolean grab_keyboard)
1211 if ((gdk_pointer_grab (window, TRUE,
1212 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1213 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1214 GDK_POINTER_MOTION_MASK,
1215 NULL, NULL, activate_time) == 0))
1217 if (!grab_keyboard ||
1218 gdk_keyboard_grab (window, TRUE,
1219 activate_time) == 0)
1223 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1234 * @menu: a #GtkMenu.
1235 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1236 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1237 * @func: a user supplied function used to position the menu, or %NULL
1238 * @data: user supplied data to be passed to @func.
1239 * @button: the mouse button which was pressed to initiate the event.
1240 * @activate_time: the time at which the activation event occurred.
1242 * Displays a menu and makes it available for selection. Applications can use
1243 * this function to display context-sensitive menus, and will typically supply
1244 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1245 * parameters. The default menu positioning function will position the menu
1246 * at the current mouse cursor position.
1248 * The @button parameter should be the mouse button pressed to initiate
1249 * the menu popup. If the menu popup was initiated by something other than
1250 * a mouse button press, such as a mouse button release or a keypress,
1251 * @button should be 0.
1253 * The @activate_time parameter is used to conflict-resolve initiation of
1254 * concurrent requests for mouse/keyboard grab requests. To function
1255 * properly, this needs to be the time stamp of the user event (such as
1256 * a mouse click or key press) that caused the initiation of the popup.
1257 * Only if no such event is available, gtk_get_current_event_time() can
1261 gtk_menu_popup (GtkMenu *menu,
1262 GtkWidget *parent_menu_shell,
1263 GtkWidget *parent_menu_item,
1264 GtkMenuPositionFunc func,
1267 guint32 activate_time)
1270 GtkWidget *xgrab_shell;
1272 GdkEvent *current_event;
1273 GtkMenuShell *menu_shell;
1274 gboolean grab_keyboard;
1275 GtkMenuPrivate *priv;
1276 GtkWidget *parent_toplevel;
1278 g_return_if_fail (GTK_IS_MENU (menu));
1280 widget = GTK_WIDGET (menu);
1281 menu_shell = GTK_MENU_SHELL (menu);
1282 priv = gtk_menu_get_private (menu);
1284 menu_shell->parent_menu_shell = parent_menu_shell;
1286 priv->seen_item_enter = FALSE;
1288 /* Find the last viewable ancestor, and make an X grab on it
1290 parent = GTK_WIDGET (menu);
1294 gboolean viewable = TRUE;
1295 GtkWidget *tmp = parent;
1299 if (!GTK_WIDGET_MAPPED (tmp))
1308 xgrab_shell = parent;
1310 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1313 /* We want to receive events generated when we map the menu; unfortunately,
1314 * since there is probably already an implicit grab in place from the
1315 * button that the user used to pop up the menu, we won't receive then --
1316 * in particular, the EnterNotify when the menu pops up under the pointer.
1318 * If we are grabbing on a parent menu shell, no problem; just grab on
1319 * that menu shell first before popping up the window with owner_events = TRUE.
1321 * When grabbing on the menu itself, things get more convuluted - we
1322 * we do an explicit grab on a specially created window with
1323 * owner_events = TRUE, which we override further down with a grab
1324 * on the menu. (We can't grab on the menu until it is mapped; we
1325 * probably could just leave the grab on the other window, with a
1326 * little reorganization of the code in gtkmenu*).
1328 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1329 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1331 if (xgrab_shell && xgrab_shell != widget)
1333 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1334 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1338 GdkWindow *transfer_window;
1340 xgrab_shell = widget;
1341 transfer_window = menu_grab_transfer_window_get (menu);
1342 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1343 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1346 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1348 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1349 * with a stuck up window, we just abort here. Presumably the user will
1352 menu_shell->parent_menu_shell = NULL;
1353 menu_grab_transfer_window_destroy (menu);
1357 menu_shell->active = TRUE;
1358 menu_shell->button = button;
1360 /* If we are popping up the menu from something other than, a button
1361 * press then, as a heuristic, we ignore enter events for the menu
1362 * until we get a MOTION_NOTIFY.
1365 current_event = gtk_get_current_event ();
1368 if ((current_event->type != GDK_BUTTON_PRESS) &&
1369 (current_event->type != GDK_ENTER_NOTIFY))
1370 menu_shell->ignore_enter = TRUE;
1372 gdk_event_free (current_event);
1375 menu_shell->ignore_enter = TRUE;
1379 gtk_menu_tearoff_bg_copy (menu);
1381 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1384 parent_toplevel = NULL;
1385 if (parent_menu_shell)
1386 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1387 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1389 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1391 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1394 /* Set transient for to get the right window group and parent relationship */
1395 if (GTK_IS_WINDOW (parent_toplevel))
1396 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1397 GTK_WINDOW (parent_toplevel));
1399 menu->parent_menu_item = parent_menu_item;
1400 menu->position_func = func;
1401 menu->position_func_data = data;
1402 menu_shell->activate_time = activate_time;
1404 /* We need to show the menu here rather in the init function because
1405 * code expects to be able to tell if the menu is onscreen by
1406 * looking at the GTK_WIDGET_VISIBLE (menu)
1408 gtk_widget_show (GTK_WIDGET (menu));
1410 /* Position the menu, possibly changing the size request
1412 gtk_menu_position (menu);
1414 /* Compute the size of the toplevel and realize it so we
1415 * can scroll correctly.
1418 GtkRequisition tmp_request;
1419 GtkAllocation tmp_allocation = { 0, };
1421 gtk_widget_size_request (menu->toplevel, &tmp_request);
1423 tmp_allocation.width = tmp_request.width;
1424 tmp_allocation.height = tmp_request.height;
1426 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1428 gtk_widget_realize (GTK_WIDGET (menu));
1431 gtk_menu_scroll_to (menu, menu->scroll_offset);
1433 /* if no item is selected, select the first one */
1434 if (!menu_shell->active_menu_item)
1436 gboolean touchscreen_mode;
1438 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1439 "gtk-touchscreen-mode", &touchscreen_mode,
1442 if (touchscreen_mode)
1443 gtk_menu_shell_select_first (menu_shell, TRUE);
1446 /* Once everything is set up correctly, map the toplevel window on
1449 gtk_widget_show (menu->toplevel);
1451 if (xgrab_shell == widget)
1452 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1453 gtk_grab_add (GTK_WIDGET (menu));
1457 gtk_menu_popdown (GtkMenu *menu)
1459 GtkMenuPrivate *private;
1460 GtkMenuShell *menu_shell;
1462 g_return_if_fail (GTK_IS_MENU (menu));
1464 menu_shell = GTK_MENU_SHELL (menu);
1465 private = gtk_menu_get_private (menu);
1467 menu_shell->parent_menu_shell = NULL;
1468 menu_shell->active = FALSE;
1469 menu_shell->ignore_enter = FALSE;
1471 private->have_position = FALSE;
1473 gtk_menu_stop_scrolling (menu);
1475 gtk_menu_stop_navigating_submenu (menu);
1477 if (menu_shell->active_menu_item)
1479 if (menu->old_active_menu_item)
1480 g_object_unref (menu->old_active_menu_item);
1481 menu->old_active_menu_item = menu_shell->active_menu_item;
1482 g_object_ref (menu->old_active_menu_item);
1485 gtk_menu_shell_deselect (menu_shell);
1487 /* The X Grab, if present, will automatically be removed when we hide
1489 gtk_widget_hide (menu->toplevel);
1490 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1494 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1496 if (GTK_BIN (menu->toplevel)->child)
1498 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1502 /* We popped up the menu from the tearoff, so we need to
1503 * release the grab - we aren't actually hiding the menu.
1505 if (menu_shell->have_xgrab)
1507 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1509 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1510 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1514 /* gtk_menu_popdown is called each time a menu item is selected from
1515 * a torn off menu. Only scroll back to the saved position if the
1516 * non-tearoff menu was popped down.
1518 if (!menu->tearoff_active)
1519 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1520 menu->tearoff_active = TRUE;
1523 gtk_widget_hide (GTK_WIDGET (menu));
1525 menu_shell->have_xgrab = FALSE;
1526 gtk_grab_remove (GTK_WIDGET (menu));
1528 menu_grab_transfer_window_destroy (menu);
1532 gtk_menu_get_active (GtkMenu *menu)
1537 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1539 if (!menu->old_active_menu_item)
1542 children = GTK_MENU_SHELL (menu)->children;
1546 child = children->data;
1547 children = children->next;
1549 if (GTK_BIN (child)->child)
1554 menu->old_active_menu_item = child;
1555 if (menu->old_active_menu_item)
1556 g_object_ref (menu->old_active_menu_item);
1559 return menu->old_active_menu_item;
1563 gtk_menu_set_active (GtkMenu *menu,
1569 g_return_if_fail (GTK_IS_MENU (menu));
1571 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1574 child = tmp_list->data;
1575 if (GTK_BIN (child)->child)
1577 if (menu->old_active_menu_item)
1578 g_object_unref (menu->old_active_menu_item);
1579 menu->old_active_menu_item = child;
1580 g_object_ref (menu->old_active_menu_item);
1586 gtk_menu_set_accel_group (GtkMenu *menu,
1587 GtkAccelGroup *accel_group)
1589 g_return_if_fail (GTK_IS_MENU (menu));
1591 if (menu->accel_group != accel_group)
1593 if (menu->accel_group)
1594 g_object_unref (menu->accel_group);
1595 menu->accel_group = accel_group;
1596 if (menu->accel_group)
1597 g_object_ref (menu->accel_group);
1598 _gtk_menu_refresh_accel_paths (menu, TRUE);
1603 gtk_menu_get_accel_group (GtkMenu *menu)
1605 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1607 return menu->accel_group;
1611 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1614 /* Menu items chain here to figure whether they can activate their
1615 * accelerators. Unlike ordinary widgets, menus allow accel
1616 * activation even if invisible since that's the usual case for
1617 * submenus/popup-menus. however, the state of the attach widget
1618 * affects the "activeness" of the menu.
1620 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1623 return gtk_widget_can_activate_accel (awidget, signal_id);
1625 return GTK_WIDGET_IS_SENSITIVE (widget);
1629 * gtk_menu_set_accel_path
1630 * @menu: a valid #GtkMenu
1631 * @accel_path: a valid accelerator path
1633 * Sets an accelerator path for this menu from which accelerator paths
1634 * for its immediate children, its menu items, can be constructed.
1635 * The main purpose of this function is to spare the programmer the
1636 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1637 * each menu item that should support runtime user changable accelerators.
1638 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1639 * each menu item of this menu, that contains a label describing its purpose,
1640 * automatically gets an accel path assigned. For example, a menu containing
1641 * menu items "New" and "Exit", will, after
1642 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1643 * has been called, assign its items the accel paths:
1644 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1645 * Assigning accel paths to menu items then enables the user to change
1646 * their accelerators at runtime. More details about accelerator paths
1647 * and their default setups can be found at gtk_accel_map_add_entry().
1650 gtk_menu_set_accel_path (GtkMenu *menu,
1651 const gchar *accel_path)
1653 gchar *old_accel_path;
1655 g_return_if_fail (GTK_IS_MENU (menu));
1657 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1659 old_accel_path = menu->accel_path;
1660 menu->accel_path = g_strdup (accel_path);
1661 g_free (old_accel_path);
1662 if (menu->accel_path)
1663 _gtk_menu_refresh_accel_paths (menu, FALSE);
1668 gboolean group_changed;
1672 refresh_accel_paths_foreach (GtkWidget *widget,
1675 AccelPropagation *prop = data;
1677 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1678 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1679 prop->menu->accel_path,
1680 prop->menu->accel_group,
1681 prop->group_changed);
1685 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1686 gboolean group_changed)
1688 g_return_if_fail (GTK_IS_MENU (menu));
1690 if (menu->accel_path && menu->accel_group)
1692 AccelPropagation prop;
1695 prop.group_changed = group_changed;
1696 gtk_container_foreach (GTK_CONTAINER (menu),
1697 refresh_accel_paths_foreach,
1703 gtk_menu_reposition (GtkMenu *menu)
1705 g_return_if_fail (GTK_IS_MENU (menu));
1707 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1708 gtk_menu_position (menu);
1712 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1715 g_return_if_fail (GTK_IS_MENU (menu));
1717 if (adjustment->value != menu->scroll_offset)
1718 gtk_menu_scroll_to (menu, adjustment->value);
1722 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1725 GdkGeometry geometry_hints;
1727 if (!menu->tearoff_window)
1730 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1732 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1733 width += menu->tearoff_scrollbar->requisition.width;
1736 geometry_hints.min_width = width;
1737 geometry_hints.max_width = width;
1739 geometry_hints.min_height = 0;
1740 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1742 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1745 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1749 gtk_menu_update_title (GtkMenu *menu)
1751 if (menu->tearoff_window)
1754 GtkWidget *attach_widget;
1756 title = gtk_menu_get_title (menu);
1759 attach_widget = gtk_menu_get_attach_widget (menu);
1760 if (GTK_IS_MENU_ITEM (attach_widget))
1762 GtkWidget *child = GTK_BIN (attach_widget)->child;
1763 if (GTK_IS_LABEL (child))
1764 title = gtk_label_get_text (GTK_LABEL (child));
1769 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1774 gtk_menu_get_toplevel (GtkWidget *menu)
1776 GtkWidget *attach, *toplevel;
1778 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1780 if (GTK_IS_MENU_ITEM (attach))
1781 attach = attach->parent;
1783 if (GTK_IS_MENU (attach))
1784 return gtk_menu_get_toplevel (attach);
1785 else if (GTK_IS_WIDGET (attach))
1787 toplevel = gtk_widget_get_toplevel (attach);
1788 if (GTK_WIDGET_TOPLEVEL (toplevel))
1796 tearoff_window_destroyed (GtkWidget *widget,
1799 gtk_menu_set_tearoff_state (menu, FALSE);
1803 gtk_menu_set_tearoff_state (GtkMenu *menu,
1808 g_return_if_fail (GTK_IS_MENU (menu));
1810 if (menu->torn_off != torn_off)
1812 menu->torn_off = torn_off;
1813 menu->tearoff_active = torn_off;
1817 if (GTK_WIDGET_VISIBLE (menu))
1818 gtk_menu_popdown (menu);
1820 if (!menu->tearoff_window)
1822 GtkWidget *toplevel;
1824 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1825 "type", GTK_WINDOW_TOPLEVEL,
1826 "screen", gtk_widget_get_screen (menu->toplevel),
1827 "app-paintable", TRUE,
1830 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1831 GDK_WINDOW_TYPE_HINT_MENU);
1832 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1833 g_signal_connect (menu->tearoff_window, "destroy",
1834 G_CALLBACK (tearoff_window_destroyed), menu);
1835 g_signal_connect (menu->tearoff_window, "event",
1836 G_CALLBACK (gtk_menu_window_event), menu);
1838 gtk_menu_update_title (menu);
1840 gtk_widget_realize (menu->tearoff_window);
1842 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1843 if (toplevel != NULL)
1844 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1845 GTK_WINDOW (toplevel));
1847 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1848 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1850 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1851 menu->tearoff_adjustment =
1852 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1854 GTK_WIDGET (menu)->requisition.height,
1858 g_object_connect (menu->tearoff_adjustment,
1859 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1861 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1863 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1864 menu->tearoff_scrollbar,
1867 if (menu->tearoff_adjustment->upper > height)
1868 gtk_widget_show (menu->tearoff_scrollbar);
1870 gtk_widget_show (menu->tearoff_hbox);
1873 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1875 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1877 /* Update menu->requisition
1879 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1881 gtk_menu_set_tearoff_hints (menu, width);
1883 gtk_widget_realize (menu->tearoff_window);
1884 gtk_menu_position (menu);
1886 gtk_widget_show (GTK_WIDGET (menu));
1887 gtk_widget_show (menu->tearoff_window);
1889 gtk_menu_scroll_to (menu, 0);
1894 gtk_widget_hide (GTK_WIDGET (menu));
1895 gtk_widget_hide (menu->tearoff_window);
1896 if (GTK_IS_CONTAINER (menu->toplevel))
1897 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1898 gtk_widget_destroy (menu->tearoff_window);
1900 menu->tearoff_window = NULL;
1901 menu->tearoff_hbox = NULL;
1902 menu->tearoff_scrollbar = NULL;
1903 menu->tearoff_adjustment = NULL;
1906 g_object_notify (G_OBJECT (menu), "tearoff-state");
1911 * gtk_menu_get_tearoff_state:
1914 * Returns whether the menu is torn off. See
1915 * gtk_menu_set_tearoff_state ().
1917 * Return value: %TRUE if the menu is currently torn off.
1920 gtk_menu_get_tearoff_state (GtkMenu *menu)
1922 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1924 return menu->torn_off;
1928 * gtk_menu_set_title:
1930 * @title: a string containing the title for the menu.
1932 * Sets the title string for the menu. The title is displayed when the menu
1933 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
1934 * attached to a parent menu item, and if so it will try to use the same text as
1935 * that menu item's label.
1938 gtk_menu_set_title (GtkMenu *menu,
1941 GtkMenuPrivate *priv;
1944 g_return_if_fail (GTK_IS_MENU (menu));
1946 priv = gtk_menu_get_private (menu);
1948 old_title = priv->title;
1949 priv->title = g_strdup (title);
1952 gtk_menu_update_title (menu);
1953 g_object_notify (G_OBJECT (menu), "tearoff-title");
1957 * gtk_menu_get_title:
1960 * Returns the title of the menu. See gtk_menu_set_title().
1962 * Return value: the title of the menu, or %NULL if the menu has no
1963 * title set on it. This string is owned by the widget and should
1964 * not be modified or freed.
1966 G_CONST_RETURN gchar *
1967 gtk_menu_get_title (GtkMenu *menu)
1969 GtkMenuPrivate *priv;
1971 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1973 priv = gtk_menu_get_private (menu);
1979 gtk_menu_reorder_child (GtkMenu *menu,
1983 GtkMenuShell *menu_shell;
1985 g_return_if_fail (GTK_IS_MENU (menu));
1986 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1988 menu_shell = GTK_MENU_SHELL (menu);
1990 if (g_list_find (menu_shell->children, child))
1992 menu_shell->children = g_list_remove (menu_shell->children, child);
1993 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1995 menu_queue_resize (menu);
2000 gtk_menu_style_set (GtkWidget *widget,
2001 GtkStyle *previous_style)
2003 if (GTK_WIDGET_REALIZED (widget))
2005 GtkMenu *menu = GTK_MENU (widget);
2007 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2008 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2009 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2014 get_arrows_border (GtkMenu *menu, GtkBorder *border)
2016 guint scroll_arrow_height;
2018 gtk_widget_style_get (GTK_WIDGET (menu),
2019 "scroll-arrow-vlength", &scroll_arrow_height,
2022 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2023 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2025 border->left = border->right = 0;
2029 gtk_menu_realize (GtkWidget *widget)
2031 GdkWindowAttr attributes;
2032 gint attributes_mask;
2037 guint vertical_padding;
2038 guint horizontal_padding;
2039 GtkBorder arrow_border;
2041 g_return_if_fail (GTK_IS_MENU (widget));
2043 menu = GTK_MENU (widget);
2045 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2047 attributes.window_type = GDK_WINDOW_CHILD;
2048 attributes.x = widget->allocation.x;
2049 attributes.y = widget->allocation.y;
2050 attributes.width = widget->allocation.width;
2051 attributes.height = widget->allocation.height;
2052 attributes.wclass = GDK_INPUT_OUTPUT;
2053 attributes.visual = gtk_widget_get_visual (widget);
2054 attributes.colormap = gtk_widget_get_colormap (widget);
2056 attributes.event_mask = gtk_widget_get_events (widget);
2058 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2059 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2061 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2062 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2063 gdk_window_set_user_data (widget->window, widget);
2065 border_width = GTK_CONTAINER (widget)->border_width;
2067 gtk_widget_style_get (GTK_WIDGET (menu),
2068 "vertical-padding", &vertical_padding,
2069 "horizontal-padding", &horizontal_padding,
2072 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2073 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2074 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2075 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2077 get_arrows_border (menu, &arrow_border);
2078 attributes.y += arrow_border.top;
2079 attributes.height -= arrow_border.top;
2080 attributes.height -= arrow_border.bottom;
2082 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2083 gdk_window_set_user_data (menu->view_window, menu);
2087 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2088 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2090 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2091 gdk_window_set_user_data (menu->bin_window, menu);
2093 children = GTK_MENU_SHELL (menu)->children;
2096 child = children->data;
2097 children = children->next;
2099 gtk_widget_set_parent_window (child, menu->bin_window);
2102 widget->style = gtk_style_attach (widget->style, widget->window);
2103 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2104 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2105 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2107 if (GTK_MENU_SHELL (widget)->active_menu_item)
2108 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2109 GTK_MENU_SHELL (widget)->active_menu_item);
2111 gdk_window_show (menu->bin_window);
2112 gdk_window_show (menu->view_window);
2116 gtk_menu_focus (GtkWidget *widget,
2117 GtkDirectionType direction)
2120 * A menu or its menu items cannot have focus
2125 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2128 menu_grab_transfer_window_get (GtkMenu *menu)
2130 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2133 GdkWindowAttr attributes;
2134 gint attributes_mask;
2136 attributes.x = -100;
2137 attributes.y = -100;
2138 attributes.width = 10;
2139 attributes.height = 10;
2140 attributes.window_type = GDK_WINDOW_TEMP;
2141 attributes.wclass = GDK_INPUT_ONLY;
2142 attributes.override_redirect = TRUE;
2143 attributes.event_mask = 0;
2145 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2147 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2148 &attributes, attributes_mask);
2149 gdk_window_set_user_data (window, menu);
2151 gdk_window_show (window);
2153 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2160 menu_grab_transfer_window_destroy (GtkMenu *menu)
2162 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2165 gdk_window_set_user_data (window, NULL);
2166 gdk_window_destroy (window);
2167 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2172 gtk_menu_unrealize (GtkWidget *widget)
2174 GtkMenu *menu = GTK_MENU (widget);
2176 menu_grab_transfer_window_destroy (menu);
2178 gdk_window_set_user_data (menu->view_window, NULL);
2179 gdk_window_destroy (menu->view_window);
2180 menu->view_window = NULL;
2182 gdk_window_set_user_data (menu->bin_window, NULL);
2183 gdk_window_destroy (menu->bin_window);
2184 menu->bin_window = NULL;
2186 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize) (widget);
2190 gtk_menu_size_request (GtkWidget *widget,
2191 GtkRequisition *requisition)
2195 GtkMenuShell *menu_shell;
2198 guint max_toggle_size;
2199 guint max_accel_width;
2200 guint vertical_padding;
2201 guint horizontal_padding;
2202 GtkRequisition child_requisition;
2203 GtkMenuPrivate *priv;
2205 g_return_if_fail (GTK_IS_MENU (widget));
2206 g_return_if_fail (requisition != NULL);
2208 menu = GTK_MENU (widget);
2209 menu_shell = GTK_MENU_SHELL (widget);
2210 priv = gtk_menu_get_private (menu);
2212 requisition->width = 0;
2213 requisition->height = 0;
2215 max_toggle_size = 0;
2216 max_accel_width = 0;
2218 g_free (priv->heights);
2219 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2220 priv->heights_length = gtk_menu_get_n_rows (menu);
2222 children = menu_shell->children;
2229 child = children->data;
2230 children = children->next;
2232 if (! GTK_WIDGET_VISIBLE (child))
2235 get_effective_child_attach (child, &l, &r, &t, &b);
2237 /* It's important to size_request the child
2238 * before doing the toggle size request, in
2239 * case the toggle size request depends on the size
2240 * request of a child of the child (e.g. for ImageMenuItem)
2243 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2244 gtk_widget_size_request (child, &child_requisition);
2246 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2247 max_toggle_size = MAX (max_toggle_size, toggle_size);
2248 max_accel_width = MAX (max_accel_width,
2249 GTK_MENU_ITEM (child)->accelerator_width);
2251 part = child_requisition.width / (r - l);
2252 requisition->width = MAX (requisition->width, part);
2254 part = MAX (child_requisition.height, toggle_size) / (b - t);
2255 priv->heights[t] = MAX (priv->heights[t], part);
2258 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2259 requisition->height += priv->heights[i];
2261 requisition->width += max_toggle_size + max_accel_width;
2262 requisition->width *= gtk_menu_get_n_columns (menu);
2264 gtk_widget_style_get (GTK_WIDGET (menu),
2265 "vertical-padding", &vertical_padding,
2266 "horizontal-padding", &horizontal_padding,
2269 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2270 widget->style->xthickness) * 2;
2271 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2272 widget->style->ythickness) * 2;
2274 menu->toggle_size = max_toggle_size;
2276 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2278 if (menu->tearoff_active)
2279 gtk_menu_set_tearoff_hints (menu, requisition->width);
2283 gtk_menu_size_allocate (GtkWidget *widget,
2284 GtkAllocation *allocation)
2287 GtkMenuShell *menu_shell;
2289 GtkAllocation child_allocation;
2290 GtkRequisition child_requisition;
2291 GtkMenuPrivate *priv;
2295 guint vertical_padding;
2296 guint horizontal_padding;
2298 g_return_if_fail (GTK_IS_MENU (widget));
2299 g_return_if_fail (allocation != NULL);
2301 menu = GTK_MENU (widget);
2302 menu_shell = GTK_MENU_SHELL (widget);
2303 priv = gtk_menu_get_private (menu);
2305 widget->allocation = *allocation;
2306 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2308 gtk_widget_style_get (GTK_WIDGET (menu),
2309 "vertical-padding", &vertical_padding,
2310 "horizontal-padding", &horizontal_padding,
2313 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2314 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2316 width = MAX (1, allocation->width - x * 2);
2317 height = MAX (1, allocation->height - y * 2);
2319 child_requisition.width -= x * 2;
2320 child_requisition.height -= y * 2;
2322 if (menu_shell->active)
2323 gtk_menu_scroll_to (menu, menu->scroll_offset);
2325 if (!menu->tearoff_active)
2327 GtkBorder arrow_border;
2329 get_arrows_border (menu, &arrow_border);
2330 y += arrow_border.top;
2331 height -= arrow_border.top;
2332 height -= arrow_border.bottom;
2335 if (GTK_WIDGET_REALIZED (widget))
2337 gdk_window_move_resize (widget->window,
2338 allocation->x, allocation->y,
2339 allocation->width, allocation->height);
2341 gdk_window_move_resize (menu->view_window,
2348 if (menu_shell->children)
2350 gint base_width = width / gtk_menu_get_n_columns (menu);
2352 children = menu_shell->children;
2355 child = children->data;
2356 children = children->next;
2358 if (GTK_WIDGET_VISIBLE (child))
2363 get_effective_child_attach (child, &l, &r, &t, &b);
2365 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2368 tmp = gtk_menu_get_n_columns (menu) - l;
2369 l = gtk_menu_get_n_columns (menu) - r;
2373 child_allocation.width = (r - l) * base_width;
2374 child_allocation.height = 0;
2375 child_allocation.x = l * base_width;
2376 child_allocation.y = 0;
2378 for (i = 0; i < b; i++)
2381 child_allocation.y += priv->heights[i];
2383 child_allocation.height += priv->heights[i];
2386 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2389 gtk_widget_size_allocate (child, &child_allocation);
2390 gtk_widget_queue_draw (child);
2394 /* Resize the item window */
2395 if (GTK_WIDGET_REALIZED (widget))
2401 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2402 height += priv->heights[i];
2404 width = gtk_menu_get_n_columns (menu) * base_width;
2405 gdk_window_resize (menu->bin_window, width, height);
2408 if (menu->tearoff_active)
2410 if (allocation->height >= widget->requisition.height)
2412 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2414 gtk_widget_hide (menu->tearoff_scrollbar);
2415 gtk_menu_set_tearoff_hints (menu, allocation->width);
2417 gtk_menu_scroll_to (menu, 0);
2422 menu->tearoff_adjustment->upper = widget->requisition.height;
2423 menu->tearoff_adjustment->page_size = allocation->height;
2425 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2426 menu->tearoff_adjustment->upper)
2429 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2432 gtk_menu_scroll_to (menu, value);
2435 gtk_adjustment_changed (menu->tearoff_adjustment);
2437 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2439 gtk_widget_show (menu->tearoff_scrollbar);
2440 gtk_menu_set_tearoff_hints (menu, allocation->width);
2448 get_arrows_visible_area (GtkMenu *menu,
2449 GdkRectangle *border,
2450 GdkRectangle *upper,
2451 GdkRectangle *lower,
2454 GtkWidget *widget = GTK_WIDGET (menu);
2455 guint vertical_padding;
2456 guint horizontal_padding;
2457 gint scroll_arrow_height;
2459 gtk_widget_style_get (widget,
2460 "vertical-padding", &vertical_padding,
2461 "horizontal-padding", &horizontal_padding,
2462 "scroll-arrow-vlength", &scroll_arrow_height,
2465 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2466 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2467 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2469 upper->x = border->x;
2470 upper->y = border->y;
2471 upper->width = border->width - 2 * border->x;
2472 upper->height = scroll_arrow_height;
2474 lower->x = border->x;
2475 lower->y = border->height - border->y - scroll_arrow_height;
2476 lower->width = border->width - 2 * border->x;
2477 lower->height = scroll_arrow_height;
2479 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2483 gtk_menu_paint (GtkWidget *widget,
2484 GdkEventExpose *event)
2487 GtkMenuPrivate *priv;
2488 GdkRectangle border;
2493 g_return_if_fail (GTK_IS_MENU (widget));
2495 menu = GTK_MENU (widget);
2496 priv = gtk_menu_get_private (menu);
2498 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2500 if (event->window == widget->window)
2502 gint arrow_size = 0.7 * arrow_space;
2504 gtk_paint_box (widget->style,
2508 &event->area, widget, "menu",
2511 if (menu->upper_arrow_visible && !menu->tearoff_active)
2513 gtk_paint_box (widget->style,
2515 priv->upper_arrow_state,
2517 &event->area, widget, "menu",
2523 gtk_paint_arrow (widget->style,
2525 priv->upper_arrow_state,
2527 &event->area, widget, "menu_scroll_arrow_up",
2530 upper.x + (upper.width - arrow_size) / 2,
2531 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2532 arrow_size, arrow_size);
2535 if (menu->lower_arrow_visible && !menu->tearoff_active)
2537 gtk_paint_box (widget->style,
2539 priv->lower_arrow_state,
2541 &event->area, widget, "menu",
2547 gtk_paint_arrow (widget->style,
2549 priv->lower_arrow_state,
2551 &event->area, widget, "menu_scroll_arrow_down",
2554 lower.x + (lower.width - arrow_size) / 2,
2555 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2556 arrow_size, arrow_size);
2559 else if (event->window == menu->bin_window)
2561 gint y = -border.y + menu->scroll_offset;
2563 if (!menu->tearoff_active)
2565 GtkBorder arrow_border;
2567 get_arrows_border (menu, &arrow_border);
2568 y -= arrow_border.top;
2571 gtk_paint_box (widget->style,
2575 &event->area, widget, "menu",
2577 border.width, border.height);
2582 gtk_menu_expose (GtkWidget *widget,
2583 GdkEventExpose *event)
2585 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2586 g_return_val_if_fail (event != NULL, FALSE);
2588 if (GTK_WIDGET_DRAWABLE (widget))
2590 gtk_menu_paint (widget, event);
2592 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event) (widget, event);
2599 gtk_menu_show (GtkWidget *widget)
2601 GtkMenu *menu = GTK_MENU (widget);
2603 _gtk_menu_refresh_accel_paths (menu, FALSE);
2605 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2609 gtk_menu_button_scroll (GtkMenu *menu,
2610 GdkEventButton *event)
2612 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2614 gboolean touchscreen_mode;
2616 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
2617 "gtk-touchscreen-mode", &touchscreen_mode,
2620 if (touchscreen_mode)
2621 gtk_menu_handle_scrolling (menu,
2622 event->x_root, event->y_root,
2623 event->type == GDK_BUTTON_PRESS,
2633 pointer_in_menu_window (GtkWidget *widget,
2637 GtkMenu *menu = GTK_MENU (widget);
2639 if (GTK_WIDGET_MAPPED (menu->toplevel))
2641 GtkMenuShell *menu_shell;
2642 gint window_x, window_y;
2644 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
2646 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
2647 y_root >= window_y && y_root < window_y + widget->allocation.height)
2650 menu_shell = GTK_MENU_SHELL (widget);
2652 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
2653 return pointer_in_menu_window (menu_shell->parent_menu_shell,
2661 gtk_menu_button_press (GtkWidget *widget,
2662 GdkEventButton *event)
2664 if (event->type != GDK_BUTTON_PRESS)
2667 /* Don't pass down to menu shell for presses over scroll arrows
2669 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2672 /* Don't pass down to menu shell if a non-menuitem part of the menu
2673 * was clicked. The check for the event_widget being a GtkMenuShell
2674 * works because we have the pointer grabbed on menu_shell->window
2675 * with owner_events=TRUE, so all events that are either outside
2676 * the menu or on its border are delivered relative to
2677 * menu_shell->window.
2679 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2680 pointer_in_menu_window (widget, event->x_root, event->y_root))
2683 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2687 gtk_menu_button_release (GtkWidget *widget,
2688 GdkEventButton *event)
2690 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2692 if (priv->ignore_button_release)
2694 priv->ignore_button_release = FALSE;
2698 if (event->type != GDK_BUTTON_RELEASE)
2701 /* Don't pass down to menu shell for releases over scroll arrows
2703 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2706 /* Don't pass down to menu shell if a non-menuitem part of the menu
2707 * was clicked (see comment in button_press()).
2709 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2710 pointer_in_menu_window (widget, event->x_root, event->y_root))
2712 /* Ugly: make sure menu_shell->button gets reset to 0 when we
2713 * bail out early here so it is in a consistent state for the
2714 * next button_press/button_release in GtkMenuShell.
2717 if (GTK_MENU_SHELL (widget)->active)
2718 GTK_MENU_SHELL (widget)->button = 0;
2723 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
2726 static const gchar *
2727 get_accel_path (GtkWidget *menu_item,
2732 GClosure *accel_closure;
2733 GtkAccelGroup *accel_group;
2735 path = _gtk_widget_get_accel_path (menu_item, locked);
2738 path = GTK_MENU_ITEM (menu_item)->accel_path;
2744 label = GTK_BIN (menu_item)->child;
2746 if (GTK_IS_ACCEL_LABEL (label))
2748 g_object_get (label,
2749 "accel-closure", &accel_closure,
2753 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2755 *locked = accel_group->lock_count > 0;
2765 gtk_menu_key_press (GtkWidget *widget,
2768 GtkMenuShell *menu_shell;
2770 gboolean delete = FALSE;
2771 gboolean can_change_accels;
2772 gchar *accel = NULL;
2773 guint accel_key, accel_mods;
2774 GdkModifierType consumed_modifiers;
2775 GdkDisplay *display;
2777 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2778 g_return_val_if_fail (event != NULL, FALSE);
2780 menu_shell = GTK_MENU_SHELL (widget);
2781 menu = GTK_MENU (widget);
2783 gtk_menu_stop_navigating_submenu (menu);
2785 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
2788 display = gtk_widget_get_display (widget);
2790 g_object_get (gtk_widget_get_settings (widget),
2791 "gtk-menu-bar-accel", &accel,
2792 "gtk-can-change-accels", &can_change_accels,
2795 if (accel && *accel)
2798 GdkModifierType mods = 0;
2800 gtk_accelerator_parse (accel, &keyval, &mods);
2803 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2805 /* FIXME this is wrong, needs to be in the global accel resolution
2806 * thing, to properly consider i18n etc., but that probably requires
2807 * AccelGroup changes etc.
2809 if (event->keyval == keyval && (mods & event->state) == mods)
2811 gtk_menu_shell_cancel (menu_shell);
2819 switch (event->keyval)
2830 /* Figure out what modifiers went into determining the key symbol */
2831 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2832 event->hardware_keycode, event->state, event->group,
2833 NULL, NULL, NULL, &consumed_modifiers);
2835 accel_key = gdk_keyval_to_lower (event->keyval);
2836 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2838 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2839 * We re-upper case when we match against the keyval, but display and save in caseless form.
2841 if (accel_key != event->keyval)
2842 accel_mods |= GDK_SHIFT_MASK;
2844 /* Modify the accelerators */
2845 if (can_change_accels &&
2846 menu_shell->active_menu_item &&
2847 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2848 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2849 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2851 GtkWidget *menu_item = menu_shell->active_menu_item;
2852 gboolean locked, replace_accels = TRUE;
2855 path = get_accel_path (menu_item, &locked);
2856 if (!path || locked)
2858 /* can't change accelerators on menu_items without paths
2859 * (basically, those items are accelerator-locked).
2861 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2862 gtk_widget_error_bell (widget);
2868 /* For the keys that act to delete the current setting, we delete
2869 * the current setting if there is one, otherwise, we set the
2870 * key as the accelerator.
2876 if (gtk_accel_map_lookup_entry (path, &key) &&
2877 (key.accel_key || key.accel_mods))
2883 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2887 /* we failed, probably because this key is in use and
2890 /* g_print("failed to change\n"); */
2891 gtk_widget_error_bell (widget);
2900 check_threshold (GtkWidget *widget,
2901 int start_x, int start_y,
2907 ABS (start_x - x) > THRESHOLD ||
2908 ABS (start_y - y) > THRESHOLD;
2912 definitely_within_item (GtkWidget *widget,
2916 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
2919 gdk_drawable_get_size (window, &w, &h);
2922 check_threshold (widget, 0, 0, x, y) &&
2923 check_threshold (widget, w - 1, 0, x, y) &&
2924 check_threshold (widget, w - 1, h - 1, x, y) &&
2925 check_threshold (widget, 0, h - 1, x, y);
2929 gtk_menu_motion_notify (GtkWidget *widget,
2930 GdkEventMotion *event)
2932 GtkWidget *menu_item;
2934 GtkMenuShell *menu_shell;
2936 gboolean need_enter;
2938 if (GTK_IS_MENU (widget))
2940 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2942 if (priv->ignore_button_release)
2943 priv->ignore_button_release = FALSE;
2945 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
2949 /* We received the event for one of two reasons:
2951 * a) We are the active menu, and did gtk_grab_add()
2952 * b) The widget is a child of ours, and the event was propagated
2954 * Since for computation of navigation regions, we want the menu which
2955 * is the parent of the menu item, for a), we need to find that menu,
2956 * which may be different from 'widget'.
2958 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2959 if (!GTK_IS_MENU_ITEM (menu_item) ||
2960 !GTK_IS_MENU (menu_item->parent))
2963 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2964 menu = GTK_MENU (menu_shell);
2966 if (definitely_within_item (menu_item, event->x, event->y))
2967 menu_shell->activate_time = 0;
2969 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2971 /* Check to see if we are within an active submenu's navigation region
2973 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2976 /* Make sure we pop down if we enter a non-selectable menu item, so we
2977 * don't show a submenu when the cursor is outside the stay-up triangle.
2979 if (!_gtk_menu_item_is_selectable (menu_item))
2981 gtk_menu_shell_deselect (menu_shell);
2987 /* The menu is now sensitive to enter events on its items, but
2988 * was previously sensitive. So we fake an enter event.
2992 menu_shell->ignore_enter = FALSE;
2994 gdk_drawable_get_size (event->window, &width, &height);
2995 if (event->x >= 0 && event->x < width &&
2996 event->y >= 0 && event->y < height)
2998 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3001 send_event->crossing.window = g_object_ref (event->window);
3002 send_event->crossing.time = event->time;
3003 send_event->crossing.send_event = TRUE;
3004 send_event->crossing.x_root = event->x_root;
3005 send_event->crossing.y_root = event->y_root;
3006 send_event->crossing.x = event->x;
3007 send_event->crossing.y = event->y;
3008 send_event->crossing.state = event->state;
3010 /* We send the event to 'widget', the currently active menu,
3011 * instead of 'menu', the menu that the pointer is in. This
3012 * will ensure that the event will be ignored unless the
3013 * menuitem is a child of the active menu or some parent
3014 * menu of the active menu.
3016 result = gtk_widget_event (widget, send_event);
3017 gdk_event_free (send_event);
3027 get_double_arrows (GtkMenu *menu)
3029 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3030 gboolean double_arrows;
3032 gtk_widget_style_get (GTK_WIDGET (menu),
3033 "double-arrows", &double_arrows,
3036 return double_arrows || (priv->initially_pushed_in &&
3037 menu->scroll_offset != 0);
3041 gtk_menu_scroll_by (GtkMenu *menu,
3046 gint view_width, view_height;
3047 gboolean double_arrows;
3048 GtkBorder arrow_border;
3050 widget = GTK_WIDGET (menu);
3051 offset = menu->scroll_offset + step;
3053 get_arrows_border (menu, &arrow_border);
3055 double_arrows = get_double_arrows (menu);
3057 /* If we scroll upward and the non-visible top part
3058 * is smaller than the scroll arrow it would be
3059 * pretty stupid to show the arrow and taking more
3060 * screen space than just scrolling to the top.
3063 if ((step < 0) && (offset < arrow_border.top))
3066 /* Don't scroll over the top if we weren't before: */
3067 if ((menu->scroll_offset >= 0) && (offset < 0))
3070 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3072 if (menu->scroll_offset == 0 &&
3073 view_height >= widget->requisition.height)
3076 /* Don't scroll past the bottom if we weren't before: */
3077 if (menu->scroll_offset > 0)
3078 view_height -= arrow_border.top;
3080 /* When both arrows are always shown, reduce
3081 * view height even more.
3084 view_height -= arrow_border.bottom;
3086 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3087 (offset + view_height > widget->requisition.height))
3088 offset = widget->requisition.height - view_height;
3090 if (offset != menu->scroll_offset)
3091 gtk_menu_scroll_to (menu, offset);
3095 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3096 gboolean touchscreen_mode)
3098 gboolean upper_visible;
3099 gboolean lower_visible;
3101 upper_visible = menu->upper_arrow_visible;
3102 lower_visible = menu->lower_arrow_visible;
3104 gtk_menu_scroll_by (menu, menu->scroll_step);
3106 if (touchscreen_mode &&
3107 (upper_visible != menu->upper_arrow_visible ||
3108 lower_visible != menu->lower_arrow_visible))
3110 /* We are about to hide a scroll arrow while the mouse is pressed,
3111 * this would cause the uncovered menu item to be activated on button
3112 * release. Therefore we need to ignore button release here
3114 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3115 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3120 gtk_menu_scroll_timeout (gpointer data)
3123 gboolean touchscreen_mode;
3125 menu = GTK_MENU (data);
3127 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3128 "gtk-touchscreen-mode", &touchscreen_mode,
3131 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3137 gtk_menu_scroll_timeout_initial (gpointer data)
3141 gboolean touchscreen_mode;
3143 menu = GTK_MENU (data);
3145 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3146 "gtk-timeout-repeat", &timeout,
3147 "gtk-touchscreen-mode", &touchscreen_mode,
3150 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3152 gtk_menu_remove_scroll_timeout (menu);
3154 menu->timeout_id = gdk_threads_add_timeout (timeout,
3155 gtk_menu_scroll_timeout,
3162 gtk_menu_start_scrolling (GtkMenu *menu)
3165 gboolean touchscreen_mode;
3167 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3168 "gtk-timeout-repeat", &timeout,
3169 "gtk-touchscreen-mode", &touchscreen_mode,
3172 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3174 menu->timeout_id = gdk_threads_add_timeout (timeout,
3175 gtk_menu_scroll_timeout_initial,
3180 gtk_menu_scroll (GtkWidget *widget,
3181 GdkEventScroll *event)
3183 GtkMenu *menu = GTK_MENU (widget);
3185 switch (event->direction)
3187 case GDK_SCROLL_RIGHT:
3188 case GDK_SCROLL_DOWN:
3189 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3191 case GDK_SCROLL_LEFT:
3193 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3201 get_arrows_sensitive_area (GtkMenu *menu,
3202 GdkRectangle *upper,
3203 GdkRectangle *lower)
3207 guint vertical_padding;
3209 gint scroll_arrow_height;
3211 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3213 gtk_widget_style_get (GTK_WIDGET (menu),
3214 "vertical-padding", &vertical_padding,
3215 "scroll-arrow-vlength", &scroll_arrow_height,
3218 border = GTK_CONTAINER (menu)->border_width +
3219 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3221 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3227 upper->width = width;
3228 upper->height = scroll_arrow_height + border;
3234 lower->y = win_y + height - border - scroll_arrow_height;
3235 lower->width = width;
3236 lower->height = scroll_arrow_height + border;
3242 gtk_menu_handle_scrolling (GtkMenu *menu,
3248 GtkMenuShell *menu_shell;
3249 GtkMenuPrivate *priv;
3252 gboolean scroll_fast = FALSE;
3254 gboolean touchscreen_mode;
3256 priv = gtk_menu_get_private (menu);
3258 menu_shell = GTK_MENU_SHELL (menu);
3260 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3261 "gtk-touchscreen-mode", &touchscreen_mode,
3264 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3268 /* upper arrow handling */
3270 get_arrows_sensitive_area (menu, &rect, NULL);
3273 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3274 (x >= rect.x) && (x < rect.x + rect.width) &&
3275 (y >= rect.y) && (y < rect.y + rect.height))
3280 if (touchscreen_mode)
3281 menu->upper_arrow_prelight = in_arrow;
3283 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3285 gboolean arrow_pressed = FALSE;
3287 if (menu->upper_arrow_visible && !menu->tearoff_active)
3289 if (touchscreen_mode)
3291 if (enter && menu->upper_arrow_prelight)
3293 if (menu->timeout_id == 0)
3295 /* Deselect the active item so that
3296 * any submenus are popped down
3298 gtk_menu_shell_deselect (menu_shell);
3300 gtk_menu_remove_scroll_timeout (menu);
3301 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3305 /* Only do stuff on click. */
3306 gtk_menu_start_scrolling (menu);
3307 arrow_pressed = TRUE;
3312 arrow_pressed = TRUE;
3317 gtk_menu_stop_scrolling (menu);
3320 else /* !touchscreen_mode */
3322 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3324 if (enter && in_arrow &&
3325 (!menu->upper_arrow_prelight ||
3326 menu->scroll_fast != scroll_fast))
3328 menu->upper_arrow_prelight = TRUE;
3329 menu->scroll_fast = scroll_fast;
3331 /* Deselect the active item so that
3332 * any submenus are popped down
3334 gtk_menu_shell_deselect (menu_shell);
3336 gtk_menu_remove_scroll_timeout (menu);
3337 menu->scroll_step = scroll_fast ?
3338 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3341 gdk_threads_add_timeout (scroll_fast ?
3342 MENU_SCROLL_TIMEOUT2 :
3343 MENU_SCROLL_TIMEOUT1,
3344 gtk_menu_scroll_timeout, menu);
3346 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3348 gtk_menu_stop_scrolling (menu);
3353 /* gtk_menu_start_scrolling() might have hit the top of the
3354 * menu, so check if the button isn't insensitive before
3355 * changing it to something else.
3357 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3359 GtkStateType arrow_state = GTK_STATE_NORMAL;
3362 arrow_state = GTK_STATE_ACTIVE;
3363 else if (menu->upper_arrow_prelight)
3364 arrow_state = GTK_STATE_PRELIGHT;
3366 if (arrow_state != priv->upper_arrow_state)
3368 priv->upper_arrow_state = arrow_state;
3370 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3376 /* lower arrow handling */
3378 get_arrows_sensitive_area (menu, NULL, &rect);
3381 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3382 (x >= rect.x) && (x < rect.x + rect.width) &&
3383 (y >= rect.y) && (y < rect.y + rect.height))
3388 if (touchscreen_mode)
3389 menu->lower_arrow_prelight = in_arrow;
3391 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3393 gboolean arrow_pressed = FALSE;
3395 if (menu->lower_arrow_visible && !menu->tearoff_active)
3397 if (touchscreen_mode)
3399 if (enter && menu->lower_arrow_prelight)
3401 if (menu->timeout_id == 0)
3403 /* Deselect the active item so that
3404 * any submenus are popped down
3406 gtk_menu_shell_deselect (menu_shell);
3408 gtk_menu_remove_scroll_timeout (menu);
3409 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3413 /* Only do stuff on click. */
3414 gtk_menu_start_scrolling (menu);
3415 arrow_pressed = TRUE;
3420 arrow_pressed = TRUE;
3425 gtk_menu_stop_scrolling (menu);
3428 else /* !touchscreen_mode */
3430 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3432 if (enter && in_arrow &&
3433 (!menu->lower_arrow_prelight ||
3434 menu->scroll_fast != scroll_fast))
3436 menu->lower_arrow_prelight = TRUE;
3437 menu->scroll_fast = scroll_fast;
3439 /* Deselect the active item so that
3440 * any submenus are popped down
3442 gtk_menu_shell_deselect (menu_shell);
3444 gtk_menu_remove_scroll_timeout (menu);
3445 menu->scroll_step = scroll_fast ?
3446 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3449 gdk_threads_add_timeout (scroll_fast ?
3450 MENU_SCROLL_TIMEOUT2 :
3451 MENU_SCROLL_TIMEOUT1,
3452 gtk_menu_scroll_timeout, menu);
3454 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3456 gtk_menu_stop_scrolling (menu);
3461 /* gtk_menu_start_scrolling() might have hit the bottom of the
3462 * menu, so check if the button isn't insensitive before
3463 * changing it to something else.
3465 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3467 GtkStateType arrow_state = GTK_STATE_NORMAL;
3470 arrow_state = GTK_STATE_ACTIVE;
3471 else if (menu->lower_arrow_prelight)
3472 arrow_state = GTK_STATE_PRELIGHT;
3474 if (arrow_state != priv->lower_arrow_state)
3476 priv->lower_arrow_state = arrow_state;
3478 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3486 gtk_menu_enter_notify (GtkWidget *widget,
3487 GdkEventCrossing *event)
3489 GtkWidget *menu_item;
3490 gboolean touchscreen_mode;
3492 g_object_get (gtk_widget_get_settings (widget),
3493 "gtk-touchscreen-mode", &touchscreen_mode,
3496 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3497 if (GTK_IS_MENU (widget))
3499 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3501 if (!menu_shell->ignore_enter)
3502 gtk_menu_handle_scrolling (GTK_MENU (widget),
3503 event->x_root, event->y_root, TRUE, TRUE);
3506 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3508 GtkWidget *menu = menu_item->parent;
3510 if (GTK_IS_MENU (menu))
3512 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3513 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3515 if (priv->seen_item_enter)
3517 /* This is the second enter we see for an item
3518 * on this menu. This means a release should always
3521 menu_shell->activate_time = 0;
3523 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3524 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3526 if (definitely_within_item (menu_item, event->x, event->y))
3528 /* This is an actual user-enter (ie. not a pop-under)
3529 * In this case, the user must either have entered
3530 * sufficiently far enough into the item, or he must move
3531 * far enough away from the enter point. (see
3532 * gtk_menu_motion_notify())
3534 menu_shell->activate_time = 0;
3538 priv->seen_item_enter = TRUE;
3542 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3543 * will not correspond to the event widget's parent. Check to see
3544 * if we are in the parent's navigation region.
3546 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3547 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3548 event->x_root, event->y_root))
3551 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3555 gtk_menu_leave_notify (GtkWidget *widget,
3556 GdkEventCrossing *event)
3558 GtkMenuShell *menu_shell;
3560 GtkMenuItem *menu_item;
3561 GtkWidget *event_widget;
3563 menu = GTK_MENU (widget);
3564 menu_shell = GTK_MENU_SHELL (widget);
3566 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3569 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3571 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3573 if (!GTK_IS_MENU_ITEM (event_widget))
3576 menu_item = GTK_MENU_ITEM (event_widget);
3578 /* Here we check to see if we're leaving an active menu item with a submenu,
3579 * in which case we enter submenu navigation mode.
3581 if (menu_shell->active_menu_item != NULL
3582 && menu_item->submenu != NULL
3583 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3585 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3587 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3590 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3592 /* We are leaving an active menu item with nonactive submenu.
3593 * Deselect it so we don't surprise the user with by popping
3594 * up a submenu _after_ he left the item.
3596 gtk_menu_shell_deselect (menu_shell);
3601 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3605 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3607 if (menu->navigation_region)
3609 gdk_region_destroy (menu->navigation_region);
3610 menu->navigation_region = NULL;
3612 if (menu->navigation_timeout)
3614 g_source_remove (menu->navigation_timeout);
3615 menu->navigation_timeout = 0;
3619 /* When the timeout is elapsed, the navigation region is destroyed
3620 * and the menuitem under the pointer (if any) is selected.
3623 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3625 GtkMenu *menu = user_data;
3626 GdkWindow *child_window;
3628 gtk_menu_stop_navigating_submenu (menu);
3630 if (GTK_WIDGET_REALIZED (menu))
3632 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3636 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3638 send_event->crossing.window = g_object_ref (child_window);
3639 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3640 send_event->crossing.send_event = TRUE;
3642 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3644 gdk_event_free (send_event);
3652 gtk_menu_navigating_submenu (GtkMenu *menu,
3656 if (menu->navigation_region)
3658 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3662 gtk_menu_stop_navigating_submenu (menu);
3669 #undef DRAW_STAY_UP_TRIANGLE
3671 #ifdef DRAW_STAY_UP_TRIANGLE
3674 draw_stay_up_triangle (GdkWindow *window,
3677 /* Draw ugly color all over the stay-up triangle */
3678 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3679 GdkGCValues gc_values;
3681 GdkRectangle clipbox;
3683 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3684 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3685 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3686 gdk_gc_set_clip_region (ugly_gc, region);
3688 gdk_region_get_clipbox (region, &clipbox);
3690 gdk_draw_rectangle (window,
3693 clipbox.x, clipbox.y,
3694 clipbox.width, clipbox.height);
3696 g_object_unref (ugly_gc);
3701 flip_region (GdkRegion *region,
3706 GdkRectangle *rectangles;
3707 GdkRectangle clipbox;
3708 GdkRegion *new_region;
3711 new_region = gdk_region_new ();
3713 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3714 gdk_region_get_clipbox (region, &clipbox);
3716 for (i = 0; i < n_rectangles; ++i)
3718 GdkRectangle rect = rectangles[i];
3721 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3724 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3726 gdk_region_union_with_rect (new_region, &rect);
3729 g_free (rectangles);
3735 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3736 GtkMenuItem *menu_item,
3737 GdkEventCrossing *event)
3739 gint submenu_left = 0;
3740 gint submenu_right = 0;
3741 gint submenu_top = 0;
3742 gint submenu_bottom = 0;
3746 GtkWidget *event_widget;
3748 g_return_if_fail (menu_item->submenu != NULL);
3749 g_return_if_fail (event != NULL);
3751 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3753 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3754 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3756 submenu_right = submenu_left + width;
3757 submenu_bottom = submenu_top + height;
3759 gdk_drawable_get_size (event_widget->window, &width, &height);
3761 if (event->x >= 0 && event->x < width)
3764 gboolean flip_y = FALSE;
3765 gboolean flip_x = FALSE;
3767 gtk_menu_stop_navigating_submenu (menu);
3769 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3772 point[0].x = event->x_root;
3773 point[1].x = submenu_left;
3778 point[0].x = event->x_root + 1;
3779 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3787 point[0].y = event->y_root + 1;
3788 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3790 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3798 point[0].y = event->y_root;
3799 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3801 if (point[0].y >= submenu_bottom)
3805 point[2].x = point[1].x;
3806 point[2].y = point[0].y;
3808 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3810 if (flip_x || flip_y)
3812 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3813 gdk_region_destroy (menu->navigation_region);
3814 menu->navigation_region = new_region;
3817 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3818 "gtk-menu-popdown-delay", &popdown_delay,
3821 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
3822 gtk_menu_stop_navigating_submenu_cb,
3825 #ifdef DRAW_STAY_UP_TRIANGLE
3826 draw_stay_up_triangle (gdk_get_default_root_window(),
3827 menu->navigation_region);
3833 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3837 g_return_if_fail (GTK_IS_MENU (menu_shell));
3839 parent = menu_shell->parent_menu_shell;
3841 menu_shell->activate_time = 0;
3842 gtk_menu_popdown (GTK_MENU (menu_shell));
3845 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3849 gtk_menu_position (GtkMenu *menu)
3852 GtkRequisition requisition;
3853 GtkMenuPrivate *private;
3858 GdkScreen *pointer_screen;
3859 GdkRectangle monitor;
3861 g_return_if_fail (GTK_IS_MENU (menu));
3863 widget = GTK_WIDGET (menu);
3865 screen = gtk_widget_get_screen (widget);
3866 gdk_display_get_pointer (gdk_screen_get_display (screen),
3867 &pointer_screen, &x, &y, NULL);
3869 /* We need the requisition to figure out the right place to
3870 * popup the menu. In fact, we always need to ask here, since
3871 * if a size_request was queued while we weren't popped up,
3872 * the requisition won't have been recomputed yet.
3874 gtk_widget_size_request (widget, &requisition);
3876 if (pointer_screen != screen)
3878 /* Pointer is on a different screen; roughly center the
3879 * menu on the screen. If someone was using multiscreen
3880 * + Xinerama together they'd probably want something
3881 * fancier; but that is likely to be vanishingly rare.
3883 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3884 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3887 private = gtk_menu_get_private (menu);
3888 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3890 private->initially_pushed_in = FALSE;
3892 /* Set the type hint here to allow custom position functions to set a different hint */
3893 if (!GTK_WIDGET_VISIBLE (menu->toplevel))
3894 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
3896 if (menu->position_func)
3898 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
3899 menu->position_func_data);
3900 if (private->monitor_num < 0)
3901 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3903 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3907 gint space_left, space_right, space_above, space_below;
3910 gint xthickness = widget->style->xthickness;
3911 gint ythickness = widget->style->ythickness;
3912 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3914 /* The placement of popup menus horizontally works like this (with
3915 * RTL in parentheses)
3917 * - If there is enough room to the right (left) of the mouse cursor,
3918 * position the menu there.
3920 * - Otherwise, if if there is enough room to the left (right) of the
3921 * mouse cursor, position the menu there.
3923 * - Otherwise if the menu is smaller than the monitor, position it
3924 * on the side of the mouse cursor that has the most space available
3926 * - Otherwise (if there is simply not enough room for the menu on the
3927 * monitor), position it as far left (right) as possible.
3929 * Positioning in the vertical direction is similar: first try below
3930 * mouse cursor, then above.
3932 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3934 space_left = x - monitor.x;
3935 space_right = monitor.x + monitor.width - x - 1;
3936 space_above = y - monitor.y;
3937 space_below = monitor.y + monitor.height - y - 1;
3939 /* position horizontally */
3941 /* the amount of space we need to position the menu. Note the
3942 * menu is offset "xthickness" pixels
3944 needed_width = requisition.width - xthickness;
3946 if (needed_width <= space_left ||
3947 needed_width <= space_right)
3949 if ((rtl && needed_width <= space_left) ||
3950 (!rtl && needed_width > space_right))
3953 x = x + xthickness - requisition.width + 1;
3957 /* position right */
3961 /* x is clamped on-screen further down */
3963 else if (requisition.width <= monitor.width)
3965 /* the menu is too big to fit on either side of the mouse
3966 * cursor, but smaller than the monitor. Position it on
3967 * the side that has the most space
3969 if (space_left > space_right)
3977 x = monitor.x + monitor.width - requisition.width;
3980 else /* menu is simply too big for the monitor */
3985 x = monitor.x + monitor.width - requisition.width;
3994 /* Position vertically. The algorithm is the same as above, but
3995 * simpler because we don't have to take RTL into account.
3997 needed_height = requisition.height - ythickness;
3999 if (needed_height <= space_above ||
4000 needed_height <= space_below)
4002 if (needed_height <= space_below)
4005 y = y + ythickness - requisition.height + 1;
4007 y = CLAMP (y, monitor.y,
4008 monitor.y + monitor.height - requisition.height);
4010 else if (needed_height > space_below && needed_height > space_above)
4012 if (space_below >= space_above)
4013 y = monitor.y + monitor.height - requisition.height;
4025 if (private->initially_pushed_in)
4027 menu_height = GTK_WIDGET (menu)->requisition.height;
4029 if (y + menu_height > monitor.y + monitor.height)
4031 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4032 y = (monitor.y + monitor.height) - menu_height;
4037 scroll_offset += monitor.y - y;
4042 /* FIXME: should this be done in the various position_funcs ? */
4043 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4045 if (GTK_MENU_SHELL (menu)->active)
4047 private->have_position = TRUE;
4052 if (y + requisition.height > monitor.y + monitor.height)
4053 requisition.height = (monitor.y + monitor.height) - y;
4057 scroll_offset += monitor.y - y;
4058 requisition.height -= monitor.y - y;
4062 if (scroll_offset > 0)
4064 GtkBorder arrow_border;
4066 get_arrows_border (menu, &arrow_border);
4067 scroll_offset += arrow_border.top;
4070 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4073 if (!GTK_MENU_SHELL (menu)->active)
4075 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4076 requisition.width, requisition.height);
4079 menu->scroll_offset = scroll_offset;
4083 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4085 if (menu->timeout_id)
4087 g_source_remove (menu->timeout_id);
4088 menu->timeout_id = 0;
4093 gtk_menu_stop_scrolling (GtkMenu *menu)
4095 gboolean touchscreen_mode;
4097 gtk_menu_remove_scroll_timeout (menu);
4099 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4100 "gtk-touchscreen-mode", &touchscreen_mode,
4103 if (!touchscreen_mode)
4105 menu->upper_arrow_prelight = FALSE;
4106 menu->lower_arrow_prelight = FALSE;
4111 gtk_menu_scroll_to (GtkMenu *menu,
4116 gint view_width, view_height;
4119 guint vertical_padding;
4120 guint horizontal_padding;
4121 gboolean double_arrows;
4122 GtkBorder arrow_border;
4124 widget = GTK_WIDGET (menu);
4126 if (menu->tearoff_active &&
4127 menu->tearoff_adjustment &&
4128 (menu->tearoff_adjustment->value != offset))
4130 menu->tearoff_adjustment->value =
4132 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4133 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4136 /* Move/resize the viewport according to arrows: */
4137 view_width = widget->allocation.width;
4138 view_height = widget->allocation.height;
4140 gtk_widget_style_get (GTK_WIDGET (menu),
4141 "vertical-padding", &vertical_padding,
4142 "horizontal-padding", &horizontal_padding,
4145 double_arrows = get_double_arrows (menu);
4147 border_width = GTK_CONTAINER (menu)->border_width;
4148 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4149 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4150 menu_height = widget->requisition.height -
4151 (border_width + widget->style->ythickness + vertical_padding) * 2;
4153 x = border_width + widget->style->xthickness + horizontal_padding;
4154 y = border_width + widget->style->ythickness + vertical_padding;
4156 if (double_arrows && !menu->tearoff_active)
4158 if (view_height < menu_height ||
4159 (offset > 0 && menu->scroll_offset > 0) ||
4160 (offset < 0 && menu->scroll_offset < 0))
4162 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4163 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4164 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4166 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4167 gtk_widget_queue_draw (GTK_WIDGET (menu));
4169 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4171 get_arrows_border (menu, &arrow_border);
4172 y += arrow_border.top;
4173 view_height -= arrow_border.top;
4174 view_height -= arrow_border.bottom;
4177 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4178 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4179 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4180 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4182 if (offset >= menu_height - view_height)
4183 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4184 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4185 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4186 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4188 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4189 (priv->lower_arrow_state != lower_arrow_previous_state))
4190 gtk_widget_queue_draw (GTK_WIDGET (menu));
4192 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4193 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4195 /* At the upper border, possibly remove timeout */
4196 if (menu->scroll_step < 0)
4198 gtk_menu_stop_scrolling (menu);
4199 gtk_widget_queue_draw (GTK_WIDGET (menu));
4203 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4204 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4206 /* At the lower border, possibly remove timeout */
4207 if (menu->scroll_step > 0)
4209 gtk_menu_stop_scrolling (menu);
4210 gtk_widget_queue_draw (GTK_WIDGET (menu));
4214 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4218 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4219 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4221 gtk_menu_stop_scrolling (menu);
4222 gtk_widget_queue_draw (GTK_WIDGET (menu));
4225 else if (!menu->tearoff_active)
4227 gboolean last_visible;
4229 last_visible = menu->upper_arrow_visible;
4230 menu->upper_arrow_visible = offset > 0;
4232 /* upper_arrow_visible may have changed, so requery the border */
4233 get_arrows_border (menu, &arrow_border);
4234 view_height -= arrow_border.top;
4236 if ((last_visible != menu->upper_arrow_visible) &&
4237 !menu->upper_arrow_visible)
4239 menu->upper_arrow_prelight = FALSE;
4241 /* If we hid the upper arrow, possibly remove timeout */
4242 if (menu->scroll_step < 0)
4244 gtk_menu_stop_scrolling (menu);
4245 gtk_widget_queue_draw (GTK_WIDGET (menu));
4249 last_visible = menu->lower_arrow_visible;
4250 menu->lower_arrow_visible = offset < menu_height - view_height;
4252 /* lower_arrow_visible may have changed, so requery the border */
4253 get_arrows_border (menu, &arrow_border);
4254 view_height -= arrow_border.bottom;
4256 if ((last_visible != menu->lower_arrow_visible) &&
4257 !menu->lower_arrow_visible)
4259 menu->lower_arrow_prelight = FALSE;
4261 /* If we hid the lower arrow, possibly remove timeout */
4262 if (menu->scroll_step > 0)
4264 gtk_menu_stop_scrolling (menu);
4265 gtk_widget_queue_draw (GTK_WIDGET (menu));
4269 y += arrow_border.top;
4272 /* Scroll the menu: */
4273 if (GTK_WIDGET_REALIZED (menu))
4274 gdk_window_move (menu->bin_window, 0, -offset);
4276 if (GTK_WIDGET_REALIZED (menu))
4277 gdk_window_move_resize (menu->view_window,
4283 menu->scroll_offset = offset;
4287 compute_child_offset (GtkMenu *menu,
4288 GtkWidget *menu_item,
4291 gboolean *is_last_child)
4293 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4294 gint item_top_attach;
4295 gint item_bottom_attach;
4296 gint child_offset = 0;
4299 get_effective_child_attach (menu_item, NULL, NULL,
4300 &item_top_attach, &item_bottom_attach);
4302 /* there is a possibility that we get called before _size_request, so
4303 * check the height table for safety.
4305 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4308 /* when we have a row with only invisible children, it's height will
4309 * be zero, so there's no need to check WIDGET_VISIBLE here
4311 for (i = 0; i < item_top_attach; i++)
4312 child_offset += priv->heights[i];
4315 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4317 *offset = child_offset;
4319 *height = priv->heights[item_top_attach];
4325 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4326 GtkWidget *menu_item)
4329 gint child_offset, child_height;
4333 gboolean last_child = 0;
4335 menu = GTK_MENU (menu_shell);
4337 /* We need to check if the selected item fully visible.
4338 * If not we need to scroll the menu so that it becomes fully
4342 if (compute_child_offset (menu, menu_item,
4343 &child_offset, &child_height, &last_child))
4345 guint vertical_padding;
4346 gboolean double_arrows;
4348 y = menu->scroll_offset;
4349 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4351 gtk_widget_style_get (GTK_WIDGET (menu),
4352 "vertical-padding", &vertical_padding,
4355 double_arrows = get_double_arrows (menu);
4357 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4359 if (child_offset < y)
4361 /* Ignore the enter event we might get if the pointer is on the menu
4363 menu_shell->ignore_enter = TRUE;
4364 gtk_menu_scroll_to (menu, child_offset);
4368 GtkBorder arrow_border;
4372 get_arrows_border (menu, &arrow_border);
4373 if (!menu->tearoff_active)
4374 arrow_height = arrow_border.top + arrow_border.bottom;
4376 if (child_offset + child_height > y + height - arrow_height)
4379 if ((!last_child && !menu->tearoff_active) || double_arrows)
4380 arrow_height += arrow_border.bottom;
4382 y = child_offset + child_height - height + arrow_height;
4383 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4385 /* Need upper arrow */
4386 arrow_height += arrow_border.top;
4387 y = child_offset + child_height - height + arrow_height;
4389 /* Ignore the enter event we might get if the pointer is on the menu
4391 menu_shell->ignore_enter = TRUE;
4392 gtk_menu_scroll_to (menu, y);
4400 gtk_menu_select_item (GtkMenuShell *menu_shell,
4401 GtkWidget *menu_item)
4403 GtkMenu *menu = GTK_MENU (menu_shell);
4405 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4406 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4408 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4412 /* Reparent the menu, taking care of the refcounting
4414 * If unrealize is true we force a unrealize while reparenting the parent.
4415 * This can help eliminate flicker in some cases.
4417 * What happens is that when the menu is unrealized and then re-realized,
4418 * the allocations are as follows:
4420 * parent - 1x1 at (0,0)
4421 * child1 - 100x20 at (0,0)
4422 * child2 - 100x20 at (0,20)
4423 * child3 - 100x20 at (0,40)
4425 * That is, the parent is small but the children are full sized. Then,
4426 * when the queued_resize gets processed, the parent gets resized to
4429 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4430 * contains the following logic:
4432 * - if a move or resize operation on a window would change the clip
4433 * region on the children, then before the window is resized
4434 * the background for children is temporarily set to None, the
4435 * move/resize done, and the background for the children restored.
4437 * So, at the point where the parent is resized to final size, the
4438 * background for the children is temporarily None, and thus they
4439 * are not cleared to the background color and the previous background
4440 * (the image of the menu) is left in place.
4443 gtk_menu_reparent (GtkMenu *menu,
4444 GtkWidget *new_parent,
4447 GtkObject *object = GTK_OBJECT (menu);
4448 GtkWidget *widget = GTK_WIDGET (menu);
4449 gboolean was_floating = g_object_is_floating (object);
4451 g_object_ref_sink (object);
4455 g_object_ref (object);
4456 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4457 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4458 g_object_unref (object);
4461 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4464 g_object_force_floating (G_OBJECT (object));
4466 g_object_unref (object);
4470 gtk_menu_show_all (GtkWidget *widget)
4472 /* Show children, but not self. */
4473 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4478 gtk_menu_hide_all (GtkWidget *widget)
4480 /* Hide children, but not self. */
4481 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4485 * gtk_menu_set_screen:
4486 * @menu: a #GtkMenu.
4487 * @screen: a #GdkScreen, or %NULL if the screen should be
4488 * determined by the widget the menu is attached to.
4490 * Sets the #GdkScreen on which the menu will be displayed.
4495 gtk_menu_set_screen (GtkMenu *menu,
4498 g_return_if_fail (GTK_IS_MENU (menu));
4499 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4501 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4505 menu_change_screen (menu, screen);
4509 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4511 attach_widget_screen_changed (attach_widget, NULL, menu);
4517 * @menu: a #GtkMenu.
4518 * @child: a #GtkMenuItem.
4519 * @left_attach: The column number to attach the left side of the item to.
4520 * @right_attach: The column number to attach the right side of the item to.
4521 * @top_attach: The row number to attach the top of the item to.
4522 * @bottom_attach: The row number to attach the bottom of the item to.
4524 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4525 * an item will occupy is specified by @left_attach, @right_attach,
4526 * @top_attach and @bottom_attach. These each represent the leftmost,
4527 * rightmost, uppermost and lower column and row numbers of the table.
4528 * (Columns and rows are indexed from zero).
4530 * Note that this function is not related to gtk_menu_detach().
4535 gtk_menu_attach (GtkMenu *menu,
4540 guint bottom_attach)
4542 GtkMenuShell *menu_shell;
4544 g_return_if_fail (GTK_IS_MENU (menu));
4545 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4546 g_return_if_fail (child->parent == NULL ||
4547 child->parent == GTK_WIDGET (menu));
4548 g_return_if_fail (left_attach < right_attach);
4549 g_return_if_fail (top_attach < bottom_attach);
4551 menu_shell = GTK_MENU_SHELL (menu);
4555 AttachInfo *ai = get_attach_info (child);
4557 ai->left_attach = left_attach;
4558 ai->right_attach = right_attach;
4559 ai->top_attach = top_attach;
4560 ai->bottom_attach = bottom_attach;
4562 menu_shell->children = g_list_append (menu_shell->children, child);
4564 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4566 menu_queue_resize (menu);
4570 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4571 "left-attach", left_attach,
4572 "right-attach", right_attach,
4573 "top-attach", top_attach,
4574 "bottom-attach", bottom_attach,
4580 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4584 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4585 "gtk-menu-popup-delay", &popup_delay,
4592 find_child_containing (GtkMenuShell *menu_shell,
4600 /* find a child which includes the area given by
4601 * left, right, top, bottom.
4604 for (list = menu_shell->children; list; list = list->next)
4608 if (!_gtk_menu_item_is_selectable (list->data))
4611 get_effective_child_attach (list->data, &l, &r, &t, &b);
4613 if (l <= left && right <= r
4614 && t <= top && bottom <= b)
4615 return GTK_WIDGET (list->data);
4622 gtk_menu_move_current (GtkMenuShell *menu_shell,
4623 GtkMenuDirectionType direction)
4625 GtkMenu *menu = GTK_MENU (menu_shell);
4628 GtkWidget *match = NULL;
4630 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4634 case GTK_MENU_DIR_CHILD:
4635 direction = GTK_MENU_DIR_PARENT;
4637 case GTK_MENU_DIR_PARENT:
4638 direction = GTK_MENU_DIR_CHILD;
4644 /* use special table menu key bindings */
4645 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4647 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4649 if (direction == GTK_MENU_DIR_NEXT)
4651 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4653 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4661 for (i = 0; i < t; i++)
4663 match = find_child_containing (menu_shell,
4664 l, l + 1, i, i + 1);
4670 else if (direction == GTK_MENU_DIR_PREV)
4672 for (i = t; i > 0; i--)
4674 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4682 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4684 match = find_child_containing (menu_shell,
4685 l, l + 1, i - 1, i);
4691 else if (direction == GTK_MENU_DIR_PARENT)
4693 /* we go one left if possible */
4695 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4699 GtkWidget *parent = menu_shell->parent_menu_shell;
4702 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4703 match = menu_shell->active_menu_item;
4706 else if (direction == GTK_MENU_DIR_CHILD)
4708 /* we go one right if possible */
4709 if (r < gtk_menu_get_n_columns (menu))
4710 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4714 GtkWidget *parent = menu_shell->parent_menu_shell;
4716 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4718 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4719 match = menu_shell->active_menu_item;
4725 gtk_menu_shell_select_item (menu_shell, match);
4730 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
4734 get_visible_size (GtkMenu *menu)
4736 GtkWidget *widget = GTK_WIDGET (menu);
4737 GtkContainer *container = GTK_CONTAINER (menu);
4739 gint menu_height = (widget->allocation.height
4740 - 2 * (container->border_width
4741 + widget->style->ythickness));
4743 if (!menu->tearoff_active)
4745 GtkBorder arrow_border;
4747 get_arrows_border (menu, &arrow_border);
4748 menu_height -= arrow_border.top;
4749 menu_height -= arrow_border.bottom;
4755 /* Find the sensitive on-screen child containing @y, or if none,
4756 * the nearest selectable onscreen child. (%NULL if none)
4759 child_at (GtkMenu *menu,
4762 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4763 GtkWidget *child = NULL;
4764 gint child_offset = 0;
4767 gint lower, upper; /* Onscreen bounds */
4769 menu_height = get_visible_size (menu);
4770 lower = menu->scroll_offset;
4771 upper = menu->scroll_offset + menu_height;
4773 for (children = menu_shell->children; children; children = children->next)
4775 if (GTK_WIDGET_VISIBLE (children->data))
4777 GtkRequisition child_requisition;
4779 gtk_widget_size_request (children->data, &child_requisition);
4781 if (_gtk_menu_item_is_selectable (children->data) &&
4782 child_offset >= lower &&
4783 child_offset + child_requisition.height <= upper)
4785 child = children->data;
4787 if (child_offset + child_requisition.height > y &&
4788 !GTK_IS_TEAROFF_MENU_ITEM (child))
4792 child_offset += child_requisition.height;
4800 get_menu_height (GtkMenu *menu)
4803 GtkWidget *widget = GTK_WIDGET (menu);
4805 height = widget->requisition.height;
4806 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4808 if (!menu->tearoff_active)
4810 GtkBorder arrow_border;
4812 get_arrows_border (menu, &arrow_border);
4813 height -= arrow_border.top;
4814 height -= arrow_border.bottom;
4821 gtk_menu_real_move_scroll (GtkMenu *menu,
4824 gint page_size = get_visible_size (menu);
4825 gint end_position = get_menu_height (menu);
4826 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4830 case GTK_SCROLL_PAGE_UP:
4831 case GTK_SCROLL_PAGE_DOWN:
4835 gint child_offset = 0;
4836 gboolean old_upper_arrow_visible;
4839 if (type == GTK_SCROLL_PAGE_UP)
4844 if (menu_shell->active_menu_item)
4848 compute_child_offset (menu, menu_shell->active_menu_item,
4849 &child_offset, &child_height, NULL);
4850 child_offset += child_height / 2;
4853 menu_shell->ignore_enter = TRUE;
4854 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4855 old_offset = menu->scroll_offset;
4857 new_offset = menu->scroll_offset + step;
4858 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4860 gtk_menu_scroll_to (menu, new_offset);
4862 if (menu_shell->active_menu_item)
4864 GtkWidget *new_child;
4865 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4866 GtkBorder arrow_border;
4868 get_arrows_border (menu, &arrow_border);
4870 if (menu->scroll_offset != old_offset)
4871 step = menu->scroll_offset - old_offset;
4873 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
4875 new_child = child_at (menu, child_offset + step);
4877 gtk_menu_shell_select_item (menu_shell, new_child);
4881 case GTK_SCROLL_START:
4882 /* Ignore the enter event we might get if the pointer is on the menu
4884 menu_shell->ignore_enter = TRUE;
4885 gtk_menu_scroll_to (menu, 0);
4886 gtk_menu_shell_select_first (menu_shell, TRUE);
4888 case GTK_SCROLL_END:
4889 /* Ignore the enter event we might get if the pointer is on the menu
4891 menu_shell->ignore_enter = TRUE;
4892 gtk_menu_scroll_to (menu, end_position - page_size);
4893 _gtk_menu_shell_select_last (menu_shell, TRUE);
4902 * gtk_menu_set_monitor:
4904 * @monitor_num: the number of the monitor on which the menu should
4907 * Informs GTK+ on which monitor a menu should be popped up.
4908 * See gdk_screen_get_monitor_geometry().
4910 * This function should be called from a #GtkMenuPositionFunc if the
4911 * menu should not appear on the same monitor as the pointer. This
4912 * information can't be reliably inferred from the coordinates returned
4913 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4914 * may extend beyond the monitor boundaries or even the screen boundaries.
4919 gtk_menu_set_monitor (GtkMenu *menu,
4922 GtkMenuPrivate *priv;
4923 g_return_if_fail (GTK_IS_MENU (menu));
4925 priv = gtk_menu_get_private (menu);
4927 priv->monitor_num = monitor_num;
4931 * gtk_menu_get_for_attach_widget:
4932 * @widget: a #GtkWidget
4934 * Returns a list of the menus which are attached to this widget.
4935 * This list is owned by GTK+ and must not be modified.
4937 * Return value: the list of menus attached to his widget.
4942 gtk_menu_get_for_attach_widget (GtkWidget *widget)
4946 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4948 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
4953 gtk_menu_grab_notify (GtkWidget *widget,
4954 gboolean was_grabbed)
4956 GtkWidget *toplevel;
4957 GtkWindowGroup *group;
4960 toplevel = gtk_widget_get_toplevel (widget);
4961 group = gtk_window_get_group (GTK_WINDOW (toplevel));
4962 grab = _gtk_window_group_get_current_grab (group);
4966 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
4967 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
4971 #define __GTK_MENU_C__
4972 #include "gtkaliasdef.c"