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

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21
 * 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
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23
 */
Manish Singh's avatar
Manish Singh committed
24

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

Tim Janik's avatar
Tim Janik committed
29
#include "gtkaccelgroup.h"
30
#include "gtkaccelgroupprivate.h"
31
#include "gtkaccellabel.h"
Matthias Clasen's avatar
Matthias Clasen committed
32
#include "gtkaccelmapprivate.h"
33
#include "gtkintl.h"
34
#include "gtkmarshalers.h"
35
#include "gtkprivate.h"
36

37 38
/**
 * SECTION:gtkaccelgroup
Matthias Clasen's avatar
Matthias Clasen committed
39 40
 * @Short_description: Groups of global keyboard accelerators for an
 *     entire GtkWindow
41
 * @Title: Accelerator Groups
42 43
 * @See_also: gtk_window_add_accel_group(), gtk_accel_map_change_entry(),
 * gtk_label_new_with_mnemonic()
44
 *
45 46
 * A #GtkAccelGroup represents a group of keyboard accelerators,
 * typically attached to a toplevel #GtkWindow (with
47
 * gtk_window_add_accel_group()). Usually you won’t need to create a
48 49
 * #GtkAccelGroup directly; instead, when using #GtkUIManager, GTK+
 * automatically sets up the accelerators for your menus in the ui
50
 * manager’s #GtkAccelGroup.
Matthias Clasen's avatar
Matthias Clasen committed
51
 *
52 53
 * Note that “accelerators” are different from
 * “mnemonics”. Accelerators are shortcuts for
54
 * activating a menu item; they appear alongside the menu item they’re a
55
 * shortcut for. For example “Ctrl+Q” might appear alongside the “Quit”
56 57 58 59 60 61
 * 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.
 */

62
/* --- prototypes --- */
63 64 65 66 67 68 69
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);
70

Tim Janik's avatar
Tim Janik committed
71 72

/* --- variables --- */
73 74 75
static guint  signal_accel_activate      = 0;
static guint  signal_accel_changed       = 0;
static guint  quark_acceleratable_groups = 0;
76
static guint  default_accel_mod_mask     = 0;
77

78
enum {
79 80 81
  PROP_0,
  PROP_IS_LOCKED,
  PROP_MODIFIER_MASK,
82
  N_PROPERTIES
83 84
};

85 86
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

87
G_DEFINE_TYPE_WITH_PRIVATE (GtkAccelGroup, gtk_accel_group, G_TYPE_OBJECT)
88

89

Matthias Clasen's avatar
Matthias Clasen committed
90
/* --- functions --- */
91
static void
92
gtk_accel_group_class_init (GtkAccelGroupClass *class)
Tim Janik's avatar
Tim Janik committed
93
{
94 95 96 97 98
  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;
99
  object_class->get_property = gtk_accel_group_get_property;
100 101

  class->accel_changed = NULL;
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  obj_properties [PROP_IS_LOCKED] =
    g_param_spec_boolean ("is-locked",
                          "Is locked",
                          "Is the accel group locked",
                          FALSE,
                          G_PARAM_READABLE);

  obj_properties [PROP_MODIFIER_MASK] =
    g_param_spec_flags ("modifier-mask",
                        "Modifier Mask",
                        "Modifier Mask",
                        GDK_TYPE_MODIFIER_TYPE,
                        default_accel_mod_mask,
                        G_PARAM_READABLE);

   g_object_class_install_properties (object_class,
                                      N_PROPERTIES,
                                      obj_properties);
121

122 123 124 125 126 127 128 129 130
  /**
   * 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
131
   *
132 133
   * Returns: %TRUE if the accelerator was activated
   */
Manish Singh's avatar
Manish Singh committed
134
  signal_accel_activate =
135
    g_signal_new (I_("accel-activate"),
Matthias Clasen's avatar
Matthias Clasen committed
136 137 138 139 140 141 142 143 144
                  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);
145 146 147 148 149 150 151
  /**
   * 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
   *
152
   * The accel-changed signal is emitted when an entry
Matthias Clasen's avatar
Matthias Clasen committed
153
   * is added to or removed from the accel group.
154
   *
Matthias Clasen's avatar
Matthias Clasen committed
155 156
   * Widgets like #GtkAccelLabel which display an associated
   * accelerator should connect to this signal, and rebuild
157 158
   * their visual representation if the @accel_closure is theirs.
   */
Manish Singh's avatar
Manish Singh committed
159
  signal_accel_changed =
160
    g_signal_new (I_("accel-changed"),
Matthias Clasen's avatar
Matthias Clasen committed
161 162 163 164 165 166 167 168 169
                  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);
170 171
}

172 173
static void
gtk_accel_group_finalize (GObject *object)
174
{
175
  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);
176
  guint i;
Matthias Clasen's avatar
Matthias Clasen committed
177

178
  for (i = 0; i < accel_group->priv->n_accels; i++)
179
    {
180
      GtkAccelGroupEntry *entry = &accel_group->priv->priv_accels[i];
181 182

      if (entry->accel_path_quark)
Matthias Clasen's avatar
Matthias Clasen committed
183 184
        {
          const gchar *accel_path = g_quark_to_string (entry->accel_path_quark);
185

Matthias Clasen's avatar
Matthias Clasen committed
186 187
          _gtk_accel_map_remove_group (accel_path, accel_group);
        }
188
      g_closure_remove_invalidate_notifier (entry->closure, accel_group, accel_closure_invalidate);
189 190 191

      /* remove quick_accel_add() refcount */
      g_closure_unref (entry->closure);
192
    }
193

194
  g_free (accel_group->priv->priv_accels);
195

Matthias Clasen's avatar
Matthias Clasen committed
196
  G_OBJECT_CLASS (gtk_accel_group_parent_class)->finalize (object);
Tim Janik's avatar
Tim Janik committed
197 198
}

199 200 201 202 203 204 205 206
static void
gtk_accel_group_get_property (GObject    *object,
                              guint       param_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (object);

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

221 222
static void
gtk_accel_group_init (GtkAccelGroup *accel_group)
Tim Janik's avatar
Tim Janik committed
223
{
224 225
  GtkAccelGroupPrivate *priv;

226
  accel_group->priv = gtk_accel_group_get_instance_private (accel_group);
227
  priv = accel_group->priv;
228 229 230 231 232 233

  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
234 235
}

236
/**
Matthias Clasen's avatar
Matthias Clasen committed
237
 * gtk_accel_group_new:
Matthias Clasen's avatar
Matthias Clasen committed
238 239
 *
 * Creates a new #GtkAccelGroup.
Matthias Clasen's avatar
Matthias Clasen committed
240 241
 *
 * Returns: a new #GtkAccelGroup object
242
 */
Tim Janik's avatar
Tim Janik committed
243
GtkAccelGroup*
244
gtk_accel_group_new (void)
Tim Janik's avatar
Tim Janik committed
245
{
246
  return g_object_new (GTK_TYPE_ACCEL_GROUP, NULL);
Tim Janik's avatar
Tim Janik committed
247 248
}

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

266
  return accel_group->priv->lock_count > 0;
267 268
}

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

285
  return accel_group->priv->modifier_mask;
286 287
}

Tim Janik's avatar
Tim Janik committed
288
static void
289
accel_group_weak_ref_detach (GSList  *free_list,
Matthias Clasen's avatar
Matthias Clasen committed
290
                             GObject *stale_object)
Tim Janik's avatar
Tim Janik committed
291
{
292
  GSList *slist;
Matthias Clasen's avatar
Matthias Clasen committed
293

Tim Janik's avatar
Tim Janik committed
294 295 296
  for (slist = free_list; slist; slist = slist->next)
    {
      GtkAccelGroup *accel_group;
Matthias Clasen's avatar
Matthias Clasen committed
297

Tim Janik's avatar
Tim Janik committed
298
      accel_group = slist->data;
299
      accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, stale_object);
300
      g_object_unref (accel_group);
Tim Janik's avatar
Tim Janik committed
301 302
    }
  g_slist_free (free_list);
303
  g_object_set_qdata (stale_object, quark_acceleratable_groups, NULL);
