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