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