extend Gtkmm2ext::Bindings system to handle mouse buttons as well as keyboard events
authorPaul Davis <paul@linuxaudiosystems.com>
Fri, 4 Mar 2011 03:10:17 +0000 (03:10 +0000)
committerPaul Davis <paul@linuxaudiosystems.com>
Fri, 4 Mar 2011 03:10:17 +0000 (03:10 +0000)
git-svn-id: svn://localhost/ardour2/branches/3.0@9062 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/gtkmm2ext/bindings.cc
libs/gtkmm2ext/gtkmm2ext/bindings.h

index 64bdd5203ae0ac71e24446abcea6952fb81398e5..6002f16371814dd9d12f07c10ef069bc805196d1 100644 (file)
@@ -1,5 +1,9 @@
 #include <iostream>
+
 #include "pbd/xml++.h"
+#include "pbd/convert.h"
+
+#include "gtkmm2ext/actions.h"
 #include "gtkmm2ext/bindings.h"
 #include "gtkmm2ext/keyboard.h"
 
@@ -10,11 +14,100 @@ using namespace Glib;
 using namespace Gtk;
 using namespace Gtkmm2ext;
 
-uint32_t KeyboardKey::_ignored_state = 0;
+uint32_t Bindings::_ignored_state = 0;
+
+MouseButton::MouseButton (uint32_t state, uint32_t keycode)
+{
+        uint32_t ignore = Bindings::ignored_state();
+        
+        if (gdk_keyval_is_upper (keycode) && gdk_keyval_is_lower (keycode)) {
+                /* key is not subject to case, so ignore SHIFT
+                 */
+                ignore |= GDK_SHIFT_MASK;
+        }
+
+        _val = (state & ~ignore);
+        _val <<= 32;
+        _val |= keycode;
+};
+
+bool
+MouseButton::make_button (const string& str, MouseButton& b)
+{
+        int s = 0;
+
+        if (str.find ("Primary") != string::npos) {
+                s |= Keyboard::PrimaryModifier;
+        }
+
+        if (str.find ("Secondary") != string::npos) {
+                s |= Keyboard::SecondaryModifier;
+        }
+
+        if (str.find ("Tertiary") != string::npos) {
+                s |= Keyboard::TertiaryModifier;
+        }
+
+        if (str.find ("Level4") != string::npos) {
+                s |= Keyboard::Level4Modifier;
+        }
+
+        string::size_type lastmod = str.find_last_of ('-');
+        uint32_t button_number;
+        
+        if (lastmod == string::npos) {
+                button_number = PBD::atoi (str);
+        } else {
+                button_number = PBD::atoi (str.substr (lastmod+1));
+        }
+
+        b = MouseButton (s, button_number);
+        return true;
+}
+
+string
+MouseButton::name () const
+{
+        int s = state();
+        
+        string str;
+
+        if (s & Keyboard::PrimaryModifier) {
+                str += "Primary";
+        } 
+        if (s & Keyboard::SecondaryModifier) {
+                if (!str.empty()) {
+                        str += '-';
+                }
+                str += "Secondary";
+        }
+        if (s & Keyboard::TertiaryModifier) {
+                if (!str.empty()) {
+                        str += '-';
+                }
+                str += "Tertiary";
+        } 
+        if (s & Keyboard::Level4Modifier) {
+                if (!str.empty()) {
+                        str += '-';
+                }
+                str += "Level4";
+        }
+        
+        if (!str.empty()) {
+                str += '-';
+        }
+
+        char buf[16];
+        snprintf (buf, sizeof (buf), "%u", button());
+        str += buf;
+
+        return str;
+}
 
 KeyboardKey::KeyboardKey (uint32_t state, uint32_t keycode)
 {
-        uint32_t ignore = _ignored_state;
+        uint32_t ignore = Bindings::ignored_state();
         
         if (gdk_keyval_is_upper (keycode) && gdk_keyval_is_lower (keycode)) {
                 /* key is not subject to case, so ignore SHIFT
@@ -122,15 +215,15 @@ Bindings::set_action_map (ActionMap& am)
 }
 
 bool
-Bindings::activate (KeyboardKey kb, KeyboardKey::Operation op)
+Bindings::activate (KeyboardKey kb, Operation op)
 {
         KeybindingMap* kbm;
 
         switch (op) {
-        case KeyboardKey::Press:
+        case Press:
                 kbm = &press_bindings;
                 break;
-        case KeyboardKey::Release:
+        case Release:
                 kbm = &release_bindings;
                 break;
         }
@@ -149,15 +242,15 @@ Bindings::activate (KeyboardKey kb, KeyboardKey::Operation op)
 }
 
 void
-Bindings::add (KeyboardKey kb, KeyboardKey::Operation op, RefPtr<Action> what)
+Bindings::add (KeyboardKey kb, Operation op, RefPtr<Action> what)
 {
         KeybindingMap* kbm;
 
         switch (op) {
-        case KeyboardKey::Press:
+        case Press:
                 kbm = &press_bindings;
                 break;
-        case KeyboardKey::Release:
+        case Release:
                 kbm = &release_bindings;
                 break;
         }
@@ -167,22 +260,22 @@ Bindings::add (KeyboardKey kb, KeyboardKey::Operation op, RefPtr<Action> what)
         if (k == kbm->end()) {
                 pair<KeyboardKey,RefPtr<Action> > newpair (kb, what);
                 kbm->insert (newpair);
-                cerr << "Bindings added " << kb.key() << " w/ " << kb.state() << endl;
+                cerr << "Bindings added " << kb.key() << " w/ " << kb.state() << " => " << what->get_name() << endl;
         } else {
                 k->second = what;
         }
 }
 
 void
-Bindings::remove (KeyboardKey kb, KeyboardKey::Operation op)
+Bindings::remove (KeyboardKey kb, Operation op)
 {
         KeybindingMap* kbm;
 
         switch (op) {
-        case KeyboardKey::Press:
+        case Press:
                 kbm = &press_bindings;
                 break;
-        case KeyboardKey::Release:
+        case Release:
                 kbm = &release_bindings;
                 break;
         }
@@ -194,41 +287,141 @@ Bindings::remove (KeyboardKey kb, KeyboardKey::Operation op)
         }
 }
 
+bool
+Bindings::activate (MouseButton bb, Operation op)
+{
+        MouseButtonBindingMap* bbm;
+
+        switch (op) {
+        case Press:
+                bbm = &button_press_bindings;
+                break;
+        case Release:
+                bbm = &button_release_bindings;
+                break;
+        }
+
+        MouseButtonBindingMap::iterator b = bbm->find (bb);
+        
+        if (b == bbm->end()) {
+                /* no entry for this key in the state map */
+                return false;
+        }
+
+        /* lets do it ... */
+
+        b->second->activate ();
+        return true;
+}
+
+void
+Bindings::add (MouseButton bb, Operation op, RefPtr<Action> what)
+{
+        MouseButtonBindingMap* bbm;
+
+        switch (op) {
+        case Press:
+                bbm = &button_press_bindings;
+                break;
+        case Release:
+                bbm = &button_release_bindings;
+                break;
+        }
+
+        MouseButtonBindingMap::iterator b = bbm->find (bb);
+
+        if (b == bbm->end()) {
+                pair<MouseButton,RefPtr<Action> > newpair (bb, what);
+                bbm->insert (newpair);
+                cerr << "Bindings added mouse button " << bb.button() << " w/ " << bb.state() << " => " << what->get_name() << endl;
+        } else {
+                b->second = what;
+        }
+}
+
+void
+Bindings::remove (MouseButton bb, Operation op)
+{
+        MouseButtonBindingMap* bbm;
+
+        switch (op) {
+        case Press:
+                bbm = &button_press_bindings;
+                break;
+        case Release:
+                bbm = &button_release_bindings;
+                break;
+        }
+        
+        MouseButtonBindingMap::iterator b = bbm->find (bb);
+
+        if (b != bbm->end()) {
+                bbm->erase (b);
+        }
+}
+
 bool
 Bindings::save (const string& path)
 {
         XMLTree tree;
         XMLNode* root = new XMLNode (X_("Bindings"));
         tree.set_root (root);
+        
+        save (*root);
+
+        if (!tree.write (path)) {
+                ::unlink (path.c_str());
+                return false;
+        }
+
+        return true;
+}
 
+void
+Bindings::save (XMLNode& root)
+{
         XMLNode* presses = new XMLNode (X_("Press"));
-        root->add_child_nocopy (*presses);
+        root.add_child_nocopy (*presses);
 
         for (KeybindingMap::iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
                 XMLNode* child;
                 child = new XMLNode (X_("Binding"));
                 child->add_property (X_("key"), k->first.name());
-                child->add_property (X_("action"), k->second->get_name());
+                string ap = k->second->get_accel_path();
+                child->add_property (X_("action"), ap.substr (ap.find ('/') + 1));
+                presses->add_child_nocopy (*child);
+        }
+
+        for (MouseButtonBindingMap::iterator k = button_press_bindings.begin(); k != button_press_bindings.end(); ++k) {
+                XMLNode* child;
+                child = new XMLNode (X_("Binding"));
+                child->add_property (X_("button"), k->first.name());
+                string ap = k->second->get_accel_path();
+                child->add_property (X_("action"), ap.substr (ap.find ('/') + 1));
                 presses->add_child_nocopy (*child);
         }
 
         XMLNode* releases = new XMLNode (X_("Release"));
-        root->add_child_nocopy (*releases);
+        root.add_child_nocopy (*releases);
 
         for (KeybindingMap::iterator k = release_bindings.begin(); k != release_bindings.end(); ++k) {
                 XMLNode* child;
                 child = new XMLNode (X_("Binding"));
                 child->add_property (X_("key"), k->first.name());
-                child->add_property (X_("action"), k->second->get_name());
+                string ap = k->second->get_accel_path();
+                child->add_property (X_("action"), ap.substr (ap.find ('/') + 1));
                 releases->add_child_nocopy (*child);
         }
 
-        if (!tree.write (path)) {
-                ::unlink (path.c_str());
-                return false;
+        for (MouseButtonBindingMap::iterator k = button_release_bindings.begin(); k != button_release_bindings.end(); ++k) {
+                XMLNode* child;
+                child = new XMLNode (X_("Binding"));
+                child->add_property (X_("button"), k->first.name());
+                string ap = k->second->get_accel_path();
+                child->add_property (X_("action"), ap.substr (ap.find ('/') + 1));
+                releases->add_child_nocopy (*child);
         }
 
-        return true;
 }
 
 bool
@@ -251,50 +444,76 @@ Bindings::load (const string& path)
         const XMLNodeList& children (root.children());
 
         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
+                load (**i);
+        }
 
-                if ((*i)->name() == X_("Press") || (*i)->name() == X_("Release")) {
-
-                        KeyboardKey::Operation op;
+        return true;
+}
 
-                        if ((*i)->name() == X_("Press")) {
-                                op = KeyboardKey::Press;
-                        } else {
-                                op = KeyboardKey::Release;
-                        }
+void
+Bindings::load (const XMLNode& node)
+{
+        if (node.name() == X_("Press") || node.name() == X_("Release")) {
+                
+                Operation op;
+                
+                if (node.name() == X_("Press")) {
+                        op = Press;
+                } else {
+                        op = Release;
+                }
+                
+                const XMLNodeList& children (node.children());
+                
+                for (XMLNodeList::const_iterator p = children.begin(); p != children.end(); ++p) {
                         
-                        const XMLNodeList& gchildren ((*i)->children());
-
-                        for (XMLNodeList::const_iterator p = gchildren.begin(); p != gchildren.end(); ++p) {
+                        XMLProperty* ap;
+                        XMLProperty* kp;
+                        XMLProperty* bp;
+                        
+                        ap = (*p)->property ("action");
+                        kp = (*p)->property ("key");
+                        bp = (*p)->property ("button");
+                        
+                        if (!ap || (!kp && !bp)) {
+                                continue;
+                        }
 
-                                XMLProperty* ap;
-                                XMLProperty* kp;
+                        RefPtr<Action> act;
 
-                                ap = (*p)->property ("action");
-                                kp = (*p)->property ("key");
+                        if (action_map) {
+                                act = action_map->find_action (ap->value());
+                        } 
 
-                                if (!ap || !kp) {
-                                        continue;
+                        if (!act) {
+                                string::size_type slash = ap->value().find ('/');
+                                if (slash != string::npos) {
+                                        string group = ap->value().substr (0, slash);
+                                        string action = ap->value().substr (slash+1);
+                                        act = ActionManager::get_action (group.c_str(), action.c_str());
                                 }
-
-                                RefPtr<Action> act = action_map->find_action (ap->value());
-                                
-                                if (!act) {
-                                        continue;
-                                }
-
+                        }
+                        
+                        if (!act) {
+                                continue;
+                        }
+                        
+                        if (kp) {
                                 KeyboardKey k;
-                                
                                 if (!KeyboardKey::make_key (kp->value(), k)) {
                                         continue;
                                 }
-
                                 add (k, op, act);
+                        } else {
+                                MouseButton b;
+                                if (!MouseButton::make_button (bp->value(), b)) {
+                                        continue;
+                                }
+                                add (b, op, act);
                         }
                 }
         }
-
-        return true;
-}
+}        
 
 RefPtr<Action>
 ActionMap::find_action (const string& name)
index 45c3cfd809863a4db99b55a30cafd2fa37a841ff..36da0b6075b3b4d8c45fd3e28048ec3873a5bc6e 100644 (file)
@@ -9,16 +9,13 @@
 #include <gtkmm/radioaction.h>
 #include <gtkmm/toggleaction.h>
 
+class XMLNode;
+
 namespace Gtkmm2ext {
 
 class KeyboardKey
 {
   public:
-        enum Operation { 
-                Press,
-                Release
-        };
-
         KeyboardKey () {
                 _val = GDK_VoidSymbol;
         }
@@ -38,6 +35,31 @@ class KeyboardKey
 
         std::string name() const;
         static bool make_key (const std::string&, KeyboardKey&);
+
+  private:
+        uint64_t _val;
+};
+
+class MouseButton {
+  public:
+        MouseButton () {
+                _val = ~0ULL;
+        }
+
+        MouseButton (uint32_t state, uint32_t button_number);
+        uint32_t state() const { return _val >> 32; }
+        uint32_t button() const { return _val & 0xffff; }
+
+        bool operator<(const MouseButton& other) const {
+                return _val < other._val;
+        }
+
+        bool operator==(const MouseButton& other) const {
+                return _val == other._val;
+        }
+
+        std::string name() const;
+        static bool make_button (const std::string&, MouseButton&);
         static void set_ignored_state (int mask) {
                 _ignored_state = mask;
         }
@@ -70,24 +92,46 @@ class ActionMap {
 
 class Bindings {
   public:
+        enum Operation { 
+                Press,
+                Release
+        };
+        
         Bindings();
         ~Bindings ();
 
-        void add (KeyboardKey, KeyboardKey::Operation, Glib::RefPtr<Gtk::Action>);
-        void remove (KeyboardKey, KeyboardKey::Operation);
-        bool activate (KeyboardKey, KeyboardKey::Operation);
+        void add (KeyboardKey, Operation, Glib::RefPtr<Gtk::Action>);
+        void remove (KeyboardKey, Operation);
+        bool activate (KeyboardKey, Operation);
+
+        void add (MouseButton, Operation, Glib::RefPtr<Gtk::Action>);
+        void remove (MouseButton, Operation);
+        bool activate (MouseButton, Operation);
 
         bool load (const std::string& path);
+        void load (const XMLNode& node);
         bool save (const std::string& path);
+        void save (XMLNode& root);
         
         void set_action_map (ActionMap&);
 
+        static void set_ignored_state (int mask) {
+                _ignored_state = mask;
+        }
+        static uint32_t ignored_state() { return _ignored_state; }
+
   private:
         typedef std::map<KeyboardKey,Glib::RefPtr<Gtk::Action> > KeybindingMap;
+
         KeybindingMap press_bindings;
         KeybindingMap release_bindings;
 
+        typedef std::map<MouseButton,Glib::RefPtr<Gtk::Action> > MouseButtonBindingMap;
+        MouseButtonBindingMap button_press_bindings;
+        MouseButtonBindingMap button_release_bindings;
+
         ActionMap* action_map;
+        static uint32_t _ignored_state;
 };
 
 } // namespace