gtkmenushell.c 70.8 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * 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/. 
 */

25 26 27 28 29 30 31 32 33 34 35 36
/**
 * 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.
37
 *
38
 * # Terminology
39
 *
40
 * A menu item can be “selected”, this means that it is displayed
41 42 43
 * in the prelight state, and if it has a submenu, that submenu
 * will be popped up.
 *
44
 * A menu is “active” when it is visible onscreen and the user
45 46 47 48 49 50 51 52 53 54 55 56
 * 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.
 *
 * There is also is a concept of the current menu and a current
 * menu item. The current menu item is the selected menu item
 * that is furthest down in the hierarchy. (Every active menu shell
 * does not necessarily contain a selected menu item, but if
 * it does, then the 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.
57
 */
58
#include "config.h"
59

60
#include "gtkbindings.h"
61
#include "gtkkeyhash.h"
62
#include "gtklabel.h"
63
#include "gtkmain.h"
64
#include "gtkmarshalers.h"
65
#include "gtkmenubar.h"
66
#include "gtkmenuitemprivate.h"
67
#include "gtkmenushellprivate.h"
68
#include "gtkmnemonichash.h"
69
#include "gtkwindow.h"
70
#include "gtkwindowprivate.h"
71
#include "gtkprivate.h"
72
#include "gtkmain.h"
73
#include "gtkintl.h"
74
#include "gtktypebuiltins.h"
75
#include "gtkmodelmenuitem.h"
76
#include "gtkwidgetprivate.h"
77
#include "gtklabelprivate.h"
78

79
#include "deprecated/gtktearoffmenuitem.h"
80

81
#include "a11y/gtkmenushellaccessible.h"
Elliot Lee's avatar
Elliot Lee committed
82

83

Elliot Lee's avatar
Elliot Lee committed
84
#define MENU_SHELL_TIMEOUT   500
85 86
#define MENU_POPUP_DELAY     225
#define MENU_POPDOWN_DELAY   1000
Elliot Lee's avatar
Elliot Lee committed
87

88 89 90 91 92
#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
93 94
enum {
  DEACTIVATE,
Tim Janik's avatar
Tim Janik committed
95
  SELECTION_DONE,
96 97 98
  MOVE_CURRENT,
  ACTIVATE_CURRENT,
  CANCEL,
99
  CYCLE_FOCUS,
100
  MOVE_SELECTED,
101
  INSERT,
Elliot Lee's avatar
Elliot Lee committed
102 103 104
  LAST_SIGNAL
};

105 106 107 108 109
enum {
  PROP_0,
  PROP_TAKE_FOCUS
};

110

111 112 113 114 115 116 117 118
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
119
static void gtk_menu_shell_realize           (GtkWidget         *widget);
120
static void gtk_menu_shell_finalize          (GObject           *object);
121
static void gtk_menu_shell_dispose           (GObject           *object);
Elliot Lee's avatar
Elliot Lee committed
122
static gint gtk_menu_shell_button_press      (GtkWidget         *widget,
123
                                              GdkEventButton    *event);
Elliot Lee's avatar
Elliot Lee committed
124
static gint gtk_menu_shell_button_release    (GtkWidget         *widget,
125 126 127
                                              GdkEventButton    *event);
static gint gtk_menu_shell_key_press         (GtkWidget         *widget,
                                              GdkEventKey       *event);
Elliot Lee's avatar
Elliot Lee committed
128
static gint gtk_menu_shell_enter_notify      (GtkWidget         *widget,
129
                                              GdkEventCrossing  *event);
Elliot Lee's avatar
Elliot Lee committed
130
static gint gtk_menu_shell_leave_notify      (GtkWidget         *widget,
131
                                              GdkEventCrossing  *event);
132
static void gtk_menu_shell_screen_changed    (GtkWidget         *widget,
133
                                              GdkScreen         *previous_screen);
134
static gboolean gtk_menu_shell_grab_broken       (GtkWidget         *widget,
135
                                              GdkEventGrabBroken *event);
Elliot Lee's avatar
Elliot Lee committed
136
static void gtk_menu_shell_add               (GtkContainer      *container,
137
                                              GtkWidget         *widget);
Elliot Lee's avatar
Elliot Lee committed
138
static void gtk_menu_shell_remove            (GtkContainer      *container,
139
                                              GtkWidget         *widget);
140
static void gtk_menu_shell_forall            (GtkContainer      *container,
141 142 143
                                              gboolean           include_internals,
                                              GtkCallback        callback,
                                              gpointer           callback_data);
144
static void gtk_menu_shell_real_insert       (GtkMenuShell *menu_shell,
145 146
                                              GtkWidget    *child,
                                              gint          position);
Elliot Lee's avatar
Elliot Lee committed
147 148
static void gtk_real_menu_shell_deactivate   (GtkMenuShell      *menu_shell);
static gint gtk_menu_shell_is_item           (GtkMenuShell      *menu_shell,
149
                                              GtkWidget         *child);
Owen Taylor's avatar
Owen Taylor committed
150
static GtkWidget *gtk_menu_shell_get_item    (GtkMenuShell      *menu_shell,
151
                                              GdkEvent          *event);
Manish Singh's avatar
Manish Singh committed
152
static GType    gtk_menu_shell_child_type  (GtkContainer      *container);
153
static void gtk_menu_shell_real_select_item  (GtkMenuShell      *menu_shell,
154
                                              GtkWidget         *menu_item);
155
static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell   *menu_shell); 
Elliot Lee's avatar
Elliot Lee committed
156

157
static void gtk_real_menu_shell_move_current (GtkMenuShell      *menu_shell,
158
                                              GtkMenuDirectionType direction);
159
static void gtk_real_menu_shell_activate_current (GtkMenuShell      *menu_shell,
160
                                                  gboolean           force_hide);
161
static void gtk_real_menu_shell_cancel           (GtkMenuShell      *menu_shell);
162
static void gtk_real_menu_shell_cycle_focus      (GtkMenuShell      *menu_shell,
163
                                                  GtkDirectionType   dir);
Elliot Lee's avatar
Elliot Lee committed
164

165 166
static void     gtk_menu_shell_reset_key_hash    (GtkMenuShell *menu_shell);
static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
167
                                                  GdkEventKey  *event);
168
static gboolean gtk_menu_shell_real_move_selected (GtkMenuShell  *menu_shell, 
169
                                                   gint           distance);
170

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

173
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
Elliot Lee's avatar
Elliot Lee committed
174 175 176 177

static void
gtk_menu_shell_class_init (GtkMenuShellClass *klass)
{
Manish Singh's avatar
Manish Singh committed
178
  GObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
179 180 181
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

182 183
  GtkBindingSet *binding_set;

Manish Singh's avatar
Manish Singh committed
184
  object_class = (GObjectClass*) klass;
Elliot Lee's avatar
Elliot Lee committed
185 186 187
  widget_class = (GtkWidgetClass*) klass;
  container_class = (GtkContainerClass*) klass;

188 189
  object_class->set_property = gtk_menu_shell_set_property;
  object_class->get_property = gtk_menu_shell_get_property;
190
  object_class->finalize = gtk_menu_shell_finalize;
191
  object_class->dispose = gtk_menu_shell_dispose;
192

193 194 195
  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;
196
  widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
197 198 199
  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;
200
  widget_class->screen_changed = gtk_menu_shell_screen_changed;
201 202 203 204 205 206 207 208 209 210 211 212 213 214

  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;
215
  klass->move_selected = gtk_menu_shell_real_move_selected;
216

217 218 219 220 221 222
  /**
   * 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
223
  menu_shell_signals[DEACTIVATE] =
224
    g_signal_new (I_("deactivate"),
225 226 227 228 229 230
                  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);
231

232 233 234 235 236 237 238
  /**
   * 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
239
  menu_shell_signals[SELECTION_DONE] =
240
    g_signal_new (I_("selection-done"),
241 242 243 244 245 246
                  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);
247

248 249 250 251 252 253 254 255
  /**
   * 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.
   */
256
  menu_shell_signals[MOVE_CURRENT] =
257
    g_signal_new (I_("move-current"),
258 259 260 261 262 263 264
                  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);
265

266 267 268 269 270 271 272 273
  /**
   * 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.
   */
274
  menu_shell_signals[ACTIVATE_CURRENT] =
275
    g_signal_new (I_("activate-current"),
276 277 278 279 280 281 282
                  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);
283

284 285 286 287 288 289 290
  /**
   * 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.
   */
291
  menu_shell_signals[CANCEL] =
292
    g_signal_new (I_("cancel"),
293 294 295 296 297 298
                  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);
299

300 301 302 303 304 305 306 307
  /**
   * 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.
   */
308
  menu_shell_signals[CYCLE_FOCUS] =
309
    g_signal_new_class_handler (I_("cycle-focus"),
310 311 312 313 314 315 316 317
                                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
318 319 320 321 322 323
  /**
   * 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
324 325
   * another item.
   *
Matthias Clasen's avatar
Matthias Clasen committed
326 327 328
   * Returns: %TRUE to stop the signal emission, %FALSE to continue
   *
   * Since: 2.12
329
   */
330
  menu_shell_signals[MOVE_SELECTED] =
331
    g_signal_new (I_("move-selected"),
332 333 334 335 336 337 338
                  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);
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  /**
   * 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);


365 366
  binding_set = gtk_binding_set_by_class (klass);
  gtk_binding_entry_add_signal (binding_set,
367 368
                                GDK_KEY_Escape, 0,
                                "cancel", 0);
369
  gtk_binding_entry_add_signal (binding_set,
370 371 372 373
                                GDK_KEY_Return, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
374
  gtk_binding_entry_add_signal (binding_set,
375 376 377 378
                                GDK_KEY_ISO_Enter, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
379
  gtk_binding_entry_add_signal (binding_set,
380 381 382 383
                                GDK_KEY_KP_Enter, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
384
  gtk_binding_entry_add_signal (binding_set,
385 386 387 388
                                GDK_KEY_space, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                FALSE);
389
  gtk_binding_entry_add_signal (binding_set,
390 391 392 393
                                GDK_KEY_KP_Space, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                FALSE);
394
  gtk_binding_entry_add_signal (binding_set,
395 396
                                GDK_KEY_F10, 0,
                                "cycle-focus", 1,
397 398
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
  gtk_binding_entry_add_signal (binding_set,
399 400
                                GDK_KEY_F10, GDK_SHIFT_MASK,
                                "cycle-focus", 1,
401
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
402

403 404 405 406 407 408 409 410 411 412 413 414
  /**
   * 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",
415 416 417 418
                                                         P_("Take Focus"),
                                                         P_("A boolean that determines whether the menu grabs the keyboard focus"),
                                                         TRUE,
                                                         GTK_PARAM_READWRITE));
419

420
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_SHELL_ACCESSIBLE);
Elliot Lee's avatar
Elliot Lee committed
421 422
}

Manish Singh's avatar
Manish Singh committed
423
static GType
424
gtk_menu_shell_child_type (GtkContainer *container)
425 426 427 428
{
  return GTK_TYPE_MENU_ITEM;
}

Elliot Lee's avatar
Elliot Lee committed
429 430 431
static void
gtk_menu_shell_init (GtkMenuShell *menu_shell)
{
432 433
  menu_shell->priv = gtk_menu_shell_get_instance_private (menu_shell);
  menu_shell->priv->take_focus = TRUE;
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
}

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
456 457 458 459
gtk_menu_shell_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
460 461 462 463 464 465 466 467 468 469 470 471
{
  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
472 473
}

474 475 476 477
static void
gtk_menu_shell_finalize (GObject *object)
{
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
478
  GtkMenuShellPrivate *priv = menu_shell->priv;
479 480 481 482 483 484

  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
485
  G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
486 487 488
}


489 490
static void
gtk_menu_shell_dispose (GObject *object)
491
{
492 493 494 495
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);

  g_clear_pointer (&menu_shell->priv->tracker, gtk_menu_tracker_free);
  gtk_menu_shell_deactivate (menu_shell);
496 497 498 499

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

500 501 502 503 504 505 506 507
/**
 * 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
508 509
void
gtk_menu_shell_append (GtkMenuShell *menu_shell,
510
                       GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
511 512 513 514
{
  gtk_menu_shell_insert (menu_shell, child, -1);
}

515 516 517 518 519 520 521 522
/**
 * 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
523 524
void
gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
525
                        GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
526 527 528 529
{
  gtk_menu_shell_insert (menu_shell, child, 0);
}

530 531 532 533 534 535 536
/**
 * 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
 *
537
 * Adds a new #GtkMenuItem to the menu shell’s item list
538 539
 * at the position indicated by @position.
 */
Elliot Lee's avatar
Elliot Lee committed
540 541
void
gtk_menu_shell_insert (GtkMenuShell *menu_shell,
542 543
                       GtkWidget    *child,
                       gint          position)
Elliot Lee's avatar
Elliot Lee committed
544 545 546 547
{
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
  g_return_if_fail (GTK_IS_MENU_ITEM (child));

548
  g_signal_emit (menu_shell, menu_shell_signals[INSERT], 0, child, position);
549 550 551 552
}

static void
gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
553 554
                            GtkWidget    *child,
                            gint          position)
555
{
556 557 558
  GtkMenuShellPrivate *priv = menu_shell->priv;

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

Elliot Lee's avatar
Elliot Lee committed
560 561 562
  gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
}

563 564 565 566 567 568 569 570 571
/**
 * 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
572 573 574 575 576
void
gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
{
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));

577 578
  if (menu_shell->priv->active)
    g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
Elliot Lee's avatar
Elliot Lee committed
579 580 581 582 583
}

static void
gtk_menu_shell_realize (GtkWidget *widget)
{
584 585
  GtkAllocation allocation;
  GdkWindow *window;
Elliot Lee's avatar
Elliot Lee committed
586 587
  GdkWindowAttr attributes;
  gint attributes_mask;
588
  GtkStyleContext *context;
Elliot Lee's avatar
Elliot Lee committed
589

590
  gtk_widget_set_realized (widget, TRUE);
Elliot Lee's avatar
Elliot Lee committed
591

592 593 594 595 596 597
  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
598 599 600 601 602
  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 |
603 604
                            GDK_BUTTON_PRESS_MASK |
                            GDK_BUTTON_RELEASE_MASK |
605
                            GDK_POINTER_MOTION_MASK |
606 607 608
                            GDK_KEY_PRESS_MASK |
                            GDK_ENTER_NOTIFY_MASK |
                            GDK_LEAVE_NOTIFY_MASK);
Elliot Lee's avatar
Elliot Lee committed
609

610
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
Elliot Lee's avatar
Elliot Lee committed
611

612 613 614
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
615
  gtk_widget_register_window (widget, window);
616

617 618
  context = gtk_widget_get_style_context (widget);
  gtk_style_context_set_background (context, window);
Elliot Lee's avatar
Elliot Lee committed
619 620
}

Matthias Clasen's avatar
Matthias Clasen committed
621 622
static void
gtk_menu_shell_activate (GtkMenuShell *menu_shell)
623
{
624 625 626
  GtkMenuShellPrivate *priv = menu_shell->priv;

  if (!priv->active)
627
    {
628 629 630 631 632 633 634
      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);

635 636
      priv->have_grab = TRUE;
      priv->active = TRUE;
637 638 639
    }
}

Elliot Lee's avatar
Elliot Lee committed
640 641
static gint
gtk_menu_shell_button_press (GtkWidget      *widget,
642
                             GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
643 644
{
  GtkMenuShell *menu_shell;
645
  GtkMenuShellPrivate *priv;
Elliot Lee's avatar
Elliot Lee committed
646
  GtkWidget *menu_item;
647
  GtkWidget *parent;
Elliot Lee's avatar
Elliot Lee committed
648 649 650 651 652

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

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

655 656
  if (priv->parent_menu_shell)
    return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
657 658 659

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

660
  if (menu_item && _gtk_menu_item_is_selectable (menu_item))
Elliot Lee's avatar
Elliot Lee committed
661
    {
662 663
      parent = gtk_widget_get_parent (menu_item);

664
      if (menu_item != GTK_MENU_SHELL (parent)->priv->active_menu_item)
665
        {
666 667 668 669 670 671 672 673 674
          /*  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);
675
        }
Elliot Lee's avatar
Elliot Lee committed
676
    }
677

678
  if (!priv->active || !priv->button)
Elliot Lee's avatar
Elliot Lee committed
679
    {
680
      gboolean initially_active = priv->active;
681

682
      priv->button = event->button;
683

684
      if (menu_item)
685
        {
686
          if (_gtk_menu_item_is_selectable (menu_item) &&
687
              gtk_widget_get_parent (menu_item) == widget &&
688
              menu_item != priv->active_menu_item)
689
            {
Matthias Clasen's avatar
Matthias Clasen committed
690
              gtk_menu_shell_activate (menu_shell);
691
              priv->button = event->button;
692 693 694

              if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
                {
695
                  priv->activate_time = event->time;
696 697 698 699 700 701 702 703
                  gtk_menu_shell_select_item (menu_shell, menu_item);
                }
            }
        }
      else
        {
          if (!initially_active)
            {
704 705
              gtk_menu_shell_deactivate (menu_shell);
              return FALSE;
706 707
            }
        }
Elliot Lee's avatar
Elliot Lee committed
708 709 710 711 712
    }
  else
    {
      widget = gtk_get_event_widget ((GdkEvent*) event);
      if (widget == GTK_WIDGET (menu_shell))
713 714 715 716
        {
          gtk_menu_shell_deactivate (menu_shell);
          g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
        }
Elliot Lee's avatar
Elliot Lee committed
717 718
    }

719 720 721 722
  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))
723 724 725 726 727
    {
      _gtk_menu_item_popup_submenu (menu_item, FALSE);
      priv->activated_submenu = TRUE;
    }

Elliot Lee's avatar
Elliot Lee committed
728 729 730
  return TRUE;
}

731 732
static gboolean
gtk_menu_shell_grab_broken (GtkWidget          *widget,
733
                            GdkEventGrabBroken *event)
734
{
735
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
736
  GtkMenuShellPrivate *priv = menu_shell->priv;
737

738
  if (priv->have_xgrab && event->grab_window == NULL)
739
    {
740
      /* Unset the active menu item so gtk_menu_popdown() doesn't see it. */
741
      gtk_menu_shell_deselect (menu_shell);
742 743 744 745 746 747 748
      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
749 750
static gint
gtk_menu_shell_button_release (GtkWidget      *widget,
751
                               GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
752
{
753
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
754
  GtkMenuShellPrivate *priv = menu_shell->priv;
Elliot Lee's avatar
Elliot Lee committed
755

756 757 758 759 760 761 762 763 764 765 766 767
  if (priv->parent_menu_shell &&
      (event->time - GTK_MENU_SHELL (priv->parent_menu_shell)->priv->activate_time) < MENU_SHELL_TIMEOUT)
    {
      /* The button-press originated in the parent menu bar and we are
       * a pop-up menu. It was a quick press-and-release so we don't want
       * to activate an item but we leave the popup in place instead.
       * https://bugzilla.gnome.org/show_bug.cgi?id=703069
       */
      GTK_MENU_SHELL (priv->parent_menu_shell)->priv->activate_time = 0;
      return TRUE;
    }

768
  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
769
    {
770 771 772
      GtkWidget *menu_item;
      gboolean   deactivate = TRUE;

773 774 775 776 777 778
      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);
        }
779

780
      priv->button = 0;
Owen Taylor's avatar
Owen Taylor committed
781
      menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
782

783
      if ((event->time - priv->activate_time) > MENU_SHELL_TIMEOUT)
784
        {
785
          if (menu_item && (priv->active_menu_item == menu_item) &&
786 787
              _gtk_menu_item_is_selectable (menu_item))
            {
788
              GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->priv->submenu;
789

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
              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)
                {
                  GTimeVal *popup_time;
                  gint64 usec_since_popup = 0;

                  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);
                    }

819
                  /* Only close the submenu on click if we opened the
820
                   * menu explicitly (usec_since_popup == 0) or
821 822
                   * enough time has passed since it was opened by
                   * GtkMenuItem's timeout (usec_since_popup > delay).
823 824 825
                   */
                  if (!priv->activated_submenu &&
                      (usec_since_popup == 0 ||
826
                       usec_since_popup > MENU_POPDOWN_DELAY * 1000))
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
                    {
                      _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;
            }
844
          else if (priv->parent_menu_shell)
845
            {
846 847
              priv->active = TRUE;
              gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
848 849 850
              deactivate = FALSE;
            }

851 852 853
          /* If we ended up on an item with a submenu, leave the menu up. */
          if (menu_item &&
              (priv->active_menu_item == menu_item) &&
854 855 856 857 858
              GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
            {
              deactivate = FALSE;
            }
        }
859
      else /* a very fast press-release */
860 861
        {
          /* We only ever want to prevent deactivation on the first
Owen Taylor's avatar
Owen Taylor committed
862
           * press/release. Setting the time to zero is a bit of a
863 864 865 866 867
           * 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.
           */
868
          priv->activate_time = 0;
869 870 871
          deactivate = FALSE;
        }

Elliot Lee's avatar
Elliot Lee committed
872
      if (deactivate)
873 874 875 876 877 878
        {
          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
879 880 881 882 883
    }

  return TRUE;
}

884 885 886 887
void
_gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
                                   gboolean      keyboard_mode)
{
888
  menu_shell->priv->keyboard_mode = keyboard_mode;
889 890 891 892 893
}

gboolean
_gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
{
894
  return menu_shell->priv->keyboard_mode;
895 896 897 898 899 900 901 902 903 904 905 906 907
}

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

  target = menu_shell;
  found = FALSE;
  while (target)
    {
908
      GtkMenuShellPrivate *priv = target->priv;
909 910
      GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));

911 912 913 914 915 916
      /* 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.
       */
917 918
      if (menu_shell->priv->keyboard_mode)
        target->priv->keyboard_mode = TRUE;
919 920 921 922 923 924 925 926

      /* 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.
       */
927 928
      mnemonics_visible = target->priv->keyboard_mode &&
                          (((target->priv->active_menu_item || priv->in_unselectable_item) && !found) ||
929
                           (target == menu_shell &&
930
                            !target->priv->parent_menu_shell &&
931
                            gtk_widget_has_grab (GTK_WIDGET (target))));
932 933 934 935 936

      /* While menus are up, only show underlines inside the menubar,
       * not in the entire window.
       */
      if (GTK_IS_MENU_BAR (target))
937
        {
938
          gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
939 940 941
          _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
                                                          mnemonics_visible);
        }
942
      else
943
        gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
944

945
      if (target->priv->active_menu_item || priv->in_unselectable_item)
946 947
        found = TRUE;

948
      target = GTK_MENU_SHELL (target->priv->parent_menu_shell);
949 950 951
    }
}

952
static gint
953
gtk_menu_shell_key_press (GtkWidget   *widget,
954
                          GdkEventKey *event)
955
{
956
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
957
  GtkMenuShellPrivate *priv = menu_shell->priv;
958

959
  priv->keyboard_mode = TRUE;
960

961 962 963
  if (!(priv->active_menu_item || priv->in_unselectable_item) &&
      priv->parent_menu_shell)
    return gtk_widget_event (priv->parent_menu_shell, (GdkEvent *)event);
964

965
  if (gtk_bindings_activate_event (G_OBJECT (widget), event))
966 967
    return TRUE;

968
  return gtk_menu_shell_activate_mnemonic (menu_shell, event);
969 970
}

Elliot Lee's avatar
Elliot Lee committed
971 972
static gint
gtk_menu_shell_enter_notify (GtkWidget        *widget,
973
                             GdkEventCrossing *event)
Elliot Lee's avatar
Elliot Lee committed
974
{
975
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
976
  GtkMenuShellPrivate *priv = menu_shell->priv;
977

978 979 980 981 982
  if (event->mode == GDK_CROSSING_GTK_GRAB ||
      event->mode == GDK_CROSSING_GTK_UNGRAB ||
      event->mode == GDK_CROSSING_STATE_CHANGED)
    return TRUE;

983
  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
984
    {
985
      GtkWidget *menu_item;
986
      GtkWidget *parent;
987

Elliot Lee's avatar
Elliot Lee committed
988 989
      menu_item = gtk_get_event_widget ((GdkEvent*) event);

990 991 992 993 994 995 996 997 998
      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;
        }
999

1000 1001
      parent = gtk_widget_get_parent (menu_item);
      if (parent == widget &&
1002 1003 1004 1005
          GTK_IS_MENU_ITEM (menu_item))
        {
          if (priv->ignore_enter)
            return TRUE;
1006

1007
          if (event->detail != GDK_NOTIFY_INFERIOR)
1008
            {
1009
              if ((gtk_widget_get_state_flags (menu_item) & GTK_STATE_FLAG_PRELIGHT) == 0)
1010
                gtk_menu_shell_select_item (menu_shell, menu_item);
1011 1012 1013 1014 1015 1016 1017 1018 1019

              /* 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.
               */
1020
              if ((event->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK)) &&
1021
                  GTK_MENU_ITEM (menu_item)->priv->submenu != NULL)
1022
                {
1023
                  GTK_MENU_SHELL (parent)->priv->activated_submenu = TRUE;
1024

1025
                  if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
1026
                    {
1027
                      GdkDevice *source_device;
1028

1029
                      source_device = gdk_event_get_source_device ((GdkEvent *) event);
1030

1031
                      if (gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
1032 1033
                        _gtk_menu_item_popup_submenu (menu_item, TRUE);
                    }
1034
                }
1035 1036 1037 1038 1039 1040
            }
        }
      else if (priv->parent_menu_shell)
        {
          gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
        }
Elliot Lee's avatar
Elliot Lee committed
1041 1042 1043 1044 1045 1046 1047
    }

  return TRUE;
}

