gtkaccelmap.c 29.8 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14
 * Copyright (C) 1998, 2001 Tim Janik
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * 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/>.
16
 */
17

18
#include "config.h"
19

20
#include "gtkaccelmapprivate.h"
21

22
#include "gtkmarshalers.h"
23
#include "gtkwindowprivate.h"
24
#include "gtkintl.h"
25

26 27
#include <glib/gstdio.h>

28
#include <string.h>
29
#include <errno.h>
30
#include <fcntl.h>
31
#ifdef HAVE_UNISTD_H
32
#include <unistd.h>
33 34 35 36
#endif
#ifdef G_OS_WIN32
#include <io.h>
#endif
37 38


39 40 41 42
/**
 * SECTION:gtkaccelmap
 * @Short_description: Loadable keyboard accelerator specifications
 * @Title: Accelerator Maps
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 * @See_also: #GtkAccelGroup, #GtkAccelKey, #GtkUIManager, gtk_widget_set_accel_path(), gtk_menu_item_set_accel_path(), #GtkSettings:gtk-can-change-accels
 *
 * Accelerator maps are used to define runtime configurable accelerators.
 * Functions for manipulating them are are usually used by higher level
 * convenience mechanisms like #GtkUIManager and are thus considered
 * "low-level". You'll want to use them if you're manually creating menus that
 * should have user-configurable accelerators.
 *
 * Accelerator is uniquely defined by:
 *
 * <itemizedlist>
 *   <listitem><para>accelerator path</para></listitem>
 *   <listitem><para>accelerator key</para></listitem>
 *   <listitem><para>accelerator modifiers</para></listitem>
 * </itemizedlist>
 *
 * The accelerator path must consist of
 * "&lt;WINDOWTYPE&gt;/Category1/Category2/.../Action", where WINDOWTYPE
 * should be a unique application-specific identifier that corresponds to the
 * kind of window the accelerator is being used in, e.g. "Gimp-Image",
 * "Abiword-Document" or "Gnumeric-Settings".
 * The "Category1/.../Action" portion is most appropriately chosen by the action
 * the accelerator triggers, i.e. for accelerators on menu items, choose the
 * item's menu path, e.g. "File/Save As", "Image/View/Zoom" or
 * "Edit/Select All". So a full valid accelerator path may look like:
 * "&lt;Gimp-Toolbox&gt;/File/Dialogs/Tool Options...".
 *
 * All accelerators are stored inside one global #GtkAccelMap that can be
 * obtained using gtk_accel_map_get(). See <link
 * linkend="monitoring-changes">Monitoring changes</link> for additional
 * details.
 *
 * <refsect2 id="manipulating-accelerators">
 * <title>Manipulating accelerators</title>
 * <para>
 * New accelerators can be added using gtk_accel_map_add_entry(). To search for
 * specific accelerator, use gtk_accel_map_lookup_entry(). Modifications of
 * existing accelerators should be done using gtk_accel_map_change_entry().
 *
 * In order to avoid having some accelerators changed, they can be locked using
 * gtk_accel_map_lock_path(). Unlocking is done using
 * gtk_accel_map_unlock_path().
 * </para>
 * </refsect2>
 * <refsect2 id="saving-and-loading">
 * <title>Saving and loading accelerator maps</title>
 * <para>
 * Accelerator maps can be saved to and loaded from some external resource. For
 * simple saving and loading from file, gtk_accel_map_save() and
 * gtk_accel_map_load() are provided. Saving and loading can also be done by
 * providing file descriptor to gtk_accel_map_save_fd() and
 * gtk_accel_map_load_fd().
 * </para>
 * </refsect2>
 * <refsect2 id="monitoring-changes">
 * <title>Monitoring changes</title>
 * <para>
 * #GtkAccelMap object is only useful for monitoring changes of accelerators. By
 * connecting to #GtkAccelMap::changed signal, one can monitor changes of all
 * accelerators. It is also possible to monitor only single accelerator path by
 * using it as a detail of the #GtkAccelMap::changed signal.
 * </para>
 * </refsect2>
106 107 108
 */


109
/* --- structures --- */
110 111 112 113 114 115 116 117 118 119
struct _GtkAccelMap
{
  GObject parent_instance;
};

struct _GtkAccelMapClass
{
  GObjectClass parent_class;
};

120 121 122 123 124 125
typedef struct {
  const gchar *accel_path;
  guint        accel_key;
  guint        accel_mods;
  guint	       std_accel_key;
  guint	       std_accel_mods;
126 127
  guint        changed    :  1;
  guint        lock_count : 15;
128
  GSList      *groups;
129 130
} AccelEntry;

131 132 133 134 135
/* --- signals --- */
enum {
  CHANGED,
  LAST_SIGNAL
};
136 137

/* --- variables --- */
138 139 140 141 142 143 144 145

static GHashTable  *accel_entry_ht = NULL;	/* accel_path -> AccelEntry */
static GSList      *accel_filters = NULL;
static gulong	    accel_map_signals[LAST_SIGNAL] = { 0, };
static GtkAccelMap *accel_map;

/* --- prototypes --- */
static void do_accel_map_changed (AccelEntry *entry);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

/* --- functions --- */
static guint
accel_entry_hash (gconstpointer key)
{
  const AccelEntry *entry = key;

  return g_str_hash (entry->accel_path);
}

static gboolean
accel_entry_equal (gconstpointer key1,
		   gconstpointer key2)
{
  const AccelEntry *entry1 = key1;
  const AccelEntry *entry2 = key2;

  return g_str_equal (entry1->accel_path, entry2->accel_path);
}

static inline AccelEntry*
accel_path_lookup (const gchar *accel_path)
{
  AccelEntry ekey;

  ekey.accel_path = accel_path;

  /* safety NULL check for return_if_fail()s */
  return accel_path ? g_hash_table_lookup (accel_entry_ht, &ekey) : NULL;
}

void
_gtk_accel_map_init (void)
{
180 181
  if (accel_entry_ht == NULL)
    accel_entry_ht = g_hash_table_new (accel_entry_hash, accel_entry_equal);
182 183
}