Tim Janik's avatar
Tim Janik committed
304 305 306
}

void
307
_gtk_accel_group_attach (GtkAccelGroup *accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
308
                         GObject       *object)
Tim Janik's avatar
Tim Janik committed
309 310
{
  GSList *slist;
Matthias Clasen's avatar
Matthias Clasen committed
311

312
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
313
  g_return_if_fail (G_IS_OBJECT (object));
314
  g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) == NULL);
Matthias Clasen's avatar
Matthias Clasen committed
315

316
  g_object_ref (accel_group);
317
  accel_group->priv->acceleratables = g_slist_prepend (accel_group->priv->acceleratables, object);
318
  slist = g_object_get_qdata (object, quark_acceleratable_groups);
319
  if (slist)
320
    g_object_weak_unref (object,
Matthias Clasen's avatar
Matthias Clasen committed
321 322
                         (GWeakNotify) accel_group_weak_ref_detach,
                         slist);
Tim Janik's avatar
Tim Janik committed
323
  slist = g_slist_prepend (slist, accel_group);
324 325
  g_object_set_qdata (object, quark_acceleratable_groups, slist);
  g_object_weak_ref (object,
Matthias Clasen's avatar
Matthias Clasen committed
326 327
                     (GWeakNotify) accel_group_weak_ref_detach,
                     slist);
Tim Janik's avatar
Tim Janik committed
328 329 330
}

void
331
_gtk_accel_group_detach (GtkAccelGroup *accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
332
                         GObject       *object)
Tim Janik's avatar
Tim Janik committed
333 334
{
  GSList *slist;
Matthias Clasen's avatar
Matthias Clasen committed
335

336
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
337
  g_return_if_fail (G_IS_OBJECT (object));
338
  g_return_if_fail (g_slist_find (accel_group->priv->acceleratables, object) != NULL);
Matthias Clasen's avatar
Matthias Clasen committed
339

340
  accel_group->priv->acceleratables = g_slist_remove (accel_group->priv->acceleratables, object);
341 342
  slist = g_object_get_qdata (object, quark_acceleratable_groups);
  g_object_weak_unref (object,
Matthias Clasen's avatar
Matthias Clasen committed
343 344
                       (GWeakNotify) accel_group_weak_ref_detach,
                       slist);
Tim Janik's avatar
Tim Janik committed
345
  slist = g_slist_remove (slist, accel_group);
346
  g_object_set_qdata (object, quark_acceleratable_groups, slist);
347
  if (slist)
348
    g_object_weak_ref (object,
Matthias Clasen's avatar
Matthias Clasen committed
349 350
                       (GWeakNotify) accel_group_weak_ref_detach,
                       slist);
351 352 353
  g_object_unref (accel_group);
}

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

368 369 370
  return g_object_get_qdata (object, quark_acceleratable_groups);
}

371 372 373
/**
 * gtk_accel_group_find:
 * @accel_group: a #GtkAccelGroup
374 375
 * @find_func: (scope call): a function to filter the entries
 *    of @accel_group with
376 377
 * @data: data to pass to @find_func
 *
Matthias Clasen's avatar
Matthias Clasen committed
378
 * Finds the first entry in an accelerator group for which
Matthias Clasen's avatar
Matthias Clasen committed
379
 * @find_func returns %TRUE and returns its #GtkAccelKey.
Matthias Clasen's avatar
Matthias Clasen committed
380 381 382
 *
 * Returns: (transfer none): the key of the first entry passing
 *    @find_func. The key is owned by GTK+ and must not be freed.
383
 */
384
GtkAccelKey*
Matthias Clasen's avatar
Matthias Clasen committed
385 386 387
gtk_accel_group_find (GtkAccelGroup         *accel_group,
                      GtkAccelGroupFindFunc  find_func,
                      gpointer               data)
388 389 390 391 392 393 394 395
{
  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);
396 397
  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
398 399
                   accel_group->priv->priv_accels[i].closure,
                   data))
400
      {
Matthias Clasen's avatar
Matthias Clasen committed
401 402
        key = &accel_group->priv->priv_accels[i].key;
        break;
403 404 405 406
      }
  g_object_unref (accel_group);

  return key;
Tim Janik's avatar
Tim Janik committed
407 408
}

409
/**
Matthias Clasen's avatar
Matthias Clasen committed
410
 * gtk_accel_group_lock:
411
 * @accel_group: a #GtkAccelGroup
Matthias Clasen's avatar
Matthias Clasen committed
412
 *
Matthias Clasen's avatar
Matthias Clasen committed
413 414
 * Locks the given accelerator group.
 *
415 416 417
 * 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.
418 419 420 421
 *
 * If called more than once, @accel_group remains locked until
 * gtk_accel_group_unlock() has been called an equivalent number
 * of times.
422
 */
Tim Janik's avatar
Tim Janik committed
423
void
424
gtk_accel_group_lock (GtkAccelGroup *accel_group)
Tim Janik's avatar
Tim Janik committed
425
{
426
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
Matthias Clasen's avatar
Matthias Clasen committed
427

428
  accel_group->priv->lock_count += 1;
429

430
  if (accel_group->priv->lock_count == 1) {
431
    /* State change from unlocked to locked */
432
    g_object_notify_by_pspec (G_OBJECT (accel_group), obj_properties[PROP_IS_LOCKED]);
433
  }
Tim Janik's avatar
Tim Janik committed
434 435
}

436
/**
Matthias Clasen's avatar
Matthias Clasen committed
437
 * gtk_accel_group_unlock:
438
 * @accel_group: a #GtkAccelGroup
Matthias Clasen's avatar
Matthias Clasen committed
439
 *
Matthias Clasen's avatar
Matthias Clasen committed
440
 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
441
 */
Tim Janik's avatar
Tim Janik committed
442
void
443
gtk_accel_group_unlock (GtkAccelGroup *accel_group)
Tim Janik's avatar
Tim Janik committed
444
{
445
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
446
  g_return_if_fail (accel_group->priv->lock_count > 0);
Tim Janik's avatar
Tim Janik committed
447

448
  accel_group->priv->lock_count -= 1;
449

450
  if (accel_group->priv->lock_count < 1) {
451
    /* State change from locked to unlocked */
452
    g_object_notify_by_pspec (G_OBJECT (accel_group), obj_properties[PROP_IS_LOCKED]);
453
  }
Tim Janik's avatar
Tim Janik committed
454 455
}

456
static void
457
accel_closure_invalidate (gpointer  data,
Matthias Clasen's avatar
Matthias Clasen committed
458
                          GClosure *closure)
Tim Janik's avatar
Tim Janik committed
459
{
460 461 462
  GtkAccelGroup *accel_group = GTK_ACCEL_GROUP (data);

  gtk_accel_group_disconnect (accel_group, closure);
Tim Janik's avatar
Tim Janik committed
463 464
}

465 466
static int
bsearch_compare_accels (const void *d1,
Matthias Clasen's avatar
Matthias Clasen committed
467
                        const void *d2)
Tim Janik's avatar
Tim Janik committed
468
{
469 470 471 472 473 474 475
  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
476 477
}