static gint
gtk_menu_shell_leave_notify (GtkWidget        *widget,
1048
                             GdkEventCrossing *event)
Elliot Lee's avatar
Elliot Lee committed
1049
{
1050
  if (event->mode == GDK_CROSSING_GTK_GRAB ||
1051
      event->mode == GDK_CROSSING_GTK_UNGRAB ||
1052 1053 1054
      event->mode == GDK_CROSSING_STATE_CHANGED)
    return TRUE;

1055
  if (gtk_widget_get_visible (widget))
Elliot Lee's avatar
Elliot Lee committed
1056
    {
1057
      GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1058
      GtkMenuShellPrivate *priv = menu_shell->priv;
1059 1060
      GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
      GtkMenuItem *menu_item;
Elliot Lee's avatar
Elliot Lee committed
1061

1062
      if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1063
        return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1064 1065 1066

      menu_item = GTK_MENU_ITEM (event_widget);

1067
      if (!_gtk_menu_item_is_selectable (event_widget))
1068 1069 1070 1071
        {
          priv->in_unselectable_item = TRUE;
          return TRUE;
        }
Tim Janik's avatar
Tim Janik committed
1072

1073
      if ((priv->active_menu_item == event_widget) &&
1074
          (menu_item->priv->submenu == NULL))
1075 1076
        {
          if ((event->detail != GDK_NOTIFY_INFERIOR) &&
1077
              (gtk_widget_get_state_flags (GTK_WIDGET (menu_item)) & GTK_STATE_FLAG_PRELIGHT) != 0)
1078 1079 1080 1081 1082 1083 1084 1085
            {
              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
1086 1087 1088 1089 1090
    }

  return TRUE;
}

1091 1092
static void
gtk_menu_shell_screen_changed (GtkWidget *widget,
1093
                               GdkScreen *previous_screen)
1094 1095 1096 1097
{
  gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
}

Elliot Lee's avatar
Elliot Lee committed
1098 1099
static void
gtk_menu_shell_add (GtkContainer *container,
1100
                    GtkWidget    *widget)
Elliot Lee's avatar
Elliot Lee committed
1101 1102 1103 1104 1105 1106
{
  gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
}

static void
gtk_menu_shell_remove (GtkContainer *container,
1107
                       GtkWidget    *widget)
Elliot Lee's avatar
Elliot Lee committed
1108
{
1109
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1110
  GtkMenuShellPrivate *priv = menu_shell->priv;
1111
  gint was_visible;
1112

1113
  was_visible = gtk_widget_get_visible (widget);
1114 1115 1116
  priv->children = g_list_remove (priv->children, widget);

  if (widget == priv->active_menu_item)
1117
    {
1118 1119
      g_signal_emit_by_name (priv->active_menu_item, "deselect");
      priv->active_menu_item = NULL;
1120 1121
    }

1122
  gtk_widget_unparent (widget);
1123 1124

  /* Queue resize regardless of gtk_widget_get_visible (container),
1125 1126 1127
   * since that's what is needed by toplevels.
   */
  if (was_visible)
Elliot Lee's avatar
Elliot Lee committed
1128 1129 1130 1131
    gtk_widget_queue_resize (GTK_WIDGET (container));
}

static void
1132
gtk_menu_shell_forall (GtkContainer *container,
1133 1134 1135
                       gboolean      include_internals,
                       GtkCallback   callback,
                       gpointer      callback_data)
Elliot Lee's avatar
Elliot Lee committed
1136
{
1137
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
Elliot Lee's avatar
Elliot Lee committed
1138 1139 1140
  GtkWidget *child;
  GList *children;

1141
  children = menu_shell->priv->children;
Elliot Lee's avatar
Elliot Lee committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
  while (children)
    {
      child = children->data;
      children = children->next;

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


static void
gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
{
1155 1156 1157
  GtkMenuShellPrivate *priv = menu_shell->priv;

  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
1158
    {
1159 1160 1161
      priv->button = 0;
      priv->active = FALSE;
      priv->activate_time = 0;
Elliot Lee's avatar
Elliot Lee committed
1162

1163 1164 1165 1166 1167
      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
1168

1169 1170 1171
      if (priv->have_grab)
        {
          priv->have_grab = FALSE;
1172
          gtk_device_grab_remove (GTK_WIDGET (menu_shell), priv->grab_pointer);
1173 1174 1175
        }
      if (priv->have_xgrab)
        {
1176 1177
          GdkDevice *keyboard;

1178
          gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1179 1180 1181 1182
          keyboard = gdk_device_get_associated_device (priv->grab_pointer);

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

1184 1185
          priv->have_xgrab = FALSE;
        }
1186

1187
      priv->keyboard_mode = FALSE;
1188
      _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1189 1190

      _gtk_menu_shell_update_mnemonics (menu_shell);
Elliot Lee's avatar
Elliot Lee committed
1191 1192 1193 1194 1195
    }
}

static gint
gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1196
                        GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
1197
{
1198
  GtkWidget *parent;
Elliot Lee's avatar
Elliot Lee committed
1199 1200 1201 1202

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

1203
  parent = gtk_widget_get_parent (child);
1204
  while (GTK_IS_MENU_SHELL (parent))
Elliot Lee's avatar
Elliot Lee committed
1205
    {
1206
      if (parent == (GtkWidget*) menu_shell)
1207 1208
        return TRUE;
      parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
Elliot Lee's avatar
Elliot Lee committed
1209 1210 1211 1212
    }

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

1214
static GtkWidget*
Owen Taylor's avatar
Owen Taylor committed
1215
gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1216
                         GdkEvent     *event)
Owen Taylor's avatar
Owen Taylor committed
1217 1218 1219 1220
{
  GtkWidget *menu_item;

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

Owen Taylor's avatar
Owen Taylor committed
1222
  while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1223
    menu_item = gtk_widget_get_parent (menu_item);
Owen Taylor's avatar
Owen Taylor committed
1224 1225 1226 1227 1228 1229 1230

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

1231 1232
/* Handlers for action signals */

1233 1234 1235 1236 1237 1238 1239
/**
 * gtk_menu_shell_select_item:
 * @menu_shell: a #GtkMenuShell
 * @menu_item: The #GtkMenuItem to select
 *
 * Selects the menu item from the menu shell.
 */
1240
void
1241
gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1242
                            GtkWidget    *menu_item)
1243
{
1244
  GtkMenuShellPrivate *priv = menu_shell->priv;
1245 1246
  GtkMenuShellClass *class;

1247 1248 1249
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));

1250 1251
  class = GTK_MENU_SHELL_GET_CLASS (menu_shell);

1252
  if (class->select_item &&
1253 1254
      !(priv->active &&
        priv->active_menu_item == menu_item))
1255 1256 1257
    class->select_item (menu_shell, menu_item);
}

1258
void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1259
                                   GtkSubmenuPlacement  placement);
1260 1261 1262

static void
gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1263
                                 GtkWidget    *menu_item)
1264
{
1265
  GtkMenuShellPrivate *priv = menu_shell->priv;
1266 1267
  GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);

1268
  if (priv->active_menu_item)
1269
    {
1270 1271
      gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
      priv->active_menu_item = NULL;
1272
    }
1273

1274
  if (!_gtk_menu_item_is_selectable (menu_item))
1275 1276 1277 1278 1279
    {
      priv->in_unselectable_item = TRUE;
      _gtk_menu_shell_update_mnemonics (menu_shell);
      return;
    }
1280

Matthias Clasen's avatar
Matthias Clasen committed
1281
  gtk_menu_shell_activate (menu_shell);
1282

1283
  priv->active_menu_item = menu_item;
1284
  if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1285 1286
    _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
                                  GTK_LEFT_RIGHT);
1287
  else
1288 1289 1290
    _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<