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