1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #define GTK_MENU_INTERNALS
29 #include <string.h> /* memset */
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
36 #include "gtkmarshalers.h"
38 #include "gtkmenuitem.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
49 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
51 #define DEFAULT_POPUP_DELAY 225
52 #define DEFAULT_POPDOWN_DELAY 1000
54 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
55 * extends below the submenu
58 #define MENU_SCROLL_STEP1 8
59 #define MENU_SCROLL_STEP2 15
60 #define MENU_SCROLL_FAST_ZONE 8
61 #define MENU_SCROLL_TIMEOUT1 50
62 #define MENU_SCROLL_TIMEOUT2 20
64 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
65 #define ATTACHED_MENUS "gtk-attached-menus"
67 typedef struct _GtkMenuAttachData GtkMenuAttachData;
68 typedef struct _GtkMenuPrivate GtkMenuPrivate;
70 struct _GtkMenuAttachData
72 GtkWidget *attach_widget;
73 GtkMenuDetachFunc detacher;
76 struct _GtkMenuPrivate
78 gboolean seen_item_enter;
80 gboolean have_position;
84 /* info used for the table */
90 /* Cached layout information */
98 GtkStateType lower_arrow_state;
99 GtkStateType upper_arrow_state;
101 gboolean ignore_button_release;
102 gboolean initially_pushed_in;
111 gint effective_left_attach;
112 gint effective_right_attach;
113 gint effective_top_attach;
114 gint effective_bottom_attach;
130 CHILD_PROP_LEFT_ATTACH,
131 CHILD_PROP_RIGHT_ATTACH,
132 CHILD_PROP_TOP_ATTACH,
133 CHILD_PROP_BOTTOM_ATTACH
136 static void gtk_menu_set_property (GObject *object,
140 static void gtk_menu_get_property (GObject *object,
144 static void gtk_menu_set_child_property(GtkContainer *container,
149 static void gtk_menu_get_child_property(GtkContainer *container,
154 static void gtk_menu_destroy (GtkObject *object);
155 static void gtk_menu_realize (GtkWidget *widget);
156 static void gtk_menu_unrealize (GtkWidget *widget);
157 static void gtk_menu_size_request (GtkWidget *widget,
158 GtkRequisition *requisition);
159 static void gtk_menu_size_allocate (GtkWidget *widget,
160 GtkAllocation *allocation);
161 static void gtk_menu_paint (GtkWidget *widget,
162 GdkEventExpose *expose);
163 static void gtk_menu_show (GtkWidget *widget);
164 static gboolean gtk_menu_expose (GtkWidget *widget,
165 GdkEventExpose *event);
166 static gboolean gtk_menu_key_press (GtkWidget *widget,
168 static gboolean gtk_menu_scroll (GtkWidget *widget,
169 GdkEventScroll *event);
170 static gboolean gtk_menu_button_press (GtkWidget *widget,
171 GdkEventButton *event);
172 static gboolean gtk_menu_button_release (GtkWidget *widget,
173 GdkEventButton *event);
174 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
175 GdkEventMotion *event);
176 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
177 GdkEventCrossing *event);
178 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
179 GdkEventCrossing *event);
180 static void gtk_menu_scroll_to (GtkMenu *menu,
182 static void gtk_menu_grab_notify (GtkWidget *widget,
183 gboolean was_grabbed);
185 static void gtk_menu_stop_scrolling (GtkMenu *menu);
186 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
187 static gboolean gtk_menu_scroll_timeout (gpointer data);
188 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
189 static void gtk_menu_start_scrolling (GtkMenu *menu);
191 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
192 GtkWidget *menu_item);
193 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
194 GtkWidget *menu_item);
195 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
198 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
200 static void gtk_menu_handle_scrolling (GtkMenu *menu,
205 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
207 static void gtk_menu_style_set (GtkWidget *widget,
208 GtkStyle *previous_style);
209 static gboolean gtk_menu_focus (GtkWidget *widget,
210 GtkDirectionType direction);
211 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
212 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
213 GtkMenuDirectionType direction);
214 static void gtk_menu_real_move_scroll (GtkMenu *menu,
217 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
218 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
219 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
222 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
223 GtkMenuItem *menu_item,
224 GdkEventCrossing *event);
226 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
227 static void gtk_menu_show_all (GtkWidget *widget);
228 static void gtk_menu_hide_all (GtkWidget *widget);
229 static void gtk_menu_position (GtkMenu *menu);
230 static void gtk_menu_reparent (GtkMenu *menu,
231 GtkWidget *new_parent,
233 static void gtk_menu_remove (GtkContainer *menu,
236 static void gtk_menu_update_title (GtkMenu *menu);
238 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
239 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
241 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
243 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
244 gboolean group_changed);
246 static const gchar attach_data_key[] = "gtk-menu-attach-data";
248 static guint menu_signals[LAST_SIGNAL] = { 0 };
250 static GtkMenuPrivate *
251 gtk_menu_get_private (GtkMenu *menu)
253 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
256 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
259 menu_queue_resize (GtkMenu *menu)
261 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
263 priv->have_layout = FALSE;
264 gtk_widget_queue_resize (GTK_WIDGET (menu));
268 attach_info_free (AttachInfo *info)
270 g_slice_free (AttachInfo, info);
274 get_attach_info (GtkWidget *child)
276 GObject *object = G_OBJECT (child);
277 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
281 ai = g_slice_new0 (AttachInfo);
282 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
283 (GDestroyNotify) attach_info_free);
290 is_grid_attached (AttachInfo *ai)
292 return (ai->left_attach >= 0 &&
293 ai->right_attach >= 0 &&
294 ai->top_attach >= 0 &&
295 ai->bottom_attach >= 0);
299 menu_ensure_layout (GtkMenu *menu)
301 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
303 if (!priv->have_layout)
305 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
309 gint max_right_attach;
310 gint max_bottom_attach;
312 /* Find extents of gridded portion
314 max_right_attach = 1;
315 max_bottom_attach = 0;
317 for (l = menu_shell->children; l; l = l->next)
319 GtkWidget *child = l->data;
320 AttachInfo *ai = get_attach_info (child);
322 if (is_grid_attached (ai))
324 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
325 max_right_attach = MAX (max_right_attach, ai->right_attach);
331 row_occupied = g_malloc0 (max_bottom_attach);
333 for (l = menu_shell->children; l; l = l->next)
335 GtkWidget *child = l->data;
336 AttachInfo *ai = get_attach_info (child);
338 if (is_grid_attached (ai))
342 for (i = ai->top_attach; i < ai->bottom_attach; i++)
343 row_occupied[i] = TRUE;
347 /* Lay non-grid-items out in those rows
350 for (l = menu_shell->children; l; l = l->next)
352 GtkWidget *child = l->data;
353 AttachInfo *ai = get_attach_info (child);
355 if (!is_grid_attached (ai))
357 while (current_row < max_bottom_attach && row_occupied[current_row])
360 ai->effective_left_attach = 0;
361 ai->effective_right_attach = max_right_attach;
362 ai->effective_top_attach = current_row;
363 ai->effective_bottom_attach = current_row + 1;
369 ai->effective_left_attach = ai->left_attach;
370 ai->effective_right_attach = ai->right_attach;
371 ai->effective_top_attach = ai->top_attach;
372 ai->effective_bottom_attach = ai->bottom_attach;
376 g_free (row_occupied);
378 priv->n_rows = MAX (current_row, max_bottom_attach);
379 priv->n_columns = max_right_attach;
380 priv->have_layout = TRUE;
386 gtk_menu_get_n_columns (GtkMenu *menu)
388 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
390 menu_ensure_layout (menu);
392 return priv->n_columns;
396 gtk_menu_get_n_rows (GtkMenu *menu)
398 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
400 menu_ensure_layout (menu);
406 get_effective_child_attach (GtkWidget *child,
412 GtkMenu *menu = GTK_MENU (child->parent);
415 menu_ensure_layout (menu);
417 ai = get_attach_info (child);
420 *l = ai->effective_left_attach;
422 *r = ai->effective_right_attach;
424 *t = ai->effective_top_attach;
426 *b = ai->effective_bottom_attach;
431 gtk_menu_class_init (GtkMenuClass *class)
433 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
434 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
435 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
436 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
437 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
438 GtkBindingSet *binding_set;
440 gobject_class->set_property = gtk_menu_set_property;
441 gobject_class->get_property = gtk_menu_get_property;
443 object_class->destroy = gtk_menu_destroy;
445 widget_class->realize = gtk_menu_realize;
446 widget_class->unrealize = gtk_menu_unrealize;
447 widget_class->size_request = gtk_menu_size_request;
448 widget_class->size_allocate = gtk_menu_size_allocate;
449 widget_class->show = gtk_menu_show;
450 widget_class->expose_event = gtk_menu_expose;
451 widget_class->scroll_event = gtk_menu_scroll;
452 widget_class->key_press_event = gtk_menu_key_press;
453 widget_class->button_press_event = gtk_menu_button_press;
454 widget_class->button_release_event = gtk_menu_button_release;
455 widget_class->motion_notify_event = gtk_menu_motion_notify;
456 widget_class->show_all = gtk_menu_show_all;
457 widget_class->hide_all = gtk_menu_hide_all;
458 widget_class->enter_notify_event = gtk_menu_enter_notify;
459 widget_class->leave_notify_event = gtk_menu_leave_notify;
460 widget_class->style_set = gtk_menu_style_set;
461 widget_class->focus = gtk_menu_focus;
462 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
463 widget_class->grab_notify = gtk_menu_grab_notify;
465 container_class->remove = gtk_menu_remove;
466 container_class->get_child_property = gtk_menu_get_child_property;
467 container_class->set_child_property = gtk_menu_set_child_property;
469 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
470 menu_shell_class->deactivate = gtk_menu_deactivate;
471 menu_shell_class->select_item = gtk_menu_select_item;
472 menu_shell_class->insert = gtk_menu_real_insert;
473 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
474 menu_shell_class->move_current = gtk_menu_move_current;
476 menu_signals[MOVE_SCROLL] =
477 _gtk_binding_signal_new (I_("move_scroll"),
478 G_OBJECT_CLASS_TYPE (object_class),
479 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
480 G_CALLBACK (gtk_menu_real_move_scroll),
482 _gtk_marshal_VOID__ENUM,
484 GTK_TYPE_SCROLL_TYPE);
486 g_object_class_install_property (gobject_class,
488 g_param_spec_string ("tearoff-title",
490 P_("A title that may be displayed by the window manager when this menu is torn-off"),
492 GTK_PARAM_READWRITE));
495 * GtkMenu:tearoff-state:
497 * A boolean that indicates whether the menu is torn-off.
501 g_object_class_install_property (gobject_class,
503 g_param_spec_boolean ("tearoff-state",
505 P_("A boolean that indicates whether the menu is torn-off"),
507 GTK_PARAM_READWRITE));
509 gtk_widget_class_install_style_property (widget_class,
510 g_param_spec_int ("vertical-padding",
511 P_("Vertical Padding"),
512 P_("Extra space at the top and bottom of the menu"),
516 GTK_PARAM_READABLE));
518 gtk_widget_class_install_style_property (widget_class,
519 g_param_spec_int ("horizontal-padding",
520 P_("Horizontal Padding"),
521 P_("Extra space at the left and right edges of the menu"),
525 GTK_PARAM_READABLE));
527 gtk_widget_class_install_style_property (widget_class,
528 g_param_spec_int ("vertical-offset",
529 P_("Vertical Offset"),
530 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
534 GTK_PARAM_READABLE));
536 gtk_widget_class_install_style_property (widget_class,
537 g_param_spec_int ("horizontal-offset",
538 P_("Horizontal Offset"),
539 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
543 GTK_PARAM_READABLE));
545 gtk_widget_class_install_style_property (widget_class,
546 g_param_spec_boolean ("double-arrows",
548 P_("When scrolling, always show both arrows."),
550 GTK_PARAM_READABLE));
553 gtk_container_class_install_child_property (container_class,
554 CHILD_PROP_LEFT_ATTACH,
555 g_param_spec_int ("left-attach",
557 P_("The column number to attach the left side of the child to"),
559 GTK_PARAM_READWRITE));
561 gtk_container_class_install_child_property (container_class,
562 CHILD_PROP_RIGHT_ATTACH,
563 g_param_spec_int ("right-attach",
565 P_("The column number to attach the right side of the child to"),
567 GTK_PARAM_READWRITE));
569 gtk_container_class_install_child_property (container_class,
570 CHILD_PROP_TOP_ATTACH,
571 g_param_spec_int ("top-attach",
573 P_("The row number to attach the top of the child to"),
575 GTK_PARAM_READWRITE));
577 gtk_container_class_install_child_property (container_class,
578 CHILD_PROP_BOTTOM_ATTACH,
579 g_param_spec_int ("bottom-attach",
581 P_("The row number to attach the bottom of the child to"),
583 GTK_PARAM_READWRITE));
585 binding_set = gtk_binding_set_by_class (class);
586 gtk_binding_entry_add_signal (binding_set,
588 I_("move_current"), 1,
589 GTK_TYPE_MENU_DIRECTION_TYPE,
591 gtk_binding_entry_add_signal (binding_set,
594 GTK_TYPE_MENU_DIRECTION_TYPE,
596 gtk_binding_entry_add_signal (binding_set,
599 GTK_TYPE_MENU_DIRECTION_TYPE,
601 gtk_binding_entry_add_signal (binding_set,
604 GTK_TYPE_MENU_DIRECTION_TYPE,
606 gtk_binding_entry_add_signal (binding_set,
609 GTK_TYPE_MENU_DIRECTION_TYPE,
610 GTK_MENU_DIR_PARENT);
611 gtk_binding_entry_add_signal (binding_set,
614 GTK_TYPE_MENU_DIRECTION_TYPE,
615 GTK_MENU_DIR_PARENT);
616 gtk_binding_entry_add_signal (binding_set,
619 GTK_TYPE_MENU_DIRECTION_TYPE,
621 gtk_binding_entry_add_signal (binding_set,
624 GTK_TYPE_MENU_DIRECTION_TYPE,
626 gtk_binding_entry_add_signal (binding_set,
629 GTK_TYPE_SCROLL_TYPE,
631 gtk_binding_entry_add_signal (binding_set,
634 GTK_TYPE_SCROLL_TYPE,
636 gtk_binding_entry_add_signal (binding_set,
639 GTK_TYPE_SCROLL_TYPE,
641 gtk_binding_entry_add_signal (binding_set,
644 GTK_TYPE_SCROLL_TYPE,
646 gtk_binding_entry_add_signal (binding_set,
649 GTK_TYPE_SCROLL_TYPE,
651 gtk_binding_entry_add_signal (binding_set,
654 GTK_TYPE_SCROLL_TYPE,
656 gtk_binding_entry_add_signal (binding_set,
659 GTK_TYPE_SCROLL_TYPE,
660 GTK_SCROLL_PAGE_DOWN);
661 gtk_binding_entry_add_signal (binding_set,
664 GTK_TYPE_SCROLL_TYPE,
665 GTK_SCROLL_PAGE_DOWN);
667 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
668 P_("Can change accelerators"),
669 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
671 GTK_PARAM_READWRITE));
673 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
674 P_("Delay before submenus appear"),
675 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
679 GTK_PARAM_READWRITE));
681 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
682 P_("Delay before hiding a submenu"),
683 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
686 DEFAULT_POPDOWN_DELAY,
687 GTK_PARAM_READWRITE));
689 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
694 gtk_menu_set_property (GObject *object,
701 menu = GTK_MENU (object);
705 case PROP_TEAROFF_STATE:
706 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
708 case PROP_TEAROFF_TITLE:
709 gtk_menu_set_title (menu, g_value_get_string (value));
712 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
718 gtk_menu_get_property (GObject *object,
725 menu = GTK_MENU (object);
729 case PROP_TEAROFF_STATE:
730 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
732 case PROP_TEAROFF_TITLE:
733 g_value_set_string (value, gtk_menu_get_title (menu));
736 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
742 gtk_menu_set_child_property (GtkContainer *container,
748 GtkMenu *menu = GTK_MENU (container);
749 AttachInfo *ai = get_attach_info (child);
753 case CHILD_PROP_LEFT_ATTACH:
754 ai->left_attach = g_value_get_int (value);
756 case CHILD_PROP_RIGHT_ATTACH:
757 ai->right_attach = g_value_get_int (value);
759 case CHILD_PROP_TOP_ATTACH:
760 ai->top_attach = g_value_get_int (value);
762 case CHILD_PROP_BOTTOM_ATTACH:
763 ai->bottom_attach = g_value_get_int (value);
767 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
771 menu_queue_resize (menu);
775 gtk_menu_get_child_property (GtkContainer *container,
781 AttachInfo *ai = get_attach_info (child);
785 case CHILD_PROP_LEFT_ATTACH:
786 g_value_set_int (value, ai->left_attach);
788 case CHILD_PROP_RIGHT_ATTACH:
789 g_value_set_int (value, ai->right_attach);
791 case CHILD_PROP_TOP_ATTACH:
792 g_value_set_int (value, ai->top_attach);
794 case CHILD_PROP_BOTTOM_ATTACH:
795 g_value_set_int (value, ai->bottom_attach);
799 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
805 gtk_menu_window_event (GtkWidget *window,
809 gboolean handled = FALSE;
811 g_object_ref (window);
817 case GDK_KEY_RELEASE:
818 handled = gtk_widget_event (menu, event);
824 g_object_unref (window);
825 g_object_unref (menu);
831 gtk_menu_window_size_request (GtkWidget *window,
832 GtkRequisition *requisition,
835 GtkMenuPrivate *private = gtk_menu_get_private (menu);
837 if (private->have_position)
839 GdkScreen *screen = gtk_widget_get_screen (window);
840 GdkRectangle monitor;
842 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
844 if (private->y + requisition->height > monitor.y + monitor.height)
845 requisition->height = monitor.y + monitor.height - private->y;
847 if (private->y < monitor.y)
848 requisition->height -= monitor.y - private->y;
853 gtk_menu_init (GtkMenu *menu)
855 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
857 menu->parent_menu_item = NULL;
858 menu->old_active_menu_item = NULL;
859 menu->accel_group = NULL;
860 menu->position_func = NULL;
861 menu->position_func_data = NULL;
862 menu->toggle_size = 0;
864 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
865 "type", GTK_WINDOW_POPUP,
868 "signal::event", gtk_menu_window_event, menu,
869 "signal::size_request", gtk_menu_window_size_request, menu,
870 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
872 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
873 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
875 /* Refloat the menu, so that reference counting for the menu isn't
876 * affected by it being a child of the toplevel
878 g_object_force_floating (G_OBJECT (menu));
879 menu->needs_destruction_ref_count = TRUE;
881 menu->view_window = NULL;
882 menu->bin_window = NULL;
884 menu->scroll_offset = 0;
885 menu->scroll_step = 0;
886 menu->timeout_id = 0;
887 menu->scroll_fast = FALSE;
889 menu->tearoff_window = NULL;
890 menu->tearoff_hbox = NULL;
891 menu->torn_off = FALSE;
892 menu->tearoff_active = FALSE;
893 menu->tearoff_adjustment = NULL;
894 menu->tearoff_scrollbar = NULL;
896 menu->upper_arrow_visible = FALSE;
897 menu->lower_arrow_visible = FALSE;
898 menu->upper_arrow_prelight = FALSE;
899 menu->lower_arrow_prelight = FALSE;
901 priv->upper_arrow_state = GTK_STATE_NORMAL;
902 priv->lower_arrow_state = GTK_STATE_NORMAL;
904 priv->have_layout = FALSE;
908 gtk_menu_destroy (GtkObject *object)
910 GtkMenu *menu = GTK_MENU (object);
911 GtkMenuAttachData *data;
912 GtkMenuPrivate *priv;
914 gtk_menu_remove_scroll_timeout (menu);
916 data = g_object_get_data (G_OBJECT (object), attach_data_key);
918 gtk_menu_detach (menu);
920 gtk_menu_stop_navigating_submenu (menu);
922 if (menu->old_active_menu_item)
924 g_object_unref (menu->old_active_menu_item);
925 menu->old_active_menu_item = NULL;
928 /* Add back the reference count for being a child */
929 if (menu->needs_destruction_ref_count)
931 menu->needs_destruction_ref_count = FALSE;
932 g_object_ref (object);
935 if (menu->accel_group)
937 g_object_unref (menu->accel_group);
938 menu->accel_group = NULL;
942 gtk_widget_destroy (menu->toplevel);
944 if (menu->tearoff_window)
945 gtk_widget_destroy (menu->tearoff_window);
947 priv = gtk_menu_get_private (menu);
951 g_free (priv->heights);
952 priv->heights = NULL;
957 g_free (priv->title);
961 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
965 menu_change_screen (GtkMenu *menu,
966 GdkScreen *new_screen)
968 GtkMenuPrivate *private = gtk_menu_get_private (menu);
970 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
972 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
978 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
979 gtk_menu_position (menu);
982 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
983 private->monitor_num = -1;
987 attach_widget_screen_changed (GtkWidget *attach_widget,
988 GdkScreen *previous_screen,
991 if (gtk_widget_has_screen (attach_widget) &&
992 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
994 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
999 gtk_menu_attach_to_widget (GtkMenu *menu,
1000 GtkWidget *attach_widget,
1001 GtkMenuDetachFunc detacher)
1003 GtkMenuAttachData *data;
1006 g_return_if_fail (GTK_IS_MENU (menu));
1007 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1009 /* keep this function in sync with gtk_widget_set_parent()
1012 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1015 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1016 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1020 g_object_ref_sink (menu);
1022 data = g_slice_new (GtkMenuAttachData);
1023 data->attach_widget = attach_widget;
1025 g_signal_connect (attach_widget, "screen_changed",
1026 G_CALLBACK (attach_widget_screen_changed), menu);
1027 attach_widget_screen_changed (attach_widget, NULL, menu);
1029 data->detacher = detacher;
1030 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1031 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1032 if (!g_list_find (list, menu))
1034 list = g_list_prepend (list, menu);
1036 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1037 (GDestroyNotify) g_list_free);
1039 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1040 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1042 /* we don't need to set the style here, since
1043 * we are a toplevel widget.
1046 /* Fallback title for menu comes from attach widget */
1047 gtk_menu_update_title (menu);
1051 gtk_menu_get_attach_widget (GtkMenu *menu)
1053 GtkMenuAttachData *data;
1055 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1057 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1059 return data->attach_widget;
1064 gtk_menu_detach (GtkMenu *menu)
1066 GtkMenuAttachData *data;
1069 g_return_if_fail (GTK_IS_MENU (menu));
1071 /* keep this function in sync with gtk_widget_unparent()
1073 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1076 g_warning ("gtk_menu_detach(): menu is not attached");
1079 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1081 g_signal_handlers_disconnect_by_func (data->attach_widget,
1082 (gpointer) attach_widget_screen_changed,
1086 data->detacher (data->attach_widget, menu);
1087 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1088 list = g_list_remove (list, menu);
1090 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1091 (GDestroyNotify) g_list_free);
1093 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1095 if (GTK_WIDGET_REALIZED (menu))
1096 gtk_widget_unrealize (GTK_WIDGET (menu));
1098 g_slice_free (GtkMenuAttachData, data);
1100 /* Fallback title for menu comes from attach widget */
1101 gtk_menu_update_title (menu);
1103 g_object_unref (menu);
1107 gtk_menu_remove (GtkContainer *container,
1110 GtkMenu *menu = GTK_MENU (container);
1112 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1114 /* Clear out old_active_menu_item if it matches the item we are removing
1116 if (menu->old_active_menu_item == widget)
1118 g_object_unref (menu->old_active_menu_item);
1119 menu->old_active_menu_item = NULL;
1122 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1123 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1125 menu_queue_resize (menu);
1131 return g_object_new (GTK_TYPE_MENU, NULL);
1135 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1139 GtkMenu *menu = GTK_MENU (menu_shell);
1140 AttachInfo *ai = get_attach_info (child);
1142 ai->left_attach = -1;
1143 ai->right_attach = -1;
1144 ai->top_attach = -1;
1145 ai->bottom_attach = -1;
1147 if (GTK_WIDGET_REALIZED (menu_shell))
1148 gtk_widget_set_parent_window (child, menu->bin_window);
1150 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1152 menu_queue_resize (menu);
1156 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1161 widget = GTK_WIDGET (menu);
1167 GdkGCValues gc_values;
1169 menu->tearoff_active = FALSE;
1170 menu->saved_scroll_offset = menu->scroll_offset;
1172 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1173 gc = gdk_gc_new_with_values (widget->window,
1174 &gc_values, GDK_GC_SUBWINDOW);
1176 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1178 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1183 gdk_draw_drawable (pixmap, gc,
1184 menu->tearoff_window->window,
1185 0, 0, 0, 0, -1, -1);
1186 g_object_unref (gc);
1188 gtk_widget_set_size_request (menu->tearoff_window,
1192 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1193 g_object_unref (pixmap);
1198 popup_grab_on_window (GdkWindow *window,
1199 guint32 activate_time,
1200 gboolean grab_keyboard)
1202 if ((gdk_pointer_grab (window, TRUE,
1203 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1204 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1205 GDK_POINTER_MOTION_MASK,
1206 NULL, NULL, activate_time) == 0))
1208 if (!grab_keyboard ||
1209 gdk_keyboard_grab (window, TRUE,
1210 activate_time) == 0)
1214 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1225 * @menu: a #GtkMenu.
1226 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1227 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1228 * @func: a user supplied function used to position the menu, or %NULL
1229 * @data: user supplied data to be passed to @func.
1230 * @button: the mouse button which was pressed to initiate the event.
1231 * @activate_time: the time at which the activation event occurred.
1233 * Displays a menu and makes it available for selection. Applications can use
1234 * this function to display context-sensitive menus, and will typically supply
1235 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1236 * parameters. The default menu positioning function will position the menu
1237 * at the current mouse cursor position.
1239 * The @button parameter should be the mouse button pressed to initiate
1240 * the menu popup. If the menu popup was initiated by something other than
1241 * a mouse button press, such as a mouse button release or a keypress,
1242 * @button should be 0.
1244 * The @activate_time parameter is used to conflict-resolve initiation of
1245 * concurrent requests for mouse/keyboard grab requests. To function
1246 * properly, this needs to be the time stamp of the user event (such as
1247 * a mouse click or key press) that caused the initiation of the popup.
1248 * Only if no such event is available, gtk_get_current_event_time() can
1252 gtk_menu_popup (GtkMenu *menu,
1253 GtkWidget *parent_menu_shell,
1254 GtkWidget *parent_menu_item,
1255 GtkMenuPositionFunc func,
1258 guint32 activate_time)
1261 GtkWidget *xgrab_shell;
1263 GdkEvent *current_event;
1264 GtkMenuShell *menu_shell;
1265 gboolean grab_keyboard;
1266 GtkMenuPrivate *priv;
1267 GtkWidget *parent_toplevel;
1269 g_return_if_fail (GTK_IS_MENU (menu));
1271 widget = GTK_WIDGET (menu);
1272 menu_shell = GTK_MENU_SHELL (menu);
1273 priv = gtk_menu_get_private (menu);
1275 menu_shell->parent_menu_shell = parent_menu_shell;
1277 priv->seen_item_enter = FALSE;
1279 /* Find the last viewable ancestor, and make an X grab on it
1281 parent = GTK_WIDGET (menu);
1285 gboolean viewable = TRUE;
1286 GtkWidget *tmp = parent;
1290 if (!GTK_WIDGET_MAPPED (tmp))
1299 xgrab_shell = parent;
1301 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1304 /* We want to receive events generated when we map the menu; unfortunately,
1305 * since there is probably already an implicit grab in place from the
1306 * button that the user used to pop up the menu, we won't receive then --
1307 * in particular, the EnterNotify when the menu pops up under the pointer.
1309 * If we are grabbing on a parent menu shell, no problem; just grab on
1310 * that menu shell first before popping up the window with owner_events = TRUE.
1312 * When grabbing on the menu itself, things get more convuluted - we
1313 * we do an explicit grab on a specially created window with
1314 * owner_events = TRUE, which we override further down with a grab
1315 * on the menu. (We can't grab on the menu until it is mapped; we
1316 * probably could just leave the grab on the other window, with a
1317 * little reorganization of the code in gtkmenu*).
1319 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1320 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1322 if (xgrab_shell && xgrab_shell != widget)
1324 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1325 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1329 GdkWindow *transfer_window;
1331 xgrab_shell = widget;
1332 transfer_window = menu_grab_transfer_window_get (menu);
1333 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1334 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1337 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1339 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1340 * with a stuck up window, we just abort here. Presumably the user will
1343 menu_shell->parent_menu_shell = NULL;
1344 menu_grab_transfer_window_destroy (menu);
1348 menu_shell->active = TRUE;
1349 menu_shell->button = button;
1351 /* If we are popping up the menu from something other than, a button
1352 * press then, as a heuristic, we ignore enter events for the menu
1353 * until we get a MOTION_NOTIFY.
1356 current_event = gtk_get_current_event ();
1359 if ((current_event->type != GDK_BUTTON_PRESS) &&
1360 (current_event->type != GDK_ENTER_NOTIFY))
1361 menu_shell->ignore_enter = TRUE;
1363 gdk_event_free (current_event);
1366 menu_shell->ignore_enter = TRUE;
1370 gtk_menu_tearoff_bg_copy (menu);
1372 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1375 parent_toplevel = NULL;
1376 if (parent_menu_shell)
1377 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1378 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1380 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1382 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1385 /* Set transient for to get the right window group and parent relationship */
1386 if (GTK_IS_WINDOW (parent_toplevel))
1387 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1388 GTK_WINDOW (parent_toplevel));
1390 menu->parent_menu_item = parent_menu_item;
1391 menu->position_func = func;
1392 menu->position_func_data = data;
1393 menu_shell->activate_time = activate_time;
1395 /* We need to show the menu here rather in the init function because
1396 * code expects to be able to tell if the menu is onscreen by
1397 * looking at the GTK_WIDGET_VISIBLE (menu)
1399 gtk_widget_show (GTK_WIDGET (menu));
1401 /* Position the menu, possibly changing the size request
1403 gtk_menu_position (menu);
1405 /* Compute the size of the toplevel and realize it so we
1406 * can scroll correctly.
1409 GtkRequisition tmp_request;
1410 GtkAllocation tmp_allocation = { 0, };
1412 gtk_widget_size_request (menu->toplevel, &tmp_request);
1414 tmp_allocation.width = tmp_request.width;
1415 tmp_allocation.height = tmp_request.height;
1417 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1419 gtk_widget_realize (GTK_WIDGET (menu));
1422 gtk_menu_scroll_to (menu, menu->scroll_offset);
1424 /* if no item is selected, select the first one */
1425 if (!menu_shell->active_menu_item)
1427 gboolean touchscreen_mode;
1429 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1430 "gtk-touchscreen-mode", &touchscreen_mode,
1433 if (touchscreen_mode)
1434 gtk_menu_shell_select_first (menu_shell, TRUE);
1437 /* Once everything is set up correctly, map the toplevel window on
1440 gtk_widget_show (menu->toplevel);
1442 if (xgrab_shell == widget)
1443 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1444 gtk_grab_add (GTK_WIDGET (menu));
1448 gtk_menu_popdown (GtkMenu *menu)
1450 GtkMenuPrivate *private;
1451 GtkMenuShell *menu_shell;
1453 g_return_if_fail (GTK_IS_MENU (menu));
1455 menu_shell = GTK_MENU_SHELL (menu);
1456 private = gtk_menu_get_private (menu);
1458 menu_shell->parent_menu_shell = NULL;
1459 menu_shell->active = FALSE;
1460 menu_shell->ignore_enter = FALSE;
1462 private->have_position = FALSE;
1464 gtk_menu_stop_scrolling (menu);
1466 gtk_menu_stop_navigating_submenu (menu);
1468 if (menu_shell->active_menu_item)
1470 if (menu->old_active_menu_item)
1471 g_object_unref (menu->old_active_menu_item);
1472 menu->old_active_menu_item = menu_shell->active_menu_item;
1473 g_object_ref (menu->old_active_menu_item);
1476 gtk_menu_shell_deselect (menu_shell);
1478 /* The X Grab, if present, will automatically be removed when we hide
1480 gtk_widget_hide (menu->toplevel);
1481 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1485 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1487 if (GTK_BIN (menu->toplevel)->child)
1489 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1493 /* We popped up the menu from the tearoff, so we need to
1494 * release the grab - we aren't actually hiding the menu.
1496 if (menu_shell->have_xgrab)
1498 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1500 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1501 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1505 /* gtk_menu_popdown is called each time a menu item is selected from
1506 * a torn off menu. Only scroll back to the saved position if the
1507 * non-tearoff menu was popped down.
1509 if (!menu->tearoff_active)
1510 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1511 menu->tearoff_active = TRUE;
1514 gtk_widget_hide (GTK_WIDGET (menu));
1516 menu_shell->have_xgrab = FALSE;
1517 gtk_grab_remove (GTK_WIDGET (menu));
1519 menu_grab_transfer_window_destroy (menu);
1523 gtk_menu_get_active (GtkMenu *menu)
1528 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1530 if (!menu->old_active_menu_item)
1533 children = GTK_MENU_SHELL (menu)->children;
1537 child = children->data;
1538 children = children->next;
1540 if (GTK_BIN (child)->child)
1545 menu->old_active_menu_item = child;
1546 if (menu->old_active_menu_item)
1547 g_object_ref (menu->old_active_menu_item);
1550 return menu->old_active_menu_item;
1554 gtk_menu_set_active (GtkMenu *menu,
1560 g_return_if_fail (GTK_IS_MENU (menu));
1562 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1565 child = tmp_list->data;
1566 if (GTK_BIN (child)->child)
1568 if (menu->old_active_menu_item)
1569 g_object_unref (menu->old_active_menu_item);
1570 menu->old_active_menu_item = child;
1571 g_object_ref (menu->old_active_menu_item);
1577 gtk_menu_set_accel_group (GtkMenu *menu,
1578 GtkAccelGroup *accel_group)
1580 g_return_if_fail (GTK_IS_MENU (menu));
1582 if (menu->accel_group != accel_group)
1584 if (menu->accel_group)
1585 g_object_unref (menu->accel_group);
1586 menu->accel_group = accel_group;
1587 if (menu->accel_group)
1588 g_object_ref (menu->accel_group);
1589 _gtk_menu_refresh_accel_paths (menu, TRUE);
1594 gtk_menu_get_accel_group (GtkMenu *menu)
1596 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1598 return menu->accel_group;
1602 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1605 /* Menu items chain here to figure whether they can activate their
1606 * accelerators. Unlike ordinary widgets, menus allow accel
1607 * activation even if invisible since that's the usual case for
1608 * submenus/popup-menus. however, the state of the attach widget
1609 * affects the "activeness" of the menu.
1611 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1614 return gtk_widget_can_activate_accel (awidget, signal_id);
1616 return GTK_WIDGET_IS_SENSITIVE (widget);
1620 * gtk_menu_set_accel_path
1621 * @menu: a valid #GtkMenu
1622 * @accel_path: a valid accelerator path
1624 * Sets an accelerator path for this menu from which accelerator paths
1625 * for its immediate children, its menu items, can be constructed.
1626 * The main purpose of this function is to spare the programmer the
1627 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1628 * each menu item that should support runtime user changable accelerators.
1629 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1630 * each menu item of this menu, that contains a label describing its purpose,
1631 * automatically gets an accel path assigned. For example, a menu containing
1632 * menu items "New" and "Exit", will, after
1633 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1634 * has been called, assign its items the accel paths:
1635 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1636 * Assigning accel paths to menu items then enables the user to change
1637 * their accelerators at runtime. More details about accelerator paths
1638 * and their default setups can be found at gtk_accel_map_add_entry().
1640 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1641 * pass a static string, you can save some memory by interning it first with
1642 * g_intern_static_string().
1645 gtk_menu_set_accel_path (GtkMenu *menu,
1646 const gchar *accel_path)
1648 gchar *old_accel_path;
1650 g_return_if_fail (GTK_IS_MENU (menu));
1652 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1654 menu->accel_path = g_intern_string (accel_path);
1655 if (menu->accel_path)
1656 _gtk_menu_refresh_accel_paths (menu, FALSE);
1661 gboolean group_changed;
1665 refresh_accel_paths_foreach (GtkWidget *widget,
1668 AccelPropagation *prop = data;
1670 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1671 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1672 prop->menu->accel_path,
1673 prop->menu->accel_group,
1674 prop->group_changed);
1678 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1679 gboolean group_changed)
1681 g_return_if_fail (GTK_IS_MENU (menu));
1683 if (menu->accel_path && menu->accel_group)
1685 AccelPropagation prop;
1688 prop.group_changed = group_changed;
1689 gtk_container_foreach (GTK_CONTAINER (menu),
1690 refresh_accel_paths_foreach,
1696 gtk_menu_reposition (GtkMenu *menu)
1698 g_return_if_fail (GTK_IS_MENU (menu));
1700 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1701 gtk_menu_position (menu);
1705 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1708 g_return_if_fail (GTK_IS_MENU (menu));
1710 if (adjustment->value != menu->scroll_offset)
1711 gtk_menu_scroll_to (menu, adjustment->value);
1715 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1718 GdkGeometry geometry_hints;
1720 if (!menu->tearoff_window)
1723 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1725 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1726 width += menu->tearoff_scrollbar->requisition.width;
1729 geometry_hints.min_width = width;
1730 geometry_hints.max_width = width;
1732 geometry_hints.min_height = 0;
1733 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1735 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1738 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1742 gtk_menu_update_title (GtkMenu *menu)
1744 if (menu->tearoff_window)
1747 GtkWidget *attach_widget;
1749 title = gtk_menu_get_title (menu);
1752 attach_widget = gtk_menu_get_attach_widget (menu);
1753 if (GTK_IS_MENU_ITEM (attach_widget))
1755 GtkWidget *child = GTK_BIN (attach_widget)->child;
1756 if (GTK_IS_LABEL (child))
1757 title = gtk_label_get_text (GTK_LABEL (child));
1762 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1767 gtk_menu_get_toplevel (GtkWidget *menu)
1769 GtkWidget *attach, *toplevel;
1771 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1773 if (GTK_IS_MENU_ITEM (attach))
1774 attach = attach->parent;
1776 if (GTK_IS_MENU (attach))
1777 return gtk_menu_get_toplevel (attach);
1778 else if (GTK_IS_WIDGET (attach))
1780 toplevel = gtk_widget_get_toplevel (attach);
1781 if (GTK_WIDGET_TOPLEVEL (toplevel))
1789 tearoff_window_destroyed (GtkWidget *widget,
1792 gtk_menu_set_tearoff_state (menu, FALSE);
1796 gtk_menu_set_tearoff_state (GtkMenu *menu,
1801 g_return_if_fail (GTK_IS_MENU (menu));
1803 if (menu->torn_off != torn_off)
1805 menu->torn_off = torn_off;
1806 menu->tearoff_active = torn_off;
1810 if (GTK_WIDGET_VISIBLE (menu))
1811 gtk_menu_popdown (menu);
1813 if (!menu->tearoff_window)
1815 GtkWidget *toplevel;
1817 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
1818 "type", GTK_WINDOW_TOPLEVEL,
1819 "screen", gtk_widget_get_screen (menu->toplevel),
1820 "app-paintable", TRUE,
1823 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1824 GDK_WINDOW_TYPE_HINT_MENU);
1825 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1826 g_signal_connect (menu->tearoff_window, "destroy",
1827 G_CALLBACK (tearoff_window_destroyed), menu);
1828 g_signal_connect (menu->tearoff_window, "event",
1829 G_CALLBACK (gtk_menu_window_event), menu);
1831 gtk_menu_update_title (menu);
1833 gtk_widget_realize (menu->tearoff_window);
1835 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1836 if (toplevel != NULL)
1837 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1838 GTK_WINDOW (toplevel));
1840 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1841 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1843 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1844 menu->tearoff_adjustment =
1845 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1847 GTK_WIDGET (menu)->requisition.height,
1851 g_object_connect (menu->tearoff_adjustment,
1852 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1854 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1856 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1857 menu->tearoff_scrollbar,
1860 if (menu->tearoff_adjustment->upper > height)
1861 gtk_widget_show (menu->tearoff_scrollbar);
1863 gtk_widget_show (menu->tearoff_hbox);
1866 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1868 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1870 /* Update menu->requisition
1872 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1874 gtk_menu_set_tearoff_hints (menu, width);
1876 gtk_widget_realize (menu->tearoff_window);
1877 gtk_menu_position (menu);
1879 gtk_widget_show (GTK_WIDGET (menu));
1880 gtk_widget_show (menu->tearoff_window);
1882 gtk_menu_scroll_to (menu, 0);
1887 gtk_widget_hide (GTK_WIDGET (menu));
1888 gtk_widget_hide (menu->tearoff_window);
1889 if (GTK_IS_CONTAINER (menu->toplevel))
1890 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1891 gtk_widget_destroy (menu->tearoff_window);
1893 menu->tearoff_window = NULL;
1894 menu->tearoff_hbox = NULL;
1895 menu->tearoff_scrollbar = NULL;
1896 menu->tearoff_adjustment = NULL;
1899 g_object_notify (G_OBJECT (menu), "tearoff-state");
1904 * gtk_menu_get_tearoff_state:
1907 * Returns whether the menu is torn off. See
1908 * gtk_menu_set_tearoff_state ().
1910 * Return value: %TRUE if the menu is currently torn off.
1913 gtk_menu_get_tearoff_state (GtkMenu *menu)
1915 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1917 return menu->torn_off;
1921 * gtk_menu_set_title:
1923 * @title: a string containing the title for the menu.
1925 * Sets the title string for the menu. The title is displayed when the menu
1926 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
1927 * attached to a parent menu item, and if so it will try to use the same text as
1928 * that menu item's label.
1931 gtk_menu_set_title (GtkMenu *menu,
1934 GtkMenuPrivate *priv;
1937 g_return_if_fail (GTK_IS_MENU (menu));
1939 priv = gtk_menu_get_private (menu);
1941 old_title = priv->title;
1942 priv->title = g_strdup (title);
1945 gtk_menu_update_title (menu);
1946 g_object_notify (G_OBJECT (menu), "tearoff-title");
1950 * gtk_menu_get_title:
1953 * Returns the title of the menu. See gtk_menu_set_title().
1955 * Return value: the title of the menu, or %NULL if the menu has no
1956 * title set on it. This string is owned by the widget and should
1957 * not be modified or freed.
1959 G_CONST_RETURN gchar *
1960 gtk_menu_get_title (GtkMenu *menu)
1962 GtkMenuPrivate *priv;
1964 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1966 priv = gtk_menu_get_private (menu);
1972 gtk_menu_reorder_child (GtkMenu *menu,
1976 GtkMenuShell *menu_shell;
1978 g_return_if_fail (GTK_IS_MENU (menu));
1979 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1981 menu_shell = GTK_MENU_SHELL (menu);
1983 if (g_list_find (menu_shell->children, child))
1985 menu_shell->children = g_list_remove (menu_shell->children, child);
1986 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1988 menu_queue_resize (menu);
1993 gtk_menu_style_set (GtkWidget *widget,
1994 GtkStyle *previous_style)
1996 if (GTK_WIDGET_REALIZED (widget))
1998 GtkMenu *menu = GTK_MENU (widget);
2000 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2001 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2002 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2007 get_arrows_border (GtkMenu *menu, GtkBorder *border)
2009 guint scroll_arrow_height;
2011 gtk_widget_style_get (GTK_WIDGET (menu),
2012 "scroll-arrow-vlength", &scroll_arrow_height,
2015 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2016 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2018 border->left = border->right = 0;
2022 gtk_menu_realize (GtkWidget *widget)
2024 GdkWindowAttr attributes;
2025 gint attributes_mask;
2030 guint vertical_padding;
2031 guint horizontal_padding;
2032 GtkBorder arrow_border;
2034 g_return_if_fail (GTK_IS_MENU (widget));
2036 menu = GTK_MENU (widget);
2038 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2040 attributes.window_type = GDK_WINDOW_CHILD;
2041 attributes.x = widget->allocation.x;
2042 attributes.y = widget->allocation.y;
2043 attributes.width = widget->allocation.width;
2044 attributes.height = widget->allocation.height;
2045 attributes.wclass = GDK_INPUT_OUTPUT;
2046 attributes.visual = gtk_widget_get_visual (widget);
2047 attributes.colormap = gtk_widget_get_colormap (widget);
2049 attributes.event_mask = gtk_widget_get_events (widget);
2051 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2052 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2054 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2055 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2056 gdk_window_set_user_data (widget->window, widget);
2058 border_width = GTK_CONTAINER (widget)->border_width;
2060 gtk_widget_style_get (GTK_WIDGET (menu),
2061 "vertical-padding", &vertical_padding,
2062 "horizontal-padding", &horizontal_padding,
2065 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2066 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2067 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2068 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2070 get_arrows_border (menu, &arrow_border);
2071 attributes.y += arrow_border.top;
2072 attributes.height -= arrow_border.top;
2073 attributes.height -= arrow_border.bottom;
2075 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2076 gdk_window_set_user_data (menu->view_window, menu);
2080 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2081 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2083 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2084 gdk_window_set_user_data (menu->bin_window, menu);
2086 children = GTK_MENU_SHELL (menu)->children;
2089 child = children->data;
2090 children = children->next;
2092 gtk_widget_set_parent_window (child, menu->bin_window);
2095 widget->style = gtk_style_attach (widget->style, widget->window);
2096 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2097 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2098 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2100 if (GTK_MENU_SHELL (widget)->active_menu_item)
2101 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2102 GTK_MENU_SHELL (widget)->active_menu_item);
2104 gdk_window_show (menu->bin_window);
2105 gdk_window_show (menu->view_window);
2109 gtk_menu_focus (GtkWidget *widget,
2110 GtkDirectionType direction)
2113 * A menu or its menu items cannot have focus
2118 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2121 menu_grab_transfer_window_get (GtkMenu *menu)
2123 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2126 GdkWindowAttr attributes;
2127 gint attributes_mask;
2129 attributes.x = -100;
2130 attributes.y = -100;
2131 attributes.width = 10;
2132 attributes.height = 10;
2133 attributes.window_type = GDK_WINDOW_TEMP;
2134 attributes.wclass = GDK_INPUT_ONLY;
2135 attributes.override_redirect = TRUE;
2136 attributes.event_mask = 0;
2138 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2140 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2141 &attributes, attributes_mask);
2142 gdk_window_set_user_data (window, menu);
2144 gdk_window_show (window);
2146 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2153 menu_grab_transfer_window_destroy (GtkMenu *menu)
2155 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2158 gdk_window_set_user_data (window, NULL);
2159 gdk_window_destroy (window);
2160 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2165 gtk_menu_unrealize (GtkWidget *widget)
2167 GtkMenu *menu = GTK_MENU (widget);
2169 menu_grab_transfer_window_destroy (menu);
2171 gdk_window_set_user_data (menu->view_window, NULL);
2172 gdk_window_destroy (menu->view_window);
2173 menu->view_window = NULL;
2175 gdk_window_set_user_data (menu->bin_window, NULL);
2176 gdk_window_destroy (menu->bin_window);
2177 menu->bin_window = NULL;
2179 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize) (widget);
2183 gtk_menu_size_request (GtkWidget *widget,
2184 GtkRequisition *requisition)
2188 GtkMenuShell *menu_shell;
2191 guint max_toggle_size;
2192 guint max_accel_width;
2193 guint vertical_padding;
2194 guint horizontal_padding;
2195 GtkRequisition child_requisition;
2196 GtkMenuPrivate *priv;
2198 g_return_if_fail (GTK_IS_MENU (widget));
2199 g_return_if_fail (requisition != NULL);
2201 menu = GTK_MENU (widget);
2202 menu_shell = GTK_MENU_SHELL (widget);
2203 priv = gtk_menu_get_private (menu);
2205 requisition->width = 0;
2206 requisition->height = 0;
2208 max_toggle_size = 0;
2209 max_accel_width = 0;
2211 g_free (priv->heights);
2212 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2213 priv->heights_length = gtk_menu_get_n_rows (menu);
2215 children = menu_shell->children;
2222 child = children->data;
2223 children = children->next;
2225 if (! GTK_WIDGET_VISIBLE (child))
2228 get_effective_child_attach (child, &l, &r, &t, &b);
2230 /* It's important to size_request the child
2231 * before doing the toggle size request, in
2232 * case the toggle size request depends on the size
2233 * request of a child of the child (e.g. for ImageMenuItem)
2236 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2237 gtk_widget_size_request (child, &child_requisition);
2239 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2240 max_toggle_size = MAX (max_toggle_size, toggle_size);
2241 max_accel_width = MAX (max_accel_width,
2242 GTK_MENU_ITEM (child)->accelerator_width);
2244 part = child_requisition.width / (r - l);
2245 requisition->width = MAX (requisition->width, part);
2247 part = MAX (child_requisition.height, toggle_size) / (b - t);
2248 priv->heights[t] = MAX (priv->heights[t], part);
2251 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2252 requisition->height += priv->heights[i];
2254 requisition->width += max_toggle_size + max_accel_width;
2255 requisition->width *= gtk_menu_get_n_columns (menu);
2257 gtk_widget_style_get (GTK_WIDGET (menu),
2258 "vertical-padding", &vertical_padding,
2259 "horizontal-padding", &horizontal_padding,
2262 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2263 widget->style->xthickness) * 2;
2264 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2265 widget->style->ythickness) * 2;
2267 menu->toggle_size = max_toggle_size;
2269 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2271 if (menu->tearoff_active)
2272 gtk_menu_set_tearoff_hints (menu, requisition->width);
2276 gtk_menu_size_allocate (GtkWidget *widget,
2277 GtkAllocation *allocation)
2280 GtkMenuShell *menu_shell;
2282 GtkAllocation child_allocation;
2283 GtkRequisition child_requisition;
2284 GtkMenuPrivate *priv;
2288 guint vertical_padding;
2289 guint horizontal_padding;
2291 g_return_if_fail (GTK_IS_MENU (widget));
2292 g_return_if_fail (allocation != NULL);
2294 menu = GTK_MENU (widget);
2295 menu_shell = GTK_MENU_SHELL (widget);
2296 priv = gtk_menu_get_private (menu);
2298 widget->allocation = *allocation;
2299 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2301 gtk_widget_style_get (GTK_WIDGET (menu),
2302 "vertical-padding", &vertical_padding,
2303 "horizontal-padding", &horizontal_padding,
2306 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2307 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2309 width = MAX (1, allocation->width - x * 2);
2310 height = MAX (1, allocation->height - y * 2);
2312 child_requisition.width -= x * 2;
2313 child_requisition.height -= y * 2;
2315 if (menu_shell->active)
2316 gtk_menu_scroll_to (menu, menu->scroll_offset);
2318 if (!menu->tearoff_active)
2320 GtkBorder arrow_border;
2322 get_arrows_border (menu, &arrow_border);
2323 y += arrow_border.top;
2324 height -= arrow_border.top;
2325 height -= arrow_border.bottom;
2328 if (GTK_WIDGET_REALIZED (widget))
2330 gdk_window_move_resize (widget->window,
2331 allocation->x, allocation->y,
2332 allocation->width, allocation->height);
2334 gdk_window_move_resize (menu->view_window,
2341 if (menu_shell->children)
2343 gint base_width = width / gtk_menu_get_n_columns (menu);
2345 children = menu_shell->children;
2348 child = children->data;
2349 children = children->next;
2351 if (GTK_WIDGET_VISIBLE (child))
2356 get_effective_child_attach (child, &l, &r, &t, &b);
2358 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2361 tmp = gtk_menu_get_n_columns (menu) - l;
2362 l = gtk_menu_get_n_columns (menu) - r;
2366 child_allocation.width = (r - l) * base_width;
2367 child_allocation.height = 0;
2368 child_allocation.x = l * base_width;
2369 child_allocation.y = 0;
2371 for (i = 0; i < b; i++)
2374 child_allocation.y += priv->heights[i];
2376 child_allocation.height += priv->heights[i];
2379 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2382 gtk_widget_size_allocate (child, &child_allocation);
2383 gtk_widget_queue_draw (child);
2387 /* Resize the item window */
2388 if (GTK_WIDGET_REALIZED (widget))
2394 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2395 height += priv->heights[i];
2397 width = gtk_menu_get_n_columns (menu) * base_width;
2398 gdk_window_resize (menu->bin_window, width, height);
2401 if (menu->tearoff_active)
2403 if (allocation->height >= widget->requisition.height)
2405 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2407 gtk_widget_hide (menu->tearoff_scrollbar);
2408 gtk_menu_set_tearoff_hints (menu, allocation->width);
2410 gtk_menu_scroll_to (menu, 0);
2415 menu->tearoff_adjustment->upper = widget->requisition.height;
2416 menu->tearoff_adjustment->page_size = allocation->height;
2418 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2419 menu->tearoff_adjustment->upper)
2422 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2425 gtk_menu_scroll_to (menu, value);
2428 gtk_adjustment_changed (menu->tearoff_adjustment);
2430 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2432 gtk_widget_show (menu->tearoff_scrollbar);
2433 gtk_menu_set_tearoff_hints (menu, allocation->width);
2441 get_arrows_visible_area (GtkMenu *menu,
2442 GdkRectangle *border,
2443 GdkRectangle *upper,
2444 GdkRectangle *lower,
2447 GtkWidget *widget = GTK_WIDGET (menu);
2448 guint vertical_padding;
2449 guint horizontal_padding;
2450 gint scroll_arrow_height;
2452 gtk_widget_style_get (widget,
2453 "vertical-padding", &vertical_padding,
2454 "horizontal-padding", &horizontal_padding,
2455 "scroll-arrow-vlength", &scroll_arrow_height,
2458 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2459 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2460 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2462 upper->x = border->x;
2463 upper->y = border->y;
2464 upper->width = border->width - 2 * border->x;
2465 upper->height = scroll_arrow_height;
2467 lower->x = border->x;
2468 lower->y = border->height - border->y - scroll_arrow_height;
2469 lower->width = border->width - 2 * border->x;
2470 lower->height = scroll_arrow_height;
2472 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2476 gtk_menu_paint (GtkWidget *widget,
2477 GdkEventExpose *event)
2480 GtkMenuPrivate *priv;
2481 GdkRectangle border;
2486 g_return_if_fail (GTK_IS_MENU (widget));
2488 menu = GTK_MENU (widget);
2489 priv = gtk_menu_get_private (menu);
2491 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2493 if (event->window == widget->window)
2495 gint arrow_size = 0.7 * arrow_space;
2497 gtk_paint_box (widget->style,
2501 &event->area, widget, "menu",
2504 if (menu->upper_arrow_visible && !menu->tearoff_active)
2506 gtk_paint_box (widget->style,
2508 priv->upper_arrow_state,
2510 &event->area, widget, "menu_scroll_arrow_up",
2516 gtk_paint_arrow (widget->style,
2518 priv->upper_arrow_state,
2520 &event->area, widget, "menu_scroll_arrow_up",
2523 upper.x + (upper.width - arrow_size) / 2,
2524 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2525 arrow_size, arrow_size);
2528 if (menu->lower_arrow_visible && !menu->tearoff_active)
2530 gtk_paint_box (widget->style,
2532 priv->lower_arrow_state,
2534 &event->area, widget, "menu_scroll_arrow_down",
2540 gtk_paint_arrow (widget->style,
2542 priv->lower_arrow_state,
2544 &event->area, widget, "menu_scroll_arrow_down",
2547 lower.x + (lower.width - arrow_size) / 2,
2548 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2549 arrow_size, arrow_size);
2552 else if (event->window == menu->bin_window)
2554 gint y = -border.y + menu->scroll_offset;
2556 if (!menu->tearoff_active)
2558 GtkBorder arrow_border;
2560 get_arrows_border (menu, &arrow_border);
2561 y -= arrow_border.top;
2564 gtk_paint_box (widget->style,
2568 &event->area, widget, "menu",
2570 border.width, border.height);
2575 gtk_menu_expose (GtkWidget *widget,
2576 GdkEventExpose *event)
2578 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2579 g_return_val_if_fail (event != NULL, FALSE);
2581 if (GTK_WIDGET_DRAWABLE (widget))
2583 gtk_menu_paint (widget, event);
2585 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event) (widget, event);
2592 gtk_menu_show (GtkWidget *widget)
2594 GtkMenu *menu = GTK_MENU (widget);
2596 _gtk_menu_refresh_accel_paths (menu, FALSE);
2598 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2602 gtk_menu_button_scroll (GtkMenu *menu,
2603 GdkEventButton *event)
2605 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2607 gboolean touchscreen_mode;
2609 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
2610 "gtk-touchscreen-mode", &touchscreen_mode,
2613 if (touchscreen_mode)
2614 gtk_menu_handle_scrolling (menu,
2615 event->x_root, event->y_root,
2616 event->type == GDK_BUTTON_PRESS,
2626 pointer_in_menu_window (GtkWidget *widget,
2630 GtkMenu *menu = GTK_MENU (widget);
2632 if (GTK_WIDGET_MAPPED (menu->toplevel))
2634 GtkMenuShell *menu_shell;
2635 gint window_x, window_y;
2637 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
2639 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
2640 y_root >= window_y && y_root < window_y + widget->allocation.height)
2643 menu_shell = GTK_MENU_SHELL (widget);
2645 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
2646 return pointer_in_menu_window (menu_shell->parent_menu_shell,
2654 gtk_menu_button_press (GtkWidget *widget,
2655 GdkEventButton *event)
2657 if (event->type != GDK_BUTTON_PRESS)
2660 /* Don't pass down to menu shell for presses over scroll arrows
2662 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2665 /* Don't pass down to menu shell if a non-menuitem part of the menu
2666 * was clicked. The check for the event_widget being a GtkMenuShell
2667 * works because we have the pointer grabbed on menu_shell->window
2668 * with owner_events=TRUE, so all events that are either outside
2669 * the menu or on its border are delivered relative to
2670 * menu_shell->window.
2672 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2673 pointer_in_menu_window (widget, event->x_root, event->y_root))
2676 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2680 gtk_menu_button_release (GtkWidget *widget,
2681 GdkEventButton *event)
2683 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2685 if (priv->ignore_button_release)
2687 priv->ignore_button_release = FALSE;
2691 if (event->type != GDK_BUTTON_RELEASE)
2694 /* Don't pass down to menu shell for releases over scroll arrows
2696 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2699 /* Don't pass down to menu shell if a non-menuitem part of the menu
2700 * was clicked (see comment in button_press()).
2702 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2703 pointer_in_menu_window (widget, event->x_root, event->y_root))
2705 /* Ugly: make sure menu_shell->button gets reset to 0 when we
2706 * bail out early here so it is in a consistent state for the
2707 * next button_press/button_release in GtkMenuShell.
2710 if (GTK_MENU_SHELL (widget)->active)
2711 GTK_MENU_SHELL (widget)->button = 0;
2716 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
2719 static const gchar *
2720 get_accel_path (GtkWidget *menu_item,
2725 GClosure *accel_closure;
2726 GtkAccelGroup *accel_group;
2728 path = _gtk_widget_get_accel_path (menu_item, locked);
2731 path = GTK_MENU_ITEM (menu_item)->accel_path;
2737 label = GTK_BIN (menu_item)->child;
2739 if (GTK_IS_ACCEL_LABEL (label))
2741 g_object_get (label,
2742 "accel-closure", &accel_closure,
2746 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2748 *locked = accel_group->lock_count > 0;
2758 gtk_menu_key_press (GtkWidget *widget,
2761 GtkMenuShell *menu_shell;
2763 gboolean delete = FALSE;
2764 gboolean can_change_accels;
2765 gchar *accel = NULL;
2766 guint accel_key, accel_mods;
2767 GdkModifierType consumed_modifiers;
2768 GdkDisplay *display;
2770 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2771 g_return_val_if_fail (event != NULL, FALSE);
2773 menu_shell = GTK_MENU_SHELL (widget);
2774 menu = GTK_MENU (widget);
2776 gtk_menu_stop_navigating_submenu (menu);
2778 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
2781 display = gtk_widget_get_display (widget);
2783 g_object_get (gtk_widget_get_settings (widget),
2784 "gtk-menu-bar-accel", &accel,
2785 "gtk-can-change-accels", &can_change_accels,
2788 if (accel && *accel)
2791 GdkModifierType mods = 0;
2793 gtk_accelerator_parse (accel, &keyval, &mods);
2796 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2798 /* FIXME this is wrong, needs to be in the global accel resolution
2799 * thing, to properly consider i18n etc., but that probably requires
2800 * AccelGroup changes etc.
2802 if (event->keyval == keyval && (mods & event->state) == mods)
2804 gtk_menu_shell_cancel (menu_shell);
2812 switch (event->keyval)
2823 /* Figure out what modifiers went into determining the key symbol */
2824 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2825 event->hardware_keycode, event->state, event->group,
2826 NULL, NULL, NULL, &consumed_modifiers);
2828 accel_key = gdk_keyval_to_lower (event->keyval);
2829 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2831 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2832 * We re-upper case when we match against the keyval, but display and save in caseless form.
2834 if (accel_key != event->keyval)
2835 accel_mods |= GDK_SHIFT_MASK;
2837 /* Modify the accelerators */
2838 if (can_change_accels &&
2839 menu_shell->active_menu_item &&
2840 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2841 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2842 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2844 GtkWidget *menu_item = menu_shell->active_menu_item;
2845 gboolean locked, replace_accels = TRUE;
2848 path = get_accel_path (menu_item, &locked);
2849 if (!path || locked)
2851 /* can't change accelerators on menu_items without paths
2852 * (basically, those items are accelerator-locked).
2854 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2855 gtk_widget_error_bell (widget);
2861 /* For the keys that act to delete the current setting, we delete
2862 * the current setting if there is one, otherwise, we set the
2863 * key as the accelerator.
2869 if (gtk_accel_map_lookup_entry (path, &key) &&
2870 (key.accel_key || key.accel_mods))
2876 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2880 /* we failed, probably because this key is in use and
2883 /* g_print("failed to change\n"); */
2884 gtk_widget_error_bell (widget);
2893 check_threshold (GtkWidget *widget,
2894 int start_x, int start_y,
2900 ABS (start_x - x) > THRESHOLD ||
2901 ABS (start_y - y) > THRESHOLD;
2905 definitely_within_item (GtkWidget *widget,
2909 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
2912 gdk_drawable_get_size (window, &w, &h);
2915 check_threshold (widget, 0, 0, x, y) &&
2916 check_threshold (widget, w - 1, 0, x, y) &&
2917 check_threshold (widget, w - 1, h - 1, x, y) &&
2918 check_threshold (widget, 0, h - 1, x, y);
2922 gtk_menu_motion_notify (GtkWidget *widget,
2923 GdkEventMotion *event)
2925 GtkWidget *menu_item;
2927 GtkMenuShell *menu_shell;
2929 gboolean need_enter;
2931 if (GTK_IS_MENU (widget))
2933 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2935 if (priv->ignore_button_release)
2936 priv->ignore_button_release = FALSE;
2938 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
2942 /* We received the event for one of two reasons:
2944 * a) We are the active menu, and did gtk_grab_add()
2945 * b) The widget is a child of ours, and the event was propagated
2947 * Since for computation of navigation regions, we want the menu which
2948 * is the parent of the menu item, for a), we need to find that menu,
2949 * which may be different from 'widget'.
2951 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2952 if (!GTK_IS_MENU_ITEM (menu_item) ||
2953 !GTK_IS_MENU (menu_item->parent))
2956 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2957 menu = GTK_MENU (menu_shell);
2959 if (definitely_within_item (menu_item, event->x, event->y))
2960 menu_shell->activate_time = 0;
2962 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2964 /* Check to see if we are within an active submenu's navigation region
2966 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2969 /* Make sure we pop down if we enter a non-selectable menu item, so we
2970 * don't show a submenu when the cursor is outside the stay-up triangle.
2972 if (!_gtk_menu_item_is_selectable (menu_item))
2974 gtk_menu_shell_deselect (menu_shell);
2980 /* The menu is now sensitive to enter events on its items, but
2981 * was previously sensitive. So we fake an enter event.
2985 menu_shell->ignore_enter = FALSE;
2987 gdk_drawable_get_size (event->window, &width, &height);
2988 if (event->x >= 0 && event->x < width &&
2989 event->y >= 0 && event->y < height)
2991 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2994 send_event->crossing.window = g_object_ref (event->window);
2995 send_event->crossing.time = event->time;
2996 send_event->crossing.send_event = TRUE;
2997 send_event->crossing.x_root = event->x_root;
2998 send_event->crossing.y_root = event->y_root;
2999 send_event->crossing.x = event->x;
3000 send_event->crossing.y = event->y;
3001 send_event->crossing.state = event->state;
3003 /* We send the event to 'widget', the currently active menu,
3004 * instead of 'menu', the menu that the pointer is in. This
3005 * will ensure that the event will be ignored unless the
3006 * menuitem is a child of the active menu or some parent
3007 * menu of the active menu.
3009 result = gtk_widget_event (widget, send_event);
3010 gdk_event_free (send_event);
3020 get_double_arrows (GtkMenu *menu)
3022 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3023 gboolean double_arrows;
3025 gtk_widget_style_get (GTK_WIDGET (menu),
3026 "double-arrows", &double_arrows,
3029 return double_arrows || (priv->initially_pushed_in &&
3030 menu->scroll_offset != 0);
3034 gtk_menu_scroll_by (GtkMenu *menu,
3039 gint view_width, view_height;
3040 gboolean double_arrows;
3041 GtkBorder arrow_border;
3043 widget = GTK_WIDGET (menu);
3044 offset = menu->scroll_offset + step;
3046 get_arrows_border (menu, &arrow_border);
3048 double_arrows = get_double_arrows (menu);
3050 /* If we scroll upward and the non-visible top part
3051 * is smaller than the scroll arrow it would be
3052 * pretty stupid to show the arrow and taking more
3053 * screen space than just scrolling to the top.
3056 if ((step < 0) && (offset < arrow_border.top))
3059 /* Don't scroll over the top if we weren't before: */
3060 if ((menu->scroll_offset >= 0) && (offset < 0))
3063 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3065 if (menu->scroll_offset == 0 &&
3066 view_height >= widget->requisition.height)
3069 /* Don't scroll past the bottom if we weren't before: */
3070 if (menu->scroll_offset > 0)
3071 view_height -= arrow_border.top;
3073 /* When both arrows are always shown, reduce
3074 * view height even more.
3077 view_height -= arrow_border.bottom;
3079 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3080 (offset + view_height > widget->requisition.height))
3081 offset = widget->requisition.height - view_height;
3083 if (offset != menu->scroll_offset)
3084 gtk_menu_scroll_to (menu, offset);
3088 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3089 gboolean touchscreen_mode)
3091 gboolean upper_visible;
3092 gboolean lower_visible;
3094 upper_visible = menu->upper_arrow_visible;
3095 lower_visible = menu->lower_arrow_visible;
3097 gtk_menu_scroll_by (menu, menu->scroll_step);
3099 if (touchscreen_mode &&
3100 (upper_visible != menu->upper_arrow_visible ||
3101 lower_visible != menu->lower_arrow_visible))
3103 /* We are about to hide a scroll arrow while the mouse is pressed,
3104 * this would cause the uncovered menu item to be activated on button
3105 * release. Therefore we need to ignore button release here
3107 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3108 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3113 gtk_menu_scroll_timeout (gpointer data)
3116 gboolean touchscreen_mode;
3118 menu = GTK_MENU (data);
3120 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3121 "gtk-touchscreen-mode", &touchscreen_mode,
3124 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3130 gtk_menu_scroll_timeout_initial (gpointer data)
3134 gboolean touchscreen_mode;
3136 menu = GTK_MENU (data);
3138 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3139 "gtk-timeout-repeat", &timeout,
3140 "gtk-touchscreen-mode", &touchscreen_mode,
3143 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3145 gtk_menu_remove_scroll_timeout (menu);
3147 menu->timeout_id = gdk_threads_add_timeout (timeout,
3148 gtk_menu_scroll_timeout,
3155 gtk_menu_start_scrolling (GtkMenu *menu)
3158 gboolean touchscreen_mode;
3160 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3161 "gtk-timeout-repeat", &timeout,
3162 "gtk-touchscreen-mode", &touchscreen_mode,
3165 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3167 menu->timeout_id = gdk_threads_add_timeout (timeout,
3168 gtk_menu_scroll_timeout_initial,
3173 gtk_menu_scroll (GtkWidget *widget,
3174 GdkEventScroll *event)
3176 GtkMenu *menu = GTK_MENU (widget);
3178 switch (event->direction)
3180 case GDK_SCROLL_RIGHT:
3181 case GDK_SCROLL_DOWN:
3182 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3184 case GDK_SCROLL_LEFT:
3186 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3194 get_arrows_sensitive_area (GtkMenu *menu,
3195 GdkRectangle *upper,
3196 GdkRectangle *lower)
3200 guint vertical_padding;
3202 gint scroll_arrow_height;
3204 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3206 gtk_widget_style_get (GTK_WIDGET (menu),
3207 "vertical-padding", &vertical_padding,
3208 "scroll-arrow-vlength", &scroll_arrow_height,
3211 border = GTK_CONTAINER (menu)->border_width +
3212 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3214 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3220 upper->width = width;
3221 upper->height = scroll_arrow_height + border;
3227 lower->y = win_y + height - border - scroll_arrow_height;
3228 lower->width = width;
3229 lower->height = scroll_arrow_height + border;
3235 gtk_menu_handle_scrolling (GtkMenu *menu,
3241 GtkMenuShell *menu_shell;
3242 GtkMenuPrivate *priv;
3245 gboolean scroll_fast = FALSE;
3247 gboolean touchscreen_mode;
3249 priv = gtk_menu_get_private (menu);
3251 menu_shell = GTK_MENU_SHELL (menu);
3253 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3254 "gtk-touchscreen-mode", &touchscreen_mode,
3257 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3261 /* upper arrow handling */
3263 get_arrows_sensitive_area (menu, &rect, NULL);
3266 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3267 (x >= rect.x) && (x < rect.x + rect.width) &&
3268 (y >= rect.y) && (y < rect.y + rect.height))
3273 if (touchscreen_mode)
3274 menu->upper_arrow_prelight = in_arrow;
3276 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3278 gboolean arrow_pressed = FALSE;
3280 if (menu->upper_arrow_visible && !menu->tearoff_active)
3282 if (touchscreen_mode)
3284 if (enter && menu->upper_arrow_prelight)
3286 if (menu->timeout_id == 0)
3288 /* Deselect the active item so that
3289 * any submenus are popped down
3291 gtk_menu_shell_deselect (menu_shell);
3293 gtk_menu_remove_scroll_timeout (menu);
3294 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3298 /* Only do stuff on click. */
3299 gtk_menu_start_scrolling (menu);
3300 arrow_pressed = TRUE;
3305 arrow_pressed = TRUE;
3310 gtk_menu_stop_scrolling (menu);
3313 else /* !touchscreen_mode */
3315 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3317 if (enter && in_arrow &&
3318 (!menu->upper_arrow_prelight ||
3319 menu->scroll_fast != scroll_fast))
3321 menu->upper_arrow_prelight = TRUE;
3322 menu->scroll_fast = scroll_fast;
3324 /* Deselect the active item so that
3325 * any submenus are popped down
3327 gtk_menu_shell_deselect (menu_shell);
3329 gtk_menu_remove_scroll_timeout (menu);
3330 menu->scroll_step = scroll_fast ?
3331 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3334 gdk_threads_add_timeout (scroll_fast ?
3335 MENU_SCROLL_TIMEOUT2 :
3336 MENU_SCROLL_TIMEOUT1,
3337 gtk_menu_scroll_timeout, menu);
3339 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3341 gtk_menu_stop_scrolling (menu);
3346 /* gtk_menu_start_scrolling() might have hit the top of the
3347 * menu, so check if the button isn't insensitive before
3348 * changing it to something else.
3350 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3352 GtkStateType arrow_state = GTK_STATE_NORMAL;
3355 arrow_state = GTK_STATE_ACTIVE;
3356 else if (menu->upper_arrow_prelight)
3357 arrow_state = GTK_STATE_PRELIGHT;
3359 if (arrow_state != priv->upper_arrow_state)
3361 priv->upper_arrow_state = arrow_state;
3363 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3369 /* lower arrow handling */
3371 get_arrows_sensitive_area (menu, NULL, &rect);
3374 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3375 (x >= rect.x) && (x < rect.x + rect.width) &&
3376 (y >= rect.y) && (y < rect.y + rect.height))
3381 if (touchscreen_mode)
3382 menu->lower_arrow_prelight = in_arrow;
3384 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3386 gboolean arrow_pressed = FALSE;
3388 if (menu->lower_arrow_visible && !menu->tearoff_active)
3390 if (touchscreen_mode)
3392 if (enter && menu->lower_arrow_prelight)
3394 if (menu->timeout_id == 0)
3396 /* Deselect the active item so that
3397 * any submenus are popped down
3399 gtk_menu_shell_deselect (menu_shell);
3401 gtk_menu_remove_scroll_timeout (menu);
3402 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3406 /* Only do stuff on click. */
3407 gtk_menu_start_scrolling (menu);
3408 arrow_pressed = TRUE;
3413 arrow_pressed = TRUE;
3418 gtk_menu_stop_scrolling (menu);
3421 else /* !touchscreen_mode */
3423 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3425 if (enter && in_arrow &&
3426 (!menu->lower_arrow_prelight ||
3427 menu->scroll_fast != scroll_fast))
3429 menu->lower_arrow_prelight = TRUE;
3430 menu->scroll_fast = scroll_fast;
3432 /* Deselect the active item so that
3433 * any submenus are popped down
3435 gtk_menu_shell_deselect (menu_shell);
3437 gtk_menu_remove_scroll_timeout (menu);
3438 menu->scroll_step = scroll_fast ?
3439 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3442 gdk_threads_add_timeout (scroll_fast ?
3443 MENU_SCROLL_TIMEOUT2 :
3444 MENU_SCROLL_TIMEOUT1,
3445 gtk_menu_scroll_timeout, menu);
3447 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3449 gtk_menu_stop_scrolling (menu);
3454 /* gtk_menu_start_scrolling() might have hit the bottom of the
3455 * menu, so check if the button isn't insensitive before
3456 * changing it to something else.
3458 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3460 GtkStateType arrow_state = GTK_STATE_NORMAL;
3463 arrow_state = GTK_STATE_ACTIVE;
3464 else if (menu->lower_arrow_prelight)
3465 arrow_state = GTK_STATE_PRELIGHT;
3467 if (arrow_state != priv->lower_arrow_state)
3469 priv->lower_arrow_state = arrow_state;
3471 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3479 gtk_menu_enter_notify (GtkWidget *widget,
3480 GdkEventCrossing *event)
3482 GtkWidget *menu_item;
3483 gboolean touchscreen_mode;
3485 g_object_get (gtk_widget_get_settings (widget),
3486 "gtk-touchscreen-mode", &touchscreen_mode,
3489 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3490 if (GTK_IS_MENU (widget))
3492 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3494 if (!menu_shell->ignore_enter)
3495 gtk_menu_handle_scrolling (GTK_MENU (widget),
3496 event->x_root, event->y_root, TRUE, TRUE);
3499 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3501 GtkWidget *menu = menu_item->parent;
3503 if (GTK_IS_MENU (menu))
3505 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3506 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3508 if (priv->seen_item_enter)
3510 /* This is the second enter we see for an item
3511 * on this menu. This means a release should always
3514 menu_shell->activate_time = 0;
3516 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3517 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3519 if (definitely_within_item (menu_item, event->x, event->y))
3521 /* This is an actual user-enter (ie. not a pop-under)
3522 * In this case, the user must either have entered
3523 * sufficiently far enough into the item, or he must move
3524 * far enough away from the enter point. (see
3525 * gtk_menu_motion_notify())
3527 menu_shell->activate_time = 0;
3531 priv->seen_item_enter = TRUE;
3535 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3536 * will not correspond to the event widget's parent. Check to see
3537 * if we are in the parent's navigation region.
3539 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3540 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3541 event->x_root, event->y_root))
3544 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3548 gtk_menu_leave_notify (GtkWidget *widget,
3549 GdkEventCrossing *event)
3551 GtkMenuShell *menu_shell;
3553 GtkMenuItem *menu_item;
3554 GtkWidget *event_widget;
3556 menu = GTK_MENU (widget);
3557 menu_shell = GTK_MENU_SHELL (widget);
3559 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3562 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3564 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3566 if (!GTK_IS_MENU_ITEM (event_widget))
3569 menu_item = GTK_MENU_ITEM (event_widget);
3571 /* Here we check to see if we're leaving an active menu item with a submenu,
3572 * in which case we enter submenu navigation mode.
3574 if (menu_shell->active_menu_item != NULL
3575 && menu_item->submenu != NULL
3576 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3578 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3580 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3583 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3585 /* We are leaving an active menu item with nonactive submenu.
3586 * Deselect it so we don't surprise the user with by popping
3587 * up a submenu _after_ he left the item.
3589 gtk_menu_shell_deselect (menu_shell);
3594 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3598 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3600 if (menu->navigation_region)
3602 gdk_region_destroy (menu->navigation_region);
3603 menu->navigation_region = NULL;
3605 if (menu->navigation_timeout)
3607 g_source_remove (menu->navigation_timeout);
3608 menu->navigation_timeout = 0;
3612 /* When the timeout is elapsed, the navigation region is destroyed
3613 * and the menuitem under the pointer (if any) is selected.
3616 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3618 GtkMenu *menu = user_data;
3619 GdkWindow *child_window;
3621 gtk_menu_stop_navigating_submenu (menu);
3623 if (GTK_WIDGET_REALIZED (menu))
3625 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3629 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3631 send_event->crossing.window = g_object_ref (child_window);
3632 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3633 send_event->crossing.send_event = TRUE;
3635 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3637 gdk_event_free (send_event);
3645 gtk_menu_navigating_submenu (GtkMenu *menu,
3649 if (menu->navigation_region)
3651 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3655 gtk_menu_stop_navigating_submenu (menu);
3662 #undef DRAW_STAY_UP_TRIANGLE
3664 #ifdef DRAW_STAY_UP_TRIANGLE
3667 draw_stay_up_triangle (GdkWindow *window,
3670 /* Draw ugly color all over the stay-up triangle */
3671 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3672 GdkGCValues gc_values;
3674 GdkRectangle clipbox;
3676 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3677 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3678 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3679 gdk_gc_set_clip_region (ugly_gc, region);
3681 gdk_region_get_clipbox (region, &clipbox);
3683 gdk_draw_rectangle (window,
3686 clipbox.x, clipbox.y,
3687 clipbox.width, clipbox.height);
3689 g_object_unref (ugly_gc);
3694 flip_region (GdkRegion *region,
3699 GdkRectangle *rectangles;
3700 GdkRectangle clipbox;
3701 GdkRegion *new_region;
3704 new_region = gdk_region_new ();
3706 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3707 gdk_region_get_clipbox (region, &clipbox);
3709 for (i = 0; i < n_rectangles; ++i)
3711 GdkRectangle rect = rectangles[i];
3714 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3717 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3719 gdk_region_union_with_rect (new_region, &rect);
3722 g_free (rectangles);
3728 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3729 GtkMenuItem *menu_item,
3730 GdkEventCrossing *event)
3732 gint submenu_left = 0;
3733 gint submenu_right = 0;
3734 gint submenu_top = 0;
3735 gint submenu_bottom = 0;
3739 GtkWidget *event_widget;
3741 g_return_if_fail (menu_item->submenu != NULL);
3742 g_return_if_fail (event != NULL);
3744 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3746 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3747 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3749 submenu_right = submenu_left + width;
3750 submenu_bottom = submenu_top + height;
3752 gdk_drawable_get_size (event_widget->window, &width, &height);
3754 if (event->x >= 0 && event->x < width)
3757 gboolean flip_y = FALSE;
3758 gboolean flip_x = FALSE;
3760 gtk_menu_stop_navigating_submenu (menu);
3762 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3765 point[0].x = event->x_root;
3766 point[1].x = submenu_left;
3771 point[0].x = event->x_root + 1;
3772 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3780 point[0].y = event->y_root + 1;
3781 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3783 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3791 point[0].y = event->y_root;
3792 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3794 if (point[0].y >= submenu_bottom)
3798 point[2].x = point[1].x;
3799 point[2].y = point[0].y;
3801 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3803 if (flip_x || flip_y)
3805 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3806 gdk_region_destroy (menu->navigation_region);
3807 menu->navigation_region = new_region;
3810 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3811 "gtk-menu-popdown-delay", &popdown_delay,
3814 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
3815 gtk_menu_stop_navigating_submenu_cb,
3818 #ifdef DRAW_STAY_UP_TRIANGLE
3819 draw_stay_up_triangle (gdk_get_default_root_window(),
3820 menu->navigation_region);
3826 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3830 g_return_if_fail (GTK_IS_MENU (menu_shell));
3832 parent = menu_shell->parent_menu_shell;
3834 menu_shell->activate_time = 0;
3835 gtk_menu_popdown (GTK_MENU (menu_shell));
3838 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3842 gtk_menu_position (GtkMenu *menu)
3845 GtkRequisition requisition;
3846 GtkMenuPrivate *private;
3851 GdkScreen *pointer_screen;
3852 GdkRectangle monitor;
3854 g_return_if_fail (GTK_IS_MENU (menu));
3856 widget = GTK_WIDGET (menu);
3858 screen = gtk_widget_get_screen (widget);
3859 gdk_display_get_pointer (gdk_screen_get_display (screen),
3860 &pointer_screen, &x, &y, NULL);
3862 /* We need the requisition to figure out the right place to
3863 * popup the menu. In fact, we always need to ask here, since
3864 * if a size_request was queued while we weren't popped up,
3865 * the requisition won't have been recomputed yet.
3867 gtk_widget_size_request (widget, &requisition);
3869 if (pointer_screen != screen)
3871 /* Pointer is on a different screen; roughly center the
3872 * menu on the screen. If someone was using multiscreen
3873 * + Xinerama together they'd probably want something
3874 * fancier; but that is likely to be vanishingly rare.
3876 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3877 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3880 private = gtk_menu_get_private (menu);
3881 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3883 private->initially_pushed_in = FALSE;
3885 /* Set the type hint here to allow custom position functions to set a different hint */
3886 if (!GTK_WIDGET_VISIBLE (menu->toplevel))
3887 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
3889 if (menu->position_func)
3891 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
3892 menu->position_func_data);
3893 if (private->monitor_num < 0)
3894 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3896 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3900 gint space_left, space_right, space_above, space_below;
3903 gint xthickness = widget->style->xthickness;
3904 gint ythickness = widget->style->ythickness;
3905 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3907 /* The placement of popup menus horizontally works like this (with
3908 * RTL in parentheses)
3910 * - If there is enough room to the right (left) of the mouse cursor,
3911 * position the menu there.
3913 * - Otherwise, if if there is enough room to the left (right) of the
3914 * mouse cursor, position the menu there.
3916 * - Otherwise if the menu is smaller than the monitor, position it
3917 * on the side of the mouse cursor that has the most space available
3919 * - Otherwise (if there is simply not enough room for the menu on the
3920 * monitor), position it as far left (right) as possible.
3922 * Positioning in the vertical direction is similar: first try below
3923 * mouse cursor, then above.
3925 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3927 space_left = x - monitor.x;
3928 space_right = monitor.x + monitor.width - x - 1;
3929 space_above = y - monitor.y;
3930 space_below = monitor.y + monitor.height - y - 1;
3932 /* position horizontally */
3934 /* the amount of space we need to position the menu. Note the
3935 * menu is offset "xthickness" pixels
3937 needed_width = requisition.width - xthickness;
3939 if (needed_width <= space_left ||
3940 needed_width <= space_right)
3942 if ((rtl && needed_width <= space_left) ||
3943 (!rtl && needed_width > space_right))
3946 x = x + xthickness - requisition.width + 1;
3950 /* position right */
3954 /* x is clamped on-screen further down */
3956 else if (requisition.width <= monitor.width)
3958 /* the menu is too big to fit on either side of the mouse
3959 * cursor, but smaller than the monitor. Position it on
3960 * the side that has the most space
3962 if (space_left > space_right)
3970 x = monitor.x + monitor.width - requisition.width;
3973 else /* menu is simply too big for the monitor */
3978 x = monitor.x + monitor.width - requisition.width;
3987 /* Position vertically. The algorithm is the same as above, but
3988 * simpler because we don't have to take RTL into account.
3990 needed_height = requisition.height - ythickness;
3992 if (needed_height <= space_above ||
3993 needed_height <= space_below)
3995 if (needed_height <= space_below)
3998 y = y + ythickness - requisition.height + 1;
4000 y = CLAMP (y, monitor.y,
4001 monitor.y + monitor.height - requisition.height);
4003 else if (needed_height > space_below && needed_height > space_above)
4005 if (space_below >= space_above)
4006 y = monitor.y + monitor.height - requisition.height;
4018 if (private->initially_pushed_in)
4020 menu_height = GTK_WIDGET (menu)->requisition.height;
4022 if (y + menu_height > monitor.y + monitor.height)
4024 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4025 y = (monitor.y + monitor.height) - menu_height;
4030 scroll_offset += monitor.y - y;
4035 /* FIXME: should this be done in the various position_funcs ? */
4036 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4038 if (GTK_MENU_SHELL (menu)->active)
4040 private->have_position = TRUE;
4045 if (y + requisition.height > monitor.y + monitor.height)
4046 requisition.height = (monitor.y + monitor.height) - y;
4050 scroll_offset += monitor.y - y;
4051 requisition.height -= monitor.y - y;
4055 if (scroll_offset > 0)
4057 GtkBorder arrow_border;
4059 get_arrows_border (menu, &arrow_border);
4060 scroll_offset += arrow_border.top;
4063 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4066 if (!GTK_MENU_SHELL (menu)->active)
4068 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4069 requisition.width, requisition.height);
4072 menu->scroll_offset = scroll_offset;
4076 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4078 if (menu->timeout_id)
4080 g_source_remove (menu->timeout_id);
4081 menu->timeout_id = 0;
4086 gtk_menu_stop_scrolling (GtkMenu *menu)
4088 gboolean touchscreen_mode;
4090 gtk_menu_remove_scroll_timeout (menu);
4092 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4093 "gtk-touchscreen-mode", &touchscreen_mode,
4096 if (!touchscreen_mode)
4098 menu->upper_arrow_prelight = FALSE;
4099 menu->lower_arrow_prelight = FALSE;
4104 gtk_menu_scroll_to (GtkMenu *menu,
4109 gint view_width, view_height;
4112 guint vertical_padding;
4113 guint horizontal_padding;
4114 gboolean double_arrows;
4115 GtkBorder arrow_border;
4117 widget = GTK_WIDGET (menu);
4119 if (menu->tearoff_active &&
4120 menu->tearoff_adjustment &&
4121 (menu->tearoff_adjustment->value != offset))
4123 menu->tearoff_adjustment->value =
4125 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4126 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4129 /* Move/resize the viewport according to arrows: */
4130 view_width = widget->allocation.width;
4131 view_height = widget->allocation.height;
4133 gtk_widget_style_get (GTK_WIDGET (menu),
4134 "vertical-padding", &vertical_padding,
4135 "horizontal-padding", &horizontal_padding,
4138 double_arrows = get_double_arrows (menu);
4140 border_width = GTK_CONTAINER (menu)->border_width;
4141 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4142 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4143 menu_height = widget->requisition.height -
4144 (border_width + widget->style->ythickness + vertical_padding) * 2;
4146 x = border_width + widget->style->xthickness + horizontal_padding;
4147 y = border_width + widget->style->ythickness + vertical_padding;
4149 if (double_arrows && !menu->tearoff_active)
4151 if (view_height < menu_height ||
4152 (offset > 0 && menu->scroll_offset > 0) ||
4153 (offset < 0 && menu->scroll_offset < 0))
4155 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4156 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4157 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4159 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4160 gtk_widget_queue_draw (GTK_WIDGET (menu));
4162 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4164 get_arrows_border (menu, &arrow_border);
4165 y += arrow_border.top;
4166 view_height -= arrow_border.top;
4167 view_height -= arrow_border.bottom;
4170 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4171 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4172 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4173 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4175 if (offset >= menu_height - view_height)
4176 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4177 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4178 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4179 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4181 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4182 (priv->lower_arrow_state != lower_arrow_previous_state))
4183 gtk_widget_queue_draw (GTK_WIDGET (menu));
4185 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4186 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4188 /* At the upper border, possibly remove timeout */
4189 if (menu->scroll_step < 0)
4191 gtk_menu_stop_scrolling (menu);
4192 gtk_widget_queue_draw (GTK_WIDGET (menu));
4196 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4197 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4199 /* At the lower border, possibly remove timeout */
4200 if (menu->scroll_step > 0)
4202 gtk_menu_stop_scrolling (menu);
4203 gtk_widget_queue_draw (GTK_WIDGET (menu));
4207 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4211 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4212 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4214 gtk_menu_stop_scrolling (menu);
4215 gtk_widget_queue_draw (GTK_WIDGET (menu));
4218 else if (!menu->tearoff_active)
4220 gboolean last_visible;
4222 last_visible = menu->upper_arrow_visible;
4223 menu->upper_arrow_visible = offset > 0;
4225 /* upper_arrow_visible may have changed, so requery the border */
4226 get_arrows_border (menu, &arrow_border);
4227 view_height -= arrow_border.top;
4229 if ((last_visible != menu->upper_arrow_visible) &&
4230 !menu->upper_arrow_visible)
4232 menu->upper_arrow_prelight = FALSE;
4234 /* If we hid the upper arrow, possibly remove timeout */
4235 if (menu->scroll_step < 0)
4237 gtk_menu_stop_scrolling (menu);
4238 gtk_widget_queue_draw (GTK_WIDGET (menu));
4242 last_visible = menu->lower_arrow_visible;
4243 menu->lower_arrow_visible = offset < menu_height - view_height;
4245 /* lower_arrow_visible may have changed, so requery the border */
4246 get_arrows_border (menu, &arrow_border);
4247 view_height -= arrow_border.bottom;
4249 if ((last_visible != menu->lower_arrow_visible) &&
4250 !menu->lower_arrow_visible)
4252 menu->lower_arrow_prelight = FALSE;
4254 /* If we hid the lower arrow, possibly remove timeout */
4255 if (menu->scroll_step > 0)
4257 gtk_menu_stop_scrolling (menu);
4258 gtk_widget_queue_draw (GTK_WIDGET (menu));
4262 y += arrow_border.top;
4265 /* Scroll the menu: */
4266 if (GTK_WIDGET_REALIZED (menu))
4267 gdk_window_move (menu->bin_window, 0, -offset);
4269 if (GTK_WIDGET_REALIZED (menu))
4270 gdk_window_move_resize (menu->view_window,
4276 menu->scroll_offset = offset;
4280 compute_child_offset (GtkMenu *menu,
4281 GtkWidget *menu_item,
4284 gboolean *is_last_child)
4286 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4287 gint item_top_attach;
4288 gint item_bottom_attach;
4289 gint child_offset = 0;
4292 get_effective_child_attach (menu_item, NULL, NULL,
4293 &item_top_attach, &item_bottom_attach);
4295 /* there is a possibility that we get called before _size_request, so
4296 * check the height table for safety.
4298 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4301 /* when we have a row with only invisible children, it's height will
4302 * be zero, so there's no need to check WIDGET_VISIBLE here
4304 for (i = 0; i < item_top_attach; i++)
4305 child_offset += priv->heights[i];
4308 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4310 *offset = child_offset;
4312 *height = priv->heights[item_top_attach];
4318 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4319 GtkWidget *menu_item)
4322 gint child_offset, child_height;
4326 gboolean last_child = 0;
4328 menu = GTK_MENU (menu_shell);
4330 /* We need to check if the selected item fully visible.
4331 * If not we need to scroll the menu so that it becomes fully
4335 if (compute_child_offset (menu, menu_item,
4336 &child_offset, &child_height, &last_child))
4338 guint vertical_padding;
4339 gboolean double_arrows;
4341 y = menu->scroll_offset;
4342 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4344 gtk_widget_style_get (GTK_WIDGET (menu),
4345 "vertical-padding", &vertical_padding,
4348 double_arrows = get_double_arrows (menu);
4350 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4352 if (child_offset < y)
4354 /* Ignore the enter event we might get if the pointer is on the menu
4356 menu_shell->ignore_enter = TRUE;
4357 gtk_menu_scroll_to (menu, child_offset);
4361 GtkBorder arrow_border;
4365 get_arrows_border (menu, &arrow_border);
4366 if (!menu->tearoff_active)
4367 arrow_height = arrow_border.top + arrow_border.bottom;
4369 if (child_offset + child_height > y + height - arrow_height)
4372 if ((!last_child && !menu->tearoff_active) || double_arrows)
4373 arrow_height += arrow_border.bottom;
4375 y = child_offset + child_height - height + arrow_height;
4376 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4378 /* Need upper arrow */
4379 arrow_height += arrow_border.top;
4380 y = child_offset + child_height - height + arrow_height;
4382 /* Ignore the enter event we might get if the pointer is on the menu
4384 menu_shell->ignore_enter = TRUE;
4385 gtk_menu_scroll_to (menu, y);
4393 gtk_menu_select_item (GtkMenuShell *menu_shell,
4394 GtkWidget *menu_item)
4396 GtkMenu *menu = GTK_MENU (menu_shell);
4398 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4399 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4401 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4405 /* Reparent the menu, taking care of the refcounting
4407 * If unrealize is true we force a unrealize while reparenting the parent.
4408 * This can help eliminate flicker in some cases.
4410 * What happens is that when the menu is unrealized and then re-realized,
4411 * the allocations are as follows:
4413 * parent - 1x1 at (0,0)
4414 * child1 - 100x20 at (0,0)
4415 * child2 - 100x20 at (0,20)
4416 * child3 - 100x20 at (0,40)
4418 * That is, the parent is small but the children are full sized. Then,
4419 * when the queued_resize gets processed, the parent gets resized to
4422 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4423 * contains the following logic:
4425 * - if a move or resize operation on a window would change the clip
4426 * region on the children, then before the window is resized
4427 * the background for children is temporarily set to None, the
4428 * move/resize done, and the background for the children restored.
4430 * So, at the point where the parent is resized to final size, the
4431 * background for the children is temporarily None, and thus they
4432 * are not cleared to the background color and the previous background
4433 * (the image of the menu) is left in place.
4436 gtk_menu_reparent (GtkMenu *menu,
4437 GtkWidget *new_parent,
4440 GtkObject *object = GTK_OBJECT (menu);
4441 GtkWidget *widget = GTK_WIDGET (menu);
4442 gboolean was_floating = g_object_is_floating (object);
4444 g_object_ref_sink (object);
4448 g_object_ref (object);
4449 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4450 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4451 g_object_unref (object);
4454 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4457 g_object_force_floating (G_OBJECT (object));
4459 g_object_unref (object);
4463 gtk_menu_show_all (GtkWidget *widget)
4465 /* Show children, but not self. */
4466 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4471 gtk_menu_hide_all (GtkWidget *widget)
4473 /* Hide children, but not self. */
4474 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4478 * gtk_menu_set_screen:
4479 * @menu: a #GtkMenu.
4480 * @screen: a #GdkScreen, or %NULL if the screen should be
4481 * determined by the widget the menu is attached to.
4483 * Sets the #GdkScreen on which the menu will be displayed.
4488 gtk_menu_set_screen (GtkMenu *menu,
4491 g_return_if_fail (GTK_IS_MENU (menu));
4492 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4494 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4498 menu_change_screen (menu, screen);
4502 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4504 attach_widget_screen_changed (attach_widget, NULL, menu);
4510 * @menu: a #GtkMenu.
4511 * @child: a #GtkMenuItem.
4512 * @left_attach: The column number to attach the left side of the item to.
4513 * @right_attach: The column number to attach the right side of the item to.
4514 * @top_attach: The row number to attach the top of the item to.
4515 * @bottom_attach: The row number to attach the bottom of the item to.
4517 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4518 * an item will occupy is specified by @left_attach, @right_attach,
4519 * @top_attach and @bottom_attach. These each represent the leftmost,
4520 * rightmost, uppermost and lower column and row numbers of the table.
4521 * (Columns and rows are indexed from zero).
4523 * Note that this function is not related to gtk_menu_detach().
4528 gtk_menu_attach (GtkMenu *menu,
4533 guint bottom_attach)
4535 GtkMenuShell *menu_shell;
4537 g_return_if_fail (GTK_IS_MENU (menu));
4538 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4539 g_return_if_fail (child->parent == NULL ||
4540 child->parent == GTK_WIDGET (menu));
4541 g_return_if_fail (left_attach < right_attach);
4542 g_return_if_fail (top_attach < bottom_attach);
4544 menu_shell = GTK_MENU_SHELL (menu);
4548 AttachInfo *ai = get_attach_info (child);
4550 ai->left_attach = left_attach;
4551 ai->right_attach = right_attach;
4552 ai->top_attach = top_attach;
4553 ai->bottom_attach = bottom_attach;
4555 menu_shell->children = g_list_append (menu_shell->children, child);
4557 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4559 menu_queue_resize (menu);
4563 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4564 "left-attach", left_attach,
4565 "right-attach", right_attach,
4566 "top-attach", top_attach,
4567 "bottom-attach", bottom_attach,
4573 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4577 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4578 "gtk-menu-popup-delay", &popup_delay,
4585 find_child_containing (GtkMenuShell *menu_shell,
4593 /* find a child which includes the area given by
4594 * left, right, top, bottom.
4597 for (list = menu_shell->children; list; list = list->next)
4601 if (!_gtk_menu_item_is_selectable (list->data))
4604 get_effective_child_attach (list->data, &l, &r, &t, &b);
4606 if (l <= left && right <= r
4607 && t <= top && bottom <= b)
4608 return GTK_WIDGET (list->data);
4615 gtk_menu_move_current (GtkMenuShell *menu_shell,
4616 GtkMenuDirectionType direction)
4618 GtkMenu *menu = GTK_MENU (menu_shell);
4621 GtkWidget *match = NULL;
4623 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4627 case GTK_MENU_DIR_CHILD:
4628 direction = GTK_MENU_DIR_PARENT;
4630 case GTK_MENU_DIR_PARENT:
4631 direction = GTK_MENU_DIR_CHILD;
4637 /* use special table menu key bindings */
4638 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4640 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4642 if (direction == GTK_MENU_DIR_NEXT)
4644 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4646 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4654 for (i = 0; i < t; i++)
4656 match = find_child_containing (menu_shell,
4657 l, l + 1, i, i + 1);
4663 else if (direction == GTK_MENU_DIR_PREV)
4665 for (i = t; i > 0; i--)
4667 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4675 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4677 match = find_child_containing (menu_shell,
4678 l, l + 1, i - 1, i);
4684 else if (direction == GTK_MENU_DIR_PARENT)
4686 /* we go one left if possible */
4688 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4692 GtkWidget *parent = menu_shell->parent_menu_shell;
4695 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4696 match = menu_shell->active_menu_item;
4699 else if (direction == GTK_MENU_DIR_CHILD)
4701 /* we go one right if possible */
4702 if (r < gtk_menu_get_n_columns (menu))
4703 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4707 GtkWidget *parent = menu_shell->parent_menu_shell;
4709 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4711 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4712 match = menu_shell->active_menu_item;
4718 gtk_menu_shell_select_item (menu_shell, match);
4723 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
4727 get_visible_size (GtkMenu *menu)
4729 GtkWidget *widget = GTK_WIDGET (menu);
4730 GtkContainer *container = GTK_CONTAINER (menu);
4732 gint menu_height = (widget->allocation.height
4733 - 2 * (container->border_width
4734 + widget->style->ythickness));
4736 if (!menu->tearoff_active)
4738 GtkBorder arrow_border;
4740 get_arrows_border (menu, &arrow_border);
4741 menu_height -= arrow_border.top;
4742 menu_height -= arrow_border.bottom;
4748 /* Find the sensitive on-screen child containing @y, or if none,
4749 * the nearest selectable onscreen child. (%NULL if none)
4752 child_at (GtkMenu *menu,
4755 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4756 GtkWidget *child = NULL;
4757 gint child_offset = 0;
4760 gint lower, upper; /* Onscreen bounds */
4762 menu_height = get_visible_size (menu);
4763 lower = menu->scroll_offset;
4764 upper = menu->scroll_offset + menu_height;
4766 for (children = menu_shell->children; children; children = children->next)
4768 if (GTK_WIDGET_VISIBLE (children->data))
4770 GtkRequisition child_requisition;
4772 gtk_widget_size_request (children->data, &child_requisition);
4774 if (_gtk_menu_item_is_selectable (children->data) &&
4775 child_offset >= lower &&
4776 child_offset + child_requisition.height <= upper)
4778 child = children->data;
4780 if (child_offset + child_requisition.height > y &&
4781 !GTK_IS_TEAROFF_MENU_ITEM (child))
4785 child_offset += child_requisition.height;
4793 get_menu_height (GtkMenu *menu)
4796 GtkWidget *widget = GTK_WIDGET (menu);
4798 height = widget->requisition.height;
4799 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4801 if (!menu->tearoff_active)
4803 GtkBorder arrow_border;
4805 get_arrows_border (menu, &arrow_border);
4806 height -= arrow_border.top;
4807 height -= arrow_border.bottom;
4814 gtk_menu_real_move_scroll (GtkMenu *menu,
4817 gint page_size = get_visible_size (menu);
4818 gint end_position = get_menu_height (menu);
4819 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4823 case GTK_SCROLL_PAGE_UP:
4824 case GTK_SCROLL_PAGE_DOWN:
4828 gint child_offset = 0;
4829 gboolean old_upper_arrow_visible;
4832 if (type == GTK_SCROLL_PAGE_UP)
4837 if (menu_shell->active_menu_item)
4841 compute_child_offset (menu, menu_shell->active_menu_item,
4842 &child_offset, &child_height, NULL);
4843 child_offset += child_height / 2;
4846 menu_shell->ignore_enter = TRUE;
4847 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4848 old_offset = menu->scroll_offset;
4850 new_offset = menu->scroll_offset + step;
4851 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4853 gtk_menu_scroll_to (menu, new_offset);
4855 if (menu_shell->active_menu_item)
4857 GtkWidget *new_child;
4858 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4859 GtkBorder arrow_border;
4861 get_arrows_border (menu, &arrow_border);
4863 if (menu->scroll_offset != old_offset)
4864 step = menu->scroll_offset - old_offset;
4866 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
4868 new_child = child_at (menu, child_offset + step);
4870 gtk_menu_shell_select_item (menu_shell, new_child);
4874 case GTK_SCROLL_START:
4875 /* Ignore the enter event we might get if the pointer is on the menu
4877 menu_shell->ignore_enter = TRUE;
4878 gtk_menu_scroll_to (menu, 0);
4879 gtk_menu_shell_select_first (menu_shell, TRUE);
4881 case GTK_SCROLL_END:
4882 /* Ignore the enter event we might get if the pointer is on the menu
4884 menu_shell->ignore_enter = TRUE;
4885 gtk_menu_scroll_to (menu, end_position - page_size);
4886 _gtk_menu_shell_select_last (menu_shell, TRUE);
4895 * gtk_menu_set_monitor:
4897 * @monitor_num: the number of the monitor on which the menu should
4900 * Informs GTK+ on which monitor a menu should be popped up.
4901 * See gdk_screen_get_monitor_geometry().
4903 * This function should be called from a #GtkMenuPositionFunc if the
4904 * menu should not appear on the same monitor as the pointer. This
4905 * information can't be reliably inferred from the coordinates returned
4906 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4907 * may extend beyond the monitor boundaries or even the screen boundaries.
4912 gtk_menu_set_monitor (GtkMenu *menu,
4915 GtkMenuPrivate *priv;
4916 g_return_if_fail (GTK_IS_MENU (menu));
4918 priv = gtk_menu_get_private (menu);
4920 priv->monitor_num = monitor_num;
4924 * gtk_menu_get_for_attach_widget:
4925 * @widget: a #GtkWidget
4927 * Returns a list of the menus which are attached to this widget.
4928 * This list is owned by GTK+ and must not be modified.
4930 * Return value: the list of menus attached to his widget.
4935 gtk_menu_get_for_attach_widget (GtkWidget *widget)
4939 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4941 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
4946 gtk_menu_grab_notify (GtkWidget *widget,
4947 gboolean was_grabbed)
4949 GtkWidget *toplevel;
4950 GtkWindowGroup *group;
4953 toplevel = gtk_widget_get_toplevel (widget);
4954 group = gtk_window_get_group (GTK_WINDOW (toplevel));
4955 grab = _gtk_window_group_get_current_grab (group);
4959 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
4960 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
4964 #define __GTK_MENU_C__
4965 #include "gtkaliasdef.c"