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