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