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