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