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"
47 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
48 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
50 #define DEFAULT_POPUP_DELAY 200
51 #define DEFAULT_POPDOWN_DELAY 1000
53 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
54 * extends below the submenu
57 #define MENU_SCROLL_STEP1 8
58 #define MENU_SCROLL_STEP2 15
59 #define MENU_SCROLL_ARROW_HEIGHT 16
60 #define MENU_SCROLL_FAST_ZONE 8
61 #define MENU_SCROLL_TIMEOUT1 50
62 #define MENU_SCROLL_TIMEOUT2 50
64 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
66 typedef struct _GtkMenuAttachData GtkMenuAttachData;
67 typedef struct _GtkMenuPrivate GtkMenuPrivate;
69 struct _GtkMenuAttachData
71 GtkWidget *attach_widget;
72 GtkMenuDetachFunc detacher;
75 struct _GtkMenuPrivate
77 gboolean have_position;
81 /* info used for the table */
113 CHILD_PROP_LEFT_ATTACH,
114 CHILD_PROP_RIGHT_ATTACH,
115 CHILD_PROP_TOP_ATTACH,
116 CHILD_PROP_BOTTOM_ATTACH
119 static void gtk_menu_class_init (GtkMenuClass *klass);
120 static void gtk_menu_init (GtkMenu *menu);
121 static void gtk_menu_set_property (GObject *object,
125 static void gtk_menu_get_property (GObject *object,
129 static void gtk_menu_set_child_property(GtkContainer *container,
134 static void gtk_menu_get_child_property(GtkContainer *container,
139 static void gtk_menu_destroy (GtkObject *object);
140 static void gtk_menu_finalize (GObject *object);
141 static void gtk_menu_realize (GtkWidget *widget);
142 static void gtk_menu_unrealize (GtkWidget *widget);
143 static void gtk_menu_size_request (GtkWidget *widget,
144 GtkRequisition *requisition);
145 static void gtk_menu_size_allocate (GtkWidget *widget,
146 GtkAllocation *allocation);
147 static void gtk_menu_paint (GtkWidget *widget,
148 GdkEventExpose *expose);
149 static void gtk_menu_show (GtkWidget *widget);
150 static gboolean gtk_menu_expose (GtkWidget *widget,
151 GdkEventExpose *event);
152 static gboolean gtk_menu_key_press (GtkWidget *widget,
154 static gboolean gtk_menu_button_press (GtkWidget *widget,
155 GdkEventButton *event);
156 static gboolean gtk_menu_button_release (GtkWidget *widget,
157 GdkEventButton *event);
158 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
159 GdkEventMotion *event);
160 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
161 GdkEventCrossing *event);
162 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
163 GdkEventCrossing *event);
164 static void gtk_menu_scroll_to (GtkMenu *menu,
167 static void gtk_menu_stop_scrolling (GtkMenu *menu);
168 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
169 static gboolean gtk_menu_scroll_timeout (gpointer data);
171 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
172 GtkWidget *menu_item);
173 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
174 GtkWidget *menu_item);
175 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
178 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
180 static void gtk_menu_handle_scrolling (GtkMenu *menu,
182 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
184 static void gtk_menu_style_set (GtkWidget *widget,
185 GtkStyle *previous_style);
186 static gboolean gtk_menu_focus (GtkWidget *widget,
187 GtkDirectionType direction);
188 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
189 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
190 GtkMenuDirectionType direction);
191 static void gtk_menu_real_move_scroll (GtkMenu *menu,
194 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
195 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
196 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
199 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
200 GtkMenuItem *menu_item,
201 GdkEventCrossing *event);
203 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
204 static void gtk_menu_show_all (GtkWidget *widget);
205 static void gtk_menu_hide_all (GtkWidget *widget);
206 static void gtk_menu_position (GtkMenu *menu);
207 static void gtk_menu_reparent (GtkMenu *menu,
208 GtkWidget *new_parent,
210 static void gtk_menu_remove (GtkContainer *menu,
213 static void gtk_menu_update_title (GtkMenu *menu);
215 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
216 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
218 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
219 gboolean group_changed);
221 static GtkMenuShellClass *parent_class = NULL;
222 static const gchar *attach_data_key = "gtk-menu-attach-data";
224 static guint menu_signals[LAST_SIGNAL] = { 0 };
227 gtk_menu_free_private (gpointer data)
229 GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
231 g_free (priv->heights);
237 gtk_menu_get_private (GtkMenu *menu)
239 GtkMenuPrivate *private;
240 static GQuark private_quark = 0;
243 private_quark = g_quark_from_static_string ("gtk-menu-private");
245 private = g_object_get_qdata (G_OBJECT (menu), private_quark);
249 private = g_new0 (GtkMenuPrivate, 1);
250 private->have_position = FALSE;
252 g_object_set_qdata_full (G_OBJECT (menu), private_quark,
253 private, gtk_menu_free_private);
260 gtk_menu_get_type (void)
262 static GType menu_type = 0;
266 static const GTypeInfo menu_info =
268 sizeof (GtkMenuClass),
269 NULL, /* base_init */
270 NULL, /* base_finalize */
271 (GClassInitFunc) gtk_menu_class_init,
272 NULL, /* class_finalize */
273 NULL, /* class_data */
276 (GInstanceInitFunc) gtk_menu_init,
279 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
287 gtk_menu_class_init (GtkMenuClass *class)
289 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
290 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
291 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
292 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
293 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
294 GtkBindingSet *binding_set;
296 parent_class = g_type_class_peek_parent (class);
298 gobject_class->finalize = gtk_menu_finalize;
299 gobject_class->set_property = gtk_menu_set_property;
300 gobject_class->get_property = gtk_menu_get_property;
302 object_class->destroy = gtk_menu_destroy;
304 widget_class->realize = gtk_menu_realize;
305 widget_class->unrealize = gtk_menu_unrealize;
306 widget_class->size_request = gtk_menu_size_request;
307 widget_class->size_allocate = gtk_menu_size_allocate;
308 widget_class->show = gtk_menu_show;
309 widget_class->expose_event = gtk_menu_expose;
310 widget_class->key_press_event = gtk_menu_key_press;
311 widget_class->button_press_event = gtk_menu_button_press;
312 widget_class->button_release_event = gtk_menu_button_release;
313 widget_class->motion_notify_event = gtk_menu_motion_notify;
314 widget_class->show_all = gtk_menu_show_all;
315 widget_class->hide_all = gtk_menu_hide_all;
316 widget_class->enter_notify_event = gtk_menu_enter_notify;
317 widget_class->leave_notify_event = gtk_menu_leave_notify;
318 widget_class->motion_notify_event = gtk_menu_motion_notify;
319 widget_class->style_set = gtk_menu_style_set;
320 widget_class->focus = gtk_menu_focus;
322 container_class->remove = gtk_menu_remove;
323 container_class->get_child_property = gtk_menu_get_child_property;
324 container_class->set_child_property = gtk_menu_set_child_property;
326 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
327 menu_shell_class->deactivate = gtk_menu_deactivate;
328 menu_shell_class->select_item = gtk_menu_select_item;
329 menu_shell_class->insert = gtk_menu_real_insert;
330 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
331 menu_shell_class->move_current = gtk_menu_move_current;
333 menu_signals[MOVE_SCROLL] =
334 _gtk_binding_signal_new ("move_scroll",
335 G_OBJECT_CLASS_TYPE (object_class),
336 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
337 G_CALLBACK (gtk_menu_real_move_scroll),
339 _gtk_marshal_VOID__ENUM,
341 GTK_TYPE_SCROLL_TYPE);
343 g_object_class_install_property (gobject_class,
345 g_param_spec_string ("tearoff-title",
347 _("A title that may be displayed by the window manager when this menu is torn-off"),
349 G_PARAM_READABLE | G_PARAM_WRITABLE));
351 gtk_widget_class_install_style_property (widget_class,
352 g_param_spec_int ("vertical-padding",
353 _("Vertical Padding"),
354 _("Extra space at the top and bottom of the menu"),
360 gtk_widget_class_install_style_property (widget_class,
361 g_param_spec_int ("vertical-offset",
362 _("Vertical Offset"),
363 _("When the menu is a submenu, position it this number of pixels offset vertically"),
369 gtk_widget_class_install_style_property (widget_class,
370 g_param_spec_int ("horizontal-offset",
371 _("Horizontal Offset"),
372 _("When the menu is a submenu, position it this number of pixels offset horizontally"),
379 gtk_container_class_install_child_property (container_class,
380 CHILD_PROP_LEFT_ATTACH,
381 g_param_spec_uint ("left_attach",
383 _("The column number to attach the left side of the child to"),
387 gtk_container_class_install_child_property (container_class,
388 CHILD_PROP_RIGHT_ATTACH,
389 g_param_spec_uint ("right_attach",
391 _("The column number to attach the right side of the child to"),
395 gtk_container_class_install_child_property (container_class,
396 CHILD_PROP_TOP_ATTACH,
397 g_param_spec_uint ("top_attach",
399 _("The row number to attach the top of the child to"),
403 gtk_container_class_install_child_property (container_class,
404 CHILD_PROP_BOTTOM_ATTACH,
405 g_param_spec_uint ("bottom_attach",
407 _("The row number to attach the bottom of the child to"),
411 binding_set = gtk_binding_set_by_class (class);
412 gtk_binding_entry_add_signal (binding_set,
415 GTK_TYPE_MENU_DIRECTION_TYPE,
417 gtk_binding_entry_add_signal (binding_set,
420 GTK_TYPE_MENU_DIRECTION_TYPE,
422 gtk_binding_entry_add_signal (binding_set,
425 GTK_TYPE_MENU_DIRECTION_TYPE,
427 gtk_binding_entry_add_signal (binding_set,
430 GTK_TYPE_MENU_DIRECTION_TYPE,
432 gtk_binding_entry_add_signal (binding_set,
435 GTK_TYPE_MENU_DIRECTION_TYPE,
436 GTK_MENU_DIR_PARENT);
437 gtk_binding_entry_add_signal (binding_set,
440 GTK_TYPE_MENU_DIRECTION_TYPE,
441 GTK_MENU_DIR_PARENT);
442 gtk_binding_entry_add_signal (binding_set,
445 GTK_TYPE_MENU_DIRECTION_TYPE,
447 gtk_binding_entry_add_signal (binding_set,
450 GTK_TYPE_MENU_DIRECTION_TYPE,
452 gtk_binding_entry_add_signal (binding_set,
455 GTK_TYPE_SCROLL_TYPE,
457 gtk_binding_entry_add_signal (binding_set,
460 GTK_TYPE_SCROLL_TYPE,
462 gtk_binding_entry_add_signal (binding_set,
465 GTK_TYPE_SCROLL_TYPE,
467 gtk_binding_entry_add_signal (binding_set,
470 GTK_TYPE_SCROLL_TYPE,
472 gtk_binding_entry_add_signal (binding_set,
475 GTK_TYPE_SCROLL_TYPE,
477 gtk_binding_entry_add_signal (binding_set,
480 GTK_TYPE_SCROLL_TYPE,
482 gtk_binding_entry_add_signal (binding_set,
485 GTK_TYPE_SCROLL_TYPE,
486 GTK_SCROLL_PAGE_DOWN);
487 gtk_binding_entry_add_signal (binding_set,
490 GTK_TYPE_SCROLL_TYPE,
491 GTK_SCROLL_PAGE_DOWN);
493 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
494 _("Can change accelerators"),
495 _("Whether menu accelerators can be changed by pressing a key over the menu item"),
499 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
500 _("Delay before submenus appear"),
501 _("Minimum time the pointer must stay over a menu item before the submenu appear"),
507 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
508 _("Delay before hiding a submenu"),
509 _("The time before hiding a submenu when the pointer is moving towards the submenu"),
512 DEFAULT_POPDOWN_DELAY,
519 gtk_menu_set_property (GObject *object,
526 menu = GTK_MENU (object);
530 case PROP_TEAROFF_TITLE:
531 gtk_menu_set_title (menu, g_value_get_string (value));
534 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
540 gtk_menu_get_property (GObject *object,
547 menu = GTK_MENU (object);
551 case PROP_TEAROFF_TITLE:
552 g_value_set_string (value, gtk_menu_get_title (menu));
555 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
561 get_attach_info (GObject *child)
563 AttachInfo *ai = g_object_get_data (child, ATTACH_INFO_KEY);
567 ai = g_new0 (AttachInfo, 1);
568 g_object_set_data_full (child, ATTACH_INFO_KEY, ai, g_free);
575 gtk_menu_set_child_property (GtkContainer *container,
581 GtkMenu *menu = GTK_MENU (container);
582 GtkMenuPrivate *priv;
583 AttachInfo *ai = get_attach_info (G_OBJECT (child));
585 priv = gtk_menu_get_private (menu);
589 case CHILD_PROP_LEFT_ATTACH:
590 ai->left_attach = g_value_get_uint (value);
592 case CHILD_PROP_RIGHT_ATTACH:
593 ai->right_attach = g_value_get_uint (value);
595 case CHILD_PROP_TOP_ATTACH:
596 ai->top_attach = g_value_get_uint (value);
598 case CHILD_PROP_BOTTOM_ATTACH:
599 ai->bottom_attach = g_value_get_uint (value);
603 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
607 priv->columns = MAX (priv->columns, ai->right_attach);
608 priv->rows = MAX (priv->rows, ai->bottom_attach);
610 gtk_widget_queue_resize (GTK_WIDGET (menu));
614 gtk_menu_get_child_property (GtkContainer *container,
620 AttachInfo *ai = get_attach_info (G_OBJECT (child));
624 case CHILD_PROP_LEFT_ATTACH:
625 g_value_set_uint (value, ai->left_attach);
627 case CHILD_PROP_RIGHT_ATTACH:
628 g_value_set_uint (value, ai->right_attach);
630 case CHILD_PROP_TOP_ATTACH:
631 g_value_set_uint (value, ai->top_attach);
633 case CHILD_PROP_BOTTOM_ATTACH:
634 g_value_set_uint (value, ai->bottom_attach);
638 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
644 get_child_attach (GtkWidget *child,
650 gtk_container_child_get (GTK_CONTAINER (child->parent), child,
659 gtk_menu_window_event (GtkWidget *window,
663 gboolean handled = FALSE;
665 g_object_ref (window);
671 case GDK_KEY_RELEASE:
672 handled = gtk_widget_event (menu, event);
678 g_object_unref (window);
679 g_object_unref (menu);
685 gtk_menu_window_size_request (GtkWidget *window,
686 GtkRequisition *requisition,
689 GtkMenuPrivate *private = gtk_menu_get_private (menu);
691 if (private->have_position)
693 GdkScreen *screen = gtk_widget_get_screen (window);
694 GdkRectangle monitor;
697 monitor_num = gdk_screen_get_monitor_at_point (screen,
698 private->x, private->y);
699 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
701 if (private->y + requisition->height > monitor.y + monitor.height)
702 requisition->height = monitor.y + monitor.height - private->y;
704 if (private->y < monitor.y)
705 requisition->height -= monitor.y - private->y;
710 gtk_menu_init (GtkMenu *menu)
712 menu->parent_menu_item = NULL;
713 menu->old_active_menu_item = NULL;
714 menu->accel_group = NULL;
715 menu->position_func = NULL;
716 menu->position_func_data = NULL;
717 menu->toggle_size = 0;
719 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
720 "type", GTK_WINDOW_POPUP,
723 "signal::event", gtk_menu_window_event, menu,
724 "signal::size_request", gtk_menu_window_size_request, menu,
725 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
727 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
728 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
730 /* Refloat the menu, so that reference counting for the menu isn't
731 * affected by it being a child of the toplevel
733 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
734 menu->needs_destruction_ref_count = TRUE;
736 menu->view_window = NULL;
737 menu->bin_window = NULL;
739 menu->scroll_offset = 0;
740 menu->scroll_step = 0;
741 menu->timeout_id = 0;
742 menu->scroll_fast = FALSE;
744 menu->tearoff_window = NULL;
745 menu->tearoff_hbox = NULL;
746 menu->torn_off = FALSE;
747 menu->tearoff_active = FALSE;
748 menu->tearoff_adjustment = NULL;
749 menu->tearoff_scrollbar = NULL;
751 menu->upper_arrow_visible = FALSE;
752 menu->lower_arrow_visible = FALSE;
753 menu->upper_arrow_prelight = FALSE;
754 menu->lower_arrow_prelight = FALSE;
756 MENU_NEEDS_RESIZE (menu) = TRUE;
760 gtk_menu_destroy (GtkObject *object)
763 GtkMenuAttachData *data;
765 g_return_if_fail (GTK_IS_MENU (object));
767 menu = GTK_MENU (object);
769 gtk_menu_stop_scrolling (menu);
771 data = g_object_get_data (G_OBJECT (object), attach_data_key);
773 gtk_menu_detach (menu);
775 gtk_menu_stop_navigating_submenu (menu);
777 if (menu->old_active_menu_item)
779 g_object_unref (menu->old_active_menu_item);
780 menu->old_active_menu_item = NULL;
783 /* Add back the reference count for being a child */
784 if (menu->needs_destruction_ref_count)
786 menu->needs_destruction_ref_count = FALSE;
787 g_object_ref (object);
790 if (menu->accel_group)
792 g_object_unref (menu->accel_group);
793 menu->accel_group = NULL;
797 gtk_widget_destroy (menu->toplevel);
798 if (menu->tearoff_window)
799 gtk_widget_destroy (menu->tearoff_window);
801 GTK_OBJECT_CLASS (parent_class)->destroy (object);
805 gtk_menu_finalize (GObject *object)
807 GtkMenu *menu = GTK_MENU (object);
809 g_free (menu->accel_path);
811 G_OBJECT_CLASS (parent_class)->finalize (object);
815 menu_change_screen (GtkMenu *menu,
816 GdkScreen *new_screen)
820 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
821 gtk_menu_position (menu);
824 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
828 attach_widget_screen_changed (GtkWidget *attach_widget,
829 GdkScreen *previous_screen,
832 if (gtk_widget_has_screen (attach_widget) &&
833 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
835 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
840 gtk_menu_attach_to_widget (GtkMenu *menu,
841 GtkWidget *attach_widget,
842 GtkMenuDetachFunc detacher)
844 GtkMenuAttachData *data;
846 g_return_if_fail (GTK_IS_MENU (menu));
847 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
848 g_return_if_fail (detacher != NULL);
850 /* keep this function in sync with gtk_widget_set_parent()
853 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
856 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
857 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
862 gtk_object_sink (GTK_OBJECT (menu));
864 data = g_new (GtkMenuAttachData, 1);
865 data->attach_widget = attach_widget;
867 g_signal_connect (attach_widget, "screen_changed",
868 G_CALLBACK (attach_widget_screen_changed), menu);
869 attach_widget_screen_changed (attach_widget, NULL, menu);
871 data->detacher = detacher;
872 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
874 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
875 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
877 /* we don't need to set the style here, since
878 * we are a toplevel widget.
881 /* Fallback title for menu comes from attach widget */
882 gtk_menu_update_title (menu);
886 gtk_menu_get_attach_widget (GtkMenu *menu)
888 GtkMenuAttachData *data;
890 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
892 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
894 return data->attach_widget;
899 gtk_menu_detach (GtkMenu *menu)
901 GtkMenuAttachData *data;
903 g_return_if_fail (GTK_IS_MENU (menu));
905 /* keep this function in sync with gtk_widget_unparent()
907 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
910 g_warning ("gtk_menu_detach(): menu is not attached");
913 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
915 g_signal_handlers_disconnect_by_func (data->attach_widget,
916 (gpointer) attach_widget_screen_changed,
919 data->detacher (data->attach_widget, menu);
921 if (GTK_WIDGET_REALIZED (menu))
922 gtk_widget_unrealize (GTK_WIDGET (menu));
926 /* Fallback title for menu comes from attach widget */
927 gtk_menu_update_title (menu);
929 g_object_unref (menu);
933 gtk_menu_remove (GtkContainer *container,
938 g_return_if_fail (GTK_IS_MENU (container));
939 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
941 menu = GTK_MENU (container);
943 /* Clear out old_active_menu_item if it matches the item we are removing
945 if (menu->old_active_menu_item == widget)
947 g_object_unref (menu->old_active_menu_item);
948 menu->old_active_menu_item = NULL;
951 g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
953 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
960 return g_object_new (GTK_TYPE_MENU, NULL);
964 gtk_menu_real_insert (GtkMenuShell *menu_shell,
970 GtkMenuPrivate *priv;
972 if (GTK_WIDGET_REALIZED (menu_shell))
973 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
975 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
977 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
981 /* attach after the last row */
982 i = g_list_length (menu_shell->children) - 1;
983 gtk_menu_attach (GTK_MENU (menu_shell), child,
984 0, priv->columns ? priv->columns : 1,
990 /* we need to make space for this new item; move all items with
991 * top >= position one down
993 for (children = menu_shell->children; children; children = children->next)
997 gtk_container_child_get (GTK_CONTAINER (menu_shell), children->data,
999 "bottom_attach", &bottom,
1002 if (top >= position)
1003 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
1004 "top_attach", top + 1,
1005 "bottom_attach", bottom + 1,
1009 /* attach the new item */
1010 gtk_menu_attach (GTK_MENU (menu_shell), child,
1011 0, priv->columns ? priv->columns : 1,
1012 position, position + 1);
1016 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1021 widget = GTK_WIDGET (menu);
1027 GdkGCValues gc_values;
1029 menu->tearoff_active = FALSE;
1030 menu->saved_scroll_offset = menu->scroll_offset;
1032 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1033 gc = gdk_gc_new_with_values (widget->window,
1034 &gc_values, GDK_GC_SUBWINDOW);
1036 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1038 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1043 gdk_draw_drawable (pixmap, gc,
1044 menu->tearoff_window->window,
1045 0, 0, 0, 0, -1, -1);
1046 g_object_unref (gc);
1048 gtk_widget_set_size_request (menu->tearoff_window,
1052 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1053 g_object_unref (pixmap);
1058 popup_grab_on_window (GdkWindow *window,
1059 guint32 activate_time)
1061 if ((gdk_pointer_grab (window, TRUE,
1062 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1063 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1064 GDK_POINTER_MOTION_MASK,
1065 NULL, NULL, activate_time) == 0))
1067 if (gdk_keyboard_grab (window, TRUE,
1068 activate_time) == 0)
1072 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1083 * @menu: a #GtkMenu.
1084 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1085 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1086 * @func: a user supplied function used to position the menu, or %NULL
1087 * @data: user supplied data to be passed to @func.
1088 * @button: the mouse button which was pressed to initiate the event.
1089 * @activate_time: the time at which the activation event occurred.
1091 * Displays a menu and makes it available for selection. Applications can use
1092 * this function to display context-sensitive menus, and will typically supply
1093 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1094 * parameters. The default menu positioning function will position the menu
1095 * at the current mouse cursor position.
1097 * The @button parameter should be the mouse button pressed to initiate
1098 * the menu popup. If the menu popup was initiated by something other than
1099 * a mouse button press, such as a mouse button release or a keypress,
1100 * @button should be 0.
1102 * The @activate_time parameter should be the time stamp of the event that
1103 * initiated the popup. If such an event is not available, use
1104 * gtk_get_current_event_time() instead.
1108 gtk_menu_popup (GtkMenu *menu,
1109 GtkWidget *parent_menu_shell,
1110 GtkWidget *parent_menu_item,
1111 GtkMenuPositionFunc func,
1114 guint32 activate_time)
1117 GtkWidget *xgrab_shell;
1119 GdkEvent *current_event;
1120 GtkMenuShell *menu_shell;
1122 g_return_if_fail (GTK_IS_MENU (menu));
1124 widget = GTK_WIDGET (menu);
1125 menu_shell = GTK_MENU_SHELL (menu);
1127 menu_shell->parent_menu_shell = parent_menu_shell;
1129 /* Find the last viewable ancestor, and make an X grab on it
1131 parent = GTK_WIDGET (menu);
1135 gboolean viewable = TRUE;
1136 GtkWidget *tmp = parent;
1140 if (!GTK_WIDGET_MAPPED (tmp))
1149 xgrab_shell = parent;
1151 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1154 /* We want to receive events generated when we map the menu; unfortunately,
1155 * since there is probably already an implicit grab in place from the
1156 * button that the user used to pop up the menu, we won't receive then --
1157 * in particular, the EnterNotify when the menu pops up under the pointer.
1159 * If we are grabbing on a parent menu shell, no problem; just grab on
1160 * that menu shell first before popping up the window with owner_events = TRUE.
1162 * When grabbing on the menu itself, things get more convuluted - we
1163 * we do an explicit grab on a specially created window with
1164 * owner_events = TRUE, which we override further down with a grab
1165 * on the menu. (We can't grab on the menu until it is mapped; we
1166 * probably could just leave the grab on the other window, with a
1167 * little reorganization of the code in gtkmenu*).
1169 if (xgrab_shell && xgrab_shell != widget)
1171 if (popup_grab_on_window (xgrab_shell->window, activate_time))
1172 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1176 GdkWindow *transfer_window;
1178 xgrab_shell = widget;
1179 transfer_window = menu_grab_transfer_window_get (menu);
1180 if (popup_grab_on_window (transfer_window, activate_time))
1181 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1184 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1186 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1187 * with a stuck up window, we just abort here. Presumably the user will
1190 menu_shell->parent_menu_shell = NULL;
1191 menu_grab_transfer_window_destroy (menu);
1195 menu_shell->active = TRUE;
1196 menu_shell->button = button;
1198 /* If we are popping up the menu from something other than, a button
1199 * press then, as a heuristic, we ignore enter events for the menu
1200 * until we get a MOTION_NOTIFY.
1203 current_event = gtk_get_current_event ();
1206 if ((current_event->type != GDK_BUTTON_PRESS) &&
1207 (current_event->type != GDK_ENTER_NOTIFY))
1208 menu_shell->ignore_enter = TRUE;
1210 gdk_event_free (current_event);
1213 menu_shell->ignore_enter = TRUE;
1217 gtk_menu_tearoff_bg_copy (menu);
1219 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1222 menu->parent_menu_item = parent_menu_item;
1223 menu->position_func = func;
1224 menu->position_func_data = data;
1225 menu_shell->activate_time = activate_time;
1227 /* We need to show the menu here rather in the init function because
1228 * code expects to be able to tell if the menu is onscreen by
1229 * looking at the GTK_WIDGET_VISIBLE (menu)
1231 gtk_widget_show (GTK_WIDGET (menu));
1233 /* Position the menu, possibly changing the size request
1235 gtk_menu_position (menu);
1237 /* Compute the size of the toplevel and realize it so we
1238 * can scroll correctly.
1241 GtkRequisition tmp_request;
1242 GtkAllocation tmp_allocation = { 0, };
1244 gtk_widget_size_request (menu->toplevel, &tmp_request);
1246 tmp_allocation.width = tmp_request.width;
1247 tmp_allocation.height = tmp_request.height;
1249 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1251 gtk_widget_realize (GTK_WIDGET (menu));
1254 gtk_menu_scroll_to (menu, menu->scroll_offset);
1256 /* Once everything is set up correctly, map the toplevel window on
1259 gtk_widget_show (menu->toplevel);
1261 if (xgrab_shell == widget)
1262 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1263 gtk_grab_add (GTK_WIDGET (menu));
1267 gtk_menu_popdown (GtkMenu *menu)
1269 GtkMenuPrivate *private;
1270 GtkMenuShell *menu_shell;
1272 g_return_if_fail (GTK_IS_MENU (menu));
1274 menu_shell = GTK_MENU_SHELL (menu);
1275 private = gtk_menu_get_private (menu);
1277 menu_shell->parent_menu_shell = NULL;
1278 menu_shell->active = FALSE;
1279 menu_shell->ignore_enter = FALSE;
1281 private->have_position = FALSE;
1283 gtk_menu_stop_scrolling (menu);
1285 gtk_menu_stop_navigating_submenu (menu);
1287 if (menu_shell->active_menu_item)
1289 if (menu->old_active_menu_item)
1290 g_object_unref (menu->old_active_menu_item);
1291 menu->old_active_menu_item = menu_shell->active_menu_item;
1292 g_object_ref (menu->old_active_menu_item);
1295 gtk_menu_shell_deselect (menu_shell);
1297 /* The X Grab, if present, will automatically be removed when we hide
1299 gtk_widget_hide (menu->toplevel);
1303 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1305 if (GTK_BIN (menu->toplevel)->child)
1307 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1311 /* We popped up the menu from the tearoff, so we need to
1312 * release the grab - we aren't actually hiding the menu.
1314 if (menu_shell->have_xgrab)
1316 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1318 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1319 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1323 /* gtk_menu_popdown is called each time a menu item is selected from
1324 * a torn off menu. Only scroll back to the saved position if the
1325 * non-tearoff menu was popped down.
1327 if (!menu->tearoff_active)
1328 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1329 menu->tearoff_active = TRUE;
1332 gtk_widget_hide (GTK_WIDGET (menu));
1334 menu_shell->have_xgrab = FALSE;
1335 gtk_grab_remove (GTK_WIDGET (menu));
1337 menu_grab_transfer_window_destroy (menu);
1341 gtk_menu_get_active (GtkMenu *menu)
1346 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1348 if (!menu->old_active_menu_item)
1351 children = GTK_MENU_SHELL (menu)->children;
1355 child = children->data;
1356 children = children->next;
1358 if (GTK_BIN (child)->child)
1363 menu->old_active_menu_item = child;
1364 if (menu->old_active_menu_item)
1365 g_object_ref (menu->old_active_menu_item);
1368 return menu->old_active_menu_item;
1372 gtk_menu_set_active (GtkMenu *menu,
1378 g_return_if_fail (GTK_IS_MENU (menu));
1380 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1383 child = tmp_list->data;
1384 if (GTK_BIN (child)->child)
1386 if (menu->old_active_menu_item)
1387 g_object_unref (menu->old_active_menu_item);
1388 menu->old_active_menu_item = child;
1389 g_object_ref (menu->old_active_menu_item);
1395 gtk_menu_set_accel_group (GtkMenu *menu,
1396 GtkAccelGroup *accel_group)
1398 g_return_if_fail (GTK_IS_MENU (menu));
1400 if (menu->accel_group != accel_group)
1402 if (menu->accel_group)
1403 g_object_unref (menu->accel_group);
1404 menu->accel_group = accel_group;
1405 if (menu->accel_group)
1406 g_object_ref (menu->accel_group);
1407 _gtk_menu_refresh_accel_paths (menu, TRUE);
1412 gtk_menu_get_accel_group (GtkMenu *menu)
1414 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1416 return menu->accel_group;
1420 * gtk_menu_set_accel_path
1421 * @menu: a valid #GtkMenu
1422 * @accel_path: a valid accelerator path
1424 * Sets an accelerator path for this menu from which accelerator paths
1425 * for its immediate children, its menu items, can be constructed.
1426 * The main purpose of this function is to spare the programmer the
1427 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1428 * each menu item that should support runtime user changable accelerators.
1429 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1430 * each menu item of this menu, that contains a label describing its purpose,
1431 * automatically gets an accel path assigned. For example, a menu containing
1432 * menu items "New" and "Exit", will, after
1433 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1434 * has been called, assign its items the accel paths:
1435 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1436 * Assigning accel paths to menu items then enables the user to change
1437 * their accelerators at runtime. More details about accelerator paths
1438 * and their default setups can be found at gtk_accel_map_add_entry().
1441 gtk_menu_set_accel_path (GtkMenu *menu,
1442 const gchar *accel_path)
1444 g_return_if_fail (GTK_IS_MENU (menu));
1446 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1448 g_free (menu->accel_path);
1449 menu->accel_path = g_strdup (accel_path);
1450 if (menu->accel_path)
1451 _gtk_menu_refresh_accel_paths (menu, FALSE);
1456 gboolean group_changed;
1460 refresh_accel_paths_foreach (GtkWidget *widget,
1463 AccelPropagation *prop = data;
1465 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1466 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1467 prop->menu->accel_path,
1468 prop->menu->accel_group,
1469 prop->group_changed);
1473 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1474 gboolean group_changed)
1476 g_return_if_fail (GTK_IS_MENU (menu));
1478 if (menu->accel_path && menu->accel_group)
1480 AccelPropagation prop;
1483 prop.group_changed = group_changed;
1484 gtk_container_foreach (GTK_CONTAINER (menu),
1485 refresh_accel_paths_foreach,
1491 gtk_menu_reposition (GtkMenu *menu)
1493 g_return_if_fail (GTK_IS_MENU (menu));
1495 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1496 gtk_menu_position (menu);
1500 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1503 g_return_if_fail (GTK_IS_MENU (menu));
1505 if (adjustment->value != menu->scroll_offset)
1506 gtk_menu_scroll_to (menu, adjustment->value);
1510 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1513 GdkGeometry geometry_hints;
1515 if (!menu->tearoff_window)
1518 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1520 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1521 width += menu->tearoff_scrollbar->requisition.width;
1524 geometry_hints.min_width = width;
1525 geometry_hints.max_width = width;
1527 geometry_hints.min_height = 0;
1528 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1530 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1533 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1537 gtk_menu_update_title (GtkMenu *menu)
1539 if (menu->tearoff_window)
1542 GtkWidget *attach_widget;
1544 title = gtk_menu_get_title (menu);
1547 attach_widget = gtk_menu_get_attach_widget (menu);
1548 if (GTK_IS_MENU_ITEM (attach_widget))
1550 GtkWidget *child = GTK_BIN (attach_widget)->child;
1551 if (GTK_IS_LABEL (child))
1552 title = gtk_label_get_text (GTK_LABEL (child));
1557 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1562 gtk_menu_get_toplevel (GtkWidget *menu)
1564 GtkWidget *attach, *toplevel;
1566 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1568 if (GTK_IS_MENU_ITEM (attach))
1569 attach = attach->parent;
1571 if (GTK_IS_MENU (attach))
1572 return gtk_menu_get_toplevel (attach->parent);
1573 else if (GTK_IS_WIDGET (attach))
1575 toplevel = gtk_widget_get_toplevel (attach->parent);
1576 if (GTK_WIDGET_TOPLEVEL (toplevel))
1584 gtk_menu_set_tearoff_state (GtkMenu *menu,
1589 g_return_if_fail (GTK_IS_MENU (menu));
1591 if (menu->torn_off != torn_off)
1593 menu->torn_off = torn_off;
1594 menu->tearoff_active = torn_off;
1598 if (GTK_WIDGET_VISIBLE (menu))
1599 gtk_menu_popdown (menu);
1601 if (!menu->tearoff_window)
1603 GtkWidget *toplevel;
1605 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1606 "type", GTK_WINDOW_TOPLEVEL,
1607 "screen", gtk_widget_get_screen (menu->toplevel),
1608 "app_paintable", TRUE,
1612 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1613 GDK_WINDOW_TYPE_HINT_MENU);
1614 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1615 g_signal_connect (menu->tearoff_window, "destroy",
1616 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1617 g_signal_connect (menu->tearoff_window, "event",
1618 G_CALLBACK (gtk_menu_window_event), menu);
1620 gtk_menu_update_title (menu);
1622 gtk_widget_realize (menu->tearoff_window);
1624 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1625 if (toplevel != NULL)
1626 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1627 GTK_WINDOW (toplevel));
1629 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1630 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1632 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1633 menu->tearoff_adjustment =
1634 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1636 GTK_WIDGET (menu)->requisition.height,
1640 g_object_connect (menu->tearoff_adjustment,
1641 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1643 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1645 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1646 menu->tearoff_scrollbar,
1649 if (menu->tearoff_adjustment->upper > height)
1650 gtk_widget_show (menu->tearoff_scrollbar);
1652 gtk_widget_show (menu->tearoff_hbox);
1655 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1657 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1659 /* Update menu->requisition
1661 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1663 gtk_menu_set_tearoff_hints (menu, width);
1665 gtk_widget_realize (menu->tearoff_window);
1666 gtk_menu_position (menu);
1668 gtk_widget_show (GTK_WIDGET (menu));
1669 gtk_widget_show (menu->tearoff_window);
1671 gtk_menu_scroll_to (menu, 0);
1676 gtk_widget_hide (menu);
1677 gtk_widget_hide (menu->tearoff_window);
1678 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1679 gtk_widget_destroy (menu->tearoff_window);
1681 menu->tearoff_window = NULL;
1682 menu->tearoff_hbox = NULL;
1683 menu->tearoff_scrollbar = NULL;
1684 menu->tearoff_adjustment = NULL;
1690 * gtk_menu_get_tearoff_state:
1693 * Returns whether the menu is torn off. See
1694 * gtk_menu_set_tearoff_state ().
1696 * Return value: %TRUE if the menu is currently torn off.
1699 gtk_menu_get_tearoff_state (GtkMenu *menu)
1701 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1703 return menu->torn_off;
1707 * gtk_menu_set_title:
1709 * @title: a string containing the title for the menu.
1711 * Sets the title string for the menu. The title is displayed when the menu
1712 * is shown as a tearoff menu.
1715 gtk_menu_set_title (GtkMenu *menu,
1718 g_return_if_fail (GTK_IS_MENU (menu));
1721 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1722 g_strdup (title), (GtkDestroyNotify) g_free);
1724 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1726 gtk_menu_update_title (menu);
1727 g_object_notify (G_OBJECT (menu), "tearoff_title");
1731 * gtk_menu_get_title:
1734 * Returns the title of the menu. See gtk_menu_set_title().
1736 * Return value: the title of the menu, or %NULL if the menu has no
1737 * title set on it. This string is owned by the widget and should
1738 * not be modified or freed.
1740 G_CONST_RETURN gchar *
1741 gtk_menu_get_title (GtkMenu *menu)
1743 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1745 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1749 gtk_menu_reorder_child (GtkMenu *menu,
1753 GtkMenuShell *menu_shell;
1754 g_return_if_fail (GTK_IS_MENU (menu));
1755 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1756 menu_shell = GTK_MENU_SHELL (menu);
1757 if (g_list_find (menu_shell->children, child))
1759 menu_shell->children = g_list_remove (menu_shell->children, child);
1760 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1761 if (GTK_WIDGET_VISIBLE (menu_shell))
1762 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1767 gtk_menu_style_set (GtkWidget *widget,
1768 GtkStyle *previous_style)
1770 if (GTK_WIDGET_REALIZED (widget))
1772 GtkMenu *menu = GTK_MENU (widget);
1774 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1775 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1776 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1781 gtk_menu_realize (GtkWidget *widget)
1783 GdkWindowAttr attributes;
1784 gint attributes_mask;
1789 guint vertical_padding;
1791 g_return_if_fail (GTK_IS_MENU (widget));
1793 menu = GTK_MENU (widget);
1795 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1797 attributes.window_type = GDK_WINDOW_CHILD;
1798 attributes.x = widget->allocation.x;
1799 attributes.y = widget->allocation.y;
1800 attributes.width = widget->allocation.width;
1801 attributes.height = widget->allocation.height;
1802 attributes.wclass = GDK_INPUT_OUTPUT;
1803 attributes.visual = gtk_widget_get_visual (widget);
1804 attributes.colormap = gtk_widget_get_colormap (widget);
1806 attributes.event_mask = gtk_widget_get_events (widget);
1808 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1809 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1811 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1812 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1813 gdk_window_set_user_data (widget->window, widget);
1815 border_width = GTK_CONTAINER (widget)->border_width;
1817 gtk_widget_style_get (GTK_WIDGET (menu),
1818 "vertical-padding", &vertical_padding,
1821 attributes.x = border_width + widget->style->xthickness;
1822 attributes.y = border_width + widget->style->ythickness + vertical_padding;
1823 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1824 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1826 if (menu->upper_arrow_visible)
1828 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1829 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1831 if (menu->lower_arrow_visible)
1832 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1834 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1835 gdk_window_set_user_data (menu->view_window, menu);
1839 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
1841 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1842 gdk_window_set_user_data (menu->bin_window, menu);
1844 children = GTK_MENU_SHELL (menu)->children;
1847 child = children->data;
1848 children = children->next;
1850 gtk_widget_set_parent_window (child, menu->bin_window);
1853 widget->style = gtk_style_attach (widget->style, widget->window);
1854 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1855 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1856 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1858 if (GTK_MENU_SHELL (widget)->active_menu_item)
1859 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1860 GTK_MENU_SHELL (widget)->active_menu_item);
1862 gdk_window_show (menu->bin_window);
1863 gdk_window_show (menu->view_window);
1867 gtk_menu_focus (GtkWidget *widget,
1868 GtkDirectionType direction)
1871 * A menu or its menu items cannot have focus
1876 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1879 menu_grab_transfer_window_get (GtkMenu *menu)
1881 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1884 GdkWindowAttr attributes;
1885 gint attributes_mask;
1887 attributes.x = -100;
1888 attributes.y = -100;
1889 attributes.width = 10;
1890 attributes.height = 10;
1891 attributes.window_type = GDK_WINDOW_TEMP;
1892 attributes.wclass = GDK_INPUT_ONLY;
1893 attributes.override_redirect = TRUE;
1894 attributes.event_mask = 0;
1896 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1898 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1899 &attributes, attributes_mask);
1900 gdk_window_set_user_data (window, menu);
1902 gdk_window_show (window);
1904 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1911 menu_grab_transfer_window_destroy (GtkMenu *menu)
1913 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1916 gdk_window_set_user_data (window, NULL);
1917 gdk_window_destroy (window);
1918 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1923 gtk_menu_unrealize (GtkWidget *widget)
1927 g_return_if_fail (GTK_IS_MENU (widget));
1929 menu = GTK_MENU (widget);
1931 menu_grab_transfer_window_destroy (menu);
1933 gdk_window_set_user_data (menu->view_window, NULL);
1934 gdk_window_destroy (menu->view_window);
1935 menu->view_window = NULL;
1937 gdk_window_set_user_data (menu->bin_window, NULL);
1938 gdk_window_destroy (menu->bin_window);
1939 menu->bin_window = NULL;
1941 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1945 gtk_menu_size_request (GtkWidget *widget,
1946 GtkRequisition *requisition)
1950 GtkMenuShell *menu_shell;
1953 guint max_toggle_size;
1954 guint max_accel_width;
1955 guint vertical_padding;
1956 GtkRequisition child_requisition;
1957 GtkMenuPrivate *priv;
1959 g_return_if_fail (GTK_IS_MENU (widget));
1960 g_return_if_fail (requisition != NULL);
1962 menu = GTK_MENU (widget);
1963 menu_shell = GTK_MENU_SHELL (widget);
1964 priv = gtk_menu_get_private (menu);
1966 requisition->width = 0;
1967 requisition->height = 0;
1969 max_toggle_size = 0;
1970 max_accel_width = 0;
1972 g_free (priv->heights);
1973 priv->heights = g_new0 (guint, priv->rows);
1974 priv->heights_length = priv->rows;
1976 children = menu_shell->children;
1983 child = children->data;
1984 children = children->next;
1986 if (! GTK_WIDGET_VISIBLE (child))
1989 get_child_attach (child, &l, &r, &t, &b);
1991 /* It's important to size_request the child
1992 * before doing the toggle size request, in
1993 * case the toggle size request depends on the size
1994 * request of a child of the child (e.g. for ImageMenuItem)
1997 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
1998 gtk_widget_size_request (child, &child_requisition);
2000 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2001 max_toggle_size = MAX (max_toggle_size, toggle_size);
2002 max_accel_width = MAX (max_accel_width,
2003 GTK_MENU_ITEM (child)->accelerator_width);
2005 part = child_requisition.width / (r - l);
2006 requisition->width = MAX (requisition->width, part);
2008 part = child_requisition.height / (b - t);
2009 priv->heights[t] = MAX (priv->heights[t], part);
2012 for (i = 0; i < priv->rows; i++)
2013 requisition->height += priv->heights[i];
2015 requisition->width += max_toggle_size + max_accel_width;
2016 requisition->width *= priv->columns;
2017 requisition->width += (GTK_CONTAINER (menu)->border_width +
2018 widget->style->xthickness) * 2;
2020 gtk_widget_style_get (GTK_WIDGET (menu),
2021 "vertical-padding", &vertical_padding,
2023 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2024 widget->style->ythickness) * 2;
2026 menu->toggle_size = max_toggle_size;
2028 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2030 if (menu->tearoff_active)
2031 gtk_menu_set_tearoff_hints (menu, requisition->width);
2035 gtk_menu_size_allocate (GtkWidget *widget,
2036 GtkAllocation *allocation)
2039 GtkMenuShell *menu_shell;
2041 GtkAllocation child_allocation;
2042 GtkRequisition child_requisition;
2043 GtkMenuPrivate *priv;
2047 guint vertical_padding;
2049 g_return_if_fail (GTK_IS_MENU (widget));
2050 g_return_if_fail (allocation != NULL);
2052 menu = GTK_MENU (widget);
2053 menu_shell = GTK_MENU_SHELL (widget);
2054 priv = gtk_menu_get_private (menu);
2056 widget->allocation = *allocation;
2057 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2059 gtk_widget_style_get (GTK_WIDGET (menu),
2060 "vertical-padding", &vertical_padding,
2063 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2064 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2066 width = MAX (1, allocation->width - x * 2);
2067 height = MAX (1, allocation->height - y * 2);
2069 child_requisition.width -= x * 2;
2070 child_requisition.height -= y * 2;
2072 if (menu_shell->active)
2073 gtk_menu_scroll_to (menu, menu->scroll_offset);
2075 if (menu->upper_arrow_visible && !menu->tearoff_active)
2077 y += MENU_SCROLL_ARROW_HEIGHT;
2078 height -= MENU_SCROLL_ARROW_HEIGHT;
2081 if (menu->lower_arrow_visible && !menu->tearoff_active)
2082 height -= MENU_SCROLL_ARROW_HEIGHT;
2084 if (GTK_WIDGET_REALIZED (widget))
2086 gdk_window_move_resize (widget->window,
2087 allocation->x, allocation->y,
2088 allocation->width, allocation->height);
2090 gdk_window_move_resize (menu->view_window,
2097 if (menu_shell->children)
2099 gint base_width = width / priv->columns;
2101 children = menu_shell->children;
2104 child = children->data;
2105 children = children->next;
2107 if (GTK_WIDGET_VISIBLE (child))
2112 get_child_attach (child, &l, &r, &t, &b);
2114 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2117 tmp = priv->columns - l;
2118 l = priv->columns - r;
2122 child_allocation.width = (r - l) * base_width;
2123 child_allocation.height = 0;
2124 child_allocation.x = l * base_width;
2125 child_allocation.y = 0;
2127 for (i = 0; i < b; i++)
2130 child_allocation.y += priv->heights[i];
2132 child_allocation.height += priv->heights[i];
2135 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2138 gtk_widget_size_allocate (child, &child_allocation);
2139 gtk_widget_queue_draw (child);
2143 /* Resize the item window */
2144 if (GTK_WIDGET_REALIZED (widget))
2150 for (i = 0; i < priv->rows; i++)
2151 height += priv->heights[i];
2153 width = priv->columns * base_width;
2154 gdk_window_resize (menu->bin_window, width, height);
2157 if (menu->tearoff_active)
2159 if (allocation->height >= widget->requisition.height)
2161 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2163 gtk_widget_hide (menu->tearoff_scrollbar);
2164 gtk_menu_set_tearoff_hints (menu, allocation->width);
2166 gtk_menu_scroll_to (menu, 0);
2171 menu->tearoff_adjustment->upper = widget->requisition.height;
2172 menu->tearoff_adjustment->page_size = allocation->height;
2174 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2175 menu->tearoff_adjustment->upper)
2178 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2181 gtk_menu_scroll_to (menu, value);
2184 gtk_adjustment_changed (menu->tearoff_adjustment);
2186 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2188 gtk_widget_show (menu->tearoff_scrollbar);
2189 gtk_menu_set_tearoff_hints (menu, allocation->width);
2197 gtk_menu_paint (GtkWidget *widget,
2198 GdkEventExpose *event)
2202 gint border_x, border_y;
2203 guint vertical_padding;
2205 g_return_if_fail (GTK_IS_MENU (widget));
2207 menu = GTK_MENU (widget);
2209 gtk_widget_style_get (GTK_WIDGET (menu),
2210 "vertical-padding", &vertical_padding,
2213 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2214 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2215 gdk_drawable_get_size (widget->window, &width, &height);
2217 if (event->window == widget->window)
2219 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2220 gint arrow_size = 0.7 * arrow_space;
2222 gtk_paint_box (widget->style,
2226 NULL, widget, "menu",
2228 if (menu->upper_arrow_visible && !menu->tearoff_active)
2230 gtk_paint_box (widget->style,
2232 menu->upper_arrow_prelight ?
2233 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2235 NULL, widget, "menu",
2238 width - 2 * border_x,
2239 MENU_SCROLL_ARROW_HEIGHT);
2241 gtk_paint_arrow (widget->style,
2243 menu->upper_arrow_prelight ?
2244 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2246 NULL, widget, "menu_scroll_arrow_up",
2249 (width - arrow_size ) / 2,
2250 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2251 arrow_size, arrow_size);
2254 if (menu->lower_arrow_visible && !menu->tearoff_active)
2256 gtk_paint_box (widget->style,
2258 menu->lower_arrow_prelight ?
2259 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2261 NULL, widget, "menu",
2263 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2265 MENU_SCROLL_ARROW_HEIGHT);
2267 gtk_paint_arrow (widget->style,
2269 menu->lower_arrow_prelight ?
2270 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2272 NULL, widget, "menu_scroll_arrow_down",
2275 (width - arrow_size) / 2,
2276 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2277 widget->style->ythickness + (arrow_space - arrow_size)/2,
2278 arrow_size, arrow_size);
2284 gtk_menu_expose (GtkWidget *widget,
2285 GdkEventExpose *event)
2287 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2288 g_return_val_if_fail (event != NULL, FALSE);
2290 if (GTK_WIDGET_DRAWABLE (widget))
2292 gtk_menu_paint (widget, event);
2294 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2301 gtk_menu_show (GtkWidget *widget)
2303 GtkMenu *menu = GTK_MENU (widget);
2305 _gtk_menu_refresh_accel_paths (menu, FALSE);
2307 GTK_WIDGET_CLASS (parent_class)->show (widget);
2311 gtk_menu_button_press (GtkWidget *widget,
2312 GdkEventButton *event)
2314 /* Don't pop down the menu for releases over scroll arrows
2316 if (GTK_IS_MENU (widget))
2318 GtkMenu *menu = GTK_MENU (widget);
2320 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2324 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2328 gtk_menu_button_release (GtkWidget *widget,
2329 GdkEventButton *event)
2331 /* Don't pop down the menu for releases over scroll arrows
2333 if (GTK_IS_MENU (widget))
2335 GtkMenu *menu = GTK_MENU (widget);
2337 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2341 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2344 static const gchar *
2345 get_accel_path (GtkWidget *menu_item,
2350 GClosure *accel_closure;
2351 GtkAccelGroup *accel_group;
2353 path = _gtk_widget_get_accel_path (menu_item, locked);
2356 path = GTK_MENU_ITEM (menu_item)->accel_path;
2362 label = GTK_BIN (menu_item)->child;
2364 if (GTK_IS_ACCEL_LABEL (label))
2366 g_object_get (label,
2367 "accel_closure", &accel_closure,
2369 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2371 *locked = accel_group->lock_count > 0;
2380 gtk_menu_key_press (GtkWidget *widget,
2383 GtkMenuShell *menu_shell;
2385 gboolean delete = FALSE;
2386 gboolean can_change_accels;
2387 gchar *accel = NULL;
2388 guint accel_key, accel_mods;
2389 GdkModifierType consumed_modifiers;
2390 GdkDisplay *display;
2392 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2393 g_return_val_if_fail (event != NULL, FALSE);
2395 menu_shell = GTK_MENU_SHELL (widget);
2396 menu = GTK_MENU (widget);
2398 gtk_menu_stop_navigating_submenu (menu);
2400 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2403 display = gtk_widget_get_display (widget);
2405 g_object_get (gtk_widget_get_settings (widget),
2406 "gtk-menu-bar-accel", &accel,
2407 "gtk-can-change-accels", &can_change_accels,
2413 GdkModifierType mods = 0;
2414 gboolean handled = FALSE;
2416 gtk_accelerator_parse (accel, &keyval, &mods);
2419 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2421 /* FIXME this is wrong, needs to be in the global accel resolution
2422 * thing, to properly consider i18n etc., but that probably requires
2423 * AccelGroup changes etc.
2425 if (event->keyval == keyval &&
2426 (mods & event->state) == mods)
2428 g_signal_emit_by_name (menu, "cancel", 0);
2437 switch (event->keyval)
2448 /* Figure out what modifiers went into determining the key symbol */
2449 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2450 event->hardware_keycode, event->state, event->group,
2451 NULL, NULL, NULL, &consumed_modifiers);
2453 accel_key = gdk_keyval_to_lower (event->keyval);
2454 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2456 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2457 * We re-upper case when we match against the keyval, but display and save in caseless form.
2459 if (accel_key != event->keyval)
2460 accel_mods |= GDK_SHIFT_MASK;
2462 /* Modify the accelerators */
2463 if (can_change_accels &&
2464 menu_shell->active_menu_item &&
2465 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2466 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2467 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2469 GtkWidget *menu_item = menu_shell->active_menu_item;
2470 gboolean locked, replace_accels = TRUE;
2473 path = get_accel_path (menu_item, &locked);
2474 if (!path || locked)
2476 /* can't change accelerators on menu_items without paths
2477 * (basically, those items are accelerator-locked).
2479 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2480 gdk_display_beep (display);
2486 /* For the keys that act to delete the current setting, we delete
2487 * the current setting if there is one, otherwise, we set the
2488 * key as the accelerator.
2494 if (gtk_accel_map_lookup_entry (path, &key) &&
2495 (key.accel_key || key.accel_mods))
2501 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2505 /* we failed, probably because this key is in use and
2508 /* g_print("failed to change\n"); */
2509 gdk_display_beep (display);
2518 gtk_menu_motion_notify (GtkWidget *widget,
2519 GdkEventMotion *event)
2521 GtkWidget *menu_item;
2523 GtkMenuShell *menu_shell;
2525 gboolean need_enter;
2527 if (GTK_IS_MENU (widget))
2528 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2530 /* We received the event for one of two reasons:
2532 * a) We are the active menu, and did gtk_grab_add()
2533 * b) The widget is a child of ours, and the event was propagated
2535 * Since for computation of navigation regions, we want the menu which
2536 * is the parent of the menu item, for a), we need to find that menu,
2537 * which may be different from 'widget'.
2539 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2540 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2541 !_gtk_menu_item_is_selectable (menu_item) ||
2542 !GTK_IS_MENU (menu_item->parent))
2545 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2546 menu = GTK_MENU (menu_shell);
2548 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2550 /* Check to see if we are within an active submenu's navigation region
2552 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2557 /* The menu is now sensitive to enter events on its items, but
2558 * was previously sensitive. So we fake an enter event.
2562 menu_shell->ignore_enter = FALSE;
2564 gdk_drawable_get_size (event->window, &width, &height);
2565 if (event->x >= 0 && event->x < width &&
2566 event->y >= 0 && event->y < height)
2568 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2571 send_event->crossing.window = g_object_ref (event->window);
2572 send_event->crossing.time = event->time;
2573 send_event->crossing.send_event = TRUE;
2574 send_event->crossing.x_root = event->x_root;
2575 send_event->crossing.y_root = event->y_root;
2576 send_event->crossing.x = event->x;
2577 send_event->crossing.y = event->y;
2579 /* We send the event to 'widget', the currently active menu,
2580 * instead of 'menu', the menu that the pointer is in. This
2581 * will ensure that the event will be ignored unless the
2582 * menuitem is a child of the active menu or some parent
2583 * menu of the active menu.
2585 result = gtk_widget_event (widget, send_event);
2586 gdk_event_free (send_event);
2596 gtk_menu_scroll_timeout (gpointer data)
2601 gint view_width, view_height;
2603 GDK_THREADS_ENTER ();
2605 menu = GTK_MENU (data);
2606 widget = GTK_WIDGET (menu);
2608 offset = menu->scroll_offset + menu->scroll_step;
2610 /* If we scroll upward and the non-visible top part
2611 * is smaller than the scroll arrow it would be
2612 * pretty stupid to show the arrow and taking more
2613 * screen space than just scrolling to the top.
2615 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2618 /* Don't scroll over the top if we weren't before: */
2619 if ((menu->scroll_offset >= 0) && (offset < 0))
2622 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2624 /* Don't scroll past the bottom if we weren't before: */
2625 if (menu->scroll_offset > 0)
2626 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2628 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2629 (offset + view_height > widget->requisition.height))
2630 offset = widget->requisition.height - view_height;
2632 gtk_menu_scroll_to (menu, offset);
2634 GDK_THREADS_LEAVE ();
2640 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2642 GtkMenuShell *menu_shell;
2648 gboolean scroll_fast = FALSE;
2649 guint vertical_padding;
2651 menu_shell = GTK_MENU_SHELL (menu);
2653 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2654 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2656 gtk_widget_style_get (GTK_WIDGET (menu),
2657 "vertical-padding", &vertical_padding,
2660 border = GTK_CONTAINER (menu)->border_width +
2661 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2663 if (menu->upper_arrow_visible && !menu->tearoff_active)
2668 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2671 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2672 (y >= rect.y) && (y < rect.y + rect.height))
2675 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2678 if (enter && in_arrow &&
2679 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2681 menu->upper_arrow_prelight = TRUE;
2682 menu->scroll_fast = scroll_fast;
2683 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2685 /* Deselect the active item so that any submenus are poped down */
2686 gtk_menu_shell_deselect (menu_shell);
2688 gtk_menu_remove_scroll_timeout (menu);
2689 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2690 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2691 gtk_menu_scroll_timeout,
2694 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2696 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2698 gtk_menu_stop_scrolling (menu);
2702 if (menu->lower_arrow_visible && !menu->tearoff_active)
2705 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2707 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2710 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2711 (y >= rect.y) && (y < rect.y + rect.height))
2714 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2717 if (enter && in_arrow &&
2718 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2720 menu->lower_arrow_prelight = TRUE;
2721 menu->scroll_fast = scroll_fast;
2722 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2724 /* Deselect the active item so that any submenus are poped down */
2725 gtk_menu_shell_deselect (menu_shell);
2727 gtk_menu_remove_scroll_timeout (menu);
2728 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2729 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2730 gtk_menu_scroll_timeout,
2733 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2735 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2737 gtk_menu_stop_scrolling (menu);
2743 gtk_menu_enter_notify (GtkWidget *widget,
2744 GdkEventCrossing *event)
2746 GtkWidget *menu_item;
2748 if (widget && GTK_IS_MENU (widget))
2750 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2752 if (!menu_shell->ignore_enter)
2753 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2756 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2757 * will not correspond to the event widget's parent. Check to see
2758 * if we are in the parent's navigation region.
2760 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2761 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2762 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2765 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2769 gtk_menu_leave_notify (GtkWidget *widget,
2770 GdkEventCrossing *event)
2772 GtkMenuShell *menu_shell;
2774 GtkMenuItem *menu_item;
2775 GtkWidget *event_widget;
2777 menu = GTK_MENU (widget);
2778 menu_shell = GTK_MENU_SHELL (widget);
2780 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2783 gtk_menu_handle_scrolling (menu, FALSE);
2785 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2787 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2790 menu_item = GTK_MENU_ITEM (event_widget);
2792 /* Here we check to see if we're leaving an active menu item with a submenu,
2793 * in which case we enter submenu navigation mode.
2795 if (menu_shell->active_menu_item != NULL
2796 && menu_item->submenu != NULL
2797 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2799 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2801 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2806 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2810 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2812 if (menu->navigation_region)
2814 gdk_region_destroy (menu->navigation_region);
2815 menu->navigation_region = NULL;
2818 if (menu->navigation_timeout)
2820 g_source_remove (menu->navigation_timeout);
2821 menu->navigation_timeout = 0;
2825 /* When the timeout is elapsed, the navigation region is destroyed
2826 * and the menuitem under the pointer (if any) is selected.
2829 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2831 GtkMenu *menu = user_data;
2832 GdkWindow *child_window;
2834 GDK_THREADS_ENTER ();
2836 gtk_menu_stop_navigating_submenu (menu);
2838 if (GTK_WIDGET_REALIZED (menu))
2840 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2844 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2846 send_event->crossing.window = g_object_ref (child_window);
2847 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2848 send_event->crossing.send_event = TRUE;
2850 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2852 gdk_event_free (send_event);
2856 GDK_THREADS_LEAVE ();
2862 gtk_menu_navigating_submenu (GtkMenu *menu,
2866 if (menu->navigation_region)
2868 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2872 gtk_menu_stop_navigating_submenu (menu);
2879 #undef DRAW_STAY_UP_TRIANGLE
2881 #ifdef DRAW_STAY_UP_TRIANGLE
2884 draw_stay_up_triangle (GdkWindow *window,
2887 /* Draw ugly color all over the stay-up triangle */
2888 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2889 GdkGCValues gc_values;
2891 GdkRectangle clipbox;
2893 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2894 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2895 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2896 gdk_gc_set_clip_region (ugly_gc, region);
2898 gdk_region_get_clipbox (region, &clipbox);
2900 gdk_draw_rectangle (window,
2903 clipbox.x, clipbox.y,
2904 clipbox.width, clipbox.height);
2906 g_object_unref (ugly_gc);
2911 flip_region (GdkRegion *region,
2916 GdkRectangle *rectangles;
2917 GdkRectangle clipbox;
2918 GdkRegion *new_region;
2921 new_region = gdk_region_new ();
2923 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
2924 gdk_region_get_clipbox (region, &clipbox);
2926 for (i = 0; i < n_rectangles; ++i)
2928 GdkRectangle rect = rectangles[i];
2931 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
2934 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
2936 gdk_region_union_with_rect (new_region, &rect);
2939 g_free (rectangles);
2945 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
2946 GtkMenuItem *menu_item,
2947 GdkEventCrossing *event)
2949 gint submenu_left = 0;
2950 gint submenu_right = 0;
2951 gint submenu_top = 0;
2952 gint submenu_bottom = 0;
2956 GtkWidget *event_widget;
2958 g_return_if_fail (menu_item->submenu != NULL);
2959 g_return_if_fail (event != NULL);
2961 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2963 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
2964 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
2966 submenu_right = submenu_left + width;
2967 submenu_bottom = submenu_top + height;
2969 gdk_drawable_get_size (event_widget->window, &width, &height);
2971 if (event->x >= 0 && event->x < width)
2974 gboolean flip_y = FALSE;
2975 gboolean flip_x = FALSE;
2977 gtk_menu_stop_navigating_submenu (menu);
2979 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2982 point[0].x = event->x_root;
2983 point[1].x = submenu_left;
2988 point[0].x = event->x_root + 1;
2989 point[1].x = 2 * (event->x_root + 1) - submenu_right;
2997 point[0].y = event->y_root + 1;
2998 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3000 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3008 point[0].y = event->y_root;
3009 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3011 if (point[0].y >= submenu_bottom)
3015 point[2].x = point[1].x;
3016 point[2].y = point[0].y;
3018 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3020 if (flip_x || flip_y)
3022 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3023 gdk_region_destroy (menu->navigation_region);
3024 menu->navigation_region = new_region;
3027 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3028 "gtk-menu-popdown-delay", &popdown_delay,
3031 menu->navigation_timeout = g_timeout_add (popdown_delay,
3032 gtk_menu_stop_navigating_submenu_cb, menu);
3034 #ifdef DRAW_STAY_UP_TRIANGLE
3035 draw_stay_up_triangle (gdk_get_default_root_window(),
3036 menu->navigation_region);
3042 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3046 g_return_if_fail (GTK_IS_MENU (menu_shell));
3048 parent = menu_shell->parent_menu_shell;
3050 menu_shell->activate_time = 0;
3051 gtk_menu_popdown (GTK_MENU (menu_shell));
3054 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3058 gtk_menu_position (GtkMenu *menu)
3061 GtkRequisition requisition;
3062 GtkMenuPrivate *private;
3068 GdkScreen *pointer_screen;
3069 GdkRectangle monitor;
3071 g_return_if_fail (GTK_IS_MENU (menu));
3073 widget = GTK_WIDGET (menu);
3075 screen = gtk_widget_get_screen (widget);
3076 gdk_display_get_pointer (gdk_screen_get_display (screen),
3077 &pointer_screen, &x, &y, NULL);
3079 /* We need the requisition to figure out the right place to
3080 * popup the menu. In fact, we always need to ask here, since
3081 * if a size_request was queued while we weren't popped up,
3082 * the requisition won't have been recomputed yet.
3084 gtk_widget_size_request (widget, &requisition);
3086 if (pointer_screen != screen)
3088 /* Pointer is on a different screen; roughly center the
3089 * menu on the screen. If someone was using multiscreen
3090 * + Xinerama together they'd probably want something
3091 * fancier; but that is likely to be vanishingly rare.
3093 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3094 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3097 private = gtk_menu_get_private (menu);
3098 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3102 if (menu->position_func)
3104 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3105 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3109 gint space_left, space_right, space_above, space_below;
3112 gint xthickness = widget->style->xthickness;
3113 gint ythickness = widget->style->ythickness;
3114 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3116 /* The placement of popup menus horizontally works like this (with
3117 * RTL in parentheses)
3119 * - If there is enough room to the right (left) of the mouse cursor,
3120 * position the menu there.
3122 * - Otherwise, if if there is enough room to the left (right) of the
3123 * mouse cursor, position the menu there.
3125 * - Otherwise if the menu is smaller than the monitor, position it
3126 * on the side of the mouse cursor that has the most space available
3128 * - Otherwise (if there is simply not enough room for the menu on the
3129 * monitor), position it as far left (right) as possible.
3131 * Positioning in the vertical direction is similar: first try below
3132 * mouse cursor, then above.
3134 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3136 space_left = x - monitor.x;
3137 space_right = monitor.x + monitor.width - x - 1;
3138 space_above = y - monitor.y;
3139 space_below = monitor.y + monitor.height - y - 1;
3141 /* position horizontally */
3143 /* the amount of space we need to position the menu. Note the
3144 * menu is offset "xthickness" pixels
3146 needed_width = requisition.width - xthickness;
3148 if (needed_width <= space_left ||
3149 needed_width <= space_right)
3151 if ((rtl && needed_width <= space_left) ||
3152 (!rtl && needed_width > space_right))
3155 x = x + xthickness - requisition.width + 1;
3159 /* position right */
3163 /* x is clamped on-screen further down */
3165 else if (requisition.width <= monitor.width)
3167 /* the menu is too big to fit on either side of the mouse
3168 * cursor, but smaller than the monitor. Position it on
3169 * the side that has the most space
3171 if (space_left > space_right)
3179 x = monitor.x + monitor.width - requisition.width;
3182 else /* menu is simply too big for the monitor */
3187 x = monitor.x + monitor.width - requisition.width;
3196 /* Position vertically. The algorithm is the same as above, but
3197 * simpler because we don't have to take RTL into account.
3199 needed_height = requisition.height - ythickness;
3201 if (needed_height <= space_above ||
3202 needed_height <= space_below)
3204 if (needed_height <= space_below)
3207 y = y + ythickness - requisition.height + 1;
3209 y = CLAMP (y, monitor.y,
3210 monitor.y + monitor.height - requisition.height);
3212 else if (needed_height > space_below && needed_height > space_above)
3214 if (space_below >= space_above)
3215 y = monitor.y + monitor.height - requisition.height;
3229 menu_height = GTK_WIDGET (menu)->requisition.height;
3231 if (y + menu_height > monitor.y + monitor.height)
3233 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3234 y = (monitor.y + monitor.height) - menu_height;
3239 scroll_offset += monitor.y - y;
3244 /* FIXME: should this be done in the various position_funcs ? */
3245 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3247 if (GTK_MENU_SHELL (menu)->active)
3249 private->have_position = TRUE;
3254 if (y + requisition.height > monitor.y + monitor.height)
3255 requisition.height = (monitor.y + monitor.height) - y;
3259 scroll_offset += monitor.y - y;
3260 requisition.height -= monitor.y - y;
3264 if (scroll_offset > 0)
3265 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3267 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3270 if (!GTK_MENU_SHELL (menu)->active)
3272 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3273 requisition.width, requisition.height);
3276 menu->scroll_offset = scroll_offset;
3280 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3282 if (menu->timeout_id)
3284 g_source_remove (menu->timeout_id);
3285 menu->timeout_id = 0;
3290 gtk_menu_stop_scrolling (GtkMenu *menu)
3292 gtk_menu_remove_scroll_timeout (menu);
3294 menu->upper_arrow_prelight = FALSE;
3295 menu->lower_arrow_prelight = FALSE;
3299 gtk_menu_scroll_to (GtkMenu *menu,
3304 gint view_width, view_height;
3306 gboolean last_visible;
3308 guint vertical_padding;
3310 widget = GTK_WIDGET (menu);
3312 if (menu->tearoff_active &&
3313 menu->tearoff_adjustment &&
3314 (menu->tearoff_adjustment->value != offset))
3316 menu->tearoff_adjustment->value =
3318 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3319 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3322 /* Move/resize the viewport according to arrows: */
3323 view_width = widget->allocation.width;
3324 view_height = widget->allocation.height;
3326 gtk_widget_style_get (GTK_WIDGET (menu),
3327 "vertical-padding", &vertical_padding,
3330 border_width = GTK_CONTAINER (menu)->border_width;
3331 view_width -= (border_width + widget->style->xthickness) * 2;
3332 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3333 menu_height = widget->requisition.height -
3334 (border_width + widget->style->ythickness + vertical_padding) * 2;
3336 x = border_width + widget->style->xthickness;
3337 y = border_width + widget->style->ythickness + vertical_padding;
3339 if (!menu->tearoff_active)
3341 last_visible = menu->upper_arrow_visible;
3342 menu->upper_arrow_visible = offset > 0;
3344 if (menu->upper_arrow_visible)
3345 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3347 if ( (last_visible != menu->upper_arrow_visible) &&
3348 !menu->upper_arrow_visible)
3350 menu->upper_arrow_prelight = FALSE;
3352 /* If we hid the upper arrow, possibly remove timeout */
3353 if (menu->scroll_step < 0)
3355 gtk_menu_stop_scrolling (menu);
3356 gtk_widget_queue_draw (GTK_WIDGET (menu));
3360 last_visible = menu->lower_arrow_visible;
3361 menu->lower_arrow_visible = offset < menu_height - view_height;
3363 if (menu->lower_arrow_visible)
3364 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3366 if ( (last_visible != menu->lower_arrow_visible) &&
3367 !menu->lower_arrow_visible)
3369 menu->lower_arrow_prelight = FALSE;
3371 /* If we hid the lower arrow, possibly remove timeout */
3372 if (menu->scroll_step > 0)
3374 gtk_menu_stop_scrolling (menu);
3375 gtk_widget_queue_draw (GTK_WIDGET (menu));
3379 if (menu->upper_arrow_visible)
3380 y += MENU_SCROLL_ARROW_HEIGHT;
3383 /* Scroll the menu: */
3384 if (GTK_WIDGET_REALIZED (menu))
3385 gdk_window_move (menu->bin_window, 0, -offset);
3387 if (GTK_WIDGET_REALIZED (menu))
3388 gdk_window_move_resize (menu->view_window,
3394 menu->scroll_offset = offset;
3398 compute_child_offset (GtkMenu *menu,
3399 GtkWidget *menu_item,
3402 gboolean *is_last_child)
3404 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3405 guint item_top_attach;
3406 guint item_bottom_attach;
3407 gint child_offset = 0;
3410 gtk_container_child_get (GTK_CONTAINER (menu), menu_item,
3411 "top_attach", &item_top_attach,
3412 "bottom_attach", &item_bottom_attach,
3415 /* there is a possibility that we get called before _size_request, so
3416 * check the height table for safety.
3418 if (!priv->heights || priv->heights_length < priv->rows)
3421 /* when we have a row with only invisible children, it's height will
3422 * be zero, so there's no need to check WIDGET_VISIBLE here
3424 for (i = 0; i < item_top_attach; i++)
3425 child_offset += priv->heights[i];
3428 *is_last_child = (item_bottom_attach == priv->rows);
3430 *offset = child_offset;
3432 *height = priv->heights[item_top_attach];
3438 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
3439 GtkWidget *menu_item)
3442 gint child_offset, child_height;
3446 gboolean last_child = 0;
3448 menu = GTK_MENU (menu_shell);
3450 /* We need to check if the selected item fully visible.
3451 * If not we need to scroll the menu so that it becomes fully
3455 if (compute_child_offset (menu, menu_item,
3456 &child_offset, &child_height, &last_child))
3458 guint vertical_padding;
3460 y = menu->scroll_offset;
3461 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3463 gtk_widget_style_get (GTK_WIDGET (menu),
3464 "vertical-padding", &vertical_padding,
3467 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3469 if (child_offset < y)
3471 /* Ignore the enter event we might get if the pointer is on the menu
3473 menu_shell->ignore_enter = TRUE;
3474 gtk_menu_scroll_to (menu, child_offset);
3479 if (menu->upper_arrow_visible && !menu->tearoff_active)
3480 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3481 if (menu->lower_arrow_visible && !menu->tearoff_active)
3482 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3484 if (child_offset + child_height > y + height - arrow_height)
3487 if (!last_child && !menu->tearoff_active)
3488 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3490 y = child_offset + child_height - height + arrow_height;
3491 if ((y > 0) && !menu->tearoff_active)
3493 /* Need upper arrow */
3494 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3495 y = child_offset + child_height - height + arrow_height;
3497 /* Ignore the enter event we might get if the pointer is on the menu
3499 menu_shell->ignore_enter = TRUE;
3500 gtk_menu_scroll_to (menu, y);
3508 gtk_menu_select_item (GtkMenuShell *menu_shell,
3509 GtkWidget *menu_item)
3511 GtkMenu *menu = GTK_MENU (menu_shell);
3513 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3514 gtk_menu_scroll_item_visible (menu_shell, menu_item);
3516 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3520 /* Reparent the menu, taking care of the refcounting
3522 * If unrealize is true we force a unrealize while reparenting the parent.
3523 * This can help eliminate flicker in some cases.
3525 * What happens is that when the menu is unrealized and then re-realized,
3526 * the allocations are as follows:
3528 * parent - 1x1 at (0,0)
3529 * child1 - 100x20 at (0,0)
3530 * child2 - 100x20 at (0,20)
3531 * child3 - 100x20 at (0,40)
3533 * That is, the parent is small but the children are full sized. Then,
3534 * when the queued_resize gets processed, the parent gets resized to
3537 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3538 * contains the following logic:
3540 * - if a move or resize operation on a window would change the clip
3541 * region on the children, then before the window is resized
3542 * the background for children is temporarily set to None, the
3543 * move/resize done, and the background for the children restored.
3545 * So, at the point where the parent is resized to final size, the
3546 * background for the children is temporarily None, and thus they
3547 * are not cleared to the background color and the previous background
3548 * (the image of the menu) is left in place.
3551 gtk_menu_reparent (GtkMenu *menu,
3552 GtkWidget *new_parent,
3555 GtkObject *object = GTK_OBJECT (menu);
3556 GtkWidget *widget = GTK_WIDGET (menu);
3557 gboolean was_floating = GTK_OBJECT_FLOATING (object);
3559 g_object_ref (object);
3560 gtk_object_sink (object);
3564 g_object_ref (object);
3565 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3566 gtk_container_add (GTK_CONTAINER (new_parent), widget);
3567 g_object_unref (object);
3570 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3573 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3575 g_object_unref (object);
3579 gtk_menu_show_all (GtkWidget *widget)
3581 /* Show children, but not self. */
3582 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3587 gtk_menu_hide_all (GtkWidget *widget)
3589 /* Hide children, but not self. */
3590 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3594 * gtk_menu_set_screen:
3595 * @menu: a #GtkMenu.
3596 * @screen: a #GdkScreen, or %NULL if the screen should be
3597 * determined by the widget the menu is attached to.
3599 * Sets the #GdkScreen on which the menu will be displayed.
3604 gtk_menu_set_screen (GtkMenu *menu,
3607 g_return_if_fail (GTK_IS_MENU (menu));
3608 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3610 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3614 menu_change_screen (menu, screen);
3618 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3620 attach_widget_screen_changed (attach_widget, NULL, menu);
3626 * @menu: a #GtkMenu.
3627 * @child: a #GtkMenuItem.
3628 * @left_attach: The column number to attach the left side of the item to.
3629 * @right_attach: The column number to attach the right side of the item to.
3630 * @top_attach: The row number to attach the top of the item to.
3631 * @bottom_attach: The row number to attach the bottom of the item to.
3633 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3634 * an item will occupy is specified by @left_attach, @right_attach,
3635 * @top_attach and @bottom_attach. These each represent the leftmost,
3636 * rightmost, uppermost and lower column and row numbers of the table.
3637 * (Columns and rows are indexed from zero).
3642 gtk_menu_attach (GtkMenu *menu,
3647 guint bottom_attach)
3649 g_return_if_fail (GTK_IS_MENU (menu));
3650 g_return_if_fail (GTK_IS_MENU_ITEM (child));
3652 g_return_if_fail (left_attach < right_attach);
3653 g_return_if_fail (top_attach < bottom_attach);
3656 gtk_menu_shell_append (GTK_MENU_SHELL (menu), child);
3658 gtk_container_child_set (GTK_CONTAINER (menu), child,
3659 "left_attach", left_attach,
3660 "right_attach", right_attach,
3661 "top_attach", top_attach,
3662 "bottom_attach", bottom_attach,
3667 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
3671 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
3672 "gtk-menu-popup-delay", &popup_delay,
3679 find_child_containing (GtkMenuShell *menu_shell,
3687 /* find a child which includes the area given by
3688 * left, right, top, bottom.
3691 for (list = menu_shell->children; list; list = list->next)
3695 if (!_gtk_menu_item_is_selectable (list->data))
3698 get_child_attach (list->data, &l, &r, &t, &b);
3700 if (l <= left && right <= r
3701 && t <= top && bottom <= b)
3702 return GTK_WIDGET (list->data);
3709 gtk_menu_move_current (GtkMenuShell *menu_shell,
3710 GtkMenuDirectionType direction)
3712 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu_shell));
3714 /* use special table menu key bindings */
3715 if (menu_shell->active_menu_item && priv->columns > 1)
3719 gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
3720 GtkWidget *match = NULL;
3722 get_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
3724 if (direction == GTK_MENU_DIR_NEXT)
3726 for (i = b; i < priv->rows; i++)
3728 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
3736 for (i = 0; i < t; i++)
3738 match = find_child_containing (menu_shell,
3739 l, l + 1, i, i + 1);
3745 else if (direction == GTK_MENU_DIR_PREV)
3747 for (i = t; i > 0; i--)
3749 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
3757 for (i = priv->rows; i > b; i--)
3759 match = find_child_containing (menu_shell,
3760 l, l + 1, i - 1, i);
3766 else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
3767 || (rtl && direction == GTK_MENU_DIR_CHILD))
3769 /* we go one left if possible */
3771 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
3775 GtkWidget *parent = menu_shell->parent_menu_shell;
3778 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
3779 match = menu_shell->active_menu_item;
3782 else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
3783 || (rtl && direction == GTK_MENU_DIR_PARENT))
3785 /* we go one right if possible */
3786 if (r < priv->columns)
3787 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
3791 GtkWidget *parent = menu_shell->parent_menu_shell;
3793 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
3795 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
3796 match = menu_shell->active_menu_item;
3802 gtk_menu_shell_select_item (menu_shell, match);
3807 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
3811 get_visible_size (GtkMenu *menu)
3813 GtkWidget *widget = GTK_WIDGET (menu);
3814 GtkContainer *container = GTK_CONTAINER (menu);
3816 gint menu_height = (widget->allocation.height
3817 - 2 * (container->border_width
3818 + widget->style->ythickness));
3820 if (menu->upper_arrow_visible && !menu->tearoff_active)
3821 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3822 if (menu->lower_arrow_visible && !menu->tearoff_active)
3823 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3828 /* Find the sensitive on-screen child containing @y, or if none,
3829 * the nearest selectable onscreen child. (%NULL if none)
3832 child_at (GtkMenu *menu,
3835 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3836 GtkWidget *child = NULL;
3837 gint child_offset = 0;
3840 gint lower, upper; /* Onscreen bounds */
3842 menu_height = get_visible_size (menu);
3843 lower = menu->scroll_offset;
3844 upper = menu->scroll_offset + menu_height;
3846 for (children = menu_shell->children; children; children = children->next)
3848 if (GTK_WIDGET_VISIBLE (children->data))
3850 GtkRequisition child_requisition;
3852 gtk_widget_size_request (children->data, &child_requisition);
3854 if (_gtk_menu_item_is_selectable (children->data) &&
3855 child_offset >= lower &&
3856 child_offset + child_requisition.height <= upper)
3858 child = children->data;
3860 if (child_offset + child_requisition.height > y &&
3861 !GTK_IS_TEAROFF_MENU_ITEM (child))
3865 child_offset += child_requisition.height;
3873 get_menu_height (GtkMenu *menu)
3876 GtkWidget *widget = GTK_WIDGET (menu);
3878 height = widget->requisition.height;
3879 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
3881 if (menu->upper_arrow_visible && !menu->tearoff_active)
3882 height -= MENU_SCROLL_ARROW_HEIGHT;
3884 if (menu->lower_arrow_visible && !menu->tearoff_active)
3885 height -= MENU_SCROLL_ARROW_HEIGHT;
3891 gtk_menu_real_move_scroll (GtkMenu *menu,
3894 gint page_size = get_visible_size (menu);
3895 gint end_position = get_menu_height (menu);
3896 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3900 case GTK_SCROLL_PAGE_UP:
3901 case GTK_SCROLL_PAGE_DOWN:
3905 gint child_offset = 0;
3906 gboolean old_upper_arrow_visible;
3909 if (type == GTK_SCROLL_PAGE_UP)
3914 if (menu_shell->active_menu_item)
3918 compute_child_offset (menu, menu_shell->active_menu_item,
3919 &child_offset, &child_height, NULL);
3920 child_offset += child_height / 2;
3923 menu_shell->ignore_enter = TRUE;
3924 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
3925 old_offset = menu->scroll_offset;
3927 new_offset = menu->scroll_offset + step;
3928 new_offset = CLAMP (new_offset, 0, end_position - page_size);
3930 gtk_menu_scroll_to (menu, new_offset);
3932 if (menu_shell->active_menu_item)
3934 GtkWidget *new_child;
3935 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
3937 if (menu->scroll_offset != old_offset)
3938 step = menu->scroll_offset - old_offset;
3940 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
3942 new_child = child_at (menu, child_offset + step);
3944 gtk_menu_shell_select_item (menu_shell, new_child);
3948 case GTK_SCROLL_START:
3949 /* Ignore the enter event we might get if the pointer is on the menu
3951 menu_shell->ignore_enter = TRUE;
3952 gtk_menu_scroll_to (menu, 0);
3953 gtk_menu_shell_select_first (menu_shell, TRUE);
3955 case GTK_SCROLL_END:
3956 /* Ignore the enter event we might get if the pointer is on the menu
3958 menu_shell->ignore_enter = TRUE;
3959 gtk_menu_scroll_to (menu, end_position - page_size);
3960 _gtk_menu_shell_select_last (menu_shell, TRUE);
3969 * gtk_menu_set_monitor:
3971 * @monitor_num: the number of the monitor on which the menu should
3974 * Informs GTK+ on which monitor a menu should be popped up.
3975 * See gdk_screen_get_monitor_geometry().
3977 * This function should be called from a #GtkMenuPositionFunc if the
3978 * menu should not appear on the same monitor as the pointer. This
3979 * information can't be reliably inferred from the coordinates returned
3980 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
3981 * may extend beyond the monitor boundaries or even the screen boundaries.
3985 void gtk_menu_set_monitor (GtkMenu *menu,
3988 GtkMenuPrivate *priv;
3989 g_return_if_fail (GTK_IS_MENU (menu));
3991 priv = gtk_menu_get_private (menu);
3993 priv->monitor_num = monitor_num;