Code

Translations. French translation minor update.
[inkscape.git] / src / shortcuts.cpp
index 7061df77a4d5ec241b9a7fec1467f37682d96f1c..d647d30b3264aebfaf9b01e035f5ce72280dcff5 100644 (file)
 #endif
 
 #include <vector>
+#include <cstring>
+#include <string>
 
 #include <gdk/gdkkeys.h>
 #include <gdk/gdkkeysyms.h>
+#include <gtk/gtk.h>
 
 #include "helper/action.h"
 #include "io/sys.h"
@@ -92,33 +95,38 @@ static void read_shortcuts_file(char const *filename) {
     }
 
     XML::Node const *root=doc->root();
-    g_return_if_fail(!strcmp(root->name(), "keybindings"));
+    g_return_if_fail(!strcmp(root->name(), "keys"));
     XML::NodeConstSiblingIterator iter=root->firstChild();
     for ( ; iter ; ++iter ) {
         bool is_primary;
 
-        if (!strcmp(iter->name(), "primary")) {
-            is_primary = true;
-        } else if (!strcmp(iter->name(), "secondary")) {
-            is_primary = false;
+        if (!strcmp(iter->name(), "bind")) {
+            is_primary = iter->attribute("display") && strcmp(iter->attribute("display"), "false") && strcmp(iter->attribute("display"), "0");
         } else {
-            g_warning("Unknown key binding type %s", iter->name());
+            // some unknown element, do not complain
             continue;
         }
 
-        gchar const *verb_name=iter->attribute("verb");
+        gchar const *verb_name=iter->attribute("action");
         if (!verb_name) {
-            g_warning("Missing verb name for shortcut");
+            g_warning("Missing verb name (action= attribute) for shortcut");
             continue;
         }
 
-        gchar const *keyval_name=iter->attribute("keyval");
-        if (!keyval_name) {
-            g_warning("Missing keyval for %s", verb_name);
+        Inkscape::Verb *verb=Inkscape::Verb::getbyid(verb_name);
+        if (!verb) {
+            g_warning("Unknown verb name: %s", verb_name);
             continue;
         }
+
+        gchar const *keyval_name=iter->attribute("key");
+        if (!keyval_name || !*keyval_name) {
+            // that's ok, it's just listed for reference without assignment, skip it
+            continue;
+        }
+
         guint keyval=gdk_keyval_from_name(keyval_name);
-        if (keyval == GDK_VoidSymbol) {
+        if (keyval == GDK_VoidSymbol || keyval == 0) {
             g_warning("Unknown keyval %s for %s", keyval_name, verb_name);
             continue;
         }
@@ -131,11 +139,11 @@ static void read_shortcuts_file(char const *filename) {
             while (*iter) {
                 size_t length=strcspn(iter, ",");
                 gchar *mod=g_strndup(iter, length);
-                if (!strcmp(mod, "control")) {
+                if (!strcmp(mod, "Control") || !strcmp(mod, "Ctrl")) {
                     modifiers |= SP_SHORTCUT_CONTROL_MASK;
-                } else if (!strcmp(mod, "shift")) {
+                } else if (!strcmp(mod, "Shift")) {
                     modifiers |= SP_SHORTCUT_SHIFT_MASK;
-                } else if (!strcmp(mod, "alt")) {
+                } else if (!strcmp(mod, "Alt")) {
                     modifiers |= SP_SHORTCUT_ALT_MASK;
                 } else {
                     g_warning("Unknown modifier %s for %s", mod, verb_name);
@@ -146,9 +154,7 @@ static void read_shortcuts_file(char const *filename) {
             }
         }
 
-        sp_shortcut_set(keyval | modifiers,
-                        Inkscape::Verb::getbyid(verb_name),
-                        is_primary);
+        sp_shortcut_set(keyval | modifiers, verb, is_primary);
     }
 
     GC::release(doc);
@@ -193,14 +199,41 @@ sp_shortcut_get_verb(unsigned int shortcut)
     return (Inkscape::Verb *)(g_hash_table_lookup(verbs, GINT_TO_POINTER(shortcut)));
 }
 
-unsigned int
-sp_shortcut_get_primary(Inkscape::Verb *verb)
+unsigned int sp_shortcut_get_primary(Inkscape::Verb *verb)
 {
-    if (!primary_shortcuts) sp_shortcut_init();
-    return (unsigned int)GPOINTER_TO_INT(g_hash_table_lookup(primary_shortcuts,
-                                                             (gpointer)(verb)));
+    unsigned int result = GDK_VoidSymbol;
+    if (!primary_shortcuts) {
+        sp_shortcut_init();
+    }
+    gpointer value = 0;
+    if (g_hash_table_lookup_extended(primary_shortcuts, static_cast<gpointer>(verb), NULL, &value)) {
+        result = static_cast<unsigned int>(GPOINTER_TO_INT(value));
+    }
+    return result;
 }
 
+gchar *sp_shortcut_get_label(unsigned int shortcut)
+{
+    // The comment below was copied from the function sp_ui_shortcut_string in interface.cpp (which was subsequently removed)
+    /* TODO: This function shouldn't exist.  Our callers should use GtkAccelLabel instead of
+     * a generic GtkLabel containing this string, and should call gtk_widget_add_accelerator.
+     * Will probably need to change sp_shortcut_invoke callers.
+     *
+     * The existing gtk_label_new_with_mnemonic call can be replaced with
+     * g_object_new(GTK_TYPE_ACCEL_LABEL, NULL) followed by
+     * gtk_label_set_text_with_mnemonic(lbl, str).
+     */
+    gchar *result = 0;
+    if (shortcut != GDK_VoidSymbol) {
+        result = gtk_accelerator_get_label(
+            shortcut & (~SP_SHORTCUT_MODIFIER_MASK), static_cast<GdkModifierType>(
+                ((shortcut & SP_SHORTCUT_SHIFT_MASK) ? GDK_SHIFT_MASK : 0) |
+                ((shortcut & SP_SHORTCUT_CONTROL_MASK) ? GDK_CONTROL_MASK : 0) |
+                ((shortcut & SP_SHORTCUT_ALT_MASK) ? GDK_MOD1_MASK : 0)
+                ));
+    }
+    return result;
+}
 
 /*
   Local Variables:
@@ -211,4 +244,4 @@ sp_shortcut_get_primary(Inkscape::Verb *verb)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :