gtkmenushell.c 65.6 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27 28 29 30 31 32 33 34 35 36 37 38 39
/**
 * SECTION:gtkmenushell
 * @Title: GtkMenuShell
 * @Short_description: A base class for menu objects
 *
 * A #GtkMenuShell is the abstract base class used to derive the
 * #GtkMenu and #GtkMenuBar subclasses.
 *
 * A #GtkMenuShell is a container of #GtkMenuItem objects arranged
 * in a list which can be navigated, selected, and activated by the
 * user to perform application functions. A #GtkMenuItem can have a
 * submenu associated with it, allowing for nested hierarchical menus.
 */
40
#include "config.h"
41

42
#include "gtkbindings.h"
43
#include "gtkkeyhash.h"
44
#include "gtklabel.h"
45
#include "gtkmainprivate.h"
46
#include "gtkmarshalers.h"
47
#include "gtkmenubar.h"
48
#include "gtkmenuitemprivate.h"
49
#include "gtkmenushellprivate.h"
50
#include "gtkmenuprivate.h"
51
#include "gtkmnemonichash.h"
52
#include "gtktearoffmenuitem.h"
53
#include "gtkwindow.h"
54 55
#include "gtkprivate.h"
#include "gtkintl.h"
56
#include "gtktypebuiltins.h"
57
#include "a11y/gtkmenushellaccessible.h"
Elliot Lee's avatar
Elliot Lee committed
58 59 60

#define MENU_SHELL_TIMEOUT   500

61 62 63 64 65
#define PACK_DIRECTION(m)                                 \
   (GTK_IS_MENU_BAR (m)                                   \
     ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
     : GTK_PACK_DIRECTION_LTR)

Elliot Lee's avatar
Elliot Lee committed
66 67
enum {
  DEACTIVATE,
Tim Janik's avatar
Tim Janik committed
68
  SELECTION_DONE,
69 70 71
  MOVE_CURRENT,
  ACTIVATE_CURRENT,
  CANCEL,
72
  CYCLE_FOCUS,
73
  MOVE_SELECTED,
74
  INSERT,
Elliot Lee's avatar
Elliot Lee committed
75 76 77
  LAST_SIGNAL
};

78 79 80 81 82
enum {
  PROP_0,
  PROP_TAKE_FOCUS
};

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/* Terminology:
 * 
 * A menu item can be "selected", this means that it is displayed
 * in the prelight state, and if it has a submenu, that submenu
 * will be popped up. 
 * 
 * A menu is "active" when it is visible onscreen and the user
 * is selecting from it. A menubar is not active until the user
 * clicks on one of its menuitems. When a menu is active,
 * passing the mouse over a submenu will pop it up.
 *
 * menu_shell->active_menu_item, is however, not an "active"
 * menu item (there is no such thing) but rather, the selected
 * menu item in that MenuShell, if there is one.
 *
 * There is also is a concept of the current menu and a current
 * menu item. The current menu item is the selected menu item
Matthias Clasen's avatar
Matthias Clasen committed
100
 * that is furthest down in the hierarchy. (Every active menu_shell
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
 * does not necessarily contain a selected menu item, but if
 * it does, then menu_shell->parent_menu_shell must also contain
 * a selected menu item. The current menu is the menu that 
 * contains the current menu_item. It will always have a GTK
 * grab and receive all key presses.
 *
 *
 * Action signals:
 *
 *  ::move_current (GtkMenuDirection *dir)
 *     Moves the current menu item in direction 'dir':
 *
 *       GTK_MENU_DIR_PARENT: To the parent menu shell
 *       GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
 *          a submenu.
 *       GTK_MENU_DIR_NEXT/PREV: To the next or previous item
 *          in this menu.
 * 
 *     As a a bit of a hack to get movement between menus and
 *     menubars working, if submenu_placement is different for
 *     the menu and its MenuShell then the following apply:
 * 
 *       - For 'parent' the current menu is not just moved to
 *         the parent, but moved to the previous entry in the parent
 *       - For 'child', if there is no child, then current is
 *         moved to the next item in the parent.
 *
128 129 130 131
 *    Note that the above explanation of ::move_current was written
 *    before menus and menubars had support for RTL flipping and
 *    different packing directions, and therefore only applies for
 *    when text direction and packing direction are both left-to-right.
132 133 134 135 136 137 138 139 140
 * 
 *  ::activate_current (GBoolean *force_hide)
 *     Activate the current item. If 'force_hide' is true, hide
 *     the current menu item always. Otherwise, only hide
 *     it if menu_item->klass->hide_on_activate is true.
 *
 *  ::cancel ()
 *     Cancels the current selection
 */
Elliot Lee's avatar
Elliot Lee committed
141

142

143 144 145 146 147 148 149 150
static void gtk_menu_shell_set_property      (GObject           *object,
                                              guint              prop_id,
                                              const GValue      *value,
                                              GParamSpec        *pspec);
static void gtk_menu_shell_get_property      (GObject           *object,
                                              guint              prop_id,
                                              GValue            *value,
                                              GParamSpec        *pspec);
Elliot Lee's avatar
Elliot Lee committed
151
static void gtk_menu_shell_realize           (GtkWidget         *widget);
152
static void gtk_menu_shell_finalize          (GObject           *object);
153
static void gtk_menu_shell_dispose           (GObject           *object);
Elliot Lee's avatar
Elliot Lee committed
154
static gint gtk_menu_shell_button_press      (GtkWidget         *widget,
155
                                              GdkEventButton    *event);
Elliot Lee's avatar
Elliot Lee committed
156
static gint gtk_menu_shell_button_release    (GtkWidget         *widget,
157 158 159
                                              GdkEventButton    *event);
static gint gtk_menu_shell_key_press         (GtkWidget         *widget,
                                              GdkEventKey       *event);
Elliot Lee's avatar
Elliot Lee committed
160
static gint gtk_menu_shell_enter_notify      (GtkWidget         *widget,
161
                                              GdkEventCrossing  *event);
Elliot Lee's avatar
Elliot Lee committed
162
static gint gtk_menu_shell_leave_notify      (GtkWidget         *widget,
163
                                              GdkEventCrossing  *event);
164
static void gtk_menu_shell_screen_changed    (GtkWidget         *widget,
165
                                              GdkScreen         *previous_screen);
166
static gboolean gtk_menu_shell_grab_broken       (GtkWidget         *widget,
167
                                              GdkEventGrabBroken *event);
Elliot Lee's avatar
Elliot Lee committed
168
static void gtk_menu_shell_add               (GtkContainer      *container,
169
                                              GtkWidget         *widget);
Elliot Lee's avatar
Elliot Lee committed
170
static void gtk_menu_shell_remove            (GtkContainer      *container,
171
                                              GtkWidget         *widget);
172
static void gtk_menu_shell_forall            (GtkContainer      *container,
173 174 175
                                              gboolean           include_internals,
                                              GtkCallback        callback,
                                              gpointer           callback_data);
176
static void gtk_menu_shell_real_insert       (GtkMenuShell *menu_shell,
177 178
                                              GtkWidget    *child,
                                              gint          position);
Elliot Lee's avatar
Elliot Lee committed
179 180
static void gtk_real_menu_shell_deactivate   (GtkMenuShell      *menu_shell);
static gint gtk_menu_shell_is_item           (GtkMenuShell      *menu_shell,
181
                                              GtkWidget         *child);
Owen Taylor's avatar
Owen Taylor committed
182
static GtkWidget *gtk_menu_shell_get_item    (GtkMenuShell      *menu_shell,
183
                                              GdkEvent          *event);
Manish Singh's avatar
Manish Singh committed
184
static GType    gtk_menu_shell_child_type  (GtkContainer      *container);
185
static void gtk_menu_shell_real_select_item  (GtkMenuShell      *menu_shell,
186
                                              GtkWidget         *menu_item);
187
static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell   *menu_shell); 
Elliot Lee's avatar
Elliot Lee committed
188

189
static void gtk_real_menu_shell_move_current (GtkMenuShell      *menu_shell,
190
                                              GtkMenuDirectionType direction);
191
static void gtk_real_menu_shell_activate_current (GtkMenuShell      *menu_shell,
192
                                                  gboolean           force_hide);
193
static void gtk_real_menu_shell_cancel           (GtkMenuShell      *menu_shell);
194
static void gtk_real_menu_shell_cycle_focus      (GtkMenuShell      *menu_shell,
195
                                                  GtkDirectionType   dir);
Elliot Lee's avatar
Elliot Lee committed
196

197 198
static void     gtk_menu_shell_reset_key_hash    (GtkMenuShell *menu_shell);
static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
199
                                                  GdkEventKey  *event);
200
static gboolean gtk_menu_shell_real_move_selected (GtkMenuShell  *menu_shell, 
201
                                                   gint           distance);
202

203
static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
204

205
G_DEFINE_ABSTRACT_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
Elliot Lee's avatar
Elliot Lee committed
206 207 208 209

