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