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 225
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_do_insert (GtkMenuShell *menu_shell,
178 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
181 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
183 static void gtk_menu_handle_scrolling (GtkMenu *menu,
185 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
187 static void gtk_menu_style_set (GtkWidget *widget,
188 GtkStyle *previous_style);
189 static gboolean gtk_menu_focus (GtkWidget *widget,
190 GtkDirectionType direction);
191 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
192 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
193 GtkMenuDirectionType direction);
194 static void gtk_menu_real_move_scroll (GtkMenu *menu,
197 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
198 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
199 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
202 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
203 GtkMenuItem *menu_item,
204 GdkEventCrossing *event);
206 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
207 static void gtk_menu_show_all (GtkWidget *widget);
208 static void gtk_menu_hide_all (GtkWidget *widget);
209 static void gtk_menu_position (GtkMenu *menu);
210 static void gtk_menu_reparent (GtkMenu *menu,
211 GtkWidget *new_parent,
213 static void gtk_menu_remove (GtkContainer *menu,
216 static void gtk_menu_update_title (GtkMenu *menu);
218 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
219 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
221 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
223 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
224 gboolean group_changed);
226 static GtkMenuShellClass *parent_class = NULL;
227 static const gchar *attach_data_key = "gtk-menu-attach-data";
229 static guint menu_signals[LAST_SIGNAL] = { 0 };
232 gtk_menu_free_private (gpointer data)
234 GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
236 g_free (priv->heights);
242 gtk_menu_get_private (GtkMenu *menu)
244 GtkMenuPrivate *private;
245 static GQuark private_quark = 0;
248 private_quark = g_quark_from_static_string ("gtk-menu-private");
250 private = g_object_get_qdata (G_OBJECT (menu), private_quark);
254 private = g_new0 (GtkMenuPrivate, 1);
255 private->have_position = FALSE;
257 g_object_set_qdata_full (G_OBJECT (menu), private_quark,
258 private, gtk_menu_free_private);
265 gtk_menu_get_type (void)
267 static GType menu_type = 0;
271 static const GTypeInfo menu_info =
273 sizeof (GtkMenuClass),
274 NULL, /* base_init */
275 NULL, /* base_finalize */
276 (GClassInitFunc) gtk_menu_class_init,
277 NULL, /* class_finalize */
278 NULL, /* class_data */
281 (GInstanceInitFunc) gtk_menu_init,
284 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
292 gtk_menu_class_init (GtkMenuClass *class)
294 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
295 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
296 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
297 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
298 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
299 GtkBindingSet *binding_set;
301 parent_class = g_type_class_peek_parent (class);
303 gobject_class->finalize = gtk_menu_finalize;
304 gobject_class->set_property = gtk_menu_set_property;
305 gobject_class->get_property = gtk_menu_get_property;
307 object_class->destroy = gtk_menu_destroy;
309 widget_class->realize = gtk_menu_realize;
310 widget_class->unrealize = gtk_menu_unrealize;
311 widget_class->size_request = gtk_menu_size_request;
312 widget_class->size_allocate = gtk_menu_size_allocate;
313 widget_class->show = gtk_menu_show;
314 widget_class->expose_event = gtk_menu_expose;
315 widget_class->key_press_event = gtk_menu_key_press;
316 widget_class->button_press_event = gtk_menu_button_press;
317 widget_class->button_release_event = gtk_menu_button_release;
318 widget_class->motion_notify_event = gtk_menu_motion_notify;
319 widget_class->show_all = gtk_menu_show_all;
320 widget_class->hide_all = gtk_menu_hide_all;
321 widget_class->enter_notify_event = gtk_menu_enter_notify;
322 widget_class->leave_notify_event = gtk_menu_leave_notify;
323 widget_class->motion_notify_event = gtk_menu_motion_notify;
324 widget_class->style_set = gtk_menu_style_set;
325 widget_class->focus = gtk_menu_focus;
326 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
328 container_class->remove = gtk_menu_remove;
329 container_class->get_child_property = gtk_menu_get_child_property;
330 container_class->set_child_property = gtk_menu_set_child_property;
332 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
333 menu_shell_class->deactivate = gtk_menu_deactivate;
334 menu_shell_class->select_item = gtk_menu_select_item;
335 menu_shell_class->insert = gtk_menu_real_insert;
336 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
337 menu_shell_class->move_current = gtk_menu_move_current;
339 menu_signals[MOVE_SCROLL] =
340 _gtk_binding_signal_new ("move_scroll",
341 G_OBJECT_CLASS_TYPE (object_class),
342 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
343 G_CALLBACK (gtk_menu_real_move_scroll),
345 _gtk_marshal_VOID__ENUM,
347 GTK_TYPE_SCROLL_TYPE);
349 g_object_class_install_property (gobject_class,
351 g_param_spec_string ("tearoff-title",
353 P_("A title that may be displayed by the window manager when this menu is torn-off"),
355 G_PARAM_READABLE | G_PARAM_WRITABLE));
357 gtk_widget_class_install_style_property (widget_class,
358 g_param_spec_int ("vertical-padding",
359 P_("Vertical Padding"),
360 P_("Extra space at the top and bottom of the menu"),
366 gtk_widget_class_install_style_property (widget_class,
367 g_param_spec_int ("vertical-offset",
368 P_("Vertical Offset"),
369 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
375 gtk_widget_class_install_style_property (widget_class,
376 g_param_spec_int ("horizontal-offset",
377 P_("Horizontal Offset"),
378 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
385 gtk_container_class_install_child_property (container_class,
386 CHILD_PROP_LEFT_ATTACH,
387 g_param_spec_uint ("left_attach",
389 P_("The column number to attach the left side of the child to"),
393 gtk_container_class_install_child_property (container_class,
394 CHILD_PROP_RIGHT_ATTACH,
395 g_param_spec_uint ("right_attach",
397 P_("The column number to attach the right side of the child to"),
401 gtk_container_class_install_child_property (container_class,
402 CHILD_PROP_TOP_ATTACH,
403 g_param_spec_uint ("top_attach",
405 P_("The row number to attach the top of the child to"),
409 gtk_container_class_install_child_property (container_class,
410 CHILD_PROP_BOTTOM_ATTACH,
411 g_param_spec_uint ("bottom_attach",
413 P_("The row number to attach the bottom of the child to"),
417 binding_set = gtk_binding_set_by_class (class);
418 gtk_binding_entry_add_signal (binding_set,
421 GTK_TYPE_MENU_DIRECTION_TYPE,
423 gtk_binding_entry_add_signal (binding_set,
426 GTK_TYPE_MENU_DIRECTION_TYPE,
428 gtk_binding_entry_add_signal (binding_set,
431 GTK_TYPE_MENU_DIRECTION_TYPE,
433 gtk_binding_entry_add_signal (binding_set,
436 GTK_TYPE_MENU_DIRECTION_TYPE,
438 gtk_binding_entry_add_signal (binding_set,
441 GTK_TYPE_MENU_DIRECTION_TYPE,
442 GTK_MENU_DIR_PARENT);
443 gtk_binding_entry_add_signal (binding_set,
446 GTK_TYPE_MENU_DIRECTION_TYPE,
447 GTK_MENU_DIR_PARENT);
448 gtk_binding_entry_add_signal (binding_set,
451 GTK_TYPE_MENU_DIRECTION_TYPE,
453 gtk_binding_entry_add_signal (binding_set,
456 GTK_TYPE_MENU_DIRECTION_TYPE,
458 gtk_binding_entry_add_signal (binding_set,
461 GTK_TYPE_SCROLL_TYPE,
463 gtk_binding_entry_add_signal (binding_set,
466 GTK_TYPE_SCROLL_TYPE,
468 gtk_binding_entry_add_signal (binding_set,
471 GTK_TYPE_SCROLL_TYPE,
473 gtk_binding_entry_add_signal (binding_set,
476 GTK_TYPE_SCROLL_TYPE,
478 gtk_binding_entry_add_signal (binding_set,
481 GTK_TYPE_SCROLL_TYPE,
483 gtk_binding_entry_add_signal (binding_set,
486 GTK_TYPE_SCROLL_TYPE,
488 gtk_binding_entry_add_signal (binding_set,
491 GTK_TYPE_SCROLL_TYPE,
492 GTK_SCROLL_PAGE_DOWN);
493 gtk_binding_entry_add_signal (binding_set,
496 GTK_TYPE_SCROLL_TYPE,
497 GTK_SCROLL_PAGE_DOWN);
499 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
500 P_("Can change accelerators"),
501 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
505 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
506 P_("Delay before submenus appear"),
507 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
513 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
514 P_("Delay before hiding a submenu"),
515 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
518 DEFAULT_POPDOWN_DELAY,
525 gtk_menu_set_property (GObject *object,
532 menu = GTK_MENU (object);
536 case PROP_TEAROFF_TITLE:
537 gtk_menu_set_title (menu, g_value_get_string (value));
540 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
546 gtk_menu_get_property (GObject *object,
553 menu = GTK_MENU (object);
557 case PROP_TEAROFF_TITLE:
558 g_value_set_string (value, gtk_menu_get_title (menu));
561 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
567 get_attach_info (GObject *child)
569 AttachInfo *ai = g_object_get_data (child, ATTACH_INFO_KEY);
573 ai = g_new0 (AttachInfo, 1);
574 g_object_set_data_full (child, ATTACH_INFO_KEY, ai, g_free);
581 gtk_menu_set_child_property (GtkContainer *container,
587 GtkMenu *menu = GTK_MENU (container);
588 GtkMenuPrivate *priv;
589 AttachInfo *ai = get_attach_info (G_OBJECT (child));
591 priv = gtk_menu_get_private (menu);
595 case CHILD_PROP_LEFT_ATTACH:
596 ai->left_attach = g_value_get_uint (value);
598 case CHILD_PROP_RIGHT_ATTACH:
599 ai->right_attach = g_value_get_uint (value);
600 priv->columns = MAX (priv->columns, ai->right_attach);
602 case CHILD_PROP_TOP_ATTACH:
603 ai->top_attach = g_value_get_uint (value);
605 case CHILD_PROP_BOTTOM_ATTACH:
606 ai->bottom_attach = g_value_get_uint (value);
607 priv->rows = MAX (priv->rows, ai->bottom_attach);
610 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
614 gtk_widget_queue_resize (GTK_WIDGET (menu));
618 gtk_menu_get_child_property (GtkContainer *container,
624 AttachInfo *ai = get_attach_info (G_OBJECT (child));
628 case CHILD_PROP_LEFT_ATTACH:
629 g_value_set_uint (value, ai->left_attach);
631 case CHILD_PROP_RIGHT_ATTACH:
632 g_value_set_uint (value, ai->right_attach);
634 case CHILD_PROP_TOP_ATTACH:
635 g_value_set_uint (value, ai->top_attach);
637 case CHILD_PROP_BOTTOM_ATTACH:
638 g_value_set_uint (value, ai->bottom_attach);
642 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
648 get_child_attach (GtkWidget *child,
654 gtk_container_child_get (GTK_CONTAINER (child->parent), child,
663 gtk_menu_window_event (GtkWidget *window,
667 gboolean handled = FALSE;
669 g_object_ref (window);
675 case GDK_KEY_RELEASE:
676 handled = gtk_widget_event (menu, event);
682 g_object_unref (window);
683 g_object_unref (menu);
689 gtk_menu_window_size_request (GtkWidget *window,
690 GtkRequisition *requisition,
693 GtkMenuPrivate *private = gtk_menu_get_private (menu);
695 if (private->have_position)
697 GdkScreen *screen = gtk_widget_get_screen (window);
698 GdkRectangle monitor;
701 monitor_num = gdk_screen_get_monitor_at_point (screen,
702 private->x, private->y);
703 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
705 if (private->y + requisition->height > monitor.y + monitor.height)
706 requisition->height = monitor.y + monitor.height - private->y;
708 if (private->y < monitor.y)
709 requisition->height -= monitor.y - private->y;
714 gtk_menu_init (GtkMenu *menu)
716 menu->parent_menu_item = NULL;
717 menu->old_active_menu_item = NULL;
718 menu->accel_group = NULL;
719 menu->position_func = NULL;
720 menu->position_func_data = NULL;
721 menu->toggle_size = 0;
723 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
724 "type", GTK_WINDOW_POPUP,
727 "signal::event", gtk_menu_window_event, menu,
728 "signal::size_request", gtk_menu_window_size_request, menu,
729 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
731 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
732 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
734 /* Refloat the menu, so that reference counting for the menu isn't
735 * affected by it being a child of the toplevel
737 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
738 menu->needs_destruction_ref_count = TRUE;
740 menu->view_window = NULL;
741 menu->bin_window = NULL;
743 menu->scroll_offset = 0;
744 menu->scroll_step = 0;
745 menu->timeout_id = 0;
746 menu->scroll_fast = FALSE;
748 menu->tearoff_window = NULL;
749 menu->tearoff_hbox = NULL;
750 menu->torn_off = FALSE;
751 menu->tearoff_active = FALSE;
752 menu->tearoff_adjustment = NULL;
753 menu->tearoff_scrollbar = NULL;
755 menu->upper_arrow_visible = FALSE;
756 menu->lower_arrow_visible = FALSE;
757 menu->upper_arrow_prelight = FALSE;
758 menu->lower_arrow_prelight = FALSE;
760 MENU_NEEDS_RESIZE (menu) = TRUE;
764 gtk_menu_destroy (GtkObject *object)
767 GtkMenuAttachData *data;
769 g_return_if_fail (GTK_IS_MENU (object));
771 menu = GTK_MENU (object);
773 gtk_menu_stop_scrolling (menu);
775 data = g_object_get_data (G_OBJECT (object), attach_data_key);
777 gtk_menu_detach (menu);
779 gtk_menu_stop_navigating_submenu (menu);
781 if (menu->old_active_menu_item)
783 g_object_unref (menu->old_active_menu_item);
784 menu->old_active_menu_item = NULL;
787 /* Add back the reference count for being a child */
788 if (menu->needs_destruction_ref_count)
790 menu->needs_destruction_ref_count = FALSE;
791 g_object_ref (object);
794 if (menu->accel_group)
796 g_object_unref (menu->accel_group);
797 menu->accel_group = NULL;
801 gtk_widget_destroy (menu->toplevel);
802 if (menu->tearoff_window)
803 gtk_widget_destroy (menu->tearoff_window);
805 GTK_OBJECT_CLASS (parent_class)->destroy (object);
809 gtk_menu_finalize (GObject *object)
811 GtkMenu *menu = GTK_MENU (object);
813 g_free (menu->accel_path);
815 G_OBJECT_CLASS (parent_class)->finalize (object);
819 menu_change_screen (GtkMenu *menu,
820 GdkScreen *new_screen)
824 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
825 gtk_menu_position (menu);
828 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
832 attach_widget_screen_changed (GtkWidget *attach_widget,
833 GdkScreen *previous_screen,
836 if (gtk_widget_has_screen (attach_widget) &&
837 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
839 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
844 gtk_menu_attach_to_widget (GtkMenu *menu,
845 GtkWidget *attach_widget,
846 GtkMenuDetachFunc detacher)
848 GtkMenuAttachData *data;
850 g_return_if_fail (GTK_IS_MENU (menu));
851 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
852 g_return_if_fail (detacher != NULL);
854 /* keep this function in sync with gtk_widget_set_parent()
857 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
860 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
861 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
866 gtk_object_sink (GTK_OBJECT (menu));
868 data = g_new (GtkMenuAttachData, 1);
869 data->attach_widget = attach_widget;
871 g_signal_connect (attach_widget, "screen_changed",
872 G_CALLBACK (attach_widget_screen_changed), menu);
873 attach_widget_screen_changed (attach_widget, NULL, menu);
875 data->detacher = detacher;
876 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
878 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
879 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
881 /* we don't need to set the style here, since
882 * we are a toplevel widget.
885 /* Fallback title for menu comes from attach widget */
886 gtk_menu_update_title (menu);
890 gtk_menu_get_attach_widget (GtkMenu *menu)
892 GtkMenuAttachData *data;
894 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
896 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
898 return data->attach_widget;
903 gtk_menu_detach (GtkMenu *menu)
905 GtkMenuAttachData *data;
907 g_return_if_fail (GTK_IS_MENU (menu));
909 /* keep this function in sync with gtk_widget_unparent()
911 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
914 g_warning ("gtk_menu_detach(): menu is not attached");
917 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
919 g_signal_handlers_disconnect_by_func (data->attach_widget,
920 (gpointer) attach_widget_screen_changed,
923 data->detacher (data->attach_widget, menu);
925 if (GTK_WIDGET_REALIZED (menu))
926 gtk_widget_unrealize (GTK_WIDGET (menu));
930 /* Fallback title for menu comes from attach widget */
931 gtk_menu_update_title (menu);
933 g_object_unref (menu);
937 gtk_menu_do_remove (GtkMenuShell *menu_shell,
942 GtkMenuPrivate *priv;
944 gboolean single_column;
946 ai = get_attach_info (G_OBJECT (child));
947 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
948 delta = ai->bottom_attach - ai->top_attach;
949 single_column = priv->columns == 1;
951 /* recalculate these, assuming the child has already been removed */
955 for (children = menu_shell->children; children; children = children->next)
957 AttachInfo *child_ai = get_attach_info (children->data);
959 /* Do not move items in table menus */
960 if (single_column && child_ai->bottom_attach > ai->bottom_attach)
961 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
962 "top_attach", child_ai->top_attach - delta,
963 "bottom_attach", child_ai->bottom_attach - delta,
967 priv->columns = MAX (priv->columns, child_ai->right_attach);
968 priv->rows = MAX (priv->rows, child_ai->bottom_attach);
974 gtk_menu_remove (GtkContainer *container,
979 g_return_if_fail (GTK_IS_MENU (container));
980 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
982 menu = GTK_MENU (container);
984 /* Clear out old_active_menu_item if it matches the item we are removing
986 if (menu->old_active_menu_item == widget)
988 g_object_unref (menu->old_active_menu_item);
989 menu->old_active_menu_item = NULL;
992 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
993 gtk_menu_do_remove (GTK_MENU_SHELL (container), widget);
994 g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
1001 return g_object_new (GTK_TYPE_MENU, NULL);
1005 gtk_menu_do_insert (GtkMenuShell *menu_shell,
1010 GtkMenuPrivate *priv;
1012 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
1014 if (position < 0 || position >= priv->rows)
1016 /* attach after the last row */
1017 gtk_menu_attach (GTK_MENU (menu_shell), child,
1018 0, priv->columns ? priv->columns : 1,
1019 priv->rows, priv->rows + 1);
1024 /* We need to make space for this new item; move all items with
1025 * top >= position one down.
1026 * Note that this does not prevent overlaps when the menu contains
1027 * vertically spanning items.
1029 for (children = menu_shell->children; children; children = children->next)
1031 AttachInfo *child_ai = get_attach_info (children->data);
1033 if (child_ai->top_attach >= position)
1034 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
1035 "top_attach", child_ai->top_attach + 1,
1036 "bottom_attach", child_ai->bottom_attach + 1,
1040 /* attach the new item */
1041 gtk_menu_attach (GTK_MENU (menu_shell), child,
1042 0, priv->columns ? priv->columns : 1,
1043 position, position + 1);
1047 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1051 if (GTK_WIDGET_REALIZED (menu_shell))
1052 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
1054 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1056 gtk_menu_do_insert (menu_shell, child, position);
1060 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1065 widget = GTK_WIDGET (menu);
1071 GdkGCValues gc_values;
1073 menu->tearoff_active = FALSE;
1074 menu->saved_scroll_offset = menu->scroll_offset;
1076 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1077 gc = gdk_gc_new_with_values (widget->window,
1078 &gc_values, GDK_GC_SUBWINDOW);
1080 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1082 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1087 gdk_draw_drawable (pixmap, gc,
1088 menu->tearoff_window->window,
1089 0, 0, 0, 0, -1, -1);
1090 g_object_unref (gc);
1092 gtk_widget_set_size_request (menu->tearoff_window,
1096 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1097 g_object_unref (pixmap);
1102 popup_grab_on_window (GdkWindow *window,
1103 guint32 activate_time)
1105 if ((gdk_pointer_grab (window, TRUE,
1106 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1107 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1108 GDK_POINTER_MOTION_MASK,
1109 NULL, NULL, activate_time) == 0))
1111 if (gdk_keyboard_grab (window, TRUE,
1112 activate_time) == 0)
1116 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1127 * @menu: a #GtkMenu.
1128 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1129 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1130 * @func: a user supplied function used to position the menu, or %NULL
1131 * @data: user supplied data to be passed to @func.
1132 * @button: the mouse button which was pressed to initiate the event.
1133 * @activate_time: the time at which the activation event occurred.
1135 * Displays a menu and makes it available for selection. Applications can use
1136 * this function to display context-sensitive menus, and will typically supply
1137 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1138 * parameters. The default menu positioning function will position the menu
1139 * at the current mouse cursor position.
1141 * The @button parameter should be the mouse button pressed to initiate
1142 * the menu popup. If the menu popup was initiated by something other than
1143 * a mouse button press, such as a mouse button release or a keypress,
1144 * @button should be 0.
1146 * The @activate_time parameter should be the time stamp of the event that
1147 * initiated the popup. If such an event is not available, use
1148 * gtk_get_current_event_time() instead.
1152 gtk_menu_popup (GtkMenu *menu,
1153 GtkWidget *parent_menu_shell,
1154 GtkWidget *parent_menu_item,
1155 GtkMenuPositionFunc func,
1158 guint32 activate_time)
1161 GtkWidget *xgrab_shell;
1163 GdkEvent *current_event;
1164 GtkMenuShell *menu_shell;
1166 g_return_if_fail (GTK_IS_MENU (menu));
1168 widget = GTK_WIDGET (menu);
1169 menu_shell = GTK_MENU_SHELL (menu);
1171 menu_shell->parent_menu_shell = parent_menu_shell;
1173 /* Find the last viewable ancestor, and make an X grab on it
1175 parent = GTK_WIDGET (menu);
1179 gboolean viewable = TRUE;
1180 GtkWidget *tmp = parent;
1184 if (!GTK_WIDGET_MAPPED (tmp))
1193 xgrab_shell = parent;
1195 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1198 /* We want to receive events generated when we map the menu; unfortunately,
1199 * since there is probably already an implicit grab in place from the
1200 * button that the user used to pop up the menu, we won't receive then --
1201 * in particular, the EnterNotify when the menu pops up under the pointer.
1203 * If we are grabbing on a parent menu shell, no problem; just grab on
1204 * that menu shell first before popping up the window with owner_events = TRUE.
1206 * When grabbing on the menu itself, things get more convuluted - we
1207 * we do an explicit grab on a specially created window with
1208 * owner_events = TRUE, which we override further down with a grab
1209 * on the menu. (We can't grab on the menu until it is mapped; we
1210 * probably could just leave the grab on the other window, with a
1211 * little reorganization of the code in gtkmenu*).
1213 if (xgrab_shell && xgrab_shell != widget)
1215 if (popup_grab_on_window (xgrab_shell->window, activate_time))
1216 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1220 GdkWindow *transfer_window;
1222 xgrab_shell = widget;
1223 transfer_window = menu_grab_transfer_window_get (menu);
1224 if (popup_grab_on_window (transfer_window, activate_time))
1225 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1228 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1230 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1231 * with a stuck up window, we just abort here. Presumably the user will
1234 menu_shell->parent_menu_shell = NULL;
1235 menu_grab_transfer_window_destroy (menu);
1239 menu_shell->active = TRUE;
1240 menu_shell->button = button;
1242 /* If we are popping up the menu from something other than, a button
1243 * press then, as a heuristic, we ignore enter events for the menu
1244 * until we get a MOTION_NOTIFY.
1247 current_event = gtk_get_current_event ();
1250 if ((current_event->type != GDK_BUTTON_PRESS) &&
1251 (current_event->type != GDK_ENTER_NOTIFY))
1252 menu_shell->ignore_enter = TRUE;
1254 gdk_event_free (current_event);
1257 menu_shell->ignore_enter = TRUE;
1261 gtk_menu_tearoff_bg_copy (menu);
1263 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1266 menu->parent_menu_item = parent_menu_item;
1267 menu->position_func = func;
1268 menu->position_func_data = data;
1269 menu_shell->activate_time = activate_time;
1271 /* We need to show the menu here rather in the init function because
1272 * code expects to be able to tell if the menu is onscreen by
1273 * looking at the GTK_WIDGET_VISIBLE (menu)
1275 gtk_widget_show (GTK_WIDGET (menu));
1277 /* Position the menu, possibly changing the size request
1279 gtk_menu_position (menu);
1281 /* Compute the size of the toplevel and realize it so we
1282 * can scroll correctly.
1285 GtkRequisition tmp_request;
1286 GtkAllocation tmp_allocation = { 0, };
1288 gtk_widget_size_request (menu->toplevel, &tmp_request);
1290 tmp_allocation.width = tmp_request.width;
1291 tmp_allocation.height = tmp_request.height;
1293 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1295 gtk_widget_realize (GTK_WIDGET (menu));
1298 gtk_menu_scroll_to (menu, menu->scroll_offset);
1300 /* Once everything is set up correctly, map the toplevel window on
1303 gtk_widget_show (menu->toplevel);
1305 if (xgrab_shell == widget)
1306 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1307 gtk_grab_add (GTK_WIDGET (menu));
1311 gtk_menu_popdown (GtkMenu *menu)
1313 GtkMenuPrivate *private;
1314 GtkMenuShell *menu_shell;
1316 g_return_if_fail (GTK_IS_MENU (menu));
1318 menu_shell = GTK_MENU_SHELL (menu);
1319 private = gtk_menu_get_private (menu);
1321 menu_shell->parent_menu_shell = NULL;
1322 menu_shell->active = FALSE;
1323 menu_shell->ignore_enter = FALSE;
1325 private->have_position = FALSE;
1327 gtk_menu_stop_scrolling (menu);
1329 gtk_menu_stop_navigating_submenu (menu);
1331 if (menu_shell->active_menu_item)
1333 if (menu->old_active_menu_item)
1334 g_object_unref (menu->old_active_menu_item);
1335 menu->old_active_menu_item = menu_shell->active_menu_item;
1336 g_object_ref (menu->old_active_menu_item);
1339 gtk_menu_shell_deselect (menu_shell);
1341 /* The X Grab, if present, will automatically be removed when we hide
1343 gtk_widget_hide (menu->toplevel);
1347 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1349 if (GTK_BIN (menu->toplevel)->child)
1351 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1355 /* We popped up the menu from the tearoff, so we need to
1356 * release the grab - we aren't actually hiding the menu.
1358 if (menu_shell->have_xgrab)
1360 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1362 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1363 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1367 /* gtk_menu_popdown is called each time a menu item is selected from
1368 * a torn off menu. Only scroll back to the saved position if the
1369 * non-tearoff menu was popped down.
1371 if (!menu->tearoff_active)
1372 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1373 menu->tearoff_active = TRUE;
1376 gtk_widget_hide (GTK_WIDGET (menu));
1378 menu_shell->have_xgrab = FALSE;
1379 gtk_grab_remove (GTK_WIDGET (menu));
1381 menu_grab_transfer_window_destroy (menu);
1385 gtk_menu_get_active (GtkMenu *menu)
1390 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1392 if (!menu->old_active_menu_item)
1395 children = GTK_MENU_SHELL (menu)->children;
1399 child = children->data;
1400 children = children->next;
1402 if (GTK_BIN (child)->child)
1407 menu->old_active_menu_item = child;
1408 if (menu->old_active_menu_item)
1409 g_object_ref (menu->old_active_menu_item);
1412 return menu->old_active_menu_item;
1416 gtk_menu_set_active (GtkMenu *menu,
1422 g_return_if_fail (GTK_IS_MENU (menu));
1424 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1427 child = tmp_list->data;
1428 if (GTK_BIN (child)->child)
1430 if (menu->old_active_menu_item)
1431 g_object_unref (menu->old_active_menu_item);
1432 menu->old_active_menu_item = child;
1433 g_object_ref (menu->old_active_menu_item);
1439 gtk_menu_set_accel_group (GtkMenu *menu,
1440 GtkAccelGroup *accel_group)
1442 g_return_if_fail (GTK_IS_MENU (menu));
1444 if (menu->accel_group != accel_group)
1446 if (menu->accel_group)
1447 g_object_unref (menu->accel_group);
1448 menu->accel_group = accel_group;
1449 if (menu->accel_group)
1450 g_object_ref (menu->accel_group);
1451 _gtk_menu_refresh_accel_paths (menu, TRUE);
1456 gtk_menu_get_accel_group (GtkMenu *menu)
1458 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1460 return menu->accel_group;
1464 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1467 /* Menu items chain here to figure whether they can activate their
1468 * accelerators. Unlike ordinary widgets, menus allow accel
1469 * activation even if invisible since that's the usual case for
1470 * submenus/popup-menus. however, the state of the attach widget
1471 * affects the "activeness" of the menu.
1473 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1476 return gtk_widget_can_activate_accel (awidget, signal_id);
1478 return GTK_WIDGET_IS_SENSITIVE (widget);
1482 * gtk_menu_set_accel_path
1483 * @menu: a valid #GtkMenu
1484 * @accel_path: a valid accelerator path
1486 * Sets an accelerator path for this menu from which accelerator paths
1487 * for its immediate children, its menu items, can be constructed.
1488 * The main purpose of this function is to spare the programmer the
1489 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1490 * each menu item that should support runtime user changable accelerators.
1491 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1492 * each menu item of this menu, that contains a label describing its purpose,
1493 * automatically gets an accel path assigned. For example, a menu containing
1494 * menu items "New" and "Exit", will, after
1495 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1496 * has been called, assign its items the accel paths:
1497 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1498 * Assigning accel paths to menu items then enables the user to change
1499 * their accelerators at runtime. More details about accelerator paths
1500 * and their default setups can be found at gtk_accel_map_add_entry().
1503 gtk_menu_set_accel_path (GtkMenu *menu,
1504 const gchar *accel_path)
1506 g_return_if_fail (GTK_IS_MENU (menu));
1508 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1510 g_free (menu->accel_path);
1511 menu->accel_path = g_strdup (accel_path);
1512 if (menu->accel_path)
1513 _gtk_menu_refresh_accel_paths (menu, FALSE);
1518 gboolean group_changed;
1522 refresh_accel_paths_foreach (GtkWidget *widget,
1525 AccelPropagation *prop = data;
1527 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1528 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1529 prop->menu->accel_path,
1530 prop->menu->accel_group,
1531 prop->group_changed);
1535 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1536 gboolean group_changed)
1538 g_return_if_fail (GTK_IS_MENU (menu));
1540 if (menu->accel_path && menu->accel_group)
1542 AccelPropagation prop;
1545 prop.group_changed = group_changed;
1546 gtk_container_foreach (GTK_CONTAINER (menu),
1547 refresh_accel_paths_foreach,
1553 gtk_menu_reposition (GtkMenu *menu)
1555 g_return_if_fail (GTK_IS_MENU (menu));
1557 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1558 gtk_menu_position (menu);
1562 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1565 g_return_if_fail (GTK_IS_MENU (menu));
1567 if (adjustment->value != menu->scroll_offset)
1568 gtk_menu_scroll_to (menu, adjustment->value);
1572 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1575 GdkGeometry geometry_hints;
1577 if (!menu->tearoff_window)
1580 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1582 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1583 width += menu->tearoff_scrollbar->requisition.width;
1586 geometry_hints.min_width = width;
1587 geometry_hints.max_width = width;
1589 geometry_hints.min_height = 0;
1590 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1592 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1595 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1599 gtk_menu_update_title (GtkMenu *menu)
1601 if (menu->tearoff_window)
1604 GtkWidget *attach_widget;
1606 title = gtk_menu_get_title (menu);
1609 attach_widget = gtk_menu_get_attach_widget (menu);
1610 if (GTK_IS_MENU_ITEM (attach_widget))
1612 GtkWidget *child = GTK_BIN (attach_widget)->child;
1613 if (GTK_IS_LABEL (child))
1614 title = gtk_label_get_text (GTK_LABEL (child));
1619 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1624 gtk_menu_get_toplevel (GtkWidget *menu)
1626 GtkWidget *attach, *toplevel;
1628 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1630 if (GTK_IS_MENU_ITEM (attach))
1631 attach = attach->parent;
1633 if (GTK_IS_MENU (attach))
1634 return gtk_menu_get_toplevel (attach->parent);
1635 else if (GTK_IS_WIDGET (attach))
1637 toplevel = gtk_widget_get_toplevel (attach->parent);
1638 if (GTK_WIDGET_TOPLEVEL (toplevel))
1646 gtk_menu_set_tearoff_state (GtkMenu *menu,
1651 g_return_if_fail (GTK_IS_MENU (menu));
1653 if (menu->torn_off != torn_off)
1655 menu->torn_off = torn_off;
1656 menu->tearoff_active = torn_off;
1660 if (GTK_WIDGET_VISIBLE (menu))
1661 gtk_menu_popdown (menu);
1663 if (!menu->tearoff_window)
1665 GtkWidget *toplevel;
1667 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1668 "type", GTK_WINDOW_TOPLEVEL,
1669 "screen", gtk_widget_get_screen (menu->toplevel),
1670 "app_paintable", TRUE,
1674 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1675 GDK_WINDOW_TYPE_HINT_MENU);
1676 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1677 g_signal_connect (menu->tearoff_window, "destroy",
1678 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1679 g_signal_connect (menu->tearoff_window, "event",
1680 G_CALLBACK (gtk_menu_window_event), menu);
1682 gtk_menu_update_title (menu);
1684 gtk_widget_realize (menu->tearoff_window);
1686 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1687 if (toplevel != NULL)
1688 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1689 GTK_WINDOW (toplevel));
1691 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1692 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1694 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1695 menu->tearoff_adjustment =
1696 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1698 GTK_WIDGET (menu)->requisition.height,
1702 g_object_connect (menu->tearoff_adjustment,
1703 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1705 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1707 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1708 menu->tearoff_scrollbar,
1711 if (menu->tearoff_adjustment->upper > height)
1712 gtk_widget_show (menu->tearoff_scrollbar);
1714 gtk_widget_show (menu->tearoff_hbox);
1717 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1719 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1721 /* Update menu->requisition
1723 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1725 gtk_menu_set_tearoff_hints (menu, width);
1727 gtk_widget_realize (menu->tearoff_window);
1728 gtk_menu_position (menu);
1730 gtk_widget_show (GTK_WIDGET (menu));
1731 gtk_widget_show (menu->tearoff_window);
1733 gtk_menu_scroll_to (menu, 0);
1738 gtk_widget_hide (GTK_WIDGET (menu));
1739 gtk_widget_hide (menu->tearoff_window);
1740 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1741 gtk_widget_destroy (menu->tearoff_window);
1743 menu->tearoff_window = NULL;
1744 menu->tearoff_hbox = NULL;
1745 menu->tearoff_scrollbar = NULL;
1746 menu->tearoff_adjustment = NULL;
1752 * gtk_menu_get_tearoff_state:
1755 * Returns whether the menu is torn off. See
1756 * gtk_menu_set_tearoff_state ().
1758 * Return value: %TRUE if the menu is currently torn off.
1761 gtk_menu_get_tearoff_state (GtkMenu *menu)
1763 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1765 return menu->torn_off;
1769 * gtk_menu_set_title:
1771 * @title: a string containing the title for the menu.
1773 * Sets the title string for the menu. The title is displayed when the menu
1774 * is shown as a tearoff menu.
1777 gtk_menu_set_title (GtkMenu *menu,
1780 g_return_if_fail (GTK_IS_MENU (menu));
1783 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1784 g_strdup (title), (GtkDestroyNotify) g_free);
1786 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1788 gtk_menu_update_title (menu);
1789 g_object_notify (G_OBJECT (menu), "tearoff_title");
1793 * gtk_menu_get_title:
1796 * Returns the title of the menu. See gtk_menu_set_title().
1798 * Return value: the title of the menu, or %NULL if the menu has no
1799 * title set on it. This string is owned by the widget and should
1800 * not be modified or freed.
1802 G_CONST_RETURN gchar *
1803 gtk_menu_get_title (GtkMenu *menu)
1805 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1807 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1811 gtk_menu_reorder_child (GtkMenu *menu,
1815 GtkMenuShell *menu_shell;
1817 g_return_if_fail (GTK_IS_MENU (menu));
1818 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1820 menu_shell = GTK_MENU_SHELL (menu);
1822 if (g_list_find (menu_shell->children, child))
1824 menu_shell->children = g_list_remove (menu_shell->children, child);
1825 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1826 gtk_menu_do_insert (menu_shell, child, position);
1828 if (GTK_WIDGET_VISIBLE (menu_shell))
1829 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1834 gtk_menu_style_set (GtkWidget *widget,
1835 GtkStyle *previous_style)
1837 if (GTK_WIDGET_REALIZED (widget))
1839 GtkMenu *menu = GTK_MENU (widget);
1841 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1842 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1843 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1848 gtk_menu_realize (GtkWidget *widget)
1850 GdkWindowAttr attributes;
1851 gint attributes_mask;
1856 guint vertical_padding;
1858 g_return_if_fail (GTK_IS_MENU (widget));
1860 menu = GTK_MENU (widget);
1862 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1864 attributes.window_type = GDK_WINDOW_CHILD;
1865 attributes.x = widget->allocation.x;
1866 attributes.y = widget->allocation.y;
1867 attributes.width = widget->allocation.width;
1868 attributes.height = widget->allocation.height;
1869 attributes.wclass = GDK_INPUT_OUTPUT;
1870 attributes.visual = gtk_widget_get_visual (widget);
1871 attributes.colormap = gtk_widget_get_colormap (widget);
1873 attributes.event_mask = gtk_widget_get_events (widget);
1875 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1876 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1878 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1879 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1880 gdk_window_set_user_data (widget->window, widget);
1882 border_width = GTK_CONTAINER (widget)->border_width;
1884 gtk_widget_style_get (GTK_WIDGET (menu),
1885 "vertical-padding", &vertical_padding,
1888 attributes.x = border_width + widget->style->xthickness;
1889 attributes.y = border_width + widget->style->ythickness + vertical_padding;
1890 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1891 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1893 if (menu->upper_arrow_visible)
1895 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1896 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1898 if (menu->lower_arrow_visible)
1899 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1901 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1902 gdk_window_set_user_data (menu->view_window, menu);
1906 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
1908 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1909 gdk_window_set_user_data (menu->bin_window, menu);
1911 children = GTK_MENU_SHELL (menu)->children;
1914 child = children->data;
1915 children = children->next;
1917 gtk_widget_set_parent_window (child, menu->bin_window);
1920 widget->style = gtk_style_attach (widget->style, widget->window);
1921 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1922 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1923 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1925 if (GTK_MENU_SHELL (widget)->active_menu_item)
1926 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1927 GTK_MENU_SHELL (widget)->active_menu_item);
1929 gdk_window_show (menu->bin_window);
1930 gdk_window_show (menu->view_window);
1934 gtk_menu_focus (GtkWidget *widget,
1935 GtkDirectionType direction)
1938 * A menu or its menu items cannot have focus
1943 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1946 menu_grab_transfer_window_get (GtkMenu *menu)
1948 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1951 GdkWindowAttr attributes;
1952 gint attributes_mask;
1954 attributes.x = -100;
1955 attributes.y = -100;
1956 attributes.width = 10;
1957 attributes.height = 10;
1958 attributes.window_type = GDK_WINDOW_TEMP;
1959 attributes.wclass = GDK_INPUT_ONLY;
1960 attributes.override_redirect = TRUE;
1961 attributes.event_mask = 0;
1963 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1965 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1966 &attributes, attributes_mask);
1967 gdk_window_set_user_data (window, menu);
1969 gdk_window_show (window);
1971 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1978 menu_grab_transfer_window_destroy (GtkMenu *menu)
1980 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1983 gdk_window_set_user_data (window, NULL);
1984 gdk_window_destroy (window);
1985 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1990 gtk_menu_unrealize (GtkWidget *widget)
1994 g_return_if_fail (GTK_IS_MENU (widget));
1996 menu = GTK_MENU (widget);
1998 menu_grab_transfer_window_destroy (menu);
2000 gdk_window_set_user_data (menu->view_window, NULL);
2001 gdk_window_destroy (menu->view_window);
2002 menu->view_window = NULL;
2004 gdk_window_set_user_data (menu->bin_window, NULL);
2005 gdk_window_destroy (menu->bin_window);
2006 menu->bin_window = NULL;
2008 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2012 gtk_menu_size_request (GtkWidget *widget,
2013 GtkRequisition *requisition)
2017 GtkMenuShell *menu_shell;
2020 guint max_toggle_size;
2021 guint max_accel_width;
2022 guint vertical_padding;
2023 GtkRequisition child_requisition;
2024 GtkMenuPrivate *priv;
2026 g_return_if_fail (GTK_IS_MENU (widget));
2027 g_return_if_fail (requisition != NULL);
2029 menu = GTK_MENU (widget);
2030 menu_shell = GTK_MENU_SHELL (widget);
2031 priv = gtk_menu_get_private (menu);
2033 requisition->width = 0;
2034 requisition->height = 0;
2036 max_toggle_size = 0;
2037 max_accel_width = 0;
2039 g_free (priv->heights);
2040 priv->heights = g_new0 (guint, priv->rows);
2041 priv->heights_length = priv->rows;
2043 children = menu_shell->children;
2050 child = children->data;
2051 children = children->next;
2053 if (! GTK_WIDGET_VISIBLE (child))
2056 get_child_attach (child, &l, &r, &t, &b);
2058 /* It's important to size_request the child
2059 * before doing the toggle size request, in
2060 * case the toggle size request depends on the size
2061 * request of a child of the child (e.g. for ImageMenuItem)
2064 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2065 gtk_widget_size_request (child, &child_requisition);
2067 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2068 max_toggle_size = MAX (max_toggle_size, toggle_size);
2069 max_accel_width = MAX (max_accel_width,
2070 GTK_MENU_ITEM (child)->accelerator_width);
2072 part = child_requisition.width / (r - l);
2073 requisition->width = MAX (requisition->width, part);
2075 part = child_requisition.height / (b - t);
2076 priv->heights[t] = MAX (priv->heights[t], part);
2079 for (i = 0; i < priv->rows; i++)
2080 requisition->height += priv->heights[i];
2082 requisition->width += max_toggle_size + max_accel_width;
2083 requisition->width *= priv->columns;
2084 requisition->width += (GTK_CONTAINER (menu)->border_width +
2085 widget->style->xthickness) * 2;
2087 gtk_widget_style_get (GTK_WIDGET (menu),
2088 "vertical-padding", &vertical_padding,
2090 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2091 widget->style->ythickness) * 2;
2093 menu->toggle_size = max_toggle_size;
2095 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2097 if (menu->tearoff_active)
2098 gtk_menu_set_tearoff_hints (menu, requisition->width);
2102 gtk_menu_size_allocate (GtkWidget *widget,
2103 GtkAllocation *allocation)
2106 GtkMenuShell *menu_shell;
2108 GtkAllocation child_allocation;
2109 GtkRequisition child_requisition;
2110 GtkMenuPrivate *priv;
2114 guint vertical_padding;
2116 g_return_if_fail (GTK_IS_MENU (widget));
2117 g_return_if_fail (allocation != NULL);
2119 menu = GTK_MENU (widget);
2120 menu_shell = GTK_MENU_SHELL (widget);
2121 priv = gtk_menu_get_private (menu);
2123 widget->allocation = *allocation;
2124 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2126 gtk_widget_style_get (GTK_WIDGET (menu),
2127 "vertical-padding", &vertical_padding,
2130 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2131 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2133 width = MAX (1, allocation->width - x * 2);
2134 height = MAX (1, allocation->height - y * 2);
2136 child_requisition.width -= x * 2;
2137 child_requisition.height -= y * 2;
2139 if (menu_shell->active)
2140 gtk_menu_scroll_to (menu, menu->scroll_offset);
2142 if (menu->upper_arrow_visible && !menu->tearoff_active)
2144 y += MENU_SCROLL_ARROW_HEIGHT;
2145 height -= MENU_SCROLL_ARROW_HEIGHT;
2148 if (menu->lower_arrow_visible && !menu->tearoff_active)
2149 height -= MENU_SCROLL_ARROW_HEIGHT;
2151 if (GTK_WIDGET_REALIZED (widget))
2153 gdk_window_move_resize (widget->window,
2154 allocation->x, allocation->y,
2155 allocation->width, allocation->height);
2157 gdk_window_move_resize (menu->view_window,
2164 if (menu_shell->children)
2166 gint base_width = width / priv->columns;
2168 children = menu_shell->children;
2171 child = children->data;
2172 children = children->next;
2174 if (GTK_WIDGET_VISIBLE (child))
2179 get_child_attach (child, &l, &r, &t, &b);
2181 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2184 tmp = priv->columns - l;
2185 l = priv->columns - r;
2189 child_allocation.width = (r - l) * base_width;
2190 child_allocation.height = 0;
2191 child_allocation.x = l * base_width;
2192 child_allocation.y = 0;
2194 for (i = 0; i < b; i++)
2197 child_allocation.y += priv->heights[i];
2199 child_allocation.height += priv->heights[i];
2202 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2205 gtk_widget_size_allocate (child, &child_allocation);
2206 gtk_widget_queue_draw (child);
2210 /* Resize the item window */
2211 if (GTK_WIDGET_REALIZED (widget))
2217 for (i = 0; i < priv->rows; i++)
2218 height += priv->heights[i];
2220 width = priv->columns * base_width;
2221 gdk_window_resize (menu->bin_window, width, height);
2224 if (menu->tearoff_active)
2226 if (allocation->height >= widget->requisition.height)
2228 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2230 gtk_widget_hide (menu->tearoff_scrollbar);
2231 gtk_menu_set_tearoff_hints (menu, allocation->width);
2233 gtk_menu_scroll_to (menu, 0);
2238 menu->tearoff_adjustment->upper = widget->requisition.height;
2239 menu->tearoff_adjustment->page_size = allocation->height;
2241 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2242 menu->tearoff_adjustment->upper)
2245 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2248 gtk_menu_scroll_to (menu, value);
2251 gtk_adjustment_changed (menu->tearoff_adjustment);
2253 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2255 gtk_widget_show (menu->tearoff_scrollbar);
2256 gtk_menu_set_tearoff_hints (menu, allocation->width);
2264 gtk_menu_paint (GtkWidget *widget,
2265 GdkEventExpose *event)
2269 gint border_x, border_y;
2270 guint vertical_padding;
2272 g_return_if_fail (GTK_IS_MENU (widget));
2274 menu = GTK_MENU (widget);
2276 gtk_widget_style_get (GTK_WIDGET (menu),
2277 "vertical-padding", &vertical_padding,
2280 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2281 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2282 gdk_drawable_get_size (widget->window, &width, &height);
2284 if (event->window == widget->window)
2286 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2287 gint arrow_size = 0.7 * arrow_space;
2289 gtk_paint_box (widget->style,
2293 NULL, widget, "menu",
2295 if (menu->upper_arrow_visible && !menu->tearoff_active)
2297 gtk_paint_box (widget->style,
2299 menu->upper_arrow_prelight ?
2300 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2302 NULL, widget, "menu",
2305 width - 2 * border_x,
2306 MENU_SCROLL_ARROW_HEIGHT);
2308 gtk_paint_arrow (widget->style,
2310 menu->upper_arrow_prelight ?
2311 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2313 NULL, widget, "menu_scroll_arrow_up",
2316 (width - arrow_size ) / 2,
2317 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2318 arrow_size, arrow_size);
2321 if (menu->lower_arrow_visible && !menu->tearoff_active)
2323 gtk_paint_box (widget->style,
2325 menu->lower_arrow_prelight ?
2326 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2328 NULL, widget, "menu",
2330 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2332 MENU_SCROLL_ARROW_HEIGHT);
2334 gtk_paint_arrow (widget->style,
2336 menu->lower_arrow_prelight ?
2337 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2339 NULL, widget, "menu_scroll_arrow_down",
2342 (width - arrow_size) / 2,
2343 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2344 widget->style->ythickness + (arrow_space - arrow_size)/2,
2345 arrow_size, arrow_size);
2351 gtk_menu_expose (GtkWidget *widget,
2352 GdkEventExpose *event)
2354 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2355 g_return_val_if_fail (event != NULL, FALSE);
2357 if (GTK_WIDGET_DRAWABLE (widget))
2359 gtk_menu_paint (widget, event);
2361 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2368 gtk_menu_show (GtkWidget *widget)
2370 GtkMenu *menu = GTK_MENU (widget);
2372 _gtk_menu_refresh_accel_paths (menu, FALSE);
2374 GTK_WIDGET_CLASS (parent_class)->show (widget);
2378 gtk_menu_button_press (GtkWidget *widget,
2379 GdkEventButton *event)
2381 /* Don't pop down the menu for releases over scroll arrows
2383 if (GTK_IS_MENU (widget))
2385 GtkMenu *menu = GTK_MENU (widget);
2387 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2391 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2395 gtk_menu_button_release (GtkWidget *widget,
2396 GdkEventButton *event)
2398 /* Don't pop down the menu for releases over scroll arrows
2400 if (GTK_IS_MENU (widget))
2402 GtkMenu *menu = GTK_MENU (widget);
2404 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2408 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2411 static const gchar *
2412 get_accel_path (GtkWidget *menu_item,
2417 GClosure *accel_closure;
2418 GtkAccelGroup *accel_group;
2420 path = _gtk_widget_get_accel_path (menu_item, locked);
2423 path = GTK_MENU_ITEM (menu_item)->accel_path;
2429 label = GTK_BIN (menu_item)->child;
2431 if (GTK_IS_ACCEL_LABEL (label))
2433 g_object_get (label,
2434 "accel_closure", &accel_closure,
2436 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2438 *locked = accel_group->lock_count > 0;
2447 gtk_menu_key_press (GtkWidget *widget,
2450 GtkMenuShell *menu_shell;
2452 gboolean delete = FALSE;
2453 gboolean can_change_accels;
2454 gchar *accel = NULL;
2455 guint accel_key, accel_mods;
2456 GdkModifierType consumed_modifiers;
2457 GdkDisplay *display;
2459 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2460 g_return_val_if_fail (event != NULL, FALSE);
2462 menu_shell = GTK_MENU_SHELL (widget);
2463 menu = GTK_MENU (widget);
2465 gtk_menu_stop_navigating_submenu (menu);
2467 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2470 display = gtk_widget_get_display (widget);
2472 g_object_get (gtk_widget_get_settings (widget),
2473 "gtk-menu-bar-accel", &accel,
2474 "gtk-can-change-accels", &can_change_accels,
2480 GdkModifierType mods = 0;
2481 gboolean handled = FALSE;
2483 gtk_accelerator_parse (accel, &keyval, &mods);
2486 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2488 /* FIXME this is wrong, needs to be in the global accel resolution
2489 * thing, to properly consider i18n etc., but that probably requires
2490 * AccelGroup changes etc.
2492 if (event->keyval == keyval &&
2493 (mods & event->state) == mods)
2494 gtk_menu_shell_cancel (menu_shell);
2502 switch (event->keyval)
2513 /* Figure out what modifiers went into determining the key symbol */
2514 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2515 event->hardware_keycode, event->state, event->group,
2516 NULL, NULL, NULL, &consumed_modifiers);
2518 accel_key = gdk_keyval_to_lower (event->keyval);
2519 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2521 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2522 * We re-upper case when we match against the keyval, but display and save in caseless form.
2524 if (accel_key != event->keyval)
2525 accel_mods |= GDK_SHIFT_MASK;
2527 /* Modify the accelerators */
2528 if (can_change_accels &&
2529 menu_shell->active_menu_item &&
2530 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2531 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2532 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2534 GtkWidget *menu_item = menu_shell->active_menu_item;
2535 gboolean locked, replace_accels = TRUE;
2538 path = get_accel_path (menu_item, &locked);
2539 if (!path || locked)
2541 /* can't change accelerators on menu_items without paths
2542 * (basically, those items are accelerator-locked).
2544 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2545 gdk_display_beep (display);
2551 /* For the keys that act to delete the current setting, we delete
2552 * the current setting if there is one, otherwise, we set the
2553 * key as the accelerator.
2559 if (gtk_accel_map_lookup_entry (path, &key) &&
2560 (key.accel_key || key.accel_mods))
2566 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2570 /* we failed, probably because this key is in use and
2573 /* g_print("failed to change\n"); */
2574 gdk_display_beep (display);
2583 gtk_menu_motion_notify (GtkWidget *widget,
2584 GdkEventMotion *event)
2586 GtkWidget *menu_item;
2588 GtkMenuShell *menu_shell;
2590 gboolean need_enter;
2592 if (GTK_IS_MENU (widget))
2593 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2595 /* We received the event for one of two reasons:
2597 * a) We are the active menu, and did gtk_grab_add()
2598 * b) The widget is a child of ours, and the event was propagated
2600 * Since for computation of navigation regions, we want the menu which
2601 * is the parent of the menu item, for a), we need to find that menu,
2602 * which may be different from 'widget'.
2604 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2605 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2606 !_gtk_menu_item_is_selectable (menu_item) ||
2607 !GTK_IS_MENU (menu_item->parent))
2610 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2611 menu = GTK_MENU (menu_shell);
2613 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2615 /* Check to see if we are within an active submenu's navigation region
2617 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2622 /* The menu is now sensitive to enter events on its items, but
2623 * was previously sensitive. So we fake an enter event.
2627 menu_shell->ignore_enter = FALSE;
2629 gdk_drawable_get_size (event->window, &width, &height);
2630 if (event->x >= 0 && event->x < width &&
2631 event->y >= 0 && event->y < height)
2633 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2636 send_event->crossing.window = g_object_ref (event->window);
2637 send_event->crossing.time = event->time;
2638 send_event->crossing.send_event = TRUE;
2639 send_event->crossing.x_root = event->x_root;
2640 send_event->crossing.y_root = event->y_root;
2641 send_event->crossing.x = event->x;
2642 send_event->crossing.y = event->y;
2644 /* We send the event to 'widget', the currently active menu,
2645 * instead of 'menu', the menu that the pointer is in. This
2646 * will ensure that the event will be ignored unless the
2647 * menuitem is a child of the active menu or some parent
2648 * menu of the active menu.
2650 result = gtk_widget_event (widget, send_event);
2651 gdk_event_free (send_event);
2661 gtk_menu_scroll_timeout (gpointer data)
2666 gint view_width, view_height;
2668 GDK_THREADS_ENTER ();
2670 menu = GTK_MENU (data);
2671 widget = GTK_WIDGET (menu);
2673 offset = menu->scroll_offset + menu->scroll_step;
2675 /* If we scroll upward and the non-visible top part
2676 * is smaller than the scroll arrow it would be
2677 * pretty stupid to show the arrow and taking more
2678 * screen space than just scrolling to the top.
2680 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2683 /* Don't scroll over the top if we weren't before: */
2684 if ((menu->scroll_offset >= 0) && (offset < 0))
2687 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2689 /* Don't scroll past the bottom if we weren't before: */
2690 if (menu->scroll_offset > 0)
2691 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2693 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2694 (offset + view_height > widget->requisition.height))
2695 offset = widget->requisition.height - view_height;
2697 gtk_menu_scroll_to (menu, offset);
2699 GDK_THREADS_LEAVE ();
2705 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2707 GtkMenuShell *menu_shell;
2713 gboolean scroll_fast = FALSE;
2714 guint vertical_padding;
2716 menu_shell = GTK_MENU_SHELL (menu);
2718 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2719 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2721 gtk_widget_style_get (GTK_WIDGET (menu),
2722 "vertical-padding", &vertical_padding,
2725 border = GTK_CONTAINER (menu)->border_width +
2726 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2728 if (menu->upper_arrow_visible && !menu->tearoff_active)
2733 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2736 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2737 (y >= rect.y) && (y < rect.y + rect.height))
2740 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2743 if (enter && in_arrow &&
2744 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2746 menu->upper_arrow_prelight = TRUE;
2747 menu->scroll_fast = scroll_fast;
2748 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2750 /* Deselect the active item so that any submenus are poped down */
2751 gtk_menu_shell_deselect (menu_shell);
2753 gtk_menu_remove_scroll_timeout (menu);
2754 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2755 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2756 gtk_menu_scroll_timeout,
2759 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2761 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2763 gtk_menu_stop_scrolling (menu);
2767 if (menu->lower_arrow_visible && !menu->tearoff_active)
2770 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2772 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2775 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2776 (y >= rect.y) && (y < rect.y + rect.height))
2779 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2782 if (enter && in_arrow &&
2783 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2785 menu->lower_arrow_prelight = TRUE;
2786 menu->scroll_fast = scroll_fast;
2787 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2789 /* Deselect the active item so that any submenus are poped down */
2790 gtk_menu_shell_deselect (menu_shell);
2792 gtk_menu_remove_scroll_timeout (menu);
2793 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2794 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2795 gtk_menu_scroll_timeout,
2798 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2800 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2802 gtk_menu_stop_scrolling (menu);
2808 gtk_menu_enter_notify (GtkWidget *widget,
2809 GdkEventCrossing *event)
2811 GtkWidget *menu_item;
2813 if (widget && GTK_IS_MENU (widget))
2815 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2817 if (!menu_shell->ignore_enter)
2818 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2821 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2822 * will not correspond to the event widget's parent. Check to see
2823 * if we are in the parent's navigation region.
2825 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2826 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2827 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2830 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2834 gtk_menu_leave_notify (GtkWidget *widget,
2835 GdkEventCrossing *event)
2837 GtkMenuShell *menu_shell;
2839 GtkMenuItem *menu_item;
2840 GtkWidget *event_widget;
2842 menu = GTK_MENU (widget);
2843 menu_shell = GTK_MENU_SHELL (widget);
2845 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2848 gtk_menu_handle_scrolling (menu, FALSE);
2850 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2852 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2855 menu_item = GTK_MENU_ITEM (event_widget);
2857 /* Here we check to see if we're leaving an active menu item with a submenu,
2858 * in which case we enter submenu navigation mode.
2860 if (menu_shell->active_menu_item != NULL
2861 && menu_item->submenu != NULL
2862 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2864 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2866 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2871 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2875 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2877 if (menu->navigation_region)
2879 gdk_region_destroy (menu->navigation_region);
2880 menu->navigation_region = NULL;
2883 if (menu->navigation_timeout)
2885 g_source_remove (menu->navigation_timeout);
2886 menu->navigation_timeout = 0;
2890 /* When the timeout is elapsed, the navigation region is destroyed
2891 * and the menuitem under the pointer (if any) is selected.
2894 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2896 GtkMenu *menu = user_data;
2897 GdkWindow *child_window;
2899 GDK_THREADS_ENTER ();
2901 gtk_menu_stop_navigating_submenu (menu);
2903 if (GTK_WIDGET_REALIZED (menu))
2905 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2909 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2911 send_event->crossing.window = g_object_ref (child_window);
2912 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2913 send_event->crossing.send_event = TRUE;
2915 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2917 gdk_event_free (send_event);
2921 GDK_THREADS_LEAVE ();
2927 gtk_menu_navigating_submenu (GtkMenu *menu,
2931 if (menu->navigation_region)
2933 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2937 gtk_menu_stop_navigating_submenu (menu);
2944 #undef DRAW_STAY_UP_TRIANGLE
2946 #ifdef DRAW_STAY_UP_TRIANGLE
2949 draw_stay_up_triangle (GdkWindow *window,
2952 /* Draw ugly color all over the stay-up triangle */
2953 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2954 GdkGCValues gc_values;
2956 GdkRectangle clipbox;
2958 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2959 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2960 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2961 gdk_gc_set_clip_region (ugly_gc, region);
2963 gdk_region_get_clipbox (region, &clipbox);
2965 gdk_draw_rectangle (window,
2968 clipbox.x, clipbox.y,
2969 clipbox.width, clipbox.height);
2971 g_object_unref (ugly_gc);
2976 flip_region (GdkRegion *region,
2981 GdkRectangle *rectangles;
2982 GdkRectangle clipbox;
2983 GdkRegion *new_region;
2986 new_region = gdk_region_new ();
2988 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
2989 gdk_region_get_clipbox (region, &clipbox);
2991 for (i = 0; i < n_rectangles; ++i)
2993 GdkRectangle rect = rectangles[i];
2996 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
2999 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3001 gdk_region_union_with_rect (new_region, &rect);
3004 g_free (rectangles);
3010 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3011 GtkMenuItem *menu_item,
3012 GdkEventCrossing *event)
3014 gint submenu_left = 0;
3015 gint submenu_right = 0;
3016 gint submenu_top = 0;
3017 gint submenu_bottom = 0;
3021 GtkWidget *event_widget;
3023 g_return_if_fail (menu_item->submenu != NULL);
3024 g_return_if_fail (event != NULL);
3026 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3028 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3029 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3031 submenu_right = submenu_left + width;
3032 submenu_bottom = submenu_top + height;
3034 gdk_drawable_get_size (event_widget->window, &width, &height);
3036 if (event->x >= 0 && event->x < width)
3039 gboolean flip_y = FALSE;
3040 gboolean flip_x = FALSE;
3042 gtk_menu_stop_navigating_submenu (menu);
3044 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3047 point[0].x = event->x_root;
3048 point[1].x = submenu_left;
3053 point[0].x = event->x_root + 1;
3054 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3062 point[0].y = event->y_root + 1;
3063 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3065 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3073 point[0].y = event->y_root;
3074 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3076 if (point[0].y >= submenu_bottom)
3080 point[2].x = point[1].x;
3081 point[2].y = point[0].y;
3083 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3085 if (flip_x || flip_y)
3087 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3088 gdk_region_destroy (menu->navigation_region);
3089 menu->navigation_region = new_region;
3092 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3093 "gtk-menu-popdown-delay", &popdown_delay,
3096 menu->navigation_timeout = g_timeout_add (popdown_delay,
3097 gtk_menu_stop_navigating_submenu_cb, menu);
3099 #ifdef DRAW_STAY_UP_TRIANGLE
3100 draw_stay_up_triangle (gdk_get_default_root_window(),
3101 menu->navigation_region);
3107 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3111 g_return_if_fail (GTK_IS_MENU (menu_shell));
3113 parent = menu_shell->parent_menu_shell;
3115 menu_shell->activate_time = 0;
3116 gtk_menu_popdown (GTK_MENU (menu_shell));
3119 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3123 gtk_menu_position (GtkMenu *menu)
3126 GtkRequisition requisition;
3127 GtkMenuPrivate *private;
3133 GdkScreen *pointer_screen;
3134 GdkRectangle monitor;
3136 g_return_if_fail (GTK_IS_MENU (menu));
3138 widget = GTK_WIDGET (menu);
3140 screen = gtk_widget_get_screen (widget);
3141 gdk_display_get_pointer (gdk_screen_get_display (screen),
3142 &pointer_screen, &x, &y, NULL);
3144 /* We need the requisition to figure out the right place to
3145 * popup the menu. In fact, we always need to ask here, since
3146 * if a size_request was queued while we weren't popped up,
3147 * the requisition won't have been recomputed yet.
3149 gtk_widget_size_request (widget, &requisition);
3151 if (pointer_screen != screen)
3153 /* Pointer is on a different screen; roughly center the
3154 * menu on the screen. If someone was using multiscreen
3155 * + Xinerama together they'd probably want something
3156 * fancier; but that is likely to be vanishingly rare.
3158 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3159 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3162 private = gtk_menu_get_private (menu);
3163 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3167 if (menu->position_func)
3169 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3170 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3174 gint space_left, space_right, space_above, space_below;
3177 gint xthickness = widget->style->xthickness;
3178 gint ythickness = widget->style->ythickness;
3179 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3181 /* The placement of popup menus horizontally works like this (with
3182 * RTL in parentheses)
3184 * - If there is enough room to the right (left) of the mouse cursor,
3185 * position the menu there.
3187 * - Otherwise, if if there is enough room to the left (right) of the
3188 * mouse cursor, position the menu there.
3190 * - Otherwise if the menu is smaller than the monitor, position it
3191 * on the side of the mouse cursor that has the most space available
3193 * - Otherwise (if there is simply not enough room for the menu on the
3194 * monitor), position it as far left (right) as possible.
3196 * Positioning in the vertical direction is similar: first try below
3197 * mouse cursor, then above.
3199 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3201 space_left = x - monitor.x;
3202 space_right = monitor.x + monitor.width - x - 1;
3203 space_above = y - monitor.y;
3204 space_below = monitor.y + monitor.height - y - 1;
3206 /* position horizontally */
3208 /* the amount of space we need to position the menu. Note the
3209 * menu is offset "xthickness" pixels
3211 needed_width = requisition.width - xthickness;
3213 if (needed_width <= space_left ||
3214 needed_width <= space_right)
3216 if ((rtl && needed_width <= space_left) ||
3217 (!rtl && needed_width > space_right))
3220 x = x + xthickness - requisition.width + 1;
3224 /* position right */
3228 /* x is clamped on-screen further down */
3230 else if (requisition.width <= monitor.width)
3232 /* the menu is too big to fit on either side of the mouse
3233 * cursor, but smaller than the monitor. Position it on
3234 * the side that has the most space
3236 if (space_left > space_right)
3244 x = monitor.x + monitor.width - requisition.width;
3247 else /* menu is simply too big for the monitor */
3252 x = monitor.x + monitor.width - requisition.width;
3261 /* Position vertically. The algorithm is the same as above, but
3262 * simpler because we don't have to take RTL into account.
3264 needed_height = requisition.height - ythickness;
3266 if (needed_height <= space_above ||
3267 needed_height <= space_below)
3269 if (needed_height <= space_below)
3272 y = y + ythickness - requisition.height + 1;
3274 y = CLAMP (y, monitor.y,
3275 monitor.y + monitor.height - requisition.height);
3277 else if (needed_height > space_below && needed_height > space_above)
3279 if (space_below >= space_above)
3280 y = monitor.y + monitor.height - requisition.height;
3294 menu_height = GTK_WIDGET (menu)->requisition.height;
3296 if (y + menu_height > monitor.y + monitor.height)
3298 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3299 y = (monitor.y + monitor.height) - menu_height;
3304 scroll_offset += monitor.y - y;
3309 /* FIXME: should this be done in the various position_funcs ? */
3310 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3312 if (GTK_MENU_SHELL (menu)->active)
3314 private->have_position = TRUE;
3319 if (y + requisition.height > monitor.y + monitor.height)
3320 requisition.height = (monitor.y + monitor.height) - y;
3324 scroll_offset += monitor.y - y;
3325 requisition.height -= monitor.y - y;
3329 if (scroll_offset > 0)
3330 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3332 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3335 if (!GTK_MENU_SHELL (menu)->active)
3337 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3338 requisition.width, requisition.height);
3341 menu->scroll_offset = scroll_offset;
3345 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3347 if (menu->timeout_id)
3349 g_source_remove (menu->timeout_id);
3350 menu->timeout_id = 0;
3355 gtk_menu_stop_scrolling (GtkMenu *menu)
3357 gtk_menu_remove_scroll_timeout (menu);
3359 menu->upper_arrow_prelight = FALSE;
3360 menu->lower_arrow_prelight = FALSE;
3364 gtk_menu_scroll_to (GtkMenu *menu,
3369 gint view_width, view_height;
3371 gboolean last_visible;
3373 guint vertical_padding;
3375 widget = GTK_WIDGET (menu);
3377 if (menu->tearoff_active &&
3378 menu->tearoff_adjustment &&
3379 (menu->tearoff_adjustment->value != offset))
3381 menu->tearoff_adjustment->value =
3383 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3384 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3387 /* Move/resize the viewport according to arrows: */
3388 view_width = widget->allocation.width;
3389 view_height = widget->allocation.height;
3391 gtk_widget_style_get (GTK_WIDGET (menu),
3392 "vertical-padding", &vertical_padding,
3395 border_width = GTK_CONTAINER (menu)->border_width;
3396 view_width -= (border_width + widget->style->xthickness) * 2;
3397 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3398 menu_height = widget->requisition.height -
3399 (border_width + widget->style->ythickness + vertical_padding) * 2;
3401 x = border_width + widget->style->xthickness;
3402 y = border_width + widget->style->ythickness + vertical_padding;
3404 if (!menu->tearoff_active)
3406 last_visible = menu->upper_arrow_visible;
3407 menu->upper_arrow_visible = offset > 0;
3409 if (menu->upper_arrow_visible)
3410 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3412 if ( (last_visible != menu->upper_arrow_visible) &&
3413 !menu->upper_arrow_visible)
3415 menu->upper_arrow_prelight = FALSE;
3417 /* If we hid the upper arrow, possibly remove timeout */
3418 if (menu->scroll_step < 0)
3420 gtk_menu_stop_scrolling (menu);
3421 gtk_widget_queue_draw (GTK_WIDGET (menu));
3425 last_visible = menu->lower_arrow_visible;
3426 menu->lower_arrow_visible = offset < menu_height - view_height;
3428 if (menu->lower_arrow_visible)
3429 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3431 if ( (last_visible != menu->lower_arrow_visible) &&
3432 !menu->lower_arrow_visible)
3434 menu->lower_arrow_prelight = FALSE;
3436 /* If we hid the lower arrow, possibly remove timeout */
3437 if (menu->scroll_step > 0)
3439 gtk_menu_stop_scrolling (menu);
3440 gtk_widget_queue_draw (GTK_WIDGET (menu));
3444 if (menu->upper_arrow_visible)
3445 y += MENU_SCROLL_ARROW_HEIGHT;
3448 /* Scroll the menu: */
3449 if (GTK_WIDGET_REALIZED (menu))
3450 gdk_window_move (menu->bin_window, 0, -offset);
3452 if (GTK_WIDGET_REALIZED (menu))
3453 gdk_window_move_resize (menu->view_window,
3459 menu->scroll_offset = offset;
3463 compute_child_offset (GtkMenu *menu,
3464 GtkWidget *menu_item,
3467 gboolean *is_last_child)
3469 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3470 guint item_top_attach;
3471 guint item_bottom_attach;
3472 gint child_offset = 0;
3475 gtk_container_child_get (GTK_CONTAINER (menu), menu_item,
3476 "top_attach", &item_top_attach,
3477 "bottom_attach", &item_bottom_attach,
3480 /* there is a possibility that we get called before _size_request, so
3481 * check the height table for safety.
3483 if (!priv->heights || priv->heights_length < priv->rows)
3486 /* when we have a row with only invisible children, it's height will
3487 * be zero, so there's no need to check WIDGET_VISIBLE here
3489 for (i = 0; i < item_top_attach; i++)
3490 child_offset += priv->heights[i];
3493 *is_last_child = (item_bottom_attach == priv->rows);
3495 *offset = child_offset;
3497 *height = priv->heights[item_top_attach];
3503 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
3504 GtkWidget *menu_item)
3507 gint child_offset, child_height;
3511 gboolean last_child = 0;
3513 menu = GTK_MENU (menu_shell);
3515 /* We need to check if the selected item fully visible.
3516 * If not we need to scroll the menu so that it becomes fully
3520 if (compute_child_offset (menu, menu_item,
3521 &child_offset, &child_height, &last_child))
3523 guint vertical_padding;
3525 y = menu->scroll_offset;
3526 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3528 gtk_widget_style_get (GTK_WIDGET (menu),
3529 "vertical-padding", &vertical_padding,
3532 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3534 if (child_offset < y)
3536 /* Ignore the enter event we might get if the pointer is on the menu
3538 menu_shell->ignore_enter = TRUE;
3539 gtk_menu_scroll_to (menu, child_offset);
3544 if (menu->upper_arrow_visible && !menu->tearoff_active)
3545 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3546 if (menu->lower_arrow_visible && !menu->tearoff_active)
3547 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3549 if (child_offset + child_height > y + height - arrow_height)
3552 if (!last_child && !menu->tearoff_active)
3553 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3555 y = child_offset + child_height - height + arrow_height;
3556 if ((y > 0) && !menu->tearoff_active)
3558 /* Need upper arrow */
3559 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3560 y = child_offset + child_height - height + arrow_height;
3562 /* Ignore the enter event we might get if the pointer is on the menu
3564 menu_shell->ignore_enter = TRUE;
3565 gtk_menu_scroll_to (menu, y);
3573 gtk_menu_select_item (GtkMenuShell *menu_shell,
3574 GtkWidget *menu_item)
3576 GtkMenu *menu = GTK_MENU (menu_shell);
3578 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3579 gtk_menu_scroll_item_visible (menu_shell, menu_item);
3581 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3585 /* Reparent the menu, taking care of the refcounting
3587 * If unrealize is true we force a unrealize while reparenting the parent.
3588 * This can help eliminate flicker in some cases.
3590 * What happens is that when the menu is unrealized and then re-realized,
3591 * the allocations are as follows:
3593 * parent - 1x1 at (0,0)
3594 * child1 - 100x20 at (0,0)
3595 * child2 - 100x20 at (0,20)
3596 * child3 - 100x20 at (0,40)
3598 * That is, the parent is small but the children are full sized. Then,
3599 * when the queued_resize gets processed, the parent gets resized to
3602 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3603 * contains the following logic:
3605 * - if a move or resize operation on a window would change the clip
3606 * region on the children, then before the window is resized
3607 * the background for children is temporarily set to None, the
3608 * move/resize done, and the background for the children restored.
3610 * So, at the point where the parent is resized to final size, the
3611 * background for the children is temporarily None, and thus they
3612 * are not cleared to the background color and the previous background
3613 * (the image of the menu) is left in place.
3616 gtk_menu_reparent (GtkMenu *menu,
3617 GtkWidget *new_parent,
3620 GtkObject *object = GTK_OBJECT (menu);
3621 GtkWidget *widget = GTK_WIDGET (menu);
3622 gboolean was_floating = GTK_OBJECT_FLOATING (object);
3624 g_object_ref (object);
3625 gtk_object_sink (object);
3629 g_object_ref (object);
3630 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3631 gtk_container_add (GTK_CONTAINER (new_parent), widget);
3632 g_object_unref (object);
3635 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3638 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3640 g_object_unref (object);
3644 gtk_menu_show_all (GtkWidget *widget)
3646 /* Show children, but not self. */
3647 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3652 gtk_menu_hide_all (GtkWidget *widget)
3654 /* Hide children, but not self. */
3655 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3659 * gtk_menu_set_screen:
3660 * @menu: a #GtkMenu.
3661 * @screen: a #GdkScreen, or %NULL if the screen should be
3662 * determined by the widget the menu is attached to.
3664 * Sets the #GdkScreen on which the menu will be displayed.
3669 gtk_menu_set_screen (GtkMenu *menu,
3672 g_return_if_fail (GTK_IS_MENU (menu));
3673 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3675 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3679 menu_change_screen (menu, screen);
3683 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3685 attach_widget_screen_changed (attach_widget, NULL, menu);
3691 * @menu: a #GtkMenu.
3692 * @child: a #GtkMenuItem.
3693 * @left_attach: The column number to attach the left side of the item to.
3694 * @right_attach: The column number to attach the right side of the item to.
3695 * @top_attach: The row number to attach the top of the item to.
3696 * @bottom_attach: The row number to attach the bottom of the item to.
3698 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3699 * an item will occupy is specified by @left_attach, @right_attach,
3700 * @top_attach and @bottom_attach. These each represent the leftmost,
3701 * rightmost, uppermost and lower column and row numbers of the table.
3702 * (Columns and rows are indexed from zero).
3704 * Note that this function is not related to gtk_menu_detach().
3709 gtk_menu_attach (GtkMenu *menu,
3714 guint bottom_attach)
3716 g_return_if_fail (GTK_IS_MENU (menu));
3717 g_return_if_fail (GTK_IS_MENU_ITEM (child));
3718 g_return_if_fail (child->parent == NULL ||
3719 child->parent == GTK_WIDGET (menu));
3720 g_return_if_fail (left_attach < right_attach);
3721 g_return_if_fail (top_attach < bottom_attach);
3725 GTK_MENU_SHELL (menu)->children =
3726 g_list_append (GTK_MENU_SHELL (menu)->children, child);
3728 gtk_widget_set_parent (child, GTK_WIDGET (menu));
3731 gtk_container_child_set (GTK_CONTAINER (menu), child,
3732 "left_attach", left_attach,
3733 "right_attach", right_attach,
3734 "top_attach", top_attach,
3735 "bottom_attach", bottom_attach,
3740 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
3744 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
3745 "gtk-menu-popup-delay", &popup_delay,
3752 find_child_containing (GtkMenuShell *menu_shell,
3760 /* find a child which includes the area given by
3761 * left, right, top, bottom.
3764 for (list = menu_shell->children; list; list = list->next)
3768 if (!_gtk_menu_item_is_selectable (list->data))
3771 get_child_attach (list->data, &l, &r, &t, &b);
3773 if (l <= left && right <= r
3774 && t <= top && bottom <= b)
3775 return GTK_WIDGET (list->data);
3782 gtk_menu_move_current (GtkMenuShell *menu_shell,
3783 GtkMenuDirectionType direction)
3785 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu_shell));
3787 /* use special table menu key bindings */
3788 if (menu_shell->active_menu_item && priv->columns > 1)
3792 gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
3793 GtkWidget *match = NULL;
3795 get_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
3797 if (direction == GTK_MENU_DIR_NEXT)
3799 for (i = b; i < priv->rows; i++)
3801 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
3809 for (i = 0; i < t; i++)
3811 match = find_child_containing (menu_shell,
3812 l, l + 1, i, i + 1);
3818 else if (direction == GTK_MENU_DIR_PREV)
3820 for (i = t; i > 0; i--)
3822 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
3830 for (i = priv->rows; i > b; i--)
3832 match = find_child_containing (menu_shell,
3833 l, l + 1, i - 1, i);
3839 else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
3840 || (rtl && direction == GTK_MENU_DIR_CHILD))
3842 /* we go one left if possible */
3844 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
3848 GtkWidget *parent = menu_shell->parent_menu_shell;
3851 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
3852 match = menu_shell->active_menu_item;
3855 else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
3856 || (rtl && direction == GTK_MENU_DIR_PARENT))
3858 /* we go one right if possible */
3859 if (r < priv->columns)
3860 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
3864 GtkWidget *parent = menu_shell->parent_menu_shell;
3866 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
3868 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
3869 match = menu_shell->active_menu_item;
3875 gtk_menu_shell_select_item (menu_shell, match);
3880 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
3884 get_visible_size (GtkMenu *menu)
3886 GtkWidget *widget = GTK_WIDGET (menu);
3887 GtkContainer *container = GTK_CONTAINER (menu);
3889 gint menu_height = (widget->allocation.height
3890 - 2 * (container->border_width
3891 + widget->style->ythickness));
3893 if (menu->upper_arrow_visible && !menu->tearoff_active)
3894 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3895 if (menu->lower_arrow_visible && !menu->tearoff_active)
3896 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3901 /* Find the sensitive on-screen child containing @y, or if none,
3902 * the nearest selectable onscreen child. (%NULL if none)
3905 child_at (GtkMenu *menu,
3908 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3909 GtkWidget *child = NULL;
3910 gint child_offset = 0;
3913 gint lower, upper; /* Onscreen bounds */
3915 menu_height = get_visible_size (menu);
3916 lower = menu->scroll_offset;
3917 upper = menu->scroll_offset + menu_height;
3919 for (children = menu_shell->children; children; children = children->next)
3921 if (GTK_WIDGET_VISIBLE (children->data))
3923 GtkRequisition child_requisition;
3925 gtk_widget_size_request (children->data, &child_requisition);
3927 if (_gtk_menu_item_is_selectable (children->data) &&
3928 child_offset >= lower &&
3929 child_offset + child_requisition.height <= upper)
3931 child = children->data;
3933 if (child_offset + child_requisition.height > y &&
3934 !GTK_IS_TEAROFF_MENU_ITEM (child))
3938 child_offset += child_requisition.height;
3946 get_menu_height (GtkMenu *menu)
3949 GtkWidget *widget = GTK_WIDGET (menu);
3951 height = widget->requisition.height;
3952 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
3954 if (menu->upper_arrow_visible && !menu->tearoff_active)
3955 height -= MENU_SCROLL_ARROW_HEIGHT;
3957 if (menu->lower_arrow_visible && !menu->tearoff_active)
3958 height -= MENU_SCROLL_ARROW_HEIGHT;
3964 gtk_menu_real_move_scroll (GtkMenu *menu,
3967 gint page_size = get_visible_size (menu);
3968 gint end_position = get_menu_height (menu);
3969 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3973 case GTK_SCROLL_PAGE_UP:
3974 case GTK_SCROLL_PAGE_DOWN:
3978 gint child_offset = 0;
3979 gboolean old_upper_arrow_visible;
3982 if (type == GTK_SCROLL_PAGE_UP)
3987 if (menu_shell->active_menu_item)
3991 compute_child_offset (menu, menu_shell->active_menu_item,
3992 &child_offset, &child_height, NULL);
3993 child_offset += child_height / 2;
3996 menu_shell->ignore_enter = TRUE;
3997 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
3998 old_offset = menu->scroll_offset;
4000 new_offset = menu->scroll_offset + step;
4001 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4003 gtk_menu_scroll_to (menu, new_offset);
4005 if (menu_shell->active_menu_item)
4007 GtkWidget *new_child;
4008 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4010 if (menu->scroll_offset != old_offset)
4011 step = menu->scroll_offset - old_offset;
4013 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
4015 new_child = child_at (menu, child_offset + step);
4017 gtk_menu_shell_select_item (menu_shell, new_child);
4021 case GTK_SCROLL_START:
4022 /* Ignore the enter event we might get if the pointer is on the menu
4024 menu_shell->ignore_enter = TRUE;
4025 gtk_menu_scroll_to (menu, 0);
4026 gtk_menu_shell_select_first (menu_shell, TRUE);
4028 case GTK_SCROLL_END:
4029 /* Ignore the enter event we might get if the pointer is on the menu
4031 menu_shell->ignore_enter = TRUE;
4032 gtk_menu_scroll_to (menu, end_position - page_size);
4033 _gtk_menu_shell_select_last (menu_shell, TRUE);
4042 * gtk_menu_set_monitor:
4044 * @monitor_num: the number of the monitor on which the menu should
4047 * Informs GTK+ on which monitor a menu should be popped up.
4048 * See gdk_screen_get_monitor_geometry().
4050 * This function should be called from a #GtkMenuPositionFunc if the
4051 * menu should not appear on the same monitor as the pointer. This
4052 * information can't be reliably inferred from the coordinates returned
4053 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4054 * may extend beyond the monitor boundaries or even the screen boundaries.
4058 void gtk_menu_set_monitor (GtkMenu *menu,
4061 GtkMenuPrivate *priv;
4062 g_return_if_fail (GTK_IS_MENU (menu));
4064 priv = gtk_menu_get_private (menu);
4066 priv->monitor_num = monitor_num;