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