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