1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1998, 2001 Tim Janik
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkaccelmap.h"
24 #include "gtkwindow.h" /* in lack of GtkAcceleratable */
37 /* --- structures --- */
39 const gchar *accel_path;
49 /* --- variables --- */
50 static GHashTable *accel_entry_ht = NULL; /* accel_path -> AccelEntry */
51 static GSList *accel_filters = NULL;
54 /* --- functions --- */
56 accel_entry_hash (gconstpointer key)
58 const AccelEntry *entry = key;
60 return g_str_hash (entry->accel_path);
64 accel_entry_equal (gconstpointer key1,
67 const AccelEntry *entry1 = key1;
68 const AccelEntry *entry2 = key2;
70 return g_str_equal (entry1->accel_path, entry2->accel_path);
73 static inline AccelEntry*
74 accel_path_lookup (const gchar *accel_path)
78 ekey.accel_path = accel_path;
80 /* safety NULL check for return_if_fail()s */
81 return accel_path ? g_hash_table_lookup (accel_entry_ht, &ekey) : NULL;
85 _gtk_accel_map_init (void)
87 g_assert (accel_entry_ht == NULL);
89 accel_entry_ht = g_hash_table_new (accel_entry_hash, accel_entry_equal);
93 _gtk_accel_path_is_valid (const gchar *accel_path)
97 if (!accel_path || accel_path[0] != '<' ||
98 accel_path[1] == '<' || accel_path[1] == '>' || !accel_path[1])
100 p = strchr (accel_path, '>');
101 if (!p || p[1] != '/')
107 * gtk_accel_map_add_entry
108 * @accel_path: valid accelerator path
109 * @accel_key: the accelerator key
110 * @accel_mods: the accelerator modifiers
112 * Register a new accelerator with the global accelerator map.
113 * This function should only be called once per @accel_path
114 * with the canonical @accel_key and @accel_mods for this path.
115 * To change the accelerator during runtime programatically, use
116 * gtk_accel_map_change_entry().
117 * The accelerator path must consist of "<WINDOWTYPE>/Category1/Category2/.../Action",
118 * where WINDOWTYPE should be a unique application specifc identifier, that
119 * corresponds to the kind of window the accelerator is being used in, e.g. "Gimp-Image",
120 * "Abiword-Document" or "Gnumeric-Settings".
121 * The Category1/.../Action portion is most apropriately choosen by the action the
122 * accelerator triggers, i.e. for accelerators on menu items, choose the item's menu path,
123 * e.g. "File/Save As", "Image/View/Zoom" or "Edit/Select All".
124 * So a full valid accelerator path may look like:
125 * "<Gimp-Toolbox>/File/Dialogs/Tool Options...".
128 gtk_accel_map_add_entry (const gchar *accel_path,
134 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
139 accel_mods &= gtk_accelerator_get_default_mod_mask ();
141 entry = accel_path_lookup (accel_path);
144 if (!entry->std_accel_key && !entry->std_accel_mods &&
145 (accel_key || accel_mods))
147 entry->std_accel_key = accel_key;
148 entry->std_accel_mods = accel_mods;
150 gtk_accel_map_change_entry (entry->accel_path, accel_key, accel_mods, TRUE);
155 entry = g_new0 (AccelEntry, 1);
156 entry->accel_path = g_quark_to_string (g_quark_from_string (accel_path));
157 entry->std_accel_key = accel_key;
158 entry->std_accel_mods = accel_mods;
159 entry->accel_key = accel_key;
160 entry->accel_mods = accel_mods;
161 entry->changed = FALSE;
162 g_hash_table_insert (accel_entry_ht, entry, entry);
167 * gtk_accel_map_lookup_entry
168 * @accel_path: valid accelerator path
169 * @key: accelerator key to be filled in (optional)
170 * @returns: %TRUE if @accel_path is known, %FALSE otherwise
172 * Looks up the accelerator entry for @accel_path and fills in @key.
175 gtk_accel_map_lookup_entry (const gchar *accel_path,
180 g_return_val_if_fail (_gtk_accel_path_is_valid (accel_path), FALSE);
182 entry = accel_path_lookup (accel_path);
185 key->accel_key = entry->accel_key;
186 key->accel_mods = entry->accel_mods;
187 key->accel_flags = 0;
190 return entry ? TRUE : FALSE;
194 hash2slist_foreach (gpointer key,
198 GSList **slist_p = user_data;
200 *slist_p = g_slist_prepend (*slist_p, value);
204 g_hash_table_slist_values (GHashTable *hash_table)
206 GSList *slist = NULL;
208 g_return_val_if_fail (hash_table != NULL, NULL);
210 g_hash_table_foreach (hash_table, hash2slist_foreach, &slist);
216 internal_change_entry (const gchar *accel_path,
218 GdkModifierType accel_mods,
222 GSList *node, *slist, *win_list, *group_list, *replace_list = NULL;
223 GHashTable *group_hm, *win_hm;
224 gboolean change_accel, removable, can_change = TRUE, seen_accel = FALSE;
226 AccelEntry *entry = accel_path_lookup (accel_path);
228 /* not much todo if there's no entry yet */
233 gtk_accel_map_add_entry (accel_path, 0, 0);
234 entry = accel_path_lookup (accel_path);
235 entry->accel_key = accel_key;
236 entry->accel_mods = accel_mods;
237 entry->changed = TRUE;
242 /* if there's nothing to change, not much todo either */
243 if (entry->accel_key == accel_key && entry->accel_mods == accel_mods)
246 /* nobody's interested, easy going */
251 entry->accel_key = accel_key;
252 entry->accel_mods = accel_mods;
253 entry->changed = TRUE;
258 /* 1) fetch all accel groups affected by this entry */
259 entry_quark = g_quark_try_string (entry->accel_path);
260 group_hm = g_hash_table_new (NULL, NULL);
261 win_hm = g_hash_table_new (NULL, NULL);
262 for (slist = entry->groups; slist; slist = slist->next)
263 g_hash_table_insert (group_hm, slist->data, slist->data);
265 /* 2) collect acceleratables affected */
266 group_list = g_hash_table_slist_values (group_hm);
267 for (slist = group_list; slist; slist = slist->next)
269 GtkAccelGroup *group = slist->data;
271 for (node = group->acceleratables; node; node = node->next)
272 g_hash_table_insert (win_hm, node->data, node->data);
274 g_slist_free (group_list);
276 /* 3) include all accel groups used by acceleratables */
277 win_list = g_hash_table_slist_values (win_hm);
278 g_hash_table_destroy (win_hm);
279 for (slist = win_list; slist; slist = slist->next)
280 for (node = gtk_accel_groups_from_object (slist->data); node; node = node->next)
281 g_hash_table_insert (group_hm, node->data, node->data);
282 group_list = g_hash_table_slist_values (group_hm);
283 g_hash_table_destroy (group_hm);
285 /* 4) walk the acceleratables and figure whether they occupy accel_key&accel_mods */
287 for (slist = win_list; slist; slist = slist->next)
288 if (GTK_IS_WINDOW (slist->data)) /* bad kludge in lack of a GtkAcceleratable */
289 if (_gtk_window_query_nonaccels (slist->data, accel_key, accel_mods))
294 removable = !seen_accel;
296 /* 5) walk all accel groups and search for locks */
298 for (slist = group_list; slist; slist = slist->next)
300 GtkAccelGroup *group = slist->data;
301 GtkAccelGroupEntry *ag_entry;
305 ag_entry = entry->accel_key ? gtk_accel_group_query (group, entry->accel_key, entry->accel_mods, &n) : NULL;
306 for (i = 0; i < n; i++)
307 if (ag_entry[i].accel_path_quark == entry_quark)
309 can_change = !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
311 goto break_loop_step5;
315 ag_entry = accel_key ? gtk_accel_group_query (group, accel_key, accel_mods, &n) : NULL;
316 for (i = 0; i < n; i++)
319 removable = !group->lock_count && !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
321 goto break_loop_step5;
322 if (ag_entry[i].accel_path_quark)
323 replace_list = g_slist_prepend (replace_list, GUINT_TO_POINTER (ag_entry->accel_path_quark));
328 /* 6) check whether we can remove existing accelerators */
329 if (removable && can_change)
330 for (slist = replace_list; slist; slist = slist->next)
331 if (!internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, TRUE))
337 /* 7) check conditions and proceed if possible */
338 change_accel = can_change && (!seen_accel || (removable && replace));
340 if (change_accel && !simulate)
342 guint old_accel_key, old_accel_mods;
344 /* ref accel groups */
345 for (slist = group_list; slist; slist = slist->next)
346 g_object_ref (slist->data);
348 /* 8) remove existing accelerators */
349 for (slist = replace_list; slist; slist = slist->next)
350 internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, FALSE);
352 /* 9) install new accelerator */
353 old_accel_key = entry->accel_key;
354 old_accel_mods = entry->accel_mods;
355 entry->accel_key = accel_key;
356 entry->accel_mods = accel_mods;
357 entry->changed = TRUE;
358 for (slist = group_list; slist; slist = slist->next)
359 _gtk_accel_group_reconnect (slist->data, g_quark_from_string (entry->accel_path));
361 /* unref accel groups */
362 for (slist = group_list; slist; slist = slist->next)
363 g_object_unref (slist->data);
365 g_slist_free (replace_list);
366 g_slist_free (group_list);
367 g_slist_free (win_list);
373 * gtk_accel_map_change_entry
374 * @accel_path: valid accelerator path
375 * @accel_key: new accelerator key
376 * @accel_mods: new accelerator modifiers
377 * @replace: %TRUE if other accelerators may be deleted upon conflicts
378 * @returns: %TRUE if the accelerator could be changed, %FALSE otherwise
380 * Change the @accel_key and @accel_mods currently associated with @accel_path.
381 * Due to conflicts with other accelerators, a change may not alwys be possible,
382 * @replace indicates whether other accelerators may be deleted to resolve such
383 * conflicts. A change will only occur if all conflicts could be resolved (which
384 * might not be the case if conflicting accelerators are locked). Succesful
385 * changes are indicated by a %TRUE return value.
388 gtk_accel_map_change_entry (const gchar *accel_path,
390 GdkModifierType accel_mods,
393 g_return_val_if_fail (_gtk_accel_path_is_valid (accel_path), FALSE);
395 return internal_change_entry (accel_path, accel_key, accel_key ? accel_mods : 0, replace, FALSE);
399 accel_map_parse_accel_path (GScanner *scanner)
401 guint accel_key = 0, accel_mods = 0;
404 /* parse accel path */
405 g_scanner_get_next_token (scanner);
406 if (scanner->token != G_TOKEN_STRING)
407 return G_TOKEN_STRING;
409 /* test if the next token is an accelerator */
410 g_scanner_peek_next_token (scanner);
411 if (scanner->next_token != G_TOKEN_STRING)
413 /* if not so, eat that token and error out */
414 g_scanner_get_next_token (scanner);
415 return G_TOKEN_STRING;
418 /* get the full accelerator specification */
419 path = g_strdup (scanner->value.v_string);
420 g_scanner_get_next_token (scanner);
421 accel = g_strdup (scanner->value.v_string);
423 /* ensure the entry is present */
424 gtk_accel_map_add_entry (path, 0, 0);
426 /* and propagate it */
427 gtk_accelerator_parse (accel, &accel_key, &accel_mods);
428 gtk_accel_map_change_entry (path, accel_key, accel_mods, TRUE);
433 /* check correct statement end */
434 g_scanner_get_next_token (scanner);
435 if (scanner->token != ')')
442 accel_map_parse_statement (GScanner *scanner)
444 guint expected_token;
446 g_scanner_get_next_token (scanner);
448 if (scanner->token == G_TOKEN_SYMBOL)
450 guint (*parser_func) (GScanner*);
452 parser_func = scanner->value.v_symbol;
454 expected_token = parser_func (scanner);
457 expected_token = G_TOKEN_SYMBOL;
459 /* skip rest of statement on errrors
461 if (expected_token != G_TOKEN_NONE)
463 register guint level;
466 if (scanner->token == ')')
468 if (scanner->token == '(')
471 while (!g_scanner_eof (scanner) && level > 0)
473 g_scanner_get_next_token (scanner);
475 if (scanner->token == '(')
477 else if (scanner->token == ')')
484 gtk_accel_map_load_scanner (GScanner *scanner)
486 gboolean skip_comment_single;
487 gboolean symbol_2_token;
488 gchar *cpair_comment_single;
489 gpointer saved_symbol;
491 g_return_if_fail (scanner != 0);
493 /* configure scanner */
494 skip_comment_single = scanner->config->skip_comment_single;
495 scanner->config->skip_comment_single = TRUE;
496 cpair_comment_single = scanner->config->cpair_comment_single;
497 scanner->config->cpair_comment_single = ";\n";
498 symbol_2_token = scanner->config->symbol_2_token;
499 scanner->config->symbol_2_token = FALSE;
500 saved_symbol = g_scanner_lookup_symbol (scanner, "gtk_accel_path");
501 g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", accel_map_parse_accel_path);
503 /* outer parsing loop
505 g_scanner_peek_next_token (scanner);
506 while (scanner->next_token == '(')
508 g_scanner_get_next_token (scanner);
510 accel_map_parse_statement (scanner);
512 g_scanner_peek_next_token (scanner);
516 scanner->config->skip_comment_single = skip_comment_single;
517 scanner->config->cpair_comment_single = cpair_comment_single;
518 scanner->config->symbol_2_token = symbol_2_token;
519 g_scanner_scope_remove_symbol (scanner, 0, "gtk_accel_path");
521 g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", saved_symbol);
525 * gtk_accel_map_load_fd
526 * @fd: valid readable file descriptor
528 * Filedescriptor variant of gtk_accel_map_load().
529 * Note that the file descriptor will not be closed by this function.
532 gtk_accel_map_load_fd (gint fd)
536 g_return_if_fail (fd >= 0);
538 /* create and setup scanner */
539 scanner = g_scanner_new (NULL);
540 g_scanner_input_file (scanner, fd);
542 gtk_accel_map_load_scanner (scanner);
544 g_scanner_destroy (scanner);
549 * @file_name: a file containing accelerator specifications
551 * Parses a file previously saved with gtk_accel_map_save() for
552 * accelerator specifications, and propagates them accordingly.
555 gtk_accel_map_load (const gchar *file_name)
559 g_return_if_fail (file_name != NULL);
561 if (!g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
564 fd = open (file_name, O_RDONLY);
568 gtk_accel_map_load_fd (fd);
574 accel_map_print (gpointer data,
575 const gchar *accel_path,
580 GString *gstring = g_string_new (changed ? NULL : "; ");
581 gint err, fd = GPOINTER_TO_INT (data);
584 g_string_append (gstring, "(gtk_accel_path \"");
586 tmp = g_strescape (accel_path, NULL);
587 g_string_append (gstring, tmp);
590 g_string_append (gstring, "\" \"");
592 name = gtk_accelerator_name (accel_key, accel_mods);
593 tmp = g_strescape (name, NULL);
595 g_string_append (gstring, tmp);
598 g_string_append (gstring, "\")\n");
601 err = write (fd, gstring->str, gstring->len);
602 while (err < 0 && errno == EINTR);
604 g_string_free (gstring, TRUE);
608 * gtk_accel_map_save_fd
609 * @fd: valid writable file descriptor
611 * Filedescriptor variant of gtk_accel_map_save().
612 * Note that the file descriptor will not be closed by this function.
615 gtk_accel_map_save_fd (gint fd)
620 g_return_if_fail (fd >= 0);
622 gstring = g_string_new ("; ");
623 if (g_get_prgname ())
624 g_string_append (gstring, g_get_prgname ());
625 g_string_append (gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
626 g_string_append (gstring, "; this file is an automated accelerator map dump\n");
627 g_string_append (gstring, ";\n");
630 err = write (fd, gstring->str, gstring->len);
631 while (err < 0 && errno == EINTR);
633 gtk_accel_map_foreach (GINT_TO_POINTER (fd), accel_map_print);
638 * @file_name: the file to contain accelerator specifications
640 * Saves current accelerator specifications (accelerator path, key
641 * and modifiers) to @file_name.
642 * The file is written in a format suitable to be read back in by
643 * gtk_accel_map_load().
646 gtk_accel_map_save (const gchar *file_name)
650 g_return_if_fail (file_name != NULL);
652 fd = open (file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644);
656 gtk_accel_map_save_fd (fd);
662 * gtk_accel_map_foreach
663 * @data: data to be passed into @foreach_func
664 * @foreach_func: function to be executed for each accel map entry
666 * Loop over the entries in the accelerator map, and execute
667 * @foreach_func on each. The signature of @foreach_func is that of
668 * #GtkAccelMapForeach, the @changed parameter indicates whether
669 * this accelerator was changed during runtime (thus, would need
670 * saving during an accelerator map dump).
673 gtk_accel_map_foreach (gpointer data,
674 GtkAccelMapForeach foreach_func)
676 GSList *entries, *slist, *node;
678 g_return_if_fail (foreach_func != NULL);
680 entries = g_hash_table_slist_values (accel_entry_ht);
681 for (slist = entries; slist; slist = slist->next)
683 AccelEntry *entry = slist->data;
684 gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;
686 for (node = accel_filters; node; node = node->next)
687 if (g_pattern_match_string (node->data, entry->accel_path))
689 foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
693 g_slist_free (entries);
697 gtk_accel_map_foreach_unfiltered (gpointer data,
698 GtkAccelMapForeach foreach_func)
700 GSList *entries, *slist;
702 g_return_if_fail (foreach_func != NULL);
704 entries = g_hash_table_slist_values (accel_entry_ht);
705 for (slist = entries; slist; slist = slist->next)
707 AccelEntry *entry = slist->data;
708 gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;
710 foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
712 g_slist_free (entries);
716 gtk_accel_map_add_filter (const gchar *filter_pattern)
721 g_return_if_fail (filter_pattern != NULL);
723 pspec = g_pattern_spec_new (filter_pattern);
724 for (slist = accel_filters; slist; slist = slist->next)
725 if (g_pattern_spec_equal (pspec, slist->data))
727 g_pattern_spec_free (pspec);
730 accel_filters = g_slist_prepend (accel_filters, pspec);
734 _gtk_accel_map_add_group (const gchar *accel_path,
735 GtkAccelGroup *accel_group)
739 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
740 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
742 entry = accel_path_lookup (accel_path);
745 gtk_accel_map_add_entry (accel_path, 0, 0);
746 entry = accel_path_lookup (accel_path);
748 entry->groups = g_slist_prepend (entry->groups, accel_group);
752 _gtk_accel_map_remove_group (const gchar *accel_path,
753 GtkAccelGroup *accel_group)
757 entry = accel_path_lookup (accel_path);
758 g_return_if_fail (entry != NULL);
759 g_return_if_fail (g_slist_find (entry->groups, accel_group));
761 entry->groups = g_slist_remove (entry->groups, accel_group);