gtkaccelgroup.c 54.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2
 * Copyright (C) 1998, 2001 Tim Janik
Tim Janik's avatar
Tim Janik committed
3 4
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
6 7 8 9 10
 * 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
Matthias Clasen's avatar
Matthias Clasen committed
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Tim Janik's avatar
Tim Janik committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
15 16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19 20

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

27
#include "config.h"
Manish Singh's avatar
Manish Singh committed
28 29 30
#include <string.h>
#include <stdlib.h>

Tim Janik's avatar
Tim Janik committed
31
#include "gtkaccelgroup.h"
32
#include "gtkaccelgroupprivate.h"
33
#include "gtkaccellabel.h"
34
#include "gtkaccelmap.h"
35
#include "gtkintl.h"
36
#include "gtkmarshalers.h"
37
#include "gtkprivate.h"
38

39 40
/**
 * SECTION:gtkaccelgroup
Matthias Clasen's avatar
Matthias Clasen committed
41 42
 * @Short_description: Groups of global keyboard accelerators for an
 *     entire GtkWindow
43 44 45
 * @Title: Accelerator Groups
 * @See_also:gtk_window_add_accel_group(), gtk_accel_map_change_entry(),
 * gtk_item_factory_new(), gtk_label_new_with_mnemonic()
46
 *
47 48 49
 * A #GtkAccelGroup represents a group of keyboard accelerators,
 * typically attached to a toplevel #GtkWindow (with
 * gtk_window_add_accel_group()). Usually you won't need to create a
50 51 52
 * #GtkAccelGroup directly; instead, when using #GtkUIManager, GTK+
 * automatically sets up the accelerators for your menus in the ui
 * manager's #GtkAccelGroup.
Matthias Clasen's avatar
Matthias Clasen committed
53
 *
54 55 56 57 58 59 60 61 62 63
 * Note that <firstterm>accelerators</firstterm> are different from
 * <firstterm>mnemonics</firstterm>. Accelerators are shortcuts for
 * activating a menu item; they appear alongside the menu item they're a
 * shortcut for. For example "Ctrl+Q" might appear alongside the "Quit"
 * menu item. Mnemonics are shortcuts for GUI elements such as text
 * entries or buttons; they appear as underlined characters. See
 * gtk_label_new_with_mnemonic(). Menu items can have both accelerators
 * and mnemonics, of course.
 */

64
/* --- prototypes --- */
65 66 67 68 69 70 71
static void gtk_accel_group_finalize     (GObject    *object);
static void gtk_accel_group_get_property (GObject    *object,
                                          guint       param_id,
                                          GValue     *value,
                                          GParamSpec *pspec);
static void accel_closure_invalidate     (gpointer    data,
                                          GClosure   *closure);
72

Tim Janik's avatar
Tim Janik committed
73 74

/* --- variables --- */
75 76 77 78 79 80 81 82 83
static guint  signal_accel_activate      = 0;
static guint  signal_accel_changed       = 0;
static guint  quark_acceleratable_groups = 0;
static guint  default_accel_mod_mask     = (GDK_SHIFT_MASK   |
                                            GDK_CONTROL_MASK |
                                            GDK_MOD1_MASK    |
                                            GDK_SUPER_MASK   |
                                            GDK_HYPER_MASK   |
                                            GDK_META_MASK);
84

Tim Janik's avatar
Tim Janik committed
85

86
enum {
87 88 89
  PROP_0,
  PROP_IS_LOCKED,
  PROP_MODIFIER_MASK,
90 91
};

Matthias Clasen's avatar
Matthias Clasen committed
92
G_DEFINE_TYPE (GtkAccelGroup, gtk_accel_group, G_TYPE_OBJECT)
93

Matthias Clasen's avatar
Matthias Clasen committed
94
/* --- functions --- */
95
static void
96
gtk_accel_group_class_init (GtkAccelGroupClass *class)
Tim Janik's avatar
Tim Janik committed
97
{
98 99 100 101 102
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  quark_acceleratable_groups = g_quark_from_static_string ("gtk-acceleratable-accel-groups");

  object_class->finalize = gtk_accel_group_finalize;
103
  object_class->get_property = gtk_accel_group_get_property;
104 105

  class->accel_changed = NULL;
106

107
  g_object_class_install_property (object_class,
108 109 110 111 112 113
                                   PROP_IS_LOCKED,
                                   g_param_spec_boolean ("is-locked",
                                                         "Is locked",
                                                         "Is the accel group locked",
                                                         FALSE,
                                                         G_PARAM_READABLE));
114

115 116
  g_object_class_install_property (object_class,
                                   PROP_MODIFIER_MASK,
117 118 119 120
                                   g_param_spec_flags ("modifier-mask",
                                                       "Modifier Mask",
                                                       "Modifier Mask",
                                                       GDK_TYPE_MODIFIER_TYPE,
Matthias Clasen's avatar
Matthias Clasen committed
121
                                                       default_accel_mod_mask,
122
                                                       G_PARAM_READABLE));
123

124 125 126 127 128 129 130 131 132
  /**
   * GtkAccelGroup::accel-activate:
   * @accel_group: the #GtkAccelGroup which received the signal
   * @acceleratable: the object on which the accelerator was activated
   * @keyval: the accelerator keyval
   * @modifier: the modifier combination of the accelerator
   *
   * The accel-activate signal is an implementation detail of
   * #GtkAccelGroup and not meant to be used by applications.
Matthias Clasen's avatar
Matthias Clasen committed
133
   *
134 135
   * Returns: %TRUE if the accelerator was activated
   */
Manish Singh's avatar
Manish Singh committed
136
  signal_accel_activate =
137
    g_signal_new (I_("accel-activate"),
Matthias Clasen's avatar
Matthias Clasen committed
138 139 140 141 142 143 144 145 146
                  G_OBJECT_CLASS_TYPE (class),
                  G_SIGNAL_DETAILED,
                  0,
                  _gtk_boolean_handled_accumulator, NULL,
                  _gtk_marshal_BOOLEAN__OBJECT_UINT_FLAGS,
                  G_TYPE_BOOLEAN, 3,
                  G_TYPE_OBJECT,
                  G_TYPE_UINT,
                  GDK_TYPE_MODIFIER_TYPE);
147 148 149 150 151 152 153 154
  /**
   * GtkAccelGroup::accel-changed:
   * @accel_group: the #GtkAccelGroup which received the signal
   * @keyval: the accelerator keyval
   * @modifier: the modifier combination of the accelerator
   * @accel_closure: the #GClosure of the accelerator
   *
   * The accel-changed signal is emitted when a #GtkAccelGroupEntry
Matthias Clasen's avatar
Matthias Clasen committed
155
   * is added to or removed from the accel group.
156
   *
Matthias Clasen's avatar
Matthias Clasen committed
157 158
   * Widgets like #GtkAccelLabel which display an associated
   * accelerator should connect to this signal, and rebuild
159 160
   * their visual representation if the @accel_closure is theirs.
   */
Manish Singh's avatar
Manish Singh committed
161
  signal_accel_changed =
162
    g_signal_new (I_("accel-changed"),
Matthias Clasen's avatar
Matthias Clasen committed
163 164 165 166 167 168 169 170 171
                  G_OBJECT_CLASS_TYPE (class),
                  G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
                  G_STRUCT_OFFSET (GtkAccelGroupClass, accel_changed),
                  NULL, NULL,
                  _gtk_marshal_VOID__UINT_FLAGS_BOXED,
                  G_TYPE_NONE, 3,
                  G_TYPE_UINT,
                  GDK_TYPE_MODIFIER_TYPE,
                  G_TYPE_CLOSURE);
172 173

  g_type_class_add_private (object_class, sizeof (GtkAccelGroupPrivate));
174 175
}

176 177
static void
gtk_accel_group_finalize (GObject *object)
178
{
179
  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
180
  guint i;
Matthias Clasen's avatar
Matthias Clasen committed
181

182
  for (i = 0; i < accel_group->priv->n_accels; i++)
183
    {
184
      GtkAccelGroupEntry *entry = &accel_group->priv->priv_accels[i];
185 186

      if (entry->accel_path_quark)
Matthias Clasen's avatar
Matthias Clasen committed
187 188
        {
          const gchar *accel_path = g_quark_to_string (entry->accel_path_quark);
189

Matthias Clasen's avatar
Matthias Clasen committed
190 191
          _gtk_accel_map_remove_group (accel_path, accel_group);
        }
192
      g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
193 194 195

      /* remove quick_accel_add() refcount */
      g_closure_unref (entry->closure);
196
    }
197

198
  g_free (accel_group->priv->priv_accels);
199

Matthias Clasen's avatar
Matthias Clasen committed
200
  G_OBJECT_CLASS (gtk_accel_group_parent_class)->finalize (object);
Tim Janik's avatar
Tim Janik committed
201 202
}

203 204 205 206 207 208 209 210
static void
gtk_accel_group_get_property (GObject    *object,
                              guint       param_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);

211 212 213
  switch (param_id)
    {
    case PROP_IS_LOCKED:
214
      g_value_set_boolean (value, accel_group->priv->lock_count > 0);
215 216
      break;
    case PROP_MODIFIER_MASK:
217
      g_value_set_flags (value, accel_group->priv->modifier_mask);
218 219 220 221 222
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
223 224
}

225 226
static void
gtk_accel_group_init (GtkAccelGroup *accel_group)
Tim Janik's avatar
Tim Janik committed
227
{
228 229 230 231 232 233
  GtkAccelGroupPrivate *priv;

  accel_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (accel_group,
                                                   GTK_TYPE_ACCEL_GROUP,
                                                   GtkAccelGroupPrivate);
  priv = accel_group->priv;
234 235 236 237 238 239

  priv->lock_count = 0;
  priv->modifier_mask = gtk_accelerator_get_default_mod_mask ();
  priv->acceleratables = NULL;
  priv->n_accels = 0;
  priv->priv_accels = NULL;
Tim Janik's avatar
Tim Janik committed
240 241
}

242
/**
Matthias Clasen's avatar
Matthias Clasen committed
243
 * gtk_accel_group_new:
Matthias Clasen's avatar
Matthias Clasen committed
244 245
 *
 * Creates a new #GtkAccelGroup.
Matthias Clasen's avatar
Matthias Clasen committed
246 247
 *
 * Returns: a new #GtkAccelGroup object
248
 */
Tim Janik's avatar
Tim Janik committed
249
GtkAccelGroup*
250
gtk_accel_group_new (void)
Tim Janik's avatar
Tim Janik committed
251
{
252
  return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
Tim Janik's avatar
Tim Janik committed
253 254
}

255 256 257
/**
 * gtk_accel_group_get_is_locked:
 * @accel_group: a #GtkAccelGroup
258
 *
259
 * Locks are added and removed using gtk_accel_group_lock() and
260
 * gtk_accel_group_unlock().
261
 *
Matthias Clasen's avatar
Matthias Clasen committed
262
 * Returns: %TRUE if there are 1 or more locks on the @accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
263
 *     %FALSE otherwise.
Matthias Clasen's avatar
Matthias Clasen committed
264
 *
265
 * Since: 2.14
266
 */
267
gboolean
268
gtk_accel_group_get_is_locked (GtkAccelGroup *accel_group)
269
{
270
  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
271

272
  return accel_group->priv->lock_count > 0;
273 274
}

275 276 277
/**
 * gtk_accel_group_get_modifier_mask:
 * @accel_group: a #GtkAccelGroup
278
 *
Matthias Clasen's avatar
Matthias Clasen committed
279
 * Gets a #GdkModifierType representing the mask for this
280
 * @accel_group. For example, #GDK_CONTROL_MASK, #GDK_SHIFT_MASK, etc.
281
 *
Matthias Clasen's avatar
Matthias Clasen committed
282 283
 * Returns: the modifier mask for this accel group.
 *
284
 * Since: 2.14
285
 */
286 287 288 289 290
GdkModifierType
gtk_accel_group_get_modifier_mask (GtkAccelGroup *accel_group)
{
  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), 0);

291
  return accel_group->priv->modifier_mask;
292 293
}

Tim Janik's avatar
Tim Janik committed
294
static void
295
accel_group_weak_ref_detach (GSList  *free_list,
Matthias Clasen's avatar
Matthias Clasen committed
296
                             GObject *stale_object)
Tim Janik's avatar
Tim Janik committed
297
{
298
  GSList *slist;
Matthias Clasen's avatar
Matthias Clasen committed
299

Tim Janik's avatar
Tim Janik committed
300 301 302
  for (slist = free_list; slist; slist = slist->next)
    {
      GtkAccelGroup *accel_group;
Matthias Clasen's avatar
Matthias Clasen committed
303

Tim Janik's avatar
Tim Janik committed
304
      accel_group = slist->data;
305
      accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, stale_object);
306
      g_object_unref (accel_group);
Tim Janik's avatar
Tim Janik committed
307 308
    }
  g_slist_free (free_list);
309
  g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
Tim Janik's avatar
Tim Janik committed
310 311 312
}

void
313
_gtk_accel_group_attach (GtkAccelGroup *accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
314
                         GObject       *object)
Tim Janik's avatar
Tim Janik committed
315 316
{
  GSList *slist;
Matthias Clasen's avatar
Matthias Clasen committed
317

318
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
319
  g_return_if_fail (G_IS_OBJECT (object));
320
  g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) == NULL);
Matthias Clasen's avatar
Matthias Clasen committed
321

322
  g_object_ref (accel_group);
323
  accel_group->priv->acceleratables = g_slist_prepend (accel_group->priv->acceleratables, object);
324
  slist = g_object_get_qdata (object, quark_acceleratable_groups);
325
  if (slist)
326
    g_object_weak_unref (object,
Matthias Clasen's avatar
Matthias Clasen committed
327 328
                         (GWeakNotify) accel_group_weak_ref_detach,
                         slist);
Tim Janik's avatar
Tim Janik committed
329
  slist = g_slist_prepend (slist, accel_group);
330 331
  g_object_set_qdata (object, quark_acceleratable_groups, slist);
  g_object_weak_ref (object,
Matthias Clasen's avatar
Matthias Clasen committed
332 333
                     (GWeakNotify) accel_group_weak_ref_detach,
                     slist);
Tim Janik's avatar
Tim Janik committed
334 335 336
}

void
337
_gtk_accel_group_detach (GtkAccelGroup *accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
338
                         GObject       *object)
Tim Janik's avatar
Tim Janik committed
339 340
{
  GSList *slist;
Matthias Clasen's avatar
Matthias Clasen committed
341

342
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
343
  g_return_if_fail (G_IS_OBJECT (object));
344
  g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) != NULL);
Matthias Clasen's avatar
Matthias Clasen committed
345

346
  accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, object);
347 348
  slist = g_object_get_qdata (object, quark_acceleratable_groups);
  g_object_weak_unref (object,
Matthias Clasen's avatar
Matthias Clasen committed
349 350
                       (GWeakNotify) accel_group_weak_ref_detach,
                       slist);
Tim Janik's avatar
Tim Janik committed
351
  slist = g_slist_remove (slist, accel_group);
352
  g_object_set_qdata (object, quark_acceleratable_groups, slist);
353
  if (slist)
354
    g_object_weak_ref (object,
Matthias Clasen's avatar
Matthias Clasen committed
355 356
                       (GWeakNotify) accel_group_weak_ref_detach,
                       slist);
357 358 359
  g_object_unref (accel_group);
}

360 361
/**
 * gtk_accel_groups_from_object:
Matthias Clasen's avatar
Matthias Clasen committed
362
 * @object: a #GObject, usually a #GtkWindow
363 364
 *
 * Gets a list of all accel groups which are attached to @object.
365
 *
Matthias Clasen's avatar
Matthias Clasen committed
366 367
 * Returns: (element-type GtkAccelGroup) (transfer none): a list of
 *     all accel groups which are attached to @object
368
 */
369
GSList*
370
gtk_accel_groups_from_object (GObject *object)
371 372
{
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
Matthias Clasen's avatar
Matthias Clasen committed
373

374 375 376
  return g_object_get_qdata (object, quark_acceleratable_groups);
}

377 378 379
/**
 * gtk_accel_group_find:
 * @accel_group: a #GtkAccelGroup
380 381
 * @find_func: (scope call): a function to filter the entries
 *    of @accel_group with
382 383
 * @data: data to pass to @find_func
 *
Matthias Clasen's avatar
Matthias Clasen committed
384
 * Finds the first entry in an accelerator group for which
Matthias Clasen's avatar
Matthias Clasen committed
385
 * @find_func returns %TRUE and returns its #GtkAccelKey.
Matthias Clasen's avatar
Matthias Clasen committed
386 387 388
 *
 * Returns: (transfer none): the key of the first entry passing
 *    @find_func. The key is owned by GTK+ and must not be freed.
389
 */
390
GtkAccelKey*
Matthias Clasen's avatar
Matthias Clasen committed
391 392 393
gtk_accel_group_find (GtkAccelGroup         *accel_group,
                      GtkAccelGroupFindFunc  find_func,
                      gpointer               data)
394 395 396 397 398 399 400 401
{
  GtkAccelKey *key = NULL;
  guint i;

  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);
  g_return_val_if_fail (find_func != NULL, NULL);

  g_object_ref (accel_group);
402 403
  for (i = 0; i < accel_group->priv->n_accels; i++)
    if (find_func (&accel_group->priv->priv_accels[i].key,
Matthias Clasen's avatar
Matthias Clasen committed
404 405
                   accel_group->priv->priv_accels[i].closure,
                   data))
406
      {
Matthias Clasen's avatar
Matthias Clasen committed
407 408
        key = &accel_group->priv->priv_accels[i].key;
        break;
409 410 411 412
      }
  g_object_unref (accel_group);

  return key;
Tim Janik's avatar
Tim Janik committed
413 414
}

415
/**
Matthias Clasen's avatar
Matthias Clasen committed
416
 * gtk_accel_group_lock:
417
 * @accel_group: a #GtkAccelGroup
Matthias Clasen's avatar
Matthias Clasen committed
418
 *
Matthias Clasen's avatar
Matthias Clasen committed
419 420
 * Locks the given accelerator group.
 *
421 422 423
 * Locking an acelerator group prevents the accelerators contained
 * within it to be changed during runtime. Refer to
 * gtk_accel_map_change_entry() about runtime accelerator changes.
424 425 426 427
 *
 * If called more than once, @accel_group remains locked until
 * gtk_accel_group_unlock() has been called an equivalent number
 * of times.
428
 */
Tim Janik's avatar
Tim Janik committed
429
void
430
gtk_accel_group_lock (GtkAccelGroup *accel_group)
Tim Janik's avatar
Tim Janik committed
431
{
432
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
Matthias Clasen's avatar
Matthias Clasen committed
433

434
  accel_group->priv->lock_count += 1;
435

436
  if (accel_group->priv->lock_count == 1) {
437
    /* State change from unlocked to locked */
438
    g_object_notify (G_OBJECT (accel_group), "is-locked");
439
  }
Tim Janik's avatar
Tim Janik committed
440 441
}

442
/**
Matthias Clasen's avatar
Matthias Clasen committed
443
 * gtk_accel_group_unlock:
444
 * @accel_group: a #GtkAccelGroup
Matthias Clasen's avatar
Matthias Clasen committed
445
 *
Matthias Clasen's avatar
Matthias Clasen committed
446
 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
447
 */
Tim Janik's avatar
Tim Janik committed
448
void
449
gtk_accel_group_unlock (GtkAccelGroup *accel_group)
Tim Janik's avatar
Tim Janik committed
450
{
451
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
452
  g_return_if_fail (accel_group->priv->lock_count > 0);
Tim Janik's avatar
Tim Janik committed
453

454
  accel_group->priv->lock_count -= 1;
455

456
  if (accel_group->priv->lock_count < 1) {
457
    /* State change from locked to unlocked */
458
    g_object_notify (G_OBJECT (accel_group), "is-locked");
459
  }
Tim Janik's avatar
Tim Janik committed
460 461
}

462
static void
463
accel_closure_invalidate (gpointer  data,
Matthias Clasen's avatar
Matthias Clasen committed
464
                          GClosure *closure)
Tim Janik's avatar
Tim Janik committed
465
{
466 467 468
  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);

  gtk_accel_group_disconnect (accel_group, closure);
Tim Janik's avatar
Tim Janik committed
469 470
}

471 472
static int
bsearch_compare_accels (const void *d1,
Matthias Clasen's avatar
Matthias Clasen committed
473
                        const void *d2)
Tim Janik's avatar
Tim Janik committed
474
{
475 476 477 478 479 480 481
  const GtkAccelGroupEntry *entry1 = d1;
  const GtkAccelGroupEntry *entry2 = d2;

  if (entry1->key.accel_key == entry2->key.accel_key)
    return entry1->key.accel_mods < entry2->key.accel_mods ? -1 : entry1->key.accel_mods > entry2->key.accel_mods;
  else
    return entry1->key.accel_key < entry2->key.accel_key ? -1 : 1;
Tim Janik's avatar
Tim Janik committed
482 483
}

484
static void
Matthias Clasen's avatar
Matthias Clasen committed
485 486 487 488 489 490
quick_accel_add (GtkAccelGroup   *accel_group,
                 guint            accel_key,
                 GdkModifierType  accel_mods,
                 GtkAccelFlags    accel_flags,
                 GClosure        *closure,
                 GQuark           path_quark)
Tim Janik's avatar
Tim Janik committed
491
{
492
  guint pos, i = accel_group->priv->n_accels++;
493 494
  GtkAccelGroupEntry key;

495
  /* find position */
496 497 498
  key.key.accel_key = accel_key;
  key.key.accel_mods = accel_mods;
  for (pos = 0; pos < i; pos++)
499
    if (bsearch_compare_accels (&key, accel_group->priv->priv_accels + pos) < 0)
500
      break;
501 502

  /* insert at position, ref closure */
503 504
  accel_group->priv->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv->priv_accels, accel_group->priv->n_accels);
  g_memmove (accel_group->priv->priv_accels + pos + 1, accel_group->priv->priv_accels + pos,
Matthias Clasen's avatar
Matthias Clasen committed
505
             (i - pos) * sizeof (accel_group->priv->priv_accels[0]));
506 507 508 509 510
  accel_group->priv->priv_accels[pos].key.accel_key = accel_key;
  accel_group->priv->priv_accels[pos].key.accel_mods = accel_mods;
  accel_group->priv->priv_accels[pos].key.accel_flags = accel_flags;
  accel_group->priv->priv_accels[pos].closure = g_closure_ref (closure);
  accel_group->priv->priv_accels[pos].accel_path_quark = path_quark;
511
  g_closure_sink (closure);
Matthias Clasen's avatar
Matthias Clasen committed
512

513 514 515 516 517 518 519 520 521 522 523 524
  /* handle closure invalidation and reverse lookups */
  g_closure_add_invalidate_notifier (closure, accel_group, accel_closure_invalidate);

  /* get accel path notification */
  if (path_quark)
    _gtk_accel_map_add_group (g_quark_to_string (path_quark), accel_group);

  /* connect and notify changed */
  if (accel_key)
    {
      gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);
      GQuark accel_quark = g_quark_from_string (accel_name);
525

526
      g_free (accel_name);
Matthias Clasen's avatar
Matthias Clasen committed
527

528 529
      /* setup handler */
      g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
530

531 532 533 534 535 536
      /* and notify */
      g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);
    }
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
537 538
quick_accel_remove (GtkAccelGroup *accel_group,
                    guint          pos)
539 540
{
  GQuark accel_quark = 0;
541
  GtkAccelGroupEntry *entry = accel_group->priv->priv_accels + pos;
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  guint accel_key = entry->key.accel_key;
  GdkModifierType accel_mods = entry->key.accel_mods;
  GClosure *closure = entry->closure;

  /* quark for notification */
  if (accel_key)
    {
      gchar *accel_name = gtk_accelerator_name (accel_key, accel_mods);

      accel_quark = g_quark_from_string (accel_name);
      g_free (accel_name);
    }

  /* clean up closure invalidate notification and disconnect */
  g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
  if (accel_quark)
    g_signal_handlers_disconnect_matched (accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
559 560 561
                                          G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
                                          signal_accel_activate, accel_quark,
                                          closure, NULL, NULL);
562 563 564 565 566
  /* clean up accel path notification */
  if (entry->accel_path_quark)
    _gtk_accel_map_remove_group (g_quark_to_string (entry->accel_path_quark), accel_group);

  /* physically remove */
567
  accel_group->priv->n_accels -= 1;
568
  g_memmove (entry, entry + 1,
Matthias Clasen's avatar
Matthias Clasen committed
569
             (accel_group->priv->n_accels - pos) * sizeof (accel_group->priv->priv_accels[0]));
570 571 572 573 574 575 576

  /* and notify */
  if (accel_quark)
    g_signal_emit (accel_group, signal_accel_changed, accel_quark, accel_key, accel_mods, closure);

  /* remove quick_accel_add() refcount */
  g_closure_unref (closure);
Tim Janik's avatar
Tim Janik committed
577 578
}

579
static GtkAccelGroupEntry*
Matthias Clasen's avatar
Matthias Clasen committed
580 581 582 583
quick_accel_find (GtkAccelGroup   *accel_group,
                  guint            accel_key,
                  GdkModifierType  accel_mods,
                  guint           *count_p)
Tim Janik's avatar
Tim Janik committed
584
{
585 586 587
  GtkAccelGroupEntry *entry;
  GtkAccelGroupEntry key;

588 589
  *count_p = 0;

590
  if (!accel_group->priv->n_accels)
591 592 593 594
    return NULL;

  key.key.accel_key = accel_key;
  key.key.accel_mods = accel_mods;
595
  entry = bsearch (&key, accel_group->priv->priv_accels, accel_group->priv->n_accels,
Matthias Clasen's avatar
Matthias Clasen committed
596 597
                   sizeof (accel_group->priv->priv_accels[0]), bsearch_compare_accels);

598 599 600 601
  if (!entry)
    return NULL;

  /* step back to the first member */
602
  for (; entry > accel_group->priv->priv_accels; entry--)
603
    if (entry[-1].key.accel_key != accel_key ||
Matthias Clasen's avatar
Matthias Clasen committed
604
        entry[-1].key.accel_mods != accel_mods)
605 606
      break;
  /* count equal members */
607
  for (; entry + *count_p < accel_group->priv->priv_accels + accel_group->priv->n_accels; (*count_p)++)
608
    if (entry[*count_p].key.accel_key != accel_key ||
Matthias Clasen's avatar
Matthias Clasen committed
609
        entry[*count_p].key.accel_mods != accel_mods)
610 611
      break;
  return entry;
Tim Janik's avatar
Tim Janik committed
612 613
}

614
/**
Matthias Clasen's avatar
Matthias Clasen committed
615
 * gtk_accel_group_connect:
Matthias Clasen's avatar
Matthias Clasen committed
616 617 618 619 620
 * @accel_group: the accelerator group to install an accelerator in
 * @accel_key: key value of the accelerator
 * @accel_mods: modifier combination of the accelerator
 * @accel_flags: a flag mask to configure this accelerator
 * @closure: closure to be executed upon accelerator activation
621
 *
Matthias Clasen's avatar
Matthias Clasen committed
622 623 624 625
 * Installs an accelerator in this group. When @accel_group is being
 * activated in response to a call to gtk_accel_groups_activate(),
 * @closure will be invoked if the @accel_key and @accel_mods from
 * gtk_accel_groups_activate() match those of this connection.
Matthias Clasen's avatar
Matthias Clasen committed
626
 *
627
 * The signature used for the @closure is that of #GtkAccelGroupActivate.
Matthias Clasen's avatar
Matthias Clasen committed
628 629 630
 *
 * Note that, due to implementation details, a single closure can
 * only be connected to one accelerator group.
631
 */
Tim Janik's avatar
Tim Janik committed
632
void
Matthias Clasen's avatar
Matthias Clasen committed
633 634 635 636 637
gtk_accel_group_connect (GtkAccelGroup   *accel_group,
                         guint            accel_key,
                         GdkModifierType  accel_mods,
                         GtkAccelFlags    accel_flags,
                         GClosure        *closure)
Tim Janik's avatar
Tim Janik committed
638
{
639 640 641
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
  g_return_if_fail (closure != NULL);
  g_return_if_fail (accel_key > 0);
642
  g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
643

644 645
  g_object_ref (accel_group);
  if (!closure->is_invalid)
646
    quick_accel_add (accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
647 648
                     gdk_keyval_to_lower (accel_key),
                     accel_mods, accel_flags, closure, 0);
649
  g_object_unref (accel_group);
650 651
}

652
/**
Matthias Clasen's avatar
Matthias Clasen committed
653
 * gtk_accel_group_connect_by_path:
Matthias Clasen's avatar
Matthias Clasen committed
654 655 656
 * @accel_group: the accelerator group to install an accelerator in
 * @accel_path: path used for determining key and modifiers
 * @closure: closure to be executed upon accelerator activation
657
 *
Matthias Clasen's avatar
Matthias Clasen committed
658 659 660 661 662 663
 * Installs an accelerator in this group, using an accelerator path
 * to look up the appropriate key and modifiers (see
 * gtk_accel_map_add_entry()). When @accel_group is being activated
 * in response to a call to gtk_accel_groups_activate(), @closure will
 * be invoked if the @accel_key and @accel_mods from
 * gtk_accel_groups_activate() match the key and modifiers for the path.
Matthias Clasen's avatar
Matthias Clasen committed
664
 *
665
 * The signature used for the @closure is that of #GtkAccelGroupActivate.
Matthias Clasen's avatar
Matthias Clasen committed
666 667 668 669
 *
 * Note that @accel_path string will be stored in a #GQuark. Therefore,
 * if you pass a static string, you can save some memory by interning it
 * first with g_intern_static_string().
670 671
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
672 673 674
gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
                                 const gchar   *accel_path,
                                 GClosure      *closure)
675
{
676 677 678
  guint accel_key = 0;
  GdkModifierType accel_mods = 0;
  GtkAccelKey key;
679

680 681 682
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
  g_return_if_fail (closure != NULL);
  g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
683

684 685
  if (closure->is_invalid)
    return;
686

687
  g_object_ref (accel_group);
688

689 690
  if (gtk_accel_map_lookup_entry (accel_path, &key))
    {
Tim Janik's avatar
Tim Janik committed
691
      accel_key = gdk_keyval_to_lower (key.accel_key);
692 693 694 695
      accel_mods = key.accel_mods;
    }

  quick_accel_add (accel_group, accel_key, accel_mods, GTK_ACCEL_VISIBLE, closure,
Matthias Clasen's avatar
Matthias Clasen committed
696
                   g_quark_from_string (accel_path));
697

698
  g_object_unref (accel_group);
Tim Janik's avatar
Tim Janik committed
699 700
}

701
/**
Matthias Clasen's avatar
Matthias Clasen committed
702
 * gtk_accel_group_disconnect:
703
 * @accel_group: the accelerator group to remove an accelerator from
Matthias Clasen's avatar
Matthias Clasen committed
704 705
 * @closure: (allow-none): the closure to remove from this accelerator
 *     group, or %NULL to remove all closures
706
 *
Matthias Clasen's avatar
Matthias Clasen committed
707
 * Removes an accelerator previously installed through
708
 * gtk_accel_group_connect().
709 710
 *
 * Since 2.20 @closure can be %NULL.
Matthias Clasen's avatar
Matthias Clasen committed
711 712
 *
 * Returns: %TRUE if the closure was found and got disconnected
713 714 715
 */
gboolean
gtk_accel_group_disconnect (GtkAccelGroup *accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
716
                            GClosure      *closure)
717 718 719 720 721
{
  guint i;

  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);

722 723
  for (i = 0; i < accel_group->priv->n_accels; i++)
    if (accel_group->priv->priv_accels[i].closure == closure)
724
      {
Matthias Clasen's avatar
Matthias Clasen committed
725 726 727 728
        g_object_ref (accel_group);
        quick_accel_remove (accel_group, i);
        g_object_unref (accel_group);
        return TRUE;
729 730 731 732 733
      }
  return FALSE;
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
734
 * gtk_accel_group_disconnect_key:
Matthias Clasen's avatar
Matthias Clasen committed
735 736 737
 * @accel_group: the accelerator group to install an accelerator in
 * @accel_key: key value of the accelerator
 * @accel_mods: modifier combination of the accelerator
738
 *
Matthias Clasen's avatar
Matthias Clasen committed
739
 * Removes an accelerator previously installed through
740
 * gtk_accel_group_connect().
Matthias Clasen's avatar
Matthias Clasen committed
741 742 743
 *
 * Returns: %TRUE if there was an accelerator which could be
 *     removed, %FALSE otherwise
744 745
 */
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
746 747 748
gtk_accel_group_disconnect_key (GtkAccelGroup   *accel_group,
                                guint            accel_key,
                                GdkModifierType  accel_mods)
Tim Janik's avatar
Tim Janik committed
749
{
750 751 752 753 754
  GtkAccelGroupEntry *entries;
  GSList *slist, *clist = NULL;
  gboolean removed_one = FALSE;
  guint n;

755 756
  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);

757
  g_object_ref (accel_group);
Matthias Clasen's avatar
Matthias Clasen committed
758

759
  accel_key = gdk_keyval_to_lower (accel_key);
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
  entries = quick_accel_find (accel_group, accel_key, accel_mods, &n);
  while (n--)
    {
      GClosure *closure = g_closure_ref (entries[n].closure);

      clist = g_slist_prepend (clist, closure);
    }

  for (slist = clist; slist; slist = slist->next)
    {
      GClosure *closure = slist->data;

      removed_one |= gtk_accel_group_disconnect (accel_group, closure);
      g_closure_unref (closure);
    }
  g_slist_free (clist);

  g_object_unref (accel_group);

  return removed_one;
}

void
_gtk_accel_group_reconnect (GtkAccelGroup *accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
784
                            GQuark         accel_path_quark)
785 786 787 788 789 790 791 792
{
  GSList *slist, *clist = NULL;
  guint i;

  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));

  g_object_ref (accel_group);

793 794
  for (i = 0; i < accel_group->priv->n_accels; i++)
    if (accel_group->priv->priv_accels[i].accel_path_quark == accel_path_quark)
795
      {
Matthias Clasen's avatar
Matthias Clasen committed
796
        GClosure *closure = g_closure_ref (accel_group->priv->priv_accels[i].closure);
797

Matthias Clasen's avatar
Matthias Clasen committed
798
        clist = g_slist_prepend (clist, closure);
799 800 801 802 803 804 805 806 807 808 809 810 811
      }

  for (slist = clist; slist; slist = slist->next)
    {
      GClosure *closure = slist->data;

      gtk_accel_group_disconnect (accel_group, closure);
      gtk_accel_group_connect_by_path (accel_group, g_quark_to_string (accel_path_quark), closure);
      g_closure_unref (closure);
    }
  g_slist_free (clist);

  g_object_unref (accel_group);
Tim Janik's avatar
Tim Janik committed
812 813
}

814 815 816 817 818 819 820 821
GSList*
_gtk_accel_group_get_accelerables (GtkAccelGroup *accel_group)
{
    g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);

    return accel_group->priv->acceleratables;
}

822 823
/**
 * gtk_accel_group_query:
Matthias Clasen's avatar
Matthias Clasen committed
824 825 826 827 828
 * @accel_group: the accelerator group to query
 * @accel_key: key value of the accelerator
 * @accel_mods: modifier combination of the accelerator
 * @n_entries: (allow-none): location to return the number
 *     of entries found, or %NULL
829
 *
Matthias Clasen's avatar
Matthias Clasen committed
830 831
 * Queries an accelerator group for all entries matching @accel_key
 * and @accel_mods.
Matthias Clasen's avatar
Matthias Clasen committed
832 833 834 835
 *
 * Returns: (transfer none) (array length=n_entries): an array of
 *     @n_entries #GtkAccelGroupEntry elements, or %NULL. The array
 *     is owned by GTK+ and must not be freed.
836
 */
837
GtkAccelGroupEntry*
Matthias Clasen's avatar
Matthias Clasen committed
838 839 840 841
gtk_accel_group_query (GtkAccelGroup   *accel_group,
                       guint            accel_key,
                       GdkModifierType  accel_mods,
                       guint           *n_entries)
Tim Janik's avatar
Tim Janik committed
842
{
843 844
  GtkAccelGroupEntry *entries;
  guint n;
Tim Janik's avatar
Tim Janik committed
845

846 847
  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);

848
  entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
849 850 851 852 853

  if (n_entries)
    *n_entries = entries ? n : 0;

  return entries;
Tim Janik's avatar
Tim Janik committed
854 855
}

856 857 858 859
/**
 * gtk_accel_group_from_accel_closure:
 * @closure: a #GClosure
 *
Matthias Clasen's avatar
Matthias Clasen committed
860
 * Finds the #GtkAccelGroup to which @closure is connected;
861
 * see gtk_accel_group_connect().
Matthias Clasen's avatar
Matthias Clasen committed
862 863 864
 *
 * Returns: (transfer none): the #GtkAccelGroup to which @closure
 *     is connected, or %NULL
865
 */
866 867
GtkAccelGroup*
gtk_accel_group_from_accel_closure (GClosure *closure)
Tim Janik's avatar
Tim Janik committed
868
{
869 870 871 872
  guint i;

  g_return_val_if_fail (closure != NULL, NULL);

Matthias Clasen's avatar
Matthias Clasen committed
873 874 875 876 877 878 879
  /* A few remarks on what we do here. in general, we need a way to
   * reverse lookup accel_groups from closures that are being used in
   * accel groups. this could be done e.g via a hashtable. it is however
   * cheaper (memory wise) to just use the invalidation notifier on the
   * closure itself (which we need to install anyway), that contains the
   * accel group as data which, besides needing to peek a bit at closure
   * internals, works just as good.
880 881
   */
  for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
882
    if (closure->notifiers[i].notify == accel_closure_invalidate)
883 884 885
      return closure->notifiers[i].data;

  return NULL;
Tim Janik's avatar
Tim Janik committed
886 887
}

Matthias Clasen's avatar
Matthias Clasen committed
888 889
/**
 * gtk_accel_group_activate:
Matthias Clasen's avatar
Matthias Clasen committed
890 891
 * @accel_group: a #GtkAccelGroup
 * @accel_quark: the quark for the accelerator name
Matthias Clasen's avatar
Matthias Clasen committed
892
 * @acceleratable: the #GObject, usually a #GtkWindow, on which
Matthias Clasen's avatar
Matthias Clasen committed
893 894 895 896 897 898
 *    to activate the accelerator
 * @accel_key: accelerator keyval from a key event
 * @accel_mods: keyboard state mask from a key event
 *
 * Finds the first accelerator in @accel_group that matches
 * @accel_key and @accel_mods, and activates it.
Matthias Clasen's avatar
Matthias Clasen committed
899
 *
Matthias Clasen's avatar
Matthias Clasen committed
900 901
 * Returns: %TRUE if an accelerator was activated and handled
 *     this keypress
Matthias Clasen's avatar
Matthias Clasen committed
902
 */
903
gboolean
904
gtk_accel_group_activate (GtkAccelGroup   *accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
905 906 907
                          GQuark           accel_quark,
                          GObject         *acceleratable,
                          guint            accel_key,
908
                          GdkModifierType  accel_mods)
Tim Janik's avatar
Tim Janik committed
909
{
910 911 912
  gboolean was_handled;

  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
913
  g_return_val_if_fail (G_IS_OBJECT (acceleratable), FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
914

915 916
  was_handled = FALSE;
  g_signal_emit (accel_group, signal_accel_activate, accel_quark,
Matthias Clasen's avatar
Matthias Clasen committed
917
                 acceleratable, accel_key, accel_mods, &was_handled);
918 919

  return was_handled;
Tim Janik's avatar
Tim Janik committed
920 921
}

922 923
/**
 * gtk_accel_groups_activate:
Matthias Clasen's avatar
Matthias Clasen committed
924 925 926 927 928
 * @object: the #GObject, usually a #GtkWindow, on which
 *     to activate the accelerator
 * @accel_key: accelerator keyval from a key event
 * @accel_mods: keyboard state mask from a key event
 *
929
 * Finds the first accelerator in any #GtkAccelGroup attached
930
 * to @object that matches @accel_key and @accel_mods, and
931
 * activates that accelerator.
Matthias Clasen's avatar
Matthias Clasen committed
932
 *
Matthias Clasen's avatar
Matthias Clasen committed
933 934
 * Returns: %TRUE if an accelerator was activated and handled
 *     this keypress
935 936
 */
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
937 938 939
gtk_accel_groups_activate (GObject         *object,
                           guint            accel_key,
                           GdkModifierType  accel_mods)
Tim Janik's avatar
Tim Janik committed
940
{
941
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
942

943 944 945 946 947
  if (gtk_accelerator_valid (accel_key, accel_mods))
    {
      gchar *accel_name;
      GQuark accel_quark;
      GSList *slist;
Tim Janik's avatar
Tim Janik committed
948

949
      accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
950 951
      accel_quark = g_quark_from_string (accel_name);
      g_free (accel_name);
Matthias Clasen's avatar
Matthias Clasen committed
952

953
      for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
Matthias Clasen's avatar
Matthias Clasen committed
954 955
        if (gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
          return TRUE;
956
    }
Matthias Clasen's avatar
Matthias Clasen committed
957

958
  return FALSE;
Tim Janik's avatar
Tim Janik committed
959 960
}

961
/**
Matthias Clasen's avatar
Matthias Clasen committed
962
 * gtk_accelerator_valid:
Matthias Clasen's avatar
Matthias Clasen committed
963
 * @keyval: a GDK keyval
964
 * @modifiers: modifier mask
965
 *
966
 * Determines whether a given keyval and modifier mask constitute
967
 * a valid keyboard accelerator. For example, the #GDK_KEY_a keyval
Matthias Clasen's avatar
Matthias Clasen committed
968
 * plus #GDK_CONTROL_MASK is valid - this is a "Ctrl+a" accelerator.
969
 * But, you can't, for instance, use the #GDK_KEY_Control_L keyval
970
 * as an accelerator.
Matthias Clasen's avatar
Matthias Clasen committed
971 972
 *
 * Returns: %TRUE if the accelerator is valid
973
 */
Tim Janik's avatar
Tim Janik committed
974
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
975 976
gtk_accelerator_valid (guint           keyval,
                       GdkModifierType modifiers)
Tim Janik's avatar
Tim Janik committed
977
{
978
  static const guint invalid_accelerator_vals[] = {
Matthias Clasen's avatar
Matthias Clasen committed
979 980 981 982 983 984
    GDK_KEY_Shift_L, GDK_KEY_Shift_R, GDK_KEY_Shift_Lock,
    GDK_KEY_Caps_Lock, GDK_KEY_ISO_Lock, GDK_KEY_Control_L,
    GDK_KEY_Control_R, GDK_KEY_Meta_L, GDK_KEY_Meta_R,
    GDK_KEY_Alt_L, GDK_KEY_Alt_R, GDK_KEY_Super_L, GDK_KEY_Super_R,
    GDK_KEY_Hyper_L, GDK_KEY_Hyper_R, GDK_KEY_ISO_Level3_Shift,
    GDK_KEY_ISO_Next_Group, GDK_KEY_ISO_Prev_Group,
985 986
    GDK_KEY_ISO_First_Group, GDK_KEY_ISO_Last_Group,
    GDK_KEY_Mode_switch, GDK_KEY_Num_Lock, GDK_KEY_Multi_key,
Matthias Clasen's avatar
Matthias Clasen committed
987
    GDK_KEY_Scroll_Lock, GDK_KEY_Sys_Req,
988 989 990 991
    GDK_KEY_Tab, GDK_KEY_ISO_Left_Tab, GDK_KEY_KP_Tab,
    GDK_KEY_First_Virtual_Screen, GDK_KEY_Prev_Virtual_Screen,
    GDK_KEY_Next_Virtual_Screen, GDK_KEY_Last_Virtual_Screen,
    GDK_KEY_Terminate_Server, GDK_KEY_AudibleBell_Enable,
Tim Janik's avatar
Tim Janik committed
992 993
    0
  };
994
  static const guint invalid_unmodified_vals[] = {
995 996
    GDK_KEY_Up, GDK_KEY_Down, GDK_KEY_Left, GDK_KEY_Right,
    GDK_KEY_KP_Up, GDK_KEY_KP_Down, GDK_KEY_KP_Left, GDK_KEY_KP_Right,
997 998
    0
  };
999
  const guint *ac_val;
Tim Janik's avatar
Tim Janik committed
1000 1001

  modifiers &= GDK_MODIFIER_MASK;
Matthias Clasen's avatar
Matthias Clasen committed
1002

Tim Janik's avatar
Tim Janik committed
1003 1004 1005 1006 1007 1008 1009
  if (keyval <= 0xFF)
    return keyval >= 0x20;

  ac_val = invalid_accelerator_vals;
  while (*ac_val)
    {
      if (keyval == *ac_val++)
Matthias Clasen's avatar
Matthias Clasen committed
1010
        return FALSE;
Tim Janik's avatar
Tim Janik committed
1011 1012
    }

1013 1014 1015 1016
  if (!modifiers)
    {
      ac_val = invalid_unmodified_vals;
      while (*ac_val)
Matthias Clasen's avatar
Matthias Clasen committed
1017 1018 1019 1020
        {
          if (keyval == *ac_val++)
            return FALSE;
        }
1021
    }
Matthias Clasen's avatar
Matthias Clasen committed
1022

Tim Janik's avatar
Tim Janik committed
1023 1024 1025 1026 1027 1028 1029
  return TRUE;
}

static inline gboolean
is_alt (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1030 1031 1032 1033
          (string[1] == 'a' || string[1] == 'A') &&
          (string[2] == 'l' || string[2] == 'L') &&
          (string[3] == 't' || string[3] == 'T') &&
          (string[4] == '>'));
Tim Janik's avatar
Tim Janik committed
1034 1035 1036 1037 1038 1039
}

static inline gboolean
is_ctl (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1040 1041 1042 1043
          (string[1] == 'c' || string[1] == 'C') &&
          (string[2] == 't' || string[2] == 'T') &&
          (string[3] == 'l' || string[3] == 'L') &&
          (string[4] == '>'));
Tim Janik's avatar
Tim Janik committed
1044 1045 1046 1047 1048 1049
}

static inline gboolean
is_modx (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1050 1051 1052 1053 1054
          (string[1] == 'm' || string[1] == 'M') &&
          (string[2] == 'o' || string[2] == 'O') &&
          (string[3] == 'd' || string[3] == 'D') &&
          (string[4] >= '1' && string[4] <= '5') &&
          (string[5] == '>'));
Tim Janik's avatar
Tim Janik committed
1055 1056 1057 1058 1059 1060
}

static inline gboolean
is_ctrl (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1061 1062 1063 1064 1065
          (string[1] == 'c' || string[1] == 'C') &&
          (string[2] == 't' || string[2] == 'T') &&
          (string[3] == 'r' || string[3] == 'R') &&
          (string[4] == 'l' || string[4] == 'L') &&
          (string[5] == '>'));
Tim Janik's avatar
Tim Janik committed
1066 1067 1068 1069 1070 1071
}

static inline gboolean
is_shft (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1072 1073 1074 1075 1076
          (string[1] == 's' || string[1] == 'S') &&
          (string[2] == 'h' || string[2] == 'H') &&
          (string[3] == 'f' || string[3] == 'F') &&
          (string[4] == 't' || string[4] == 'T') &&
          (string[5] == '>'));
Tim Janik's avatar
Tim Janik committed
1077 1078 1079 1080 1081 1082
}

static inline gboolean
is_shift (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1083 1084 1085 1086 1087 1088
          (string[1] == 's' || string[1] == 'S') &&
          (string[2] == 'h' || string[2] == 'H') &&
          (string[3] == 'i' || string[3] == 'I') &&
          (string[4] == 'f' || string[4] == 'F') &&
          (string[5] == 't' || string[5] == 'T') &&
          (string[6] == '>'));
Tim Janik's avatar
Tim Janik committed
1089 1090 1091 1092 1093 1094
}

static inline gboolean
is_control (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1095 1096 1097 1098 1099 1100 1101 1102
          (string[1] == 'c' || string[1] == 'C') &&
          (string[2] == 'o' || string[2] == 'O') &&
          (string[3] == 'n' || string[3] == 'N') &&
          (string[4] == 't' || string[4] == 'T') &&
          (string[5] == 'r' || string[5] == 'R') &&
          (string[6] == 'o' || string[6] == 'O') &&
          (string[7] == 'l' || string[7] == 'L') &&
          (string[8] == '>'));
Tim Janik's avatar
Tim Janik committed
1103 1104
}

1105 1106 1107 1108
static inline gboolean
is_release (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1109 1110 1111 1112 1113 1114 1115 1116
          (string[1] == 'r' || string[1] == 'R') &&
          (string[2] == 'e' || string[2] == 'E') &&
          (string[3] == 'l' || string[3] == 'L') &&
          (string[4] == 'e' || string[4] == 'E') &&
          (string[5] == 'a' || string[5] == 'A') &&
          (string[6] == 's' || string[6] == 'S') &&
          (string[7] == 'e' || string[7] == 'E') &&
          (string[8] == '>'));
1117 1118
}

1119 1120 1121 1122
static inline gboolean
is_meta (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1123 1124 1125 1126 1127
          (string[1] == 'm' || string[1] == 'M') &&
          (string[2] == 'e' || string[2] == 'E') &&
          (string[3] == 't' || string[3] == 'T') &&
          (string[4] == 'a' || string[4] == 'A') &&
          (string[5] == '>'));
1128 1129 1130 1131 1132 1133
}

static inline gboolean
is_super (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1134 1135 1136 1137 1138 1139
          (string[1] == 's' || string[1] == 'S') &&
          (string[2] == 'u' || string[2] == 'U') &&
          (string[3] == 'p' || string[3] == 'P') &&
          (string[4] == 'e' || string[4] == 'E') &&
          (string[5] == 'r' || string[5] == 'R') &&
          (string[6] == '>'));
1140 1141 1142 1143 1144 1145
}

static inline gboolean
is_hyper (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1146 1147 1148 1149 1150 1151
          (string[1] == 'h' || string[1] == 'H') &&
          (string[2] == 'y' || string[2] == 'Y') &&
          (string[3] == 'p' || string[3] == 'P') &&
          (string[4] == 'e' || string[4] == 'E') &&
          (string[5] == 'r' || string[5] == 'R') &&
          (string[6] == '>'));
1152 1153
}

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
static inline gboolean
is_primary (const gchar *string)
{
  return ((string[0] == '<') &&
	  (string[1] == 'p' || string[1] == 'P') &&
	  (string[2] == 'r' || string[2] == 'R') &&
	  (string[3] == 'i' || string[3] == 'I') &&
	  (string[4] == 'm' || string[4] == 'M') &&
	  (string[5] == 'a' || string[5] == 'A') &&
	  (string[6] == 'r' || string[6] == 'R') &&
	  (string[7] == 'y' || string[7] == 'Y') &&
	  (string[8] == '>'));
}

1168 1169 1170 1171 1172 1173 1174 1175 1176
static inline gboolean
is_keycode (const gchar *string)
{
  return (string[0] == '0' &&
          string[1] == 'x' &&
          g_ascii_isxdigit (string[2]) &&
          g_ascii_isxdigit (string[3]));
}

1177
/**
1178
 * gtk_accelerator_parse_with_keycode:
Matthias Clasen's avatar
Matthias Clasen committed
1179
 * @accelerator: string representing an accelerator
1180 1181
 * @accelerator_key: (out) (allow-none): return location for accelerator
 *     keyval, or %NULL
1182 1183
 * @accelerator_codes: (out) (allow-none): return location for accelerator
 *     keycodes, or %NULL
1184 1185
 * @accelerator_mods: (out) (allow-none): return location for accelerator
 *     modifier mask, %NULL
1186
 *
1187 1188 1189 1190
 * Parses a string representing an accelerator, similarly to
 * gtk_accelerator_parse() but handles keycodes as well. This is only
 * useful for system-level components, applications should use
 * gtk_accelerator_parse() instead.
Matthias Clasen's avatar
Matthias Clasen committed
1191
 *
1192 1193
 * If a keycode is present in the accelerator and no @accelerator_codes
 * is given, the parse will fail.
1194
 *
1195 1196 1197 1198
 * If the parse fails, @accelerator_key, @accelerator_mods and
 * @accelerator_codes will be set to 0 (zero).
 *
 * Since: 3.4
1199
 */
Tim Janik's avatar
Tim Janik committed
1200
void
1201 1202 1203 1204
gtk_accelerator_parse_with_keycode (const gchar     *accelerator,
                                    guint           *accelerator_key,
                                    guint          **accelerator_codes,
                                    GdkModifierType *accelerator_mods)
Tim Janik's avatar
Tim Janik committed
1205 1206
{
  guint keyval;
1207
  GdkModifierType mods;
Tim Janik's avatar
Tim Janik committed
1208
  gint len;
1209
  gboolean error;
Matthias Clasen's avatar
Matthias Clasen committed
1210

Tim Janik's avatar
Tim Janik committed
1211 1212 1213 1214
  if (accelerator_key)
    *accelerator_key = 0;
  if (accelerator_mods)
    *accelerator_mods = 0;
1215 1216
  if (accelerator_codes)
    *accelerator_codes = NULL;
Tim Janik's avatar
Tim Janik committed
1217
  g_return_if_fail (accelerator != NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1218