478
static void
Matthias Clasen's avatar
Matthias Clasen committed
479 480 481 482 483 484
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
485
{
486
  guint pos, i = accel_group->priv->n_accels++;
487 488
  GtkAccelGroupEntry key;

489
  /* find position */
490 491 492
  key.key.accel_key = accel_key;
  key.key.accel_mods = accel_mods;
  for (pos = 0; pos < i; pos++)
493
    if (bsearch_compare_accels (&key, accel_group->priv->priv_accels + pos) < 0)
494
      break;
495 496

  /* insert at position, ref closure */
497
  accel_group->priv->priv_accels = g_renew (GtkAccelGroupEntry, accel_group->priv->priv_accels, accel_group->priv->n_accels);
498 499
  memmove (accel_group->priv->priv_accels + pos + 1, accel_group->priv->priv_accels + pos,
           (i - pos) * sizeof (accel_group->priv->priv_accels[0]));
500 501 502 503 504
  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;
505
  g_closure_sink (closure);
Matthias Clasen's avatar
Matthias Clasen committed
506

507 508 509 510 511 512 513 514 515 516 517 518
  /* 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);
519

520
      g_free (accel_name);
Matthias Clasen's avatar
Matthias Clasen committed
521

522 523
      /* setup handler */
      g_signal_connect_closure_by_id (accel_group, signal_accel_activate, accel_quark, closure, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
524

525 526 527 528 529 530
      /* 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
531 532
quick_accel_remove (GtkAccelGroup *accel_group,
                    guint          pos)
533 534
{
  GQuark accel_quark = 0;
535
  GtkAccelGroupEntry *entry = accel_group->priv->priv_accels + pos;
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  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
553 554 555
                                          G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_CLOSURE,
                                          signal_accel_activate, accel_quark,
                                          closure, NULL, NULL);
556 557 558 559 560
  /* 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 */
561
  accel_group->priv->n_accels -= 1;
562 563
  memmove (entry, entry + 1,
           (accel_group->priv->n_accels - pos) * sizeof (accel_group->priv->priv_accels[0]));
564 565 566 567 568 569 570

  /* 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
571 572
}

573
static GtkAccelGroupEntry*
Matthias Clasen's avatar
Matthias Clasen committed
574 575 576 577
quick_accel_find (GtkAccelGroup   *accel_group,
                  guint            accel_key,
                  GdkModifierType  accel_mods,
                  guint           *count_p)
Tim Janik's avatar
Tim Janik committed
578
{
579 580 581
  GtkAccelGroupEntry *entry;
  GtkAccelGroupEntry key;

582 583
  *count_p = 0;

584
  if (!accel_group->priv->n_accels)
585 586 587 588
    return NULL;

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

592 593 594 595
  if (!entry)
    return NULL;

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

608
/**
Matthias Clasen's avatar
Matthias Clasen committed
609
 * gtk_accel_group_connect:
Matthias Clasen's avatar
Matthias Clasen committed
610 611 612 613 614
 * @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
615
 *
Matthias Clasen's avatar
Matthias Clasen committed
616 617 618 619
 * 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
620
 *
621
 * The signature used for the @closure is that of #GtkAccelGroupActivate.
Matthias Clasen's avatar
Matthias Clasen committed
622 623 624
 *
 * Note that, due to implementation details, a single closure can
 * only be connected to one accelerator group.
625
 */
Tim Janik's avatar
Tim Janik committed
626
void
Matthias Clasen's avatar
Matthias Clasen committed
627 628 629 630 631
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
632
{
633 634 635
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
  g_return_if_fail (closure != NULL);
  g_return_if_fail (accel_key > 0);
636
  g_return_if_fail (gtk_accel_group_from_accel_closure (closure) == NULL);
637

638 639
  g_object_ref (accel_group);
  if (!closure->is_invalid)
640
    quick_accel_add (accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
641 642
                     gdk_keyval_to_lower (accel_key),
                     accel_mods, accel_flags, closure, 0);
643
  g_object_unref (accel_group);
644 645
}

646
/**
Matthias Clasen's avatar
Matthias Clasen committed
647
 * gtk_accel_group_connect_by_path:
Matthias Clasen's avatar
Matthias Clasen committed
648 649 650
 * @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
651
 *
Matthias Clasen's avatar
Matthias Clasen committed
652 653 654 655 656 657
 * 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
658
 *
659
 * The signature used for the @closure is that of #GtkAccelGroupActivate.
Matthias Clasen's avatar
Matthias Clasen committed
660 661 662 663
 *
 * 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().
664 665
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
666 667 668
gtk_accel_group_connect_by_path (GtkAccelGroup *accel_group,
                                 const gchar   *accel_path,
                                 GClosure      *closure)
669
{
670 671 672
  guint accel_key = 0;
  GdkModifierType accel_mods = 0;
  GtkAccelKey key;
673

674 675 676
  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));
677

678 679
  if (closure->is_invalid)
    return;
680

681
  g_object_ref (accel_group);
682

683 684
  if (gtk_accel_map_lookup_entry (accel_path, &key))
    {
Tim Janik's avatar
Tim Janik committed
685
      accel_key = gdk_keyval_to_lower (key.accel_key);
686 687 688 689
      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
690
                   g_quark_from_string (accel_path));
691

692
  g_object_unref (accel_group);
Tim Janik's avatar
Tim Janik committed
693 694
}

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

  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);

716 717
  for (i = 0; i < accel_group->priv->n_accels; i++)
    if (accel_group->priv->priv_accels[i].closure == closure)
718
      {
Matthias Clasen's avatar
Matthias Clasen committed
719 720 721 722
        g_object_ref (accel_group);
        quick_accel_remove (accel_group, i);
        g_object_unref (accel_group);
        return TRUE;
723 724 725 726 727
      }
  return FALSE;
}

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

749 750
  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);

751
  g_object_ref (accel_group);
Matthias Clasen's avatar
Matthias Clasen committed
752

753
  accel_key = gdk_keyval_to_lower (accel_key);
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  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
778
                            GQuark         accel_path_quark)
779 780 781 782 783 784 785 786
{
  GSList *slist, *clist = NULL;
  guint i;

  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));

  g_object_ref (accel_group);

787 788
  for (i = 0; i < accel_group->priv->n_accels; i++)
    if (accel_group->priv->priv_accels[i].accel_path_quark == accel_path_quark)
789
      {
Matthias Clasen's avatar
Matthias Clasen committed
790
        GClosure *closure = g_closure_ref (accel_group->priv->priv_accels[i].closure);
791

Matthias Clasen's avatar
Matthias Clasen committed
792
        clist = g_slist_prepend (clist, closure);
793 794 795 796 797 798 799 800 801 802 803 804 805
      }

  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
806 807
}

808 809 810 811 812 813 814 815
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;
}

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

840 841
  g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), NULL);

842
  entries = quick_accel_find (accel_group, gdk_keyval_to_lower (accel_key), accel_mods, &n);
843 844 845 846 847

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

  return entries;
Tim Janik's avatar
Tim Janik committed
848 849
}

850 851 852 853
/**
 * gtk_accel_group_from_accel_closure:
 * @closure: a #GClosure
 *
Matthias Clasen's avatar
Matthias Clasen committed
854
 * Finds the #GtkAccelGroup to which @closure is connected;
855
 * see gtk_accel_group_connect().
Matthias Clasen's avatar
Matthias Clasen committed
856
 *
857
 * Returns: (nullable) (transfer none): the #GtkAccelGroup to which @closure
Matthias Clasen's avatar
Matthias Clasen committed
858
 *     is connected, or %NULL
859
 */
860 861
GtkAccelGroup*
gtk_accel_group_from_accel_closure (GClosure *closure)
Tim Janik's avatar
Tim Janik committed
862
{
863 864 865 866
  guint i;

  g_return_val_if_fail (closure != NULL, NULL);

Matthias Clasen's avatar
Matthias Clasen committed
867 868 869 870 871 872 873
  /* 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.
874 875
   */
  for (i = 0; i < G_CLOSURE_N_NOTIFIERS (closure); i++)
876
    if (closure->notifiers[i].notify == accel_closure_invalidate)
877 878 879
      return closure->notifiers[i].data;

  return NULL;
Tim Janik's avatar
Tim Janik committed
880 881
}

Matthias Clasen's avatar
Matthias Clasen committed
882 883
/**
 * gtk_accel_group_activate:
Matthias Clasen's avatar
Matthias Clasen committed
884 885
 * @accel_group: a #GtkAccelGroup
 * @accel_quark: the quark for the accelerator name
Matthias Clasen's avatar
Matthias Clasen committed
886
 * @acceleratable: the #GObject, usually a #GtkWindow, on which
Matthias Clasen's avatar
Matthias Clasen committed
887 888 889 890 891 892
 *    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
893
 *
Matthias Clasen's avatar
Matthias Clasen committed
894 895
 * Returns: %TRUE if an accelerator was activated and handled
 *     this keypress
Matthias Clasen's avatar
Matthias Clasen committed
896
 */
897
gboolean
898
gtk_accel_group_activate (GtkAccelGroup   *accel_group,
Matthias Clasen's avatar
Matthias Clasen committed
899 900 901
                          GQuark           accel_quark,
                          GObject         *acceleratable,
                          guint            accel_key,
902
                          GdkModifierType  accel_mods)
Tim Janik's avatar
Tim Janik committed
903
{
904 905 906
  gboolean was_handled;

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

909 910
  was_handled = FALSE;
  g_signal_emit (accel_group, signal_accel_activate, accel_quark,
Matthias Clasen's avatar
Matthias Clasen committed
911
                 acceleratable, accel_key, accel_mods, &was_handled);
912 913

  return was_handled;
Tim Janik's avatar
Tim Janik committed
914 915
}

916 917
/**
 * gtk_accel_groups_activate:
Matthias Clasen's avatar
Matthias Clasen committed
918 919 920 921 922
 * @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
 *
923
 * Finds the first accelerator in any #GtkAccelGroup attached
924
 * to @object that matches @accel_key and @accel_mods, and
925
 * activates that accelerator.
Matthias Clasen's avatar
Matthias Clasen committed
926
 *
Matthias Clasen's avatar
Matthias Clasen committed
927 928
 * Returns: %TRUE if an accelerator was activated and handled
 *     this keypress
929 930
 */
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
931 932 933
gtk_accel_groups_activate (GObject         *object,
                           guint            accel_key,
                           GdkModifierType  accel_mods)
Tim Janik's avatar
Tim Janik committed
934
{
935
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
936

937 938 939 940 941
  if (gtk_accelerator_valid (accel_key, accel_mods))
    {
      gchar *accel_name;
      GQuark accel_quark;
      GSList *slist;
Tim Janik's avatar
Tim Janik committed
942

943
      accel_name = gtk_accelerator_name (accel_key, (accel_mods & gtk_accelerator_get_default_mod_mask ()));
944 945
      accel_quark = g_quark_from_string (accel_name);
      g_free (accel_name);
Matthias Clasen's avatar
Matthias Clasen committed
946

947
      for (slist = gtk_accel_groups_from_object (object); slist; slist = slist->next)
Matthias Clasen's avatar
Matthias Clasen committed
948 949
        if (gtk_accel_group_activate (slist->data, accel_quark, object, accel_key, accel_mods))
          return TRUE;
950
    }
Matthias Clasen's avatar
Matthias Clasen committed
951

952
  return FALSE;
Tim Janik's avatar
Tim Janik committed
953 954
}

955
/**
Matthias Clasen's avatar
Matthias Clasen committed
956
 * gtk_accelerator_valid:
Matthias Clasen's avatar
Matthias Clasen committed
957
 * @keyval: a GDK keyval
958
 * @modifiers: modifier mask
959
 *
960
 * Determines whether a given keyval and modifier mask constitute
961
 * a valid keyboard accelerator. For example, the #GDK_KEY_a keyval
962
 * plus #GDK_CONTROL_MASK is valid - this is a “Ctrl+a” accelerator.
963
 * But, you can't, for instance, use the #GDK_KEY_Control_L keyval
964
 * as an accelerator.
Matthias Clasen's avatar
Matthias Clasen committed
965 966
 *
 * Returns: %TRUE if the accelerator is valid
967
 */
Tim Janik's avatar
Tim Janik committed
968
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
969 970
gtk_accelerator_valid (guint           keyval,
                       GdkModifierType modifiers)
Tim Janik's avatar
Tim Janik committed
971
{
972
  static const guint invalid_accelerator_vals[] = {
Matthias Clasen's avatar
Matthias Clasen committed
973 974 975 976 977 978
    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,
979 980
    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
981
    GDK_KEY_Scroll_Lock, GDK_KEY_Sys_Req,
982 983 984 985
    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
986 987
    0
  };
988
  static const guint invalid_unmodified_vals[] = {
989 990
    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,
991 992
    0
  };
993
  const guint *ac_val;
Tim Janik's avatar
Tim Janik committed
994 995

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

Tim Janik's avatar
Tim Janik committed
997 998 999 1000 1001 1002 1003
  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
1004
        return FALSE;
Tim Janik's avatar
Tim Janik committed
1005 1006
    }

1007 1008 1009 1010
  if (!modifiers)
    {
      ac_val = invalid_unmodified_vals;
      while (*ac_val)
Matthias Clasen's avatar
Matthias Clasen committed
1011 1012 1013 1014
        {
          if (keyval == *ac_val++)
            return FALSE;
        }
1015
    }
Matthias Clasen's avatar
Matthias Clasen committed
1016

Tim Janik's avatar
Tim Janik committed
1017 1018 1019 1020 1021 1022 1023
  return TRUE;
}

static inline gboolean
is_alt (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1024 1025 1026 1027
          (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
1028 1029 1030 1031 1032 1033
}

static inline gboolean
is_ctl (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1034 1035 1036 1037
          (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
1038 1039 1040 1041 1042 1043
}

static inline gboolean
is_modx (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1044 1045 1046 1047 1048
          (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
1049 1050 1051 1052 1053 1054
}

static inline gboolean
is_ctrl (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1055 1056 1057 1058 1059
          (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
1060 1061 1062 1063 1064 1065
}

static inline gboolean
is_shft (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1066 1067 1068 1069 1070
          (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
1071 1072 1073 1074 1075 1076
}

static inline gboolean
is_shift (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1077 1078 1079 1080 1081 1082
          (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
1083 1084 1085 1086 1087 1088
}

static inline gboolean
is_control (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1089 1090 1091 1092 1093 1094 1095 1096
          (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
1097 1098
}

1099 1100 1101 1102
static inline gboolean
is_release (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1103 1104 1105 1106 1107 1108 1109 1110
          (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] == '>'));
1111 1112
}

1113 1114 1115 1116
static inline gboolean
is_meta (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1117 1118 1119 1120 1121
          (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] == '>'));
1122 1123 1124 1125 1126 1127
}

static inline gboolean
is_super (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1128 1129 1130 1131 1132 1133
          (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] == '>'));
1134 1135 1136 1137 1138 1139
}

static inline gboolean
is_hyper (const gchar *string)
{
  return ((string[0] == '<') &&
Matthias Clasen's avatar
Matthias Clasen committed
1140 1141 1142 1143 1144 1145
          (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] == '>'));
1146 1147
}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
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] == '>'));
}

