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.
19 #include "gtkaccelmap.h"
21 #include "gtkwindow.h" /* in lack of GtkAcceleratable */
29 /* --- structures --- */
31 const gchar *accel_path;
41 /* --- variables --- */
42 static GHashTable *accel_entry_ht = NULL; /* accel_path -> AccelEntry */
43 static GSList *accel_filters = NULL;
46 /* --- functions --- */
48 accel_entry_hash (gconstpointer key)
50 const AccelEntry *entry = key;
52 return g_str_hash (entry->accel_path);
56 accel_entry_equal (gconstpointer key1,
59 const AccelEntry *entry1 = key1;
60 const AccelEntry *entry2 = key2;
62 return g_str_equal (entry1->accel_path, entry2->accel_path);
65 static inline AccelEntry*
66 accel_path_lookup (const gchar *accel_path)
70 ekey.accel_path = accel_path;
72 /* safety NULL check for return_if_fail()s */
73 return accel_path ? g_hash_table_lookup (accel_entry_ht, &ekey) : NULL;
77 _gtk_accel_map_init (void)
79 g_assert (accel_entry_ht == NULL);
81 accel_entry_ht = g_hash_table_new (accel_entry_hash, accel_entry_equal);
85 gtk_accel_path_is_valid (const gchar *accel_path)
89 if (!accel_path || accel_path[0] != '<' ||
90 accel_path[1] == '<' || accel_path[1] == '>' || !accel_path[1])
92 p = strchr (accel_path, '>');
93 if (!p || p[1] != '/')
99 * gtk_accel_map_add_entry
100 * @accel_path: valid accelerator path
101 * @accel_key: the accelerator key
102 * @accel_mods: the accelerator modifiers
103 * @returns: the GQuark for the @accel_path (to ease local storage)
105 * Register a new accelerator with the global accelerator map.
106 * This function should only be called once per @accel_path
107 * with the canonical @accel_key and @accel_mods for this path.
108 * To change the accelerator during runtime programatically, use
109 * gtk_accel_map_change_entry().
110 * The accelerator path must consist of "<WINDOWTYPE>/Category1/Category2/.../Action",
111 * where WINDOWTYPE should be a unique application specifc identifier, that
112 * corresponds to the kind of window the accelerator is being used in, e.g. "Gimp-Image",
113 * "Abiword-Document" or "Gnumeric-Settings".
114 * The Category1/.../Action portion is most apropriately choosen by the action the
115 * accelerator triggers, i.e. for accelerators on menu items, choose the item's menu path,
116 * e.g. "File/Save As", "Image/View/Zoom" or "Edit/Select All".
117 * So a full valid accelerator path may look like:
118 * "<Gimp-Toolbox>/File/Dialogs/Tool Options...".
121 gtk_accel_map_add_entry (const gchar *accel_path,
127 g_return_val_if_fail (gtk_accel_path_is_valid (accel_path), 0);
129 accel_mods &= gtk_accelerator_get_default_mod_mask ();
131 entry = accel_path_lookup (accel_path);
134 if (!entry->std_accel_key && !entry->std_accel_mods &&
135 (accel_key || accel_mods))
137 entry->std_accel_key = accel_key;
138 entry->std_accel_mods = accel_mods;
140 gtk_accel_map_change_entry (entry->accel_path, accel_key, accel_mods, TRUE);
145 entry = g_new0 (AccelEntry, 1);
146 entry->accel_path = g_quark_to_string (g_quark_from_string (accel_path));
147 entry->std_accel_key = accel_key;
148 entry->std_accel_mods = accel_mods;
149 entry->accel_key = accel_key;
150 entry->accel_mods = accel_mods;
151 entry->changed = FALSE;
152 g_hash_table_insert (accel_entry_ht, entry, entry);
154 return g_quark_try_string (entry->accel_path);
159 GtkAccelGroup *accel_group;
163 accel_hook_finalize (GHookList *hook_list,
166 GDestroyNotify destroy = hook->destroy;
167 AccelHook *ahook = (AccelHook*) hook;
169 if (ahook->accel_group)
170 g_object_unref (ahook->accel_group);
174 hook->destroy = NULL;
175 destroy (hook->data);
181 * @data: notifier user data
182 * @accel_path_quark: accelerator path (as #GQuark) which has just changed
183 * @accel_key: new accelerator key
184 * @accel_mods: new accelerator modifiers
185 * @accel_group: accelerator group of this notifier
186 * @old_accel_key: former accelerator key
187 * @old_accel_mods): former accelerator modifiers
189 * #GtkAccelMapNotify is the signature of user callbacks, installed via
190 * gtk_accel_map_add_notifier(). Once the accel path of the notifier changes,
191 * the notifier is invoked with this signature, where @accel_path_quark
192 * indicates the accel path that changed, and @data and @accel_group are
193 * the notifier's arguments as passed into gtk_accel_map_add_notifier().
197 * gtk_accel_map_add_notifer
198 * @accel_path: valid accelerator path
199 * @notify_data: data argument to the notifier
200 * @notify_func: the notifier function
201 * @accel_group: accelerator group used by the notifier function
203 * Install a notifier function to be called if an accelerator
204 * map entry changes. @accel_group has to be the accel group
205 * that is being affected (gets an accelerator removed or added,
206 * when the notifier function is executed).
209 gtk_accel_map_add_notifer (const gchar *accel_path,
210 gpointer notify_data,
211 GtkAccelMapNotify notify_func,
212 GtkAccelGroup *accel_group)
218 g_return_if_fail (gtk_accel_path_is_valid (accel_path));
219 g_return_if_fail (notify_func != NULL);
221 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
223 entry = accel_path_lookup (accel_path);
226 gtk_accel_map_add_entry (accel_path, 0, 0);
227 entry = accel_path_lookup (accel_path);
231 entry->hooks = g_new (GHookList, 1);
232 g_hook_list_init (entry->hooks, sizeof (AccelHook));
233 entry->hooks->finalize_hook = accel_hook_finalize;
235 hook = g_hook_alloc (entry->hooks);
236 hook->data = notify_data;
237 hook->func = notify_func;
238 hook->destroy = NULL;
239 ahook = (AccelHook*) hook;
240 ahook->accel_group = accel_group ? g_object_ref (accel_group) : NULL;
241 g_hook_append (entry->hooks, hook);
245 * gtk_accel_map_remove_notifer
246 * @accel_path: valid accelerator path
247 * @notify_data: data argument to the notifier
248 * @notify_func: the notifier function
250 * Remove a notifier function, previously installed through
251 * gtk_accel_map_add_notifer().
254 gtk_accel_map_remove_notifer (const gchar *accel_path,
255 gpointer notify_data,
256 GtkAccelMapNotify notify_func)
260 g_return_if_fail (gtk_accel_path_is_valid (accel_path));
261 g_return_if_fail (notify_func != NULL);
263 entry = accel_path_lookup (accel_path);
264 if (entry && entry->hooks)
266 GHook *hook = g_hook_find_func_data (entry->hooks, TRUE, notify_func, notify_data);
268 if (hook && g_hook_destroy (entry->hooks, hook->hook_id))
269 return; /* successfully removed */
271 g_warning (G_STRLOC ": no notifier %p(%p) installed for accel path \"%s\"",
272 notify_func, notify_data, accel_path);
276 * gtk_accel_map_lookup_entry
277 * @accel_path: valid accelerator path
278 * @key: accelerator key to be filled in (optional)
279 * @returns: #GQuark for @accel_path or (0) if @accel_path is not known
281 * Lookup the accelerator entry for @accel_path and fill in @key.
282 * If the lookup revealed no results, (0) is returned, the entry's
286 gtk_accel_map_lookup_entry (const gchar *accel_path,
291 g_return_val_if_fail (gtk_accel_path_is_valid (accel_path), 0);
293 entry = accel_path_lookup (accel_path);
296 key->accel_key = entry->accel_key;
297 key->accel_mods = entry->accel_mods;
298 key->accel_flags = 0; // FIXME: global lock?
301 return entry ? g_quark_try_string (entry->accel_path) : 0;
305 hash2slist_foreach (gpointer key,
309 GSList **slist_p = user_data;
311 *slist_p = g_slist_prepend (*slist_p, value);
315 g_hash_table_slist_values (GHashTable *hash_table)
317 GSList *slist = NULL;
319 g_return_val_if_fail (hash_table != NULL, NULL);
321 g_hash_table_foreach (hash_table, hash2slist_foreach, &slist);
327 internal_change_entry (const gchar *accel_path,
329 GdkModifierType accel_mods,
333 GSList *node, *slist, *win_list, *group_list, *replace_list = NULL;
334 GHashTable *group_hm, *win_hm;
335 gboolean change_accel, removable, can_change = TRUE, seen_accel = FALSE, hooks_may_recurse = TRUE;
338 AccelEntry *entry = accel_path_lookup (accel_path);
340 /* not much todo if there's no entry yet */
345 gtk_accel_map_add_entry (accel_path, 0, 0);
346 entry = accel_path_lookup (accel_path);
347 entry->accel_key = accel_key;
348 entry->accel_mods = accel_mods;
349 entry->changed = TRUE;
354 /* if there's nothing to change, not much todo either */
355 if (entry->accel_key == accel_key && entry->accel_mods == accel_mods)
358 /* nobody's interested, easy going */
363 entry->accel_key = accel_key;
364 entry->accel_mods = accel_mods;
365 entry->changed = TRUE;
370 /* 1) fetch all accel groups affected by this entry */
371 entry_quark = g_quark_try_string (entry->accel_path);
372 group_hm = g_hash_table_new (NULL, NULL);
373 win_hm = g_hash_table_new (NULL, NULL);
374 hook = g_hook_first_valid (entry->hooks, hooks_may_recurse);
377 AccelHook *ahook = (AccelHook*) hook;
379 if (ahook->accel_group)
380 g_hash_table_insert (group_hm, ahook->accel_group, ahook->accel_group);
381 hook = g_hook_next_valid (entry->hooks, hook, hooks_may_recurse);
384 /* 2) collect acceleratables affected */
385 group_list = g_hash_table_slist_values (group_hm);
386 for (slist = group_list; slist; slist = slist->next)
388 GtkAccelGroup *group = slist->data;
390 for (node = group->acceleratables; node; node = node->next)
391 g_hash_table_insert (win_hm, node->data, node->data);
393 g_slist_free (group_list);
395 /* 3) include all accel groups used by acceleratables */
396 win_list = g_hash_table_slist_values (win_hm);
397 g_hash_table_destroy (win_hm);
398 for (slist = win_list; slist; slist = slist->next)
399 for (node = gtk_accel_groups_from_acceleratable (slist->data); node; node = node->next)
400 g_hash_table_insert (group_hm, node->data, node->data);
401 group_list = g_hash_table_slist_values (group_hm);
402 g_hash_table_destroy (group_hm);
404 /* 4) walk the acceleratables and figure whether they occupy accel_key&accel_mods */
405 for (slist = accel_key ? win_list : NULL; slist; slist = slist->next)
406 if (GTK_IS_WINDOW (slist->data)) /* bad kludge in lack of a GtkAcceleratable */
407 if (_gtk_window_query_nonaccels (slist->data, accel_key, accel_mods))
412 removable = !seen_accel;
414 /* 5) walk all accel groups and search for locks */
415 for (slist = removable ? group_list : NULL; slist; slist = slist->next)
417 GtkAccelGroup *group = slist->data;
418 GtkAccelGroupEntry *ag_entry;
422 ag_entry = entry->accel_key ? gtk_accel_group_query (group, entry->accel_key, entry->accel_mods, &n) : NULL;
423 for (i = 0; i < n; i++)
424 if (ag_entry[i].accel_path_quark == entry_quark)
426 can_change = !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
428 goto break_loop_step5;
432 ag_entry = accel_key ? gtk_accel_group_query (group, accel_key, accel_mods, &n) : NULL;
433 for (i = 0; i < n; i++)
436 removable = !group->lock_count && !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
438 goto break_loop_step5;
439 if (ag_entry[i].accel_path_quark)
440 replace_list = g_slist_prepend (replace_list, GUINT_TO_POINTER (ag_entry->accel_path_quark));
445 /* 6) check whether we can remove existing accelerators */
446 for (slist = removable ? replace_list : NULL; slist; slist = slist->next)
447 if (!internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, TRUE))
453 /* 7) check conditions and proceed if possible */
454 change_accel = can_change && (!seen_accel || (removable && replace));
456 if (change_accel && !simulate)
458 guint old_accel_key, old_accel_mods;
460 /* 8) remove existing accelerators */
461 for (slist = replace_list; slist; slist = slist->next)
462 internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, FALSE);
464 /* 9) install new accelerator */
465 old_accel_key = entry->accel_key;
466 old_accel_mods = entry->accel_mods;
467 entry->accel_key = accel_key;
468 entry->accel_mods = accel_mods;
469 entry->changed = TRUE;
470 hook = g_hook_first_valid (entry->hooks, hooks_may_recurse);
473 gboolean was_in_call, need_destroy = FALSE;
474 GtkAccelMapNotify hook_func = hook->func;
475 AccelHook *ahook = (AccelHook*) hook;
477 was_in_call = G_HOOK_IN_CALL (hook);
478 hook->flags |= G_HOOK_FLAG_IN_CALL;
479 /* need_destroy = */ hook_func (hook->data, g_quark_try_string (entry->accel_path),
480 entry->accel_key, entry->accel_mods,
482 old_accel_key, old_accel_mods);
484 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
486 g_hook_destroy_link (entry->hooks, hook);
487 hook = g_hook_next_valid (entry->hooks, hook, hooks_may_recurse);
490 g_slist_free (replace_list);
491 g_slist_free (group_list);
492 g_slist_free (win_list);
498 * gtk_accel_map_change_entry
499 * @accel_path: valid accelerator path
500 * @accel_key: new accelerator key
501 * @accel_mods: new accelerator modifiers
502 * @replace: %TRUE if other accelerators may be deleted upon conflicts
503 * @returns: %TRUE if the accelerator could be changed, %FALSE otherwise
505 * Change the @accel_key and @accel_mods currently associated with @accel_path.
506 * Due to conflicts with other accelerators, a change may not alwys be possible,
507 * @replace indicates whether other accelerators may be deleted to resolve such
508 * conflicts. A change will only occour if all conflicts could be resolved (which
509 * might not be the case if conflicting accelerators are locked). Succesfull
510 * changes are indicated by a %TRUE return value.
511 * Changes occouring are also indicated by invocation of notifiers attached to
512 * @accel_path (see gtk_accel_map_add_notifer() on this) and other accelerators
513 * that are being deleted.
516 gtk_accel_map_change_entry (const gchar *accel_path,
518 GdkModifierType accel_mods,
521 g_return_val_if_fail (gtk_accel_path_is_valid (accel_path), FALSE);
523 return internal_change_entry (accel_path, accel_key, accel_key ? accel_mods : 0, replace, FALSE);
527 accel_map_parse_accel_path (GScanner *scanner)
529 guint accel_key = 0, accel_mods = 0;
532 /* parse accel path */
533 g_scanner_get_next_token (scanner);
534 if (scanner->token != G_TOKEN_STRING)
535 return G_TOKEN_STRING;
537 /* test if the next token is an accelerator */
538 g_scanner_peek_next_token (scanner);
539 if (scanner->next_token != G_TOKEN_STRING)
541 /* if not so, eat that token and error out */
542 g_scanner_get_next_token (scanner);
543 return G_TOKEN_STRING;
546 /* get the full accelerator specification */
547 path = g_strdup (scanner->value.v_string);
548 g_scanner_get_next_token (scanner);
549 accel = g_strdup (scanner->value.v_string);
551 /* ensure the entry is present */
552 gtk_accel_map_add_entry (path, 0, 0);
554 /* and propagate it */
555 gtk_accelerator_parse (accel, &accel_key, &accel_mods);
556 gtk_accel_map_change_entry (path, accel_key, accel_mods, TRUE);
561 /* check correct statement end */
562 g_scanner_get_next_token (scanner);
563 if (scanner->token != ')')
570 accel_map_parse_statement (GScanner *scanner)
572 guint expected_token;
574 g_scanner_get_next_token (scanner);
576 if (scanner->token == G_TOKEN_SYMBOL)
578 guint (*parser_func) (GScanner*);
580 parser_func = scanner->value.v_symbol;
582 expected_token = parser_func (scanner);
585 expected_token = G_TOKEN_SYMBOL;
587 /* skip rest of statement on errrors
589 if (expected_token != G_TOKEN_NONE)
591 register guint level;
594 if (scanner->token == ')')
596 if (scanner->token == '(')
599 while (!g_scanner_eof (scanner) && level > 0)
601 g_scanner_get_next_token (scanner);
603 if (scanner->token == '(')
605 else if (scanner->token == ')')
612 gtk_accel_map_load_scanner (GScanner *scanner)
614 gboolean skip_comment_single;
615 gboolean symbol_2_token;
616 gchar *cpair_comment_single;
617 gpointer saved_symbol;
619 g_return_if_fail (scanner != 0);
621 /* configure scanner */
622 skip_comment_single = scanner->config->skip_comment_single;
623 scanner->config->skip_comment_single = TRUE;
624 cpair_comment_single = scanner->config->cpair_comment_single;
625 scanner->config->cpair_comment_single = ";\n";
626 symbol_2_token = scanner->config->symbol_2_token;
627 scanner->config->symbol_2_token = FALSE;
628 saved_symbol = g_scanner_lookup_symbol (scanner, "gtk_accel_path");
629 g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", accel_map_parse_accel_path);
631 /* outer parsing loop
633 g_scanner_peek_next_token (scanner);
634 while (scanner->next_token == '(')
636 g_scanner_get_next_token (scanner);
638 accel_map_parse_statement (scanner);
640 g_scanner_peek_next_token (scanner);
644 scanner->config->skip_comment_single = skip_comment_single;
645 scanner->config->cpair_comment_single = cpair_comment_single;
646 scanner->config->symbol_2_token = symbol_2_token;
647 g_scanner_scope_remove_symbol (scanner, 0, "gtk_accel_path");
649 g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", saved_symbol);
653 * gtk_accel_map_load_fd
654 * @fd: valid readable file descriptor
656 * Filedescriptor variant of gtk_accel_map_load().
657 * Note that the file descriptor will not be closed by this function.
660 gtk_accel_map_load_fd (gint fd)
664 g_return_if_fail (fd >= 0);
666 /* create and setup scanner */
667 scanner = g_scanner_new (NULL);
668 g_scanner_input_file (scanner, fd);
670 gtk_accel_map_load_scanner (scanner);
672 g_scanner_destroy (scanner);
677 * @file_name: a file containing accelerator specifications
679 * Parses a file previously saved with gtk_accel_map_save() for
680 * accelerator specifications, and propagates them accordingly.
683 gtk_accel_map_load (const gchar *file_name)
687 g_return_if_fail (file_name != NULL);
689 if (!g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
692 fd = open (file_name, O_RDONLY);
696 gtk_accel_map_load_fd (fd);
702 accel_map_print (gpointer data,
703 const gchar *accel_path,
708 GString *gstring = g_string_new (changed ? NULL : "; ");
709 gint err, fd = GPOINTER_TO_INT (data);
712 g_string_append (gstring, "(gtk_accel_path \"");
714 tmp = g_strescape (accel_path, NULL);
715 g_string_append (gstring, tmp);
718 g_string_append (gstring, "\" \"");
720 name = gtk_accelerator_name (accel_key, accel_mods);
721 tmp = g_strescape (name, NULL);
723 g_string_append (gstring, tmp);
726 g_string_append (gstring, "\")\n");
729 err = write (fd, gstring->str, gstring->len);
730 while (err < 0 && errno == EINTR);
732 g_string_free (gstring, TRUE);
736 * gtk_accel_map_save_fd
737 * @fd: valid writable file descriptor
739 * Filedescriptor variant of gtk_accel_map_save().
740 * Note that the file descriptor will not be closed by this function.
743 gtk_accel_map_save_fd (gint fd)
748 g_return_if_fail (fd >= 0);
750 gstring = g_string_new ("; ");
751 if (g_get_prgname ())
752 g_string_append (gstring, g_get_prgname ());
753 g_string_append (gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
754 g_string_append (gstring, "; this file is an automated accelerator map dump\n");
755 g_string_append (gstring, ";\n");
758 err = write (fd, gstring->str, gstring->len);
759 while (err < 0 && errno == EINTR);
761 gtk_accel_map_foreach (GINT_TO_POINTER (fd), accel_map_print);
766 * @file_name: the file to contain accelerator specifications
768 * Saves current accelerator specifications (accelerator path, key
769 * and modifiers) to @file_name.
770 * The file is written in a format suitable to be read back in by
771 * gtk_accel_map_load().
774 gtk_accel_map_save (const gchar *file_name)
778 g_return_if_fail (file_name != NULL);
780 fd = open (file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644);
784 gtk_accel_map_save_fd (fd);
790 * gtk_accel_map_foreach
791 * @data: data to be passed into @foreach_func
792 * @foreach_func: function to be executed for each accel map entry
794 * Loop over the entries in the accelerator map, and execute
795 * @foreach_func on each. The signature of @foreach_func is that of
796 * #GtkAccelMapForeach, the @changed parameter indicates whether
797 * this accelerator was changed during runtime (thus, would need
798 * saving during an accelerator map dump).
801 gtk_accel_map_foreach (gpointer data,
802 GtkAccelMapForeach foreach_func)
804 GSList *entries, *slist, *node;
806 g_return_if_fail (foreach_func != NULL);
808 entries = g_hash_table_slist_values (accel_entry_ht);
809 for (slist = entries; slist; slist = slist->next)
811 AccelEntry *entry = slist->data;
812 gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;
814 for (node = accel_filters; node; node = node->next)
815 if (g_pattern_match_string (node->data, entry->accel_path))
817 foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
820 g_slist_free (entries);
824 gtk_accel_map_foreach_unfiltered (gpointer data,
825 GtkAccelMapForeach foreach_func)
827 GSList *entries, *slist;
829 g_return_if_fail (foreach_func != NULL);
831 entries = g_hash_table_slist_values (accel_entry_ht);
832 for (slist = entries; slist; slist = slist->next)
834 AccelEntry *entry = slist->data;
835 gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;
837 foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
839 g_slist_free (entries);
843 gtk_accel_map_add_filter (const gchar *filter_pattern)
848 g_return_if_fail (filter_pattern != NULL);
850 pspec = g_pattern_spec_new (filter_pattern);
851 for (slist = accel_filters; slist; slist = slist->next)
852 if (g_pattern_spec_equal (pspec, slist->data))
854 g_pattern_spec_free (pspec);
857 accel_filters = g_slist_prepend (accel_filters, pspec);