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 get_attach_info (GtkWidget *child)
271 GObject *object = G_OBJECT (child);
272 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
276 ai = g_new0 (AttachInfo, 1);
277 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai, g_free);
284 is_grid_attached (AttachInfo *ai)
286 return (ai->left_attach >= 0 &&
287 ai->right_attach >= 0 &&
288 ai->top_attach >= 0 &&
289 ai->bottom_attach >= 0);
293 menu_ensure_layout (GtkMenu *menu)
295 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
297 if (!priv->have_layout)
299 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
303 gint max_right_attach;
304 gint max_bottom_attach;
306 /* Find extents of gridded portion
308 max_right_attach = 1;
309 max_bottom_attach = 0;
311 for (l = menu_shell->children; l; l = l->next)
313 GtkWidget *child = l->data;
314 AttachInfo *ai = get_attach_info (child);
316 if (is_grid_attached (ai))
318 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
319 max_right_attach = MAX (max_right_attach, ai->right_attach);
325 row_occupied = g_malloc0 (max_bottom_attach);
327 for (l = menu_shell->children; l; l = l->next)
329 GtkWidget *child = l->data;
330 AttachInfo *ai = get_attach_info (child);
332 if (is_grid_attached (ai))
336 for (i = ai->top_attach; i < ai->bottom_attach; i++)
337 row_occupied[i] = TRUE;
341 /* Lay non-grid-items out in those rows
344 for (l = menu_shell->children; l; l = l->next)
346 GtkWidget *child = l->data;
347 AttachInfo *ai = get_attach_info (child);
349 if (!is_grid_attached (ai))
351 while (current_row < max_bottom_attach && row_occupied[current_row])
354 ai->effective_left_attach = 0;
355 ai->effective_right_attach = max_right_attach;
356 ai->effective_top_attach = current_row;
357 ai->effective_bottom_attach = current_row + 1;
363 ai->effective_left_attach = ai->left_attach;
364 ai->effective_right_attach = ai->right_attach;
365 ai->effective_top_attach = ai->top_attach;
366 ai->effective_bottom_attach = ai->bottom_attach;
370 g_free (row_occupied);
372 priv->n_rows = MAX (current_row, max_bottom_attach);
373 priv->n_columns = max_right_attach;
374 priv->have_layout = TRUE;
380 gtk_menu_get_n_columns (GtkMenu *menu)
382 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
384 menu_ensure_layout (menu);
386 return priv->n_columns;
390 gtk_menu_get_n_rows (GtkMenu *menu)
392 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
394 menu_ensure_layout (menu);
400 get_effective_child_attach (GtkWidget *child,
406 GtkMenu *menu = GTK_MENU (child->parent);
409 menu_ensure_layout (menu);
411 ai = get_attach_info (child);
414 *l = ai->effective_left_attach;
416 *r = ai->effective_right_attach;
418 *t = ai->effective_top_attach;
420 *b = ai->effective_bottom_attach;
425 gtk_menu_class_init (GtkMenuClass *class)
427 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
428 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
429 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
430 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
431 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
432 GtkBindingSet *binding_set;
434 gobject_class->finalize = gtk_menu_finalize;
435 gobject_class->set_property = gtk_menu_set_property;
436 gobject_class->get_property = gtk_menu_get_property;
438 object_class->destroy = gtk_menu_destroy;
440 widget_class->realize = gtk_menu_realize;
441 widget_class->unrealize = gtk_menu_unrealize;
442 widget_class->size_request = gtk_menu_size_request;
443 widget_class->size_allocate = gtk_menu_size_allocate;
444 widget_class->show = gtk_menu_show;
445 widget_class->expose_event = gtk_menu_expose;
446 widget_class->scroll_event = gtk_menu_scroll;
447 widget_class->key_press_event = gtk_menu_key_press;
448 widget_class->button_press_event = gtk_menu_button_press;
449 widget_class->button_release_event = gtk_menu_button_release;
450 widget_class->motion_notify_event = gtk_menu_motion_notify;
451 widget_class->show_all = gtk_menu_show_all;
452 widget_class->hide_all = gtk_menu_hide_all;
453 widget_class->enter_notify_event = gtk_menu_enter_notify;
454 widget_class->leave_notify_event = gtk_menu_leave_notify;
455 widget_class->style_set = gtk_menu_style_set;
456 widget_class->focus = gtk_menu_focus;
457 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
458 widget_class->grab_notify = gtk_menu_grab_notify;
460 container_class->remove = gtk_menu_remove;
461 container_class->get_child_property = gtk_menu_get_child_property;
462 container_class->set_child_property = gtk_menu_set_child_property;
464 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
465 menu_shell_class->deactivate = gtk_menu_deactivate;
466 menu_shell_class->select_item = gtk_menu_select_item;
467 menu_shell_class->insert = gtk_menu_real_insert;
468 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
469 menu_shell_class->move_current = gtk_menu_move_current;
471 menu_signals[MOVE_SCROLL] =
472 _gtk_binding_signal_new (I_("move_scroll"),
473 G_OBJECT_CLASS_TYPE (object_class),
474 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
475 G_CALLBACK (gtk_menu_real_move_scroll),
477 _gtk_marshal_VOID__ENUM,
479 GTK_TYPE_SCROLL_TYPE);
481 g_object_class_install_property (gobject_class,
483 g_param_spec_string ("tearoff-title",
485 P_("A title that may be displayed by the window manager when this menu is torn-off"),
487 GTK_PARAM_READWRITE));
490 * GtkMenu:tearoff-state:
492 * A boolean that indicates whether the menu is torn-off.
496 g_object_class_install_property (gobject_class,
498 g_param_spec_boolean ("tearoff-state",
500 P_("A boolean that indicates whether the menu is torn-off"),
502 GTK_PARAM_READWRITE));
504 gtk_widget_class_install_style_property (widget_class,
505 g_param_spec_int ("vertical-padding",
506 P_("Vertical Padding"),
507 P_("Extra space at the top and bottom of the menu"),
511 GTK_PARAM_READABLE));
513 gtk_widget_class_install_style_property (widget_class,
514 g_param_spec_int ("horizontal-padding",
515 P_("Horizontal Padding"),
516 P_("Extra space at the left and right edges of the menu"),
520 GTK_PARAM_READABLE));
522 gtk_widget_class_install_style_property (widget_class,
523 g_param_spec_int ("vertical-offset",
524 P_("Vertical Offset"),
525 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
529 GTK_PARAM_READABLE));
531 gtk_widget_class_install_style_property (widget_class,
532 g_param_spec_int ("horizontal-offset",
533 P_("Horizontal Offset"),
534 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
538 GTK_PARAM_READABLE));
540 gtk_widget_class_install_style_property (widget_class,
541 g_param_spec_boolean ("double-arrows",
543 P_("When scrolling, always show both arrows."),
545 GTK_PARAM_READABLE));
548 gtk_container_class_install_child_property (container_class,
549 CHILD_PROP_LEFT_ATTACH,
550 g_param_spec_int ("left-attach",
552 P_("The column number to attach the left side of the child to"),
554 GTK_PARAM_READWRITE));
556 gtk_container_class_install_child_property (container_class,
557 CHILD_PROP_RIGHT_ATTACH,
558 g_param_spec_int ("right-attach",
560 P_("The column number to attach the right side of the child to"),
562 GTK_PARAM_READWRITE));
564 gtk_container_class_install_child_property (container_class,
565 CHILD_PROP_TOP_ATTACH,
566 g_param_spec_int ("top-attach",
568 P_("The row number to attach the top of the child to"),
570 GTK_PARAM_READWRITE));
572 gtk_container_class_install_child_property (container_class,
573 CHILD_PROP_BOTTOM_ATTACH,
574 g_param_spec_int ("bottom-attach",
576 P_("The row number to attach the bottom of the child to"),
578 GTK_PARAM_READWRITE));
580 binding_set = gtk_binding_set_by_class (class);
581 gtk_binding_entry_add_signal (binding_set,
583 I_("move_current"), 1,
584 GTK_TYPE_MENU_DIRECTION_TYPE,
586 gtk_binding_entry_add_signal (binding_set,
589 GTK_TYPE_MENU_DIRECTION_TYPE,
591 gtk_binding_entry_add_signal (binding_set,
594 GTK_TYPE_MENU_DIRECTION_TYPE,
596 gtk_binding_entry_add_signal (binding_set,
599 GTK_TYPE_MENU_DIRECTION_TYPE,
601 gtk_binding_entry_add_signal (binding_set,
604 GTK_TYPE_MENU_DIRECTION_TYPE,
605 GTK_MENU_DIR_PARENT);
606 gtk_binding_entry_add_signal (binding_set,
609 GTK_TYPE_MENU_DIRECTION_TYPE,
610 GTK_MENU_DIR_PARENT);
611 gtk_binding_entry_add_signal (binding_set,
614 GTK_TYPE_MENU_DIRECTION_TYPE,
616 gtk_binding_entry_add_signal (binding_set,
619 GTK_TYPE_MENU_DIRECTION_TYPE,
621 gtk_binding_entry_add_signal (binding_set,
624 GTK_TYPE_SCROLL_TYPE,
626 gtk_binding_entry_add_signal (binding_set,
629 GTK_TYPE_SCROLL_TYPE,
631 gtk_binding_entry_add_signal (binding_set,
634 GTK_TYPE_SCROLL_TYPE,
636 gtk_binding_entry_add_signal (binding_set,
639 GTK_TYPE_SCROLL_TYPE,
641 gtk_binding_entry_add_signal (binding_set,
644 GTK_TYPE_SCROLL_TYPE,
646 gtk_binding_entry_add_signal (binding_set,
649 GTK_TYPE_SCROLL_TYPE,
651 gtk_binding_entry_add_signal (binding_set,
654 GTK_TYPE_SCROLL_TYPE,
655 GTK_SCROLL_PAGE_DOWN);
656 gtk_binding_entry_add_signal (binding_set,
659 GTK_TYPE_SCROLL_TYPE,
660 GTK_SCROLL_PAGE_DOWN);
662 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
663 P_("Can change accelerators"),
664 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
666 GTK_PARAM_READWRITE));
668 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
669 P_("Delay before submenus appear"),
670 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
674 GTK_PARAM_READWRITE));
676 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
677 P_("Delay before hiding a submenu"),
678 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
681 DEFAULT_POPDOWN_DELAY,
682 GTK_PARAM_READWRITE));
684 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
689 gtk_menu_set_property (GObject *object,
696 menu = GTK_MENU (object);
700 case PROP_TEAROFF_STATE:
701 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
703 case PROP_TEAROFF_TITLE:
704 gtk_menu_set_title (menu, g_value_get_string (value));
707 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
713 gtk_menu_get_property (GObject *object,
720 menu = GTK_MENU (object);
724 case PROP_TEAROFF_STATE:
725 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
727 case PROP_TEAROFF_TITLE:
728 g_value_set_string (value, gtk_menu_get_title (menu));
731 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
737 gtk_menu_set_child_property (GtkContainer *container,
743 GtkMenu *menu = GTK_MENU (container);
744 AttachInfo *ai = get_attach_info (child);
748 case CHILD_PROP_LEFT_ATTACH:
749 ai->left_attach = g_value_get_int (value);
751 case CHILD_PROP_RIGHT_ATTACH:
752 ai->right_attach = g_value_get_int (value);
754 case CHILD_PROP_TOP_ATTACH:
755 ai->top_attach = g_value_get_int (value);
757 case CHILD_PROP_BOTTOM_ATTACH:
758 ai->bottom_attach = g_value_get_int (value);
762 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
766 menu_queue_resize (menu);
770 gtk_menu_get_child_property (GtkContainer *container,
776 AttachInfo *ai = get_attach_info (child);
780 case CHILD_PROP_LEFT_ATTACH:
781 g_value_set_int (value, ai->left_attach);
783 case CHILD_PROP_RIGHT_ATTACH:
784 g_value_set_int (value, ai->right_attach);
786 case CHILD_PROP_TOP_ATTACH:
787 g_value_set_int (value, ai->top_attach);
789 case CHILD_PROP_BOTTOM_ATTACH:
790 g_value_set_int (value, ai->bottom_attach);
794 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
800 gtk_menu_window_event (GtkWidget *window,
804 gboolean handled = FALSE;
806 g_object_ref (window);
812 case GDK_KEY_RELEASE:
813 handled = gtk_widget_event (menu, event);
819 g_object_unref (window);
820 g_object_unref (menu);
826 gtk_menu_window_size_request (GtkWidget *window,
827 GtkRequisition *requisition,
830 GtkMenuPrivate *private = gtk_menu_get_private (menu);
832 if (private->have_position)
834 GdkScreen *screen = gtk_widget_get_screen (window);
835 GdkRectangle monitor;
837 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
839 if (private->y + requisition->height > monitor.y + monitor.height)
840 requisition->height = monitor.y + monitor.height - private->y;
842 if (private->y < monitor.y)
843 requisition->height -= monitor.y - private->y;
848 gtk_menu_init (GtkMenu *menu)
850 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
852 menu->parent_menu_item = NULL;
853 menu->old_active_menu_item = NULL;
854 menu->accel_group = NULL;
855 menu->position_func = NULL;
856 menu->position_func_data = NULL;
857 menu->toggle_size = 0;
859 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
860 "type", GTK_WINDOW_POPUP,
863 "signal::event", gtk_menu_window_event, menu,
864 "signal::size_request", gtk_menu_window_size_request, menu,
865 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
867 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
868 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
870 /* Refloat the menu, so that reference counting for the menu isn't
871 * affected by it being a child of the toplevel
873 g_object_force_floating (G_OBJECT (menu));
874 menu->needs_destruction_ref_count = TRUE;
876 menu->view_window = NULL;
877 menu->bin_window = NULL;
879 menu->scroll_offset = 0;
880 menu->scroll_step = 0;
881 menu->timeout_id = 0;
882 menu->scroll_fast = FALSE;
884 menu->tearoff_window = NULL;
885 menu->tearoff_hbox = NULL;
886 menu->torn_off = FALSE;
887 menu->tearoff_active = FALSE;
888 menu->tearoff_adjustment = NULL;
889 menu->tearoff_scrollbar = NULL;
891 menu->upper_arrow_visible = FALSE;
892 menu->lower_arrow_visible = FALSE;
893 menu->upper_arrow_prelight = FALSE;
894 menu->lower_arrow_prelight = FALSE;
896 priv->upper_arrow_state = GTK_STATE_NORMAL;
897 priv->lower_arrow_state = GTK_STATE_NORMAL;
899 priv->have_layout = FALSE;
903 gtk_menu_destroy (GtkObject *object)
906 GtkMenuAttachData *data;
907 GtkMenuPrivate *priv;
909 g_return_if_fail (GTK_IS_MENU (object));
911 menu = GTK_MENU (object);
913 gtk_menu_remove_scroll_timeout (menu);
915 data = g_object_get_data (G_OBJECT (object), attach_data_key);
917 gtk_menu_detach (menu);
919 gtk_menu_stop_navigating_submenu (menu);
921 if (menu->old_active_menu_item)
923 g_object_unref (menu->old_active_menu_item);
924 menu->old_active_menu_item = NULL;
927 /* Add back the reference count for being a child */
928 if (menu->needs_destruction_ref_count)
930 menu->needs_destruction_ref_count = FALSE;
931 g_object_ref (object);
934 if (menu->accel_group)
936 g_object_unref (menu->accel_group);
937 menu->accel_group = NULL;
941 gtk_widget_destroy (menu->toplevel);
943 if (menu->tearoff_window)
944 gtk_widget_destroy (menu->tearoff_window);
946 priv = gtk_menu_get_private (menu);
950 g_free (priv->heights);
951 priv->heights = NULL;
956 g_free (priv->title);
960 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
964 gtk_menu_finalize (GObject *object)
966 GtkMenu *menu = GTK_MENU (object);
968 g_free (menu->accel_path);
970 G_OBJECT_CLASS (gtk_menu_parent_class)->finalize (object);
974 menu_change_screen (GtkMenu *menu,
975 GdkScreen *new_screen)
977 GtkMenuPrivate *private = gtk_menu_get_private (menu);
979 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
981 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
987 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
988 gtk_menu_position (menu);
991 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
992 private->monitor_num = -1;
996 attach_widget_screen_changed (GtkWidget *attach_widget,
997 GdkScreen *previous_screen,
1000 if (gtk_widget_has_screen (attach_widget) &&
1001 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1003 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1008 gtk_menu_attach_to_widget (GtkMenu *menu,
1009 GtkWidget *attach_widget,
1010 GtkMenuDetachFunc detacher)
1012 GtkMenuAttachData *data;
1015 g_return_if_fail (GTK_IS_MENU (menu));
1016 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1018 /* keep this function in sync with gtk_widget_set_parent()
1021 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1024 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1025 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1029 g_object_ref_sink (menu);
1031 data = g_new (GtkMenuAttachData, 1);
1032 data->attach_widget = attach_widget;
1034 g_signal_connect (attach_widget, "screen_changed",
1035 G_CALLBACK (attach_widget_screen_changed), menu);
1036 attach_widget_screen_changed (attach_widget, NULL, menu);
1038 data->detacher = detacher;
1039 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1040 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1041 if (!g_list_find (list, menu))
1043 list = g_list_prepend (list, menu);
1045 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
1047 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1048 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1050 /* we don't need to set the style here, since
1051 * we are a toplevel widget.
1054 /* Fallback title for menu comes from attach widget */
1055 gtk_menu_update_title (menu);
1059 gtk_menu_get_attach_widget (GtkMenu *menu)
1061 GtkMenuAttachData *data;
1063 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1065 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1067 return data->attach_widget;
1072 gtk_menu_detach (GtkMenu *menu)
1074 GtkMenuAttachData *data;
1077 g_return_if_fail (GTK_IS_MENU (menu));
1079 /* keep this function in sync with gtk_widget_unparent()
1081 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1084 g_warning ("gtk_menu_detach(): menu is not attached");
1087 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1089 g_signal_handlers_disconnect_by_func (data->attach_widget,
1090 (gpointer) attach_widget_screen_changed,
1094 data->detacher (data->attach_widget, menu);
1095 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1096 list = g_list_remove (list, menu);
1098 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
1100 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1102 if (GTK_WIDGET_REALIZED (menu))
1103 gtk_widget_unrealize (GTK_WIDGET (menu));
1107 /* Fallback title for menu comes from attach widget */
1108 gtk_menu_update_title (menu);
1110 g_object_unref (menu);
1114 gtk_menu_remove (GtkContainer *container,
1119 g_return_if_fail (GTK_IS_MENU (container));
1120 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1122 menu = GTK_MENU (container);
1124 /* Clear out old_active_menu_item if it matches the item we are removing
1126 if (menu->old_active_menu_item == widget)
1128 g_object_unref (menu->old_active_menu_item);
1129 menu->old_active_menu_item = NULL;
1132 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1133 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1135 menu_queue_resize (menu);
1141 return g_object_new (GTK_TYPE_MENU, NULL);
1145 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1149 GtkMenu *menu = GTK_MENU (menu_shell);
1150 AttachInfo *ai = get_attach_info (child);
1152 ai->left_attach = -1;
1153 ai->right_attach = -1;
1154 ai->top_attach = -1;
1155 ai->bottom_attach = -1;
1157 if (GTK_WIDGET_REALIZED (menu_shell))
1158 gtk_widget_set_parent_window (child, menu->bin_window);
1160 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1162 menu_queue_resize (menu);
1166 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1171 widget = GTK_WIDGET (menu);
1177 GdkGCValues gc_values;
1179 menu->tearoff_active = FALSE;
1180 menu->saved_scroll_offset = menu->scroll_offset;
1182 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1183 gc = gdk_gc_new_with_values (widget->window,
1184 &gc_values, GDK_GC_SUBWINDOW);
1186 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1188 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1193 gdk_draw_drawable (pixmap, gc,
1194 menu->tearoff_window->window,
1195 0, 0, 0, 0, -1, -1);
1196 g_object_unref (gc);
1198 gtk_widget_set_size_request (menu->tearoff_window,
1202 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1203 g_object_unref (pixmap);
1208 popup_grab_on_window (GdkWindow *window,
1209 guint32 activate_time,
1210 gboolean grab_keyboard)
1212 if ((gdk_pointer_grab (window, TRUE,
1213 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1214 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1215 GDK_POINTER_MOTION_MASK,
1216 NULL, NULL, activate_time) == 0))
1218 if (!grab_keyboard ||
1219 gdk_keyboard_grab (window, TRUE,
1220 activate_time) == 0)
1224 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1235 * @menu: a #GtkMenu.
1236 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1237 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1238 * @func: a user supplied function used to position the menu, or %NULL
1239 * @data: user supplied data to be passed to @func.
1240 * @button: the mouse button which was pressed to initiate the event.
1241 * @activate_time: the time at which the activation event occurred.
1243 * Displays a menu and makes it available for selection. Applications can use
1244 * this function to display context-sensitive menus, and will typically supply
1245 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1246 * parameters. The default menu positioning function will position the menu
1247 * at the current mouse cursor position.
1249 * The @button parameter should be the mouse button pressed to initiate
1250 * the menu popup. If the menu popup was initiated by something other than
1251 * a mouse button press, such as a mouse button release or a keypress,
1252 * @button should be 0.
1254 * The @activate_time parameter should be the time stamp of the event that
1255 * initiated the popup. If such an event is not available, use
1256 * gtk_get_current_event_time() instead.
1260 gtk_menu_popup (GtkMenu *menu,
1261 GtkWidget *parent_menu_shell,
1262 GtkWidget *parent_menu_item,
1263 GtkMenuPositionFunc func,
1266 guint32 activate_time)
1269 GtkWidget *xgrab_shell;
1271 GdkEvent *current_event;
1272 GtkMenuShell *menu_shell;
1273 gboolean grab_keyboard;
1274 GtkMenuPrivate *priv;
1275 GtkWidget *parent_toplevel;
1277 g_return_if_fail (GTK_IS_MENU (menu));
1279 widget = GTK_WIDGET (menu);
1280 menu_shell = GTK_MENU_SHELL (menu);
1281 priv = gtk_menu_get_private (menu);
1283 menu_shell->parent_menu_shell = parent_menu_shell;
1285 priv->seen_item_enter = FALSE;
1287 /* Find the last viewable ancestor, and make an X grab on it
1289 parent = GTK_WIDGET (menu);
1293 gboolean viewable = TRUE;
1294 GtkWidget *tmp = parent;
1298 if (!GTK_WIDGET_MAPPED (tmp))
1307 xgrab_shell = parent;
1309 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1312 /* We want to receive events generated when we map the menu; unfortunately,
1313 * since there is probably already an implicit grab in place from the
1314 * button that the user used to pop up the menu, we won't receive then --
1315 * in particular, the EnterNotify when the menu pops up under the pointer.
1317 * If we are grabbing on a parent menu shell, no problem; just grab on
1318 * that menu shell first before popping up the window with owner_events = TRUE.
1320 * When grabbing on the menu itself, things get more convuluted - we
1321 * we do an explicit grab on a specially created window with
1322 * owner_events = TRUE, which we override further down with a grab
1323 * on the menu. (We can't grab on the menu until it is mapped; we
1324 * probably could just leave the grab on the other window, with a
1325 * little reorganization of the code in gtkmenu*).
1327 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1328 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1330 if (xgrab_shell && xgrab_shell != widget)
1332 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1333 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1337 GdkWindow *transfer_window;
1339 xgrab_shell = widget;
1340 transfer_window = menu_grab_transfer_window_get (menu);
1341 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1342 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1345 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1347 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1348 * with a stuck up window, we just abort here. Presumably the user will
1351 menu_shell->parent_menu_shell = NULL;
1352 menu_grab_transfer_window_destroy (menu);
1356 menu_shell->active = TRUE;
1357 menu_shell->button = button;
1359 /* If we are popping up the menu from something other than, a button
1360 * press then, as a heuristic, we ignore enter events for the menu
1361 * until we get a MOTION_NOTIFY.
1364 current_event = gtk_get_current_event ();
1367 if ((current_event->type != GDK_BUTTON_PRESS) &&
1368 (current_event->type != GDK_ENTER_NOTIFY))
1369 menu_shell->ignore_enter = TRUE;
1371 gdk_event_free (current_event);
1374 menu_shell->ignore_enter = TRUE;
1378 gtk_menu_tearoff_bg_copy (menu);
1380 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1383 parent_toplevel = NULL;
1384 if (parent_menu_shell)
1385 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1386 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1388 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1390 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1393 /* Set transient for to get the right window group and parent relationship */
1394 if (parent_toplevel && GTK_IS_WINDOW (parent_toplevel))
1395 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1396 GTK_WINDOW (parent_toplevel));
1398 menu->parent_menu_item = parent_menu_item;
1399 menu->position_func = func;
1400 menu->position_func_data = data;
1401 menu_shell->activate_time = activate_time;
1403 /* We need to show the menu here rather in the init function because
1404 * code expects to be able to tell if the menu is onscreen by
1405 * looking at the GTK_WIDGET_VISIBLE (menu)
1407 gtk_widget_show (GTK_WIDGET (menu));
1409 /* Position the menu, possibly changing the size request
1411 gtk_menu_position (menu);
1413 /* Compute the size of the toplevel and realize it so we
1414 * can scroll correctly.
1417 GtkRequisition tmp_request;
1418 GtkAllocation tmp_allocation = { 0, };
1420 gtk_widget_size_request (menu->toplevel, &tmp_request);
1422 tmp_allocation.width = tmp_request.width;
1423 tmp_allocation.height = tmp_request.height;
1425 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1427 gtk_widget_realize (GTK_WIDGET (menu));
1430 gtk_menu_scroll_to (menu, menu->scroll_offset);
1432 /* Once everything is set up correctly, map the toplevel window on
1435 gtk_widget_show (menu->toplevel);
1437 if (xgrab_shell == widget)
1438 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1439 gtk_grab_add (GTK_WIDGET (menu));
1443 gtk_menu_popdown (GtkMenu *menu)
1445 GtkMenuPrivate *private;
1446 GtkMenuShell *menu_shell;
1448 g_return_if_fail (GTK_IS_MENU (menu));
1450 menu_shell = GTK_MENU_SHELL (menu);
1451 private = gtk_menu_get_private (menu);
1453 menu_shell->parent_menu_shell = NULL;
1454 menu_shell->active = FALSE;
1455 menu_shell->ignore_enter = FALSE;
1457 private->have_position = FALSE;
1459 gtk_menu_stop_scrolling (menu);
1461 gtk_menu_stop_navigating_submenu (menu);
1463 if (menu_shell->active_menu_item)
1465 if (menu->old_active_menu_item)
1466 g_object_unref (menu->old_active_menu_item);
1467 menu->old_active_menu_item = menu_shell->active_menu_item;
1468 g_object_ref (menu->old_active_menu_item);
1471 gtk_menu_shell_deselect (menu_shell);
1473 /* The X Grab, if present, will automatically be removed when we hide
1475 gtk_widget_hide (menu->toplevel);
1476 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1480 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1482 if (GTK_BIN (menu->toplevel)->child)
1484 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1488 /* We popped up the menu from the tearoff, so we need to
1489 * release the grab - we aren't actually hiding the menu.
1491 if (menu_shell->have_xgrab)
1493 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1495 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1496 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1500 /* gtk_menu_popdown is called each time a menu item is selected from
1501 * a torn off menu. Only scroll back to the saved position if the
1502 * non-tearoff menu was popped down.
1504 if (!menu->tearoff_active)
1505 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1506 menu->tearoff_active = TRUE;
1509 gtk_widget_hide (GTK_WIDGET (menu));
1511 menu_shell->have_xgrab = FALSE;
1512 gtk_grab_remove (GTK_WIDGET (menu));
1514 menu_grab_transfer_window_destroy (menu);
1518 gtk_menu_get_active (GtkMenu *menu)
1523 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1525 if (!menu->old_active_menu_item)
1528 children = GTK_MENU_SHELL (menu)->children;
1532 child = children->data;
1533 children = children->next;
1535 if (GTK_BIN (child)->child)
1540 menu->old_active_menu_item = child;
1541 if (menu->old_active_menu_item)
1542 g_object_ref (menu->old_active_menu_item);
1545 return menu->old_active_menu_item;
1549 gtk_menu_set_active (GtkMenu *menu,
1555 g_return_if_fail (GTK_IS_MENU (menu));
1557 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1560 child = tmp_list->data;
1561 if (GTK_BIN (child)->child)
1563 if (menu->old_active_menu_item)
1564 g_object_unref (menu->old_active_menu_item);
1565 menu->old_active_menu_item = child;
1566 g_object_ref (menu->old_active_menu_item);
1572 gtk_menu_set_accel_group (GtkMenu *menu,
1573 GtkAccelGroup *accel_group)
1575 g_return_if_fail (GTK_IS_MENU (menu));
1577 if (menu->accel_group != accel_group)
1579 if (menu->accel_group)
1580 g_object_unref (menu->accel_group);
1581 menu->accel_group = accel_group;
1582 if (menu->accel_group)
1583 g_object_ref (menu->accel_group);
1584 _gtk_menu_refresh_accel_paths (menu, TRUE);
1589 gtk_menu_get_accel_group (GtkMenu *menu)
1591 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1593 return menu->accel_group;
1597 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1600 /* Menu items chain here to figure whether they can activate their
1601 * accelerators. Unlike ordinary widgets, menus allow accel
1602 * activation even if invisible since that's the usual case for
1603 * submenus/popup-menus. however, the state of the attach widget
1604 * affects the "activeness" of the menu.
1606 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1609 return gtk_widget_can_activate_accel (awidget, signal_id);
1611 return GTK_WIDGET_IS_SENSITIVE (widget);
1615 * gtk_menu_set_accel_path
1616 * @menu: a valid #GtkMenu
1617 * @accel_path: a valid accelerator path
1619 * Sets an accelerator path for this menu from which accelerator paths
1620 * for its immediate children, its menu items, can be constructed.
1621 * The main purpose of this function is to spare the programmer the
1622 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1623 * each menu item that should support runtime user changable accelerators.
1624 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1625 * each menu item of this menu, that contains a label describing its purpose,
1626 * automatically gets an accel path assigned. For example, a menu containing
1627 * menu items "New" and "Exit", will, after
1628 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1629 * has been called, assign its items the accel paths:
1630 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1631 * Assigning accel paths to menu items then enables the user to change
1632 * their accelerators at runtime. More details about accelerator paths
1633 * and their default setups can be found at gtk_accel_map_add_entry().
1636 gtk_menu_set_accel_path (GtkMenu *menu,
1637 const gchar *accel_path)
1639 g_return_if_fail (GTK_IS_MENU (menu));
1641 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1643 g_free (menu->accel_path);
1644 menu->accel_path = g_strdup (accel_path);
1645 if (menu->accel_path)
1646 _gtk_menu_refresh_accel_paths (menu, FALSE);
1651 gboolean group_changed;
1655 refresh_accel_paths_foreach (GtkWidget *widget,
1658 AccelPropagation *prop = data;
1660 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1661 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1662 prop->menu->accel_path,
1663 prop->menu->accel_group,
1664 prop->group_changed);
1668 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1669 gboolean group_changed)
1671 g_return_if_fail (GTK_IS_MENU (menu));
1673 if (menu->accel_path && menu->accel_group)
1675 AccelPropagation prop;
1678 prop.group_changed = group_changed;
1679 gtk_container_foreach (GTK_CONTAINER (menu),
1680 refresh_accel_paths_foreach,
1686 gtk_menu_reposition (GtkMenu *menu)
1688 g_return_if_fail (GTK_IS_MENU (menu));
1690 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1691 gtk_menu_position (menu);
1695 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1698 g_return_if_fail (GTK_IS_MENU (menu));
1700 if (adjustment->value != menu->scroll_offset)
1701 gtk_menu_scroll_to (menu, adjustment->value);
1705 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1708 GdkGeometry geometry_hints;
1710 if (!menu->tearoff_window)
1713 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1715 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1716 width += menu->tearoff_scrollbar->requisition.width;
1719 geometry_hints.min_width = width;
1720 geometry_hints.max_width = width;
1722 geometry_hints.min_height = 0;
1723 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1725 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1728 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1732 gtk_menu_update_title (GtkMenu *menu)
1734 if (menu->tearoff_window)
1737 GtkWidget *attach_widget;
1739 title = gtk_menu_get_title (menu);
1742 attach_widget = gtk_menu_get_attach_widget (menu);
1743 if (GTK_IS_MENU_ITEM (attach_widget))
1745 GtkWidget *child = GTK_BIN (attach_widget)->child;
1746 if (GTK_IS_LABEL (child))
1747 title = gtk_label_get_text (GTK_LABEL (child));
1752 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1757 gtk_menu_get_toplevel (GtkWidget *menu)
1759 GtkWidget *attach, *toplevel;
1761 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1763 if (GTK_IS_MENU_ITEM (attach))
1764 attach = attach->parent;
1766 if (GTK_IS_MENU (attach))
1767 return gtk_menu_get_toplevel (attach);
1768 else if (GTK_IS_WIDGET (attach))
1770 toplevel = gtk_widget_get_toplevel (attach);
1771 if (GTK_WIDGET_TOPLEVEL (toplevel))
1779 tearoff_window_destroyed (GtkWidget *widget,
1782 gtk_menu_set_tearoff_state (menu, FALSE);
1786 gtk_menu_set_tearoff_state (GtkMenu *menu,
1791 g_return_if_fail (GTK_IS_MENU (menu));
1793 if (menu->torn_off != torn_off)
1795 menu->torn_off = torn_off;
1796 menu->tearoff_active = torn_off;
1800 if (GTK_WIDGET_VISIBLE (menu))
1801 gtk_menu_popdown (menu);
1803 if (!menu->tearoff_window)
1805 GtkWidget *toplevel;
1807 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1808 "type", GTK_WINDOW_TOPLEVEL,
1809 "screen", gtk_widget_get_screen (menu->toplevel),
1810 "app-paintable", TRUE,
1813 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1814 GDK_WINDOW_TYPE_HINT_MENU);
1815 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1816 g_signal_connect (menu->tearoff_window, "destroy",
1817 G_CALLBACK (tearoff_window_destroyed), menu);
1818 g_signal_connect (menu->tearoff_window, "event",
1819 G_CALLBACK (gtk_menu_window_event), menu);
1821 gtk_menu_update_title (menu);
1823 gtk_widget_realize (menu->tearoff_window);
1825 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1826 if (toplevel != NULL)
1827 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1828 GTK_WINDOW (toplevel));
1830 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1831 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1833 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1834 menu->tearoff_adjustment =
1835 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1837 GTK_WIDGET (menu)->requisition.height,
1841 g_object_connect (menu->tearoff_adjustment,
1842 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1844 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1846 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1847 menu->tearoff_scrollbar,
1850 if (menu->tearoff_adjustment->upper > height)
1851 gtk_widget_show (menu->tearoff_scrollbar);
1853 gtk_widget_show (menu->tearoff_hbox);
1856 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1858 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1860 /* Update menu->requisition
1862 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1864 gtk_menu_set_tearoff_hints (menu, width);
1866 gtk_widget_realize (menu->tearoff_window);
1867 gtk_menu_position (menu);
1869 gtk_widget_show (GTK_WIDGET (menu));
1870 gtk_widget_show (menu->tearoff_window);
1872 gtk_menu_scroll_to (menu, 0);
1877 gtk_widget_hide (GTK_WIDGET (menu));
1878 gtk_widget_hide (menu->tearoff_window);
1879 if (GTK_IS_CONTAINER (menu->toplevel))
1880 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1881 gtk_widget_destroy (menu->tearoff_window);
1883 menu->tearoff_window = NULL;
1884 menu->tearoff_hbox = NULL;
1885 menu->tearoff_scrollbar = NULL;
1886 menu->tearoff_adjustment = NULL;
1889 g_object_notify (G_OBJECT (menu), "tearoff-state");
1894 * gtk_menu_get_tearoff_state:
1897 * Returns whether the menu is torn off. See
1898 * gtk_menu_set_tearoff_state ().
1900 * Return value: %TRUE if the menu is currently torn off.
1903 gtk_menu_get_tearoff_state (GtkMenu *menu)
1905 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1907 return menu->torn_off;
1911 * gtk_menu_set_title:
1913 * @title: a string containing the title for the menu.
1915 * Sets the title string for the menu. The title is displayed when the menu
1916 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
1917 * attached to a parent menu item, and if so it will try to use the same text as
1918 * that menu item's label.
1921 gtk_menu_set_title (GtkMenu *menu,
1924 GtkMenuPrivate *priv;
1927 g_return_if_fail (GTK_IS_MENU (menu));
1929 priv = gtk_menu_get_private (menu);
1931 old_title = priv->title;
1932 priv->title = g_strdup (title);
1935 gtk_menu_update_title (menu);
1936 g_object_notify (G_OBJECT (menu), "tearoff-title");
1940 * gtk_menu_get_title:
1943 * Returns the title of the menu. See gtk_menu_set_title().
1945 * Return value: the title of the menu, or %NULL if the menu has no
1946 * title set on it. This string is owned by the widget and should
1947 * not be modified or freed.
1949 G_CONST_RETURN gchar *
1950 gtk_menu_get_title (GtkMenu *menu)
1952 GtkMenuPrivate *priv;
1954 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1956 priv = gtk_menu_get_private (menu);
1962 gtk_menu_reorder_child (GtkMenu *menu,
1966 GtkMenuShell *menu_shell;
1968 g_return_if_fail (GTK_IS_MENU (menu));
1969 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1971 menu_shell = GTK_MENU_SHELL (menu);
1973 if (g_list_find (menu_shell->children, child))
1975 menu_shell->children = g_list_remove (menu_shell->children, child);
1976 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1978 menu_queue_resize (menu);
1983 gtk_menu_style_set (GtkWidget *widget,
1984 GtkStyle *previous_style)
1986 if (GTK_WIDGET_REALIZED (widget))
1988 GtkMenu *menu = GTK_MENU (widget);
1990 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1991 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1992 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1997 gtk_menu_realize (GtkWidget *widget)
1999 GdkWindowAttr attributes;
2000 gint attributes_mask;
2005 guint vertical_padding;
2006 guint horizontal_padding;
2007 guint scroll_arrow_height;
2009 g_return_if_fail (GTK_IS_MENU (widget));
2011 menu = GTK_MENU (widget);
2013 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2015 attributes.window_type = GDK_WINDOW_CHILD;
2016 attributes.x = widget->allocation.x;
2017 attributes.y = widget->allocation.y;
2018 attributes.width = widget->allocation.width;
2019 attributes.height = widget->allocation.height;
2020 attributes.wclass = GDK_INPUT_OUTPUT;
2021 attributes.visual = gtk_widget_get_visual (widget);
2022 attributes.colormap = gtk_widget_get_colormap (widget);
2024 attributes.event_mask = gtk_widget_get_events (widget);
2026 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2027 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2029 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2030 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2031 gdk_window_set_user_data (widget->window, widget);
2033 border_width = GTK_CONTAINER (widget)->border_width;
2035 gtk_widget_style_get (GTK_WIDGET (menu),
2036 "vertical-padding", &vertical_padding,
2037 "horizontal-padding", &horizontal_padding,
2038 "scroll-arrow-vlength", &scroll_arrow_height,
2041 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2042 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2043 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2044 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2046 if (menu->upper_arrow_visible)
2048 attributes.y += scroll_arrow_height;
2049 attributes.height -= scroll_arrow_height;
2052 if (menu->lower_arrow_visible)
2053 attributes.height -= scroll_arrow_height;
2055 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2056 gdk_window_set_user_data (menu->view_window, menu);
2060 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2061 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2063 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2064 gdk_window_set_user_data (menu->bin_window, menu);
2066 children = GTK_MENU_SHELL (menu)->children;
2069 child = children->data;
2070 children = children->next;
2072 gtk_widget_set_parent_window (child, menu->bin_window);
2075 widget->style = gtk_style_attach (widget->style, widget->window);
2076 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2077 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2078 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2080 if (GTK_MENU_SHELL (widget)->active_menu_item)
2081 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2082 GTK_MENU_SHELL (widget)->active_menu_item);
2084 gdk_window_show (menu->bin_window);
2085 gdk_window_show (menu->view_window);
2089 gtk_menu_focus (GtkWidget *widget,
2090 GtkDirectionType direction)
2093 * A menu or its menu items cannot have focus
2098 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2101 menu_grab_transfer_window_get (GtkMenu *menu)
2103 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2106 GdkWindowAttr attributes;
2107 gint attributes_mask;
2109 attributes.x = -100;
2110 attributes.y = -100;
2111 attributes.width = 10;
2112 attributes.height = 10;
2113 attributes.window_type = GDK_WINDOW_TEMP;
2114 attributes.wclass = GDK_INPUT_ONLY;
2115 attributes.override_redirect = TRUE;
2116 attributes.event_mask = 0;
2118 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2120 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2121 &attributes, attributes_mask);
2122 gdk_window_set_user_data (window, menu);
2124 gdk_window_show (window);
2126 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2133 menu_grab_transfer_window_destroy (GtkMenu *menu)
2135 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2138 gdk_window_set_user_data (window, NULL);
2139 gdk_window_destroy (window);
2140 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2145 gtk_menu_unrealize (GtkWidget *widget)
2149 g_return_if_fail (GTK_IS_MENU (widget));
2151 menu = GTK_MENU (widget);
2153 menu_grab_transfer_window_destroy (menu);
2155 gdk_window_set_user_data (menu->view_window, NULL);
2156 gdk_window_destroy (menu->view_window);
2157 menu->view_window = NULL;
2159 gdk_window_set_user_data (menu->bin_window, NULL);
2160 gdk_window_destroy (menu->bin_window);
2161 menu->bin_window = NULL;
2163 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize) (widget);
2167 gtk_menu_size_request (GtkWidget *widget,
2168 GtkRequisition *requisition)
2172 GtkMenuShell *menu_shell;
2175 guint max_toggle_size;
2176 guint max_accel_width;
2177 guint vertical_padding;
2178 guint horizontal_padding;
2179 GtkRequisition child_requisition;
2180 GtkMenuPrivate *priv;
2182 g_return_if_fail (GTK_IS_MENU (widget));
2183 g_return_if_fail (requisition != NULL);
2185 menu = GTK_MENU (widget);
2186 menu_shell = GTK_MENU_SHELL (widget);
2187 priv = gtk_menu_get_private (menu);
2189 requisition->width = 0;
2190 requisition->height = 0;
2192 max_toggle_size = 0;
2193 max_accel_width = 0;
2195 g_free (priv->heights);
2196 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2197 priv->heights_length = gtk_menu_get_n_rows (menu);
2199 children = menu_shell->children;
2206 child = children->data;
2207 children = children->next;
2209 if (! GTK_WIDGET_VISIBLE (child))
2212 get_effective_child_attach (child, &l, &r, &t, &b);
2214 /* It's important to size_request the child
2215 * before doing the toggle size request, in
2216 * case the toggle size request depends on the size
2217 * request of a child of the child (e.g. for ImageMenuItem)
2220 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2221 gtk_widget_size_request (child, &child_requisition);
2223 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2224 max_toggle_size = MAX (max_toggle_size, toggle_size);
2225 max_accel_width = MAX (max_accel_width,
2226 GTK_MENU_ITEM (child)->accelerator_width);
2228 part = child_requisition.width / (r - l);
2229 requisition->width = MAX (requisition->width, part);
2231 part = MAX (child_requisition.height, toggle_size) / (b - t);
2232 priv->heights[t] = MAX (priv->heights[t], part);
2235 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2236 requisition->height += priv->heights[i];
2238 requisition->width += max_toggle_size + max_accel_width;
2239 requisition->width *= gtk_menu_get_n_columns (menu);
2241 gtk_widget_style_get (GTK_WIDGET (menu),
2242 "vertical-padding", &vertical_padding,
2243 "horizontal-padding", &horizontal_padding,
2246 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2247 widget->style->xthickness) * 2;
2248 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2249 widget->style->ythickness) * 2;
2251 menu->toggle_size = max_toggle_size;
2253 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2255 if (menu->tearoff_active)
2256 gtk_menu_set_tearoff_hints (menu, requisition->width);
2260 gtk_menu_size_allocate (GtkWidget *widget,
2261 GtkAllocation *allocation)
2264 GtkMenuShell *menu_shell;
2266 GtkAllocation child_allocation;
2267 GtkRequisition child_requisition;
2268 GtkMenuPrivate *priv;
2272 guint vertical_padding;
2273 guint horizontal_padding;
2274 gint scroll_arrow_height;
2276 g_return_if_fail (GTK_IS_MENU (widget));
2277 g_return_if_fail (allocation != NULL);
2279 menu = GTK_MENU (widget);
2280 menu_shell = GTK_MENU_SHELL (widget);
2281 priv = gtk_menu_get_private (menu);
2283 widget->allocation = *allocation;
2284 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2286 gtk_widget_style_get (GTK_WIDGET (menu),
2287 "vertical-padding", &vertical_padding,
2288 "horizontal-padding", &horizontal_padding,
2289 "scroll-arrow-vlength", &scroll_arrow_height,
2292 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2293 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2295 width = MAX (1, allocation->width - x * 2);
2296 height = MAX (1, allocation->height - y * 2);
2298 child_requisition.width -= x * 2;
2299 child_requisition.height -= y * 2;
2301 if (menu_shell->active)
2302 gtk_menu_scroll_to (menu, menu->scroll_offset);
2304 if (menu->upper_arrow_visible && !menu->tearoff_active)
2306 y += scroll_arrow_height;
2307 height -= scroll_arrow_height;
2310 if (menu->lower_arrow_visible && !menu->tearoff_active)
2311 height -= scroll_arrow_height;
2313 if (GTK_WIDGET_REALIZED (widget))
2315 gdk_window_move_resize (widget->window,
2316 allocation->x, allocation->y,
2317 allocation->width, allocation->height);
2319 gdk_window_move_resize (menu->view_window,
2326 if (menu_shell->children)
2328 gint base_width = width / gtk_menu_get_n_columns (menu);
2330 children = menu_shell->children;
2333 child = children->data;
2334 children = children->next;
2336 if (GTK_WIDGET_VISIBLE (child))
2341 get_effective_child_attach (child, &l, &r, &t, &b);
2343 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2346 tmp = gtk_menu_get_n_columns (menu) - l;
2347 l = gtk_menu_get_n_columns (menu) - r;
2351 child_allocation.width = (r - l) * base_width;
2352 child_allocation.height = 0;
2353 child_allocation.x = l * base_width;
2354 child_allocation.y = 0;
2356 for (i = 0; i < b; i++)
2359 child_allocation.y += priv->heights[i];
2361 child_allocation.height += priv->heights[i];
2364 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2367 gtk_widget_size_allocate (child, &child_allocation);
2368 gtk_widget_queue_draw (child);
2372 /* Resize the item window */
2373 if (GTK_WIDGET_REALIZED (widget))
2379 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2380 height += priv->heights[i];
2382 width = gtk_menu_get_n_columns (menu) * base_width;
2383 gdk_window_resize (menu->bin_window, width, height);
2386 if (menu->tearoff_active)
2388 if (allocation->height >= widget->requisition.height)
2390 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2392 gtk_widget_hide (menu->tearoff_scrollbar);
2393 gtk_menu_set_tearoff_hints (menu, allocation->width);
2395 gtk_menu_scroll_to (menu, 0);
2400 menu->tearoff_adjustment->upper = widget->requisition.height;
2401 menu->tearoff_adjustment->page_size = allocation->height;
2403 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2404 menu->tearoff_adjustment->upper)
2407 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2410 gtk_menu_scroll_to (menu, value);
2413 gtk_adjustment_changed (menu->tearoff_adjustment);
2415 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2417 gtk_widget_show (menu->tearoff_scrollbar);
2418 gtk_menu_set_tearoff_hints (menu, allocation->width);
2426 gtk_menu_paint (GtkWidget *widget,
2427 GdkEventExpose *event)
2430 GtkMenuPrivate *priv;
2432 gint border_x, border_y;
2433 guint vertical_padding;
2434 guint horizontal_padding;
2435 gint scroll_arrow_height;
2437 g_return_if_fail (GTK_IS_MENU (widget));
2439 menu = GTK_MENU (widget);
2440 priv = gtk_menu_get_private (menu);
2442 gtk_widget_style_get (GTK_WIDGET (menu),
2443 "vertical-padding", &vertical_padding,
2444 "horizontal-padding", &horizontal_padding,
2445 "scroll-arrow-vlength", &scroll_arrow_height,
2448 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2449 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2450 gdk_drawable_get_size (widget->window, &width, &height);
2452 if (event->window == widget->window)
2454 gint arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2455 gint arrow_size = 0.7 * arrow_space;
2457 gtk_paint_box (widget->style,
2461 NULL, widget, "menu",
2464 if (menu->upper_arrow_visible && !menu->tearoff_active)
2466 gtk_paint_box (widget->style,
2468 priv->upper_arrow_state,
2470 NULL, widget, "menu",
2473 width - 2 * border_x,
2474 scroll_arrow_height);
2476 gtk_paint_arrow (widget->style,
2478 priv->upper_arrow_state,
2480 NULL, widget, "menu_scroll_arrow_up",
2483 (width - arrow_size ) / 2,
2484 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2485 arrow_size, arrow_size);
2488 if (menu->lower_arrow_visible && !menu->tearoff_active)
2490 gtk_paint_box (widget->style,
2492 priv->lower_arrow_state,
2494 NULL, widget, "menu",
2496 height - border_y - scroll_arrow_height,
2498 scroll_arrow_height);
2500 gtk_paint_arrow (widget->style,
2502 priv->lower_arrow_state,
2504 NULL, widget, "menu_scroll_arrow_down",
2507 (width - arrow_size) / 2,
2508 height - border_y - scroll_arrow_height +
2509 widget->style->ythickness + (arrow_space - arrow_size)/2,
2510 arrow_size, arrow_size);
2513 else if (event->window == menu->bin_window)
2515 gtk_paint_box (widget->style,
2519 NULL, widget, "menu",
2520 - border_x, menu->scroll_offset - border_y,
2526 gtk_menu_expose (GtkWidget *widget,
2527 GdkEventExpose *event)
2529 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2530 g_return_val_if_fail (event != NULL, FALSE);
2532 if (GTK_WIDGET_DRAWABLE (widget))
2534 gtk_menu_paint (widget, event);
2536 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event) (widget, event);
2543 gtk_menu_show (GtkWidget *widget)
2545 GtkMenu *menu = GTK_MENU (widget);
2547 _gtk_menu_refresh_accel_paths (menu, FALSE);
2549 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2553 gtk_menu_button_scroll (GtkWidget *widget,
2554 GdkEventButton *event)
2556 if (GTK_IS_MENU (widget))
2558 GtkMenu *menu = GTK_MENU (widget);
2560 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2562 GtkSettings *settings = gtk_widget_get_settings (widget);
2563 gboolean touchscreen_mode;
2565 g_object_get (G_OBJECT (settings),
2566 "gtk-touchscreen-mode", &touchscreen_mode,
2569 if (touchscreen_mode)
2570 gtk_menu_handle_scrolling (menu,
2571 event->x_root, event->y_root,
2572 event->type == GDK_BUTTON_PRESS,
2583 gtk_menu_button_press (GtkWidget *widget,
2584 GdkEventButton *event)
2586 if (event->type != GDK_BUTTON_PRESS)
2589 /* Don't pop down the menu for presses over scroll arrows
2591 if (gtk_menu_button_scroll (widget, event))
2594 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2598 gtk_menu_button_release (GtkWidget *widget,
2599 GdkEventButton *event)
2601 if (GTK_IS_MENU (widget))
2603 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2605 if (priv->ignore_button_release)
2607 priv->ignore_button_release = FALSE;
2612 if (event->type != GDK_BUTTON_RELEASE)
2615 /* Don't pop down the menu for releases over scroll arrows
2617 if (gtk_menu_button_scroll (widget, event))
2620 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
2623 static const gchar *
2624 get_accel_path (GtkWidget *menu_item,
2629 GClosure *accel_closure;
2630 GtkAccelGroup *accel_group;
2632 path = _gtk_widget_get_accel_path (menu_item, locked);
2635 path = GTK_MENU_ITEM (menu_item)->accel_path;
2641 label = GTK_BIN (menu_item)->child;
2643 if (GTK_IS_ACCEL_LABEL (label))
2645 g_object_get (label,
2646 "accel-closure", &accel_closure,
2650 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2652 *locked = accel_group->lock_count > 0;
2662 gtk_menu_key_press (GtkWidget *widget,
2665 GtkMenuShell *menu_shell;
2667 gboolean delete = FALSE;
2668 gboolean can_change_accels;
2669 gchar *accel = NULL;
2670 guint accel_key, accel_mods;
2671 GdkModifierType consumed_modifiers;
2672 GdkDisplay *display;
2674 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2675 g_return_val_if_fail (event != NULL, FALSE);
2677 menu_shell = GTK_MENU_SHELL (widget);
2678 menu = GTK_MENU (widget);
2680 gtk_menu_stop_navigating_submenu (menu);
2682 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
2685 display = gtk_widget_get_display (widget);
2687 g_object_get (gtk_widget_get_settings (widget),
2688 "gtk-menu-bar-accel", &accel,
2689 "gtk-can-change-accels", &can_change_accels,
2692 if (accel && *accel)
2695 GdkModifierType mods = 0;
2696 gboolean handled = FALSE;
2698 gtk_accelerator_parse (accel, &keyval, &mods);
2701 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2703 /* FIXME this is wrong, needs to be in the global accel resolution
2704 * thing, to properly consider i18n etc., but that probably requires
2705 * AccelGroup changes etc.
2707 if (event->keyval == keyval &&
2708 (mods & event->state) == mods)
2709 gtk_menu_shell_cancel (menu_shell);
2717 switch (event->keyval)
2728 /* Figure out what modifiers went into determining the key symbol */
2729 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2730 event->hardware_keycode, event->state, event->group,
2731 NULL, NULL, NULL, &consumed_modifiers);
2733 accel_key = gdk_keyval_to_lower (event->keyval);
2734 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2736 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2737 * We re-upper case when we match against the keyval, but display and save in caseless form.
2739 if (accel_key != event->keyval)
2740 accel_mods |= GDK_SHIFT_MASK;
2742 /* Modify the accelerators */
2743 if (can_change_accels &&
2744 menu_shell->active_menu_item &&
2745 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2746 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2747 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2749 GtkWidget *menu_item = menu_shell->active_menu_item;
2750 gboolean locked, replace_accels = TRUE;
2753 path = get_accel_path (menu_item, &locked);
2754 if (!path || locked)
2756 /* can't change accelerators on menu_items without paths
2757 * (basically, those items are accelerator-locked).
2759 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2760 gtk_widget_error_bell (widget);
2766 /* For the keys that act to delete the current setting, we delete
2767 * the current setting if there is one, otherwise, we set the
2768 * key as the accelerator.
2774 if (gtk_accel_map_lookup_entry (path, &key) &&
2775 (key.accel_key || key.accel_mods))
2781 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2785 /* we failed, probably because this key is in use and
2788 /* g_print("failed to change\n"); */
2789 gtk_widget_error_bell (widget);
2798 check_threshold (GtkWidget *widget,
2799 int start_x, int start_y,
2805 ABS (start_x - x) > THRESHOLD ||
2806 ABS (start_y - y) > THRESHOLD;
2810 definitely_within_item (GtkWidget *widget,
2814 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
2817 gdk_drawable_get_size (window, &w, &h);
2820 check_threshold (widget, 0, 0, x, y) &&
2821 check_threshold (widget, w - 1, 0, x, y) &&
2822 check_threshold (widget, w - 1, h - 1, x, y) &&
2823 check_threshold (widget, 0, h - 1, x, y);
2827 gtk_menu_motion_notify (GtkWidget *widget,
2828 GdkEventMotion *event)
2830 GtkWidget *menu_item;
2832 GtkMenuShell *menu_shell;
2834 gboolean need_enter;
2836 if (GTK_IS_MENU (widget))
2838 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2840 if (priv->ignore_button_release)
2841 priv->ignore_button_release = FALSE;
2843 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
2847 /* We received the event for one of two reasons:
2849 * a) We are the active menu, and did gtk_grab_add()
2850 * b) The widget is a child of ours, and the event was propagated
2852 * Since for computation of navigation regions, we want the menu which
2853 * is the parent of the menu item, for a), we need to find that menu,
2854 * which may be different from 'widget'.
2856 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2857 if (!GTK_IS_MENU_ITEM (menu_item) ||
2858 !GTK_IS_MENU (menu_item->parent))
2861 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2862 menu = GTK_MENU (menu_shell);
2864 if (definitely_within_item (menu_item, event->x, event->y))
2865 menu_shell->activate_time = 0;
2867 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2869 /* Check to see if we are within an active submenu's navigation region
2871 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2874 /* Make sure we pop down if we enter a non-selectable menu item, so we
2875 * don't show a submenu when the cursor is outside the stay-up triangle.
2877 if (!_gtk_menu_item_is_selectable (menu_item))
2879 gtk_menu_shell_deselect (menu_shell);
2885 /* The menu is now sensitive to enter events on its items, but
2886 * was previously sensitive. So we fake an enter event.
2890 menu_shell->ignore_enter = FALSE;
2892 gdk_drawable_get_size (event->window, &width, &height);
2893 if (event->x >= 0 && event->x < width &&
2894 event->y >= 0 && event->y < height)
2896 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2899 send_event->crossing.window = g_object_ref (event->window);
2900 send_event->crossing.time = event->time;
2901 send_event->crossing.send_event = TRUE;
2902 send_event->crossing.x_root = event->x_root;
2903 send_event->crossing.y_root = event->y_root;
2904 send_event->crossing.x = event->x;
2905 send_event->crossing.y = event->y;
2907 /* We send the event to 'widget', the currently active menu,
2908 * instead of 'menu', the menu that the pointer is in. This
2909 * will ensure that the event will be ignored unless the
2910 * menuitem is a child of the active menu or some parent
2911 * menu of the active menu.
2913 result = gtk_widget_event (widget, send_event);
2914 gdk_event_free (send_event);
2924 get_double_arrows (GtkMenu *menu)
2926 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
2927 gboolean double_arrows;
2929 gtk_widget_style_get (GTK_WIDGET (menu),
2930 "double-arrows", &double_arrows,
2933 return double_arrows || (priv->initially_pushed_in &&
2934 menu->scroll_offset != 0);
2938 gtk_menu_scroll_by (GtkMenu *menu,
2943 gint view_width, view_height;
2944 gboolean double_arrows;
2945 gint scroll_arrow_height;
2947 widget = GTK_WIDGET (menu);
2948 offset = menu->scroll_offset + step;
2950 gtk_widget_style_get (GTK_WIDGET (menu),
2951 "scroll-arrow-vlength", &scroll_arrow_height,
2954 double_arrows = get_double_arrows (menu);
2956 /* If we scroll upward and the non-visible top part
2957 * is smaller than the scroll arrow it would be
2958 * pretty stupid to show the arrow and taking more
2959 * screen space than just scrolling to the top.
2962 if ((step < 0) && (offset < scroll_arrow_height))
2965 /* Don't scroll over the top if we weren't before: */
2966 if ((menu->scroll_offset >= 0) && (offset < 0))
2969 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2971 if (menu->scroll_offset == 0 &&
2972 view_height >= widget->requisition.height)
2975 /* Don't scroll past the bottom if we weren't before: */
2976 if (menu->scroll_offset > 0)
2977 view_height -= scroll_arrow_height;
2979 /* When both arrows are always shown, reduce
2980 * view height even more.
2983 view_height -= scroll_arrow_height;
2985 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2986 (offset + view_height > widget->requisition.height))
2987 offset = widget->requisition.height - view_height;
2989 if (offset != menu->scroll_offset)
2990 gtk_menu_scroll_to (menu, offset);
2994 gtk_menu_do_timeout_scroll (GtkMenu *menu,
2995 gboolean touchscreen_mode)
2997 gboolean upper_visible;
2998 gboolean lower_visible;
3000 upper_visible = menu->upper_arrow_visible;
3001 lower_visible = menu->lower_arrow_visible;
3003 gtk_menu_scroll_by (menu, menu->scroll_step);
3005 if (touchscreen_mode &&
3006 (upper_visible != menu->upper_arrow_visible ||
3007 lower_visible != menu->lower_arrow_visible))
3009 /* We are about to hide a scroll arrow while the mouse is pressed,
3010 * this would cause the uncovered menu item to be activated on button
3011 * release. Therefore we need to ignore button release here
3013 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3014 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3019 gtk_menu_scroll_timeout (gpointer data)
3022 GtkSettings *settings;
3023 gboolean touchscreen_mode;
3025 menu = GTK_MENU (data);
3027 settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3028 g_object_get (settings,
3029 "gtk-touchscreen-mode", &touchscreen_mode,
3032 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3038 gtk_menu_scroll_timeout_initial (gpointer data)
3041 GtkSettings *settings;
3043 gboolean touchscreen_mode;
3045 menu = GTK_MENU (data);
3047 settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3048 g_object_get (settings,
3049 "gtk-timeout-repeat", &timeout,
3050 "gtk-touchscreen-mode", &touchscreen_mode,
3053 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3055 gtk_menu_remove_scroll_timeout (menu);
3057 menu->timeout_id = gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3063 gtk_menu_start_scrolling (GtkMenu *menu)
3065 GtkSettings *settings;
3067 gboolean touchscreen_mode;
3069 settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3070 g_object_get (settings,
3071 "gtk-timeout-repeat", &timeout,
3072 "gtk-touchscreen-mode", &touchscreen_mode,
3075 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3077 menu->timeout_id = gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial,
3082 gtk_menu_scroll (GtkWidget *widget,
3083 GdkEventScroll *event)
3085 GtkMenu *menu = GTK_MENU (widget);
3087 switch (event->direction)
3089 case GDK_SCROLL_RIGHT:
3090 case GDK_SCROLL_DOWN:
3091 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3093 case GDK_SCROLL_LEFT:
3095 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3103 gtk_menu_handle_scrolling (GtkMenu *menu,
3109 GtkMenuShell *menu_shell;
3110 GtkMenuPrivate *priv;
3115 gboolean scroll_fast = FALSE;
3116 guint vertical_padding;
3119 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3120 gboolean touchscreen_mode;
3121 gint scroll_arrow_height;
3123 priv = gtk_menu_get_private (menu);
3125 menu_shell = GTK_MENU_SHELL (menu);
3127 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3129 g_object_get (G_OBJECT (settings),
3130 "gtk-touchscreen-mode", &touchscreen_mode,
3133 gtk_widget_style_get (GTK_WIDGET (menu),
3134 "vertical-padding", &vertical_padding,
3135 "scroll-arrow-vlength", &scroll_arrow_height,
3138 border = GTK_CONTAINER (menu)->border_width +
3139 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3141 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3145 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3147 /* upper arrow handling */
3152 rect.height = scroll_arrow_height + border;
3155 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3156 (x >= rect.x) && (x < rect.x + rect.width) &&
3157 (y >= rect.y) && (y < rect.y + rect.height))
3162 if (touchscreen_mode)
3163 menu->upper_arrow_prelight = in_arrow;
3165 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3167 if (menu->upper_arrow_visible && !menu->tearoff_active)
3169 if (touchscreen_mode)
3171 if (enter && menu->upper_arrow_prelight &&
3172 menu->timeout_id == 0)
3174 /* Deselect the active item so that
3175 * any submenus are popped down
3177 gtk_menu_shell_deselect (menu_shell);
3179 gtk_menu_remove_scroll_timeout (menu);
3180 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3184 /* Only do stuff on click. */
3185 gtk_menu_start_scrolling (menu);
3186 priv->upper_arrow_state = GTK_STATE_ACTIVE;
3189 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3194 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3197 gtk_menu_stop_scrolling (menu);
3200 else /* !touchscreen_mode */
3202 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3204 if (enter && in_arrow &&
3205 (!menu->upper_arrow_prelight ||
3206 menu->scroll_fast != scroll_fast))
3208 menu->upper_arrow_prelight = TRUE;
3209 menu->scroll_fast = scroll_fast;
3210 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3213 /* Deselect the active item so that
3214 * any submenus are popped down
3216 gtk_menu_shell_deselect (menu_shell);
3218 gtk_menu_remove_scroll_timeout (menu);
3219 menu->scroll_step = scroll_fast ?
3220 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3223 gdk_threads_add_timeout (scroll_fast ?
3224 MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
3225 gtk_menu_scroll_timeout, menu);
3227 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3229 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3232 gtk_menu_stop_scrolling (menu);
3237 priv->upper_arrow_state = menu->upper_arrow_prelight ?
3238 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
3241 /* lower arrow handling */
3244 rect.y = win_y + height - border - scroll_arrow_height;
3246 rect.height = scroll_arrow_height + border;
3249 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3250 (x >= rect.x) && (x < rect.x + rect.width) &&
3251 (y >= rect.y) && (y < rect.y + rect.height))
3256 if (touchscreen_mode)
3257 menu->lower_arrow_prelight = in_arrow;
3259 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3261 if (menu->lower_arrow_visible && !menu->tearoff_active)
3263 if (touchscreen_mode)
3265 if (enter && menu->lower_arrow_prelight &&
3266 menu->timeout_id == 0)
3268 /* Deselect the active item so that
3269 * any submenus are popped down
3271 gtk_menu_shell_deselect (menu_shell);
3273 gtk_menu_remove_scroll_timeout (menu);
3274 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3278 /* Only do stuff on click. */
3279 gtk_menu_start_scrolling (menu);
3280 priv->lower_arrow_state = GTK_STATE_ACTIVE;
3283 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3288 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3291 gtk_menu_stop_scrolling (menu);
3294 else /* !touchscreen_mode */
3296 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3298 if (enter && in_arrow &&
3299 (!menu->lower_arrow_prelight ||
3300 menu->scroll_fast != scroll_fast))
3302 menu->lower_arrow_prelight = TRUE;
3303 menu->scroll_fast = scroll_fast;
3304 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3307 /* Deselect the active item so that
3308 * any submenus are popped down
3310 gtk_menu_shell_deselect (menu_shell);
3312 gtk_menu_remove_scroll_timeout (menu);
3313 menu->scroll_step = scroll_fast ?
3314 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3317 gdk_threads_add_timeout (scroll_fast ?
3318 MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
3319 gtk_menu_scroll_timeout, menu);
3321 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3323 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3326 gtk_menu_stop_scrolling (menu);
3331 priv->lower_arrow_state = menu->lower_arrow_prelight ?
3332 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
3337 gtk_menu_enter_notify (GtkWidget *widget,
3338 GdkEventCrossing *event)
3340 GtkSettings *settings = gtk_widget_get_settings (widget);
3341 GtkWidget *menu_item;
3342 gboolean touchscreen_mode;
3344 g_object_get (G_OBJECT (settings),
3345 "gtk-touchscreen-mode", &touchscreen_mode,
3348 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3349 if (GTK_IS_MENU (widget))
3351 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3353 if (!menu_shell->ignore_enter)
3354 gtk_menu_handle_scrolling (GTK_MENU (widget),
3355 event->x_root, event->y_root, TRUE, TRUE);
3358 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3360 GtkWidget *menu = menu_item->parent;
3362 if (GTK_IS_MENU (menu))
3364 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3365 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3367 if (priv->seen_item_enter)
3369 /* This is the second enter we see for an item
3370 * on this menu. This means a release should always
3373 menu_shell->activate_time = 0;
3375 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3376 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3378 if (definitely_within_item (menu_item, event->x, event->y))
3380 /* This is an actual user-enter (ie. not a pop-under)
3381 * In this case, the user must either have entered
3382 * sufficiently far enough into the item, or he must move
3383 * far enough away from the enter point. (see
3384 * gtk_menu_motion_notify())
3386 menu_shell->activate_time = 0;
3390 priv->seen_item_enter = TRUE;
3394 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3395 * will not correspond to the event widget's parent. Check to see
3396 * if we are in the parent's navigation region.
3398 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3399 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3400 event->x_root, event->y_root))
3403 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3407 gtk_menu_leave_notify (GtkWidget *widget,
3408 GdkEventCrossing *event)
3410 GtkMenuShell *menu_shell;
3412 GtkMenuItem *menu_item;
3413 GtkWidget *event_widget;
3415 menu = GTK_MENU (widget);
3416 menu_shell = GTK_MENU_SHELL (widget);
3418 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3421 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3423 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3425 if (!GTK_IS_MENU_ITEM (event_widget))
3428 menu_item = GTK_MENU_ITEM (event_widget);
3430 /* Here we check to see if we're leaving an active menu item with a submenu,
3431 * in which case we enter submenu navigation mode.
3433 if (menu_shell->active_menu_item != NULL
3434 && menu_item->submenu != NULL
3435 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3437 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3439 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3442 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3444 /* We are leaving an active menu item with nonactive submenu.
3445 * Deselect it so we don't surprise the user with by popping
3446 * up a submenu _after_ he left the item.
3448 gtk_menu_shell_deselect (menu_shell);
3453 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3457 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3459 if (menu->navigation_region)
3461 gdk_region_destroy (menu->navigation_region);
3462 menu->navigation_region = NULL;
3464 if (menu->navigation_timeout)
3466 g_source_remove (menu->navigation_timeout);
3467 menu->navigation_timeout = 0;
3471 /* When the timeout is elapsed, the navigation region is destroyed
3472 * and the menuitem under the pointer (if any) is selected.
3475 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3477 GtkMenu *menu = user_data;
3478 GdkWindow *child_window;
3480 gtk_menu_stop_navigating_submenu (menu);
3482 if (GTK_WIDGET_REALIZED (menu))
3484 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3488 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3490 send_event->crossing.window = g_object_ref (child_window);
3491 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3492 send_event->crossing.send_event = TRUE;
3494 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3496 gdk_event_free (send_event);
3504 gtk_menu_navigating_submenu (GtkMenu *menu,
3508 if (menu->navigation_region)
3510 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3514 gtk_menu_stop_navigating_submenu (menu);
3521 #undef DRAW_STAY_UP_TRIANGLE
3523 #ifdef DRAW_STAY_UP_TRIANGLE
3526 draw_stay_up_triangle (GdkWindow *window,
3529 /* Draw ugly color all over the stay-up triangle */
3530 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3531 GdkGCValues gc_values;
3533 GdkRectangle clipbox;
3535 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3536 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3537 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3538 gdk_gc_set_clip_region (ugly_gc, region);
3540 gdk_region_get_clipbox (region, &clipbox);
3542 gdk_draw_rectangle (window,
3545 clipbox.x, clipbox.y,
3546 clipbox.width, clipbox.height);
3548 g_object_unref (ugly_gc);
3553 flip_region (GdkRegion *region,
3558 GdkRectangle *rectangles;
3559 GdkRectangle clipbox;
3560 GdkRegion *new_region;
3563 new_region = gdk_region_new ();
3565 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3566 gdk_region_get_clipbox (region, &clipbox);
3568 for (i = 0; i < n_rectangles; ++i)
3570 GdkRectangle rect = rectangles[i];
3573 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3576 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3578 gdk_region_union_with_rect (new_region, &rect);
3581 g_free (rectangles);
3587 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3588 GtkMenuItem *menu_item,
3589 GdkEventCrossing *event)
3591 gint submenu_left = 0;
3592 gint submenu_right = 0;
3593 gint submenu_top = 0;
3594 gint submenu_bottom = 0;
3598 GtkWidget *event_widget;
3600 g_return_if_fail (menu_item->submenu != NULL);
3601 g_return_if_fail (event != NULL);
3603 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3605 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3606 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3608 submenu_right = submenu_left + width;
3609 submenu_bottom = submenu_top + height;
3611 gdk_drawable_get_size (event_widget->window, &width, &height);
3613 if (event->x >= 0 && event->x < width)
3616 gboolean flip_y = FALSE;
3617 gboolean flip_x = FALSE;
3619 gtk_menu_stop_navigating_submenu (menu);
3621 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3624 point[0].x = event->x_root;
3625 point[1].x = submenu_left;
3630 point[0].x = event->x_root + 1;
3631 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3639 point[0].y = event->y_root + 1;
3640 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3642 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3650 point[0].y = event->y_root;
3651 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3653 if (point[0].y >= submenu_bottom)
3657 point[2].x = point[1].x;
3658 point[2].y = point[0].y;
3660 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3662 if (flip_x || flip_y)
3664 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3665 gdk_region_destroy (menu->navigation_region);
3666 menu->navigation_region = new_region;
3669 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3670 "gtk-menu-popdown-delay", &popdown_delay,
3673 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
3674 gtk_menu_stop_navigating_submenu_cb, menu);
3676 #ifdef DRAW_STAY_UP_TRIANGLE
3677 draw_stay_up_triangle (gdk_get_default_root_window(),
3678 menu->navigation_region);
3684 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3688 g_return_if_fail (GTK_IS_MENU (menu_shell));
3690 parent = menu_shell->parent_menu_shell;
3692 menu_shell->activate_time = 0;
3693 gtk_menu_popdown (GTK_MENU (menu_shell));
3696 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3700 gtk_menu_position (GtkMenu *menu)
3703 GtkRequisition requisition;
3704 GtkMenuPrivate *private;
3709 GdkScreen *pointer_screen;
3710 GdkRectangle monitor;
3711 gint scroll_arrow_height;
3713 g_return_if_fail (GTK_IS_MENU (menu));
3715 widget = GTK_WIDGET (menu);
3717 screen = gtk_widget_get_screen (widget);
3718 gdk_display_get_pointer (gdk_screen_get_display (screen),
3719 &pointer_screen, &x, &y, NULL);
3721 gtk_widget_style_get (GTK_WIDGET (menu),
3722 "scroll-arrow-vlength", &scroll_arrow_height,
3725 /* We need the requisition to figure out the right place to
3726 * popup the menu. In fact, we always need to ask here, since
3727 * if a size_request was queued while we weren't popped up,
3728 * the requisition won't have been recomputed yet.
3730 gtk_widget_size_request (widget, &requisition);
3732 if (pointer_screen != screen)
3734 /* Pointer is on a different screen; roughly center the
3735 * menu on the screen. If someone was using multiscreen
3736 * + Xinerama together they'd probably want something
3737 * fancier; but that is likely to be vanishingly rare.
3739 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3740 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3743 private = gtk_menu_get_private (menu);
3744 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3746 private->initially_pushed_in = FALSE;
3748 /* Set the type hint here to allow custom position functions to set a different hint */
3749 if (!GTK_WIDGET_VISIBLE (menu->toplevel))
3750 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
3752 if (menu->position_func)
3754 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
3755 menu->position_func_data);
3756 if (private->monitor_num < 0)
3757 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3759 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3763 gint space_left, space_right, space_above, space_below;
3766 gint xthickness = widget->style->xthickness;
3767 gint ythickness = widget->style->ythickness;
3768 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3770 /* The placement of popup menus horizontally works like this (with
3771 * RTL in parentheses)
3773 * - If there is enough room to the right (left) of the mouse cursor,
3774 * position the menu there.
3776 * - Otherwise, if if there is enough room to the left (right) of the
3777 * mouse cursor, position the menu there.
3779 * - Otherwise if the menu is smaller than the monitor, position it
3780 * on the side of the mouse cursor that has the most space available
3782 * - Otherwise (if there is simply not enough room for the menu on the
3783 * monitor), position it as far left (right) as possible.
3785 * Positioning in the vertical direction is similar: first try below
3786 * mouse cursor, then above.
3788 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3790 space_left = x - monitor.x;
3791 space_right = monitor.x + monitor.width - x - 1;
3792 space_above = y - monitor.y;
3793 space_below = monitor.y + monitor.height - y - 1;
3795 /* position horizontally */
3797 /* the amount of space we need to position the menu. Note the
3798 * menu is offset "xthickness" pixels
3800 needed_width = requisition.width - xthickness;
3802 if (needed_width <= space_left ||
3803 needed_width <= space_right)
3805 if ((rtl && needed_width <= space_left) ||
3806 (!rtl && needed_width > space_right))
3809 x = x + xthickness - requisition.width + 1;
3813 /* position right */
3817 /* x is clamped on-screen further down */
3819 else if (requisition.width <= monitor.width)
3821 /* the menu is too big to fit on either side of the mouse
3822 * cursor, but smaller than the monitor. Position it on
3823 * the side that has the most space
3825 if (space_left > space_right)
3833 x = monitor.x + monitor.width - requisition.width;
3836 else /* menu is simply too big for the monitor */
3841 x = monitor.x + monitor.width - requisition.width;
3850 /* Position vertically. The algorithm is the same as above, but
3851 * simpler because we don't have to take RTL into account.
3853 needed_height = requisition.height - ythickness;
3855 if (needed_height <= space_above ||
3856 needed_height <= space_below)
3858 if (needed_height <= space_below)
3861 y = y + ythickness - requisition.height + 1;
3863 y = CLAMP (y, monitor.y,
3864 monitor.y + monitor.height - requisition.height);
3866 else if (needed_height > space_below && needed_height > space_above)
3868 if (space_below >= space_above)
3869 y = monitor.y + monitor.height - requisition.height;
3881 if (private->initially_pushed_in)
3883 menu_height = GTK_WIDGET (menu)->requisition.height;
3885 if (y + menu_height > monitor.y + monitor.height)
3887 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3888 y = (monitor.y + monitor.height) - menu_height;
3893 scroll_offset += monitor.y - y;
3898 /* FIXME: should this be done in the various position_funcs ? */
3899 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3901 if (GTK_MENU_SHELL (menu)->active)
3903 private->have_position = TRUE;
3908 if (y + requisition.height > monitor.y + monitor.height)
3909 requisition.height = (monitor.y + monitor.height) - y;
3913 scroll_offset += monitor.y - y;
3914 requisition.height -= monitor.y - y;
3918 if (scroll_offset > 0)
3919 scroll_offset += scroll_arrow_height;
3921 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3924 if (!GTK_MENU_SHELL (menu)->active)
3926 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3927 requisition.width, requisition.height);
3930 menu->scroll_offset = scroll_offset;
3934 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3936 if (menu->timeout_id)
3938 g_source_remove (menu->timeout_id);
3939 menu->timeout_id = 0;
3944 gtk_menu_stop_scrolling (GtkMenu *menu)
3946 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3947 gboolean touchscreen_mode;
3949 gtk_menu_remove_scroll_timeout (menu);
3951 g_object_get (G_OBJECT (settings),
3952 "gtk-touchscreen-mode", &touchscreen_mode,
3955 if (!touchscreen_mode)
3957 menu->upper_arrow_prelight = FALSE;
3958 menu->lower_arrow_prelight = FALSE;
3963 gtk_menu_scroll_to (GtkMenu *menu,
3968 gint view_width, view_height;
3971 guint vertical_padding;
3972 guint horizontal_padding;
3973 gboolean double_arrows;
3974 gint scroll_arrow_height;
3976 widget = GTK_WIDGET (menu);
3978 if (menu->tearoff_active &&
3979 menu->tearoff_adjustment &&
3980 (menu->tearoff_adjustment->value != offset))
3982 menu->tearoff_adjustment->value =
3984 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3985 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3988 /* Move/resize the viewport according to arrows: */
3989 view_width = widget->allocation.width;
3990 view_height = widget->allocation.height;
3992 gtk_widget_style_get (GTK_WIDGET (menu),
3993 "vertical-padding", &vertical_padding,
3994 "horizontal-padding", &horizontal_padding,
3995 "scroll-arrow-vlength", &scroll_arrow_height,
3998 double_arrows = get_double_arrows (menu);
4000 border_width = GTK_CONTAINER (menu)->border_width;
4001 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4002 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4003 menu_height = widget->requisition.height -
4004 (border_width + widget->style->ythickness + vertical_padding) * 2;
4006 x = border_width + widget->style->xthickness + horizontal_padding;
4007 y = border_width + widget->style->ythickness + vertical_padding;
4009 if (double_arrows && !menu->tearoff_active)
4011 if (view_height < menu_height ||
4012 (offset > 0 && menu->scroll_offset > 0) ||
4013 (offset < 0 && menu->scroll_offset < 0))
4015 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4016 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4017 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4019 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4020 gtk_widget_queue_draw (GTK_WIDGET (menu));
4022 view_height -= 2 * scroll_arrow_height;
4023 y += scroll_arrow_height;
4025 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4028 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4030 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4031 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4033 if (offset >= menu_height - view_height)
4034 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4036 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4037 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4039 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4040 (priv->lower_arrow_state != lower_arrow_previous_state))
4041 gtk_widget_queue_draw (GTK_WIDGET (menu));
4043 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4044 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4046 /* At the upper border, possibly remove timeout */
4047 if (menu->scroll_step < 0)
4049 gtk_menu_stop_scrolling (menu);
4050 gtk_widget_queue_draw (GTK_WIDGET (menu));
4054 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4055 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4057 /* At the lower border, possibly remove timeout */
4058 if (menu->scroll_step > 0)
4060 gtk_menu_stop_scrolling (menu);
4061 gtk_widget_queue_draw (GTK_WIDGET (menu));
4065 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4069 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4070 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4072 gtk_menu_stop_scrolling (menu);
4073 gtk_widget_queue_draw (GTK_WIDGET (menu));
4076 else if (!menu->tearoff_active)
4078 gboolean last_visible;
4080 last_visible = menu->upper_arrow_visible;
4081 menu->upper_arrow_visible = offset > 0;
4083 if (menu->upper_arrow_visible)
4084 view_height -= scroll_arrow_height;
4086 if ((last_visible != menu->upper_arrow_visible) &&
4087 !menu->upper_arrow_visible)
4089 menu->upper_arrow_prelight = FALSE;
4091 /* If we hid the upper arrow, possibly remove timeout */
4092 if (menu->scroll_step < 0)
4094 gtk_menu_stop_scrolling (menu);
4095 gtk_widget_queue_draw (GTK_WIDGET (menu));
4099 last_visible = menu->lower_arrow_visible;
4100 menu->lower_arrow_visible = offset < menu_height - view_height;
4102 if (menu->lower_arrow_visible)
4103 view_height -= scroll_arrow_height;
4105 if ((last_visible != menu->lower_arrow_visible) &&
4106 !menu->lower_arrow_visible)
4108 menu->lower_arrow_prelight = FALSE;
4110 /* If we hid the lower arrow, possibly remove timeout */
4111 if (menu->scroll_step > 0)
4113 gtk_menu_stop_scrolling (menu);
4114 gtk_widget_queue_draw (GTK_WIDGET (menu));
4118 if (menu->upper_arrow_visible)
4119 y += scroll_arrow_height;
4122 /* Scroll the menu: */
4123 if (GTK_WIDGET_REALIZED (menu))
4124 gdk_window_move (menu->bin_window, 0, -offset);
4126 if (GTK_WIDGET_REALIZED (menu))
4127 gdk_window_move_resize (menu->view_window,
4133 menu->scroll_offset = offset;
4137 compute_child_offset (GtkMenu *menu,
4138 GtkWidget *menu_item,
4141 gboolean *is_last_child)
4143 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4144 gint item_top_attach;
4145 gint item_bottom_attach;
4146 gint child_offset = 0;
4149 get_effective_child_attach (menu_item, NULL, NULL,
4150 &item_top_attach, &item_bottom_attach);
4152 /* there is a possibility that we get called before _size_request, so
4153 * check the height table for safety.
4155 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4158 /* when we have a row with only invisible children, it's height will
4159 * be zero, so there's no need to check WIDGET_VISIBLE here
4161 for (i = 0; i < item_top_attach; i++)
4162 child_offset += priv->heights[i];
4165 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4167 *offset = child_offset;
4169 *height = priv->heights[item_top_attach];
4175 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4176 GtkWidget *menu_item)
4179 gint child_offset, child_height;
4183 gboolean last_child = 0;
4185 menu = GTK_MENU (menu_shell);
4187 /* We need to check if the selected item fully visible.
4188 * If not we need to scroll the menu so that it becomes fully
4192 if (compute_child_offset (menu, menu_item,
4193 &child_offset, &child_height, &last_child))
4195 guint vertical_padding;
4196 gboolean double_arrows;
4197 gint scroll_arrow_height;
4199 y = menu->scroll_offset;
4200 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4202 gtk_widget_style_get (GTK_WIDGET (menu),
4203 "vertical-padding", &vertical_padding,
4204 "scroll-arrow-vlength", &scroll_arrow_height,
4207 double_arrows = get_double_arrows (menu);
4209 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4211 if (child_offset < y)
4213 /* Ignore the enter event we might get if the pointer is on the menu
4215 menu_shell->ignore_enter = TRUE;
4216 gtk_menu_scroll_to (menu, child_offset);
4221 if (menu->upper_arrow_visible && !menu->tearoff_active)
4222 arrow_height += scroll_arrow_height;
4223 if (menu->lower_arrow_visible && !menu->tearoff_active)
4224 arrow_height += scroll_arrow_height;
4226 if (child_offset + child_height > y + height - arrow_height)
4229 if ((!last_child && !menu->tearoff_active) || double_arrows)
4230 arrow_height += scroll_arrow_height;
4232 y = child_offset + child_height - height + arrow_height;
4233 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4235 /* Need upper arrow */
4236 arrow_height += scroll_arrow_height;
4237 y = child_offset + child_height - height + arrow_height;
4239 /* Ignore the enter event we might get if the pointer is on the menu
4241 menu_shell->ignore_enter = TRUE;
4242 gtk_menu_scroll_to (menu, y);
4250 gtk_menu_select_item (GtkMenuShell *menu_shell,
4251 GtkWidget *menu_item)
4253 GtkMenu *menu = GTK_MENU (menu_shell);
4255 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4256 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4258 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4262 /* Reparent the menu, taking care of the refcounting
4264 * If unrealize is true we force a unrealize while reparenting the parent.
4265 * This can help eliminate flicker in some cases.
4267 * What happens is that when the menu is unrealized and then re-realized,
4268 * the allocations are as follows:
4270 * parent - 1x1 at (0,0)
4271 * child1 - 100x20 at (0,0)
4272 * child2 - 100x20 at (0,20)
4273 * child3 - 100x20 at (0,40)
4275 * That is, the parent is small but the children are full sized. Then,
4276 * when the queued_resize gets processed, the parent gets resized to
4279 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4280 * contains the following logic:
4282 * - if a move or resize operation on a window would change the clip
4283 * region on the children, then before the window is resized
4284 * the background for children is temporarily set to None, the
4285 * move/resize done, and the background for the children restored.
4287 * So, at the point where the parent is resized to final size, the
4288 * background for the children is temporarily None, and thus they
4289 * are not cleared to the background color and the previous background
4290 * (the image of the menu) is left in place.
4293 gtk_menu_reparent (GtkMenu *menu,
4294 GtkWidget *new_parent,
4297 GtkObject *object = GTK_OBJECT (menu);
4298 GtkWidget *widget = GTK_WIDGET (menu);
4299 gboolean was_floating = g_object_is_floating (object);
4301 g_object_ref_sink (object);
4305 g_object_ref (object);
4306 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4307 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4308 g_object_unref (object);
4311 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4314 g_object_force_floating (G_OBJECT (object));
4316 g_object_unref (object);
4320 gtk_menu_show_all (GtkWidget *widget)
4322 /* Show children, but not self. */
4323 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4328 gtk_menu_hide_all (GtkWidget *widget)
4330 /* Hide children, but not self. */
4331 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4335 * gtk_menu_set_screen:
4336 * @menu: a #GtkMenu.
4337 * @screen: a #GdkScreen, or %NULL if the screen should be
4338 * determined by the widget the menu is attached to.
4340 * Sets the #GdkScreen on which the menu will be displayed.
4345 gtk_menu_set_screen (GtkMenu *menu,
4348 g_return_if_fail (GTK_IS_MENU (menu));
4349 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4351 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4355 menu_change_screen (menu, screen);
4359 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4361 attach_widget_screen_changed (attach_widget, NULL, menu);
4367 * @menu: a #GtkMenu.
4368 * @child: a #GtkMenuItem.
4369 * @left_attach: The column number to attach the left side of the item to.
4370 * @right_attach: The column number to attach the right side of the item to.
4371 * @top_attach: The row number to attach the top of the item to.
4372 * @bottom_attach: The row number to attach the bottom of the item to.
4374 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4375 * an item will occupy is specified by @left_attach, @right_attach,
4376 * @top_attach and @bottom_attach. These each represent the leftmost,
4377 * rightmost, uppermost and lower column and row numbers of the table.
4378 * (Columns and rows are indexed from zero).
4380 * Note that this function is not related to gtk_menu_detach().
4385 gtk_menu_attach (GtkMenu *menu,
4390 guint bottom_attach)
4392 GtkMenuShell *menu_shell;
4394 g_return_if_fail (GTK_IS_MENU (menu));
4395 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4396 g_return_if_fail (child->parent == NULL ||
4397 child->parent == GTK_WIDGET (menu));
4398 g_return_if_fail (left_attach < right_attach);
4399 g_return_if_fail (top_attach < bottom_attach);
4401 menu_shell = GTK_MENU_SHELL (menu);
4405 AttachInfo *ai = get_attach_info (child);
4407 ai->left_attach = left_attach;
4408 ai->right_attach = right_attach;
4409 ai->top_attach = top_attach;
4410 ai->bottom_attach = bottom_attach;
4412 menu_shell->children = g_list_append (menu_shell->children, child);
4414 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4416 menu_queue_resize (menu);
4420 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4421 "left-attach", left_attach,
4422 "right-attach", right_attach,
4423 "top-attach", top_attach,
4424 "bottom-attach", bottom_attach,
4430 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4434 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4435 "gtk-menu-popup-delay", &popup_delay,
4442 find_child_containing (GtkMenuShell *menu_shell,
4450 /* find a child which includes the area given by
4451 * left, right, top, bottom.
4454 for (list = menu_shell->children; list; list = list->next)
4458 if (!_gtk_menu_item_is_selectable (list->data))
4461 get_effective_child_attach (list->data, &l, &r, &t, &b);
4463 if (l <= left && right <= r
4464 && t <= top && bottom <= b)
4465 return GTK_WIDGET (list->data);
4472 gtk_menu_move_current (GtkMenuShell *menu_shell,
4473 GtkMenuDirectionType direction)
4475 GtkMenu *menu = GTK_MENU (menu_shell);
4478 GtkWidget *match = NULL;
4480 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4484 case GTK_MENU_DIR_CHILD:
4485 direction = GTK_MENU_DIR_PARENT;
4487 case GTK_MENU_DIR_PARENT:
4488 direction = GTK_MENU_DIR_CHILD;
4494 /* use special table menu key bindings */
4495 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4497 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4499 if (direction == GTK_MENU_DIR_NEXT)
4501 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4503 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4511 for (i = 0; i < t; i++)
4513 match = find_child_containing (menu_shell,
4514 l, l + 1, i, i + 1);
4520 else if (direction == GTK_MENU_DIR_PREV)
4522 for (i = t; i > 0; i--)
4524 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4532 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4534 match = find_child_containing (menu_shell,
4535 l, l + 1, i - 1, i);
4541 else if (direction == GTK_MENU_DIR_PARENT)
4543 /* we go one left if possible */
4545 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4549 GtkWidget *parent = menu_shell->parent_menu_shell;
4552 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4553 match = menu_shell->active_menu_item;
4556 else if (direction == GTK_MENU_DIR_CHILD)
4558 /* we go one right if possible */
4559 if (r < gtk_menu_get_n_columns (menu))
4560 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4564 GtkWidget *parent = menu_shell->parent_menu_shell;
4566 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4568 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4569 match = menu_shell->active_menu_item;
4575 gtk_menu_shell_select_item (menu_shell, match);
4580 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
4584 get_visible_size (GtkMenu *menu)
4586 GtkWidget *widget = GTK_WIDGET (menu);
4587 GtkContainer *container = GTK_CONTAINER (menu);
4588 gint scroll_arrow_height;
4590 gint menu_height = (widget->allocation.height
4591 - 2 * (container->border_width
4592 + widget->style->ythickness));
4594 gtk_widget_style_get (GTK_WIDGET (menu),
4595 "scroll-arrow-vlength", &scroll_arrow_height,
4598 if (menu->upper_arrow_visible && !menu->tearoff_active)
4599 menu_height -= scroll_arrow_height;
4600 if (menu->lower_arrow_visible && !menu->tearoff_active)
4601 menu_height -= scroll_arrow_height;
4606 /* Find the sensitive on-screen child containing @y, or if none,
4607 * the nearest selectable onscreen child. (%NULL if none)
4610 child_at (GtkMenu *menu,
4613 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4614 GtkWidget *child = NULL;
4615 gint child_offset = 0;
4618 gint lower, upper; /* Onscreen bounds */
4620 menu_height = get_visible_size (menu);
4621 lower = menu->scroll_offset;
4622 upper = menu->scroll_offset + menu_height;
4624 for (children = menu_shell->children; children; children = children->next)
4626 if (GTK_WIDGET_VISIBLE (children->data))
4628 GtkRequisition child_requisition;
4630 gtk_widget_size_request (children->data, &child_requisition);
4632 if (_gtk_menu_item_is_selectable (children->data) &&
4633 child_offset >= lower &&
4634 child_offset + child_requisition.height <= upper)
4636 child = children->data;
4638 if (child_offset + child_requisition.height > y &&
4639 !GTK_IS_TEAROFF_MENU_ITEM (child))
4643 child_offset += child_requisition.height;
4651 get_menu_height (GtkMenu *menu)
4654 GtkWidget *widget = GTK_WIDGET (menu);
4655 gint scroll_arrow_height;
4657 gtk_widget_style_get (GTK_WIDGET (menu),
4658 "scroll-arrow-vlength", &scroll_arrow_height,
4661 height = widget->requisition.height;
4662 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4664 if (menu->upper_arrow_visible && !menu->tearoff_active)
4665 height -= scroll_arrow_height;
4667 if (menu->lower_arrow_visible && !menu->tearoff_active)
4668 height -= scroll_arrow_height;
4674 gtk_menu_real_move_scroll (GtkMenu *menu,
4677 gint page_size = get_visible_size (menu);
4678 gint end_position = get_menu_height (menu);
4679 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4680 gint scroll_arrow_height;
4682 gtk_widget_style_get (GTK_WIDGET (menu),
4683 "scroll-arrow-vlength", &scroll_arrow_height,
4688 case GTK_SCROLL_PAGE_UP:
4689 case GTK_SCROLL_PAGE_DOWN:
4693 gint child_offset = 0;
4694 gboolean old_upper_arrow_visible;
4697 if (type == GTK_SCROLL_PAGE_UP)
4702 if (menu_shell->active_menu_item)
4706 compute_child_offset (menu, menu_shell->active_menu_item,
4707 &child_offset, &child_height, NULL);
4708 child_offset += child_height / 2;
4711 menu_shell->ignore_enter = TRUE;
4712 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4713 old_offset = menu->scroll_offset;
4715 new_offset = menu->scroll_offset + step;
4716 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4718 gtk_menu_scroll_to (menu, new_offset);
4720 if (menu_shell->active_menu_item)
4722 GtkWidget *new_child;
4723 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4725 if (menu->scroll_offset != old_offset)
4726 step = menu->scroll_offset - old_offset;
4728 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * scroll_arrow_height;
4730 new_child = child_at (menu, child_offset + step);
4732 gtk_menu_shell_select_item (menu_shell, new_child);
4736 case GTK_SCROLL_START:
4737 /* Ignore the enter event we might get if the pointer is on the menu
4739 menu_shell->ignore_enter = TRUE;
4740 gtk_menu_scroll_to (menu, 0);
4741 gtk_menu_shell_select_first (menu_shell, TRUE);
4743 case GTK_SCROLL_END:
4744 /* Ignore the enter event we might get if the pointer is on the menu
4746 menu_shell->ignore_enter = TRUE;
4747 gtk_menu_scroll_to (menu, end_position - page_size);
4748 _gtk_menu_shell_select_last (menu_shell, TRUE);
4757 * gtk_menu_set_monitor:
4759 * @monitor_num: the number of the monitor on which the menu should
4762 * Informs GTK+ on which monitor a menu should be popped up.
4763 * See gdk_screen_get_monitor_geometry().
4765 * This function should be called from a #GtkMenuPositionFunc if the
4766 * menu should not appear on the same monitor as the pointer. This
4767 * information can't be reliably inferred from the coordinates returned
4768 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4769 * may extend beyond the monitor boundaries or even the screen boundaries.
4774 gtk_menu_set_monitor (GtkMenu *menu,
4777 GtkMenuPrivate *priv;
4778 g_return_if_fail (GTK_IS_MENU (menu));
4780 priv = gtk_menu_get_private (menu);
4782 priv->monitor_num = monitor_num;
4786 * gtk_menu_get_for_attach_widget:
4787 * @widget: a #GtkWidget
4789 * Returns a list of the menus which are attached to this widget.
4790 * This list is owned by GTK+ and must not be modified.
4792 * Return value: the list of menus attached to his widget.
4797 gtk_menu_get_for_attach_widget (GtkWidget *widget)
4801 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4803 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
4808 gtk_menu_grab_notify (GtkWidget *widget,
4809 gboolean was_grabbed)
4811 GtkWidget *toplevel;
4812 GtkWindowGroup *group;
4815 toplevel = gtk_widget_get_toplevel (widget);
4816 group = gtk_window_get_group (GTK_WINDOW (toplevel));
4817 grab = _gtk_window_group_get_current_grab (group);
4821 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
4822 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
4826 #define __GTK_MENU_C__
4827 #include "gtkaliasdef.c"