184 185
gboolean
_gtk_accel_path_is_valid (const gchar *accel_path)
186 187 188 189 190 191 192
{
  gchar *p;

  if (!accel_path || accel_path[0] != '<' ||
      accel_path[1] == '<' || accel_path[1] == '>' || !accel_path[1])
    return FALSE;
  p = strchr (accel_path, '>');
193
  if (!p || (p[1] != 0 && p[1] != '/'))
194 195 196 197 198
    return FALSE;
  return TRUE;
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
199
 * gtk_accel_map_add_entry:
200 201 202 203
 * @accel_path: valid accelerator path
 * @accel_key:  the accelerator key
 * @accel_mods: the accelerator modifiers
 *
Matthias Clasen's avatar
Matthias Clasen committed
204
 * Registers a new accelerator with the global accelerator map.
205 206 207 208
 * This function should only be called once per @accel_path
 * with the canonical @accel_key and @accel_mods for this path.
 * To change the accelerator during runtime programatically, use
 * gtk_accel_map_change_entry().
209 210 211 212
 * 
 * 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().
213
 */
214
void
215 216 217
gtk_accel_map_add_entry (const gchar    *accel_path,
			 guint           accel_key,
			 GdkModifierType accel_mods)
218 219 220
{
  AccelEntry *entry;

221
  g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
222

223 224 225 226
  if (!accel_key)
    accel_mods = 0;
  else
    accel_mods &= gtk_accelerator_get_default_mod_mask ();
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

  entry = accel_path_lookup (accel_path);
  if (entry)
    {
      if (!entry->std_accel_key && !entry->std_accel_mods &&
	  (accel_key || accel_mods))
	{
	  entry->std_accel_key = accel_key;
	  entry->std_accel_mods = accel_mods;
	  if (!entry->changed)
	    gtk_accel_map_change_entry (entry->accel_path, accel_key, accel_mods, TRUE);
	}
    }
  else
    {
242 243
      entry = g_slice_new0 (AccelEntry);
      entry->accel_path = g_intern_string (accel_path);
244 245 246 247 248
      entry->std_accel_key = accel_key;
      entry->std_accel_mods = accel_mods;
      entry->accel_key = accel_key;
      entry->accel_mods = accel_mods;
      entry->changed = FALSE;
249
      entry->lock_count = 0;
250
      g_hash_table_insert (accel_entry_ht, entry, entry);
251 252

      do_accel_map_changed (entry);
253 254 255 256
    }
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
257
 * gtk_accel_map_lookup_entry:
258 259
 * @accel_path: a valid accelerator path
 * @key: (allow-none) (out): the accelerator key to be filled in (optional)
260
 *
261
 * Looks up the accelerator entry for @accel_path and fills in @key.
Matthias Clasen's avatar
Matthias Clasen committed
262 263
 *
 * Returns: %TRUE if @accel_path is known, %FALSE otherwise
264
 */
265
gboolean
266 267 268 269 270
gtk_accel_map_lookup_entry (const gchar *accel_path,
			    GtkAccelKey *key)
{
  AccelEntry *entry;

271
  g_return_val_if_fail (_gtk_accel_path_is_valid (accel_path), FALSE);
272 273 274 275 276 277

  entry = accel_path_lookup (accel_path);
  if (entry && key)
    {
      key->accel_key = entry->accel_key;
      key->accel_mods = entry->accel_mods;
278
      key->accel_flags = 0;
279 280
    }

281
  return entry ? TRUE : FALSE;
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

static void
hash2slist_foreach (gpointer  key,
		    gpointer  value,
		    gpointer  user_data)
{
  GSList **slist_p = user_data;

  *slist_p = g_slist_prepend (*slist_p, value);
}

static GSList*
g_hash_table_slist_values (GHashTable *hash_table)
{
  GSList *slist = NULL;

  g_return_val_if_fail (hash_table != NULL, NULL);

  g_hash_table_foreach (hash_table, hash2slist_foreach, &slist);

  return slist;
}

306 307 308 309
/* if simulate==TRUE, return whether accel_path can be changed to
 * accel_key && accel_mods. otherwise, return whether accel_path
 * was actually changed.
 */
310 311 312 313 314 315 316 317
static gboolean
internal_change_entry (const gchar    *accel_path,
		       guint           accel_key,
		       GdkModifierType accel_mods,
		       gboolean        replace,
		       gboolean	       simulate)
{
  GSList *node, *slist, *win_list, *group_list, *replace_list = NULL;
318
  GHashTable *group_hm, *window_hm;
319
  gboolean change_accel, removable, can_change = TRUE, seen_accel = FALSE;
320 321 322 323 324 325 326 327 328 329 330 331 332
  GQuark entry_quark;
  AccelEntry *entry = accel_path_lookup (accel_path);

  /* not much todo if there's no entry yet */
  if (!entry)
    {
      if (!simulate)
	{
	  gtk_accel_map_add_entry (accel_path, 0, 0);
	  entry = accel_path_lookup (accel_path);
	  entry->accel_key = accel_key;
	  entry->accel_mods = accel_mods;
	  entry->changed = TRUE;
333 334

	  do_accel_map_changed (entry);
335 336 337 338 339 340
	}
      return TRUE;
    }

  /* if there's nothing to change, not much todo either */
  if (entry->accel_key == accel_key && entry->accel_mods == accel_mods)
341 342 343 344 345
    {
      if (!simulate)
	entry->changed = TRUE;
      return simulate ? TRUE : FALSE;
    }
346 347 348 349 350

  /* The no-change case has already been handled, so 
   * simulate doesn't make a difference here.
   */
  if (entry->lock_count > 0)
351
    return FALSE;
352 353

  /* nobody's interested, easy going */
354
  if (!entry->groups)
355 356 357 358 359 360
    {
      if (!simulate)
	{
	  entry->accel_key = accel_key;
	  entry->accel_mods = accel_mods;
	  entry->changed = TRUE;
361 362

	  do_accel_map_changed (entry);
363 364 365 366 367 368 369
	}
      return TRUE;
    }

  /* 1) fetch all accel groups affected by this entry */
  entry_quark = g_quark_try_string (entry->accel_path);
  group_hm = g_hash_table_new (NULL, NULL);
370
  window_hm = g_hash_table_new (NULL, NULL);
371 372
  for (slist = entry->groups; slist; slist = slist->next)
    g_hash_table_insert (group_hm, slist->data, slist->data);
373 374 375 376 377 378 379

  /* 2) collect acceleratables affected */
  group_list = g_hash_table_slist_values (group_hm);
  for (slist = group_list; slist; slist = slist->next)
    {
      GtkAccelGroup *group = slist->data;

380
      for (node = _gtk_accel_group_get_accelerables (group); node; node = node->next)
381
	g_hash_table_insert (window_hm, node->data, node->data);
382 383 384 385
    }
  g_slist_free (group_list);

  /* 3) include all accel groups used by acceleratables */
386 387
  win_list = g_hash_table_slist_values (window_hm);
  g_hash_table_destroy (window_hm);
388
  for (slist = win_list; slist; slist = slist->next)
389
    for (node = gtk_accel_groups_from_object (slist->data); node; node = node->next)
390 391 392
      g_hash_table_insert (group_hm, node->data, node->data);
  group_list = g_hash_table_slist_values (group_hm);
  g_hash_table_destroy (group_hm);
393
  
394
  /* 4) walk the acceleratables and figure whether they occupy accel_key&accel_mods */
395 396 397 398 399 400 401 402
  if (accel_key)
    for (slist = win_list; slist; slist = slist->next)
      if (GTK_IS_WINDOW (slist->data))	/* bad kludge in lack of a GtkAcceleratable */
	if (_gtk_window_query_nonaccels (slist->data, accel_key, accel_mods))
	  {
	    seen_accel = TRUE;
	    break;
	  }
403
  removable = !seen_accel;
404
  
405
  /* 5) walk all accel groups and search for locks */
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
  if (removable)
    for (slist = group_list; slist; slist = slist->next)
      {
	GtkAccelGroup *group = slist->data;
	GtkAccelGroupEntry *ag_entry;
	guint i, n;
	
	n = 0;
	ag_entry = entry->accel_key ? gtk_accel_group_query (group, entry->accel_key, entry->accel_mods, &n) : NULL;
	for (i = 0; i < n; i++)
	  if (ag_entry[i].accel_path_quark == entry_quark)
	    {
	      can_change = !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
	      if (!can_change)
		goto break_loop_step5;
	    }
	
	n = 0;
	ag_entry = accel_key ? gtk_accel_group_query (group, accel_key, accel_mods, &n) : NULL;
	for (i = 0; i < n; i++)
426
	  {
427
	    seen_accel = TRUE;
428
	    removable = !gtk_accel_group_get_is_locked (group) && !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
429
	    if (!removable)
430
	      goto break_loop_step5;
431
	    if (ag_entry[i].accel_path_quark)
432
	      replace_list = g_slist_prepend (replace_list, GUINT_TO_POINTER (ag_entry[i].accel_path_quark));
433
	  }
434
      }
435 436 437
 break_loop_step5:
  
  /* 6) check whether we can remove existing accelerators */
438 439 440 441 442 443 444 445
  if (removable && can_change)
    for (slist = replace_list; slist; slist = slist->next)
      if (!internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, TRUE))
	{
	  removable = FALSE;
	  break;
	}
  
446 447
  /* 7) check conditions and proceed if possible */
  change_accel = can_change && (!seen_accel || (removable && replace));
448
  
449 450
  if (change_accel && !simulate)
    {
451 452 453
      /* ref accel groups */
      for (slist = group_list; slist; slist = slist->next)
	g_object_ref (slist->data);
454 455 456 457 458 459 460 461 462

      /* 8) remove existing accelerators */
      for (slist = replace_list; slist; slist = slist->next)
	internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, FALSE);

      /* 9) install new accelerator */
      entry->accel_key = accel_key;
      entry->accel_mods = accel_mods;
      entry->changed = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
463

464 465 466 467 468 469
      for (slist = group_list; slist; slist = slist->next)
	_gtk_accel_group_reconnect (slist->data, g_quark_from_string (entry->accel_path));

      /* unref accel groups */
      for (slist = group_list; slist; slist = slist->next)
	g_object_unref (slist->data);
470 471

      do_accel_map_changed (entry);
472 473 474 475 476 477 478 479 480
    }
  g_slist_free (replace_list);
  g_slist_free (group_list);
  g_slist_free (win_list);

  return change_accel;
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
481 482 483 484
 * gtk_accel_map_change_entry:
 * @accel_path:  a valid accelerator path
 * @accel_key:   the new accelerator key
 * @accel_mods:  the new accelerator modifiers
485 486
 * @replace:     %TRUE if other accelerators may be deleted upon conflicts
 *
Matthias Clasen's avatar
Matthias Clasen committed
487
 * Changes the @accel_key and @accel_mods currently associated with @accel_path.
Matthias Clasen's avatar
Matthias Clasen committed
488
 * Due to conflicts with other accelerators, a change may not always be possible,
489
 * @replace indicates whether other accelerators may be deleted to resolve such
490
 * conflicts. A change will only occur if all conflicts could be resolved (which
Matthias Clasen's avatar
Matthias Clasen committed
491
 * might not be the case if conflicting accelerators are locked). Successful
492
 * changes are indicated by a %TRUE return value.
Matthias Clasen's avatar
Matthias Clasen committed
493
 *
494
 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
Matthias Clasen's avatar
Matthias Clasen committed
495
 * pass a static string, you can save some memory by interning it first with
496
 * g_intern_static_string().
Matthias Clasen's avatar
Matthias Clasen committed
497 498
 *
 * Returns: %TRUE if the accelerator could be changed, %FALSE otherwise
499 500 501 502 503 504 505
 */
gboolean
gtk_accel_map_change_entry (const gchar    *accel_path,
			    guint           accel_key,
			    GdkModifierType accel_mods,
			    gboolean        replace)
{
506
  g_return_val_if_fail (_gtk_accel_path_is_valid (accel_path), FALSE);
507 508 509 510 511 512 513

  return internal_change_entry (accel_path, accel_key, accel_key ? accel_mods : 0, replace, FALSE);
}

static guint
accel_map_parse_accel_path (GScanner *scanner)
{
514 515
  guint accel_key = 0;
  GdkModifierType accel_mods = 0;
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
  gchar *path, *accel;
  
  /* parse accel path */
  g_scanner_get_next_token (scanner);
  if (scanner->token != G_TOKEN_STRING)
    return G_TOKEN_STRING;

  /* test if the next token is an accelerator */
  g_scanner_peek_next_token (scanner);
  if (scanner->next_token != G_TOKEN_STRING)
    {
      /* if not so, eat that token and error out */
      g_scanner_get_next_token (scanner);
      return G_TOKEN_STRING;
    }

  /* get the full accelerator specification */
  path = g_strdup (scanner->value.v_string);
  g_scanner_get_next_token (scanner);
  accel = g_strdup (scanner->value.v_string);

  /* ensure the entry is present */
  gtk_accel_map_add_entry (path, 0, 0);

  /* and propagate it */
  gtk_accelerator_parse (accel, &accel_key, &accel_mods);
  gtk_accel_map_change_entry (path, accel_key, accel_mods, TRUE);

  g_free (accel);
  g_free (path);

  /* check correct statement end */
  g_scanner_get_next_token (scanner);
  if (scanner->token != ')')
    return ')';
  else
    return G_TOKEN_NONE;
}

static void
accel_map_parse_statement (GScanner *scanner)
{
  guint expected_token;

  g_scanner_get_next_token (scanner);

  if (scanner->token == G_TOKEN_SYMBOL)
    {
      guint (*parser_func) (GScanner*);

566
      parser_func = (guint (*) (GScanner *))scanner->value.v_symbol;
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

      expected_token = parser_func (scanner);
    }
  else
    expected_token = G_TOKEN_SYMBOL;

  /* skip rest of statement on errrors
   */
  if (expected_token != G_TOKEN_NONE)
    {
      register guint level;

      level = 1;
      if (scanner->token == ')')
	level--;
      if (scanner->token == '(')
	level++;

      while (!g_scanner_eof (scanner) && level > 0)
	{
	  g_scanner_get_next_token (scanner);

	  if (scanner->token == '(')
	    level++;
	  else if (scanner->token == ')')
	    level--;
	}
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
597 598 599 600 601 602
/**
 * gtk_accel_map_load_scanner:
 * @scanner: a #GScanner which has already been provided with an input file
 *
 * #GScanner variant of gtk_accel_map_load().
 */
603 604 605 606 607 608 609 610
void
gtk_accel_map_load_scanner (GScanner *scanner)
{
  gboolean skip_comment_single;
  gboolean symbol_2_token;
  gchar *cpair_comment_single;
  gpointer saved_symbol;
  
611
  g_return_if_fail (scanner != NULL);
612 613 614 615 616 617 618 619 620

  /* configure scanner */
  skip_comment_single = scanner->config->skip_comment_single;
  scanner->config->skip_comment_single = TRUE;
  cpair_comment_single = scanner->config->cpair_comment_single;
  scanner->config->cpair_comment_single = ";\n";
  symbol_2_token = scanner->config->symbol_2_token;
  scanner->config->symbol_2_token = FALSE;
  saved_symbol = g_scanner_lookup_symbol (scanner, "gtk_accel_path");
621
  g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", 
Manish Singh's avatar
Manish Singh committed
622
			      accel_map_parse_accel_path);
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

  /* outer parsing loop
   */
  g_scanner_peek_next_token (scanner);
  while (scanner->next_token == '(')
    {
      g_scanner_get_next_token (scanner);

      accel_map_parse_statement (scanner);

      g_scanner_peek_next_token (scanner);
    }

  /* restore config */
  scanner->config->skip_comment_single = skip_comment_single;
  scanner->config->cpair_comment_single = cpair_comment_single;
  scanner->config->symbol_2_token = symbol_2_token;
  g_scanner_scope_remove_symbol (scanner, 0, "gtk_accel_path");
  if (saved_symbol)
    g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", saved_symbol);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
646 647
 * gtk_accel_map_load_fd:
 * @fd: a valid readable file descriptor
648 649
 *
 * Filedescriptor variant of gtk_accel_map_load().
Matthias Clasen's avatar
Matthias Clasen committed
650
 *
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
 * Note that the file descriptor will not be closed by this function.
 */
void
gtk_accel_map_load_fd (gint fd)
{
  GScanner *scanner;

  g_return_if_fail (fd >= 0);

  /* create and setup scanner */
  scanner = g_scanner_new (NULL);
  g_scanner_input_file (scanner, fd);

  gtk_accel_map_load_scanner (scanner);

  g_scanner_destroy (scanner);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
670
 * gtk_accel_map_load:
671
 * @file_name: (type filename): a file containing accelerator specifications,
672
 *   in the GLib file name encoding
673 674 675 676 677 678 679 680 681 682 683 684 685 686
 *
 * Parses a file previously saved with gtk_accel_map_save() for
 * accelerator specifications, and propagates them accordingly.
 */
void
gtk_accel_map_load (const gchar *file_name)
{
  gint fd;

  g_return_if_fail (file_name != NULL);

  if (!g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
    return;

687
  fd = g_open (file_name, O_RDONLY, 0);
688 689 690 691 692 693 694 695
  if (fd < 0)
    return;

  gtk_accel_map_load_fd (fd);

  close (fd);
}

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
static gboolean
write_all (gint   fd,
	   gchar *buf,
	   gsize  to_write)
{
  while (to_write > 0)
    {
      gssize count = write (fd, buf, to_write);
      if (count < 0)
	{
	  if (errno != EINTR)
	    return FALSE;
	}
      else
	{
	  to_write -= count;
	  buf += count;
	}
    }

  return TRUE;
}

719 720 721 722
static void
accel_map_print (gpointer        data,
		 const gchar    *accel_path,
		 guint           accel_key,
723
		 GdkModifierType accel_mods,
724 725 726
		 gboolean        changed)
{
  GString *gstring = g_string_new (changed ? NULL : "; ");
727
  gint fd = GPOINTER_TO_INT (data);
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
  gchar *tmp, *name;

  g_string_append (gstring, "(gtk_accel_path \"");

  tmp = g_strescape (accel_path, NULL);
  g_string_append (gstring, tmp);
  g_free (tmp);

  g_string_append (gstring, "\" \"");

  name = gtk_accelerator_name (accel_key, accel_mods);
  tmp = g_strescape (name, NULL);
  g_free (name);
  g_string_append (gstring, tmp);
  g_free (tmp);

  g_string_append (gstring, "\")\n");

746
  write_all (fd, gstring->str, gstring->len);
747 748 749 750 751

  g_string_free (gstring, TRUE);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
752 753
 * gtk_accel_map_save_fd:
 * @fd: a valid writable file descriptor
754 755
 *
 * Filedescriptor variant of gtk_accel_map_save().
Matthias Clasen's avatar
Matthias Clasen committed
756
 *
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
 * Note that the file descriptor will not be closed by this function.
 */
void
gtk_accel_map_save_fd (gint fd)
{
  GString *gstring;

  g_return_if_fail (fd >= 0);

  gstring = g_string_new ("; ");
  if (g_get_prgname ())
    g_string_append (gstring, g_get_prgname ());
  g_string_append (gstring, " GtkAccelMap rc-file         -*- scheme -*-\n");
  g_string_append (gstring, "; this file is an automated accelerator map dump\n");
  g_string_append (gstring, ";\n");

773 774 775
  write_all (fd, gstring->str, gstring->len);
  
  g_string_free (gstring, TRUE);
776 777 778 779 780

  gtk_accel_map_foreach (GINT_TO_POINTER (fd), accel_map_print);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
781
 * gtk_accel_map_save:
782 783
 * @file_name: (type filename): the name of the file to contain
 *   accelerator specifications, in the GLib file name encoding
784 785 786 787 788 789 790 791 792 793 794 795 796
 *
 * Saves current accelerator specifications (accelerator path, key
 * and modifiers) to @file_name.
 * The file is written in a format suitable to be read back in by
 * gtk_accel_map_load().
 */
void
gtk_accel_map_save (const gchar *file_name)
{
  gint fd;

  g_return_if_fail (file_name != NULL);

797
  fd = g_open (file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644);
798 799 800 801 802 803 804 805 806
  if (fd < 0)
    return;

  gtk_accel_map_save_fd (fd);

  close (fd);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
807
 * gtk_accel_map_foreach:
808
 * @data: (allow-none): data to be passed into @foreach_func
809 810
 * @foreach_func: (scope call): function to be executed for each accel
 *                map entry which is not filtered out
811
 *
Matthias Clasen's avatar
Matthias Clasen committed
812 813 814 815
 * Loops over the entries in the accelerator map whose accel path 
 * doesn't match any of the filters added with gtk_accel_map_add_filter(), 
 * and execute @foreach_func on each. The signature of @foreach_func is 
 * that of #GtkAccelMapForeach, the @changed parameter indicates whether
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
 * this accelerator was changed during runtime (thus, would need
 * saving during an accelerator map dump).
 */
void
gtk_accel_map_foreach (gpointer           data,
		       GtkAccelMapForeach foreach_func)
{
  GSList *entries, *slist, *node;

  g_return_if_fail (foreach_func != NULL);

  entries = g_hash_table_slist_values (accel_entry_ht);
  for (slist = entries; slist; slist = slist->next)
    {
      AccelEntry *entry = slist->data;
      gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;

      for (node = accel_filters; node; node = node->next)
	if (g_pattern_match_string (node->data, entry->accel_path))
	  goto skip_accel;
      foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
    skip_accel:
838
      /* noop */;
839 840 841 842
    }
  g_slist_free (entries);
}

Matthias Clasen's avatar
Matthias Clasen committed
843 844 845
/**
 * gtk_accel_map_foreach_unfiltered:
 * @data:         data to be passed into @foreach_func
846 847
 * @foreach_func: (scope call): function to be executed for each accel
 *                map entry
Matthias Clasen's avatar
Matthias Clasen committed
848 849 850 851 852 853 854
 *
 * Loops over all entries in the accelerator map, and execute
 * @foreach_func on each. The signature of @foreach_func is that of
 * #GtkAccelMapForeach, the @changed parameter indicates whether
 * this accelerator was changed during runtime (thus, would need
 * saving during an accelerator map dump).
 */
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
void
gtk_accel_map_foreach_unfiltered (gpointer           data,
				  GtkAccelMapForeach foreach_func)
{
  GSList *entries, *slist;

  g_return_if_fail (foreach_func != NULL);

  entries = g_hash_table_slist_values (accel_entry_ht);
  for (slist = entries; slist; slist = slist->next)
    {
      AccelEntry *entry = slist->data;
      gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;

      foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
    }
  g_slist_free (entries);
}

Matthias Clasen's avatar
Matthias Clasen committed
874 875 876 877 878 879 880 881
/**
 * gtk_accel_map_add_filter:
 * @filter_pattern: a pattern (see #GPatternSpec)
 *
 * Adds a filter to the global list of accel path filters.
 *
 * Accel map entries whose accel path matches one of the filters
 * are skipped by gtk_accel_map_foreach().
882 883 884 885
 *
 * This function is intended for GTK+ modules that create their own
 * menus, but don't want them to be saved into the applications accelerator
 * map dump.
Matthias Clasen's avatar
Matthias Clasen committed
886
 */
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
void
gtk_accel_map_add_filter (const gchar *filter_pattern)
{
  GPatternSpec *pspec;
  GSList *slist;

  g_return_if_fail (filter_pattern != NULL);

  pspec = g_pattern_spec_new (filter_pattern);
  for (slist = accel_filters; slist; slist = slist->next)
    if (g_pattern_spec_equal (pspec, slist->data))
      {
	g_pattern_spec_free (pspec);
	return;
      }
  accel_filters = g_slist_prepend (accel_filters, pspec);
}
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

void
_gtk_accel_map_add_group (const gchar   *accel_path,
			  GtkAccelGroup *accel_group)
{
  AccelEntry *entry;

  g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));

  entry = accel_path_lookup (accel_path);
  if (!entry)
    {
      gtk_accel_map_add_entry (accel_path, 0, 0);
      entry = accel_path_lookup (accel_path);
    }
  entry->groups = g_slist_prepend (entry->groups, accel_group);
}

void
_gtk_accel_map_remove_group (const gchar   *accel_path,
			     GtkAccelGroup *accel_group)
{
  AccelEntry *entry;

  entry = accel_path_lookup (accel_path);
  g_return_if_fail (entry != NULL);
  g_return_if_fail (g_slist_find (entry->groups, accel_group));

  entry->groups = g_slist_remove (entry->groups, accel_group);
}
935 936 937 938 939 940


/**
 * gtk_accel_map_lock_path:
 * @accel_path: a valid accelerator path
 * 
941 942
 * Locks the given accelerator path. If the accelerator map doesn't yet contain
 * an entry for @accel_path, a new one is created.
943
 *
Matthias Clasen's avatar
Matthias Clasen committed
944
 * Locking an accelerator path prevents its accelerator from being changed 
945 946
 * during runtime. A locked accelerator path can be unlocked by 
 * gtk_accel_map_unlock_path(). Refer to gtk_accel_map_change_entry() 
Matthias Clasen's avatar
Matthias Clasen committed
947
 * for information about runtime accelerator changes.
948
 *
949 950 951 952
 * If called more than once, @accel_path remains locked until
 * gtk_accel_map_unlock_path() has been called an equivalent number
 * of times.
 *
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
 * Note that locking of individual accelerator paths is independent from 
 * locking the #GtkAccelGroup containing them. For runtime accelerator
 * changes to be possible both the accelerator path and its #GtkAccelGroup
 * have to be unlocked. 
 *
 * Since: 2.4
 **/
void 
gtk_accel_map_lock_path (const gchar *accel_path)
{
  AccelEntry *entry;

  g_return_if_fail (_gtk_accel_path_is_valid (accel_path));

  entry = accel_path_lookup (accel_path);
  
969 970 971 972 973 974 975
  if (!entry)
    {
      gtk_accel_map_add_entry (accel_path, 0, 0);
      entry = accel_path_lookup (accel_path);
    }

  entry->lock_count += 1;
976 977 978 979 980 981
}

/**
 * gtk_accel_map_unlock_path:
 * @accel_path: a valid accelerator path
 * 
982 983
 * Undoes the last call to gtk_accel_map_lock_path() on this @accel_path.
 * Refer to gtk_accel_map_lock_path() for information about accelerator path locking.
984 985 986 987 988 989 990 991 992 993 994
 *
 * Since: 2.4
 **/
void 
gtk_accel_map_unlock_path (const gchar *accel_path)
{
  AccelEntry *entry;

  g_return_if_fail (_gtk_accel_path_is_valid (accel_path));

  entry = accel_path_lookup (accel_path);
Matthias Clasen's avatar
Matthias Clasen committed
995

996
  g_return_if_fail (entry != NULL && entry->lock_count > 0);
Matthias Clasen's avatar
Matthias Clasen committed
997

998
  entry->lock_count -= 1;  
Matthias Clasen's avatar
Matthias Clasen committed
999 1000
}

Matthias Clasen's avatar
Matthias Clasen committed
1001
G_DEFINE_TYPE (GtkAccelMap, gtk_accel_map, G_TYPE_OBJECT)
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

static void
gtk_accel_map_class_init (GtkAccelMapClass *accel_map_class)
{
  /**
   * GtkAccelMap::changed:
   * @object: the global accel map object
   * @accel_path: the path of the accelerator that changed
   * @accel_key: the key value for the new accelerator
   * @accel_mods: the modifier mask for the new accelerator
   *
   * Notifies of a change in the global accelerator map.
   * The path is also used as the detail for the signal,
   * so it is possible to connect to
   * changed::<replaceable>accel_path</replaceable>.
   *
   * Since: 2.4
   */
1020
  accel_map_signals[CHANGED] = g_signal_new (I_("changed"),
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
					     G_TYPE_FROM_CLASS (accel_map_class),
					     G_SIGNAL_DETAILED|G_SIGNAL_RUN_LAST,
					     0,
					     NULL, NULL,
					     _gtk_marshal_VOID__STRING_UINT_FLAGS,
					     G_TYPE_NONE, 3,
					     G_TYPE_STRING, G_TYPE_UINT, GDK_TYPE_MODIFIER_TYPE);
}

static void
gtk_accel_map_init (GtkAccelMap *accel_map)
{
}

/**
 * gtk_accel_map_get:
 * 
 * Gets the singleton global #GtkAccelMap object. This object
 * is useful only for notification of changes to the accelerator
 * map via the ::changed signal; it isn't a parameter to the
 * other accelerator map functions.
 * 
1043
 * Return value: (transfer none): the global #GtkAccelMap object
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
 *
 * Since: 2.4
 **/
GtkAccelMap *
gtk_accel_map_get (void)
{
  if (!accel_map)
    accel_map = g_object_new (GTK_TYPE_ACCEL_MAP, NULL);

  return accel_map;
}

static void
do_accel_map_changed (AccelEntry *entry)
{
  if (accel_map)
    g_signal_emit (accel_map,
		   accel_map_signals[CHANGED],
		   g_quark_from_string (entry->accel_path),
		   entry->accel_path,
		   entry->accel_key,
		   entry->accel_mods);
}
1067 1068 1069 1070 1071 1072 1073

gchar *
_gtk_accel_path_for_action (const gchar *action_name,
                            GVariant    *parameter)
{
  GString *s;

1074
  s = g_string_new ("<GAction>/");
1075 1076 1077 1078 1079 1080 1081 1082 1083
  g_string_append (s, action_name);
  if (parameter)
    {
      g_string_append_c (s, '/');
      g_variant_print_string (parameter, s, FALSE);
    }
  return g_string_free (s, FALSE);
}