13
0

some tweaks to Bindings API to allow ::is_bound() to indicate the action path for an existing binding

Plus some minor comment additions and cleanups
This commit is contained in:
Paul Davis 2020-04-12 08:57:17 -06:00
parent 920a77f6af
commit 2084a39168
2 changed files with 27 additions and 9 deletions

View File

@ -583,7 +583,10 @@ bool
Bindings::replace (KeyboardKey kb, Operation op, string const & action_name, bool can_save) Bindings::replace (KeyboardKey kb, Operation op, string const & action_name, bool can_save)
{ {
if (is_registered(op, action_name)) { if (is_registered(op, action_name)) {
remove (op, action_name, can_save); /* never save after the remove, because if can_save is true, we
will save after we add the new binding.
*/
remove (op, action_name, false);
} }
/* XXX need a way to get the old group name */ /* XXX need a way to get the old group name */
@ -608,8 +611,8 @@ Bindings::add (KeyboardKey kb, Operation op, string const& action_name, XMLPrope
(void) kbm.insert (new_pair).first; (void) kbm.insert (new_pair).first;
} }
DEBUG_TRACE (DEBUG::Bindings, string_compose ("add binding between %1 and %2, group [%3]\n", DEBUG_TRACE (DEBUG::Bindings, string_compose ("add binding between %1 (%3) and %2, group [%3]\n",
kb, action_name, (group ? group->value() : string()))); kb, action_name, (group ? group->value() : string()), op));
if (can_save) { if (can_save) {
Keyboard::keybindings_changed (); Keyboard::keybindings_changed ();
@ -624,6 +627,7 @@ Bindings::remove (Operation op, std::string const& action_name, bool can_save)
{ {
bool erased_action = false; bool erased_action = false;
KeybindingMap& kbm = get_keymap (op); KeybindingMap& kbm = get_keymap (op);
for (KeybindingMap::iterator k = kbm.begin(); k != kbm.end(); ++k) { for (KeybindingMap::iterator k = kbm.begin(); k != kbm.end(); ++k) {
if (k->second.action_name == action_name) { if (k->second.action_name == action_name) {
kbm.erase (k); kbm.erase (k);
@ -1054,10 +1058,17 @@ Bindings::associate_all ()
} }
bool bool
Bindings::is_bound (KeyboardKey const& kb, Operation op) const Bindings::is_bound (KeyboardKey const& kb, Operation op, std::string* path) const
{ {
const KeybindingMap& km = get_keymap(op); const KeybindingMap& km = get_keymap(op);
return km.find(kb) != km.end(); KeybindingMap::const_iterator i = km.find (kb);
if (i != km.end()) {
if (path) {
*path = i->second.action_name;
}
return true;
}
return false;
} }
std::string std::string
@ -1065,10 +1076,17 @@ Bindings::bound_name (KeyboardKey const& kb, Operation op) const
{ {
const KeybindingMap& km = get_keymap(op); const KeybindingMap& km = get_keymap(op);
KeybindingMap::const_iterator b = km.find(kb); KeybindingMap::const_iterator b = km.find(kb);
if (b == km.end()) { if (b == km.end()) {
return ""; return string();
} }
return b->second.action->get_label();
if (!b->second.action) {
/* action is looked up lazily, so it may not have been set yet */
b->second.action = ActionManager::get_action (b->second.action_name, false);
}
return b->second.action->get_label ();
} }
bool bool

View File

@ -113,7 +113,7 @@ class LIBGTKMM2EXT_API Bindings {
std::string action_name; std::string action_name;
std::string group_name; /* may be empty */ std::string group_name; /* may be empty */
Glib::RefPtr<Gtk::Action> action; mutable Glib::RefPtr<Gtk::Action> action;
}; };
typedef std::map<KeyboardKey,ActionInfo> KeybindingMap; typedef std::map<KeyboardKey,ActionInfo> KeybindingMap;
@ -140,7 +140,7 @@ class LIBGTKMM2EXT_API Bindings {
void remove (MouseButton, Operation); void remove (MouseButton, Operation);
bool activate (MouseButton, Operation); bool activate (MouseButton, Operation);
bool is_bound (KeyboardKey const&, Operation) const; bool is_bound (KeyboardKey const&, Operation, std::string* path = 0) const;
std::string bound_name (KeyboardKey const&, Operation) const; std::string bound_name (KeyboardKey const&, Operation) const;
bool is_registered (Operation op, std::string const& action_name) const; bool is_registered (Operation op, std::string const& action_name) const;