static void
gtk_menu_shell_class_init (GtkMenuShellClass *klass)
{
Manish Singh's avatar
Manish Singh committed
210
  GObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
211 212 213
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

214 215
  GtkBindingSet *binding_set;

Manish Singh's avatar
Manish Singh committed
216
  object_class = (GObjectClass*) klass;
Elliot Lee's avatar
Elliot Lee committed
217 218 219
  widget_class = (GtkWidgetClass*) klass;
  container_class = (GtkContainerClass*) klass;

220 221
  object_class->set_property = gtk_menu_shell_set_property;
  object_class->get_property = gtk_menu_shell_get_property;
222
  object_class->finalize = gtk_menu_shell_finalize;
223
  object_class->dispose = gtk_menu_shell_dispose;
224

225 226 227
  widget_class->realize = gtk_menu_shell_realize;
  widget_class->button_press_event = gtk_menu_shell_button_press;
  widget_class->button_release_event = gtk_menu_shell_button_release;
228
  widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
229 230 231
  widget_class->key_press_event = gtk_menu_shell_key_press;
  widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
  widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
232
  widget_class->screen_changed = gtk_menu_shell_screen_changed;
233 234 235 236 237 238 239 240 241 242 243 244 245 246

  container_class->add = gtk_menu_shell_add;
  container_class->remove = gtk_menu_shell_remove;
  container_class->forall = gtk_menu_shell_forall;
  container_class->child_type = gtk_menu_shell_child_type;

  klass->submenu_placement = GTK_TOP_BOTTOM;
  klass->deactivate = gtk_real_menu_shell_deactivate;
  klass->selection_done = NULL;
  klass->move_current = gtk_real_menu_shell_move_current;
  klass->activate_current = gtk_real_menu_shell_activate_current;
  klass->cancel = gtk_real_menu_shell_cancel;
  klass->select_item = gtk_menu_shell_real_select_item;
  klass->insert = gtk_menu_shell_real_insert;
247
  klass->move_selected = gtk_menu_shell_real_move_selected;
248

249 250 251 252 253 254
  /**
   * GtkMenuShell::deactivate:
   * @menushell: the object which received the signal
   *
   * This signal is emitted when a menu shell is deactivated.
   */
Elliot Lee's avatar
Elliot Lee committed
255
  menu_shell_signals[DEACTIVATE] =
256
    g_signal_new (I_("deactivate"),
257 258 259 260 261 262
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
263

264 265 266 267 268 269 270
  /**
   * GtkMenuShell::selection-done:
   * @menushell: the object which received the signal
   *
   * This signal is emitted when a selection has been
   * completed within a menu shell.
   */
Tim Janik's avatar
Tim Janik committed
271
  menu_shell_signals[SELECTION_DONE] =
272
    g_signal_new (I_("selection-done"),
273 274 275 276 277 278
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
279

280 281 282 283 284 285 286 287
  /**
   * GtkMenuShell::move-current:
   * @menushell: the object which received the signal
   * @direction: the direction to move
   *
   * An keybinding signal which moves the current menu item
   * in the direction specified by @direction.
   */
288
  menu_shell_signals[MOVE_CURRENT] =
289
    g_signal_new (I_("move-current"),
290 291 292 293 294 295 296
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
                  NULL, NULL,
                  _gtk_marshal_VOID__ENUM,
                  G_TYPE_NONE, 1,
                  GTK_TYPE_MENU_DIRECTION_TYPE);
297

298 299 300 301 302 303 304 305
  /**
   * GtkMenuShell::activate-current:
   * @menushell: the object which received the signal
   * @force_hide: if %TRUE, hide the menu after activating the menu item
   *
   * An action signal that activates the current menu item within
   * the menu shell.
   */
306
  menu_shell_signals[ACTIVATE_CURRENT] =
307
    g_signal_new (I_("activate-current"),
308 309 310 311 312 313 314
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
                  NULL, NULL,
                  _gtk_marshal_VOID__BOOLEAN,
                  G_TYPE_NONE, 1,
                  G_TYPE_BOOLEAN);
315

316 317 318 319 320 321 322
  /**
   * GtkMenuShell::cancel:
   * @menushell: the object which received the signal
   *
   * An action signal which cancels the selection within the menu shell.
   * Causes the #GtkMenuShell::selection-done signal to be emitted.
   */
323
  menu_shell_signals[CANCEL] =
324
    g_signal_new (I_("cancel"),
325 326 327 328 329 330
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
331

332 333 334 335 336 337 338 339
  /**
   * GtkMenuShell::cycle-focus:
   * @menushell: the object which received the signal
   * @direction: the direction to cycle in
   *
   * A keybinding signal which moves the focus in the
   * given @direction.
   */
340
  menu_shell_signals[CYCLE_FOCUS] =
341
    g_signal_new_class_handler (I_("cycle-focus"),
342 343 344 345 346 347 348 349
                                G_OBJECT_CLASS_TYPE (object_class),
                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                G_CALLBACK (gtk_real_menu_shell_cycle_focus),
                                NULL, NULL,
                                _gtk_marshal_VOID__ENUM,
                                G_TYPE_NONE, 1,
                                GTK_TYPE_DIRECTION_TYPE);

Matthias Clasen's avatar
Matthias Clasen committed
350 351 352 353 354 355
  /**
   * GtkMenuShell::move-selected:
   * @menu_shell: the object on which the signal is emitted
   * @distance: +1 to move to the next item, -1 to move to the previous
   *
   * The ::move-selected signal is emitted to move the selection to
356 357
   * another item.
   *
Matthias Clasen's avatar
Matthias Clasen committed
358 359 360
   * Returns: %TRUE to stop the signal emission, %FALSE to continue
   *
   * Since: 2.12
361
   */
362
  menu_shell_signals[MOVE_SELECTED] =
363
    g_signal_new (I_("move-selected"),
364 365 366 367 368 369 370
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkMenuShellClass, move_selected),
                  _gtk_boolean_handled_accumulator, NULL,
                  _gtk_marshal_BOOLEAN__INT,
                  G_TYPE_BOOLEAN, 1,
                  G_TYPE_INT);
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  /**
   * GtkMenuShell::insert:
   * @menu_shell: the object on which the signal is emitted
   * @child: the #GtkMenuItem that is being inserted
   * @position: the position at which the insert occurs
   *
   * The ::insert signal is emitted when a new #GtkMenuItem is added to
   * a #GtkMenuShell.  A separate signal is used instead of
   * GtkContainer::add because of the need for an additional position
   * parameter.
   *
   * The inverse of this signal is the GtkContainer::removed signal.
   *
   * Since: 3.2
   **/
  menu_shell_signals[INSERT] =
    g_signal_new (I_("insert"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuShellClass, insert),
                  NULL, NULL,
                  _gtk_marshal_VOID__OBJECT_INT,
                  G_TYPE_NONE, 2, GTK_TYPE_WIDGET, G_TYPE_INT);


397 398
  binding_set = gtk_binding_set_by_class (klass);
  gtk_binding_entry_add_signal (binding_set,
399 400
                                GDK_KEY_Escape, 0,
                                "cancel", 0);
401
  gtk_binding_entry_add_signal (binding_set,
402 403 404 405
                                GDK_KEY_Return, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
406
  gtk_binding_entry_add_signal (binding_set,
407 408 409 410
                                GDK_KEY_ISO_Enter, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
411
  gtk_binding_entry_add_signal (binding_set,
412 413 414 415
                                GDK_KEY_KP_Enter, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
416
  gtk_binding_entry_add_signal (binding_set,
417 418 419 420
                                GDK_KEY_space, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                FALSE);
421
  gtk_binding_entry_add_signal (binding_set,
422 423 424 425
                                GDK_KEY_KP_Space, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                FALSE);
426
  gtk_binding_entry_add_signal (binding_set,
427 428
                                GDK_KEY_F10, 0,
                                "cycle-focus", 1,
429 430
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
  gtk_binding_entry_add_signal (binding_set,
431 432
                                GDK_KEY_F10, GDK_SHIFT_MASK,
                                "cycle-focus", 1,
433
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
434

435 436 437 438 439 440 441 442 443 444 445 446
  /**
   * GtkMenuShell:take-focus:
   *
   * A boolean that determines whether the menu and its submenus grab the
   * keyboard focus. See gtk_menu_shell_set_take_focus() and
   * gtk_menu_shell_get_take_focus().
   *
   * Since: 2.8
   **/
  g_object_class_install_property (object_class,
                                   PROP_TAKE_FOCUS,
                                   g_param_spec_boolean ("take-focus",
447 448 449 450
                                                         P_("Take Focus"),
                                                         P_("A boolean that determines whether the menu grabs the keyboard focus"),
                                                         TRUE,
                                                         GTK_PARAM_READWRITE));
451

452 453
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_SHELL_ACCESSIBLE);

454
  g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
Elliot Lee's avatar
Elliot Lee committed
455 456
}

Manish Singh's avatar
Manish Singh committed
457
static GType
458
gtk_menu_shell_child_type (GtkContainer *container)
459 460 461 462
{
  return GTK_TYPE_MENU_ITEM;
}

Elliot Lee's avatar
Elliot Lee committed
463 464 465
static void
gtk_menu_shell_init (GtkMenuShell *menu_shell)
{
466 467 468 469 470 471
  GtkMenuShellPrivate *priv;

  priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_shell,
                                      GTK_TYPE_MENU_SHELL,
                                      GtkMenuShellPrivate);
  menu_shell->priv = priv;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  priv->take_focus = TRUE;
}

static void
gtk_menu_shell_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);

  switch (prop_id)
    {
    case PROP_TAKE_FOCUS:
      gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
495 496 497 498
gtk_menu_shell_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
499 500 501 502 503 504 505 506 507 508 509 510
{
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);

  switch (prop_id)
    {
    case PROP_TAKE_FOCUS:
      g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
Elliot Lee's avatar
Elliot Lee committed
511 512
}

513 514 515 516
static void
gtk_menu_shell_finalize (GObject *object)
{
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
517
  GtkMenuShellPrivate *priv = menu_shell->priv;
518 519 520 521 522 523

  if (priv->mnemonic_hash)
    _gtk_mnemonic_hash_free (priv->mnemonic_hash);
  if (priv->key_hash)
    _gtk_key_hash_free (priv->key_hash);

Matthias Clasen's avatar
Matthias Clasen committed
524
  G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
525 526 527
}


528 529
static void
gtk_menu_shell_dispose (GObject *object)
530
{
531
  gtk_menu_shell_deactivate (GTK_MENU_SHELL (object));
532 533 534 535

  G_OBJECT_CLASS (gtk_menu_shell_parent_class)->dispose (object);
}

536 537 538 539 540 541 542 543
/**
 * gtk_menu_shell_append:
 * @menu_shell: a #GtkMenuShell
 * @child: The #GtkMenuItem to add
 *
 * Adds a new #GtkMenuItem to the end of the menu shell's
 * item list.
 */
Elliot Lee's avatar
Elliot Lee committed
544 545
void
gtk_menu_shell_append (GtkMenuShell *menu_shell,
546
                       GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
547 548 549 550
{
  gtk_menu_shell_insert (menu_shell, child, -1);
}

551 552 553 554 555 556 557 558
/**
 * gtk_menu_shell_prepend:
 * @menu_shell: a #GtkMenuShell
 * @child: The #GtkMenuItem to add
 *
 * Adds a new #GtkMenuItem to the beginning of the menu shell's
 * item list.
 */
Elliot Lee's avatar
Elliot Lee committed
559 560
void
gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
561
                        GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
562 563 564 565
{
  gtk_menu_shell_insert (menu_shell, child, 0);
}

566 567 568 569 570 571 572 573 574 575
/**
 * gtk_menu_shell_insert:
 * @menu_shell: a #GtkMenuShell
 * @child: The #GtkMenuItem to add
 * @position: The position in the item list where @child
 *     is added. Positions are numbered from 0 to n-1
 *
 * Adds a new #GtkMenuItem to the menu shell's item list
 * at the position indicated by @position.
 */
Elliot Lee's avatar
Elliot Lee committed
576 577
void
gtk_menu_shell_insert (GtkMenuShell *menu_shell,
578 579
                       GtkWidget    *child,
                       gint          position)
Elliot Lee's avatar
Elliot Lee committed
580 581 582 583
{
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
  g_return_if_fail (GTK_IS_MENU_ITEM (child));

584
  g_signal_emit (menu_shell, menu_shell_signals[INSERT], 0, child, position);
585 586 587 588
}

static void
gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
589 590
                            GtkWidget    *child,
                            gint          position)
591
{
592 593 594
  GtkMenuShellPrivate *priv = menu_shell->priv;

  priv->children = g_list_insert (priv->children, child, position);
595

Elliot Lee's avatar
Elliot Lee committed
596 597 598
  gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
}

599 600 601 602 603 604 605 606 607
/**
 * gtk_menu_shell_deactivate:
 * @menu_shell: a #GtkMenuShell
 *
 * Deactivates the menu shell.
 *
 * Typically this results in the menu shell being erased
 * from the screen.
 */
Elliot Lee's avatar
Elliot Lee committed
608 609 610 611 612
void
gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
{
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));

Manish Singh's avatar
Manish Singh committed
613
  g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
Elliot Lee's avatar
Elliot Lee committed
614 615 616 617 618
}

static void
gtk_menu_shell_realize (GtkWidget *widget)
{
619 620
  GtkAllocation allocation;
  GdkWindow *window;
Elliot Lee's avatar
Elliot Lee committed
621 622
  GdkWindowAttr attributes;
  gint attributes_mask;
623
  GtkStyleContext *context;
Elliot Lee's avatar
Elliot Lee committed
624

625
  gtk_widget_set_realized (widget, TRUE);
Elliot Lee's avatar
Elliot Lee committed
626

627 628 629 630 631 632
  gtk_widget_get_allocation (widget, &allocation);

  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
Elliot Lee's avatar
Elliot Lee committed
633 634 635 636 637
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_EXPOSURE_MASK |
638 639 640 641 642
                            GDK_BUTTON_PRESS_MASK |
                            GDK_BUTTON_RELEASE_MASK |
                            GDK_KEY_PRESS_MASK |
                            GDK_ENTER_NOTIFY_MASK |
                            GDK_LEAVE_NOTIFY_MASK);
Elliot Lee's avatar
Elliot Lee committed
643

644
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
Elliot Lee's avatar
Elliot Lee committed
645

646 647 648 649 650
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
  gdk_window_set_user_data (window, widget);

651 652
  context = gtk_widget_get_style_context (widget);
  gtk_style_context_set_background (context, window);
Elliot Lee's avatar
Elliot Lee committed
653 654
}

655 656
static void
gtk_menu_shell_activate (GtkMenuShell *menu_shell)
657
{
658 659 660
  GtkMenuShellPrivate *priv = menu_shell->priv;

  if (!priv->active)
661
    {
662 663 664 665 666 667 668
      GdkDevice *device;

      device = gtk_get_current_event_device ();

      _gtk_menu_shell_set_grab_device (menu_shell, device);
      gtk_device_grab_add (GTK_WIDGET (menu_shell), device, TRUE);

669 670
      priv->have_grab = TRUE;
      priv->active = TRUE;
671 672 673
    }
}

Elliot Lee's avatar
Elliot Lee committed
674 675
static gint
gtk_menu_shell_button_press (GtkWidget      *widget,
676
                             GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
677 678
{
  GtkMenuShell *menu_shell;
679
  GtkMenuShellPrivate *priv;
Elliot Lee's avatar
Elliot Lee committed
680
  GtkWidget *menu_item;
681
  GtkWidget *parent;
Elliot Lee's avatar
Elliot Lee committed
682 683 684 685 686

  if (event->type != GDK_BUTTON_PRESS)
    return FALSE;

  menu_shell = GTK_MENU_SHELL (widget);
687
  priv = menu_shell->priv;
Elliot Lee's avatar
Elliot Lee committed
688

689 690
  if (priv->parent_menu_shell)
    return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
691 692 693

  menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);

694
  if (menu_item && _gtk_menu_item_is_selectable (menu_item))
Elliot Lee's avatar
Elliot Lee committed
695
    {
696 697
      parent = gtk_widget_get_parent (menu_item);

698
      if (menu_item != GTK_MENU_SHELL (parent)->priv->active_menu_item)
699
        {
700 701 702 703 704 705 706 707 708
          /*  select the menu item *before* activating the shell, so submenus
           *  which might be open are closed the friendly way. If we activate
           *  (and thus grab) this menu shell first, we might get grab_broken
           *  events which will close the entire menu hierarchy. Selecting the
           *  menu item also fixes up the state as if enter_notify() would
           *  have run before (which normally selects the item).
           */
          if (GTK_MENU_SHELL_GET_CLASS (parent)->submenu_placement != GTK_TOP_BOTTOM)
            gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), menu_item);
709
        }
Elliot Lee's avatar
Elliot Lee committed
710
    }
711

712
  if (!priv->active || !priv->button)
Elliot Lee's avatar
Elliot Lee committed
713
    {
714
      gboolean initially_active = priv->active;
715

716
      priv->button = event->button;
717

718
      if (menu_item)
719
        {
720
          if (_gtk_menu_item_is_selectable (menu_item) &&
721
              gtk_widget_get_parent (menu_item) == widget &&
722
              menu_item != priv->active_menu_item)
723
            {
724
              gtk_menu_shell_activate (menu_shell);
725
              priv->button = event->button;
726 727 728

              if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
                {
729
                  priv->activate_time = event->time;
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
                  gtk_menu_shell_select_item (menu_shell, menu_item);
                }
            }
        }
      else
        {
          if (!initially_active)
            {
              gboolean window_drag = FALSE;

              gtk_widget_style_get (widget,
                                    "window-dragging", &window_drag,
                                    NULL);

              if (window_drag)
                {
                  gtk_menu_shell_deactivate (menu_shell);
                  gtk_window_begin_move_drag (GTK_WINDOW (gtk_widget_get_toplevel (widget)),
                                              event->button,
                                              event->x_root,
                                              event->y_root,
                                              event->time);
                }
753 754
            }
        }
Elliot Lee's avatar
Elliot Lee committed
755 756 757 758 759
    }
  else
    {
      widget = gtk_get_event_widget ((GdkEvent*) event);
      if (widget == GTK_WIDGET (menu_shell))
760 761 762 763
        {
          gtk_menu_shell_deactivate (menu_shell);
          g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
        }
Elliot Lee's avatar
Elliot Lee committed
764 765
    }

766 767 768 769
  if (menu_item &&
      _gtk_menu_item_is_selectable (menu_item) &&
      GTK_MENU_ITEM (menu_item)->priv->submenu != NULL &&
      !gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
770 771 772 773 774
    {
      _gtk_menu_item_popup_submenu (menu_item, FALSE);
      priv->activated_submenu = TRUE;
    }

Elliot Lee's avatar
Elliot Lee committed
775 776 777
  return TRUE;
}

778 779
static gboolean
gtk_menu_shell_grab_broken (GtkWidget          *widget,
780
                            GdkEventGrabBroken *event)
781
{
782
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
783
  GtkMenuShellPrivate *priv = menu_shell->priv;
784

785
  if (priv->have_xgrab && event->grab_window == NULL)
786
    {
787
      /* Unset the active menu item so gtk_menu_popdown() doesn't see it. */
788
      gtk_menu_shell_deselect (menu_shell);
789 790 791 792 793 794 795
      gtk_menu_shell_deactivate (menu_shell);
      g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
    }

  return TRUE;
}

Elliot Lee's avatar
Elliot Lee committed
796 797
static gint
gtk_menu_shell_button_release (GtkWidget      *widget,
798
                               GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
799
{
800
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
801
  GtkMenuShellPrivate *priv = menu_shell->priv;
Elliot Lee's avatar
Elliot Lee committed
802

803
  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
804
    {
805 806 807
      GtkWidget *menu_item;
      gboolean   deactivate = TRUE;

808 809 810 811 812 813
      if (priv->button && (event->button != priv->button))
        {
          priv->button = 0;
          if (priv->parent_menu_shell)
            return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
        }
814

815
      priv->button = 0;
Owen Taylor's avatar
Owen Taylor committed
816
      menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
817

818
      if ((event->time - priv->activate_time) > MENU_SHELL_TIMEOUT)
819
        {
820
          if (menu_item && (priv->active_menu_item == menu_item) &&
821 822
              _gtk_menu_item_is_selectable (menu_item))
            {
823
              GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->priv->submenu;
824

825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
              if (submenu == NULL)
                {
                  gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
                  deactivate = FALSE;
                }
              else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
                       priv->activated_submenu)
                {
                  gint popdown_delay;
                  GTimeVal *popup_time;
                  gint64 usec_since_popup = 0;

                  g_object_get (gtk_widget_get_settings (widget),
                                "gtk-menu-popdown-delay", &popdown_delay,
                                NULL);

                  popup_time = g_object_get_data (G_OBJECT (submenu),
                                                  "gtk-menu-exact-popup-time");

                  if (popup_time)
                    {
                      GTimeVal current_time;

                      g_get_current_time (&current_time);

                      usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
                                          (gint64) current_time.tv_usec -
                                          (gint64) popup_time->tv_sec * 1000 * 1000 -
                                          (gint64) popup_time->tv_usec);

                      g_object_set_data (G_OBJECT (submenu),
                                         "gtk-menu-exact-popup-time", NULL);
                    }

859 860 861 862
                  /* Only close the submenu on click if we opened the
                   * menu explicitely (usec_since_popup == 0) or
                   * enough time has passed since it was opened by
                   * GtkMenuItem's timeout (usec_since_popup > delay).
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
                   */
                  if (!priv->activated_submenu &&
                      (usec_since_popup == 0 ||
                       usec_since_popup > popdown_delay * 1000))
                    {
                      _gtk_menu_item_popdown_submenu (menu_item);
                    }
                  else
                    {
                      gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
                    }

                  deactivate = FALSE;
                }
            }
          else if (menu_item &&
                   !_gtk_menu_item_is_selectable (menu_item) &&
                   GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
            {
              deactivate = FALSE;
            }
884
          else if (priv->parent_menu_shell)
885
            {
886 887
              priv->active = TRUE;
              gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
888 889 890
              deactivate = FALSE;
            }

891 892 893
          /* If we ended up on an item with a submenu, leave the menu up. */
          if (menu_item &&
              (priv->active_menu_item == menu_item) &&
894 895 896 897 898
              GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
            {
              deactivate = FALSE;
            }
        }
899
      else /* a very fast press-release */
900 901
        {
          /* We only ever want to prevent deactivation on the first
Owen Taylor's avatar
Owen Taylor committed
902
           * press/release. Setting the time to zero is a bit of a
903 904 905 906 907
           * hack, since we could be being triggered in the first
           * few fractions of a second after a server time wraparound.
           * the chances of that happening are ~1/10^6, without
           * serious harm if we lose.
           */
908
          priv->activate_time = 0;
909 910 911
          deactivate = FALSE;
        }

Elliot Lee's avatar
Elliot Lee committed
912
      if (deactivate)
913 914 915 916 917 918
        {
          gtk_menu_shell_deactivate (menu_shell);
          g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
        }

      priv->activated_submenu = FALSE;
Elliot Lee's avatar
Elliot Lee committed
919 920 921 922 923
    }

  return TRUE;
}

924 925 926 927
void
_gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
                                   gboolean      keyboard_mode)
{
928
  menu_shell->priv->keyboard_mode = keyboard_mode;
929 930 931 932 933
}

gboolean
_gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
{
934
  return menu_shell->priv->keyboard_mode;
935 936 937 938 939 940 941 942 943 944 945
}

void
_gtk_menu_shell_update_mnemonics (GtkMenuShell *menu_shell)
{
  GtkMenuShell *target;
  gboolean auto_mnemonics;
  gboolean found;
  gboolean mnemonics_visible;

  g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
946 947
                "gtk-auto-mnemonics", &auto_mnemonics,
                NULL);
948 949 950 951 952 953 954 955

  if (!auto_mnemonics)
    return;

  target = menu_shell;
  found = FALSE;
  while (target)
    {
956
      GtkMenuShellPrivate *priv = target->priv;
957 958
      GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));

959 960 961 962 963 964
      /* The idea with keyboard mode is that once you start using
       * the keyboard to navigate the menus, we show mnemonics
       * until the menu navigation is over. To that end, we spread
       * the keyboard mode upwards in the menu hierarchy here.
       * Also see gtk_menu_popup, where we inherit it downwards.
       */
965 966
      if (menu_shell->priv->keyboard_mode)
        target->priv->keyboard_mode = TRUE;
967 968 969 970 971 972 973 974

      /* While navigating menus, the first parent menu with an active
       * item is the one where mnemonics are effective, as can be seen
       * in gtk_menu_shell_key_press below.
       * We also show mnemonics in context menus. The grab condition is
       * necessary to ensure we remove underlines from menu bars when
       * dismissing menus.
       */
975 976
      mnemonics_visible = target->priv->keyboard_mode &&
                          (((target->priv->active_menu_item || priv->in_unselectable_item) && !found) ||
977
                           (target == menu_shell &&
978
                            !target->priv->parent_menu_shell &&
979
                            gtk_widget_has_grab (GTK_WIDGET (target))));
980 981 982 983 984

      /* While menus are up, only show underlines inside the menubar,
       * not in the entire window.
       */
      if (GTK_IS_MENU_BAR (target))
985
        {
986
          gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
987 988 989
          _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
                                                          mnemonics_visible);
        }
990
      else
991
        gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
992

993
      if (target->priv->active_menu_item || priv->in_unselectable_item)
994 995
        found = TRUE;

996
      target = GTK_MENU_SHELL (target->priv->parent_menu_shell);
997 998 999
    }
}

1000
static gint
1001
gtk_menu_shell_key_press (GtkWidget   *widget,
1002
                          GdkEventKey *event)
1003
{
1004
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1005
  GtkMenuShellPrivate *priv = menu_shell->priv;
1006
  gboolean enable_mnemonics;
1007

1008
  priv->keyboard_mode = TRUE;
1009

1010 1011 1012
  if (!(priv->active_menu_item || priv->in_unselectable_item) &&
      priv->parent_menu_shell)
    return gtk_widget_event (priv->parent_menu_shell, (GdkEvent *)event);
1013

1014
  if (gtk_bindings_activate_event (G_OBJECT (widget), event))
1015 1016
    return TRUE;

1017
  g_object_get (gtk_widget_get_settings (widget),
1018 1019
                "gtk-enable-mnemonics", &enable_mnemonics,
                NULL);
1020 1021 1022 1023 1024

  if (enable_mnemonics)
    return gtk_menu_shell_activate_mnemonic (menu_shell, event);

  return FALSE;
1025 1026
}

Elliot Lee's avatar
Elliot Lee committed
1027 1028
static gint
gtk_menu_shell_enter_notify (GtkWidget        *widget,
1029
                             GdkEventCrossing *event)
Elliot Lee's avatar
Elliot Lee committed
1030
{
1031
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1032
  GtkMenuShellPrivate *priv = menu_shell->priv;
1033

1034 1035 1036 1037 1038
  if (event->mode == GDK_CROSSING_GTK_GRAB ||
      event->mode == GDK_CROSSING_GTK_UNGRAB ||
      event->mode == GDK_CROSSING_STATE_CHANGED)
    return TRUE;

1039
  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
1040
    {
1041
      GtkWidget *menu_item;
1042
      GtkWidget *parent;
1043

Elliot Lee's avatar
Elliot Lee committed
1044 1045
      menu_item = gtk_get_event_widget ((GdkEvent*) event);

1046 1047 1048 1049 1050 1051 1052 1053 1054
      if (!menu_item)
        return TRUE;

      if (GTK_IS_MENU_ITEM (menu_item) &&
          !_gtk_menu_item_is_selectable (menu_item))
        {
          priv->in_unselectable_item = TRUE;
          return TRUE;
        }
1055

1056 1057
      parent = gtk_widget_get_parent (menu_item);
      if (parent == widget &&
1058 1059 1060 1061
          GTK_IS_MENU_ITEM (menu_item))
        {
          if (priv->ignore_enter)
            return TRUE;
1062

1063
          if (event->detail != GDK_NOTIFY_INFERIOR)
1064
            {
1065
              if ((gtk_widget_get_state_flags (menu_item) & GTK_STATE_FLAG_PRELIGHT) == 0)
1066
                gtk_menu_shell_select_item (menu_shell, menu_item);
1067 1068 1069 1070 1071 1072 1073 1074 1075

              /* If any mouse button is down, and there is a submenu
               * that is not yet visible, activate it. It's sufficient
               * to check for any button's mask (not only the one
               * matching menu_shell->button), because there is no
               * situation a mouse button could be pressed while
               * entering a menu item where we wouldn't want to show
               * its submenu.
               */
1076
              if ((event->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK)) &&
1077
                  GTK_MENU_ITEM (menu_item)->priv->submenu != NULL)
1078
                {
1079
                  GTK_MENU_SHELL (parent)->priv->activated_submenu = TRUE;
1080

1081
                  if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
1082 1083
                    {
                      gboolean touchscreen_mode;
1084

1085 1086 1087 1088 1089 1090 1091
                      g_object_get (gtk_widget_get_settings (widget),
                                    "gtk-touchscreen-mode", &touchscreen_mode,
                                    NULL);

                      if (touchscreen_mode)
                        _gtk_menu_item_popup_submenu (menu_item, TRUE);
                    }
1092
                }
1093 1094 1095 1096 1097 1098
            }
        }
      else if (priv->parent_menu_shell)
        {
          gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
        }
Elliot Lee's avatar
Elliot Lee committed
1099 1100 1101 1102 1103 1104 1105
    }

  return TRUE;
}

static gint
gtk_menu_shell_leave_notify (GtkWidget        *widget,
1106
                             GdkEventCrossing *event)
Elliot Lee's avatar
Elliot Lee committed
1107
{
1108 1109 1110 1111 1112
  if (event->mode == GDK_CROSSING_GTK_GRAB ||
      event->mode == GDK_CROSSING_GTK_GRAB ||
      event->mode == GDK_CROSSING_STATE_CHANGED)
    return TRUE;

1113
  if (gtk_widget_get_visible (widget))
Elliot Lee's avatar
Elliot Lee committed
1114
    {
1115
      GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1116
      GtkMenuShellPrivate *priv = menu_shell->priv;
1117 1118
      GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
      GtkMenuItem *menu_item;
Elliot Lee's avatar
Elliot Lee committed
1119

1120
      if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1121
        return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1122 1123 1124

      menu_item = GTK_MENU_ITEM (event_widget);

1125
      if (!_gtk_menu_item_is_selectable (event_widget))
1126 1127 1128 1129
        {
          priv->in_unselectable_item = TRUE;
          return TRUE;
        }
Tim Janik's avatar
Tim Janik committed
1130

1131
      if ((priv->active_menu_item == event_widget) &&
1132
          (menu_item->priv->submenu == NULL))
1133 1134
        {
          if ((event->detail != GDK_NOTIFY_INFERIOR) &&
1135
              (gtk_widget_get_state_flags (GTK_WIDGET (menu_item)) & GTK_STATE_FLAG_PRELIGHT) != 0)
1136 1137 1138 1139 1140 1141 1142 1143
            {
              gtk_menu_shell_deselect (menu_shell);
            }
        }
      else if (priv->parent_menu_shell)
        {
          gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
        }
Elliot Lee's avatar
Elliot Lee committed
1144 1145 1146 1147 1148
    }

  return TRUE;
}

1149 1150
static void
gtk_menu_shell_screen_changed (GtkWidget *widget,
1151
                               GdkScreen *previous_screen)
1152 1153 1154 1155
{
  gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
}

Elliot Lee's avatar
Elliot Lee committed
1156 1157
static void
gtk_menu_shell_add (GtkContainer *container,
1158
                    GtkWidget    *widget)
Elliot Lee's avatar
Elliot Lee committed
1159 1160 1161 1162 1163 1164
{
  gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
}

static void
gtk_menu_shell_remove (GtkContainer *container,
1165
                       GtkWidget    *widget)
Elliot Lee's avatar
Elliot Lee committed
1166
{
1167
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1168
  GtkMenuShellPrivate *priv = menu_shell->priv;
1169
  gint was_visible;
1170

1171
  was_visible = gtk_widget_get_visible (widget);
1172 1173 1174
  priv->children = g_list_remove (priv->children, widget);

  if (widget == priv->active_menu_item)
1175
    {
1176 1177
      g_signal_emit_by_name (priv->active_menu_item, "deselect");
      priv->active_menu_item = NULL;
1178 1179
    }

1180
  gtk_widget_unparent (widget);
1181 1182

  /* Queue resize regardless of gtk_widget_get_visible (container),
1183 1184 1185
   * since that's what is needed by toplevels.
   */
  if (was_visible)
Elliot Lee's avatar
Elliot Lee committed
1186 1187 1188 1189
    gtk_widget_queue_resize (GTK_WIDGET (container));
}

static void
1190
gtk_menu_shell_forall (GtkContainer *container,
1191 1192 1193
                       gboolean      include_internals,
                       GtkCallback   callback,
                       gpointer      callback_data)
Elliot Lee's avatar
Elliot Lee committed
1194
{
1195
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
Elliot Lee's avatar
Elliot Lee committed
1196 1197 1198
  GtkWidget *child;
  GList *children;

1199
  children = menu_shell->priv->children;
Elliot Lee's avatar
Elliot Lee committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
  while (children)
    {
      child = children->data;
      children = children->next;

      (* callback) (child, callback_data);
    }
}


static void
gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
{
1213 1214 1215
  GtkMenuShellPrivate *priv = menu_shell->priv;

  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
1216
    {
1217 1218 1219
      priv->button = 0;
      priv->active = FALSE;
      priv->activate_time = 0;
Elliot Lee's avatar
Elliot Lee committed
1220

1221 1222 1223 1224 1225
      if (priv->active_menu_item)
        {
          gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
          priv->active_menu_item = NULL;
        }
Elliot Lee's avatar
Elliot Lee committed
1226

1227 1228 1229
      if (priv->have_grab)
        {
          priv->have_grab = FALSE;
1230
          gtk_device_grab_remove (GTK_WIDGET (menu_shell), priv->grab_pointer);
1231 1232 1233
        }
      if (priv->have_xgrab)
        {
1234 1235
          GdkDevice *keyboard;

1236
          gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1237 1238 1239 1240
          keyboard = gdk_device_get_associated_device (priv->grab_pointer);

          if (keyboard)
            gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1241

1242 1243
          priv->have_xgrab = FALSE;
        }
1244

1245
      priv->keyboard_mode = FALSE;
1246
      _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1247 1248

      _gtk_menu_shell_update_mnemonics (menu_shell);
Elliot Lee's avatar
Elliot Lee committed
1249 1250 1251 1252 1253
    }
}

static gint
gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1254
                        GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
1255
{
1256
  GtkWidget *parent;
Elliot Lee's avatar
Elliot Lee committed
1257 1258 1259 1260

  g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
  g_return_val_if_fail (child != NULL, FALSE);

1261
  parent = gtk_widget_get_parent (child);
1262
  while (GTK_IS_MENU_SHELL (parent))
Elliot Lee's avatar
Elliot Lee committed
1263
    {
1264
      if (parent == (GtkWidget*) menu_shell)
1265 1266
        return TRUE;
      parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
Elliot Lee's avatar
Elliot Lee committed
1267 1268 1269 1270
    }

  return FALSE;
}
Owen Taylor's avatar
Owen Taylor committed
1271

1272
static GtkWidget*
Owen Taylor's avatar
Owen Taylor committed
1273
gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1274
                         GdkEvent     *event)
Owen Taylor's avatar
Owen Taylor committed
1275 1276 1277 1278
{
  GtkWidget *menu_item;

  menu_item = gtk_get_event_widget ((GdkEvent*) event);
1279

Owen Taylor's avatar
Owen Taylor committed
1280
  while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1281
    menu_item = gtk_widget_get_parent (menu_item);
Owen Taylor's avatar
Owen Taylor committed
1282 1283 1284 1285 1286 1287 1288

  if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
    return menu_item;
  else
    return NULL;
}

1289 1290
/* Handlers for action signals */

1291 1292 1293 1294 1295 1296 1297
/**
 * gtk_menu_shell_select_item:
 * @menu_shell: a #GtkMenuShell
 * @menu_item: The #GtkMenuItem to select
 *
 * Selects the menu item from the menu shell.
 */
1298
void
1299
gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1300
                            GtkWidget    *menu_item)
1301
{
1302
  GtkMenuShellPrivate *priv = menu_shell->priv;
1303 1304
  GtkMenuShellClass *class;

1305 1306 1307
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));

1308 1309
  class = GTK_MENU_SHELL_GET_CLASS (menu_shell);

1310
  if (class->select_item &&
1311 1312
      !(priv->active &&
        priv->active_menu_item == menu_item))
1313 1314 1315
    class->select_item (menu_shell, menu_item);
}

1316
void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1317
                                   GtkSubmenuPlacement  placement);
1318 1319 1320

static void
gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1321
                                 GtkWidget    *menu_item)
1322
{
1323
  GtkMenuShellPrivate *priv = menu_shell->priv;
1324 1325
  GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);

1326
  if (priv->active_menu_item)
1327
    {
1328 1329
      gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
      priv->active_menu_item = NULL;
1330
    }
1331

1332
  if (!_gtk_menu_item_is_selectable (menu_item))
1333 1334 1335 1336 1337
    {
      priv->in_unselectable_item = TRUE;
      _gtk_menu_shell_update_mnemonics (menu_shell);
      return;
    }
1338

1339
  gtk_menu_shell_activate (menu_shell);
1340

1341
  priv->active_menu_item = menu_item;
1342
  if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1343 1344
    _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
                                  GTK_LEFT_RIGHT);
1345
  else
1346 1347 1348
    _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
                                  GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
  gtk_menu_item_select (GTK_MENU_ITEM (priv->active_menu_item));
1349

1350 1351
  _gtk_menu_shell_update_mnemonics (menu_shell);

1352 1353 1354
  /* This allows the bizarre radio buttons-with-submenus-display-history
   * behavior
   */
1355
  if (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu)
1356
    gtk_widget_activate (priv->active_menu_item);