From 7d1886b22d892837f293833fd7d5154b3075d45b Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Tue, 11 Mar 2025 13:45:29 -0600 Subject: [PATCH] provide a copy-constructor for Gtkmm2ext::Bindings This copies all the binding information for an existing Bindings object, but then mutates all the action names from foo/bar to name/bar where name is the given name for the new Bindings object. --- libs/gtkmm2ext/bindings.cc | 28 ++++++++++++++++++++++++++++ libs/gtkmm2ext/gtkmm2ext/bindings.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/libs/gtkmm2ext/bindings.cc b/libs/gtkmm2ext/bindings.cc index af5a3739c8..f10dc51b12 100644 --- a/libs/gtkmm2ext/bindings.cc +++ b/libs/gtkmm2ext/bindings.cc @@ -378,6 +378,17 @@ Bindings::Bindings (std::string const& name) bindings.push_back (this); } +Bindings::Bindings (std::string const & name, Bindings const & other) + : _name (name) + , press_bindings (other.press_bindings) + , release_bindings (other.release_bindings) + , button_press_bindings (other.button_press_bindings) + , button_release_bindings (other.button_release_bindings) +{ + relativize (); + bindings.push_back (this); +} + Bindings::~Bindings() { bindings.remove (this); @@ -521,6 +532,23 @@ Bindings::activate (KeyboardKey kb, Operation op) return true; } +void +Bindings::relativize () +{ + for (auto & [key,action_info] : press_bindings) { + action_info.action_name = _name + action_info.action_name.substr (action_info.action_name.find_first_of ('/')); + } + for (auto & [key,action_info] : release_bindings) { + action_info.action_name = _name + action_info.action_name.substr (action_info.action_name.find_first_of ('/')); + } + for (auto & [mb,action_info] : button_press_bindings) { + action_info.action_name = _name + action_info.action_name.substr (action_info.action_name.find_first_of ('/')); + } + for (auto & [mb,action_info] : button_release_bindings) { + action_info.action_name = _name + action_info.action_name.substr (action_info.action_name.find_first_of ('/')); + } +} + void Bindings::associate () { diff --git a/libs/gtkmm2ext/gtkmm2ext/bindings.h b/libs/gtkmm2ext/gtkmm2ext/bindings.h index 1c5869a9a1..dc25297e12 100644 --- a/libs/gtkmm2ext/gtkmm2ext/bindings.h +++ b/libs/gtkmm2ext/gtkmm2ext/bindings.h @@ -118,6 +118,7 @@ class LIBGTKMM2EXT_API Bindings { typedef std::map KeybindingMap; Bindings (std::string const& name); + Bindings (std::string const & name, Bindings const & other); ~Bindings (); std::string const& name() const { return _name; } @@ -191,6 +192,8 @@ class LIBGTKMM2EXT_API Bindings { const KeybindingMap& get_keymap (Operation op) const; MouseButtonBindingMap& get_mousemap (Operation op); + void relativize (); + /* GTK has the following position a Gtk::Action: * * accel_path: /GroupName/ActionName