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