]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenu.c
22347899377fa1754f1f71cfee189bb2b29af31a
[~andy/gtk] / gtk / gtkmenu.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 /**
28  * SECTION:gtkmenu
29  * @Short_description: A menu widget
30  * @Title: GtkMenu
31  *
32  * A #GtkMenu is a #GtkMenuShell that implements a drop down menu
33  * consisting of a list of #GtkMenuItem objects which can be navigated
34  * and activated by the user to perform application functions.
35  *
36  * A #GtkMenu is most commonly dropped down by activating a
37  * #GtkMenuItem in a #GtkMenuBar or popped up by activating a
38  * #GtkMenuItem in another #GtkMenu.
39  *
40  * A #GtkMenu can also be popped up by activating a #GtkOptionMenu.
41  * Other composite widgets such as the #GtkNotebook can pop up a
42  * #GtkMenu as well.
43  *
44  * Applications can display a #GtkMenu as a popup menu by calling the 
45  * gtk_menu_popup() function.  The example below shows how an application
46  * can pop up a menu when the 3rd mouse button is pressed.  
47  *
48  * <example>
49  * <title>Connecting the popup signal handler.</title>
50  * <programlisting>
51  *   /<!---->* connect our handler which will popup the menu *<!---->/
52  *   g_signal_connect_swapped (window, "button_press_event",
53  *      G_CALLBACK (my_popup_handler), menu);
54  * </programlisting>
55  * </example>
56  *
57  * <example>
58  * <title>Signal handler which displays a popup menu.</title>
59  * <programlisting>
60  * static gint
61  * my_popup_handler (GtkWidget *widget, GdkEvent *event)
62  * {
63  *   GtkMenu *menu;
64  *   GdkEventButton *event_button;
65  *
66  *   g_return_val_if_fail (widget != NULL, FALSE);
67  *   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
68  *   g_return_val_if_fail (event != NULL, FALSE);
69  *
70  *   /<!---->* The "widget" is the menu that was supplied when 
71  *    * g_signal_connect_swapped() was called.
72  *    *<!---->/
73  *   menu = GTK_MENU (widget);
74  *
75  *   if (event->type == GDK_BUTTON_PRESS)
76  *     {
77  *       event_button = (GdkEventButton *) event;
78  *       if (event_button->button == GDK_BUTTON_SECONDARY)
79  *         {
80  *           gtk_menu_popup (menu, NULL, NULL, NULL, NULL, 
81  *                           event_button->button, event_button->time);
82  *           return TRUE;
83  *         }
84  *     }
85  * 
86  *   return FALSE;
87  * }
88  * </programlisting>
89  * </example>
90  */
91
92 #include "config.h"
93
94 #include <string.h>
95
96 #include  <gobject/gvaluecollector.h>
97
98 #include "gtkaccellabel.h"
99 #include "gtkaccelmap.h"
100 #include "gtkbindings.h"
101 #include "gtkcheckmenuitem.h"
102 #include "gtkmain.h"
103 #include "gtkmarshalers.h"
104 #include "gtkmenuprivate.h"
105 #include "gtkmenuitemprivate.h"
106 #include "gtkmenushellprivate.h"
107 #include "gtkwindow.h"
108 #include "gtkbox.h"
109 #include "gtkscrollbar.h"
110 #include "gtksettings.h"
111 #include "gtkprivate.h"
112 #include "gtkwidgetprivate.h"
113 #include "gtkintl.h"
114 #include "gtktypebuiltins.h"
115
116 #include "deprecated/gtktearoffmenuitem.h"
117
118
119 #include "a11y/gtkmenuaccessible.h"
120
121 #define NAVIGATION_REGION_OVERSHOOT 50  /* How much the navigation region
122                                          * extends below the submenu
123                                          */
124
125 #define MENU_SCROLL_STEP1      8
126 #define MENU_SCROLL_STEP2     15
127 #define MENU_SCROLL_FAST_ZONE  8
128 #define MENU_SCROLL_TIMEOUT1  50
129 #define MENU_SCROLL_TIMEOUT2  20
130
131 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
132 #define ATTACHED_MENUS "gtk-attached-menus"
133
134 typedef struct _GtkMenuAttachData  GtkMenuAttachData;
135 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
136
137 struct _GtkMenuAttachData
138 {
139   GtkWidget *attach_widget;
140   GtkMenuDetachFunc detacher;
141 };
142
143 struct _GtkMenuPopdownData
144 {
145   GtkMenu *menu;
146   GdkDevice *device;
147 };
148
149 typedef struct
150 {
151   gint left_attach;
152   gint right_attach;
153   gint top_attach;
154   gint bottom_attach;
155   gint effective_left_attach;
156   gint effective_right_attach;
157   gint effective_top_attach;
158   gint effective_bottom_attach;
159 } AttachInfo;
160
161 enum {
162   MOVE_SCROLL,
163   LAST_SIGNAL
164 };
165
166 enum {
167   PROP_0,
168   PROP_ACTIVE,
169   PROP_ACCEL_GROUP,
170   PROP_ACCEL_PATH,
171   PROP_ATTACH_WIDGET,
172   PROP_TEAROFF_STATE,
173   PROP_TEAROFF_TITLE,
174   PROP_MONITOR,
175   PROP_RESERVE_TOGGLE_SIZE
176 };
177
178 enum {
179   CHILD_PROP_0,
180   CHILD_PROP_LEFT_ATTACH,
181   CHILD_PROP_RIGHT_ATTACH,
182   CHILD_PROP_TOP_ATTACH,
183   CHILD_PROP_BOTTOM_ATTACH
184 };
185
186 static void     gtk_menu_set_property      (GObject          *object,
187                                             guint             prop_id,
188                                             const GValue     *value,
189                                             GParamSpec       *pspec);
190 static void     gtk_menu_get_property      (GObject          *object,
191                                             guint             prop_id,
192                                             GValue           *value,
193                                             GParamSpec       *pspec);
194 static void     gtk_menu_set_child_property(GtkContainer     *container,
195                                             GtkWidget        *child,
196                                             guint             property_id,
197                                             const GValue     *value,
198                                             GParamSpec       *pspec);
199 static void     gtk_menu_get_child_property(GtkContainer     *container,
200                                             GtkWidget        *child,
201                                             guint             property_id,
202                                             GValue           *value,
203                                             GParamSpec       *pspec);
204 static void     gtk_menu_destroy           (GtkWidget        *widget);
205 static void     gtk_menu_realize           (GtkWidget        *widget);
206 static void     gtk_menu_unrealize         (GtkWidget        *widget);
207 static void     gtk_menu_size_allocate     (GtkWidget        *widget,
208                                             GtkAllocation    *allocation);
209 static void     gtk_menu_show              (GtkWidget        *widget);
210 static gboolean gtk_menu_draw              (GtkWidget        *widget,
211                                             cairo_t          *cr);
212 static gboolean gtk_menu_key_press         (GtkWidget        *widget,
213                                             GdkEventKey      *event);
214 static gboolean gtk_menu_scroll            (GtkWidget        *widget,
215                                             GdkEventScroll   *event);
216 static gboolean gtk_menu_button_press      (GtkWidget        *widget,
217                                             GdkEventButton   *event);
218 static gboolean gtk_menu_button_release    (GtkWidget        *widget,
219                                             GdkEventButton   *event);
220 static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
221                                             GdkEventMotion   *event);
222 static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
223                                             GdkEventCrossing *event);
224 static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
225                                             GdkEventCrossing *event);
226 static void     gtk_menu_scroll_to         (GtkMenu          *menu,
227                                             gint              offset);
228 static void     gtk_menu_grab_notify       (GtkWidget        *widget,
229                                             gboolean          was_grabbed);
230
231 static void     gtk_menu_stop_scrolling         (GtkMenu  *menu);
232 static void     gtk_menu_remove_scroll_timeout  (GtkMenu  *menu);
233 static gboolean gtk_menu_scroll_timeout         (gpointer  data);
234 static gboolean gtk_menu_scroll_timeout_initial (gpointer  data);
235 static void     gtk_menu_start_scrolling        (GtkMenu  *menu);
236
237 static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
238                                               GtkWidget       *menu_item);
239 static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
240                                             GtkWidget        *menu_item);
241 static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
242                                             GtkWidget        *child,
243                                             gint              position);
244 static void     gtk_menu_scrollbar_changed (GtkAdjustment    *adjustment,
245                                             GtkMenu          *menu);
246 static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
247                                             gint              event_x,
248                                             gint              event_y,
249                                             gboolean          enter,
250                                             gboolean          motion);
251 static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
252                                             gint             width);
253 static void     gtk_menu_style_updated     (GtkWidget        *widget);
254 static gboolean gtk_menu_focus             (GtkWidget        *widget,
255                                             GtkDirectionType direction);
256 static gint     gtk_menu_get_popup_delay   (GtkMenuShell     *menu_shell);
257 static void     gtk_menu_move_current      (GtkMenuShell     *menu_shell,
258                                             GtkMenuDirectionType direction);
259 static void     gtk_menu_real_move_scroll  (GtkMenu          *menu,
260                                             GtkScrollType     type);
261
262 static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
263 static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
264 static gboolean gtk_menu_navigating_submenu            (GtkMenu          *menu,
265                                                         gint              event_x,
266                                                         gint              event_y);
267 static void     gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
268                                                         GtkMenuItem      *menu_item,
269                                                         GdkEventCrossing *event);
270  
271 static void gtk_menu_deactivate     (GtkMenuShell      *menu_shell);
272 static void gtk_menu_show_all       (GtkWidget         *widget);
273 static void gtk_menu_position       (GtkMenu           *menu,
274                                      gboolean           set_scroll_offset);
275 static void gtk_menu_reparent       (GtkMenu           *menu,
276                                      GtkWidget         *new_parent,
277                                      gboolean           unrealize);
278 static void gtk_menu_remove         (GtkContainer      *menu,
279                                      GtkWidget         *widget);
280
281 static void gtk_menu_update_title   (GtkMenu           *menu);
282
283 static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
284 static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);
285
286 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
287                                                   guint      signal_id);
288 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
289                                            gboolean group_changed);
290
291 static void gtk_menu_get_preferred_width            (GtkWidget           *widget,
292                                                      gint                *minimum_size,
293                                                      gint                *natural_size);
294 static void gtk_menu_get_preferred_height           (GtkWidget           *widget,
295                                                      gint                *minimum_size,
296                                                      gint                *natural_size);
297 static void gtk_menu_get_preferred_height_for_width (GtkWidget           *widget,
298                                                      gint                 for_size,
299                                                      gint                *minimum_size,
300                                                      gint                *natural_size);
301
302
303 static const gchar attach_data_key[] = "gtk-menu-attach-data";
304
305 static guint menu_signals[LAST_SIGNAL] = { 0 };
306
307 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
308
309 static void
310 menu_queue_resize (GtkMenu *menu)
311 {
312   GtkMenuPrivate *priv = menu->priv;
313
314   priv->have_layout = FALSE;
315   gtk_widget_queue_resize (GTK_WIDGET (menu));
316 }
317
318 static void
319 attach_info_free (AttachInfo *info)
320 {
321   g_slice_free (AttachInfo, info);
322 }
323
324 static AttachInfo *
325 get_attach_info (GtkWidget *child)
326 {
327   GObject *object = G_OBJECT (child);
328   AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
329
330   if (!ai)
331     {
332       ai = g_slice_new0 (AttachInfo);
333       g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
334                               (GDestroyNotify) attach_info_free);
335     }
336
337   return ai;
338 }
339
340 static gboolean
341 is_grid_attached (AttachInfo *ai)
342 {
343   return (ai->left_attach >= 0 &&
344           ai->right_attach >= 0 &&
345           ai->top_attach >= 0 &&
346           ai->bottom_attach >= 0);
347 }
348
349 static void
350 menu_ensure_layout (GtkMenu *menu)
351 {
352   GtkMenuPrivate *priv = menu->priv;
353
354   if (!priv->have_layout)
355     {
356       GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
357       GList *l;
358       gchar *row_occupied;
359       gint current_row;
360       gint max_right_attach;
361       gint max_bottom_attach;
362
363       /* Find extents of gridded portion
364        */
365       max_right_attach = 1;
366       max_bottom_attach = 0;
367
368       for (l = menu_shell->priv->children; l; l = l->next)
369         {
370           GtkWidget *child = l->data;
371           AttachInfo *ai = get_attach_info (child);
372
373           if (is_grid_attached (ai))
374             {
375               max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
376               max_right_attach = MAX (max_right_attach, ai->right_attach);
377             }
378         }
379
380       /* Find empty rows */
381       row_occupied = g_malloc0 (max_bottom_attach);
382
383       for (l = menu_shell->priv->children; l; l = l->next)
384         {
385           GtkWidget *child = l->data;
386           AttachInfo *ai = get_attach_info (child);
387
388           if (is_grid_attached (ai))
389             {
390               gint i;
391
392               for (i = ai->top_attach; i < ai->bottom_attach; i++)
393                 row_occupied[i] = TRUE;
394             }
395         }
396
397       /* Lay non-grid-items out in those rows
398        */
399       current_row = 0;
400       for (l = menu_shell->priv->children; l; l = l->next)
401         {
402           GtkWidget *child = l->data;
403           AttachInfo *ai = get_attach_info (child);
404
405           if (!is_grid_attached (ai))
406             {
407               while (current_row < max_bottom_attach && row_occupied[current_row])
408                 current_row++;
409
410               ai->effective_left_attach = 0;
411               ai->effective_right_attach = max_right_attach;
412               ai->effective_top_attach = current_row;
413               ai->effective_bottom_attach = current_row + 1;
414
415               current_row++;
416             }
417           else
418             {
419               ai->effective_left_attach = ai->left_attach;
420               ai->effective_right_attach = ai->right_attach;
421               ai->effective_top_attach = ai->top_attach;
422               ai->effective_bottom_attach = ai->bottom_attach;
423             }
424         }
425
426       g_free (row_occupied);
427
428       priv->n_rows = MAX (current_row, max_bottom_attach);
429       priv->n_columns = max_right_attach;
430       priv->have_layout = TRUE;
431     }
432 }
433
434
435 static gint
436 gtk_menu_get_n_columns (GtkMenu *menu)
437 {
438   GtkMenuPrivate *priv = menu->priv;
439
440   menu_ensure_layout (menu);
441
442   return priv->n_columns;
443 }
444
445 static gint
446 gtk_menu_get_n_rows (GtkMenu *menu)
447 {
448   GtkMenuPrivate *priv = menu->priv;
449
450   menu_ensure_layout (menu);
451
452   return priv->n_rows;
453 }
454
455 static void
456 get_effective_child_attach (GtkWidget *child,
457                             int       *l,
458                             int       *r,
459                             int       *t,
460                             int       *b)
461 {
462   GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
463   AttachInfo *ai;
464   
465   menu_ensure_layout (menu);
466
467   ai = get_attach_info (child);
468
469   if (l)
470     *l = ai->effective_left_attach;
471   if (r)
472     *r = ai->effective_right_attach;
473   if (t)
474     *t = ai->effective_top_attach;
475   if (b)
476     *b = ai->effective_bottom_attach;
477
478 }
479
480 static void
481 gtk_menu_class_init (GtkMenuClass *class)
482 {
483   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
484   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
485   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
486   GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
487   GtkBindingSet *binding_set;
488   
489   gobject_class->set_property = gtk_menu_set_property;
490   gobject_class->get_property = gtk_menu_get_property;
491
492   widget_class->destroy = gtk_menu_destroy;
493   widget_class->realize = gtk_menu_realize;
494   widget_class->unrealize = gtk_menu_unrealize;
495   widget_class->size_allocate = gtk_menu_size_allocate;
496   widget_class->show = gtk_menu_show;
497   widget_class->draw = gtk_menu_draw;
498   widget_class->scroll_event = gtk_menu_scroll;
499   widget_class->key_press_event = gtk_menu_key_press;
500   widget_class->button_press_event = gtk_menu_button_press;
501   widget_class->button_release_event = gtk_menu_button_release;
502   widget_class->motion_notify_event = gtk_menu_motion_notify;
503   widget_class->show_all = gtk_menu_show_all;
504   widget_class->enter_notify_event = gtk_menu_enter_notify;
505   widget_class->leave_notify_event = gtk_menu_leave_notify;
506   widget_class->style_updated = gtk_menu_style_updated;
507   widget_class->focus = gtk_menu_focus;
508   widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
509   widget_class->grab_notify = gtk_menu_grab_notify;
510   widget_class->get_preferred_width = gtk_menu_get_preferred_width;
511   widget_class->get_preferred_height = gtk_menu_get_preferred_height;
512   widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
513
514   container_class->remove = gtk_menu_remove;
515   container_class->get_child_property = gtk_menu_get_child_property;
516   container_class->set_child_property = gtk_menu_set_child_property;
517   
518   menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
519   menu_shell_class->deactivate = gtk_menu_deactivate;
520   menu_shell_class->select_item = gtk_menu_select_item;
521   menu_shell_class->insert = gtk_menu_real_insert;
522   menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
523   menu_shell_class->move_current = gtk_menu_move_current;
524
525   /**
526    * GtkMenu::move-scroll:
527    * @menu: a #GtkMenu
528    * @scroll_type: a #GtkScrollType
529    */
530   menu_signals[MOVE_SCROLL] =
531     g_signal_new_class_handler (I_("move-scroll"),
532                                 G_OBJECT_CLASS_TYPE (gobject_class),
533                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
534                                 G_CALLBACK (gtk_menu_real_move_scroll),
535                                 NULL, NULL,
536                                 _gtk_marshal_VOID__ENUM,
537                                 G_TYPE_NONE, 1,
538                                 GTK_TYPE_SCROLL_TYPE);
539
540   /**
541    * GtkMenu:active:
542    *
543    * The index of the currently selected menu item, or -1 if no
544    * menu item is selected.
545    *
546    * Since: 2.14
547    **/
548   g_object_class_install_property (gobject_class,
549                                    PROP_ACTIVE,
550                                    g_param_spec_int ("active",
551                                                      P_("Active"),
552                                                      P_("The currently selected menu item"),
553                                                      -1, G_MAXINT, -1,
554                                                      GTK_PARAM_READWRITE));
555
556   /**
557    * GtkMenu:accel-group:
558    *
559    * The accel group holding accelerators for the menu.
560    *
561    * Since: 2.14
562    **/
563   g_object_class_install_property (gobject_class,
564                                    PROP_ACCEL_GROUP,
565                                    g_param_spec_object ("accel-group",
566                                                         P_("Accel Group"),
567                                                         P_("The accel group holding accelerators for the menu"),
568                                                         GTK_TYPE_ACCEL_GROUP,
569                                                         GTK_PARAM_READWRITE));
570
571   /**
572    * GtkMenu:accel-path:
573    *
574    * An accel path used to conveniently construct accel paths of child items.
575    *
576    * Since: 2.14
577    **/
578   g_object_class_install_property (gobject_class,
579                                    PROP_ACCEL_PATH,
580                                    g_param_spec_string ("accel-path",
581                                                         P_("Accel Path"),
582                                                         P_("An accel path used to conveniently construct accel paths of child items"),
583                                                         NULL,
584                                                         GTK_PARAM_READWRITE));
585
586   /**
587    * GtkMenu:attach-widget:
588    *
589    * The widget the menu is attached to. Setting this property attaches
590    * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
591    * use gtk_menu_attach_to_widget() directly.
592    *
593    * Since: 2.14
594    **/
595   g_object_class_install_property (gobject_class,
596                                    PROP_ATTACH_WIDGET,
597                                    g_param_spec_object ("attach-widget",
598                                                         P_("Attach Widget"),
599                                                         P_("The widget the menu is attached to"),
600                                                         GTK_TYPE_WIDGET,
601                                                         GTK_PARAM_READWRITE));
602
603   g_object_class_install_property (gobject_class,
604                                    PROP_TEAROFF_TITLE,
605                                    g_param_spec_string ("tearoff-title",
606                                                         P_("Tearoff Title"),
607                                                         P_("A title that may be displayed by the window manager when this menu is torn-off"),
608                                                         NULL,
609                                                         GTK_PARAM_READWRITE));
610
611   /**
612    * GtkMenu:tearoff-state:
613    *
614    * A boolean that indicates whether the menu is torn-off.
615    *
616    * Since: 2.6
617    **/
618   g_object_class_install_property (gobject_class,
619                                    PROP_TEAROFF_STATE,
620                                    g_param_spec_boolean ("tearoff-state",
621                                                          P_("Tearoff State"),
622                                                          P_("A boolean that indicates whether the menu is torn-off"),
623                                                          FALSE,
624                                                          GTK_PARAM_READWRITE));
625
626   /**
627    * GtkMenu:monitor:
628    *
629    * The monitor the menu will be popped up on.
630    *
631    * Since: 2.14
632    **/
633   g_object_class_install_property (gobject_class,
634                                    PROP_MONITOR,
635                                    g_param_spec_int ("monitor",
636                                                      P_("Monitor"),
637                                                      P_("The monitor the menu will be popped up on"),
638                                                      -1, G_MAXINT, -1,
639                                                      GTK_PARAM_READWRITE));
640
641   gtk_widget_class_install_style_property (widget_class,
642                                            g_param_spec_int ("vertical-padding",
643                                                              P_("Vertical Padding"),
644                                                              P_("Extra space at the top and bottom of the menu"),
645                                                              0,
646                                                              G_MAXINT,
647                                                              1,
648                                                              GTK_PARAM_READABLE));
649
650   /**
651    * GtkMenu:reserve-toggle-size:
652    *
653    * A boolean that indicates whether the menu reserves space for
654    * toggles and icons, regardless of their actual presence.
655    *
656    * This property should only be changed from its default value
657    * for special-purposes such as tabular menus. Regular menus that
658    * are connected to a menu bar or context menus should reserve
659    * toggle space for consistency.
660    *
661    * Since: 2.18
662    */
663   g_object_class_install_property (gobject_class,
664                                    PROP_RESERVE_TOGGLE_SIZE,
665                                    g_param_spec_boolean ("reserve-toggle-size",
666                                                          P_("Reserve Toggle Size"),
667                                                          P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
668                                                          TRUE,
669                                                          GTK_PARAM_READWRITE));
670
671   gtk_widget_class_install_style_property (widget_class,
672                                            g_param_spec_int ("horizontal-padding",
673                                                              P_("Horizontal Padding"),
674                                                              P_("Extra space at the left and right edges of the menu"),
675                                                              0,
676                                                              G_MAXINT,
677                                                              0,
678                                                              GTK_PARAM_READABLE));
679
680   gtk_widget_class_install_style_property (widget_class,
681                                            g_param_spec_int ("vertical-offset",
682                                                              P_("Vertical Offset"),
683                                                              P_("When the menu is a submenu, position it this number of pixels offset vertically"),
684                                                              G_MININT,
685                                                              G_MAXINT,
686                                                              0,
687                                                              GTK_PARAM_READABLE));
688
689   gtk_widget_class_install_style_property (widget_class,
690                                            g_param_spec_int ("horizontal-offset",
691                                                              P_("Horizontal Offset"),
692                                                              P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
693                                                              G_MININT,
694                                                              G_MAXINT,
695                                                              -2,
696                                                              GTK_PARAM_READABLE));
697
698   gtk_widget_class_install_style_property (widget_class,
699                                            g_param_spec_boolean ("double-arrows",
700                                                                  P_("Double Arrows"),
701                                                                  P_("When scrolling, always show both arrows."),
702                                                                  TRUE,
703                                                                  GTK_PARAM_READABLE));
704
705   /**
706    * GtkMenu:arrow-placement:
707    *
708    * Indicates where scroll arrows should be placed.
709    *
710    * Since: 2.16
711    **/
712   gtk_widget_class_install_style_property (widget_class,
713                                            g_param_spec_enum ("arrow-placement",
714                                                               P_("Arrow Placement"),
715                                                               P_("Indicates where scroll arrows should be placed"),
716                                                               GTK_TYPE_ARROW_PLACEMENT,
717                                                               GTK_ARROWS_BOTH,
718                                                               GTK_PARAM_READABLE));
719
720  gtk_container_class_install_child_property (container_class,
721                                              CHILD_PROP_LEFT_ATTACH,
722                                               g_param_spec_int ("left-attach",
723                                                                P_("Left Attach"),
724                                                                P_("The column number to attach the left side of the child to"),
725                                                                 -1, INT_MAX, -1,
726                                                                GTK_PARAM_READWRITE));
727
728  gtk_container_class_install_child_property (container_class,
729                                              CHILD_PROP_RIGHT_ATTACH,
730                                               g_param_spec_int ("right-attach",
731                                                                P_("Right Attach"),
732                                                                P_("The column number to attach the right side of the child to"),
733                                                                 -1, INT_MAX, -1,
734                                                                GTK_PARAM_READWRITE));
735
736  gtk_container_class_install_child_property (container_class,
737                                              CHILD_PROP_TOP_ATTACH,
738                                               g_param_spec_int ("top-attach",
739                                                                P_("Top Attach"),
740                                                                P_("The row number to attach the top of the child to"),
741                                                                 -1, INT_MAX, -1,
742                                                                GTK_PARAM_READWRITE));
743
744  gtk_container_class_install_child_property (container_class,
745                                              CHILD_PROP_BOTTOM_ATTACH,
746                                               g_param_spec_int ("bottom-attach",
747                                                                P_("Bottom Attach"),
748                                                                P_("The row number to attach the bottom of the child to"),
749                                                                 -1, INT_MAX, -1,
750                                                                GTK_PARAM_READWRITE));
751
752  /**
753   * GtkMenu:arrow-scaling
754   *
755   * Arbitrary constant to scale down the size of the scroll arrow.
756   *
757   * Since: 2.16
758   */
759   gtk_widget_class_install_style_property (widget_class,
760                                            g_param_spec_float ("arrow-scaling",
761                                                                P_("Arrow Scaling"),
762                                                                P_("Arbitrary constant to scale down the size of the scroll arrow"),
763                                                                0.0, 1.0, 0.7,
764                                                                GTK_PARAM_READABLE));
765
766   binding_set = gtk_binding_set_by_class (class);
767   gtk_binding_entry_add_signal (binding_set,
768                                 GDK_KEY_Up, 0,
769                                 I_("move-current"), 1,
770                                 GTK_TYPE_MENU_DIRECTION_TYPE,
771                                 GTK_MENU_DIR_PREV);
772   gtk_binding_entry_add_signal (binding_set,
773                                 GDK_KEY_KP_Up, 0,
774                                 "move-current", 1,
775                                 GTK_TYPE_MENU_DIRECTION_TYPE,
776                                 GTK_MENU_DIR_PREV);
777   gtk_binding_entry_add_signal (binding_set,
778                                 GDK_KEY_Down, 0,
779                                 "move-current", 1,
780                                 GTK_TYPE_MENU_DIRECTION_TYPE,
781                                 GTK_MENU_DIR_NEXT);
782   gtk_binding_entry_add_signal (binding_set,
783                                 GDK_KEY_KP_Down, 0,
784                                 "move-current", 1,
785                                 GTK_TYPE_MENU_DIRECTION_TYPE,
786                                 GTK_MENU_DIR_NEXT);
787   gtk_binding_entry_add_signal (binding_set,
788                                 GDK_KEY_Left, 0,
789                                 "move-current", 1,
790                                 GTK_TYPE_MENU_DIRECTION_TYPE,
791                                 GTK_MENU_DIR_PARENT);
792   gtk_binding_entry_add_signal (binding_set,
793                                 GDK_KEY_KP_Left, 0,
794                                 "move-current", 1,
795                                 GTK_TYPE_MENU_DIRECTION_TYPE,
796                                 GTK_MENU_DIR_PARENT);
797   gtk_binding_entry_add_signal (binding_set,
798                                 GDK_KEY_Right, 0,
799                                 "move-current", 1,
800                                 GTK_TYPE_MENU_DIRECTION_TYPE,
801                                 GTK_MENU_DIR_CHILD);
802   gtk_binding_entry_add_signal (binding_set,
803                                 GDK_KEY_KP_Right, 0,
804                                 "move-current", 1,
805                                 GTK_TYPE_MENU_DIRECTION_TYPE,
806                                 GTK_MENU_DIR_CHILD);
807   gtk_binding_entry_add_signal (binding_set,
808                                 GDK_KEY_Home, 0,
809                                 "move-scroll", 1,
810                                 GTK_TYPE_SCROLL_TYPE,
811                                 GTK_SCROLL_START);
812   gtk_binding_entry_add_signal (binding_set,
813                                 GDK_KEY_KP_Home, 0,
814                                 "move-scroll", 1,
815                                 GTK_TYPE_SCROLL_TYPE,
816                                 GTK_SCROLL_START);
817   gtk_binding_entry_add_signal (binding_set,
818                                 GDK_KEY_End, 0,
819                                 "move-scroll", 1,
820                                 GTK_TYPE_SCROLL_TYPE,
821                                 GTK_SCROLL_END);
822   gtk_binding_entry_add_signal (binding_set,
823                                 GDK_KEY_KP_End, 0,
824                                 "move-scroll", 1,
825                                 GTK_TYPE_SCROLL_TYPE,
826                                 GTK_SCROLL_END);
827   gtk_binding_entry_add_signal (binding_set,
828                                 GDK_KEY_Page_Up, 0,
829                                 "move-scroll", 1,
830                                 GTK_TYPE_SCROLL_TYPE,
831                                 GTK_SCROLL_PAGE_UP);
832   gtk_binding_entry_add_signal (binding_set,
833                                 GDK_KEY_KP_Page_Up, 0,
834                                 "move-scroll", 1,
835                                 GTK_TYPE_SCROLL_TYPE,
836                                 GTK_SCROLL_PAGE_UP);
837   gtk_binding_entry_add_signal (binding_set,
838                                 GDK_KEY_Page_Down, 0,
839                                 "move-scroll", 1,
840                                 GTK_TYPE_SCROLL_TYPE,
841                                 GTK_SCROLL_PAGE_DOWN);
842   gtk_binding_entry_add_signal (binding_set,
843                                 GDK_KEY_KP_Page_Down, 0,
844                                 "move-scroll", 1,
845                                 GTK_TYPE_SCROLL_TYPE,
846                                 GTK_SCROLL_PAGE_DOWN);
847
848   g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
849
850   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
851 }
852
853
854 static void
855 gtk_menu_set_property (GObject      *object,
856                        guint         prop_id,
857                        const GValue *value,
858                        GParamSpec   *pspec)
859 {
860   GtkMenu *menu = GTK_MENU (object);
861
862   switch (prop_id)
863     {
864     case PROP_ACTIVE:
865       gtk_menu_set_active (menu, g_value_get_int (value));
866       break;
867     case PROP_ACCEL_GROUP:
868       gtk_menu_set_accel_group (menu, g_value_get_object (value));
869       break;
870     case PROP_ACCEL_PATH:
871       gtk_menu_set_accel_path (menu, g_value_get_string (value));
872       break;
873     case PROP_ATTACH_WIDGET:
874       {
875         GtkWidget *widget;
876
877         widget = gtk_menu_get_attach_widget (menu);
878         if (widget)
879           gtk_menu_detach (menu);
880
881         widget = (GtkWidget*) g_value_get_object (value); 
882         if (widget)
883           gtk_menu_attach_to_widget (menu, widget, NULL);
884       }
885       break;
886     case PROP_TEAROFF_STATE:
887       gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
888       break;
889     case PROP_TEAROFF_TITLE:
890       gtk_menu_set_title (menu, g_value_get_string (value));
891       break;
892     case PROP_MONITOR:
893       gtk_menu_set_monitor (menu, g_value_get_int (value));
894       break;
895     case PROP_RESERVE_TOGGLE_SIZE:
896       gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
897       break;
898     default:
899       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
900       break;
901     }
902 }
903
904 static void
905 gtk_menu_get_property (GObject     *object,
906                        guint        prop_id,
907                        GValue      *value,
908                        GParamSpec  *pspec)
909 {
910   GtkMenu *menu = GTK_MENU (object);
911
912   switch (prop_id)
913     {
914     case PROP_ACTIVE:
915       g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
916       break;
917     case PROP_ACCEL_GROUP:
918       g_value_set_object (value, gtk_menu_get_accel_group (menu));
919       break;
920     case PROP_ACCEL_PATH:
921       g_value_set_string (value, gtk_menu_get_accel_path (menu));
922       break;
923     case PROP_ATTACH_WIDGET:
924       g_value_set_object (value, gtk_menu_get_attach_widget (menu));
925       break;
926     case PROP_TEAROFF_STATE:
927       g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
928       break;
929     case PROP_TEAROFF_TITLE:
930       g_value_set_string (value, gtk_menu_get_title (menu));
931       break;
932     case PROP_MONITOR:
933       g_value_set_int (value, gtk_menu_get_monitor (menu));
934       break;
935     case PROP_RESERVE_TOGGLE_SIZE:
936       g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
937       break;
938     default:
939       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
940       break;
941     }
942 }
943
944 static void
945 gtk_menu_set_child_property (GtkContainer *container,
946                              GtkWidget    *child,
947                              guint         property_id,
948                              const GValue *value,
949                              GParamSpec   *pspec)
950 {
951   GtkMenu *menu = GTK_MENU (container);
952   AttachInfo *ai = get_attach_info (child);
953
954   switch (property_id)
955     {
956     case CHILD_PROP_LEFT_ATTACH:
957       ai->left_attach = g_value_get_int (value);
958       break;
959     case CHILD_PROP_RIGHT_ATTACH:
960       ai->right_attach = g_value_get_int (value);
961       break;
962     case CHILD_PROP_TOP_ATTACH:
963       ai->top_attach = g_value_get_int (value);
964       break;
965     case CHILD_PROP_BOTTOM_ATTACH:
966       ai->bottom_attach = g_value_get_int (value);
967       break;
968
969     default:
970       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
971       return;
972     }
973
974   menu_queue_resize (menu);
975 }
976
977 static void
978 gtk_menu_get_child_property (GtkContainer *container,
979                              GtkWidget    *child,
980                              guint         property_id,
981                              GValue       *value,
982                              GParamSpec   *pspec)
983 {
984   AttachInfo *ai = get_attach_info (child);
985
986   switch (property_id)
987     {
988     case CHILD_PROP_LEFT_ATTACH:
989       g_value_set_int (value, ai->left_attach);
990       break;
991     case CHILD_PROP_RIGHT_ATTACH:
992       g_value_set_int (value, ai->right_attach);
993       break;
994     case CHILD_PROP_TOP_ATTACH:
995       g_value_set_int (value, ai->top_attach);
996       break;
997     case CHILD_PROP_BOTTOM_ATTACH:
998       g_value_set_int (value, ai->bottom_attach);
999       break;
1000       
1001     default:
1002       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1003       return;
1004     }
1005 }
1006
1007 static gboolean
1008 gtk_menu_window_event (GtkWidget *window,
1009                        GdkEvent  *event,
1010                        GtkWidget *menu)
1011 {
1012   gboolean handled = FALSE;
1013
1014   g_object_ref (window);
1015   g_object_ref (menu);
1016
1017   switch (event->type)
1018     {
1019     case GDK_KEY_PRESS:
1020     case GDK_KEY_RELEASE:
1021       handled = gtk_widget_event (menu, event);
1022       break;
1023     default:
1024       break;
1025     }
1026
1027   g_object_unref (window);
1028   g_object_unref (menu);
1029
1030   return handled;
1031 }
1032
1033 static void
1034 gtk_menu_init (GtkMenu *menu)
1035 {
1036   GtkMenuPrivate *priv;
1037   GtkStyleContext *context;
1038
1039   priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1040
1041   menu->priv = priv;
1042
1043   priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1044                                                    "type", GTK_WINDOW_POPUP,
1045                                                    "child", menu,
1046                                                    NULL),
1047                                      "signal::event", gtk_menu_window_event, menu,
1048                                      "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1049                                      NULL);
1050   gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1051   gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1052
1053   /* Refloat the menu, so that reference counting for the menu isn't
1054    * affected by it being a child of the toplevel
1055    */
1056   g_object_force_floating (G_OBJECT (menu));
1057   priv->needs_destruction_ref = TRUE;
1058
1059   priv->monitor_num = -1;
1060
1061   context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1062   gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1063 }
1064
1065 static void
1066 gtk_menu_destroy (GtkWidget *widget)
1067 {
1068   GtkMenu *menu = GTK_MENU (widget);
1069   GtkMenuPrivate *priv = menu->priv;
1070   GtkMenuAttachData *data;
1071
1072   gtk_menu_remove_scroll_timeout (menu);
1073
1074   data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1075   if (data)
1076     gtk_menu_detach (menu);
1077
1078   gtk_menu_stop_navigating_submenu (menu);
1079
1080   if (priv->old_active_menu_item)
1081     g_clear_object (&priv->old_active_menu_item);
1082
1083   /* Add back the reference count for being a child */
1084   if (priv->needs_destruction_ref)
1085     {
1086       priv->needs_destruction_ref = FALSE;
1087       g_object_ref (widget);
1088     }
1089
1090   if (priv->accel_group)
1091     g_clear_object (&priv->accel_group);
1092
1093   if (priv->toplevel)
1094     gtk_widget_destroy (priv->toplevel);
1095
1096   if (priv->tearoff_window)
1097     gtk_widget_destroy (priv->tearoff_window);
1098
1099   if (priv->heights)
1100     {
1101       g_free (priv->heights);
1102       priv->heights = NULL;
1103     }
1104
1105   if (priv->title)
1106     {
1107       g_free (priv->title);
1108       priv->title = NULL;
1109     }
1110
1111   if (priv->position_func_data_destroy)
1112     {
1113       priv->position_func_data_destroy (priv->position_func_data);
1114       priv->position_func_data = NULL;
1115       priv->position_func_data_destroy = NULL;
1116     }
1117
1118   GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1119 }
1120
1121 static void
1122 menu_change_screen (GtkMenu   *menu,
1123                     GdkScreen *new_screen)
1124 {
1125   GtkMenuPrivate *priv = menu->priv;
1126
1127   if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1128     {
1129       if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1130         return;
1131     }
1132
1133   if (priv->torn_off)
1134     {
1135       gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1136       gtk_menu_position (menu, TRUE);
1137     }
1138
1139   gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1140   priv->monitor_num = -1;
1141 }
1142
1143 static void
1144 attach_widget_screen_changed (GtkWidget *attach_widget,
1145                               GdkScreen *previous_screen,
1146                               GtkMenu   *menu)
1147 {
1148   if (gtk_widget_has_screen (attach_widget) &&
1149       !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1150     menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1151 }
1152
1153 /**
1154  * gtk_menu_attach_to_widget:
1155  * @menu: a #GtkMenu
1156  * @attach_widget: the #GtkWidget that the menu will be attached to
1157  * @detacher: (scope async)(allow-none): the user supplied callback function
1158  *             that will be called when the menu calls gtk_menu_detach()
1159  *
1160  * Attaches the menu to the widget and provides a callback function
1161  * that will be invoked when the menu calls gtk_menu_detach() during
1162  * its destruction.
1163  */
1164 void
1165 gtk_menu_attach_to_widget (GtkMenu           *menu,
1166                            GtkWidget         *attach_widget,
1167                            GtkMenuDetachFunc  detacher)
1168 {
1169   GtkMenuAttachData *data;
1170   GList *list;
1171
1172   g_return_if_fail (GTK_IS_MENU (menu));
1173   g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1174
1175   /* keep this function in sync with gtk_widget_set_parent() */
1176   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1177   if (data)
1178     {
1179       g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1180                  g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1181      return;
1182     }
1183
1184   g_object_ref_sink (menu);
1185
1186   data = g_slice_new (GtkMenuAttachData);
1187   data->attach_widget = attach_widget;
1188
1189   g_signal_connect (attach_widget, "screen-changed",
1190                     G_CALLBACK (attach_widget_screen_changed), menu);
1191   attach_widget_screen_changed (attach_widget, NULL, menu);
1192
1193   data->detacher = detacher;
1194   g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1195   list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1196   if (!g_list_find (list, menu))
1197     list = g_list_prepend (list, menu);
1198
1199   g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1200                           (GDestroyNotify) g_list_free);
1201
1202   if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1203     gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1204
1205   /* Attach the widget to the toplevel window. */
1206   gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), attach_widget);
1207
1208   /* Fallback title for menu comes from attach widget */
1209   gtk_menu_update_title (menu);
1210
1211   g_object_notify (G_OBJECT (menu), "attach-widget");
1212 }
1213
1214 /**
1215  * gtk_menu_get_attach_widget:
1216  * @menu: a #GtkMenu
1217  *
1218  * Returns the #GtkWidget that the menu is attached to.
1219  *
1220  * Returns: (transfer none): the #GtkWidget that the menu is attached to
1221  */
1222 GtkWidget*
1223 gtk_menu_get_attach_widget (GtkMenu *menu)
1224 {
1225   GtkMenuAttachData *data;
1226
1227   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1228
1229   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1230   if (data)
1231     return data->attach_widget;
1232   return NULL;
1233 }
1234
1235 /**
1236  * gtk_menu_detach:
1237  * @menu: a #GtkMenu
1238  *
1239  * Detaches the menu from the widget to which it had been attached.
1240  * This function will call the callback function, @detacher, provided
1241  * when the gtk_menu_attach_to_widget() function was called.
1242  */
1243 void
1244 gtk_menu_detach (GtkMenu *menu)
1245 {
1246   GtkMenuAttachData *data;
1247   GList *list;
1248
1249   g_return_if_fail (GTK_IS_MENU (menu));
1250
1251   /* keep this function in sync with gtk_widget_unparent() */
1252   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1253   if (!data)
1254     {
1255       g_warning ("gtk_menu_detach(): menu is not attached");
1256       return;
1257     }
1258   g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1259
1260   /* Detach the toplevel window. */
1261   gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), NULL);
1262
1263   g_signal_handlers_disconnect_by_func (data->attach_widget,
1264                                         (gpointer) attach_widget_screen_changed,
1265                                         menu);
1266
1267   if (data->detacher)
1268     data->detacher (data->attach_widget, menu);
1269   list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1270   list = g_list_remove (list, menu);
1271   if (list)
1272     g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1273                             (GDestroyNotify) g_list_free);
1274   else
1275     g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1276
1277   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1278     gtk_widget_unrealize (GTK_WIDGET (menu));
1279
1280   g_slice_free (GtkMenuAttachData, data);
1281
1282   /* Fallback title for menu comes from attach widget */
1283   gtk_menu_update_title (menu);
1284
1285   g_object_unref (menu);
1286 }
1287
1288 static void
1289 gtk_menu_remove (GtkContainer *container,
1290                  GtkWidget    *widget)
1291 {
1292   GtkMenu *menu = GTK_MENU (container);
1293   GtkMenuPrivate *priv = menu->priv;
1294
1295   /* Clear out old_active_menu_item if it matches the item we are removing */
1296   if (priv->old_active_menu_item == widget)
1297     g_clear_object (&priv->old_active_menu_item);
1298
1299   GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1300
1301   g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1302
1303   menu_queue_resize (menu);
1304 }
1305
1306 /**
1307  * gtk_menu_new:
1308  *
1309  * Creates a new #GtkMenu
1310  *
1311  * Returns: a new #GtkMenu
1312  */
1313 GtkWidget*
1314 gtk_menu_new (void)
1315 {
1316   return g_object_new (GTK_TYPE_MENU, NULL);
1317 }
1318
1319 static void
1320 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1321                       GtkWidget    *child,
1322                       gint          position)
1323 {
1324   GtkMenu *menu = GTK_MENU (menu_shell);
1325   GtkMenuPrivate *priv = menu->priv;
1326   AttachInfo *ai = get_attach_info (child);
1327
1328   ai->left_attach = -1;
1329   ai->right_attach = -1;
1330   ai->top_attach = -1;
1331   ai->bottom_attach = -1;
1332
1333   if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1334     gtk_widget_set_parent_window (child, priv->bin_window);
1335
1336   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1337
1338   menu_queue_resize (menu);
1339 }
1340
1341 static void
1342 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1343 {
1344   GtkMenuPrivate *priv = menu->priv;
1345   gint width, height;
1346
1347   if (priv->torn_off)
1348     {
1349       GdkWindow *window;
1350       cairo_surface_t *surface;
1351       cairo_pattern_t *pattern;
1352       cairo_t *cr;
1353
1354       priv->tearoff_active = FALSE;
1355       priv->saved_scroll_offset = priv->scroll_offset;
1356
1357       window = gtk_widget_get_window (priv->tearoff_window);
1358       width = gdk_window_get_width (window);
1359       height = gdk_window_get_height (window);
1360
1361       surface = gdk_window_create_similar_surface (window,
1362                                                    CAIRO_CONTENT_COLOR,
1363                                                    width,
1364                                                    height);
1365
1366       cr = cairo_create (surface);
1367       gdk_cairo_set_source_window (cr, window, 0, 0);
1368       cairo_paint (cr);
1369       cairo_destroy (cr);
1370
1371       gtk_widget_set_size_request (priv->tearoff_window, width, height);
1372
1373       pattern = cairo_pattern_create_for_surface (surface);
1374       gdk_window_set_background_pattern (window, pattern);
1375
1376       cairo_pattern_destroy (pattern);
1377       cairo_surface_destroy (surface);
1378     }
1379 }
1380
1381 static gboolean
1382 popup_grab_on_window (GdkWindow *window,
1383                       GdkDevice *keyboard,
1384                       GdkDevice *pointer,
1385                       guint32    activate_time)
1386 {
1387   if (keyboard &&
1388       gdk_device_grab (keyboard, window,
1389                        GDK_OWNERSHIP_WINDOW, TRUE,
1390                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1391                        NULL, activate_time) != GDK_GRAB_SUCCESS)
1392     return FALSE;
1393
1394   if (pointer &&
1395       gdk_device_grab (pointer, window,
1396                        GDK_OWNERSHIP_WINDOW, TRUE,
1397                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1398                        GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1399                        GDK_POINTER_MOTION_MASK,
1400                        NULL, activate_time) != GDK_GRAB_SUCCESS)
1401     {
1402       if (keyboard)
1403         gdk_device_ungrab (keyboard, activate_time);
1404
1405       return FALSE;
1406     }
1407
1408   return TRUE;
1409 }
1410
1411 /**
1412  * gtk_menu_popup_for_device:
1413  * @menu: a #GtkMenu
1414  * @device: (allow-none): a #GdkDevice
1415  * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1416  *     menu item, or %NULL
1417  * @parent_menu_item: (allow-none): the menu item whose activation triggered
1418  *     the popup, or %NULL
1419  * @func: (allow-none): a user supplied function used to position the menu,
1420  *     or %NULL
1421  * @data: (allow-none): user supplied data to be passed to @func
1422  * @destroy: (allow-none): destroy notify for @data
1423  * @button: the mouse button which was pressed to initiate the event
1424  * @activate_time: the time at which the activation event occurred
1425  *
1426  * Displays a menu and makes it available for selection.
1427  *
1428  * Applications can use this function to display context-sensitive menus,
1429  * and will typically supply %NULL for the @parent_menu_shell,
1430  * @parent_menu_item, @func, @data and @destroy parameters. The default
1431  * menu positioning function will position the menu at the current position
1432  * of @device (or its corresponding pointer).
1433  *
1434  * The @button parameter should be the mouse button pressed to initiate
1435  * the menu popup. If the menu popup was initiated by something other than
1436  * a mouse button press, such as a mouse button release or a keypress,
1437  * @button should be 0.
1438  *
1439  * The @activate_time parameter is used to conflict-resolve initiation of
1440  * concurrent requests for mouse/keyboard grab requests. To function
1441  * properly, this needs to be the time stamp of the user event (such as
1442  * a mouse click or key press) that caused the initiation of the popup.
1443  * Only if no such event is available, gtk_get_current_event_time() can
1444  * be used instead.
1445  *
1446  * Since: 3.0
1447  * Rename to: gtk_menu_popup
1448  */
1449 void
1450 gtk_menu_popup_for_device (GtkMenu             *menu,
1451                            GdkDevice           *device,
1452                            GtkWidget           *parent_menu_shell,
1453                            GtkWidget           *parent_menu_item,
1454                            GtkMenuPositionFunc  func,
1455                            gpointer             data,
1456                            GDestroyNotify       destroy,
1457                            guint                button,
1458                            guint32              activate_time)
1459 {
1460   GtkMenuPrivate *priv = menu->priv;
1461   GtkWidget *widget;
1462   GtkWidget *xgrab_shell;
1463   GtkWidget *parent;
1464   GdkEvent *current_event;
1465   GtkMenuShell *menu_shell;
1466   gboolean grab_keyboard;
1467   GtkWidget *parent_toplevel;
1468   GdkDevice *keyboard, *pointer;
1469
1470   g_return_if_fail (GTK_IS_MENU (menu));
1471   g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1472
1473   if (device == NULL)
1474     device = gtk_get_current_event_device ();
1475
1476   if (device == NULL)
1477     {
1478       GdkDisplay *display;
1479       GdkDeviceManager *device_manager;
1480       GList *devices;
1481
1482       display = gtk_widget_get_display (GTK_WIDGET (menu));
1483       device_manager = gdk_display_get_device_manager (display);
1484       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1485
1486       device = devices->data;
1487
1488       g_list_free (devices);
1489     }
1490
1491   widget = GTK_WIDGET (menu);
1492   menu_shell = GTK_MENU_SHELL (menu);
1493
1494   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1495     {
1496       keyboard = device;
1497       pointer = gdk_device_get_associated_device (device);
1498     }
1499   else
1500     {
1501       pointer = device;
1502       keyboard = gdk_device_get_associated_device (device);
1503     }
1504
1505   menu_shell->priv->parent_menu_shell = parent_menu_shell;
1506
1507   priv->seen_item_enter = FALSE;
1508
1509   /* Find the last viewable ancestor, and make an X grab on it
1510    */
1511   parent = GTK_WIDGET (menu);
1512   xgrab_shell = NULL;
1513   while (parent)
1514     {
1515       gboolean viewable = TRUE;
1516       GtkWidget *tmp = parent;
1517
1518       while (tmp)
1519         {
1520           if (!gtk_widget_get_mapped (tmp))
1521             {
1522               viewable = FALSE;
1523               break;
1524             }
1525           tmp = gtk_widget_get_parent (tmp);
1526         }
1527
1528       if (viewable)
1529         xgrab_shell = parent;
1530
1531       parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1532     }
1533
1534   /* We want to receive events generated when we map the menu;
1535    * unfortunately, since there is probably already an implicit
1536    * grab in place from the button that the user used to pop up
1537    * the menu, we won't receive then -- in particular, the EnterNotify
1538    * when the menu pops up under the pointer.
1539    *
1540    * If we are grabbing on a parent menu shell, no problem; just grab
1541    * on that menu shell first before popping up the window with
1542    * owner_events = TRUE.
1543    *
1544    * When grabbing on the menu itself, things get more convoluted --
1545    * we do an explicit grab on a specially created window with
1546    * owner_events = TRUE, which we override further down with a
1547    * grab on the menu. (We can't grab on the menu until it is mapped;
1548    * we probably could just leave the grab on the other window,
1549    * with a little reorganization of the code in gtkmenu*).
1550    */
1551   grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1552   gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1553
1554   if (!grab_keyboard)
1555     keyboard = NULL;
1556
1557   if (xgrab_shell && xgrab_shell != widget)
1558     {
1559       if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1560         {
1561           _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1562           GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1563         }
1564     }
1565   else
1566     {
1567       GdkWindow *transfer_window;
1568
1569       xgrab_shell = widget;
1570       transfer_window = menu_grab_transfer_window_get (menu);
1571       if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1572         {
1573           _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1574           GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1575         }
1576     }
1577
1578   if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1579     {
1580       /* We failed to make our pointer/keyboard grab.
1581        * Rather than leaving the user with a stuck up window,
1582        * we just abort here. Presumably the user will try again.
1583        */
1584       menu_shell->priv->parent_menu_shell = NULL;
1585       menu_grab_transfer_window_destroy (menu);
1586       return;
1587     }
1588
1589   _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1590   menu_shell->priv->active = TRUE;
1591   menu_shell->priv->button = button;
1592
1593   /* If we are popping up the menu from something other than, a button
1594    * press then, as a heuristic, we ignore enter events for the menu
1595    * until we get a MOTION_NOTIFY.
1596    */
1597
1598   current_event = gtk_get_current_event ();
1599   if (current_event)
1600     {
1601       if ((current_event->type != GDK_BUTTON_PRESS) &&
1602           (current_event->type != GDK_ENTER_NOTIFY))
1603         menu_shell->priv->ignore_enter = TRUE;
1604
1605       gdk_event_free (current_event);
1606     }
1607   else
1608     menu_shell->priv->ignore_enter = TRUE;
1609
1610   if (priv->torn_off)
1611     {
1612       gtk_menu_tearoff_bg_copy (menu);
1613
1614       gtk_menu_reparent (menu, priv->toplevel, FALSE);
1615     }
1616
1617   parent_toplevel = NULL;
1618   if (parent_menu_shell)
1619     parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1620   else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1621     {
1622       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1623       if (attach_widget)
1624         parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1625     }
1626
1627   /* Set transient for to get the right window group and parent */
1628   if (GTK_IS_WINDOW (parent_toplevel))
1629     gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1630                                   GTK_WINDOW (parent_toplevel));
1631
1632   priv->parent_menu_item = parent_menu_item;
1633   priv->position_func = func;
1634   priv->position_func_data = data;
1635   priv->position_func_data_destroy = destroy;
1636   menu_shell->priv->activate_time = activate_time;
1637
1638   /* We need to show the menu here rather in the init function
1639    * because code expects to be able to tell if the menu is onscreen
1640    * by looking at gtk_widget_get_visible (menu)
1641    */
1642   gtk_widget_show (GTK_WIDGET (menu));
1643
1644   /* Position the menu, possibly changing the size request
1645    */
1646   gtk_menu_position (menu, TRUE);
1647
1648   /* Compute the size of the toplevel and realize it so we
1649    * can scroll correctly.
1650    */
1651   if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1652   {
1653     GtkRequisition tmp_request;
1654     GtkAllocation tmp_allocation = { 0, };
1655
1656     /* Instead of trusting the menu position function to queue a
1657      * resize when the menu goes out of bounds, invalidate the cached
1658      * size here.
1659      */
1660     gtk_widget_queue_resize (GTK_WIDGET (menu));
1661     gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1662
1663     tmp_allocation.width = tmp_request.width;
1664     tmp_allocation.height = tmp_request.height;
1665
1666     gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1667
1668     gtk_widget_realize (priv->toplevel);
1669   }
1670
1671   gtk_menu_scroll_to (menu, priv->scroll_offset);
1672
1673   /* if no item is selected, select the first one */
1674   if (!menu_shell->priv->active_menu_item)
1675     {
1676       gboolean touchscreen_mode;
1677
1678       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1679                     "gtk-touchscreen-mode", &touchscreen_mode,
1680                     NULL);
1681
1682       if (touchscreen_mode)
1683         gtk_menu_shell_select_first (menu_shell, TRUE);
1684     }
1685
1686   /* Once everything is set up correctly, map the toplevel */
1687   gtk_widget_show (priv->toplevel);
1688
1689   if (xgrab_shell == widget)
1690     popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1691
1692   gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1693
1694   if (parent_menu_shell)
1695     {
1696       gboolean keyboard_mode;
1697
1698       keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1699       _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1700     }
1701   else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1702     _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1703
1704   _gtk_menu_shell_update_mnemonics (menu_shell);
1705 }
1706
1707 /**
1708  * gtk_menu_popup: (skip)
1709  * @menu: a #GtkMenu
1710  * @parent_menu_shell: (allow-none): the menu shell containing the
1711  *     triggering menu item, or %NULL
1712  * @parent_menu_item: (allow-none): the menu item whose activation
1713  *     triggered the popup, or %NULL
1714  * @func: (allow-none): a user supplied function used to position
1715  *     the menu, or %NULL
1716  * @data: user supplied data to be passed to @func.
1717  * @button: the mouse button which was pressed to initiate the event.
1718  * @activate_time: the time at which the activation event occurred.
1719  *
1720  * Displays a menu and makes it available for selection.
1721  *
1722  * Applications can use this function to display context-sensitive
1723  * menus, and will typically supply %NULL for the @parent_menu_shell,
1724  * @parent_menu_item, @func and @data parameters. The default menu
1725  * positioning function will position the menu at the current mouse
1726  * cursor position.
1727  *
1728  * The @button parameter should be the mouse button pressed to initiate
1729  * the menu popup. If the menu popup was initiated by something other
1730  * than a mouse button press, such as a mouse button release or a keypress,
1731  * @button should be 0.
1732  *
1733  * The @activate_time parameter is used to conflict-resolve initiation
1734  * of concurrent requests for mouse/keyboard grab requests. To function
1735  * properly, this needs to be the timestamp of the user event (such as
1736  * a mouse click or key press) that caused the initiation of the popup.
1737  * Only if no such event is available, gtk_get_current_event_time() can
1738  * be used instead.
1739  */
1740 void
1741 gtk_menu_popup (GtkMenu             *menu,
1742                 GtkWidget           *parent_menu_shell,
1743                 GtkWidget           *parent_menu_item,
1744                 GtkMenuPositionFunc  func,
1745                 gpointer             data,
1746                 guint                button,
1747                 guint32              activate_time)
1748 {
1749   g_return_if_fail (GTK_IS_MENU (menu));
1750
1751   gtk_menu_popup_for_device (menu,
1752                              NULL,
1753                              parent_menu_shell,
1754                              parent_menu_item,
1755                              func, data, NULL,
1756                              button, activate_time);
1757 }
1758
1759 /**
1760  * gtk_menu_popdown:
1761  * @menu: a #GtkMenu
1762  *
1763  * Removes the menu from the screen.
1764  */
1765 void
1766 gtk_menu_popdown (GtkMenu *menu)
1767 {
1768   GtkMenuPrivate *priv;
1769   GtkMenuShell *menu_shell;
1770   GdkDevice *pointer;
1771
1772   g_return_if_fail (GTK_IS_MENU (menu));
1773
1774   menu_shell = GTK_MENU_SHELL (menu);
1775   priv = menu->priv;
1776
1777   menu_shell->priv->parent_menu_shell = NULL;
1778   menu_shell->priv->active = FALSE;
1779   menu_shell->priv->ignore_enter = FALSE;
1780
1781   priv->have_position = FALSE;
1782
1783   gtk_menu_stop_scrolling (menu);
1784   gtk_menu_stop_navigating_submenu (menu);
1785
1786   if (menu_shell->priv->active_menu_item)
1787     {
1788       if (priv->old_active_menu_item)
1789         g_object_unref (priv->old_active_menu_item);
1790       priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1791       g_object_ref (priv->old_active_menu_item);
1792     }
1793
1794   gtk_menu_shell_deselect (menu_shell);
1795
1796   /* The X Grab, if present, will automatically be removed
1797    * when we hide the window
1798    */
1799   if (priv->toplevel)
1800     {
1801       gtk_widget_hide (priv->toplevel);
1802       gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1803     }
1804
1805   pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1806
1807   if (priv->torn_off)
1808     {
1809       gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1810
1811       if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1812         {
1813           gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1814         }
1815       else
1816         {
1817           /* We popped up the menu from the tearoff, so we need to
1818            * release the grab - we aren't actually hiding the menu.
1819            */
1820           if (menu_shell->priv->have_xgrab && pointer)
1821             {
1822               GdkDevice *keyboard;
1823
1824               gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1825               keyboard = gdk_device_get_associated_device (pointer);
1826
1827               if (keyboard)
1828                 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1829             }
1830         }
1831
1832       /* gtk_menu_popdown is called each time a menu item is selected from
1833        * a torn off menu. Only scroll back to the saved position if the
1834        * non-tearoff menu was popped down.
1835        */
1836       if (!priv->tearoff_active)
1837         gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1838       priv->tearoff_active = TRUE;
1839     }
1840   else
1841     gtk_widget_hide (GTK_WIDGET (menu));
1842
1843   menu_shell->priv->have_xgrab = FALSE;
1844
1845   if (pointer)
1846     gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1847
1848   _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1849
1850   menu_grab_transfer_window_destroy (menu);
1851 }
1852
1853 /**
1854  * gtk_menu_get_active:
1855  * @menu: a #GtkMenu
1856  *
1857  * Returns the selected menu item from the menu.  This is used by the
1858  * #GtkOptionMenu.
1859  *
1860  * Returns: (transfer none): the #GtkMenuItem that was last selected
1861  *          in the menu.  If a selection has not yet been made, the
1862  *          first menu item is selected.
1863  */
1864 GtkWidget*
1865 gtk_menu_get_active (GtkMenu *menu)
1866 {
1867   GtkMenuPrivate *priv = menu->priv;
1868   GtkWidget *child;
1869   GList *children;
1870
1871   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1872
1873   if (!priv->old_active_menu_item)
1874     {
1875       child = NULL;
1876       children = GTK_MENU_SHELL (menu)->priv->children;
1877
1878       while (children)
1879         {
1880           child = children->data;
1881           children = children->next;
1882
1883           if (gtk_bin_get_child (GTK_BIN (child)))
1884             break;
1885           child = NULL;
1886         }
1887
1888       priv->old_active_menu_item = child;
1889       if (priv->old_active_menu_item)
1890         g_object_ref (priv->old_active_menu_item);
1891     }
1892
1893   return priv->old_active_menu_item;
1894 }
1895
1896 /**
1897  * gtk_menu_set_active:
1898  * @menu: a #GtkMenu
1899  * @index: the index of the menu item to select.  Iindex values are
1900  *         from 0 to n-1
1901  *
1902  * Selects the specified menu item within the menu.  This is used by
1903  * the #GtkOptionMenu and should not be used by anyone else.
1904  */
1905 void
1906 gtk_menu_set_active (GtkMenu *menu,
1907                      guint    index)
1908 {
1909   GtkMenuPrivate *priv = menu->priv;
1910   GtkWidget *child;
1911   GList *tmp_list;
1912
1913   g_return_if_fail (GTK_IS_MENU (menu));
1914
1915   tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1916   if (tmp_list)
1917     {
1918       child = tmp_list->data;
1919       if (gtk_bin_get_child (GTK_BIN (child)))
1920         {
1921           if (priv->old_active_menu_item)
1922             g_object_unref (priv->old_active_menu_item);
1923           priv->old_active_menu_item = child;
1924           g_object_ref (priv->old_active_menu_item);
1925         }
1926     }
1927 }
1928
1929 /**
1930  * gtk_menu_set_accel_group:
1931  * @menu: a #GtkMenu
1932  * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1933  *               with the menu.
1934  *
1935  * Set the #GtkAccelGroup which holds global accelerators for the
1936  * menu.  This accelerator group needs to also be added to all windows
1937  * that this menu is being used in with gtk_window_add_accel_group(),
1938  * in order for those windows to support all the accelerators
1939  * contained in this group.
1940  */
1941 void
1942 gtk_menu_set_accel_group (GtkMenu       *menu,
1943                           GtkAccelGroup *accel_group)
1944 {
1945   GtkMenuPrivate *priv = menu->priv;
1946   g_return_if_fail (GTK_IS_MENU (menu));
1947
1948   if (priv->accel_group != accel_group)
1949     {
1950       if (priv->accel_group)
1951         g_object_unref (priv->accel_group);
1952       priv->accel_group = accel_group;
1953       if (priv->accel_group)
1954         g_object_ref (priv->accel_group);
1955       _gtk_menu_refresh_accel_paths (menu, TRUE);
1956     }
1957 }
1958
1959 /**
1960  * gtk_menu_get_accel_group:
1961  * @menu: a #GtkMenu
1962  *
1963  * Gets the #GtkAccelGroup which holds global accelerators for the
1964  * menu. See gtk_menu_set_accel_group().
1965  *
1966  * Returns: (transfer none): the #GtkAccelGroup associated with the menu
1967  */
1968 GtkAccelGroup*
1969 gtk_menu_get_accel_group (GtkMenu *menu)
1970 {
1971   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1972
1973   return menu->priv->accel_group;
1974 }
1975
1976 static gboolean
1977 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1978                                   guint      signal_id)
1979 {
1980   /* Menu items chain here to figure whether they can activate their
1981    * accelerators.  Unlike ordinary widgets, menus allow accel
1982    * activation even if invisible since that's the usual case for
1983    * submenus/popup-menus. however, the state of the attach widget
1984    * affects the "activeness" of the menu.
1985    */
1986   GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1987
1988   if (awidget)
1989     return gtk_widget_can_activate_accel (awidget, signal_id);
1990   else
1991     return gtk_widget_is_sensitive (widget);
1992 }
1993
1994 /**
1995  * gtk_menu_set_accel_path
1996  * @menu:       a valid #GtkMenu
1997  * @accel_path: (allow-none): a valid accelerator path
1998  *
1999  * Sets an accelerator path for this menu from which accelerator paths
2000  * for its immediate children, its menu items, can be constructed.
2001  * The main purpose of this function is to spare the programmer the
2002  * inconvenience of having to call gtk_menu_item_set_accel_path() on
2003  * each menu item that should support runtime user changable accelerators.
2004  * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2005  * each menu item of this menu, that contains a label describing its
2006  * purpose, automatically gets an accel path assigned.
2007  *
2008  * For example, a menu containing menu items "New" and "Exit", will, after
2009  * <literal>gtk_menu_set_accel_path (menu, "&lt;Gnumeric-Sheet&gt;/File");</literal>
2010  * has been called, assign its items the accel paths:
2011  * <literal>"&lt;Gnumeric-Sheet&gt;/File/New"</literal> and <literal>"&lt;Gnumeric-Sheet&gt;/File/Exit"</literal>.
2012  *
2013  * Assigning accel paths to menu items then enables the user to change
2014  * their accelerators at runtime. More details about accelerator paths
2015  * and their default setups can be found at gtk_accel_map_add_entry().
2016  *
2017  * Note that @accel_path string will be stored in a #GQuark. Therefore,
2018  * if you pass a static string, you can save some memory by interning
2019  * it first with g_intern_static_string().
2020  */
2021 void
2022 gtk_menu_set_accel_path (GtkMenu     *menu,
2023                          const gchar *accel_path)
2024 {
2025   GtkMenuPrivate *priv = menu->priv;
2026   g_return_if_fail (GTK_IS_MENU (menu));
2027
2028   if (accel_path)
2029     g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2030
2031   /* FIXME: accel_path should be defined as const gchar* */
2032   priv->accel_path = (gchar*)g_intern_string (accel_path);
2033   if (priv->accel_path)
2034     _gtk_menu_refresh_accel_paths (menu, FALSE);
2035 }
2036
2037 /**
2038  * gtk_menu_get_accel_path
2039  * @menu: a valid #GtkMenu
2040  *
2041  * Retrieves the accelerator path set on the menu.
2042  *
2043  * Returns: the accelerator path set on the menu.
2044  *
2045  * Since: 2.14
2046  */
2047 const gchar*
2048 gtk_menu_get_accel_path (GtkMenu *menu)
2049 {
2050   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2051
2052   return menu->priv->accel_path;
2053 }
2054
2055 typedef struct {
2056   GtkMenu *menu;
2057   gboolean group_changed;
2058 } AccelPropagation;
2059
2060 static void
2061 refresh_accel_paths_foreach (GtkWidget *widget,
2062                              gpointer   data)
2063 {
2064   GtkMenuPrivate *priv;
2065   AccelPropagation *prop = data;
2066
2067   if (GTK_IS_MENU_ITEM (widget))  /* should always be true */
2068     {
2069       priv = prop->menu->priv;
2070       _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2071                                          priv->accel_path,
2072                                          priv->accel_group,
2073                                          prop->group_changed);
2074     }
2075 }
2076
2077 static void
2078 _gtk_menu_refresh_accel_paths (GtkMenu  *menu,
2079                                gboolean  group_changed)
2080 {
2081   GtkMenuPrivate *priv = menu->priv;
2082   g_return_if_fail (GTK_IS_MENU (menu));
2083
2084   if (priv->accel_path && priv->accel_group)
2085     {
2086       AccelPropagation prop;
2087
2088       prop.menu = menu;
2089       prop.group_changed = group_changed;
2090       gtk_container_foreach (GTK_CONTAINER (menu),
2091                              refresh_accel_paths_foreach,
2092                              &prop);
2093     }
2094 }
2095
2096 /**
2097  * gtk_menu_reposition:
2098  * @menu: a #GtkMenu
2099  *
2100  * Repositions the menu according to its position function.
2101  */
2102 void
2103 gtk_menu_reposition (GtkMenu *menu)
2104 {
2105   g_return_if_fail (GTK_IS_MENU (menu));
2106
2107   if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2108     gtk_menu_position (menu, FALSE);
2109 }
2110
2111 static void
2112 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2113                             GtkMenu       *menu)
2114 {
2115   double value;
2116
2117   value = gtk_adjustment_get_value (adjustment);
2118   if (menu->priv->scroll_offset != value)
2119     gtk_menu_scroll_to (menu, value);
2120 }
2121
2122 static void
2123 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2124                             gint     width)
2125 {
2126   GtkMenuPrivate *priv = menu->priv;
2127   GdkGeometry geometry_hints;
2128
2129   if (!priv->tearoff_window)
2130     return;
2131
2132   if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2133     {
2134       GtkRequisition requisition;
2135
2136       gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2137                                      &requisition, NULL);
2138       width += requisition.width;
2139     }
2140
2141   geometry_hints.min_width = width;
2142   geometry_hints.max_width = width;
2143
2144   geometry_hints.min_height = 0;
2145   geometry_hints.max_height = priv->requested_height;
2146
2147   gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2148                                  NULL,
2149                                  &geometry_hints,
2150                                  GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2151 }
2152
2153 static void
2154 gtk_menu_update_title (GtkMenu *menu)
2155 {
2156   GtkMenuPrivate *priv = menu->priv;
2157
2158   if (priv->tearoff_window)
2159     {
2160       const gchar *title;
2161       GtkWidget *attach_widget;
2162
2163       title = gtk_menu_get_title (menu);
2164       if (!title)
2165         {
2166           attach_widget = gtk_menu_get_attach_widget (menu);
2167           if (GTK_IS_MENU_ITEM (attach_widget))
2168             {
2169               GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2170               if (GTK_IS_LABEL (child))
2171                 title = gtk_label_get_text (GTK_LABEL (child));
2172             }
2173         }
2174
2175       if (title)
2176         gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2177     }
2178 }
2179
2180 static GtkWidget*
2181 gtk_menu_get_toplevel (GtkWidget *menu)
2182 {
2183   GtkWidget *attach, *toplevel;
2184
2185   attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2186
2187   if (GTK_IS_MENU_ITEM (attach))
2188     attach = gtk_widget_get_parent (attach);
2189
2190   if (GTK_IS_MENU (attach))
2191     return gtk_menu_get_toplevel (attach);
2192   else if (GTK_IS_WIDGET (attach))
2193     {
2194       toplevel = gtk_widget_get_toplevel (attach);
2195       if (gtk_widget_is_toplevel (toplevel))
2196         return toplevel;
2197     }
2198
2199   return NULL;
2200 }
2201
2202 static void
2203 tearoff_window_destroyed (GtkWidget *widget,
2204                           GtkMenu   *menu)
2205 {
2206   gtk_menu_set_tearoff_state (menu, FALSE);
2207 }
2208
2209 /**
2210  * gtk_menu_set_tearoff_state:
2211  * @menu: a #GtkMenu
2212  * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2213  *
2214  * Changes the tearoff state of the menu.  A menu is normally
2215  * displayed as drop down menu which persists as long as the menu is
2216  * active.  It can also be displayed as a tearoff menu which persists
2217  * until it is closed or reattached.
2218  */
2219 void
2220 gtk_menu_set_tearoff_state (GtkMenu  *menu,
2221                             gboolean  torn_off)
2222 {
2223   GtkMenuPrivate *priv = menu->priv;
2224   gint height;
2225
2226   g_return_if_fail (GTK_IS_MENU (menu));
2227
2228   if (priv->torn_off != torn_off)
2229     {
2230       priv->torn_off = torn_off;
2231       priv->tearoff_active = torn_off;
2232
2233       if (priv->torn_off)
2234         {
2235           if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2236             gtk_menu_popdown (menu);
2237
2238           if (!priv->tearoff_window)
2239             {
2240               GtkWidget *toplevel;
2241
2242               priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2243                                                    "type", GTK_WINDOW_TOPLEVEL,
2244                                                    "screen", gtk_widget_get_screen (priv->toplevel),
2245                                                    "app-paintable", TRUE,
2246                                                    NULL);
2247
2248               gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2249                                         GDK_WINDOW_TYPE_HINT_MENU);
2250               gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2251               g_signal_connect (priv->tearoff_window, "destroy",
2252                                 G_CALLBACK (tearoff_window_destroyed), menu);
2253               g_signal_connect (priv->tearoff_window, "event",
2254                                 G_CALLBACK (gtk_menu_window_event), menu);
2255
2256               gtk_menu_update_title (menu);
2257
2258               gtk_widget_realize (priv->tearoff_window);
2259
2260               toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2261               if (toplevel != NULL)
2262                 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2263                                               GTK_WINDOW (toplevel));
2264
2265               priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2266               gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2267                                  priv->tearoff_hbox);
2268
2269               height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2270               priv->tearoff_adjustment = gtk_adjustment_new (0,
2271                                                              0, priv->requested_height,
2272                                                              MENU_SCROLL_STEP2,
2273                                                              height/2,
2274                                                              height);
2275               g_object_connect (priv->tearoff_adjustment,
2276                                 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2277                                 NULL);
2278               priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2279
2280               gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2281                                 priv->tearoff_scrollbar,
2282                                 FALSE, FALSE, 0);
2283
2284               if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2285                 gtk_widget_show (priv->tearoff_scrollbar);
2286
2287               gtk_widget_show (priv->tearoff_hbox);
2288             }
2289
2290           gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2291
2292           /* Update menu->requisition */
2293           gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2294
2295           gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2296
2297           gtk_widget_realize (priv->tearoff_window);
2298           gtk_menu_position (menu, TRUE);
2299
2300           gtk_widget_show (GTK_WIDGET (menu));
2301           gtk_widget_show (priv->tearoff_window);
2302
2303           gtk_menu_scroll_to (menu, 0);
2304
2305         }
2306       else
2307         {
2308           gtk_widget_hide (GTK_WIDGET (menu));
2309           gtk_widget_hide (priv->tearoff_window);
2310           if (GTK_IS_CONTAINER (priv->toplevel))
2311             gtk_menu_reparent (menu, priv->toplevel, FALSE);
2312           gtk_widget_destroy (priv->tearoff_window);
2313
2314           priv->tearoff_window = NULL;
2315           priv->tearoff_hbox = NULL;
2316           priv->tearoff_scrollbar = NULL;
2317           priv->tearoff_adjustment = NULL;
2318         }
2319
2320       g_object_notify (G_OBJECT (menu), "tearoff-state");
2321     }
2322 }
2323
2324 /**
2325  * gtk_menu_get_tearoff_state:
2326  * @menu: a #GtkMenu
2327  *
2328  * Returns whether the menu is torn off.
2329  * See gtk_menu_set_tearoff_state().
2330  *
2331  * Return value: %TRUE if the menu is currently torn off.
2332  */
2333 gboolean
2334 gtk_menu_get_tearoff_state (GtkMenu *menu)
2335 {
2336   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2337
2338   return menu->priv->torn_off;
2339 }
2340
2341 /**
2342  * gtk_menu_set_title:
2343  * @menu: a #GtkMenu
2344  * @title: a string containing the title for the menu
2345  *
2346  * Sets the title string for the menu.
2347  *
2348  * The title is displayed when the menu is shown as a tearoff
2349  * menu. If @title is %NULL, the menu will see if it is attached
2350  * to a parent menu item, and if so it will try to use the same
2351  * text as that menu item's label.
2352  */
2353 void
2354 gtk_menu_set_title (GtkMenu     *menu,
2355                     const gchar *title)
2356 {
2357   GtkMenuPrivate *priv = menu->priv;
2358   char *old_title;
2359
2360   g_return_if_fail (GTK_IS_MENU (menu));
2361
2362   old_title = priv->title;
2363   priv->title = g_strdup (title);
2364   g_free (old_title);
2365
2366   gtk_menu_update_title (menu);
2367   g_object_notify (G_OBJECT (menu), "tearoff-title");
2368 }
2369
2370 /**
2371  * gtk_menu_get_title:
2372  * @menu: a #GtkMenu
2373  *
2374  * Returns the title of the menu. See gtk_menu_set_title().
2375  *
2376  * Return value: the title of the menu, or %NULL if the menu
2377  *     has no title set on it. This string is owned by GTK+
2378  *     and should not be modified or freed.
2379  **/
2380 const gchar *
2381 gtk_menu_get_title (GtkMenu *menu)
2382 {
2383   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2384
2385   return menu->priv->title;
2386 }
2387
2388 /**
2389  * gtk_menu_reorder_child:
2390  * @menu: a #GtkMenu
2391  * @child: the #GtkMenuItem to move
2392  * @position: the new position to place @child.
2393  *     Positions are numbered from 0 to n - 1
2394  *
2395  * Moves @child to a new @position in the list of @menu
2396  * children.
2397  */
2398 void
2399 gtk_menu_reorder_child (GtkMenu   *menu,
2400                         GtkWidget *child,
2401                         gint       position)
2402 {
2403   GtkMenuShell *menu_shell;
2404
2405   g_return_if_fail (GTK_IS_MENU (menu));
2406   g_return_if_fail (GTK_IS_MENU_ITEM (child));
2407
2408   menu_shell = GTK_MENU_SHELL (menu);
2409
2410   if (g_list_find (menu_shell->priv->children, child))
2411     {
2412       menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2413       menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2414
2415       menu_queue_resize (menu);
2416     }
2417 }
2418
2419 static void
2420 gtk_menu_style_updated (GtkWidget *widget)
2421 {
2422   GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2423
2424   if (gtk_widget_get_realized (widget))
2425     {
2426       GtkMenu *menu = GTK_MENU (widget);
2427       GtkMenuPrivate *priv = menu->priv;
2428       GtkStyleContext *context;
2429
2430       context = gtk_widget_get_style_context (widget);
2431
2432       gtk_style_context_set_background (context, priv->bin_window);
2433       gtk_style_context_set_background (context, priv->view_window);
2434       gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2435     }
2436 }
2437
2438 static void
2439 get_arrows_border (GtkMenu   *menu,
2440                    GtkBorder *border)
2441 {
2442   GtkMenuPrivate *priv = menu->priv;
2443   guint scroll_arrow_height;
2444   GtkArrowPlacement arrow_placement;
2445
2446   gtk_widget_style_get (GTK_WIDGET (menu),
2447                         "scroll-arrow-vlength", &scroll_arrow_height,
2448                         "arrow_placement", &arrow_placement,
2449                         NULL);
2450
2451   switch (arrow_placement)
2452     {
2453     case GTK_ARROWS_BOTH:
2454       border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2455       border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2456       break;
2457
2458     case GTK_ARROWS_START:
2459       border->top = (priv->upper_arrow_visible ||
2460                      priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2461       border->bottom = 0;
2462       break;
2463
2464     case GTK_ARROWS_END:
2465       border->top = 0;
2466       border->bottom = (priv->upper_arrow_visible ||
2467                         priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2468       break;
2469     }
2470
2471   border->left = border->right = 0;
2472 }
2473
2474 static void
2475 get_menu_padding (GtkWidget *widget,
2476                   GtkBorder *padding)
2477 {
2478   GtkStyleContext *context;
2479   GtkStateFlags state;
2480
2481   context = gtk_widget_get_style_context (widget);
2482   state = gtk_widget_get_state_flags (widget);
2483
2484   gtk_style_context_get_padding (context, state, padding);
2485 }
2486
2487 static void
2488 gtk_menu_realize (GtkWidget *widget)
2489 {
2490   GtkMenu *menu = GTK_MENU (widget);
2491   GtkMenuPrivate *priv = menu->priv;
2492   GtkAllocation allocation;
2493   GtkStyleContext *context;
2494   GdkWindow *window;
2495   GdkWindowAttr attributes;
2496   gint attributes_mask;
2497   gint border_width;
2498   GtkWidget *child;
2499   GList *children;
2500   guint vertical_padding;
2501   guint horizontal_padding;
2502   GtkBorder arrow_border, padding;
2503
2504   g_return_if_fail (GTK_IS_MENU (widget));
2505
2506   gtk_widget_set_realized (widget, TRUE);
2507
2508   gtk_widget_get_allocation (widget, &allocation);
2509
2510   attributes.window_type = GDK_WINDOW_CHILD;
2511   attributes.x = allocation.x;
2512   attributes.y = allocation.y;
2513   attributes.width = allocation.width;
2514   attributes.height = allocation.height;
2515   attributes.wclass = GDK_INPUT_OUTPUT;
2516   attributes.visual = gtk_widget_get_visual (widget);
2517   attributes.event_mask = gtk_widget_get_events (widget);
2518   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2519                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2520
2521   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2522
2523   window = gdk_window_new (gtk_widget_get_parent_window (widget),
2524                            &attributes, attributes_mask);
2525   gtk_widget_set_window (widget, window);
2526   gdk_window_set_user_data (window, widget);
2527
2528   get_menu_padding (widget, &padding);
2529   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2530   context = gtk_widget_get_style_context (widget);
2531
2532   gtk_widget_style_get (GTK_WIDGET (menu),
2533                         "vertical-padding", &vertical_padding,
2534                         "horizontal-padding", &horizontal_padding,
2535                         NULL);
2536
2537   gtk_widget_get_allocation (widget, &allocation);
2538
2539   attributes.x = border_width + padding.left + horizontal_padding;
2540   attributes.y = border_width + padding.top + vertical_padding;
2541   attributes.width = allocation.width -
2542     (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2543   attributes.height = allocation.height -
2544     (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2545
2546   get_arrows_border (menu, &arrow_border);
2547   attributes.y += arrow_border.top;
2548   attributes.height -= arrow_border.top;
2549   attributes.height -= arrow_border.bottom;
2550
2551   attributes.width = MAX (1, attributes.width);
2552   attributes.height = MAX (1, attributes.height);
2553
2554   priv->view_window = gdk_window_new (window,
2555                                       &attributes, attributes_mask);
2556   gdk_window_set_user_data (priv->view_window, menu);
2557
2558   gtk_widget_get_allocation (widget, &allocation);
2559
2560   attributes.x = 0;
2561   attributes.y = 0;
2562   attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2563     padding.left + padding.right;
2564   attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2565     padding.top + padding.bottom;
2566
2567   attributes.width = MAX (1, attributes.width);
2568   attributes.height = MAX (1, attributes.height);
2569
2570   priv->bin_window = gdk_window_new (priv->view_window,
2571                                      &attributes, attributes_mask);
2572   gdk_window_set_user_data (priv->bin_window, menu);
2573
2574   children = GTK_MENU_SHELL (menu)->priv->children;
2575   while (children)
2576     {
2577       child = children->data;
2578       children = children->next;
2579
2580       gtk_widget_set_parent_window (child, priv->bin_window);
2581     }
2582
2583   gtk_style_context_set_background (context, priv->bin_window);
2584   gtk_style_context_set_background (context, priv->view_window);
2585   gtk_style_context_set_background (context, window);
2586
2587   if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2588     gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2589                                   GTK_MENU_SHELL (widget)->priv->active_menu_item);
2590
2591   gdk_window_show (priv->bin_window);
2592   gdk_window_show (priv->view_window);
2593 }
2594
2595 static gboolean
2596 gtk_menu_focus (GtkWidget       *widget,
2597                 GtkDirectionType direction)
2598 {
2599   /* A menu or its menu items cannot have focus */
2600   return FALSE;
2601 }
2602
2603 /* See notes in gtk_menu_popup() for information
2604  * about the "grab transfer window"
2605  */
2606 static GdkWindow *
2607 menu_grab_transfer_window_get (GtkMenu *menu)
2608 {
2609   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2610   if (!window)
2611     {
2612       GdkWindowAttr attributes;
2613       gint attributes_mask;
2614
2615       attributes.x = -100;
2616       attributes.y = -100;
2617       attributes.width = 10;
2618       attributes.height = 10;
2619       attributes.window_type = GDK_WINDOW_TEMP;
2620       attributes.wclass = GDK_INPUT_ONLY;
2621       attributes.override_redirect = TRUE;
2622       attributes.event_mask = 0;
2623
2624       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2625
2626       window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2627                                &attributes, attributes_mask);
2628       gdk_window_set_user_data (window, menu);
2629
2630       gdk_window_show (window);
2631
2632       g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2633     }
2634
2635   return window;
2636 }
2637
2638 static void
2639 menu_grab_transfer_window_destroy (GtkMenu *menu)
2640 {
2641   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2642   if (window)
2643     {
2644       gdk_window_set_user_data (window, NULL);
2645       gdk_window_destroy (window);
2646       g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2647     }
2648 }
2649
2650 static void
2651 gtk_menu_unrealize (GtkWidget *widget)
2652 {
2653   GtkMenu *menu = GTK_MENU (widget);
2654   GtkMenuPrivate *priv = menu->priv;
2655
2656   menu_grab_transfer_window_destroy (menu);
2657
2658   gdk_window_set_user_data (priv->view_window, NULL);
2659   gdk_window_destroy (priv->view_window);
2660   priv->view_window = NULL;
2661
2662   gdk_window_set_user_data (priv->bin_window, NULL);
2663   gdk_window_destroy (priv->bin_window);
2664   priv->bin_window = NULL;
2665
2666   GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2667 }
2668
2669 static gint
2670 calculate_line_heights (GtkMenu *menu,
2671                         gint     for_width,
2672                         guint  **ret_min_heights,
2673                         guint  **ret_nat_heights)
2674 {
2675   GtkBorder       padding;
2676   GtkMenuPrivate *priv;
2677   GtkMenuShell   *menu_shell;
2678   GtkWidget      *child, *widget;
2679   GList          *children;
2680   guint           horizontal_padding;
2681   guint           border_width;
2682   guint           n_columns;
2683   gint            n_heights;
2684   guint          *min_heights;
2685   guint          *nat_heights;
2686   gint            avail_width;
2687
2688   priv         = menu->priv;
2689   widget       = GTK_WIDGET (menu);
2690   menu_shell   = GTK_MENU_SHELL (widget);
2691
2692   min_heights  = g_new0 (guint, gtk_menu_get_n_rows (menu));
2693   nat_heights  = g_new0 (guint, gtk_menu_get_n_rows (menu));
2694   n_heights    = gtk_menu_get_n_rows (menu);
2695   n_columns    = gtk_menu_get_n_columns (menu);
2696   avail_width  = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2697
2698   gtk_widget_style_get (GTK_WIDGET (menu),
2699                         "horizontal-padding", &horizontal_padding,
2700                         NULL);
2701   get_menu_padding (widget, &padding);
2702
2703   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2704   avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2705
2706   for (children = menu_shell->priv->children; children; children = children->next)
2707     {
2708       gint part;
2709       gint toggle_size;
2710       gint l, r, t, b;
2711       gint child_min, child_nat;
2712
2713       child = children->data;
2714
2715       if (!gtk_widget_get_visible (child))
2716         continue;
2717
2718       get_effective_child_attach (child, &l, &r, &t, &b);
2719
2720       part = avail_width / (r - l);
2721
2722       gtk_widget_get_preferred_height_for_width (child, part,
2723                                                  &child_min, &child_nat);
2724
2725       gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2726
2727       part = MAX (child_min, toggle_size) / (b - t);
2728       min_heights[t] = MAX (min_heights[t], part);
2729
2730       part = MAX (child_nat, toggle_size) / (b - t);
2731       nat_heights[t] = MAX (nat_heights[t], part);
2732     }
2733
2734   if (ret_min_heights)
2735     *ret_min_heights = min_heights;
2736   else
2737     g_free (min_heights);
2738
2739   if (ret_nat_heights)
2740     *ret_nat_heights = nat_heights;
2741   else
2742     g_free (nat_heights);
2743
2744   return n_heights;
2745 }
2746
2747 static void
2748 gtk_menu_size_allocate (GtkWidget     *widget,
2749                         GtkAllocation *allocation)
2750 {
2751   GtkMenu *menu;
2752   GtkMenuPrivate *priv;
2753   GtkMenuShell *menu_shell;
2754   GtkWidget *child;
2755   GtkAllocation child_allocation;
2756   GList *children;
2757   gint x, y, i;
2758   gint width, height;
2759   guint border_width;
2760   guint vertical_padding;
2761   guint horizontal_padding;
2762   GtkBorder padding;
2763
2764   g_return_if_fail (GTK_IS_MENU (widget));
2765   g_return_if_fail (allocation != NULL);
2766
2767   menu = GTK_MENU (widget);
2768   menu_shell = GTK_MENU_SHELL (widget);
2769   priv = menu->priv;
2770
2771   gtk_widget_set_allocation (widget, allocation);
2772
2773   gtk_widget_style_get (GTK_WIDGET (menu),
2774                         "vertical-padding", &vertical_padding,
2775                         "horizontal-padding", &horizontal_padding,
2776                         NULL);
2777
2778   get_menu_padding (widget, &padding);
2779   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2780
2781   g_free (priv->heights);
2782   priv->heights_length = calculate_line_heights (menu,
2783                                                  allocation->width,
2784                                                  &priv->heights,
2785                                                  NULL);
2786
2787   /* refresh our cached height request */
2788   priv->requested_height = (2 * (border_width + vertical_padding)) +
2789     padding.top + padding.bottom;
2790   for (i = 0; i < priv->heights_length; i++)
2791     priv->requested_height += priv->heights[i];
2792
2793   x = border_width + padding.left + horizontal_padding;
2794   y = border_width + padding.top + vertical_padding;
2795   width = allocation->width - (2 * (border_width + horizontal_padding)) -
2796     padding.left - padding.right;
2797   height = allocation->height - (2 * (border_width + vertical_padding)) -
2798     padding.top - padding.bottom;
2799
2800   if (menu_shell->priv->active)
2801     gtk_menu_scroll_to (menu, priv->scroll_offset);
2802
2803   if (!priv->tearoff_active)
2804     {
2805       GtkBorder arrow_border;
2806
2807       get_arrows_border (menu, &arrow_border);
2808       y += arrow_border.top;
2809       height -= arrow_border.top;
2810       height -= arrow_border.bottom;
2811     }
2812
2813   width = MAX (1, width);
2814   height = MAX (1, height);
2815
2816   if (gtk_widget_get_realized (widget))
2817     {
2818       gdk_window_move_resize (gtk_widget_get_window (widget),
2819                               allocation->x, allocation->y,
2820                               allocation->width, allocation->height);
2821
2822       gdk_window_move_resize (priv->view_window, x, y, width, height);
2823     }
2824
2825   if (menu_shell->priv->children)
2826     {
2827       gint base_width = width / gtk_menu_get_n_columns (menu);
2828
2829       children = menu_shell->priv->children;
2830       while (children)
2831         {
2832           child = children->data;
2833           children = children->next;
2834
2835           if (gtk_widget_get_visible (child))
2836             {
2837               gint i;
2838               gint l, r, t, b;
2839
2840               get_effective_child_attach (child, &l, &r, &t, &b);
2841
2842               if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2843                 {
2844                   guint tmp;
2845                   tmp = gtk_menu_get_n_columns (menu) - l;
2846                   l = gtk_menu_get_n_columns (menu) - r;
2847                   r = tmp;
2848                 }
2849
2850               child_allocation.width = (r - l) * base_width;
2851               child_allocation.height = 0;
2852               child_allocation.x = l * base_width;
2853               child_allocation.y = 0;
2854
2855               for (i = 0; i < b; i++)
2856                 {
2857                   if (i < t)
2858                     child_allocation.y += priv->heights[i];
2859                   else
2860                     child_allocation.height += priv->heights[i];
2861                 }
2862
2863               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2864                                                   priv->toggle_size);
2865
2866               gtk_widget_size_allocate (child, &child_allocation);
2867               gtk_widget_queue_draw (child);
2868             }
2869         }
2870
2871       /* Resize the item window */
2872       if (gtk_widget_get_realized (widget))
2873         {
2874           gint i;
2875           gint width, height;
2876
2877           height = 0;
2878           for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2879             height += priv->heights[i];
2880
2881           width = gtk_menu_get_n_columns (menu) * base_width;
2882           gdk_window_resize (priv->bin_window, width, height);
2883         }
2884
2885       if (priv->tearoff_active)
2886         {
2887           if (height >= priv->requested_height)
2888             {
2889               if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2890                 {
2891                   gtk_widget_hide (priv->tearoff_scrollbar);
2892                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2893
2894                   gtk_menu_scroll_to (menu, 0);
2895                 }
2896             }
2897           else
2898             {
2899               gtk_adjustment_configure (priv->tearoff_adjustment,
2900                                         gtk_adjustment_get_value (priv->tearoff_adjustment),
2901                                         0,
2902                                         priv->requested_height,
2903                                         gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2904                                         gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2905                                         allocation->height);
2906
2907               if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2908                 {
2909                   gtk_widget_show (priv->tearoff_scrollbar);
2910                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2911                 }
2912             }
2913         }
2914     }
2915 }
2916
2917 static void
2918 get_arrows_visible_area (GtkMenu      *menu,
2919                          GdkRectangle *border,
2920                          GdkRectangle *upper,
2921                          GdkRectangle *lower,
2922                          gint         *arrow_space)
2923 {
2924   GtkArrowPlacement arrow_placement;
2925   GtkWidget *widget = GTK_WIDGET (menu);
2926   guint border_width;
2927   guint vertical_padding;
2928   guint horizontal_padding;
2929   gint scroll_arrow_height;
2930   GtkBorder menu_padding;
2931
2932   gtk_widget_style_get (widget,
2933                         "vertical-padding", &vertical_padding,
2934                         "horizontal-padding", &horizontal_padding,
2935                         "scroll-arrow-vlength", &scroll_arrow_height,
2936                         "arrow-placement", &arrow_placement,
2937                         NULL);
2938
2939   get_menu_padding (widget, &menu_padding);
2940   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2941   border->x = border_width + menu_padding.left + horizontal_padding;
2942   border->y = border_width + menu_padding.top + vertical_padding;
2943   border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2944   border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2945
2946   switch (arrow_placement)
2947     {
2948     case GTK_ARROWS_BOTH:
2949       upper->x = border->x;
2950       upper->y = border->y;
2951       upper->width = border->width - 2 * border->x;
2952       upper->height = scroll_arrow_height;
2953
2954       lower->x = border->x;
2955       lower->y = border->height - border->y - scroll_arrow_height;
2956       lower->width = border->width - 2 * border->x;
2957       lower->height = scroll_arrow_height;
2958       break;
2959
2960     case GTK_ARROWS_START:
2961       upper->x = border->x;
2962       upper->y = border->y;
2963       upper->width = (border->width - 2 * border->x) / 2;
2964       upper->height = scroll_arrow_height;
2965
2966       lower->x = border->x + upper->width;
2967       lower->y = border->y;
2968       lower->width = (border->width - 2 * border->x) / 2;
2969       lower->height = scroll_arrow_height;
2970       break;
2971
2972     case GTK_ARROWS_END:
2973       upper->x = border->x;
2974       upper->y = border->height - border->y - scroll_arrow_height;
2975       upper->width = (border->width - 2 * border->x) / 2;
2976       upper->height = scroll_arrow_height;
2977
2978       lower->x = border->x + upper->width;
2979       lower->y = border->height - border->y - scroll_arrow_height;
2980       lower->width = (border->width - 2 * border->x) / 2;
2981       lower->height = scroll_arrow_height;
2982       break;
2983
2984     default:
2985        g_assert_not_reached();
2986        upper->x = upper->y = upper->width = upper->height = 0;
2987        lower->x = lower->y = lower->width = lower->height = 0;
2988     }
2989
2990   *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
2991 }
2992
2993 static gboolean
2994 gtk_menu_draw (GtkWidget *widget,
2995                cairo_t   *cr)
2996 {
2997   GtkMenu *menu;
2998   GtkMenuPrivate *priv;
2999   GtkStyleContext *context;
3000   GdkRectangle border;
3001   GdkRectangle upper;
3002   GdkRectangle lower;
3003   gint arrow_space;
3004   GtkBorder menu_padding;
3005
3006   menu = GTK_MENU (widget);
3007   priv = menu->priv;
3008   context = gtk_widget_get_style_context (widget);
3009
3010   get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3011   get_menu_padding (widget, &menu_padding);
3012
3013   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3014     {
3015       gfloat arrow_scaling;
3016       gint arrow_size;
3017
3018       gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3019       arrow_size = arrow_scaling * arrow_space;
3020
3021       gtk_render_background (context, cr, 0, 0,
3022                              gtk_widget_get_allocated_width (widget),
3023                              gtk_widget_get_allocated_height (widget));
3024       gtk_render_frame (context, cr, 0, 0,
3025                         gtk_widget_get_allocated_width (widget),
3026                         gtk_widget_get_allocated_height (widget));
3027
3028       gtk_style_context_save (context);
3029       gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3030
3031       if (priv->upper_arrow_visible && !priv->tearoff_active)
3032         {
3033           gtk_style_context_save (context);
3034           gtk_style_context_set_state (context, priv->upper_arrow_state);
3035
3036           gtk_render_background (context, cr,
3037                                  upper.x, upper.y,
3038                                  upper.width, upper.height);
3039           gtk_render_frame (context, cr,
3040                             upper.x, upper.y,
3041                             upper.width, upper.height);
3042
3043           gtk_render_arrow (context, cr, 0,
3044                             upper.x + (upper.width - arrow_size) / 2,
3045                             upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3046                             arrow_size);
3047
3048           gtk_style_context_restore (context);
3049         }
3050
3051       if (priv->lower_arrow_visible && !priv->tearoff_active)
3052         {
3053           gtk_style_context_save (context);
3054           gtk_style_context_set_state (context, priv->lower_arrow_state);
3055
3056           gtk_render_background (context, cr,
3057                                  lower.x, lower.y,
3058                                  lower.width, lower.height);
3059           gtk_render_frame (context, cr,
3060                             lower.x, lower.y,
3061                             lower.width, lower.height);
3062
3063           gtk_render_arrow (context, cr, G_PI,
3064                             lower.x + (lower.width - arrow_size) / 2,
3065                             lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3066                             arrow_size);
3067
3068           gtk_style_context_restore (context);
3069         }
3070
3071       gtk_style_context_restore (context);
3072     }
3073
3074   if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3075     {
3076       gint y = -border.y + priv->scroll_offset;
3077
3078       cairo_save (cr);
3079       gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3080
3081       if (!priv->tearoff_active)
3082         {
3083           GtkBorder arrow_border;
3084
3085           get_arrows_border (menu, &arrow_border);
3086           y -= arrow_border.top;
3087         }
3088
3089       gtk_render_background (context, cr,
3090                              - border.x, y,
3091                              border.width, border.height);
3092       gtk_render_frame (context, cr,
3093                         - border.x, y,
3094                         border.width, border.height);
3095
3096       cairo_restore (cr);
3097     }
3098
3099   GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3100
3101   return FALSE;
3102 }
3103
3104 static void
3105 gtk_menu_show (GtkWidget *widget)
3106 {
3107   GtkMenu *menu = GTK_MENU (widget);
3108
3109   _gtk_menu_refresh_accel_paths (menu, FALSE);
3110
3111   GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3112 }
3113
3114
3115 static void 
3116 gtk_menu_get_preferred_width (GtkWidget *widget,
3117                               gint      *minimum_size,
3118                               gint      *natural_size)
3119 {
3120   GtkMenu        *menu;
3121   GtkMenuShell   *menu_shell;
3122   GtkMenuPrivate *priv;
3123   GtkWidget      *child;
3124   GList          *children;
3125   guint           max_toggle_size;
3126   guint           max_accel_width;
3127   guint           horizontal_padding;
3128   guint           border_width;
3129   gint            child_min, child_nat;
3130   gint            min_width, nat_width;
3131   GtkBorder       padding;
3132
3133   menu       = GTK_MENU (widget);
3134   menu_shell = GTK_MENU_SHELL (widget);
3135   priv       = menu->priv;
3136
3137   min_width = nat_width = 0;
3138
3139   max_toggle_size = 0;
3140   max_accel_width = 0;
3141
3142   children = menu_shell->priv->children;
3143   while (children)
3144     {
3145       gint part;
3146       gint toggle_size;
3147       gint l, r, t, b;
3148
3149       child = children->data;
3150       children = children->next;
3151
3152       if (! gtk_widget_get_visible (child))
3153         continue;
3154
3155       get_effective_child_attach (child, &l, &r, &t, &b);
3156
3157       /* It's important to size_request the child
3158        * before doing the toggle size request, in
3159        * case the toggle size request depends on the size
3160        * request of a child of the child (e.g. for ImageMenuItem)
3161        */
3162        gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3163
3164        gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3165        max_toggle_size = MAX (max_toggle_size, toggle_size);
3166        max_accel_width = MAX (max_accel_width,
3167                               GTK_MENU_ITEM (child)->priv->accelerator_width);
3168
3169        part = child_min / (r - l);
3170        min_width = MAX (min_width, part);
3171
3172        part = child_nat / (r - l);
3173        nat_width = MAX (nat_width, part);
3174     }
3175
3176   /* If the menu doesn't include any images or check items
3177    * reserve the space so that all menus are consistent.
3178    * We only do this for 'ordinary' menus, not for combobox
3179    * menus or multi-column menus
3180    */
3181   if (max_toggle_size == 0 &&
3182       gtk_menu_get_n_columns (menu) == 1 &&
3183       !priv->no_toggle_size)
3184     {
3185       GtkStyleContext *context;
3186       GtkWidgetPath *menu_path, *check_path;
3187       guint toggle_spacing;
3188       guint indicator_size;
3189
3190       context = gtk_widget_get_style_context (widget);
3191       menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3192
3193       /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3194       check_path = gtk_widget_path_copy (menu_path);
3195       gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3196
3197       gtk_style_context_set_path (context, check_path);
3198       gtk_widget_path_free (check_path);
3199
3200       gtk_style_context_get_style (context,
3201                                    "toggle-spacing", &toggle_spacing,
3202                                    "indicator-size", &indicator_size,
3203                                    NULL);
3204
3205       max_toggle_size = indicator_size + toggle_spacing;
3206
3207       /* Restore real widget path */
3208       gtk_style_context_set_path (context, menu_path);
3209       gtk_widget_path_free (menu_path);
3210     }
3211
3212   min_width += 2 * max_toggle_size + max_accel_width;
3213   min_width *= gtk_menu_get_n_columns (menu);
3214
3215   nat_width += 2 * max_toggle_size + max_accel_width;
3216   nat_width *= gtk_menu_get_n_columns (menu);
3217
3218   gtk_widget_style_get (GTK_WIDGET (menu),
3219                         "horizontal-padding", &horizontal_padding,
3220                         NULL);
3221
3222   get_menu_padding (widget, &padding);
3223   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3224   min_width   += (2 * (border_width + horizontal_padding)) +
3225     padding.left + padding.right;
3226   nat_width   += (2 * (border_width + horizontal_padding)) +
3227     padding.left + padding.right;
3228
3229   priv->toggle_size = max_toggle_size;
3230   priv->accel_size  = max_accel_width;
3231
3232   if (minimum_size)
3233     *minimum_size = min_width;
3234
3235   if (natural_size)
3236     *natural_size = nat_width;
3237
3238   /* Don't resize the tearoff if it is not active,
3239    * because it won't redraw (it is only a background pixmap).
3240    */
3241   if (priv->tearoff_active)
3242     gtk_menu_set_tearoff_hints (menu, min_width);
3243 }
3244
3245 static void
3246 gtk_menu_get_preferred_height (GtkWidget *widget,
3247                                gint      *minimum_size,
3248                                gint      *natural_size)
3249 {
3250   gint min_width;
3251
3252   /* Menus are height-for-width only, just return the height
3253    * for the minimum width
3254    */
3255   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3256   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3257 }
3258
3259 static void
3260 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3261                                          gint       for_size,
3262                                          gint      *minimum_size,
3263                                          gint      *natural_size)
3264 {
3265   GtkBorder       padding;
3266   GtkMenu        *menu = GTK_MENU (widget);
3267   GtkMenuPrivate *priv = menu->priv;
3268   guint          *min_heights, *nat_heights;
3269   guint           vertical_padding, border_width;
3270   gint            n_heights, i;
3271   gint            min_height, nat_height;
3272
3273   gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3274   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3275   get_menu_padding (widget, &padding);
3276
3277   min_height = nat_height = (border_width + vertical_padding) * 2 +
3278           padding.top + padding.bottom;
3279
3280   n_heights =
3281     calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3282
3283   for (i = 0; i < n_heights; i++)
3284     {
3285       min_height += min_heights[i];
3286       nat_height += nat_heights[i];
3287     }
3288
3289   if (priv->have_position)
3290     {
3291       GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3292       GdkRectangle monitor;
3293
3294       gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
3295
3296       if (priv->position_y + min_height > monitor.y + monitor.height)
3297         min_height = monitor.y + monitor.height - priv->position_y;
3298
3299       if (priv->position_y + nat_height > monitor.y + monitor.height)
3300         nat_height = monitor.y + monitor.height - priv->position_y;
3301
3302       if (priv->position_y < monitor.y)
3303         {
3304           min_height -= monitor.y - priv->position_y;
3305           nat_height -= monitor.y - priv->position_y;
3306         }
3307     }
3308
3309   if (minimum_size)
3310     *minimum_size = min_height;
3311
3312   if (natural_size)
3313     *natural_size = nat_height;
3314
3315   g_free (min_heights);
3316   g_free (nat_heights);
3317 }
3318
3319
3320
3321 static gboolean
3322 gtk_menu_button_scroll (GtkMenu        *menu,
3323                         GdkEventButton *event)
3324 {
3325   GtkMenuPrivate *priv = menu->priv;
3326
3327   if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3328     {
3329       gboolean touchscreen_mode;
3330
3331       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3332                     "gtk-touchscreen-mode", &touchscreen_mode,
3333                     NULL);
3334
3335       if (touchscreen_mode)
3336         gtk_menu_handle_scrolling (menu,
3337                                    event->x_root, event->y_root,
3338                                    event->type == GDK_BUTTON_PRESS,
3339                                    FALSE);
3340
3341       return TRUE;
3342     }
3343
3344   return FALSE;
3345 }
3346
3347 static gboolean
3348 pointer_in_menu_window (GtkWidget *widget,
3349                         gdouble    x_root,
3350                         gdouble    y_root)
3351 {
3352   GtkMenu *menu = GTK_MENU (widget);
3353   GtkMenuPrivate *priv = menu->priv;
3354   GtkAllocation allocation;
3355
3356   if (gtk_widget_get_mapped (priv->toplevel))
3357     {
3358       GtkMenuShell *menu_shell;
3359       gint          window_x, window_y;
3360
3361       gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3362                                &window_x, &window_y);
3363
3364       gtk_widget_get_allocation (widget, &allocation);
3365       if (x_root >= window_x && x_root < window_x + allocation.width &&
3366           y_root >= window_y && y_root < window_y + allocation.height)
3367         return TRUE;
3368
3369       menu_shell = GTK_MENU_SHELL (widget);
3370
3371       if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3372         return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3373                                        x_root, y_root);
3374     }
3375
3376   return FALSE;
3377 }
3378
3379 static gboolean
3380 gtk_menu_button_press (GtkWidget      *widget,
3381                        GdkEventButton *event)
3382 {
3383   if (event->type != GDK_BUTTON_PRESS)
3384     return FALSE;
3385
3386   /* Don't pass down to menu shell for presses over scroll arrows
3387    */
3388   if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3389     return TRUE;
3390
3391   /*  Don't pass down to menu shell if a non-menuitem part of the menu
3392    *  was clicked. The check for the event_widget being a GtkMenuShell
3393    *  works because we have the pointer grabbed on menu_shell->window
3394    *  with owner_events=TRUE, so all events that are either outside
3395    *  the menu or on its border are delivered relative to
3396    *  menu_shell->window.
3397    */
3398   if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3399       pointer_in_menu_window (widget, event->x_root, event->y_root))
3400     return TRUE;
3401
3402   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3403 }
3404
3405 static gboolean
3406 gtk_menu_button_release (GtkWidget      *widget,
3407                          GdkEventButton *event)
3408 {
3409   GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3410
3411   if (priv->ignore_button_release)
3412     {
3413       priv->ignore_button_release = FALSE;
3414       return FALSE;
3415     }
3416
3417   if (event->type != GDK_BUTTON_RELEASE)
3418     return FALSE;
3419
3420   /* Don't pass down to menu shell for releases over scroll arrows
3421    */
3422   if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3423     return TRUE;
3424
3425   /*  Don't pass down to menu shell if a non-menuitem part of the menu
3426    *  was clicked (see comment in button_press()).
3427    */
3428   if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3429       pointer_in_menu_window (widget, event->x_root, event->y_root))
3430     {
3431       /*  Ugly: make sure menu_shell->button gets reset to 0 when we
3432        *  bail out early here so it is in a consistent state for the
3433        *  next button_press/button_release in GtkMenuShell.
3434        *  See bug #449371.
3435        */
3436       if (GTK_MENU_SHELL (widget)->priv->active)
3437         GTK_MENU_SHELL (widget)->priv->button = 0;
3438
3439       return TRUE;
3440     }
3441
3442   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3443 }
3444
3445 static const gchar *
3446 get_accel_path (GtkWidget *menu_item,
3447                 gboolean  *locked)
3448 {
3449   const gchar *path;
3450   GtkWidget *label;
3451   GClosure *accel_closure;
3452   GtkAccelGroup *accel_group;
3453
3454   path = _gtk_widget_get_accel_path (menu_item, locked);
3455   if (!path)
3456     {
3457       path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3458
3459       if (locked)
3460         {
3461           *locked = TRUE;
3462
3463           label = gtk_bin_get_child (GTK_BIN (menu_item));
3464
3465           if (GTK_IS_ACCEL_LABEL (label))
3466             {
3467               g_object_get (label,
3468                             "accel-closure", &accel_closure,
3469                             NULL);
3470               if (accel_closure)
3471                 {
3472                   accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3473
3474                   *locked = gtk_accel_group_get_is_locked (accel_group);
3475                 }
3476             }
3477         }
3478     }
3479
3480   return path;
3481 }
3482
3483 static gboolean
3484 gtk_menu_key_press (GtkWidget   *widget,
3485                     GdkEventKey *event)
3486 {
3487   GtkMenuShell *menu_shell;
3488   GtkMenu *menu;
3489   gboolean delete = FALSE;
3490   gboolean can_change_accels;
3491   gchar *accel = NULL;
3492   guint accel_key, accel_mods;
3493   GdkModifierType consumed_modifiers;
3494   GdkDisplay *display;
3495
3496   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3497   g_return_val_if_fail (event != NULL, FALSE);
3498
3499   menu_shell = GTK_MENU_SHELL (widget);
3500   menu = GTK_MENU (widget);
3501
3502   gtk_menu_stop_navigating_submenu (menu);
3503
3504   if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3505     return TRUE;
3506
3507   display = gtk_widget_get_display (widget);
3508
3509   g_object_get (gtk_widget_get_settings (widget),
3510                 "gtk-menu-bar-accel", &accel,
3511                 "gtk-can-change-accels", &can_change_accels,
3512                 NULL);
3513
3514   if (accel && *accel)
3515     {
3516       guint keyval = 0;
3517       GdkModifierType mods = 0;
3518
3519       gtk_accelerator_parse (accel, &keyval, &mods);
3520
3521       if (keyval == 0)
3522         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3523
3524       /* FIXME this is wrong, needs to be in the global accel resolution
3525        * thing, to properly consider i18n etc., but that probably requires
3526        * AccelGroup changes etc.
3527        */
3528       if (event->keyval == keyval && (mods & event->state) == mods)
3529         {
3530           gtk_menu_shell_cancel (menu_shell);
3531           g_free (accel);
3532           return TRUE;
3533         }
3534     }
3535
3536   g_free (accel);
3537
3538   switch (event->keyval)
3539     {
3540     case GDK_KEY_Delete:
3541     case GDK_KEY_KP_Delete:
3542     case GDK_KEY_BackSpace:
3543       delete = TRUE;
3544       break;
3545     default:
3546       break;
3547     }
3548
3549   /* Figure out what modifiers went into determining the key symbol */
3550   _gtk_translate_keyboard_accel_state (gdk_keymap_get_for_display (display),
3551                                        event->hardware_keycode,
3552                                        event->state,
3553                                        gtk_accelerator_get_default_mod_mask (),
3554                                        event->group,
3555                                        &accel_key, NULL, NULL, &consumed_modifiers);
3556
3557   accel_key = gdk_keyval_to_lower (accel_key);
3558   accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3559
3560   /* If lowercasing affects the keysym, then we need to include SHIFT
3561    * in the modifiers, We re-upper case when we match against the
3562    * keyval, but display and save in caseless form.
3563    */
3564   if (accel_key != event->keyval)
3565     accel_mods |= GDK_SHIFT_MASK;
3566
3567   /* Modify the accelerators */
3568   if (can_change_accels &&
3569       menu_shell->priv->active_menu_item &&
3570       gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3571       GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL &&  /* no submenus */
3572       (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3573     {
3574       GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3575       gboolean locked, replace_accels = TRUE;
3576       const gchar *path;
3577
3578       path = get_accel_path (menu_item, &locked);
3579       if (!path || locked)
3580         {
3581           /* Can't change accelerators on menu_items without paths
3582            * (basically, those items are accelerator-locked).
3583            */
3584           gtk_widget_error_bell (widget);
3585         }
3586       else
3587         {
3588           gboolean changed;
3589
3590           /* For the keys that act to delete the current setting,
3591            * we delete the current setting if there is one, otherwise,
3592            * we set the key as the accelerator.
3593            */
3594           if (delete)
3595             {
3596               GtkAccelKey key;
3597
3598               if (gtk_accel_map_lookup_entry (path, &key) &&
3599                   (key.accel_key || key.accel_mods))
3600                 {
3601                   accel_key = 0;
3602                   accel_mods = 0;
3603                 }
3604             }
3605           changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3606
3607           if (!changed)
3608             {
3609               /* We failed, probably because this key is in use
3610                * and locked already.
3611                */
3612               gtk_widget_error_bell (widget);
3613             }
3614         }
3615     }
3616
3617   return TRUE;
3618 }
3619
3620 static gboolean
3621 check_threshold (GtkWidget *widget,
3622                  gint       start_x,
3623                  gint       start_y,
3624                  gint       x,
3625                  gint       y)
3626 {
3627 #define THRESHOLD 8
3628
3629   return
3630     ABS (start_x - x) > THRESHOLD ||
3631     ABS (start_y - y) > THRESHOLD;
3632 }
3633
3634 static gboolean
3635 definitely_within_item (GtkWidget *widget,
3636                         gint       x,
3637                         gint       y)
3638 {
3639   GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3640   int w, h;
3641
3642   w = gdk_window_get_width (window);
3643   h = gdk_window_get_height (window);
3644
3645   return
3646     check_threshold (widget, 0, 0, x, y) &&
3647     check_threshold (widget, w - 1, 0, x, y) &&
3648     check_threshold (widget, w - 1, h - 1, x, y) &&
3649     check_threshold (widget, 0, h - 1, x, y);
3650 }
3651
3652 static gboolean
3653 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3654 {
3655   GtkMenuPrivate *priv = menu->priv;
3656
3657   return priv->navigation_height && priv->navigation_width;
3658 }
3659
3660 static gboolean
3661 gtk_menu_motion_notify (GtkWidget      *widget,
3662                         GdkEventMotion *event)
3663 {
3664   GtkWidget *menu_item;
3665   GtkMenu *menu;
3666   GtkMenuShell *menu_shell;
3667   GtkWidget *parent;
3668
3669   gboolean need_enter;
3670
3671   if (GTK_IS_MENU (widget))
3672     {
3673       GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3674
3675       if (priv->ignore_button_release)
3676         priv->ignore_button_release = FALSE;
3677
3678       gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3679                                  TRUE, TRUE);
3680     }
3681
3682   /* We received the event for one of two reasons:
3683    *
3684    * a) We are the active menu, and did gtk_grab_add()
3685    * b) The widget is a child of ours, and the event was propagated
3686    *
3687    * Since for computation of navigation regions, we want the menu which
3688    * is the parent of the menu item, for a), we need to find that menu,
3689    * which may be different from 'widget'.
3690    */
3691   menu_item = gtk_get_event_widget ((GdkEvent*) event);
3692   parent = gtk_widget_get_parent (menu_item);
3693   if (!GTK_IS_MENU_ITEM (menu_item) ||
3694       !GTK_IS_MENU (parent))
3695     return FALSE;
3696
3697   menu_shell = GTK_MENU_SHELL (parent);
3698   menu = GTK_MENU (menu_shell);
3699
3700   if (definitely_within_item (menu_item, event->x, event->y))
3701     menu_shell->priv->activate_time = 0;
3702
3703   need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3704
3705   /* Check to see if we are within an active submenu's navigation region
3706    */
3707   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3708     return TRUE;
3709
3710   /* Make sure we pop down if we enter a non-selectable menu item, so we
3711    * don't show a submenu when the cursor is outside the stay-up triangle.
3712    */
3713   if (!_gtk_menu_item_is_selectable (menu_item))
3714     {
3715       /* We really want to deselect, but this gives the menushell code
3716        * a chance to do some bookkeeping about the menuitem.
3717        */
3718       gtk_menu_shell_select_item (menu_shell, menu_item);
3719       return FALSE;
3720     }
3721
3722   if (need_enter)
3723     {
3724       /* The menu is now sensitive to enter events on its items, but
3725        * was previously sensitive.  So we fake an enter event.
3726        */
3727       menu_shell->priv->ignore_enter = FALSE;
3728
3729       if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3730           event->y >= 0 && event->y < gdk_window_get_height (event->window))
3731         {
3732           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3733           gboolean result;
3734
3735           send_event->crossing.window = g_object_ref (event->window);
3736           send_event->crossing.time = event->time;
3737           send_event->crossing.send_event = TRUE;
3738           send_event->crossing.x_root = event->x_root;
3739           send_event->crossing.y_root = event->y_root;
3740           send_event->crossing.x = event->x;
3741           send_event->crossing.y = event->y;
3742           send_event->crossing.state = event->state;
3743           gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3744
3745           /* We send the event to 'widget', the currently active menu,
3746            * instead of 'menu', the menu that the pointer is in. This
3747            * will ensure that the event will be ignored unless the
3748            * menuitem is a child of the active menu or some parent
3749            * menu of the active menu.
3750            */
3751           result = gtk_widget_event (widget, send_event);
3752           gdk_event_free (send_event);
3753
3754           return result;
3755         }
3756     }
3757
3758   return FALSE;
3759 }
3760
3761 static gboolean
3762 get_double_arrows (GtkMenu *menu)
3763 {
3764   GtkMenuPrivate   *priv = menu->priv;
3765   gboolean          double_arrows;
3766   GtkArrowPlacement arrow_placement;
3767
3768   gtk_widget_style_get (GTK_WIDGET (menu),
3769                         "double-arrows", &double_arrows,
3770                         "arrow-placement", &arrow_placement,
3771                         NULL);
3772
3773   if (arrow_placement != GTK_ARROWS_BOTH)
3774     return TRUE;
3775
3776   return double_arrows || (priv->initially_pushed_in &&
3777                            priv->scroll_offset != 0);
3778 }
3779
3780 static void
3781 gtk_menu_scroll_by (GtkMenu *menu,
3782                     gint     step)
3783 {
3784   GtkMenuPrivate *priv = menu->priv;
3785   GtkBorder arrow_border;
3786   GtkWidget *widget;
3787   gint offset;
3788   gint view_height;
3789   gboolean double_arrows;
3790
3791   widget = GTK_WIDGET (menu);
3792   offset = priv->scroll_offset + step;
3793
3794   get_arrows_border (menu, &arrow_border);
3795
3796   double_arrows = get_double_arrows (menu);
3797
3798   /* If we scroll upward and the non-visible top part
3799    * is smaller than the scroll arrow it would be
3800    * pretty stupid to show the arrow and taking more
3801    * screen space than just scrolling to the top.
3802    */
3803   if (!double_arrows)
3804     if ((step < 0) && (offset < arrow_border.top))
3805       offset = 0;
3806
3807   /* Don't scroll over the top if we weren't before: */
3808   if ((priv->scroll_offset >= 0) && (offset < 0))
3809     offset = 0;
3810
3811   view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3812
3813   if (priv->scroll_offset == 0 &&
3814       view_height >= priv->requested_height)
3815     return;
3816
3817   /* Don't scroll past the bottom if we weren't before: */
3818   if (priv->scroll_offset > 0)
3819     view_height -= arrow_border.top;
3820
3821   /* When both arrows are always shown,
3822    * reduce view height even more.
3823    */
3824   if (double_arrows)
3825     view_height -= arrow_border.bottom;
3826
3827   if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3828       (offset + view_height > priv->requested_height))
3829     offset = priv->requested_height - view_height;
3830
3831   if (offset != priv->scroll_offset)
3832     gtk_menu_scroll_to (menu, offset);
3833 }
3834
3835 static void
3836 gtk_menu_do_timeout_scroll (GtkMenu  *menu,
3837                             gboolean  touchscreen_mode)
3838 {
3839   GtkMenuPrivate *priv = menu->priv;
3840   gboolean upper_visible;
3841   gboolean lower_visible;
3842
3843   upper_visible = priv->upper_arrow_visible;
3844   lower_visible = priv->lower_arrow_visible;
3845
3846   gtk_menu_scroll_by (menu, priv->scroll_step);
3847
3848   if (touchscreen_mode &&
3849       (upper_visible != priv->upper_arrow_visible ||
3850        lower_visible != priv->lower_arrow_visible))
3851     {
3852       /* We are about to hide a scroll arrow while the mouse is pressed,
3853        * this would cause the uncovered menu item to be activated on button
3854        * release. Therefore we need to ignore button release here
3855        */
3856       GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3857       priv->ignore_button_release = TRUE;
3858     }
3859 }
3860
3861 static gboolean
3862 gtk_menu_scroll_timeout (gpointer data)
3863 {
3864   GtkMenu  *menu;
3865   gboolean  touchscreen_mode;
3866
3867   menu = GTK_MENU (data);
3868
3869   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3870                 "gtk-touchscreen-mode", &touchscreen_mode,
3871                 NULL);
3872
3873   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3874
3875   return TRUE;
3876 }
3877
3878 static gboolean
3879 gtk_menu_scroll_timeout_initial (gpointer data)
3880 {
3881   GtkMenu  *menu;
3882   guint     timeout;
3883   gboolean  touchscreen_mode;
3884
3885   menu = GTK_MENU (data);
3886
3887   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3888                 "gtk-timeout-repeat", &timeout,
3889                 "gtk-touchscreen-mode", &touchscreen_mode,
3890                 NULL);
3891
3892   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3893
3894   gtk_menu_remove_scroll_timeout (menu);
3895
3896   menu->priv->scroll_timeout =
3897     gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3898
3899   return FALSE;
3900 }
3901
3902 static void
3903 gtk_menu_start_scrolling (GtkMenu *menu)
3904 {
3905   guint    timeout;
3906   gboolean touchscreen_mode;
3907
3908   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3909                 "gtk-timeout-repeat", &timeout,
3910                 "gtk-touchscreen-mode", &touchscreen_mode,
3911                 NULL);
3912
3913   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3914
3915   menu->priv->scroll_timeout =
3916     gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3917 }
3918
3919 static gboolean
3920 gtk_menu_scroll (GtkWidget      *widget,
3921                  GdkEventScroll *event)
3922 {
3923   GtkMenu *menu = GTK_MENU (widget);
3924
3925   switch (event->direction)
3926     {
3927     case GDK_SCROLL_RIGHT:
3928     case GDK_SCROLL_DOWN:
3929       gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3930       break;
3931     case GDK_SCROLL_LEFT:
3932     case GDK_SCROLL_UP:
3933       gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3934       break;
3935     }
3936
3937   return TRUE;
3938 }
3939
3940 static void
3941 get_arrows_sensitive_area (GtkMenu      *menu,
3942                            GdkRectangle *upper,
3943                            GdkRectangle *lower)
3944 {
3945   GtkArrowPlacement arrow_placement;
3946   GtkWidget *widget = GTK_WIDGET (menu);
3947   GdkWindow *window;
3948   gint width, height;
3949   guint border;
3950   guint vertical_padding;
3951   gint win_x, win_y;
3952   gint scroll_arrow_height;
3953   GtkBorder padding;
3954
3955   window = gtk_widget_get_window (widget);
3956   width = gdk_window_get_width (window);
3957   height = gdk_window_get_height (window);
3958
3959   gtk_widget_style_get (widget,
3960                         "vertical-padding", &vertical_padding,
3961                         "scroll-arrow-vlength", &scroll_arrow_height,
3962                         "arrow-placement", &arrow_placement,
3963                         NULL);
3964
3965   border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3966   get_menu_padding (widget, &padding);
3967
3968   gdk_window_get_position (window, &win_x, &win_y);
3969
3970   switch (arrow_placement)
3971     {
3972     case GTK_ARROWS_BOTH:
3973       if (upper)
3974         {
3975           upper->x = win_x;
3976           upper->y = win_y;
3977           upper->width = width;
3978           upper->height = scroll_arrow_height + border + padding.top;
3979         }
3980
3981       if (lower)
3982         {
3983           lower->x = win_x;
3984           lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3985           lower->width = width;
3986           lower->height = scroll_arrow_height + border + padding.bottom;
3987         }
3988       break;
3989
3990     case GTK_ARROWS_START:
3991       if (upper)
3992         {
3993           upper->x = win_x;
3994           upper->y = win_y;
3995           upper->width = width / 2;
3996           upper->height = scroll_arrow_height + border + padding.top;
3997         }
3998
3999       if (lower)
4000         {
4001           lower->x = win_x + width / 2;
4002           lower->y = win_y;
4003           lower->width = width / 2;
4004           lower->height = scroll_arrow_height + border + padding.bottom;
4005         }
4006       break;
4007
4008     case GTK_ARROWS_END:
4009       if (upper)
4010         {
4011           upper->x = win_x;
4012           upper->y = win_y + height - border - scroll_arrow_height;
4013           upper->width = width / 2;
4014           upper->height = scroll_arrow_height + border + padding.top;
4015         }
4016
4017       if (lower)
4018         {
4019           lower->x = win_x + width / 2;
4020           lower->y = win_y + height - border - scroll_arrow_height;
4021           lower->width = width / 2;
4022           lower->height = scroll_arrow_height + border + padding.bottom;
4023         }
4024       break;
4025     }
4026 }
4027
4028
4029 static void
4030 gtk_menu_handle_scrolling (GtkMenu *menu,
4031                            gint     x,
4032                            gint     y,
4033                            gboolean enter,
4034                            gboolean motion)
4035 {
4036   GtkMenuPrivate *priv = menu->priv;
4037   GtkMenuShell *menu_shell;
4038   GdkRectangle rect;
4039   gboolean in_arrow;
4040   gboolean scroll_fast = FALSE;
4041   gint top_x, top_y;
4042   gboolean touchscreen_mode;
4043
4044   menu_shell = GTK_MENU_SHELL (menu);
4045
4046   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4047                 "gtk-touchscreen-mode", &touchscreen_mode,
4048                 NULL);
4049
4050   gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4051                            &top_x, &top_y);
4052   x -= top_x;
4053   y -= top_y;
4054
4055   /*  upper arrow handling  */
4056
4057   get_arrows_sensitive_area (menu, &rect, NULL);
4058
4059   in_arrow = FALSE;
4060   if (priv->upper_arrow_visible && !priv->tearoff_active &&
4061       (x >= rect.x) && (x < rect.x + rect.width) &&
4062       (y >= rect.y) && (y < rect.y + rect.height))
4063     {
4064       in_arrow = TRUE;
4065     }
4066
4067   if (touchscreen_mode)
4068     priv->upper_arrow_prelight = in_arrow;
4069
4070   if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4071     {
4072       gboolean arrow_pressed = FALSE;
4073
4074       if (priv->upper_arrow_visible && !priv->tearoff_active)
4075         {
4076           if (touchscreen_mode)
4077             {
4078               if (enter && priv->upper_arrow_prelight)
4079                 {
4080                   if (priv->scroll_timeout == 0)
4081                     {
4082                       /* Deselect the active item so that
4083                        * any submenus are popped down
4084                        */
4085                       gtk_menu_shell_deselect (menu_shell);
4086
4087                       gtk_menu_remove_scroll_timeout (menu);
4088                       priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4089
4090                       if (!motion)
4091                         {
4092                           /* Only do stuff on click. */
4093                           gtk_menu_start_scrolling (menu);
4094                           arrow_pressed = TRUE;
4095                         }
4096                     }
4097                   else
4098                     {
4099                       arrow_pressed = TRUE;
4100                     }
4101                 }
4102               else if (!enter)
4103                 {
4104                   gtk_menu_stop_scrolling (menu);
4105                 }
4106             }
4107           else /* !touchscreen_mode */
4108             {
4109               scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4110
4111               if (enter && in_arrow &&
4112                   (!priv->upper_arrow_prelight ||
4113                    priv->scroll_fast != scroll_fast))
4114                 {
4115                   priv->upper_arrow_prelight = TRUE;
4116                   priv->scroll_fast = scroll_fast;
4117
4118                   /* Deselect the active item so that
4119                    * any submenus are popped down
4120                    */
4121                   gtk_menu_shell_deselect (menu_shell);
4122
4123                   gtk_menu_remove_scroll_timeout (menu);
4124                   priv->scroll_step = scroll_fast
4125                                         ? -MENU_SCROLL_STEP2
4126                                         : -MENU_SCROLL_STEP1;
4127
4128                   priv->scroll_timeout =
4129                     gdk_threads_add_timeout (scroll_fast
4130                                                ? MENU_SCROLL_TIMEOUT2
4131                                                : MENU_SCROLL_TIMEOUT1,
4132                                              gtk_menu_scroll_timeout, menu);
4133                 }
4134               else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4135                 {
4136                   gtk_menu_stop_scrolling (menu);
4137                 }
4138             }
4139         }
4140
4141       /*  gtk_menu_start_scrolling() might have hit the top of the
4142        *  menu, so check if the button isn't insensitive before
4143        *  changing it to something else.
4144        */
4145       if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4146         {
4147           GtkStateFlags arrow_state = 0;
4148
4149           if (arrow_pressed)
4150             arrow_state |= GTK_STATE_FLAG_ACTIVE;
4151
4152           if (priv->upper_arrow_prelight)
4153             arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4154
4155           if (arrow_state != priv->upper_arrow_state)
4156             {
4157               priv->upper_arrow_state = arrow_state;
4158
4159               gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4160                                           &rect, FALSE);
4161             }
4162         }
4163     }
4164
4165   /*  lower arrow handling  */
4166
4167   get_arrows_sensitive_area (menu, NULL, &rect);
4168
4169   in_arrow = FALSE;
4170   if (priv->lower_arrow_visible && !priv->tearoff_active &&
4171       (x >= rect.x) && (x < rect.x + rect.width) &&
4172       (y >= rect.y) && (y < rect.y + rect.height))
4173     {
4174       in_arrow = TRUE;
4175     }
4176
4177   if (touchscreen_mode)
4178     priv->lower_arrow_prelight = in_arrow;
4179
4180   if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4181     {
4182       gboolean arrow_pressed = FALSE;
4183
4184       if (priv->lower_arrow_visible && !priv->tearoff_active)
4185         {
4186           if (touchscreen_mode)
4187             {
4188               if (enter && priv->lower_arrow_prelight)
4189                 {
4190                   if (priv->scroll_timeout == 0)
4191                     {
4192                       /* Deselect the active item so that
4193                        * any submenus are popped down
4194                        */
4195                       gtk_menu_shell_deselect (menu_shell);
4196
4197                       gtk_menu_remove_scroll_timeout (menu);
4198                       priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4199
4200                       if (!motion)
4201                         {
4202                           /* Only do stuff on click. */
4203                           gtk_menu_start_scrolling (menu);
4204                           arrow_pressed = TRUE;
4205                         }
4206                     }
4207                   else
4208                     {
4209                       arrow_pressed = TRUE;
4210                     }
4211                 }
4212               else if (!enter)
4213                 {
4214                   gtk_menu_stop_scrolling (menu);
4215                 }
4216             }
4217           else /* !touchscreen_mode */
4218             {
4219               scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4220
4221               if (enter && in_arrow &&
4222                   (!priv->lower_arrow_prelight ||
4223                    priv->scroll_fast != scroll_fast))
4224                 {
4225                   priv->lower_arrow_prelight = TRUE;
4226                   priv->scroll_fast = scroll_fast;
4227
4228                   /* Deselect the active item so that
4229                    * any submenus are popped down
4230                    */
4231                   gtk_menu_shell_deselect (menu_shell);
4232
4233                   gtk_menu_remove_scroll_timeout (menu);
4234                   priv->scroll_step = scroll_fast
4235                                         ? MENU_SCROLL_STEP2
4236                                         : MENU_SCROLL_STEP1;
4237
4238                   priv->scroll_timeout =
4239                     gdk_threads_add_timeout (scroll_fast
4240                                                ? MENU_SCROLL_TIMEOUT2
4241                                                : MENU_SCROLL_TIMEOUT1,
4242                                              gtk_menu_scroll_timeout, menu);
4243                 }
4244               else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4245                 {
4246                   gtk_menu_stop_scrolling (menu);
4247                 }
4248             }
4249         }
4250
4251       /*  gtk_menu_start_scrolling() might have hit the bottom of the
4252        *  menu, so check if the button isn't insensitive before
4253        *  changing it to something else.
4254        */
4255       if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4256         {
4257           GtkStateFlags arrow_state = 0;
4258
4259           if (arrow_pressed)
4260             arrow_state |= GTK_STATE_FLAG_ACTIVE;
4261
4262           if (priv->lower_arrow_prelight)
4263             arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4264
4265           if (arrow_state != priv->lower_arrow_state)
4266             {
4267               priv->lower_arrow_state = arrow_state;
4268
4269               gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4270                                           &rect, FALSE);
4271             }
4272         }
4273     }
4274 }
4275
4276 static gboolean
4277 gtk_menu_enter_notify (GtkWidget        *widget,
4278                        GdkEventCrossing *event)
4279 {
4280   GtkWidget *menu_item;
4281   GtkWidget *parent;
4282   gboolean   touchscreen_mode;
4283
4284   if (event->mode == GDK_CROSSING_GTK_GRAB ||
4285       event->mode == GDK_CROSSING_GTK_UNGRAB ||
4286       event->mode == GDK_CROSSING_STATE_CHANGED)
4287     return TRUE;
4288
4289   g_object_get (gtk_widget_get_settings (widget),
4290                 "gtk-touchscreen-mode", &touchscreen_mode,
4291                 NULL);
4292
4293   menu_item = gtk_get_event_widget ((GdkEvent*) event);
4294   if (GTK_IS_MENU (widget))
4295     {
4296       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4297
4298       if (!menu_shell->priv->ignore_enter)
4299         gtk_menu_handle_scrolling (GTK_MENU (widget),
4300                                    event->x_root, event->y_root, TRUE, TRUE);
4301     }
4302
4303   if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4304     {
4305       GtkWidget *menu = gtk_widget_get_parent (menu_item);
4306
4307       if (GTK_IS_MENU (menu))
4308         {
4309           GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4310           GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4311
4312           if (priv->seen_item_enter)
4313             {
4314               /* This is the second enter we see for an item
4315                * on this menu. This means a release should always
4316                * mean activate.
4317                */
4318               menu_shell->priv->activate_time = 0;
4319             }
4320           else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4321                     event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4322             {
4323               if (definitely_within_item (menu_item, event->x, event->y))
4324                 {
4325                   /* This is an actual user-enter (ie. not a pop-under)
4326                    * In this case, the user must either have entered
4327                    * sufficiently far enough into the item, or he must move
4328                    * far enough away from the enter point. (see
4329                    * gtk_menu_motion_notify())
4330                    */
4331                   menu_shell->priv->activate_time = 0;
4332                 }
4333             }
4334
4335           priv->seen_item_enter = TRUE;
4336         }
4337     }
4338
4339   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4340    * will not correspond to the event widget's parent.  Check to see
4341    * if we are in the parent's navigation region.
4342    */
4343   parent = gtk_widget_get_parent (menu_item);
4344   if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4345       gtk_menu_navigating_submenu (GTK_MENU (parent),
4346                                    event->x_root, event->y_root))
4347     return TRUE;
4348
4349   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4350 }
4351
4352 static gboolean
4353 gtk_menu_leave_notify (GtkWidget        *widget,
4354                        GdkEventCrossing *event)
4355 {
4356   GtkMenuShell *menu_shell;
4357   GtkMenu *menu;
4358   GtkMenuItem *menu_item;
4359   GtkWidget *event_widget;
4360
4361   if (event->mode == GDK_CROSSING_GTK_GRAB ||
4362       event->mode == GDK_CROSSING_GTK_UNGRAB ||
4363       event->mode == GDK_CROSSING_STATE_CHANGED)
4364     return TRUE;
4365
4366   menu = GTK_MENU (widget);
4367   menu_shell = GTK_MENU_SHELL (widget);
4368
4369   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4370     return TRUE;
4371
4372   gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4373
4374   event_widget = gtk_get_event_widget ((GdkEvent*) event);
4375
4376   if (!GTK_IS_MENU_ITEM (event_widget))
4377     return TRUE;
4378
4379   menu_item = GTK_MENU_ITEM (event_widget);
4380
4381   /* Here we check to see if we're leaving an active menu item
4382    * with a submenu, in which case we enter submenu navigation mode.
4383    */
4384   if (menu_shell->priv->active_menu_item != NULL
4385       && menu_item->priv->submenu != NULL
4386       && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4387     {
4388       if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4389         {
4390           gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4391           return TRUE;
4392         }
4393       else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4394         {
4395           /* We are leaving an active menu item with nonactive submenu.
4396            * Deselect it so we don't surprise the user with by popping
4397            * up a submenu _after_ he left the item.
4398            */
4399           gtk_menu_shell_deselect (menu_shell);
4400           return TRUE;
4401         }
4402     }
4403
4404   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4405 }
4406
4407 static void
4408 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4409 {
4410   GtkMenuPrivate *priv = menu->priv;
4411
4412   priv->navigation_x = 0;
4413   priv->navigation_y = 0;
4414   priv->navigation_width = 0;
4415   priv->navigation_height = 0;
4416
4417   if (priv->navigation_timeout)
4418     {
4419       g_source_remove (priv->navigation_timeout);
4420       priv->navigation_timeout = 0;
4421     }
4422 }
4423
4424 /* When the timeout is elapsed, the navigation region is destroyed
4425  * and the menuitem under the pointer (if any) is selected.
4426  */
4427 static gboolean
4428 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4429 {
4430   GtkMenuPopdownData *popdown_data = user_data;
4431   GtkMenu *menu = popdown_data->menu;
4432   GtkMenuPrivate *priv = menu->priv;
4433   GdkWindow *child_window;
4434
4435   gtk_menu_stop_navigating_submenu (menu);
4436
4437   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4438     {
4439       child_window = gdk_window_get_device_position (priv->bin_window,
4440                                                      popdown_data->device,
4441                                                      NULL, NULL, NULL);
4442
4443       if (child_window)
4444         {
4445           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4446
4447           send_event->crossing.window = g_object_ref (child_window);
4448           send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4449           send_event->crossing.send_event = TRUE;
4450           gdk_event_set_device (send_event, popdown_data->device);
4451
4452           GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4453
4454           gdk_event_free (send_event);
4455         }
4456     }
4457
4458   return FALSE;
4459 }
4460
4461 static gboolean
4462 gtk_menu_navigating_submenu (GtkMenu *menu,
4463                              gint     event_x,
4464                              gint     event_y)
4465 {
4466   GtkMenuPrivate *priv = menu->priv;
4467   gint width, height;
4468
4469   if (!gtk_menu_has_navigation_triangle (menu))
4470     return FALSE;
4471
4472   width = priv->navigation_width;
4473   height = priv->navigation_height;
4474
4475   /* Check if x/y are in the triangle spanned by the navigation parameters */
4476
4477   /* 1) Move the coordinates so the triangle starts at 0,0 */
4478   event_x -= priv->navigation_x;
4479   event_y -= priv->navigation_y;
4480
4481   /* 2) Ensure both legs move along the positive axis */
4482   if (width < 0)
4483     {
4484       event_x = -event_x;
4485       width = -width;
4486     }
4487   if (height < 0)
4488     {
4489       event_y = -event_y;
4490       height = -height;
4491     }
4492
4493   /* 3) Check that the given coordinate is inside the triangle. The formula
4494    * is a transformed form of this formula: x/w + y/h <= 1
4495    */
4496   if (event_x >= 0 && event_y >= 0 &&
4497       event_x * height + event_y * width <= width * height)
4498     {
4499       return TRUE;
4500     }
4501   else
4502     {
4503       gtk_menu_stop_navigating_submenu (menu);
4504       return FALSE;
4505     }
4506 }
4507
4508 static void
4509 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
4510                                         GtkMenuItem      *menu_item,
4511                                         GdkEventCrossing *event)
4512 {
4513   GtkMenuPrivate *priv = menu->priv;
4514   gint submenu_left = 0;
4515   gint submenu_right = 0;
4516   gint submenu_top = 0;
4517   gint submenu_bottom = 0;
4518   gint width = 0;
4519   GtkWidget *event_widget;
4520   GtkMenuPopdownData *popdown_data;
4521   GdkWindow *window;
4522
4523   g_return_if_fail (menu_item->priv->submenu != NULL);
4524   g_return_if_fail (event != NULL);
4525
4526   event_widget = gtk_get_event_widget ((GdkEvent*) event);
4527
4528   window = gtk_widget_get_window (menu_item->priv->submenu);
4529   gdk_window_get_origin (window, &submenu_left, &submenu_top);
4530
4531   submenu_right = submenu_left + gdk_window_get_width (window);
4532   submenu_bottom = submenu_top + gdk_window_get_height (window);
4533
4534   width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4535
4536   if (event->x >= 0 && event->x < width)
4537     {
4538       gint popdown_delay;
4539
4540       gtk_menu_stop_navigating_submenu (menu);
4541
4542       /* The navigation region is the triangle closest to the x/y
4543        * location of the rectangle. This is why the width or height
4544        * can be negative.
4545        */
4546       if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4547         {
4548           /* right */
4549           priv->navigation_x = submenu_left;
4550           priv->navigation_width = event->x_root - submenu_left;
4551         }
4552       else
4553         {
4554           /* left */
4555           priv->navigation_x = submenu_right;
4556           priv->navigation_width = event->x_root - submenu_right;
4557         }
4558
4559       if (event->y < 0)
4560         {
4561           /* top */
4562           priv->navigation_y = event->y_root;
4563           priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4564
4565           if (priv->navigation_height >= 0)
4566             return;
4567         }
4568       else
4569         {
4570           /* bottom */
4571           priv->navigation_y = event->y_root;
4572           priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4573
4574           if (priv->navigation_height <= 0)
4575             return;
4576         }
4577
4578       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4579                     "gtk-menu-popdown-delay", &popdown_delay,
4580                     NULL);
4581
4582       popdown_data = g_new (GtkMenuPopdownData, 1);
4583       popdown_data->menu = menu;
4584       popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4585
4586       priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4587                                                                popdown_delay,
4588                                                                gtk_menu_stop_navigating_submenu_cb,
4589                                                                popdown_data,
4590                                                                (GDestroyNotify) g_free);
4591     }
4592 }
4593
4594 static void
4595 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4596 {
4597   GtkWidget *parent;
4598
4599   g_return_if_fail (GTK_IS_MENU (menu_shell));
4600
4601   parent = menu_shell->priv->parent_menu_shell;
4602
4603   menu_shell->priv->activate_time = 0;
4604   gtk_menu_popdown (GTK_MENU (menu_shell));
4605
4606   if (parent)
4607     gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4608 }
4609
4610 static void
4611 gtk_menu_position (GtkMenu  *menu,
4612                    gboolean  set_scroll_offset)
4613 {
4614   GtkMenuPrivate *priv = menu->priv;
4615   GtkWidget *widget;
4616   GtkRequisition requisition;
4617   gint x, y;
4618   gint scroll_offset;
4619   GdkScreen *screen;
4620   GdkScreen *pointer_screen;
4621   GdkRectangle monitor;
4622   GdkDevice *pointer;
4623
4624   widget = GTK_WIDGET (menu);
4625
4626   screen = gtk_widget_get_screen (widget);
4627   pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4628   gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4629
4630   /* Realize so we have the proper width and heigh to figure out
4631    * the right place to popup the menu.
4632    */
4633   gtk_widget_realize (priv->toplevel);
4634   requisition.width = gtk_widget_get_allocated_width (widget);
4635   requisition.height = gtk_widget_get_allocated_height (widget);
4636
4637   if (pointer_screen != screen)
4638     {
4639       /* Pointer is on a different screen; roughly center the
4640        * menu on the screen. If someone was using multiscreen
4641        * + Xinerama together they'd probably want something
4642        * fancier; but that is likely to be vanishingly rare.
4643        */
4644       x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4645       y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4646     }
4647
4648   priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4649   priv->initially_pushed_in = FALSE;
4650
4651   /* Set the type hint here to allow custom position functions
4652    * to set a different hint
4653    */
4654   if (!gtk_widget_get_visible (priv->toplevel))
4655     gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4656
4657   if (priv->position_func)
4658     {
4659       (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4660                                priv->position_func_data);
4661
4662       if (priv->monitor_num < 0)
4663         priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4664
4665       gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4666     }
4667   else
4668     {
4669       gint space_left, space_right, space_above, space_below;
4670       gint needed_width;
4671       gint needed_height;
4672       GtkBorder padding;
4673       gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4674
4675       get_menu_padding (widget, &padding);
4676
4677       /* The placement of popup menus horizontally works like this (with
4678        * RTL in parentheses)
4679        *
4680        * - If there is enough room to the right (left) of the mouse cursor,
4681        *   position the menu there.
4682        *
4683        * - Otherwise, if if there is enough room to the left (right) of the
4684        *   mouse cursor, position the menu there.
4685        *
4686        * - Otherwise if the menu is smaller than the monitor, position it
4687        *   on the side of the mouse cursor that has the most space available
4688        *
4689        * - Otherwise (if there is simply not enough room for the menu on the
4690        *   monitor), position it as far left (right) as possible.
4691        *
4692        * Positioning in the vertical direction is similar: first try below
4693        * mouse cursor, then above.
4694        */
4695       gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4696
4697       space_left = x - monitor.x;
4698       space_right = monitor.x + monitor.width - x - 1;
4699       space_above = y - monitor.y;
4700       space_below = monitor.y + monitor.height - y - 1;
4701
4702       /* Position horizontally. */
4703
4704       /* the amount of space we need to position the menu.
4705        * Note the menu is offset "thickness" pixels
4706        */
4707       needed_width = requisition.width - padding.left;
4708
4709       if (needed_width <= space_left ||
4710           needed_width <= space_right)
4711         {
4712           if ((rtl  && needed_width <= space_left) ||
4713               (!rtl && needed_width >  space_right))
4714             {
4715               /* position left */
4716               x = x + padding.left - requisition.width + 1;
4717             }
4718           else
4719             {
4720               /* position right */
4721               x = x - padding.right;
4722             }
4723
4724           /* x is clamped on-screen further down */
4725         }
4726       else if (requisition.width <= monitor.width)
4727         {
4728           /* the menu is too big to fit on either side of the mouse
4729            * cursor, but smaller than the monitor. Position it on
4730            * the side that has the most space
4731            */
4732           if (space_left > space_right)
4733             {
4734               /* left justify */
4735               x = monitor.x;
4736             }
4737           else
4738             {
4739               /* right justify */
4740               x = monitor.x + monitor.width - requisition.width;
4741             }
4742         }
4743       else /* menu is simply too big for the monitor */
4744         {
4745           if (rtl)
4746             {
4747               /* right justify */
4748               x = monitor.x + monitor.width - requisition.width;
4749             }
4750           else
4751             {
4752               /* left justify */
4753               x = monitor.x;
4754             }
4755         }
4756
4757       /* Position vertically.
4758        * The algorithm is the same as above, but simpler
4759        * because we don't have to take RTL into account.
4760        */
4761       needed_height = requisition.height - padding.top;
4762
4763       if (needed_height <= space_above ||
4764           needed_height <= space_below)
4765         {
4766           if (needed_height <= space_below)
4767             y = y - padding.top;
4768           else
4769             y = y + padding.bottom - requisition.height + 1;
4770
4771           y = CLAMP (y, monitor.y,
4772                      monitor.y + monitor.height - requisition.height);
4773         }
4774       else if (needed_height > space_below && needed_height > space_above)
4775         {
4776           if (space_below >= space_above)
4777             y = monitor.y + monitor.height - requisition.height;
4778           else
4779             y = monitor.y;
4780         }
4781       else
4782         {
4783           y = monitor.y;
4784         }
4785     }
4786
4787   scroll_offset = 0;
4788
4789   if (y + requisition.height > monitor.y + monitor.height)
4790     {
4791       if (priv->initially_pushed_in)
4792         scroll_offset += (monitor.y + monitor.height) - requisition.height - y;
4793       y = (monitor.y + monitor.height) - requisition.height;
4794     }
4795
4796   if (y < monitor.y)
4797     {
4798       if (priv->initially_pushed_in)
4799         scroll_offset += monitor.y - y;
4800       y = monitor.y;
4801     }
4802
4803   x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4804
4805   if (GTK_MENU_SHELL (menu)->priv->active)
4806     {
4807       priv->have_position = TRUE;
4808       priv->position_x = x;
4809       priv->position_y = y;
4810     }
4811
4812   if (scroll_offset != 0)
4813     {
4814       GtkBorder arrow_border;
4815
4816       get_arrows_border (menu, &arrow_border);
4817       scroll_offset += arrow_border.top;
4818     }
4819
4820   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4821                    x, y);
4822
4823   if (!GTK_MENU_SHELL (menu)->priv->active)
4824     {
4825       gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4826                          requisition.width, requisition.height);
4827     }
4828
4829   if (set_scroll_offset)
4830     priv->scroll_offset = scroll_offset;
4831 }
4832
4833 static void
4834 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4835 {
4836   GtkMenuPrivate *priv = menu->priv;
4837
4838   if (priv->scroll_timeout)
4839     {
4840       g_source_remove (priv->scroll_timeout);
4841       priv->scroll_timeout = 0;
4842     }
4843 }
4844
4845 static void
4846 gtk_menu_stop_scrolling (GtkMenu *menu)
4847 {
4848   GtkMenuPrivate *priv = menu->priv;
4849   gboolean touchscreen_mode;
4850
4851   gtk_menu_remove_scroll_timeout (menu);
4852
4853   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4854                 "gtk-touchscreen-mode", &touchscreen_mode,
4855                 NULL);
4856
4857   if (!touchscreen_mode)
4858     {
4859       priv->upper_arrow_prelight = FALSE;
4860       priv->lower_arrow_prelight = FALSE;
4861     }
4862 }
4863
4864 static void
4865 gtk_menu_scroll_to (GtkMenu *menu,
4866                     gint    offset)
4867 {
4868   GtkMenuPrivate *priv = menu->priv;
4869   GtkBorder arrow_border, padding;
4870   GtkWidget *widget;
4871   gint x, y;
4872   gint view_width, view_height;
4873   gint border_width;
4874   gint menu_height;
4875   guint vertical_padding;
4876   guint horizontal_padding;
4877   gboolean double_arrows;
4878
4879   widget = GTK_WIDGET (menu);
4880
4881   if (priv->tearoff_active && priv->tearoff_adjustment)
4882     gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4883
4884   /* Move/resize the viewport according to arrows: */
4885   view_width = gtk_widget_get_allocated_width (widget);
4886   view_height = gtk_widget_get_allocated_height (widget);
4887
4888   gtk_widget_style_get (GTK_WIDGET (menu),
4889                         "vertical-padding", &vertical_padding,
4890                         "horizontal-padding", &horizontal_padding,
4891                         NULL);
4892
4893   get_menu_padding (widget, &padding);
4894   double_arrows = get_double_arrows (menu);
4895
4896   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4897
4898   view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4899   view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4900   menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4901     padding.top - padding.bottom;
4902
4903   x = border_width + padding.left + horizontal_padding;
4904   y = border_width + padding.top + vertical_padding;
4905
4906   if (double_arrows && !priv->tearoff_active)
4907     {
4908       if (view_height < menu_height               ||
4909           (offset > 0 && priv->scroll_offset > 0) ||
4910           (offset < 0 && priv->scroll_offset < 0))
4911         {
4912           GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4913           GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4914
4915           if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4916             gtk_widget_queue_draw (GTK_WIDGET (menu));
4917
4918           priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4919
4920           get_arrows_border (menu, &arrow_border);
4921           y += arrow_border.top;
4922           view_height -= arrow_border.top;
4923           view_height -= arrow_border.bottom;
4924
4925           if (offset <= 0)
4926             priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4927           else
4928             {
4929               priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4930
4931               if (priv->upper_arrow_prelight)
4932                 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4933               else
4934                 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4935             }
4936
4937           if (offset >= menu_height - view_height)
4938             priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4939           else
4940             {
4941               priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4942
4943               if (priv->lower_arrow_prelight)
4944                 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4945               else
4946                 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4947             }
4948
4949           if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4950               (priv->lower_arrow_state != lower_arrow_previous_state))
4951             gtk_widget_queue_draw (GTK_WIDGET (menu));
4952
4953           if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4954               (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4955             {
4956               /* At the upper border, possibly remove timeout */
4957               if (priv->scroll_step < 0)
4958                 {
4959                   gtk_menu_stop_scrolling (menu);
4960                   gtk_widget_queue_draw (GTK_WIDGET (menu));
4961                 }
4962             }
4963
4964           if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4965               (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4966             {
4967               /* At the lower border, possibly remove timeout */
4968               if (priv->scroll_step > 0)
4969                 {
4970                   gtk_menu_stop_scrolling (menu);
4971                   gtk_widget_queue_draw (GTK_WIDGET (menu));
4972                 }
4973             }
4974         }
4975       else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4976         {
4977           offset = 0;
4978
4979           priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4980           priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4981
4982           gtk_menu_stop_scrolling (menu);
4983           gtk_widget_queue_draw (GTK_WIDGET (menu));
4984         }
4985     }
4986   else if (!priv->tearoff_active)
4987     {
4988       gboolean last_visible;
4989
4990       last_visible = priv->upper_arrow_visible;
4991       priv->upper_arrow_visible = offset > 0;
4992
4993       /* upper_arrow_visible may have changed, so requery the border */
4994       get_arrows_border (menu, &arrow_border);
4995       view_height -= arrow_border.top;
4996
4997       if ((last_visible != priv->upper_arrow_visible) &&
4998           !priv->upper_arrow_visible)
4999         {
5000           priv->upper_arrow_prelight = FALSE;
5001
5002           /* If we hide the upper arrow, possibly remove timeout */
5003           if (priv->scroll_step < 0)
5004             {
5005               gtk_menu_stop_scrolling (menu);
5006               gtk_widget_queue_draw (GTK_WIDGET (menu));
5007             }
5008         }
5009
5010       last_visible = priv->lower_arrow_visible;
5011       priv->lower_arrow_visible = offset < menu_height - view_height;
5012
5013       /* lower_arrow_visible may have changed, so requery the border */
5014       get_arrows_border (menu, &arrow_border);
5015       view_height -= arrow_border.bottom;
5016
5017       if ((last_visible != priv->lower_arrow_visible) &&
5018            !priv->lower_arrow_visible)
5019         {
5020           priv->lower_arrow_prelight = FALSE;
5021
5022           /* If we hide the lower arrow, possibly remove timeout */
5023           if (priv->scroll_step > 0)
5024             {
5025               gtk_menu_stop_scrolling (menu);
5026               gtk_widget_queue_draw (GTK_WIDGET (menu));
5027             }
5028         }
5029
5030       y += arrow_border.top;
5031     }
5032
5033   /* Scroll the menu: */
5034   if (gtk_widget_get_realized (widget))
5035     gdk_window_move (priv->bin_window, 0, -offset);
5036
5037   if (gtk_widget_get_realized (widget))
5038     gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5039
5040   priv->scroll_offset = offset;
5041 }
5042
5043 static gboolean
5044 compute_child_offset (GtkMenu   *menu,
5045                       GtkWidget *menu_item,
5046                       gint      *offset,
5047                       gint      *height,
5048                       gboolean  *is_last_child)
5049 {
5050   GtkMenuPrivate *priv = menu->priv;
5051   gint item_top_attach;
5052   gint item_bottom_attach;
5053   gint child_offset = 0;
5054   gint i;
5055
5056   get_effective_child_attach (menu_item, NULL, NULL,
5057                               &item_top_attach, &item_bottom_attach);
5058
5059   /* there is a possibility that we get called before _size_request,
5060    * so check the height table for safety.
5061    */
5062   if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5063     return FALSE;
5064
5065   /* when we have a row with only invisible children, its height will
5066    * be zero, so there's no need to check WIDGET_VISIBLE here
5067    */
5068   for (i = 0; i < item_top_attach; i++)
5069     child_offset += priv->heights[i];
5070
5071   if (is_last_child)
5072     *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5073   if (offset)
5074     *offset = child_offset;
5075   if (height)
5076     *height = priv->heights[item_top_attach];
5077
5078   return TRUE;
5079 }
5080
5081 static void
5082 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5083                               GtkWidget    *menu_item)
5084 {
5085   GtkMenu *menu = GTK_MENU (menu_shell);
5086   GtkMenuPrivate *priv = menu->priv;
5087   GtkWidget *widget = GTK_WIDGET (menu_shell);
5088   gint child_offset, child_height;
5089   gint height;
5090   gint y;
5091   gint arrow_height;
5092   gboolean last_child = 0;
5093
5094   /* We need to check if the selected item fully visible.
5095    * If not we need to scroll the menu so that it becomes fully
5096    * visible.
5097    */
5098   if (compute_child_offset (menu, menu_item,
5099                             &child_offset, &child_height, &last_child))
5100     {
5101       guint vertical_padding;
5102       gboolean double_arrows;
5103       GtkBorder padding;
5104
5105       y = priv->scroll_offset;
5106       height = gdk_window_get_height (gtk_widget_get_window (widget));
5107
5108       gtk_widget_style_get (widget,
5109                             "vertical-padding", &vertical_padding,
5110                             NULL);
5111
5112       double_arrows = get_double_arrows (menu);
5113       get_menu_padding (widget, &padding);
5114
5115       height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5116                 padding.top + padding.bottom +
5117                 2 * vertical_padding;
5118       if (child_offset < y)
5119         {
5120           /* Ignore the enter event we might get if the pointer
5121            * is on the menu
5122            */
5123           menu_shell->priv->ignore_enter = TRUE;
5124           gtk_menu_scroll_to (menu, child_offset);
5125         }
5126       else
5127         {
5128           GtkBorder arrow_border;
5129
5130           arrow_height = 0;
5131
5132           get_arrows_border (menu, &arrow_border);
5133           if (!priv->tearoff_active)
5134             arrow_height = arrow_border.top + arrow_border.bottom;
5135
5136           if (child_offset + child_height > y + height - arrow_height)
5137             {
5138               arrow_height = 0;
5139               if ((!last_child && !priv->tearoff_active) || double_arrows)
5140                 arrow_height += arrow_border.bottom;
5141
5142               y = child_offset + child_height - height + arrow_height;
5143               if (((y > 0) && !priv->tearoff_active) || double_arrows)
5144                 {
5145                   /* Need upper arrow */
5146                   arrow_height += arrow_border.top;
5147                   y = child_offset + child_height - height + arrow_height;
5148                 }
5149               /* Ignore the enter event we might get if the pointer
5150                * is on the menu
5151                */
5152               menu_shell->priv->ignore_enter = TRUE;
5153               gtk_menu_scroll_to (menu, y);
5154             }
5155         }
5156     }
5157 }
5158
5159 static void
5160 gtk_menu_select_item (GtkMenuShell *menu_shell,
5161                       GtkWidget    *menu_item)
5162 {
5163   GtkMenu *menu = GTK_MENU (menu_shell);
5164
5165   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5166     gtk_menu_scroll_item_visible (menu_shell, menu_item);
5167
5168   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5169 }
5170
5171
5172 /* Reparent the menu, taking care of the refcounting
5173  *
5174  * If unrealize is true we force a unrealize while reparenting the parent.
5175  * This can help eliminate flicker in some cases.
5176  *
5177  * What happens is that when the menu is unrealized and then re-realized,
5178  * the allocations are as follows:
5179  *
5180  *  parent - 1x1 at (0,0)
5181  *  child1 - 100x20 at (0,0)
5182  *  child2 - 100x20 at (0,20)
5183  *  child3 - 100x20 at (0,40)
5184  *
5185  * That is, the parent is small but the children are full sized. Then,
5186  * when the queued_resize gets processed, the parent gets resized to
5187  * full size.
5188  *
5189  * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5190  * contains the following logic:
5191  *
5192  * - if a move or resize operation on a window would change the clip
5193  *   region on the children, then before the window is resized
5194  *   the background for children is temporarily set to None, the
5195  *   move/resize done, and the background for the children restored.
5196  *
5197  * So, at the point where the parent is resized to final size, the
5198  * background for the children is temporarily None, and thus they
5199  * are not cleared to the background color and the previous background
5200  * (the image of the menu) is left in place.
5201  */
5202 static void
5203 gtk_menu_reparent (GtkMenu   *menu,
5204                    GtkWidget *new_parent,
5205                    gboolean   unrealize)
5206 {
5207   GObject *object = G_OBJECT (menu);
5208   GtkWidget *widget = GTK_WIDGET (menu);
5209   gboolean was_floating = g_object_is_floating (object);
5210
5211   g_object_ref_sink (object);
5212
5213   if (unrealize)
5214     {
5215       g_object_ref (object);
5216       gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5217       gtk_container_add (GTK_CONTAINER (new_parent), widget);
5218       g_object_unref (object);
5219     }
5220   else
5221     gtk_widget_reparent (widget, new_parent);
5222
5223   if (was_floating)
5224     g_object_force_floating (object);
5225   else
5226     g_object_unref (object);
5227 }
5228
5229 static void
5230 gtk_menu_show_all (GtkWidget *widget)
5231 {
5232   /* Show children, but not self. */
5233   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5234 }
5235
5236 /**
5237  * gtk_menu_set_screen:
5238  * @menu: a #GtkMenu
5239  * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5240  *          determined by the widget the menu is attached to
5241  *
5242  * Sets the #GdkScreen on which the menu will be displayed.
5243  *
5244  * Since: 2.2
5245  */
5246 void
5247 gtk_menu_set_screen (GtkMenu   *menu,
5248                      GdkScreen *screen)
5249 {
5250   g_return_if_fail (GTK_IS_MENU (menu));
5251   g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5252
5253   g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5254
5255   if (screen)
5256     {
5257       menu_change_screen (menu, screen);
5258     }
5259   else
5260     {
5261       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5262       if (attach_widget)
5263         attach_widget_screen_changed (attach_widget, NULL, menu);
5264     }
5265 }
5266
5267 /**
5268  * gtk_menu_attach:
5269  * @menu: a #GtkMenu
5270  * @child: a #GtkMenuItem
5271  * @left_attach: The column number to attach the left side of the item to
5272  * @right_attach: The column number to attach the right side of the item to
5273  * @top_attach: The row number to attach the top of the item to
5274  * @bottom_attach: The row number to attach the bottom of the item to
5275  *
5276  * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5277  * an item will occupy is specified by @left_attach, @right_attach,
5278  * @top_attach and @bottom_attach. These each represent the leftmost,
5279  * rightmost, uppermost and lower column and row numbers of the table.
5280  * (Columns and rows are indexed from zero).
5281  *
5282  * Note that this function is not related to gtk_menu_detach().
5283  *
5284  * Since: 2.4
5285  */
5286 void
5287 gtk_menu_attach (GtkMenu   *menu,
5288                  GtkWidget *child,
5289                  guint      left_attach,
5290                  guint      right_attach,
5291                  guint      top_attach,
5292                  guint      bottom_attach)
5293 {
5294   GtkMenuShell *menu_shell;
5295   GtkWidget *parent;
5296
5297   g_return_if_fail (GTK_IS_MENU (menu));
5298   g_return_if_fail (GTK_IS_MENU_ITEM (child));
5299   parent = gtk_widget_get_parent (child);
5300   g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5301   g_return_if_fail (left_attach < right_attach);
5302   g_return_if_fail (top_attach < bottom_attach);
5303
5304   menu_shell = GTK_MENU_SHELL (menu);
5305
5306   if (!parent)
5307     {
5308       AttachInfo *ai = get_attach_info (child);
5309
5310       ai->left_attach = left_attach;
5311       ai->right_attach = right_attach;
5312       ai->top_attach = top_attach;
5313       ai->bottom_attach = bottom_attach;
5314
5315       menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5316
5317       gtk_widget_set_parent (child, GTK_WIDGET (menu));
5318
5319       menu_queue_resize (menu);
5320     }
5321   else
5322     {
5323       gtk_container_child_set (GTK_CONTAINER (parent), child,
5324                                "left-attach",   left_attach,
5325                                "right-attach",  right_attach,
5326                                "top-attach",    top_attach,
5327                                "bottom-attach", bottom_attach,
5328                                NULL);
5329     }
5330 }
5331
5332 static gint
5333 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5334 {
5335   gint popup_delay;
5336
5337   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5338                 "gtk-menu-popup-delay", &popup_delay,
5339                 NULL);
5340
5341   return popup_delay;
5342 }
5343
5344 static GtkWidget *
5345 find_child_containing (GtkMenuShell *menu_shell,
5346                        int           left,
5347                        int           right,
5348                        int           top,
5349                        int           bottom)
5350 {
5351   GList *list;
5352
5353   /* find a child which includes the area given by
5354    * left, right, top, bottom.
5355    */
5356   for (list = menu_shell->priv->children; list; list = list->next)
5357     {
5358       gint l, r, t, b;
5359
5360       if (!_gtk_menu_item_is_selectable (list->data))
5361         continue;
5362
5363       get_effective_child_attach (list->data, &l, &r, &t, &b);
5364
5365       if (l <= left && right <= r && t <= top && bottom <= b)
5366         return GTK_WIDGET (list->data);
5367     }
5368
5369   return NULL;
5370 }
5371
5372 static void
5373 gtk_menu_move_current (GtkMenuShell         *menu_shell,
5374                        GtkMenuDirectionType  direction)
5375 {
5376   GtkMenu *menu = GTK_MENU (menu_shell);
5377   gint i;
5378   gint l, r, t, b;
5379   GtkWidget *match = NULL;
5380
5381   if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5382     {
5383       switch (direction)
5384         {
5385         case GTK_MENU_DIR_CHILD:
5386           direction = GTK_MENU_DIR_PARENT;
5387           break;
5388         case GTK_MENU_DIR_PARENT:
5389           direction = GTK_MENU_DIR_CHILD;
5390           break;
5391         default: ;
5392         }
5393     }
5394
5395   /* use special table menu key bindings */
5396   if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5397     {
5398       get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5399
5400       if (direction == GTK_MENU_DIR_NEXT)
5401         {
5402           for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5403             {
5404               match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5405               if (match)
5406                 break;
5407             }
5408
5409           if (!match)
5410             {
5411               /* wrap around */
5412               for (i = 0; i < t; i++)
5413                 {
5414                   match = find_child_containing (menu_shell,
5415                                                  l, l + 1, i, i + 1);
5416                   if (match)
5417                     break;
5418                 }
5419             }
5420         }
5421       else if (direction == GTK_MENU_DIR_PREV)
5422         {
5423           for (i = t; i > 0; i--)
5424             {
5425               match = find_child_containing (menu_shell,
5426                                              l, l + 1, i - 1, i);
5427               if (match)
5428                 break;
5429             }
5430
5431           if (!match)
5432             {
5433               /* wrap around */
5434               for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5435                 {
5436                   match = find_child_containing (menu_shell,
5437                                                  l, l + 1, i - 1, i);
5438                   if (match)
5439                     break;
5440                 }
5441             }
5442         }
5443       else if (direction == GTK_MENU_DIR_PARENT)
5444         {
5445           /* we go one left if possible */
5446           if (l > 0)
5447             match = find_child_containing (menu_shell,
5448                                            l - 1, l, t, t + 1);
5449
5450           if (!match)
5451             {
5452               GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5453
5454               if (!parent
5455                   || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5456                 match = menu_shell->priv->active_menu_item;
5457             }
5458         }
5459       else if (direction == GTK_MENU_DIR_CHILD)
5460         {
5461           /* we go one right if possible */
5462           if (r < gtk_menu_get_n_columns (menu))
5463             match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5464
5465           if (!match)
5466             {
5467               GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5468
5469               if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5470                   (!parent ||
5471                    g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5472                 match = menu_shell->priv->active_menu_item;
5473             }
5474         }
5475
5476       if (match)
5477         {
5478           gtk_menu_shell_select_item (menu_shell, match);
5479           return;
5480         }
5481     }
5482
5483   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5484 }
5485
5486 static gint
5487 get_visible_size (GtkMenu *menu)
5488 {
5489   GtkMenuPrivate *priv = menu->priv;
5490   GtkAllocation allocation;
5491   GtkWidget *widget = GTK_WIDGET (menu);
5492   GtkContainer *container = GTK_CONTAINER (menu);
5493   GtkBorder padding;
5494   gint menu_height;
5495
5496   gtk_widget_get_allocation (widget, &allocation);
5497   get_menu_padding (widget, &padding);
5498
5499   menu_height = (allocation.height -
5500                  (2 * gtk_container_get_border_width (container)) -
5501                  padding.top - padding.bottom);
5502
5503   if (!priv->tearoff_active)
5504     {
5505       GtkBorder arrow_border;
5506
5507       get_arrows_border (menu, &arrow_border);
5508       menu_height -= arrow_border.top;
5509       menu_height -= arrow_border.bottom;
5510     }
5511
5512   return menu_height;
5513 }
5514
5515 /* Find the sensitive on-screen child containing @y, or if none,
5516  * the nearest selectable onscreen child. (%NULL if none)
5517  */
5518 static GtkWidget *
5519 child_at (GtkMenu *menu,
5520           gint     y)
5521 {
5522   GtkMenuPrivate *priv = menu->priv;
5523   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5524   GtkWidget *child = NULL;
5525   gint child_offset = 0;
5526   GList *children;
5527   gint menu_height;
5528   gint lower, upper; /* Onscreen bounds */
5529
5530   menu_height = get_visible_size (menu);
5531   lower = priv->scroll_offset;
5532   upper = priv->scroll_offset + menu_height;
5533
5534   for (children = menu_shell->priv->children; children; children = children->next)
5535     {
5536       if (gtk_widget_get_visible (children->data))
5537         {
5538           GtkRequisition child_requisition;
5539
5540           gtk_widget_get_preferred_size (children->data,
5541                                          &child_requisition, NULL);
5542
5543           if (_gtk_menu_item_is_selectable (children->data) &&
5544               child_offset >= lower &&
5545               child_offset + child_requisition.height <= upper)
5546             {
5547               child = children->data;
5548
5549               if (child_offset + child_requisition.height > y &&
5550                   !GTK_IS_TEAROFF_MENU_ITEM (child))
5551                 return child;
5552             }
5553
5554           child_offset += child_requisition.height;
5555         }
5556     }
5557
5558   return child;
5559 }
5560
5561 static gint
5562 get_menu_height (GtkMenu *menu)
5563 {
5564   GtkMenuPrivate *priv = menu->priv;
5565   GtkAllocation allocation;
5566   GtkWidget *widget = GTK_WIDGET (menu);
5567   GtkBorder padding;
5568   gint height;
5569
5570   gtk_widget_get_allocation (widget, &allocation);
5571   get_menu_padding (widget, &padding);
5572
5573   height = allocation.height;
5574   height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5575     padding.top + padding.bottom;
5576
5577   if (!priv->tearoff_active)
5578     {
5579       GtkBorder arrow_border;
5580
5581       get_arrows_border (menu, &arrow_border);
5582       height -= arrow_border.top;
5583       height -= arrow_border.bottom;
5584     }
5585
5586   return height;
5587 }
5588
5589 static void
5590 gtk_menu_real_move_scroll (GtkMenu       *menu,
5591                            GtkScrollType  type)
5592 {
5593   GtkMenuPrivate *priv = menu->priv;
5594   gint page_size = get_visible_size (menu);
5595   gint end_position = get_menu_height (menu);
5596   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5597
5598   switch (type)
5599     {
5600     case GTK_SCROLL_PAGE_UP:
5601     case GTK_SCROLL_PAGE_DOWN:
5602       {
5603         gint old_offset;
5604         gint new_offset;
5605         gint child_offset = 0;
5606         gboolean old_upper_arrow_visible;
5607         gint step;
5608
5609         if (type == GTK_SCROLL_PAGE_UP)
5610           step = - page_size;
5611         else
5612           step = page_size;
5613
5614         if (menu_shell->priv->active_menu_item)
5615           {
5616             gint child_height;
5617
5618             compute_child_offset (menu, menu_shell->priv->active_menu_item,
5619                                   &child_offset, &child_height, NULL);
5620             child_offset += child_height / 2;
5621           }
5622
5623         menu_shell->priv->ignore_enter = TRUE;
5624         old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5625         old_offset = priv->scroll_offset;
5626
5627         new_offset = priv->scroll_offset + step;
5628         new_offset = CLAMP (new_offset, 0, end_position - page_size);
5629
5630         gtk_menu_scroll_to (menu, new_offset);
5631
5632         if (menu_shell->priv->active_menu_item)
5633           {
5634             GtkWidget *new_child;
5635             gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5636             GtkBorder arrow_border;
5637             get_arrows_border (menu, &arrow_border);
5638
5639             if (priv->scroll_offset != old_offset)
5640               step = priv->scroll_offset - old_offset;
5641
5642             step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5643
5644             new_child = child_at (menu, child_offset + step);
5645             if (new_child)
5646               gtk_menu_shell_select_item (menu_shell, new_child);
5647           }
5648       }
5649       break;
5650     case GTK_SCROLL_START:
5651       /* Ignore the enter event we might get if the pointer is on the menu */
5652       menu_shell->priv->ignore_enter = TRUE;
5653       gtk_menu_scroll_to (menu, 0);
5654       gtk_menu_shell_select_first (menu_shell, TRUE);
5655       break;
5656     case GTK_SCROLL_END:
5657       /* Ignore the enter event we might get if the pointer is on the menu */
5658       menu_shell->priv->ignore_enter = TRUE;
5659       gtk_menu_scroll_to (menu, end_position - page_size);
5660       _gtk_menu_shell_select_last (menu_shell, TRUE);
5661       break;
5662     default:
5663       break;
5664     }
5665 }
5666
5667
5668 /**
5669  * gtk_menu_set_monitor:
5670  * @menu: a #GtkMenu
5671  * @monitor_num: the number of the monitor on which the menu should
5672  *    be popped up
5673  *
5674  * Informs GTK+ on which monitor a menu should be popped up.
5675  * See gdk_screen_get_monitor_geometry().
5676  *
5677  * This function should be called from a #GtkMenuPositionFunc
5678  * if the menu should not appear on the same monitor as the pointer.
5679  * This information can't be reliably inferred from the coordinates
5680  * returned by a #GtkMenuPositionFunc, since, for very long menus,
5681  * these coordinates may extend beyond the monitor boundaries or even
5682  * the screen boundaries.
5683  *
5684  * Since: 2.4
5685  */
5686 void
5687 gtk_menu_set_monitor (GtkMenu *menu,
5688                       gint     monitor_num)
5689 {
5690   GtkMenuPrivate *priv = menu->priv;
5691
5692   g_return_if_fail (GTK_IS_MENU (menu));
5693
5694   priv->monitor_num = monitor_num;
5695 }
5696
5697 /**
5698  * gtk_menu_get_monitor:
5699  * @menu: a #GtkMenu
5700  *
5701  * Retrieves the number of the monitor on which to show the menu.
5702  *
5703  * Returns: the number of the monitor on which the menu should
5704  *    be popped up or -1, if no monitor has been set
5705  *
5706  * Since: 2.14
5707  */
5708 gint
5709 gtk_menu_get_monitor (GtkMenu *menu)
5710 {
5711   g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5712
5713   return menu->priv->monitor_num;
5714 }
5715
5716 /**
5717  * gtk_menu_get_for_attach_widget:
5718  * @widget: a #GtkWidget
5719  *
5720  * Returns a list of the menus which are attached to this widget.
5721  * This list is owned by GTK+ and must not be modified.
5722  *
5723  * Return value: (element-type GtkWidget) (transfer none): the list
5724  *     of menus attached to his widget.
5725  *
5726  * Since: 2.6
5727  */
5728 GList*
5729 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5730 {
5731   GList *list;
5732
5733   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5734
5735   list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5736
5737   return list;
5738 }
5739
5740 static void
5741 gtk_menu_grab_notify (GtkWidget *widget,
5742                       gboolean   was_grabbed)
5743 {
5744   GtkWidget *toplevel;
5745   GtkWindowGroup *group;
5746   GtkWidget *grab;
5747   GdkDevice *pointer;
5748
5749   pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5750
5751   if (!pointer ||
5752       !gtk_widget_device_is_shadowed (widget, pointer))
5753     return;
5754
5755   toplevel = gtk_widget_get_toplevel (widget);
5756
5757   if (!GTK_IS_WINDOW (toplevel))
5758     return;
5759
5760   group = gtk_window_get_group (GTK_WINDOW (toplevel));
5761   grab = gtk_window_group_get_current_device_grab (group, pointer);
5762
5763   if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5764     gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5765 }
5766
5767 /**
5768  * gtk_menu_set_reserve_toggle_size:
5769  * @menu: a #GtkMenu
5770  * @reserve_toggle_size: whether to reserve size for toggles
5771  *
5772  * Sets whether the menu should reserve space for drawing toggles
5773  * or icons, regardless of their actual presence.
5774  *
5775  * Since: 2.18
5776  */
5777 void
5778 gtk_menu_set_reserve_toggle_size (GtkMenu  *menu,
5779                                   gboolean  reserve_toggle_size)
5780 {
5781   GtkMenuPrivate *priv = menu->priv;
5782   gboolean no_toggle_size;
5783
5784   g_return_if_fail (GTK_IS_MENU (menu));
5785
5786   no_toggle_size = !reserve_toggle_size;
5787
5788   if (priv->no_toggle_size != no_toggle_size)
5789     {
5790       priv->no_toggle_size = no_toggle_size;
5791
5792       g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5793     }
5794 }
5795
5796 /**
5797  * gtk_menu_get_reserve_toggle_size:
5798  * @menu: a #GtkMenu
5799  *
5800  * Returns whether the menu reserves space for toggles and
5801  * icons, regardless of their actual presence.
5802  *
5803  * Returns: Whether the menu reserves toggle space
5804  *
5805  * Since: 2.18
5806  */
5807 gboolean
5808 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5809 {
5810   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5811
5812   return !menu->priv->no_toggle_size;
5813 }