X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fgtkmm2ext%2Fbindings.cc;h=f96bd586d985ec6d7cbcb35957291885df535b08;hb=59dc0881fbad8440cfbfdb63d33943d76531cf9d;hp=1e8ef03fa025113ec2043a2a8970a8515f558be4;hpb=e2965cd138b31e27dc027bb674585ae93ffdc055;p=ardour.git diff --git a/libs/gtkmm2ext/bindings.cc b/libs/gtkmm2ext/bindings.cc index 1e8ef03fa0..f96bd586d9 100644 --- a/libs/gtkmm2ext/bindings.cc +++ b/libs/gtkmm2ext/bindings.cc @@ -1,5 +1,30 @@ +/* + Copyright (C) 2012 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + #include + +#include + #include "pbd/xml++.h" +#include "pbd/convert.h" + +#include "gtkmm2ext/actions.h" #include "gtkmm2ext/bindings.h" #include "gtkmm2ext/keyboard.h" @@ -10,6 +35,113 @@ using namespace Glib; using namespace Gtk; using namespace Gtkmm2ext; +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 = 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; +}; + + string KeyboardKey::name () const { @@ -104,15 +236,15 @@ Bindings::set_action_map (ActionMap& am) } bool -Bindings::activate (KeyboardKey kb, KeyboardKey::Operation op) +Bindings::activate (KeyboardKey kb, Operation op) { - KeybindingMap* kbm; + KeybindingMap* kbm = 0; switch (op) { - case KeyboardKey::Press: + case Press: kbm = &press_bindings; break; - case KeyboardKey::Release: + case Release: kbm = &release_bindings; break; } @@ -126,21 +258,20 @@ Bindings::activate (KeyboardKey kb, KeyboardKey::Operation op) /* lets do it ... */ - cerr << "Firing up " << k->second->get_name() << endl; k->second->activate (); return true; } void -Bindings::add (KeyboardKey kb, KeyboardKey::Operation op, RefPtr what) +Bindings::add (KeyboardKey kb, Operation op, RefPtr what) { - KeybindingMap* kbm; + KeybindingMap* kbm = 0; switch (op) { - case KeyboardKey::Press: + case Press: kbm = &press_bindings; break; - case KeyboardKey::Release: + case Release: kbm = &release_bindings; break; } @@ -150,21 +281,22 @@ Bindings::add (KeyboardKey kb, KeyboardKey::Operation op, RefPtr what) if (k == kbm->end()) { pair > newpair (kb, what); kbm->insert (newpair); + // 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; + KeybindingMap* kbm = 0; switch (op) { - case KeyboardKey::Press: + case Press: kbm = &press_bindings; break; - case KeyboardKey::Release: + case Release: kbm = &release_bindings; break; } @@ -176,30 +308,141 @@ Bindings::remove (KeyboardKey kb, KeyboardKey::Operation op) } } +bool +Bindings::activate (MouseButton bb, Operation op) +{ + MouseButtonBindingMap* bbm = 0; + + 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 what) +{ + MouseButtonBindingMap* bbm = 0; + + 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 > 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 = 0; + + 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)) { + ::g_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); } - if (!tree.write (path)) { - ::unlink (path.c_str()); - return false; + 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); + + 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()); + string ap = k->second->get_accel_path(); + child->add_property (X_("action"), ap.substr (ap.find ('/') + 1)); + releases->add_child_nocopy (*child); + } + + 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 @@ -208,12 +451,10 @@ Bindings::load (const string& path) XMLTree tree; if (!action_map) { - cerr << "No action map to load bindings with!\n"; return false; } if (!tree.read (path)) { - cerr << "Cannot load XML file @ " << path << endl; return false; } @@ -223,59 +464,77 @@ Bindings::load (const string& path) XMLNode& root (*tree.root()); const XMLNodeList& children (root.children()); - cerr << "check the " << children.size() << " children\n"; - for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) { + load (**i); + } - cerr << "child name: " << (*i)->name() << endl; - - 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* ap; + XMLProperty* kp; + XMLProperty* bp; + + ap = (*p)->property ("action"); + kp = (*p)->property ("key"); + bp = (*p)->property ("button"); + + if (!ap || (!kp && !bp)) { + continue; + } - ap = (*p)->property ("action"); - kp = (*p)->property ("key"); + RefPtr act; - if (!ap || !kp) { - continue; - } + if (action_map) { + act = action_map->find_action (ap->value()); + } - cerr << "key = " << kp->value () << " action = " << ap->value() << endl; - - RefPtr act = action_map->find_action (ap->value()); - - if (!act) { - 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()); } - + } + + if (!act) { + continue; + } + + if (kp) { KeyboardKey k; - if (!KeyboardKey::make_key (kp->value(), k)) { continue; } - - cerr << "binding " << act->get_name() << " to " << k.name() << endl; - add (k, op, act); + } else { + MouseButton b; + if (!MouseButton::make_button (bp->value(), b)) { + continue; + } + add (b, op, act); } } } - - return true; -} +} RefPtr ActionMap::find_action (const string& name) @@ -304,32 +563,33 @@ ActionMap::register_action (const char* path, fullpath += name; actions.insert (_ActionMap::value_type (fullpath, act)); - cerr << "Registered action @ " << fullpath << endl; return act; } RefPtr ActionMap::register_radio_action (const char* path, Gtk::RadioAction::Group& rgroup, - const char* name, const char* label, sigc::slot sl) + const char* name, const char* label, + sigc::slot sl, + int value) { string fullpath; RefPtr act = RadioAction::create (rgroup, name, label); + RefPtr ract = RefPtr::cast_dynamic(act); + ract->property_value() = value; - act->signal_activate().connect (sl); + act->signal_activate().connect (sigc::bind (sl, act->gobj())); fullpath = path; fullpath += '/'; fullpath += name; actions.insert (_ActionMap::value_type (fullpath, act)); - cerr << "Registered action @ " << fullpath << endl; - return act; } RefPtr -ActionMap::register_toggle_action (const char*path, +ActionMap::register_toggle_action (const char* path, const char* name, const char* label, sigc::slot sl) { string fullpath; @@ -343,7 +603,5 @@ ActionMap::register_toggle_action (const char*path, fullpath += name; actions.insert (_ActionMap::value_type (fullpath, act)); - cerr << "Registered action @ " << fullpath << endl; - return act; }