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