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