Code

minor cleanups
[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             is_primary = iter->attribute("display") && strcmp(iter->attribute("display"), "false") && strcmp(iter->attribute("display"), "0");
102         } else {
103             // some unknown element, do not complain
104             continue;
105         }
107         gchar const *verb_name=iter->attribute("action");
108         if (!verb_name) {
109             g_warning("Missing verb name (action= attribute) for shortcut");
110             continue;
111         }
113         Inkscape::Verb *verb=Inkscape::Verb::getbyid(verb_name);
114         if (!verb) {
115             g_warning("Unknown verb name: %s", verb_name);
116             continue;
117         }
119         gchar const *keyval_name=iter->attribute("key");
120         if (!keyval_name) {
121             // that's ok, it's just listed for reference without assignment, skip it
122             continue;
123         }
125         guint keyval=gdk_keyval_from_name(keyval_name);
126         if (keyval == GDK_VoidSymbol) {
127             g_warning("Unknown keyval %s for %s", keyval_name, verb_name);
128             continue;
129         }
131         guint modifiers=0;
133         gchar const *modifiers_string=iter->attribute("modifiers");
134         if (modifiers_string) {
135             gchar const *iter=modifiers_string;
136             while (*iter) {
137                 size_t length=strcspn(iter, ",");
138                 gchar *mod=g_strndup(iter, length);
139                 if (!strcmp(mod, "Control") || !strcmp(mod, "Ctrl")) {
140                     modifiers |= SP_SHORTCUT_CONTROL_MASK;
141                 } else if (!strcmp(mod, "Shift")) {
142                     modifiers |= SP_SHORTCUT_SHIFT_MASK;
143                 } else if (!strcmp(mod, "Alt")) {
144                     modifiers |= SP_SHORTCUT_ALT_MASK;
145                 } else {
146                     g_warning("Unknown modifier %s for %s", mod, verb_name);
147                 }
148                 g_free(mod);
149                 iter += length;
150                 if (*iter) iter++;
151             }
152         }
154         sp_shortcut_set(keyval | modifiers, verb, is_primary);
155     }
157     GC::release(doc);
160 /**
161  * Adds a keyboard shortcut for the given verb.
162  * (Removes any existing binding for the given shortcut, including appropriately
163  * adjusting sp_shortcut_get_primary if necessary.)
164  *
165  * \param is_primary True iff this is the shortcut to be written in menu items or buttons.
166  *
167  * \post sp_shortcut_get_verb(shortcut) == verb.
168  * \post !is_primary or sp_shortcut_get_primary(verb) == shortcut.
169  */
170 static void
171 sp_shortcut_set(unsigned int const shortcut, Inkscape::Verb *const verb, bool const is_primary)
173     if (!verbs) sp_shortcut_init();
175     Inkscape::Verb *old_verb = (Inkscape::Verb *)(g_hash_table_lookup(verbs, GINT_TO_POINTER(shortcut)));
176     g_hash_table_insert(verbs, GINT_TO_POINTER(shortcut), (gpointer)(verb));
178     /* Maintain the invariant that sp_shortcut_get_primary(v) returns either 0 or a valid shortcut for v. */
179     if (old_verb && old_verb != verb) {
180         unsigned int const old_primary = (unsigned int)GPOINTER_TO_INT(g_hash_table_lookup(primary_shortcuts, (gpointer)old_verb));
182         if (old_primary == shortcut) {
183             g_hash_table_insert(primary_shortcuts, (gpointer)old_verb, GINT_TO_POINTER(0));
184         }
185     }
187     if (is_primary) {
188         g_hash_table_insert(primary_shortcuts, (gpointer)(verb), GINT_TO_POINTER(shortcut));
189     }
192 Inkscape::Verb *
193 sp_shortcut_get_verb(unsigned int shortcut)
195     if (!verbs) sp_shortcut_init();
196     return (Inkscape::Verb *)(g_hash_table_lookup(verbs, GINT_TO_POINTER(shortcut)));
199 unsigned int
200 sp_shortcut_get_primary(Inkscape::Verb *verb)
202     if (!primary_shortcuts) sp_shortcut_init();
203     return (unsigned int)GPOINTER_TO_INT(g_hash_table_lookup(primary_shortcuts,
204                                                              (gpointer)(verb)));
208 /*
209   Local Variables:
210   mode:c++
211   c-file-style:"stroustrup"
212   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
213   indent-tabs-mode:nil
214   fill-column:99
215   End:
216 */
217 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :