From 87ed40a855ef082693a4aafc9556c3b42dec0ea0 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 28 Mar 2025 10:35:36 -0600 Subject: [PATCH] design to handle "cloned" bindings/actions --- libs/gtkmm2ext/bindings.cc | 76 +++++++++++++++++++++++------ libs/gtkmm2ext/gtkmm2ext/bindings.h | 16 ++++-- 2 files changed, 74 insertions(+), 18 deletions(-) diff --git a/libs/gtkmm2ext/bindings.cc b/libs/gtkmm2ext/bindings.cc index c7835d7f61..7796e3f3e1 100644 --- a/libs/gtkmm2ext/bindings.cc +++ b/libs/gtkmm2ext/bindings.cc @@ -373,19 +373,21 @@ KeyboardKey::make_key (const string& str, KeyboardKey& k) /*================================= Bindings =================================*/ Bindings::Bindings (std::string const& name) - : _name (name) + : _parent (nullptr) + , _name (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) +Bindings::Bindings (std::string const & name, Bindings & other) + : _parent (&other) + , _name (name) { - relativize (); + PBD::stacktrace (std::cerr, 13); + copy_from_parent (false); + + BindingsChanged.connect_same_thread (bc, std::bind (&Bindings::parent_changed, this, _1)); + bindings.push_back (this); } @@ -536,7 +538,9 @@ void Bindings::relativize () { for (auto & [key,action_info] : press_bindings) { + std::cerr << action_info.action_name << " ---------> "; action_info.action_name = _name + action_info.action_name; + std::cerr << action_info.action_name << std::endl; } for (auto & [key,action_info] : release_bindings) { action_info.action_name = _name + action_info.action_name; @@ -550,13 +554,8 @@ Bindings::relativize () } void -Bindings::associate () +Bindings::associate (bool force) { -#warning find a better solution than this - if (_name == "Editing" || _name == "MIDI") { - return; - } - KeybindingMap::iterator k; for (k = press_bindings.begin(); k != press_bindings.end(); ++k) { @@ -595,6 +594,55 @@ Bindings::dissociate () } } +void +Bindings::copy_from_parent (bool assoc) +{ + assert (_parent); + press_bindings.clear (); + release_bindings.clear (); + + _parent->clone_press (press_bindings); + _parent->clone_release (release_bindings); + + dissociate (); + relativize (); + + if (assoc) { + associate (true); + } +} + +void +Bindings::parent_changed (Bindings* changed) +{ + if (_parent != changed) { + return; + } + + press_bindings.clear(); + release_bindings.clear(); + + copy_from_parent (true); +} + +void +Bindings::clone_press (KeybindingMap& target) const +{ + clone_kbd_bindings (press_bindings, target); +} + +void +Bindings::clone_release (KeybindingMap& target) const +{ + clone_kbd_bindings (release_bindings, target); +} + +void +Bindings::clone_kbd_bindings (KeybindingMap const & src, KeybindingMap& target) const +{ + target = src; +} + void Bindings::push_to_gtk (KeyboardKey kb, RefPtr what) { diff --git a/libs/gtkmm2ext/gtkmm2ext/bindings.h b/libs/gtkmm2ext/gtkmm2ext/bindings.h index 456470769b..03aed4a610 100644 --- a/libs/gtkmm2ext/gtkmm2ext/bindings.h +++ b/libs/gtkmm2ext/gtkmm2ext/bindings.h @@ -118,14 +118,15 @@ 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, Bindings & other); ~Bindings (); std::string const& name() const { return _name; } + Bindings const * parent() const { return _parent; } - void reassociate (); - void associate (); + void associate (bool force = false); void dissociate (); + void reassociate (); bool empty() const; bool empty_keys () const; @@ -178,7 +179,8 @@ class LIBGTKMM2EXT_API Bindings { }; private: - std::string _name; + Bindings * _parent; + std::string _name; KeybindingMap press_bindings; KeybindingMap release_bindings; @@ -193,6 +195,12 @@ class LIBGTKMM2EXT_API Bindings { MouseButtonBindingMap& get_mousemap (Operation op); void relativize (); + void clone_press (KeybindingMap&) const; + void clone_release (KeybindingMap&) const; + void clone_kbd_bindings (KeybindingMap const&, KeybindingMap&) const; + void copy_from_parent (bool associate); + void parent_changed (Bindings*); + PBD::ScopedConnection bc; /* GTK has the following position a Gtk::Action: *