1162 1163 1164 1165 1166 1167 1168 1169 1170
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]));
}

1171
/**
1172
 * gtk_accelerator_parse_with_keycode:
Matthias Clasen's avatar
Matthias Clasen committed
1173
 * @accelerator: string representing an accelerator
1174 1175
 * @accelerator_key: (out) (allow-none): return location for accelerator
 *     keyval, or %NULL
1176 1177
 * @accelerator_codes: (out) (array zero-terminated=1) (transfer full) (allow-none):
 *     return location for accelerator keycodes, or %NULL
1178 1179
 * @accelerator_mods: (out) (allow-none): return location for accelerator
 *     modifier mask, %NULL
1180
 *
1181 1182 1183 1184
 * 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
1185
 *
1186 1187 1188
 * If @accelerator_codes is given and the result stored in it is non-%NULL,
 * the result must be freed with g_free().
 *
1189 1190
 * If a keycode is present in the accelerator and no @accelerator_codes
 * is given, the parse will fail.
1191
 *
1192 1193 1194 1195
 * If the parse fails, @accelerator_key, @accelerator_mods and
 * @accelerator_codes will be set to 0 (zero).
 *
 * Since: 3.4
1196
 */
Tim Janik's avatar
Tim Janik committed
1197
void
1198 1199 1200 1201
gtk_accelerator_parse_with_keycode (const gchar     *accelerator,
                                    guint           *accelerator_key,
                                    guint          **accelerator_codes,
                                    GdkModifierType *accelerator_mods)
Tim Janik's avatar
Tim Janik committed
1202 1203
{
  guint keyval;
1204
  GdkModifierType mods;
Tim Janik's avatar
Tim Janik committed
1205
  gint len;
1206
  gboolean error;