Code

change XML vocabulary for keys, make keys parsing a bit more relaxed
[inkscape.git] / src / shortcuts.cpp
1 #define __SP_SHORTCUTS_C__
3 /** \file
4  * Keyboard shortcut processing.
5  */
6 /*
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   MenTaLguY <mental@rydia.net>
10  *   bulia byak <buliabyak@users.sf.net>
11  *   Peter Moulder <pmoulder@mail.csse.monash.edu.au>
12  *
13  * Copyright (C) 2005  Monash University
14  * Copyright (C) 2005  MenTaLguY <mental@rydia.net>
15  *
16  * You may redistribute and/or modify this file under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2 of the License, or (at your
18  * option) any later version.
19  */
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <vector>
27 #include <gdk/gdkkeys.h>
28 #include <gdk/gdkkeysyms.h>
30 #include "helper/action.h"
31 #include "io/sys.h"
32 #include "io/resource.h"
33 #include "shortcuts.h"
34 #include "verbs.h"
35 #include "xml/node-iterators.h"
36 #include "xml/repr.h"
38 using namespace Inkscape;
40 static void sp_shortcut_set(unsigned int const shortcut, Inkscape::Verb *const verb, bool const is_primary);
41 static void try_shortcuts_file(char const *filename);
42 static void read_shortcuts_file(char const *filename);
44 /* Returns true if action was performed */
46 bool
47 sp_shortcut_invoke(unsigned int shortcut, Inkscape::UI::View::View *view)
48 {
49     Inkscape::Verb *verb = sp_shortcut_get_verb(shortcut);
50     if (verb) {
51         SPAction *action = verb->get_action(view);
52         if (action) {
53             sp_action_perform(action, NULL);
54             return true;
55         }
56     }
57     return false;
58 }
60 static GHashTable *verbs = NULL;
61 static GHashTable *primary_shortcuts = NULL;
63 static void
64 sp_shortcut_init()
65 {
66     using Inkscape::IO::Resource::get_path;
67     using Inkscape::IO::Resource::SYSTEM;
68     using Inkscape::IO::Resource::USER;
69     using Inkscape::IO::Resource::KEYS;
71     verbs = g_hash_table_new(NULL, NULL);
72     primary_shortcuts = g_hash_table_new(NULL, NULL);
74     read_shortcuts_file(get_path(SYSTEM, KEYS, "default.xml"));
75     try_shortcuts_file(get_path(USER, KEYS, "default.xml"));
76 }
78 static void try_shortcuts_file(char const *filename) {
79     using Inkscape::IO::file_test;
81     /* ah, if only we had an exception to catch... (permission, forgiveness) */
82     if (file_test(filename, G_FILE_TEST_EXISTS)) {
83         read_shortcuts_file(filename);
84     }
85 }
87 static void read_shortcuts_file(char const *filename) {
88     XML::Document *doc=sp_repr_read_file(filename, NULL);
89     if (!doc) {
90         g_warning("Unable to read keys file %s", filename);
91         return;
92     }
94     XML::Node const *root=doc->root();
95     g_return_if_fail(!strcmp(root->name(), "keys"));
96     XML::NodeConstSiblingIterator iter=root->firstChild();
97     for ( ; iter ; ++iter ) {
98         bool is_primary;
100         if (!strcmp(iter->name(), "bind")) {
101             if (iter->attribute("display") && strcmp(iter->attribute("display"), "false") && strcmp(iter->attribute("display"), "0")) {
102                 is_primary = true;
103             } else {
104                 is_primary = false;
105             }
106         } else {
107             // some unknown element, do not complain
108             continue;
109         }
111         gchar const *verb_name=iter->attribute("action");
112         if (!verb_name) {
113             g_warning("Missing verb name (action= attribute) for shortcut");
114             continue;
115         }
117         if (Inkscape::Verb::getbyid(verb_name) == NULL) {
118             g_warning("Unknown verb name: %s", verb_name);
119             continue;
120         }
122         gchar const *keyval_name=iter->attribute("key");
123         if (!keyval_name) {
124             // that's ok, it's just listed for reference without assignment, skip it
125             continue;
126         }
127         guint keyval=gdk_keyval_from_name(keyval_name);
128         if (keyval == GDK_VoidSymbol) {
129             g_warning("Unknown keyval %s for %s", keyval_name, verb_name);
130             continue;
131         }
133         guint modifiers=0;
135         gchar const *modifiers_string=iter->attribute("modifiers");
136         if (modifiers_string) {
137             gchar const *iter=modifiers_string;
138             while (*iter) {
139                 size_t length=strcspn(iter, ",");
140                 gchar *mod=g_strndup(iter, length);
141                 if (!strcmp(mod, "Control") || !strcmp(mod, "Ctrl")) {
142                     modifiers |= SP_SHORTCUT_CONTROL_MASK;
143                 } else if (!strcmp(mod, "Shift")) {
144                     modifiers |= SP_SHORTCUT_SHIFT_MASK;
145                 } else if (!strcmp(mod, "Alt")) {
146                     modifiers |= SP_SHORTCUT_ALT_MASK;
147                 } else {
148                     g_warning("Unknown modifier %s for %s", mod, verb_name);
149                 }
150                 g_free(mod);
151                 iter += length;
152                 if (*iter) iter++;
153             }
154         }
156         sp_shortcut_set(keyval | modifiers,
157                         Inkscape::Verb::getbyid(verb_name),
158                         is_primary);
159     }
161     GC::release(doc);
164 /**
165  * Adds a keyboard shortcut for the given verb.
166  * (Removes any existing binding for the given shortcut, including appropriately
167  * adjusting sp_shortcut_get_primary if necessary.)
168  *
169  * \param is_primary True iff this is the shortcut to be written in menu items or buttons.
170  *
171  * \post sp_shortcut_get_verb(shortcut) == verb.
172  * \post !is_primary or sp_shortcut_get_primary(verb) == shortcut.
173  */
174 static void
175 sp_shortcut_set(unsigned int const shortcut, Inkscape::Verb *const verb, bool const is_primary)
177     if (!verbs) sp_shortcut_init();
179     Inkscape::Verb *old_verb = (Inkscape::Verb *)(g_hash_table_lookup(verbs, GINT_TO_POINTER(shortcut)));
180     g_hash_table_insert(verbs, GINT_TO_POINTER(shortcut), (gpointer)(verb));
182     /* Maintain the invariant that sp_shortcut_get_primary(v) returns either 0 or a valid shortcut for v. */
183     if (old_verb && old_verb != verb) {
184         unsigned int const old_primary = (unsigned int)GPOINTER_TO_INT(g_hash_table_lookup(primary_shortcuts, (gpointer)old_verb));
186         if (old_primary == shortcut) {
187             g_hash_table_insert(primary_shortcuts, (gpointer)old_verb, GINT_TO_POINTER(0));
188         }
189     }
191     if (is_primary) {
192         g_hash_table_insert(primary_shortcuts, (gpointer)(verb), GINT_TO_POINTER(shortcut));
193     }
196 Inkscape::Verb *
197 sp_shortcut_get_verb(unsigned int shortcut)
199     if (!verbs) sp_shortcut_init();
200     return (Inkscape::Verb *)(g_hash_table_lookup(verbs, GINT_TO_POINTER(shortcut)));
203 unsigned int
204 sp_shortcut_get_primary(Inkscape::Verb *verb)
206     if (!primary_shortcuts) sp_shortcut_init();
207     return (unsigned int)GPOINTER_TO_INT(g_hash_table_lookup(primary_shortcuts,
208                                                              (gpointer)(verb)));
212 /*
213   Local Variables:
214   mode:c++
215   c-file-style:"stroustrup"
216   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
217   indent-tabs-mode:nil
218   fill-column:99
219   End:
220 */
221 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :