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
30 #include <string.h> /* memset */
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkaccellabel.h"
33 #include "gtkaccelmap.h"
34 #include "gtkbindings.h"
37 #include "gtkmarshalers.h"
39 #include "gtkmenuitem.h"
40 #include "gtktearoffmenuitem.h"
41 #include "gtkwindow.h"
43 #include "gtkvscrollbar.h"
44 #include "gtksettings.h"
48 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
49 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
51 #define DEFAULT_POPUP_DELAY 225
52 #define DEFAULT_POPDOWN_DELAY 1000
54 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
55 * extends below the submenu
58 #define MENU_SCROLL_STEP1 8
59 #define MENU_SCROLL_STEP2 15
60 #define MENU_SCROLL_ARROW_HEIGHT 16
61 #define MENU_SCROLL_FAST_ZONE 8
62 #define MENU_SCROLL_TIMEOUT1 50
63 #define MENU_SCROLL_TIMEOUT2 50
65 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
67 typedef struct _GtkMenuAttachData GtkMenuAttachData;
68 typedef struct _GtkMenuPrivate GtkMenuPrivate;
70 struct _GtkMenuAttachData
72 GtkWidget *attach_widget;
73 GtkMenuDetachFunc detacher;
76 struct _GtkMenuPrivate
78 gboolean have_position;
82 /* info used for the table */
114 CHILD_PROP_LEFT_ATTACH,
115 CHILD_PROP_RIGHT_ATTACH,
116 CHILD_PROP_TOP_ATTACH,
117 CHILD_PROP_BOTTOM_ATTACH
120 static void gtk_menu_class_init (GtkMenuClass *klass);
121 static void gtk_menu_init (GtkMenu *menu);
122 static void gtk_menu_set_property (GObject *object,
126 static void gtk_menu_get_property (GObject *object,
130 static void gtk_menu_set_child_property(GtkContainer *container,
135 static void gtk_menu_get_child_property(GtkContainer *container,
140 static void gtk_menu_destroy (GtkObject *object);
141 static void gtk_menu_finalize (GObject *object);
142 static void gtk_menu_realize (GtkWidget *widget);
143 static void gtk_menu_unrealize (GtkWidget *widget);
144 static void gtk_menu_size_request (GtkWidget *widget,
145 GtkRequisition *requisition);
146 static void gtk_menu_size_allocate (GtkWidget *widget,
147 GtkAllocation *allocation);
148 static void gtk_menu_paint (GtkWidget *widget,
149 GdkEventExpose *expose);
150 static void gtk_menu_show (GtkWidget *widget);
151 static gboolean gtk_menu_expose (GtkWidget *widget,
152 GdkEventExpose *event);
153 static gboolean gtk_menu_key_press (GtkWidget *widget,
155 static gboolean gtk_menu_button_press (GtkWidget *widget,
156 GdkEventButton *event);
157 static gboolean gtk_menu_button_release (GtkWidget *widget,
158 GdkEventButton *event);
159 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
160 GdkEventMotion *event);
161 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
162 GdkEventCrossing *event);
163 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
164 GdkEventCrossing *event);
165 static void gtk_menu_scroll_to (GtkMenu *menu,
168 static void gtk_menu_stop_scrolling (GtkMenu *menu);
169 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
170 static gboolean gtk_menu_scroll_timeout (gpointer data);
172 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
173 GtkWidget *menu_item);
174 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
175 GtkWidget *menu_item);
176 static void gtk_menu_do_insert (GtkMenuShell *menu_shell,
179 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
182 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
184 static void gtk_menu_handle_scrolling (GtkMenu *menu,
186 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
188 static void gtk_menu_style_set (GtkWidget *widget,
189 GtkStyle *previous_style);
190 static gboolean gtk_menu_focus (GtkWidget *widget,
191 GtkDirectionType direction);
192 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
193 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
194 GtkMenuDirectionType direction);
195 static void gtk_menu_real_move_scroll (GtkMenu *menu,
198 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
199 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
200 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
203 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
204 GtkMenuItem *menu_item,
205 GdkEventCrossing *event);
207 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
208 static void gtk_menu_show_all (GtkWidget *widget);
209 static void gtk_menu_hide_all (GtkWidget *widget);
210 static void gtk_menu_position (GtkMenu *menu);
211 static void gtk_menu_reparent (GtkMenu *menu,
212 GtkWidget *new_parent,
214 static void gtk_menu_remove (GtkContainer *menu,
217 static void gtk_menu_update_title (GtkMenu *menu);
219 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
220 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
222 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
224 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
225 gboolean group_changed);
227 static GtkMenuShellClass *parent_class = NULL;
228 static const gchar *attach_data_key = "gtk-menu-attach-data";
230 static guint menu_signals[LAST_SIGNAL] = { 0 };
233 gtk_menu_free_private (gpointer data)
235 GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
237 g_free (priv->heights);
243 gtk_menu_get_private (GtkMenu *menu)
245 GtkMenuPrivate *private;
246 static GQuark private_quark = 0;
249 private_quark = g_quark_from_static_string ("gtk-menu-private");
251 private = g_object_get_qdata (G_OBJECT (menu), private_quark);
255 private = g_new0 (GtkMenuPrivate, 1);
256 private->have_position = FALSE;
258 g_object_set_qdata_full (G_OBJECT (menu), private_quark,
259 private, gtk_menu_free_private);
266 gtk_menu_get_type (void)
268 static GType menu_type = 0;
272 static const GTypeInfo menu_info =
274 sizeof (GtkMenuClass),
275 NULL, /* base_init */
276 NULL, /* base_finalize */
277 (GClassInitFunc) gtk_menu_class_init,
278 NULL, /* class_finalize */
279 NULL, /* class_data */
282 (GInstanceInitFunc) gtk_menu_init,
285 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
293 gtk_menu_class_init (GtkMenuClass *class)
295 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
296 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
297 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
298 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
299 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
300 GtkBindingSet *binding_set;
302 parent_class = g_type_class_peek_parent (class);
304 gobject_class->finalize = gtk_menu_finalize;
305 gobject_class->set_property = gtk_menu_set_property;
306 gobject_class->get_property = gtk_menu_get_property;
308 object_class->destroy = gtk_menu_destroy;
310 widget_class->realize = gtk_menu_realize;
311 widget_class->unrealize = gtk_menu_unrealize;
312 widget_class->size_request = gtk_menu_size_request;
313 widget_class->size_allocate = gtk_menu_size_allocate;
314 widget_class->show = gtk_menu_show;
315 widget_class->expose_event = gtk_menu_expose;
316 widget_class->key_press_event = gtk_menu_key_press;
317 widget_class->button_press_event = gtk_menu_button_press;
318 widget_class->button_release_event = gtk_menu_button_release;
319 widget_class->motion_notify_event = gtk_menu_motion_notify;
320 widget_class->show_all = gtk_menu_show_all;
321 widget_class->hide_all = gtk_menu_hide_all;
322 widget_class->enter_notify_event = gtk_menu_enter_notify;
323 widget_class->leave_notify_event = gtk_menu_leave_notify;
324 widget_class->motion_notify_event = gtk_menu_motion_notify;
325 widget_class->style_set = gtk_menu_style_set;
326 widget_class->focus = gtk_menu_focus;
327 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
329 container_class->remove = gtk_menu_remove;
330 container_class->get_child_property = gtk_menu_get_child_property;
331 container_class->set_child_property = gtk_menu_set_child_property;
333 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
334 menu_shell_class->deactivate = gtk_menu_deactivate;
335 menu_shell_class->select_item = gtk_menu_select_item;
336 menu_shell_class->insert = gtk_menu_real_insert;
337 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
338 menu_shell_class->move_current = gtk_menu_move_current;
340 menu_signals[MOVE_SCROLL] =
341 _gtk_binding_signal_new ("move_scroll",
342 G_OBJECT_CLASS_TYPE (object_class),
343 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
344 G_CALLBACK (gtk_menu_real_move_scroll),
346 _gtk_marshal_VOID__ENUM,
348 GTK_TYPE_SCROLL_TYPE);
350 g_object_class_install_property (gobject_class,
352 g_param_spec_string ("tearoff-title",
354 P_("A title that may be displayed by the window manager when this menu is torn-off"),
356 G_PARAM_READABLE | G_PARAM_WRITABLE));
358 gtk_widget_class_install_style_property (widget_class,
359 g_param_spec_int ("vertical-padding",
360 P_("Vertical Padding"),
361 P_("Extra space at the top and bottom of the menu"),
367 gtk_widget_class_install_style_property (widget_class,
368 g_param_spec_int ("vertical-offset",
369 P_("Vertical Offset"),
370 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
376 gtk_widget_class_install_style_property (widget_class,
377 g_param_spec_int ("horizontal-offset",
378 P_("Horizontal Offset"),
379 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
386 gtk_container_class_install_child_property (container_class,
387 CHILD_PROP_LEFT_ATTACH,
388 g_param_spec_uint ("left_attach",
390 P_("The column number to attach the left side of the child to"),
394 gtk_container_class_install_child_property (container_class,
395 CHILD_PROP_RIGHT_ATTACH,
396 g_param_spec_uint ("right_attach",
398 P_("The column number to attach the right side of the child to"),
402 gtk_container_class_install_child_property (container_class,
403 CHILD_PROP_TOP_ATTACH,
404 g_param_spec_uint ("top_attach",
406 P_("The row number to attach the top of the child to"),
410 gtk_container_class_install_child_property (container_class,
411 CHILD_PROP_BOTTOM_ATTACH,
412 g_param_spec_uint ("bottom_attach",
414 P_("The row number to attach the bottom of the child to"),
418 binding_set = gtk_binding_set_by_class (class);
419 gtk_binding_entry_add_signal (binding_set,
422 GTK_TYPE_MENU_DIRECTION_TYPE,
424 gtk_binding_entry_add_signal (binding_set,
427 GTK_TYPE_MENU_DIRECTION_TYPE,
429 gtk_binding_entry_add_signal (binding_set,
432 GTK_TYPE_MENU_DIRECTION_TYPE,
434 gtk_binding_entry_add_signal (binding_set,
437 GTK_TYPE_MENU_DIRECTION_TYPE,
439 gtk_binding_entry_add_signal (binding_set,
442 GTK_TYPE_MENU_DIRECTION_TYPE,
443 GTK_MENU_DIR_PARENT);
444 gtk_binding_entry_add_signal (binding_set,
447 GTK_TYPE_MENU_DIRECTION_TYPE,
448 GTK_MENU_DIR_PARENT);
449 gtk_binding_entry_add_signal (binding_set,
452 GTK_TYPE_MENU_DIRECTION_TYPE,
454 gtk_binding_entry_add_signal (binding_set,
457 GTK_TYPE_MENU_DIRECTION_TYPE,
459 gtk_binding_entry_add_signal (binding_set,
462 GTK_TYPE_SCROLL_TYPE,
464 gtk_binding_entry_add_signal (binding_set,
467 GTK_TYPE_SCROLL_TYPE,
469 gtk_binding_entry_add_signal (binding_set,
472 GTK_TYPE_SCROLL_TYPE,
474 gtk_binding_entry_add_signal (binding_set,
477 GTK_TYPE_SCROLL_TYPE,
479 gtk_binding_entry_add_signal (binding_set,
482 GTK_TYPE_SCROLL_TYPE,
484 gtk_binding_entry_add_signal (binding_set,
487 GTK_TYPE_SCROLL_TYPE,
489 gtk_binding_entry_add_signal (binding_set,
492 GTK_TYPE_SCROLL_TYPE,
493 GTK_SCROLL_PAGE_DOWN);
494 gtk_binding_entry_add_signal (binding_set,
497 GTK_TYPE_SCROLL_TYPE,
498 GTK_SCROLL_PAGE_DOWN);
500 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
501 P_("Can change accelerators"),
502 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
506 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
507 P_("Delay before submenus appear"),
508 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
514 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
515 P_("Delay before hiding a submenu"),
516 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
519 DEFAULT_POPDOWN_DELAY,
526 gtk_menu_set_property (GObject *object,
533 menu = GTK_MENU (object);
537 case PROP_TEAROFF_TITLE:
538 gtk_menu_set_title (menu, g_value_get_string (value));
541 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
547 gtk_menu_get_property (GObject *object,
554 menu = GTK_MENU (object);
558 case PROP_TEAROFF_TITLE:
559 g_value_set_string (value, gtk_menu_get_title (menu));
562 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
568 get_attach_info (GObject *child)
570 AttachInfo *ai = g_object_get_data (child, ATTACH_INFO_KEY);
574 ai = g_new0 (AttachInfo, 1);
575 g_object_set_data_full (child, ATTACH_INFO_KEY, ai, g_free);
582 gtk_menu_set_child_property (GtkContainer *container,
588 GtkMenu *menu = GTK_MENU (container);
589 GtkMenuPrivate *priv;
590 AttachInfo *ai = get_attach_info (G_OBJECT (child));
592 priv = gtk_menu_get_private (menu);
596 case CHILD_PROP_LEFT_ATTACH:
597 ai->left_attach = g_value_get_uint (value);
599 case CHILD_PROP_RIGHT_ATTACH:
600 ai->right_attach = g_value_get_uint (value);
601 priv->columns = MAX (priv->columns, ai->right_attach);
603 case CHILD_PROP_TOP_ATTACH:
604 ai->top_attach = g_value_get_uint (value);
606 case CHILD_PROP_BOTTOM_ATTACH:
607 ai->bottom_attach = g_value_get_uint (value);
608 priv->rows = MAX (priv->rows, ai->bottom_attach);
611 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
615 gtk_widget_queue_resize (GTK_WIDGET (menu));
619 gtk_menu_get_child_property (GtkContainer *container,
625 AttachInfo *ai = get_attach_info (G_OBJECT (child));
629 case CHILD_PROP_LEFT_ATTACH:
630 g_value_set_uint (value, ai->left_attach);
632 case CHILD_PROP_RIGHT_ATTACH:
633 g_value_set_uint (value, ai->right_attach);
635 case CHILD_PROP_TOP_ATTACH:
636 g_value_set_uint (value, ai->top_attach);
638 case CHILD_PROP_BOTTOM_ATTACH:
639 g_value_set_uint (value, ai->bottom_attach);
643 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
649 get_child_attach (GtkWidget *child,
655 gtk_container_child_get (GTK_CONTAINER (child->parent), child,
664 gtk_menu_window_event (GtkWidget *window,
668 gboolean handled = FALSE;
670 g_object_ref (window);
676 case GDK_KEY_RELEASE:
677 handled = gtk_widget_event (menu, event);
683 g_object_unref (window);
684 g_object_unref (menu);
690 gtk_menu_window_size_request (GtkWidget *window,
691 GtkRequisition *requisition,
694 GtkMenuPrivate *private = gtk_menu_get_private (menu);
696 if (private->have_position)
698 GdkScreen *screen = gtk_widget_get_screen (window);
699 GdkRectangle monitor;
701 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
703 if (private->y + requisition->height > monitor.y + monitor.height)
704 requisition->height = monitor.y + monitor.height - private->y;
706 if (private->y < monitor.y)
707 requisition->height -= monitor.y - private->y;
712 gtk_menu_init (GtkMenu *menu)
714 GtkMenuPrivate *priv = gtk_menu_get_private (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;
766 gtk_menu_destroy (GtkObject *object)
769 GtkMenuAttachData *data;
771 g_return_if_fail (GTK_IS_MENU (object));
773 menu = GTK_MENU (object);
775 gtk_menu_stop_scrolling (menu);
777 data = g_object_get_data (G_OBJECT (object), attach_data_key);
779 gtk_menu_detach (menu);
781 gtk_menu_stop_navigating_submenu (menu);
783 if (menu->old_active_menu_item)
785 g_object_unref (menu->old_active_menu_item);
786 menu->old_active_menu_item = NULL;
789 /* Add back the reference count for being a child */
790 if (menu->needs_destruction_ref_count)
792 menu->needs_destruction_ref_count = FALSE;
793 g_object_ref (object);
796 if (menu->accel_group)
798 g_object_unref (menu->accel_group);
799 menu->accel_group = NULL;
803 gtk_widget_destroy (menu->toplevel);
804 if (menu->tearoff_window)
805 gtk_widget_destroy (menu->tearoff_window);
807 GTK_OBJECT_CLASS (parent_class)->destroy (object);
811 gtk_menu_finalize (GObject *object)
813 GtkMenu *menu = GTK_MENU (object);
815 g_free (menu->accel_path);
817 G_OBJECT_CLASS (parent_class)->finalize (object);
821 menu_change_screen (GtkMenu *menu,
822 GdkScreen *new_screen)
824 GtkMenuPrivate *private = gtk_menu_get_private (menu);
828 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
829 gtk_menu_position (menu);
832 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
833 private->monitor_num = -1;
837 attach_widget_screen_changed (GtkWidget *attach_widget,
838 GdkScreen *previous_screen,
841 if (gtk_widget_has_screen (attach_widget) &&
842 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
844 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
849 gtk_menu_attach_to_widget (GtkMenu *menu,
850 GtkWidget *attach_widget,
851 GtkMenuDetachFunc detacher)
853 GtkMenuAttachData *data;
855 g_return_if_fail (GTK_IS_MENU (menu));
856 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
857 g_return_if_fail (detacher != NULL);
859 /* keep this function in sync with gtk_widget_set_parent()
862 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
865 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
866 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
871 gtk_object_sink (GTK_OBJECT (menu));
873 data = g_new (GtkMenuAttachData, 1);
874 data->attach_widget = attach_widget;
876 g_signal_connect (attach_widget, "screen_changed",
877 G_CALLBACK (attach_widget_screen_changed), menu);
878 attach_widget_screen_changed (attach_widget, NULL, menu);
880 data->detacher = detacher;
881 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
883 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
884 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
886 /* we don't need to set the style here, since
887 * we are a toplevel widget.
890 /* Fallback title for menu comes from attach widget */
891 gtk_menu_update_title (menu);
895 gtk_menu_get_attach_widget (GtkMenu *menu)
897 GtkMenuAttachData *data;
899 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
901 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
903 return data->attach_widget;
908 gtk_menu_detach (GtkMenu *menu)
910 GtkMenuAttachData *data;
912 g_return_if_fail (GTK_IS_MENU (menu));
914 /* keep this function in sync with gtk_widget_unparent()
916 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
919 g_warning ("gtk_menu_detach(): menu is not attached");
922 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
924 g_signal_handlers_disconnect_by_func (data->attach_widget,
925 (gpointer) attach_widget_screen_changed,
928 data->detacher (data->attach_widget, menu);
930 if (GTK_WIDGET_REALIZED (menu))
931 gtk_widget_unrealize (GTK_WIDGET (menu));
935 /* Fallback title for menu comes from attach widget */
936 gtk_menu_update_title (menu);
938 g_object_unref (menu);
942 gtk_menu_do_remove (GtkMenuShell *menu_shell,
947 GtkMenuPrivate *priv;
949 gboolean single_column;
951 ai = get_attach_info (G_OBJECT (child));
952 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
953 delta = ai->bottom_attach - ai->top_attach;
954 single_column = priv->columns == 1;
956 /* Recalculate these, assuming the child has already been removed.
957 * Note that an empty menu is assumed to have one column
962 for (children = menu_shell->children; children; children = children->next)
964 AttachInfo *child_ai = get_attach_info (children->data);
966 /* Do not move items in table menus */
967 if (single_column && child_ai->bottom_attach > ai->bottom_attach)
968 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
969 "top_attach", child_ai->top_attach - delta,
970 "bottom_attach", child_ai->bottom_attach - delta,
974 priv->columns = MAX (priv->columns, child_ai->right_attach);
975 priv->rows = MAX (priv->rows, child_ai->bottom_attach);
981 gtk_menu_remove (GtkContainer *container,
986 g_return_if_fail (GTK_IS_MENU (container));
987 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
989 menu = GTK_MENU (container);
991 /* Clear out old_active_menu_item if it matches the item we are removing
993 if (menu->old_active_menu_item == widget)
995 g_object_unref (menu->old_active_menu_item);
996 menu->old_active_menu_item = NULL;
999 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1000 gtk_menu_do_remove (GTK_MENU_SHELL (container), widget);
1001 g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
1008 return g_object_new (GTK_TYPE_MENU, NULL);
1012 gtk_menu_do_insert (GtkMenuShell *menu_shell,
1017 GtkMenuPrivate *priv;
1019 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
1021 if (position < 0 || position >= priv->rows)
1023 /* attach after the last row */
1024 gtk_menu_attach (GTK_MENU (menu_shell), child,
1026 priv->rows, priv->rows + 1);
1031 /* We need to make space for this new item; move all items with
1032 * top >= position one down.
1033 * Note that this does not prevent overlaps when the menu contains
1034 * vertically spanning items.
1036 for (children = menu_shell->children; children; children = children->next)
1038 AttachInfo *child_ai = get_attach_info (children->data);
1040 if (child_ai->top_attach >= position)
1041 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
1042 "top_attach", child_ai->top_attach + 1,
1043 "bottom_attach", child_ai->bottom_attach + 1,
1047 /* attach the new item */
1048 gtk_menu_attach (GTK_MENU (menu_shell), child,
1050 position, position + 1);
1054 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1058 if (GTK_WIDGET_REALIZED (menu_shell))
1059 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
1061 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1063 gtk_menu_do_insert (menu_shell, child, position);
1067 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1072 widget = GTK_WIDGET (menu);
1078 GdkGCValues gc_values;
1080 menu->tearoff_active = FALSE;
1081 menu->saved_scroll_offset = menu->scroll_offset;
1083 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1084 gc = gdk_gc_new_with_values (widget->window,
1085 &gc_values, GDK_GC_SUBWINDOW);
1087 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1089 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1094 gdk_draw_drawable (pixmap, gc,
1095 menu->tearoff_window->window,
1096 0, 0, 0, 0, -1, -1);
1097 g_object_unref (gc);
1099 gtk_widget_set_size_request (menu->tearoff_window,
1103 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1104 g_object_unref (pixmap);
1109 popup_grab_on_window (GdkWindow *window,
1110 guint32 activate_time)
1112 if ((gdk_pointer_grab (window, TRUE,
1113 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1114 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1115 GDK_POINTER_MOTION_MASK,
1116 NULL, NULL, activate_time) == 0))
1118 if (gdk_keyboard_grab (window, TRUE,
1119 activate_time) == 0)
1123 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1134 * @menu: a #GtkMenu.
1135 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1136 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1137 * @func: a user supplied function used to position the menu, or %NULL
1138 * @data: user supplied data to be passed to @func.
1139 * @button: the mouse button which was pressed to initiate the event.
1140 * @activate_time: the time at which the activation event occurred.
1142 * Displays a menu and makes it available for selection. Applications can use
1143 * this function to display context-sensitive menus, and will typically supply
1144 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1145 * parameters. The default menu positioning function will position the menu
1146 * at the current mouse cursor position.
1148 * The @button parameter should be the mouse button pressed to initiate
1149 * the menu popup. If the menu popup was initiated by something other than
1150 * a mouse button press, such as a mouse button release or a keypress,
1151 * @button should be 0.
1153 * The @activate_time parameter should be the time stamp of the event that
1154 * initiated the popup. If such an event is not available, use
1155 * gtk_get_current_event_time() instead.
1159 gtk_menu_popup (GtkMenu *menu,
1160 GtkWidget *parent_menu_shell,
1161 GtkWidget *parent_menu_item,
1162 GtkMenuPositionFunc func,
1165 guint32 activate_time)
1168 GtkWidget *xgrab_shell;
1170 GdkEvent *current_event;
1171 GtkMenuShell *menu_shell;
1173 g_return_if_fail (GTK_IS_MENU (menu));
1175 widget = GTK_WIDGET (menu);
1176 menu_shell = GTK_MENU_SHELL (menu);
1178 menu_shell->parent_menu_shell = parent_menu_shell;
1180 /* Find the last viewable ancestor, and make an X grab on it
1182 parent = GTK_WIDGET (menu);
1186 gboolean viewable = TRUE;
1187 GtkWidget *tmp = parent;
1191 if (!GTK_WIDGET_MAPPED (tmp))
1200 xgrab_shell = parent;
1202 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1205 /* We want to receive events generated when we map the menu; unfortunately,
1206 * since there is probably already an implicit grab in place from the
1207 * button that the user used to pop up the menu, we won't receive then --
1208 * in particular, the EnterNotify when the menu pops up under the pointer.
1210 * If we are grabbing on a parent menu shell, no problem; just grab on
1211 * that menu shell first before popping up the window with owner_events = TRUE.
1213 * When grabbing on the menu itself, things get more convuluted - we
1214 * we do an explicit grab on a specially created window with
1215 * owner_events = TRUE, which we override further down with a grab
1216 * on the menu. (We can't grab on the menu until it is mapped; we
1217 * probably could just leave the grab on the other window, with a
1218 * little reorganization of the code in gtkmenu*).
1220 if (xgrab_shell && xgrab_shell != widget)
1222 if (popup_grab_on_window (xgrab_shell->window, activate_time))
1223 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1227 GdkWindow *transfer_window;
1229 xgrab_shell = widget;
1230 transfer_window = menu_grab_transfer_window_get (menu);
1231 if (popup_grab_on_window (transfer_window, activate_time))
1232 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1235 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1237 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1238 * with a stuck up window, we just abort here. Presumably the user will
1241 menu_shell->parent_menu_shell = NULL;
1242 menu_grab_transfer_window_destroy (menu);
1246 menu_shell->active = TRUE;
1247 menu_shell->button = button;
1249 /* If we are popping up the menu from something other than, a button
1250 * press then, as a heuristic, we ignore enter events for the menu
1251 * until we get a MOTION_NOTIFY.
1254 current_event = gtk_get_current_event ();
1257 if ((current_event->type != GDK_BUTTON_PRESS) &&
1258 (current_event->type != GDK_ENTER_NOTIFY))
1259 menu_shell->ignore_enter = TRUE;
1261 gdk_event_free (current_event);
1264 menu_shell->ignore_enter = TRUE;
1268 gtk_menu_tearoff_bg_copy (menu);
1270 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1273 if (parent_menu_shell)
1275 GtkWidget *toplevel;
1277 toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1278 if (GTK_IS_WINDOW (toplevel))
1279 gtk_window_group_add_window (_gtk_window_get_group (GTK_WINDOW (toplevel)),
1280 GTK_WINDOW (menu->toplevel));
1283 menu->parent_menu_item = parent_menu_item;
1284 menu->position_func = func;
1285 menu->position_func_data = data;
1286 menu_shell->activate_time = activate_time;
1288 /* We need to show the menu here rather in the init function because
1289 * code expects to be able to tell if the menu is onscreen by
1290 * looking at the GTK_WIDGET_VISIBLE (menu)
1292 gtk_widget_show (GTK_WIDGET (menu));
1294 /* Position the menu, possibly changing the size request
1296 gtk_menu_position (menu);
1298 /* Compute the size of the toplevel and realize it so we
1299 * can scroll correctly.
1302 GtkRequisition tmp_request;
1303 GtkAllocation tmp_allocation = { 0, };
1305 gtk_widget_size_request (menu->toplevel, &tmp_request);
1307 tmp_allocation.width = tmp_request.width;
1308 tmp_allocation.height = tmp_request.height;
1310 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1312 gtk_widget_realize (GTK_WIDGET (menu));
1315 gtk_menu_scroll_to (menu, menu->scroll_offset);
1317 /* Once everything is set up correctly, map the toplevel window on
1320 gtk_widget_show (menu->toplevel);
1322 if (xgrab_shell == widget)
1323 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1324 gtk_grab_add (GTK_WIDGET (menu));
1328 gtk_menu_popdown (GtkMenu *menu)
1330 GtkMenuPrivate *private;
1331 GtkMenuShell *menu_shell;
1333 g_return_if_fail (GTK_IS_MENU (menu));
1335 menu_shell = GTK_MENU_SHELL (menu);
1336 private = gtk_menu_get_private (menu);
1338 menu_shell->parent_menu_shell = NULL;
1339 menu_shell->active = FALSE;
1340 menu_shell->ignore_enter = FALSE;
1342 private->have_position = FALSE;
1344 gtk_menu_stop_scrolling (menu);
1346 gtk_menu_stop_navigating_submenu (menu);
1348 if (menu_shell->active_menu_item)
1350 if (menu->old_active_menu_item)
1351 g_object_unref (menu->old_active_menu_item);
1352 menu->old_active_menu_item = menu_shell->active_menu_item;
1353 g_object_ref (menu->old_active_menu_item);
1356 gtk_menu_shell_deselect (menu_shell);
1358 /* The X Grab, if present, will automatically be removed when we hide
1360 gtk_widget_hide (menu->toplevel);
1361 gtk_window_group_add_window (_gtk_window_get_group (NULL), GTK_WINDOW (menu->toplevel));
1365 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1367 if (GTK_BIN (menu->toplevel)->child)
1369 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1373 /* We popped up the menu from the tearoff, so we need to
1374 * release the grab - we aren't actually hiding the menu.
1376 if (menu_shell->have_xgrab)
1378 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1380 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1381 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1385 /* gtk_menu_popdown is called each time a menu item is selected from
1386 * a torn off menu. Only scroll back to the saved position if the
1387 * non-tearoff menu was popped down.
1389 if (!menu->tearoff_active)
1390 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1391 menu->tearoff_active = TRUE;
1394 gtk_widget_hide (GTK_WIDGET (menu));
1396 menu_shell->have_xgrab = FALSE;
1397 gtk_grab_remove (GTK_WIDGET (menu));
1399 menu_grab_transfer_window_destroy (menu);
1403 gtk_menu_get_active (GtkMenu *menu)
1408 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1410 if (!menu->old_active_menu_item)
1413 children = GTK_MENU_SHELL (menu)->children;
1417 child = children->data;
1418 children = children->next;
1420 if (GTK_BIN (child)->child)
1425 menu->old_active_menu_item = child;
1426 if (menu->old_active_menu_item)
1427 g_object_ref (menu->old_active_menu_item);
1430 return menu->old_active_menu_item;
1434 gtk_menu_set_active (GtkMenu *menu,
1440 g_return_if_fail (GTK_IS_MENU (menu));
1442 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1445 child = tmp_list->data;
1446 if (GTK_BIN (child)->child)
1448 if (menu->old_active_menu_item)
1449 g_object_unref (menu->old_active_menu_item);
1450 menu->old_active_menu_item = child;
1451 g_object_ref (menu->old_active_menu_item);
1457 gtk_menu_set_accel_group (GtkMenu *menu,
1458 GtkAccelGroup *accel_group)
1460 g_return_if_fail (GTK_IS_MENU (menu));
1462 if (menu->accel_group != accel_group)
1464 if (menu->accel_group)
1465 g_object_unref (menu->accel_group);
1466 menu->accel_group = accel_group;
1467 if (menu->accel_group)
1468 g_object_ref (menu->accel_group);
1469 _gtk_menu_refresh_accel_paths (menu, TRUE);
1474 gtk_menu_get_accel_group (GtkMenu *menu)
1476 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1478 return menu->accel_group;
1482 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1485 /* Menu items chain here to figure whether they can activate their
1486 * accelerators. Unlike ordinary widgets, menus allow accel
1487 * activation even if invisible since that's the usual case for
1488 * submenus/popup-menus. however, the state of the attach widget
1489 * affects the "activeness" of the menu.
1491 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1494 return gtk_widget_can_activate_accel (awidget, signal_id);
1496 return GTK_WIDGET_IS_SENSITIVE (widget);
1500 * gtk_menu_set_accel_path
1501 * @menu: a valid #GtkMenu
1502 * @accel_path: a valid accelerator path
1504 * Sets an accelerator path for this menu from which accelerator paths
1505 * for its immediate children, its menu items, can be constructed.
1506 * The main purpose of this function is to spare the programmer the
1507 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1508 * each menu item that should support runtime user changable accelerators.
1509 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1510 * each menu item of this menu, that contains a label describing its purpose,
1511 * automatically gets an accel path assigned. For example, a menu containing
1512 * menu items "New" and "Exit", will, after
1513 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1514 * has been called, assign its items the accel paths:
1515 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1516 * Assigning accel paths to menu items then enables the user to change
1517 * their accelerators at runtime. More details about accelerator paths
1518 * and their default setups can be found at gtk_accel_map_add_entry().
1521 gtk_menu_set_accel_path (GtkMenu *menu,
1522 const gchar *accel_path)
1524 g_return_if_fail (GTK_IS_MENU (menu));
1526 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1528 g_free (menu->accel_path);
1529 menu->accel_path = g_strdup (accel_path);
1530 if (menu->accel_path)
1531 _gtk_menu_refresh_accel_paths (menu, FALSE);
1536 gboolean group_changed;
1540 refresh_accel_paths_foreach (GtkWidget *widget,
1543 AccelPropagation *prop = data;
1545 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1546 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1547 prop->menu->accel_path,
1548 prop->menu->accel_group,
1549 prop->group_changed);
1553 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1554 gboolean group_changed)
1556 g_return_if_fail (GTK_IS_MENU (menu));
1558 if (menu->accel_path && menu->accel_group)
1560 AccelPropagation prop;
1563 prop.group_changed = group_changed;
1564 gtk_container_foreach (GTK_CONTAINER (menu),
1565 refresh_accel_paths_foreach,
1571 gtk_menu_reposition (GtkMenu *menu)
1573 g_return_if_fail (GTK_IS_MENU (menu));
1575 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1576 gtk_menu_position (menu);
1580 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1583 g_return_if_fail (GTK_IS_MENU (menu));
1585 if (adjustment->value != menu->scroll_offset)
1586 gtk_menu_scroll_to (menu, adjustment->value);
1590 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1593 GdkGeometry geometry_hints;
1595 if (!menu->tearoff_window)
1598 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1600 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1601 width += menu->tearoff_scrollbar->requisition.width;
1604 geometry_hints.min_width = width;
1605 geometry_hints.max_width = width;
1607 geometry_hints.min_height = 0;
1608 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1610 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1613 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1617 gtk_menu_update_title (GtkMenu *menu)
1619 if (menu->tearoff_window)
1622 GtkWidget *attach_widget;
1624 title = gtk_menu_get_title (menu);
1627 attach_widget = gtk_menu_get_attach_widget (menu);
1628 if (GTK_IS_MENU_ITEM (attach_widget))
1630 GtkWidget *child = GTK_BIN (attach_widget)->child;
1631 if (GTK_IS_LABEL (child))
1632 title = gtk_label_get_text (GTK_LABEL (child));
1637 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1642 gtk_menu_get_toplevel (GtkWidget *menu)
1644 GtkWidget *attach, *toplevel;
1646 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1648 if (GTK_IS_MENU_ITEM (attach))
1649 attach = attach->parent;
1651 if (GTK_IS_MENU (attach))
1652 return gtk_menu_get_toplevel (attach->parent);
1653 else if (GTK_IS_WIDGET (attach))
1655 toplevel = gtk_widget_get_toplevel (attach->parent);
1656 if (GTK_WIDGET_TOPLEVEL (toplevel))
1664 gtk_menu_set_tearoff_state (GtkMenu *menu,
1669 g_return_if_fail (GTK_IS_MENU (menu));
1671 if (menu->torn_off != torn_off)
1673 menu->torn_off = torn_off;
1674 menu->tearoff_active = torn_off;
1678 if (GTK_WIDGET_VISIBLE (menu))
1679 gtk_menu_popdown (menu);
1681 if (!menu->tearoff_window)
1683 GtkWidget *toplevel;
1685 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1686 "type", GTK_WINDOW_TOPLEVEL,
1687 "screen", gtk_widget_get_screen (menu->toplevel),
1688 "app_paintable", TRUE,
1692 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1693 GDK_WINDOW_TYPE_HINT_MENU);
1694 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1695 g_signal_connect (menu->tearoff_window, "destroy",
1696 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1697 g_signal_connect (menu->tearoff_window, "event",
1698 G_CALLBACK (gtk_menu_window_event), menu);
1700 gtk_menu_update_title (menu);
1702 gtk_widget_realize (menu->tearoff_window);
1704 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1705 if (toplevel != NULL)
1706 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1707 GTK_WINDOW (toplevel));
1709 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1710 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1712 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1713 menu->tearoff_adjustment =
1714 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1716 GTK_WIDGET (menu)->requisition.height,
1720 g_object_connect (menu->tearoff_adjustment,
1721 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1723 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1725 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1726 menu->tearoff_scrollbar,
1729 if (menu->tearoff_adjustment->upper > height)
1730 gtk_widget_show (menu->tearoff_scrollbar);
1732 gtk_widget_show (menu->tearoff_hbox);
1735 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1737 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1739 /* Update menu->requisition
1741 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1743 gtk_menu_set_tearoff_hints (menu, width);
1745 gtk_widget_realize (menu->tearoff_window);
1746 gtk_menu_position (menu);
1748 gtk_widget_show (GTK_WIDGET (menu));
1749 gtk_widget_show (menu->tearoff_window);
1751 gtk_menu_scroll_to (menu, 0);
1756 gtk_widget_hide (GTK_WIDGET (menu));
1757 gtk_widget_hide (menu->tearoff_window);
1758 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1759 gtk_widget_destroy (menu->tearoff_window);
1761 menu->tearoff_window = NULL;
1762 menu->tearoff_hbox = NULL;
1763 menu->tearoff_scrollbar = NULL;
1764 menu->tearoff_adjustment = NULL;
1770 * gtk_menu_get_tearoff_state:
1773 * Returns whether the menu is torn off. See
1774 * gtk_menu_set_tearoff_state ().
1776 * Return value: %TRUE if the menu is currently torn off.
1779 gtk_menu_get_tearoff_state (GtkMenu *menu)
1781 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1783 return menu->torn_off;
1787 * gtk_menu_set_title:
1789 * @title: a string containing the title for the menu.
1791 * Sets the title string for the menu. The title is displayed when the menu
1792 * is shown as a tearoff menu.
1795 gtk_menu_set_title (GtkMenu *menu,
1798 g_return_if_fail (GTK_IS_MENU (menu));
1801 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1802 g_strdup (title), (GtkDestroyNotify) g_free);
1804 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1806 gtk_menu_update_title (menu);
1807 g_object_notify (G_OBJECT (menu), "tearoff_title");
1811 * gtk_menu_get_title:
1814 * Returns the title of the menu. See gtk_menu_set_title().
1816 * Return value: the title of the menu, or %NULL if the menu has no
1817 * title set on it. This string is owned by the widget and should
1818 * not be modified or freed.
1820 G_CONST_RETURN gchar *
1821 gtk_menu_get_title (GtkMenu *menu)
1823 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1825 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1829 gtk_menu_reorder_child (GtkMenu *menu,
1833 GtkMenuShell *menu_shell;
1835 g_return_if_fail (GTK_IS_MENU (menu));
1836 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1838 menu_shell = GTK_MENU_SHELL (menu);
1840 if (g_list_find (menu_shell->children, child))
1842 menu_shell->children = g_list_remove (menu_shell->children, child);
1843 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1844 gtk_menu_do_insert (menu_shell, child, position);
1846 if (GTK_WIDGET_VISIBLE (menu_shell))
1847 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1852 gtk_menu_style_set (GtkWidget *widget,
1853 GtkStyle *previous_style)
1855 if (GTK_WIDGET_REALIZED (widget))
1857 GtkMenu *menu = GTK_MENU (widget);
1859 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1860 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1861 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1866 gtk_menu_realize (GtkWidget *widget)
1868 GdkWindowAttr attributes;
1869 gint attributes_mask;
1874 guint vertical_padding;
1876 g_return_if_fail (GTK_IS_MENU (widget));
1878 menu = GTK_MENU (widget);
1880 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1882 attributes.window_type = GDK_WINDOW_CHILD;
1883 attributes.x = widget->allocation.x;
1884 attributes.y = widget->allocation.y;
1885 attributes.width = widget->allocation.width;
1886 attributes.height = widget->allocation.height;
1887 attributes.wclass = GDK_INPUT_OUTPUT;
1888 attributes.visual = gtk_widget_get_visual (widget);
1889 attributes.colormap = gtk_widget_get_colormap (widget);
1891 attributes.event_mask = gtk_widget_get_events (widget);
1893 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1894 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1896 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1897 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1898 gdk_window_set_user_data (widget->window, widget);
1900 border_width = GTK_CONTAINER (widget)->border_width;
1902 gtk_widget_style_get (GTK_WIDGET (menu),
1903 "vertical-padding", &vertical_padding,
1906 attributes.x = border_width + widget->style->xthickness;
1907 attributes.y = border_width + widget->style->ythickness + vertical_padding;
1908 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1909 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1911 if (menu->upper_arrow_visible)
1913 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1914 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1916 if (menu->lower_arrow_visible)
1917 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1919 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1920 gdk_window_set_user_data (menu->view_window, menu);
1924 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
1926 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1927 gdk_window_set_user_data (menu->bin_window, menu);
1929 children = GTK_MENU_SHELL (menu)->children;
1932 child = children->data;
1933 children = children->next;
1935 gtk_widget_set_parent_window (child, menu->bin_window);
1938 widget->style = gtk_style_attach (widget->style, widget->window);
1939 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1940 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1941 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1943 if (GTK_MENU_SHELL (widget)->active_menu_item)
1944 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1945 GTK_MENU_SHELL (widget)->active_menu_item);
1947 gdk_window_show (menu->bin_window);
1948 gdk_window_show (menu->view_window);
1952 gtk_menu_focus (GtkWidget *widget,
1953 GtkDirectionType direction)
1956 * A menu or its menu items cannot have focus
1961 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1964 menu_grab_transfer_window_get (GtkMenu *menu)
1966 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1969 GdkWindowAttr attributes;
1970 gint attributes_mask;
1972 attributes.x = -100;
1973 attributes.y = -100;
1974 attributes.width = 10;
1975 attributes.height = 10;
1976 attributes.window_type = GDK_WINDOW_TEMP;
1977 attributes.wclass = GDK_INPUT_ONLY;
1978 attributes.override_redirect = TRUE;
1979 attributes.event_mask = 0;
1981 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1983 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1984 &attributes, attributes_mask);
1985 gdk_window_set_user_data (window, menu);
1987 gdk_window_show (window);
1989 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1996 menu_grab_transfer_window_destroy (GtkMenu *menu)
1998 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2001 gdk_window_set_user_data (window, NULL);
2002 gdk_window_destroy (window);
2003 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
2008 gtk_menu_unrealize (GtkWidget *widget)
2012 g_return_if_fail (GTK_IS_MENU (widget));
2014 menu = GTK_MENU (widget);
2016 menu_grab_transfer_window_destroy (menu);
2018 gdk_window_set_user_data (menu->view_window, NULL);
2019 gdk_window_destroy (menu->view_window);
2020 menu->view_window = NULL;
2022 gdk_window_set_user_data (menu->bin_window, NULL);
2023 gdk_window_destroy (menu->bin_window);
2024 menu->bin_window = NULL;
2026 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2030 gtk_menu_size_request (GtkWidget *widget,
2031 GtkRequisition *requisition)
2035 GtkMenuShell *menu_shell;
2038 guint max_toggle_size;
2039 guint max_accel_width;
2040 guint vertical_padding;
2041 GtkRequisition child_requisition;
2042 GtkMenuPrivate *priv;
2044 g_return_if_fail (GTK_IS_MENU (widget));
2045 g_return_if_fail (requisition != NULL);
2047 menu = GTK_MENU (widget);
2048 menu_shell = GTK_MENU_SHELL (widget);
2049 priv = gtk_menu_get_private (menu);
2051 requisition->width = 0;
2052 requisition->height = 0;
2054 max_toggle_size = 0;
2055 max_accel_width = 0;
2057 g_free (priv->heights);
2058 priv->heights = g_new0 (guint, priv->rows);
2059 priv->heights_length = priv->rows;
2061 children = menu_shell->children;
2068 child = children->data;
2069 children = children->next;
2071 if (! GTK_WIDGET_VISIBLE (child))
2074 get_child_attach (child, &l, &r, &t, &b);
2076 /* It's important to size_request the child
2077 * before doing the toggle size request, in
2078 * case the toggle size request depends on the size
2079 * request of a child of the child (e.g. for ImageMenuItem)
2082 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2083 gtk_widget_size_request (child, &child_requisition);
2085 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2086 max_toggle_size = MAX (max_toggle_size, toggle_size);
2087 max_accel_width = MAX (max_accel_width,
2088 GTK_MENU_ITEM (child)->accelerator_width);
2090 part = child_requisition.width / (r - l);
2091 requisition->width = MAX (requisition->width, part);
2093 part = MAX (child_requisition.height, toggle_size) / (b - t);
2094 priv->heights[t] = MAX (priv->heights[t], part);
2097 for (i = 0; i < priv->rows; i++)
2098 requisition->height += priv->heights[i];
2100 requisition->width += max_toggle_size + max_accel_width;
2101 requisition->width *= priv->columns;
2102 requisition->width += (GTK_CONTAINER (menu)->border_width +
2103 widget->style->xthickness) * 2;
2105 gtk_widget_style_get (GTK_WIDGET (menu),
2106 "vertical-padding", &vertical_padding,
2108 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2109 widget->style->ythickness) * 2;
2111 menu->toggle_size = max_toggle_size;
2113 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2115 if (menu->tearoff_active)
2116 gtk_menu_set_tearoff_hints (menu, requisition->width);
2120 gtk_menu_size_allocate (GtkWidget *widget,
2121 GtkAllocation *allocation)
2124 GtkMenuShell *menu_shell;
2126 GtkAllocation child_allocation;
2127 GtkRequisition child_requisition;
2128 GtkMenuPrivate *priv;
2132 guint vertical_padding;
2134 g_return_if_fail (GTK_IS_MENU (widget));
2135 g_return_if_fail (allocation != NULL);
2137 menu = GTK_MENU (widget);
2138 menu_shell = GTK_MENU_SHELL (widget);
2139 priv = gtk_menu_get_private (menu);
2141 widget->allocation = *allocation;
2142 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2144 gtk_widget_style_get (GTK_WIDGET (menu),
2145 "vertical-padding", &vertical_padding,
2148 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2149 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2151 width = MAX (1, allocation->width - x * 2);
2152 height = MAX (1, allocation->height - y * 2);
2154 child_requisition.width -= x * 2;
2155 child_requisition.height -= y * 2;
2157 if (menu_shell->active)
2158 gtk_menu_scroll_to (menu, menu->scroll_offset);
2160 if (menu->upper_arrow_visible && !menu->tearoff_active)
2162 y += MENU_SCROLL_ARROW_HEIGHT;
2163 height -= MENU_SCROLL_ARROW_HEIGHT;
2166 if (menu->lower_arrow_visible && !menu->tearoff_active)
2167 height -= MENU_SCROLL_ARROW_HEIGHT;
2169 if (GTK_WIDGET_REALIZED (widget))
2171 gdk_window_move_resize (widget->window,
2172 allocation->x, allocation->y,
2173 allocation->width, allocation->height);
2175 gdk_window_move_resize (menu->view_window,
2182 if (menu_shell->children)
2184 gint base_width = width / priv->columns;
2186 children = menu_shell->children;
2189 child = children->data;
2190 children = children->next;
2192 if (GTK_WIDGET_VISIBLE (child))
2197 get_child_attach (child, &l, &r, &t, &b);
2199 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2202 tmp = priv->columns - l;
2203 l = priv->columns - r;
2207 child_allocation.width = (r - l) * base_width;
2208 child_allocation.height = 0;
2209 child_allocation.x = l * base_width;
2210 child_allocation.y = 0;
2212 for (i = 0; i < b; i++)
2215 child_allocation.y += priv->heights[i];
2217 child_allocation.height += priv->heights[i];
2220 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2223 gtk_widget_size_allocate (child, &child_allocation);
2224 gtk_widget_queue_draw (child);
2228 /* Resize the item window */
2229 if (GTK_WIDGET_REALIZED (widget))
2235 for (i = 0; i < priv->rows; i++)
2236 height += priv->heights[i];
2238 width = priv->columns * base_width;
2239 gdk_window_resize (menu->bin_window, width, height);
2242 if (menu->tearoff_active)
2244 if (allocation->height >= widget->requisition.height)
2246 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2248 gtk_widget_hide (menu->tearoff_scrollbar);
2249 gtk_menu_set_tearoff_hints (menu, allocation->width);
2251 gtk_menu_scroll_to (menu, 0);
2256 menu->tearoff_adjustment->upper = widget->requisition.height;
2257 menu->tearoff_adjustment->page_size = allocation->height;
2259 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2260 menu->tearoff_adjustment->upper)
2263 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2266 gtk_menu_scroll_to (menu, value);
2269 gtk_adjustment_changed (menu->tearoff_adjustment);
2271 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2273 gtk_widget_show (menu->tearoff_scrollbar);
2274 gtk_menu_set_tearoff_hints (menu, allocation->width);
2282 gtk_menu_paint (GtkWidget *widget,
2283 GdkEventExpose *event)
2287 gint border_x, border_y;
2288 guint vertical_padding;
2290 g_return_if_fail (GTK_IS_MENU (widget));
2292 menu = GTK_MENU (widget);
2294 gtk_widget_style_get (GTK_WIDGET (menu),
2295 "vertical-padding", &vertical_padding,
2298 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2299 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2300 gdk_drawable_get_size (widget->window, &width, &height);
2302 if (event->window == widget->window)
2304 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2305 gint arrow_size = 0.7 * arrow_space;
2307 gtk_paint_box (widget->style,
2311 NULL, widget, "menu",
2313 if (menu->upper_arrow_visible && !menu->tearoff_active)
2315 gtk_paint_box (widget->style,
2317 menu->upper_arrow_prelight ?
2318 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2320 NULL, widget, "menu",
2323 width - 2 * border_x,
2324 MENU_SCROLL_ARROW_HEIGHT);
2326 gtk_paint_arrow (widget->style,
2328 menu->upper_arrow_prelight ?
2329 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2331 NULL, widget, "menu_scroll_arrow_up",
2334 (width - arrow_size ) / 2,
2335 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2336 arrow_size, arrow_size);
2339 if (menu->lower_arrow_visible && !menu->tearoff_active)
2341 gtk_paint_box (widget->style,
2343 menu->lower_arrow_prelight ?
2344 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2346 NULL, widget, "menu",
2348 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2350 MENU_SCROLL_ARROW_HEIGHT);
2352 gtk_paint_arrow (widget->style,
2354 menu->lower_arrow_prelight ?
2355 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2357 NULL, widget, "menu_scroll_arrow_down",
2360 (width - arrow_size) / 2,
2361 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2362 widget->style->ythickness + (arrow_space - arrow_size)/2,
2363 arrow_size, arrow_size);
2369 gtk_menu_expose (GtkWidget *widget,
2370 GdkEventExpose *event)
2372 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2373 g_return_val_if_fail (event != NULL, FALSE);
2375 if (GTK_WIDGET_DRAWABLE (widget))
2377 gtk_menu_paint (widget, event);
2379 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2386 gtk_menu_show (GtkWidget *widget)
2388 GtkMenu *menu = GTK_MENU (widget);
2390 _gtk_menu_refresh_accel_paths (menu, FALSE);
2392 GTK_WIDGET_CLASS (parent_class)->show (widget);
2396 gtk_menu_button_press (GtkWidget *widget,
2397 GdkEventButton *event)
2399 /* Don't pop down the menu for releases over scroll arrows
2401 if (GTK_IS_MENU (widget))
2403 GtkMenu *menu = GTK_MENU (widget);
2405 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2409 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2413 gtk_menu_button_release (GtkWidget *widget,
2414 GdkEventButton *event)
2416 /* Don't pop down the menu for releases over scroll arrows
2418 if (GTK_IS_MENU (widget))
2420 GtkMenu *menu = GTK_MENU (widget);
2422 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2426 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2429 static const gchar *
2430 get_accel_path (GtkWidget *menu_item,
2435 GClosure *accel_closure;
2436 GtkAccelGroup *accel_group;
2438 path = _gtk_widget_get_accel_path (menu_item, locked);
2441 path = GTK_MENU_ITEM (menu_item)->accel_path;
2447 label = GTK_BIN (menu_item)->child;
2449 if (GTK_IS_ACCEL_LABEL (label))
2451 g_object_get (label,
2452 "accel_closure", &accel_closure,
2454 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2456 *locked = accel_group->lock_count > 0;
2465 gtk_menu_key_press (GtkWidget *widget,
2468 GtkMenuShell *menu_shell;
2470 gboolean delete = FALSE;
2471 gboolean can_change_accels;
2472 gchar *accel = NULL;
2473 guint accel_key, accel_mods;
2474 GdkModifierType consumed_modifiers;
2475 GdkDisplay *display;
2477 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2478 g_return_val_if_fail (event != NULL, FALSE);
2480 menu_shell = GTK_MENU_SHELL (widget);
2481 menu = GTK_MENU (widget);
2483 gtk_menu_stop_navigating_submenu (menu);
2485 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2488 display = gtk_widget_get_display (widget);
2490 g_object_get (gtk_widget_get_settings (widget),
2491 "gtk-menu-bar-accel", &accel,
2492 "gtk-can-change-accels", &can_change_accels,
2498 GdkModifierType mods = 0;
2499 gboolean handled = FALSE;
2501 gtk_accelerator_parse (accel, &keyval, &mods);
2504 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2506 /* FIXME this is wrong, needs to be in the global accel resolution
2507 * thing, to properly consider i18n etc., but that probably requires
2508 * AccelGroup changes etc.
2510 if (event->keyval == keyval &&
2511 (mods & event->state) == mods)
2512 gtk_menu_shell_cancel (menu_shell);
2520 switch (event->keyval)
2531 /* Figure out what modifiers went into determining the key symbol */
2532 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2533 event->hardware_keycode, event->state, event->group,
2534 NULL, NULL, NULL, &consumed_modifiers);
2536 accel_key = gdk_keyval_to_lower (event->keyval);
2537 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2539 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2540 * We re-upper case when we match against the keyval, but display and save in caseless form.
2542 if (accel_key != event->keyval)
2543 accel_mods |= GDK_SHIFT_MASK;
2545 /* Modify the accelerators */
2546 if (can_change_accels &&
2547 menu_shell->active_menu_item &&
2548 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2549 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2550 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2552 GtkWidget *menu_item = menu_shell->active_menu_item;
2553 gboolean locked, replace_accels = TRUE;
2556 path = get_accel_path (menu_item, &locked);
2557 if (!path || locked)
2559 /* can't change accelerators on menu_items without paths
2560 * (basically, those items are accelerator-locked).
2562 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2563 gdk_display_beep (display);
2569 /* For the keys that act to delete the current setting, we delete
2570 * the current setting if there is one, otherwise, we set the
2571 * key as the accelerator.
2577 if (gtk_accel_map_lookup_entry (path, &key) &&
2578 (key.accel_key || key.accel_mods))
2584 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2588 /* we failed, probably because this key is in use and
2591 /* g_print("failed to change\n"); */
2592 gdk_display_beep (display);
2601 gtk_menu_motion_notify (GtkWidget *widget,
2602 GdkEventMotion *event)
2604 GtkWidget *menu_item;
2606 GtkMenuShell *menu_shell;
2608 gboolean need_enter;
2610 if (GTK_IS_MENU (widget))
2611 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2613 /* We received the event for one of two reasons:
2615 * a) We are the active menu, and did gtk_grab_add()
2616 * b) The widget is a child of ours, and the event was propagated
2618 * Since for computation of navigation regions, we want the menu which
2619 * is the parent of the menu item, for a), we need to find that menu,
2620 * which may be different from 'widget'.
2622 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2623 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2624 !GTK_IS_MENU (menu_item->parent))
2627 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2628 menu = GTK_MENU (menu_shell);
2630 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2632 /* Check to see if we are within an active submenu's navigation region
2634 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2637 /* Make sure we pop down if we enter a non-selectable menu item, so we
2638 * don't show a submenu when the cursor is outside the stay-up triangle.
2640 if (!_gtk_menu_item_is_selectable (menu_item))
2642 gtk_menu_shell_deselect (menu_shell);
2648 /* The menu is now sensitive to enter events on its items, but
2649 * was previously sensitive. So we fake an enter event.
2653 menu_shell->ignore_enter = FALSE;
2655 gdk_drawable_get_size (event->window, &width, &height);
2656 if (event->x >= 0 && event->x < width &&
2657 event->y >= 0 && event->y < height)
2659 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2662 send_event->crossing.window = g_object_ref (event->window);
2663 send_event->crossing.time = event->time;
2664 send_event->crossing.send_event = TRUE;
2665 send_event->crossing.x_root = event->x_root;
2666 send_event->crossing.y_root = event->y_root;
2667 send_event->crossing.x = event->x;
2668 send_event->crossing.y = event->y;
2670 /* We send the event to 'widget', the currently active menu,
2671 * instead of 'menu', the menu that the pointer is in. This
2672 * will ensure that the event will be ignored unless the
2673 * menuitem is a child of the active menu or some parent
2674 * menu of the active menu.
2676 result = gtk_widget_event (widget, send_event);
2677 gdk_event_free (send_event);
2687 gtk_menu_scroll_timeout (gpointer data)
2692 gint view_width, view_height;
2694 GDK_THREADS_ENTER ();
2696 menu = GTK_MENU (data);
2697 widget = GTK_WIDGET (menu);
2699 offset = menu->scroll_offset + menu->scroll_step;
2701 /* If we scroll upward and the non-visible top part
2702 * is smaller than the scroll arrow it would be
2703 * pretty stupid to show the arrow and taking more
2704 * screen space than just scrolling to the top.
2706 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2709 /* Don't scroll over the top if we weren't before: */
2710 if ((menu->scroll_offset >= 0) && (offset < 0))
2713 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2715 /* Don't scroll past the bottom if we weren't before: */
2716 if (menu->scroll_offset > 0)
2717 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2719 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2720 (offset + view_height > widget->requisition.height))
2721 offset = widget->requisition.height - view_height;
2723 gtk_menu_scroll_to (menu, offset);
2725 GDK_THREADS_LEAVE ();
2731 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2733 GtkMenuShell *menu_shell;
2739 gboolean scroll_fast = FALSE;
2740 guint vertical_padding;
2742 menu_shell = GTK_MENU_SHELL (menu);
2744 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2745 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2747 gtk_widget_style_get (GTK_WIDGET (menu),
2748 "vertical-padding", &vertical_padding,
2751 border = GTK_CONTAINER (menu)->border_width +
2752 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2754 if (menu->upper_arrow_visible && !menu->tearoff_active)
2759 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2762 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2763 (y >= rect.y) && (y < rect.y + rect.height))
2766 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2769 if (enter && in_arrow &&
2770 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2772 menu->upper_arrow_prelight = TRUE;
2773 menu->scroll_fast = scroll_fast;
2774 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2776 /* Deselect the active item so that any submenus are poped down */
2777 gtk_menu_shell_deselect (menu_shell);
2779 gtk_menu_remove_scroll_timeout (menu);
2780 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2781 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2782 gtk_menu_scroll_timeout,
2785 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2787 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2789 gtk_menu_stop_scrolling (menu);
2793 if (menu->lower_arrow_visible && !menu->tearoff_active)
2796 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2798 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2801 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2802 (y >= rect.y) && (y < rect.y + rect.height))
2805 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2808 if (enter && in_arrow &&
2809 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2811 menu->lower_arrow_prelight = TRUE;
2812 menu->scroll_fast = scroll_fast;
2813 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2815 /* Deselect the active item so that any submenus are poped down */
2816 gtk_menu_shell_deselect (menu_shell);
2818 gtk_menu_remove_scroll_timeout (menu);
2819 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2820 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2821 gtk_menu_scroll_timeout,
2824 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2826 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2828 gtk_menu_stop_scrolling (menu);
2834 gtk_menu_enter_notify (GtkWidget *widget,
2835 GdkEventCrossing *event)
2837 GtkWidget *menu_item;
2839 if (widget && GTK_IS_MENU (widget))
2841 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2843 if (!menu_shell->ignore_enter)
2844 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2847 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2848 * will not correspond to the event widget's parent. Check to see
2849 * if we are in the parent's navigation region.
2851 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2852 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2853 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2856 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2860 gtk_menu_leave_notify (GtkWidget *widget,
2861 GdkEventCrossing *event)
2863 GtkMenuShell *menu_shell;
2865 GtkMenuItem *menu_item;
2866 GtkWidget *event_widget;
2868 menu = GTK_MENU (widget);
2869 menu_shell = GTK_MENU_SHELL (widget);
2871 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2874 gtk_menu_handle_scrolling (menu, FALSE);
2876 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2878 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2881 menu_item = GTK_MENU_ITEM (event_widget);
2883 /* Here we check to see if we're leaving an active menu item with a submenu,
2884 * in which case we enter submenu navigation mode.
2886 if (menu_shell->active_menu_item != NULL
2887 && menu_item->submenu != NULL
2888 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2890 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2892 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2895 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
2897 /* We are leaving an active menu item with nonactive submenu.
2898 * Deselect it so we don't surprise the user with by popping
2899 * up a submenu _after_ he left the item.
2901 gtk_menu_shell_deselect (menu_shell);
2906 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2910 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2912 if (menu->navigation_region)
2914 gdk_region_destroy (menu->navigation_region);
2915 menu->navigation_region = NULL;
2918 if (menu->navigation_timeout)
2920 g_source_remove (menu->navigation_timeout);
2921 menu->navigation_timeout = 0;
2925 /* When the timeout is elapsed, the navigation region is destroyed
2926 * and the menuitem under the pointer (if any) is selected.
2929 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2931 GtkMenu *menu = user_data;
2932 GdkWindow *child_window;
2934 GDK_THREADS_ENTER ();
2936 gtk_menu_stop_navigating_submenu (menu);
2938 if (GTK_WIDGET_REALIZED (menu))
2940 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2944 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2946 send_event->crossing.window = g_object_ref (child_window);
2947 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2948 send_event->crossing.send_event = TRUE;
2950 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2952 gdk_event_free (send_event);
2956 GDK_THREADS_LEAVE ();
2962 gtk_menu_navigating_submenu (GtkMenu *menu,
2966 if (menu->navigation_region)
2968 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2972 gtk_menu_stop_navigating_submenu (menu);
2979 #undef DRAW_STAY_UP_TRIANGLE
2981 #ifdef DRAW_STAY_UP_TRIANGLE
2984 draw_stay_up_triangle (GdkWindow *window,
2987 /* Draw ugly color all over the stay-up triangle */
2988 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2989 GdkGCValues gc_values;
2991 GdkRectangle clipbox;
2993 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2994 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2995 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2996 gdk_gc_set_clip_region (ugly_gc, region);
2998 gdk_region_get_clipbox (region, &clipbox);
3000 gdk_draw_rectangle (window,
3003 clipbox.x, clipbox.y,
3004 clipbox.width, clipbox.height);
3006 g_object_unref (ugly_gc);
3011 flip_region (GdkRegion *region,
3016 GdkRectangle *rectangles;
3017 GdkRectangle clipbox;
3018 GdkRegion *new_region;
3021 new_region = gdk_region_new ();
3023 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3024 gdk_region_get_clipbox (region, &clipbox);
3026 for (i = 0; i < n_rectangles; ++i)
3028 GdkRectangle rect = rectangles[i];
3031 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3034 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3036 gdk_region_union_with_rect (new_region, &rect);
3039 g_free (rectangles);
3045 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3046 GtkMenuItem *menu_item,
3047 GdkEventCrossing *event)
3049 gint submenu_left = 0;
3050 gint submenu_right = 0;
3051 gint submenu_top = 0;
3052 gint submenu_bottom = 0;
3056 GtkWidget *event_widget;
3058 g_return_if_fail (menu_item->submenu != NULL);
3059 g_return_if_fail (event != NULL);
3061 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3063 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3064 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3066 submenu_right = submenu_left + width;
3067 submenu_bottom = submenu_top + height;
3069 gdk_drawable_get_size (event_widget->window, &width, &height);
3071 if (event->x >= 0 && event->x < width)
3074 gboolean flip_y = FALSE;
3075 gboolean flip_x = FALSE;
3077 gtk_menu_stop_navigating_submenu (menu);
3079 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3082 point[0].x = event->x_root;
3083 point[1].x = submenu_left;
3088 point[0].x = event->x_root + 1;
3089 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3097 point[0].y = event->y_root + 1;
3098 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3100 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3108 point[0].y = event->y_root;
3109 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3111 if (point[0].y >= submenu_bottom)
3115 point[2].x = point[1].x;
3116 point[2].y = point[0].y;
3118 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3120 if (flip_x || flip_y)
3122 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3123 gdk_region_destroy (menu->navigation_region);
3124 menu->navigation_region = new_region;
3127 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3128 "gtk-menu-popdown-delay", &popdown_delay,
3131 menu->navigation_timeout = g_timeout_add (popdown_delay,
3132 gtk_menu_stop_navigating_submenu_cb, menu);
3134 #ifdef DRAW_STAY_UP_TRIANGLE
3135 draw_stay_up_triangle (gdk_get_default_root_window(),
3136 menu->navigation_region);
3142 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3146 g_return_if_fail (GTK_IS_MENU (menu_shell));
3148 parent = menu_shell->parent_menu_shell;
3150 menu_shell->activate_time = 0;
3151 gtk_menu_popdown (GTK_MENU (menu_shell));
3154 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3158 gtk_menu_position (GtkMenu *menu)
3161 GtkRequisition requisition;
3162 GtkMenuPrivate *private;
3168 GdkScreen *pointer_screen;
3169 GdkRectangle monitor;
3171 g_return_if_fail (GTK_IS_MENU (menu));
3173 widget = GTK_WIDGET (menu);
3175 screen = gtk_widget_get_screen (widget);
3176 gdk_display_get_pointer (gdk_screen_get_display (screen),
3177 &pointer_screen, &x, &y, NULL);
3179 /* We need the requisition to figure out the right place to
3180 * popup the menu. In fact, we always need to ask here, since
3181 * if a size_request was queued while we weren't popped up,
3182 * the requisition won't have been recomputed yet.
3184 gtk_widget_size_request (widget, &requisition);
3186 if (pointer_screen != screen)
3188 /* Pointer is on a different screen; roughly center the
3189 * menu on the screen. If someone was using multiscreen
3190 * + Xinerama together they'd probably want something
3191 * fancier; but that is likely to be vanishingly rare.
3193 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3194 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3197 private = gtk_menu_get_private (menu);
3198 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3202 if (menu->position_func)
3204 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3205 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3209 gint space_left, space_right, space_above, space_below;
3212 gint xthickness = widget->style->xthickness;
3213 gint ythickness = widget->style->ythickness;
3214 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3216 /* The placement of popup menus horizontally works like this (with
3217 * RTL in parentheses)
3219 * - If there is enough room to the right (left) of the mouse cursor,
3220 * position the menu there.
3222 * - Otherwise, if if there is enough room to the left (right) of the
3223 * mouse cursor, position the menu there.
3225 * - Otherwise if the menu is smaller than the monitor, position it
3226 * on the side of the mouse cursor that has the most space available
3228 * - Otherwise (if there is simply not enough room for the menu on the
3229 * monitor), position it as far left (right) as possible.
3231 * Positioning in the vertical direction is similar: first try below
3232 * mouse cursor, then above.
3234 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3236 space_left = x - monitor.x;
3237 space_right = monitor.x + monitor.width - x - 1;
3238 space_above = y - monitor.y;
3239 space_below = monitor.y + monitor.height - y - 1;
3241 /* position horizontally */
3243 /* the amount of space we need to position the menu. Note the
3244 * menu is offset "xthickness" pixels
3246 needed_width = requisition.width - xthickness;
3248 if (needed_width <= space_left ||
3249 needed_width <= space_right)
3251 if ((rtl && needed_width <= space_left) ||
3252 (!rtl && needed_width > space_right))
3255 x = x + xthickness - requisition.width + 1;
3259 /* position right */
3263 /* x is clamped on-screen further down */
3265 else if (requisition.width <= monitor.width)
3267 /* the menu is too big to fit on either side of the mouse
3268 * cursor, but smaller than the monitor. Position it on
3269 * the side that has the most space
3271 if (space_left > space_right)
3279 x = monitor.x + monitor.width - requisition.width;
3282 else /* menu is simply too big for the monitor */
3287 x = monitor.x + monitor.width - requisition.width;
3296 /* Position vertically. The algorithm is the same as above, but
3297 * simpler because we don't have to take RTL into account.
3299 needed_height = requisition.height - ythickness;
3301 if (needed_height <= space_above ||
3302 needed_height <= space_below)
3304 if (needed_height <= space_below)
3307 y = y + ythickness - requisition.height + 1;
3309 y = CLAMP (y, monitor.y,
3310 monitor.y + monitor.height - requisition.height);
3312 else if (needed_height > space_below && needed_height > space_above)
3314 if (space_below >= space_above)
3315 y = monitor.y + monitor.height - requisition.height;
3329 menu_height = GTK_WIDGET (menu)->requisition.height;
3331 if (y + menu_height > monitor.y + monitor.height)
3333 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3334 y = (monitor.y + monitor.height) - menu_height;
3339 scroll_offset += monitor.y - y;
3344 /* FIXME: should this be done in the various position_funcs ? */
3345 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3347 if (GTK_MENU_SHELL (menu)->active)
3349 private->have_position = TRUE;
3354 if (y + requisition.height > monitor.y + monitor.height)
3355 requisition.height = (monitor.y + monitor.height) - y;
3359 scroll_offset += monitor.y - y;
3360 requisition.height -= monitor.y - y;
3364 if (scroll_offset > 0)
3365 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3367 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3370 if (!GTK_MENU_SHELL (menu)->active)
3372 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3373 requisition.width, requisition.height);
3376 menu->scroll_offset = scroll_offset;
3380 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3382 if (menu->timeout_id)
3384 g_source_remove (menu->timeout_id);
3385 menu->timeout_id = 0;
3390 gtk_menu_stop_scrolling (GtkMenu *menu)
3392 gtk_menu_remove_scroll_timeout (menu);
3394 menu->upper_arrow_prelight = FALSE;
3395 menu->lower_arrow_prelight = FALSE;
3399 gtk_menu_scroll_to (GtkMenu *menu,
3404 gint view_width, view_height;
3406 gboolean last_visible;
3408 guint vertical_padding;
3410 widget = GTK_WIDGET (menu);
3412 if (menu->tearoff_active &&
3413 menu->tearoff_adjustment &&
3414 (menu->tearoff_adjustment->value != offset))
3416 menu->tearoff_adjustment->value =
3418 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3419 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3422 /* Move/resize the viewport according to arrows: */
3423 view_width = widget->allocation.width;
3424 view_height = widget->allocation.height;
3426 gtk_widget_style_get (GTK_WIDGET (menu),
3427 "vertical-padding", &vertical_padding,
3430 border_width = GTK_CONTAINER (menu)->border_width;
3431 view_width -= (border_width + widget->style->xthickness) * 2;
3432 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3433 menu_height = widget->requisition.height -
3434 (border_width + widget->style->ythickness + vertical_padding) * 2;
3436 x = border_width + widget->style->xthickness;
3437 y = border_width + widget->style->ythickness + vertical_padding;
3439 if (!menu->tearoff_active)
3441 last_visible = menu->upper_arrow_visible;
3442 menu->upper_arrow_visible = offset > 0;
3444 if (menu->upper_arrow_visible)
3445 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3447 if ( (last_visible != menu->upper_arrow_visible) &&
3448 !menu->upper_arrow_visible)
3450 menu->upper_arrow_prelight = FALSE;
3452 /* If we hid the upper arrow, possibly remove timeout */
3453 if (menu->scroll_step < 0)
3455 gtk_menu_stop_scrolling (menu);
3456 gtk_widget_queue_draw (GTK_WIDGET (menu));
3460 last_visible = menu->lower_arrow_visible;
3461 menu->lower_arrow_visible = offset < menu_height - view_height;
3463 if (menu->lower_arrow_visible)
3464 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3466 if ( (last_visible != menu->lower_arrow_visible) &&
3467 !menu->lower_arrow_visible)
3469 menu->lower_arrow_prelight = FALSE;
3471 /* If we hid the lower arrow, possibly remove timeout */
3472 if (menu->scroll_step > 0)
3474 gtk_menu_stop_scrolling (menu);
3475 gtk_widget_queue_draw (GTK_WIDGET (menu));
3479 if (menu->upper_arrow_visible)
3480 y += MENU_SCROLL_ARROW_HEIGHT;
3483 /* Scroll the menu: */
3484 if (GTK_WIDGET_REALIZED (menu))
3485 gdk_window_move (menu->bin_window, 0, -offset);
3487 if (GTK_WIDGET_REALIZED (menu))
3488 gdk_window_move_resize (menu->view_window,
3494 menu->scroll_offset = offset;
3498 compute_child_offset (GtkMenu *menu,
3499 GtkWidget *menu_item,
3502 gboolean *is_last_child)
3504 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3505 guint item_top_attach;
3506 guint item_bottom_attach;
3507 gint child_offset = 0;
3510 gtk_container_child_get (GTK_CONTAINER (menu), menu_item,
3511 "top_attach", &item_top_attach,
3512 "bottom_attach", &item_bottom_attach,
3515 /* there is a possibility that we get called before _size_request, so
3516 * check the height table for safety.
3518 if (!priv->heights || priv->heights_length < priv->rows)
3521 /* when we have a row with only invisible children, it's height will
3522 * be zero, so there's no need to check WIDGET_VISIBLE here
3524 for (i = 0; i < item_top_attach; i++)
3525 child_offset += priv->heights[i];
3528 *is_last_child = (item_bottom_attach == priv->rows);
3530 *offset = child_offset;
3532 *height = priv->heights[item_top_attach];
3538 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
3539 GtkWidget *menu_item)
3542 gint child_offset, child_height;
3546 gboolean last_child = 0;
3548 menu = GTK_MENU (menu_shell);
3550 /* We need to check if the selected item fully visible.
3551 * If not we need to scroll the menu so that it becomes fully
3555 if (compute_child_offset (menu, menu_item,
3556 &child_offset, &child_height, &last_child))
3558 guint vertical_padding;
3560 y = menu->scroll_offset;
3561 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3563 gtk_widget_style_get (GTK_WIDGET (menu),
3564 "vertical-padding", &vertical_padding,
3567 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3569 if (child_offset < y)
3571 /* Ignore the enter event we might get if the pointer is on the menu
3573 menu_shell->ignore_enter = TRUE;
3574 gtk_menu_scroll_to (menu, child_offset);
3579 if (menu->upper_arrow_visible && !menu->tearoff_active)
3580 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3581 if (menu->lower_arrow_visible && !menu->tearoff_active)
3582 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3584 if (child_offset + child_height > y + height - arrow_height)
3587 if (!last_child && !menu->tearoff_active)
3588 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3590 y = child_offset + child_height - height + arrow_height;
3591 if ((y > 0) && !menu->tearoff_active)
3593 /* Need upper arrow */
3594 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3595 y = child_offset + child_height - height + arrow_height;
3597 /* Ignore the enter event we might get if the pointer is on the menu
3599 menu_shell->ignore_enter = TRUE;
3600 gtk_menu_scroll_to (menu, y);
3608 gtk_menu_select_item (GtkMenuShell *menu_shell,
3609 GtkWidget *menu_item)
3611 GtkMenu *menu = GTK_MENU (menu_shell);
3613 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3614 gtk_menu_scroll_item_visible (menu_shell, menu_item);
3616 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3620 /* Reparent the menu, taking care of the refcounting
3622 * If unrealize is true we force a unrealize while reparenting the parent.
3623 * This can help eliminate flicker in some cases.
3625 * What happens is that when the menu is unrealized and then re-realized,
3626 * the allocations are as follows:
3628 * parent - 1x1 at (0,0)
3629 * child1 - 100x20 at (0,0)
3630 * child2 - 100x20 at (0,20)
3631 * child3 - 100x20 at (0,40)
3633 * That is, the parent is small but the children are full sized. Then,
3634 * when the queued_resize gets processed, the parent gets resized to
3637 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3638 * contains the following logic:
3640 * - if a move or resize operation on a window would change the clip
3641 * region on the children, then before the window is resized
3642 * the background for children is temporarily set to None, the
3643 * move/resize done, and the background for the children restored.
3645 * So, at the point where the parent is resized to final size, the
3646 * background for the children is temporarily None, and thus they
3647 * are not cleared to the background color and the previous background
3648 * (the image of the menu) is left in place.
3651 gtk_menu_reparent (GtkMenu *menu,
3652 GtkWidget *new_parent,
3655 GtkObject *object = GTK_OBJECT (menu);
3656 GtkWidget *widget = GTK_WIDGET (menu);
3657 gboolean was_floating = GTK_OBJECT_FLOATING (object);
3659 g_object_ref (object);
3660 gtk_object_sink (object);
3664 g_object_ref (object);
3665 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3666 gtk_container_add (GTK_CONTAINER (new_parent), widget);
3667 g_object_unref (object);
3670 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3673 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3675 g_object_unref (object);
3679 gtk_menu_show_all (GtkWidget *widget)
3681 /* Show children, but not self. */
3682 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3687 gtk_menu_hide_all (GtkWidget *widget)
3689 /* Hide children, but not self. */
3690 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3694 * gtk_menu_set_screen:
3695 * @menu: a #GtkMenu.
3696 * @screen: a #GdkScreen, or %NULL if the screen should be
3697 * determined by the widget the menu is attached to.
3699 * Sets the #GdkScreen on which the menu will be displayed.
3704 gtk_menu_set_screen (GtkMenu *menu,
3707 g_return_if_fail (GTK_IS_MENU (menu));
3708 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3710 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3714 menu_change_screen (menu, screen);
3718 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3720 attach_widget_screen_changed (attach_widget, NULL, menu);
3726 * @menu: a #GtkMenu.
3727 * @child: a #GtkMenuItem.
3728 * @left_attach: The column number to attach the left side of the item to.
3729 * @right_attach: The column number to attach the right side of the item to.
3730 * @top_attach: The row number to attach the top of the item to.
3731 * @bottom_attach: The row number to attach the bottom of the item to.
3733 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3734 * an item will occupy is specified by @left_attach, @right_attach,
3735 * @top_attach and @bottom_attach. These each represent the leftmost,
3736 * rightmost, uppermost and lower column and row numbers of the table.
3737 * (Columns and rows are indexed from zero).
3739 * Note that this function is not related to gtk_menu_detach().
3744 gtk_menu_attach (GtkMenu *menu,
3749 guint bottom_attach)
3751 g_return_if_fail (GTK_IS_MENU (menu));
3752 g_return_if_fail (GTK_IS_MENU_ITEM (child));
3753 g_return_if_fail (child->parent == NULL ||
3754 child->parent == GTK_WIDGET (menu));
3755 g_return_if_fail (left_attach < right_attach);
3756 g_return_if_fail (top_attach < bottom_attach);
3760 GTK_MENU_SHELL (menu)->children =
3761 g_list_append (GTK_MENU_SHELL (menu)->children, child);
3763 gtk_widget_set_parent (child, GTK_WIDGET (menu));
3766 gtk_container_child_set (GTK_CONTAINER (menu), child,
3767 "left_attach", left_attach,
3768 "right_attach", right_attach,
3769 "top_attach", top_attach,
3770 "bottom_attach", bottom_attach,
3775 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
3779 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
3780 "gtk-menu-popup-delay", &popup_delay,
3787 find_child_containing (GtkMenuShell *menu_shell,
3795 /* find a child which includes the area given by
3796 * left, right, top, bottom.
3799 for (list = menu_shell->children; list; list = list->next)
3803 if (!_gtk_menu_item_is_selectable (list->data))
3806 get_child_attach (list->data, &l, &r, &t, &b);
3808 if (l <= left && right <= r
3809 && t <= top && bottom <= b)
3810 return GTK_WIDGET (list->data);
3817 gtk_menu_move_current (GtkMenuShell *menu_shell,
3818 GtkMenuDirectionType direction)
3820 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu_shell));
3822 /* use special table menu key bindings */
3823 if (menu_shell->active_menu_item && priv->columns > 1)
3827 gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
3828 GtkWidget *match = NULL;
3830 get_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
3832 if (direction == GTK_MENU_DIR_NEXT)
3834 for (i = b; i < priv->rows; i++)
3836 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
3844 for (i = 0; i < t; i++)
3846 match = find_child_containing (menu_shell,
3847 l, l + 1, i, i + 1);
3853 else if (direction == GTK_MENU_DIR_PREV)
3855 for (i = t; i > 0; i--)
3857 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
3865 for (i = priv->rows; i > b; i--)
3867 match = find_child_containing (menu_shell,
3868 l, l + 1, i - 1, i);
3874 else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
3875 || (rtl && direction == GTK_MENU_DIR_CHILD))
3877 /* we go one left if possible */
3879 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
3883 GtkWidget *parent = menu_shell->parent_menu_shell;
3886 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
3887 match = menu_shell->active_menu_item;
3890 else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
3891 || (rtl && direction == GTK_MENU_DIR_PARENT))
3893 /* we go one right if possible */
3894 if (r < priv->columns)
3895 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
3899 GtkWidget *parent = menu_shell->parent_menu_shell;
3901 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
3903 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
3904 match = menu_shell->active_menu_item;
3910 gtk_menu_shell_select_item (menu_shell, match);
3915 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
3919 get_visible_size (GtkMenu *menu)
3921 GtkWidget *widget = GTK_WIDGET (menu);
3922 GtkContainer *container = GTK_CONTAINER (menu);
3924 gint menu_height = (widget->allocation.height
3925 - 2 * (container->border_width
3926 + widget->style->ythickness));
3928 if (menu->upper_arrow_visible && !menu->tearoff_active)
3929 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3930 if (menu->lower_arrow_visible && !menu->tearoff_active)
3931 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3936 /* Find the sensitive on-screen child containing @y, or if none,
3937 * the nearest selectable onscreen child. (%NULL if none)
3940 child_at (GtkMenu *menu,
3943 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3944 GtkWidget *child = NULL;
3945 gint child_offset = 0;
3948 gint lower, upper; /* Onscreen bounds */
3950 menu_height = get_visible_size (menu);
3951 lower = menu->scroll_offset;
3952 upper = menu->scroll_offset + menu_height;
3954 for (children = menu_shell->children; children; children = children->next)
3956 if (GTK_WIDGET_VISIBLE (children->data))
3958 GtkRequisition child_requisition;
3960 gtk_widget_size_request (children->data, &child_requisition);
3962 if (_gtk_menu_item_is_selectable (children->data) &&
3963 child_offset >= lower &&
3964 child_offset + child_requisition.height <= upper)
3966 child = children->data;
3968 if (child_offset + child_requisition.height > y &&
3969 !GTK_IS_TEAROFF_MENU_ITEM (child))
3973 child_offset += child_requisition.height;
3981 get_menu_height (GtkMenu *menu)
3984 GtkWidget *widget = GTK_WIDGET (menu);
3986 height = widget->requisition.height;
3987 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
3989 if (menu->upper_arrow_visible && !menu->tearoff_active)
3990 height -= MENU_SCROLL_ARROW_HEIGHT;
3992 if (menu->lower_arrow_visible && !menu->tearoff_active)
3993 height -= MENU_SCROLL_ARROW_HEIGHT;
3999 gtk_menu_real_move_scroll (GtkMenu *menu,
4002 gint page_size = get_visible_size (menu);
4003 gint end_position = get_menu_height (menu);
4004 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4008 case GTK_SCROLL_PAGE_UP:
4009 case GTK_SCROLL_PAGE_DOWN:
4013 gint child_offset = 0;
4014 gboolean old_upper_arrow_visible;
4017 if (type == GTK_SCROLL_PAGE_UP)
4022 if (menu_shell->active_menu_item)
4026 compute_child_offset (menu, menu_shell->active_menu_item,
4027 &child_offset, &child_height, NULL);
4028 child_offset += child_height / 2;
4031 menu_shell->ignore_enter = TRUE;
4032 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4033 old_offset = menu->scroll_offset;
4035 new_offset = menu->scroll_offset + step;
4036 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4038 gtk_menu_scroll_to (menu, new_offset);
4040 if (menu_shell->active_menu_item)
4042 GtkWidget *new_child;
4043 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4045 if (menu->scroll_offset != old_offset)
4046 step = menu->scroll_offset - old_offset;
4048 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
4050 new_child = child_at (menu, child_offset + step);
4052 gtk_menu_shell_select_item (menu_shell, new_child);
4056 case GTK_SCROLL_START:
4057 /* Ignore the enter event we might get if the pointer is on the menu
4059 menu_shell->ignore_enter = TRUE;
4060 gtk_menu_scroll_to (menu, 0);
4061 gtk_menu_shell_select_first (menu_shell, TRUE);
4063 case GTK_SCROLL_END:
4064 /* Ignore the enter event we might get if the pointer is on the menu
4066 menu_shell->ignore_enter = TRUE;
4067 gtk_menu_scroll_to (menu, end_position - page_size);
4068 _gtk_menu_shell_select_last (menu_shell, TRUE);
4077 * gtk_menu_set_monitor:
4079 * @monitor_num: the number of the monitor on which the menu should
4082 * Informs GTK+ on which monitor a menu should be popped up.
4083 * See gdk_screen_get_monitor_geometry().
4085 * This function should be called from a #GtkMenuPositionFunc if the
4086 * menu should not appear on the same monitor as the pointer. This
4087 * information can't be reliably inferred from the coordinates returned
4088 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4089 * may extend beyond the monitor boundaries or even the screen boundaries.
4093 void gtk_menu_set_monitor (GtkMenu *menu,
4096 GtkMenuPrivate *priv;
4097 g_return_if_fail (GTK_IS_MENU (menu));
4099 priv = gtk_menu_get_private (menu);
4101 priv->monitor_num = monitor_num;