2 Copyright (C) 2017 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "pbd/compose.h"
23 #include "pbd/signals.h"
25 #include "ardour/automation_control.h"
26 #include "ardour/debug.h"
27 #include "ardour/selection.h"
28 #include "ardour/session.h"
29 #include "ardour/stripable.h"
31 using namespace ARDOUR;
35 CoreSelection::send_selection_change ()
38 pc.add (Properties::selected);
39 PresentationInfo::send_static_change (pc);
42 CoreSelection::CoreSelection (Session& s)
48 CoreSelection::~CoreSelection ()
53 CoreSelection::toggle (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
55 DEBUG_TRACE (DEBUG::Selection, string_compose ("toggle: s %1 selected %2 c %3 selected %4\n",
56 s, selected (s), c, selected (c)));
57 if ((c && selected (c)) || selected (s)) {
65 CoreSelection::add (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
70 Glib::Threads::RWLock::WriterLock lm (_lock);
72 SelectedStripable ss (s, c, g_atomic_int_add (&selection_order, 1));
74 if (_stripables.insert (ss).second) {
75 DEBUG_TRACE (DEBUG::Selection, string_compose ("added %1/%2 to s/c selection\n", s->name(), c));
76 /* send per-object signal to notify interested parties
77 the selection status has changed
80 PropertyChange pc (Properties::selected);
81 s->PropertyChanged (pc);
85 DEBUG_TRACE (DEBUG::Selection, string_compose ("%1/%2 already in s/c selection\n", s->name(), c));
90 send_selection_change ();
95 CoreSelection::remove (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
99 Glib::Threads::RWLock::WriterLock lm (_lock);
101 SelectedStripable ss (s, c, 0);
103 SelectedStripables::iterator i = _stripables.find (ss);
105 if (i != _stripables.end()) {
106 _stripables.erase (i);
107 DEBUG_TRACE (DEBUG::Selection, string_compose ("removed %1/%2 from s/c selection\n", s, c));
108 /* send per-object signal to notify interested parties
109 the selection status has changed
112 PropertyChange pc (Properties::selected);
113 s->PropertyChanged (pc);
120 send_selection_change ();
125 CoreSelection::set (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
128 Glib::Threads::RWLock::WriterLock lm (_lock);
130 SelectedStripable ss (s, c, g_atomic_int_add (&selection_order, 1));
132 if (_stripables.size() == 1 && _stripables.find (ss) != _stripables.end()) {
136 _stripables.clear ();
137 _stripables.insert (ss);
138 DEBUG_TRACE (DEBUG::Selection, string_compose ("set s/c selection to %1/%2\n", s->name(), c));
139 /* send per-object signal to notify interested parties
140 the selection status has changed
143 PropertyChange pc (Properties::selected);
144 s->PropertyChanged (pc);
148 send_selection_change ();
152 CoreSelection::clear_stripables ()
155 std::vector<boost::shared_ptr<Stripable> > s;
157 DEBUG_TRACE (DEBUG::Selection, "clearing s/c selection\n");
159 Glib::Threads::RWLock::WriterLock lm (_lock);
161 if (!_stripables.empty()) {
163 s.reserve (_stripables.size());
165 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
166 boost::shared_ptr<Stripable> sp = session.stripable_by_id ((*x).stripable);
172 _stripables.clear ();
175 DEBUG_TRACE (DEBUG::Selection, "cleared s/c selection\n");
180 PropertyChange pc (Properties::selected);
182 for (std::vector<boost::shared_ptr<Stripable> >::iterator ss = s.begin(); ss != s.end(); ++ss) {
183 (*ss)->PropertyChanged (pc);
186 send_selection_change ();
191 CoreSelection::selected (boost::shared_ptr<const Stripable> s) const
197 Glib::Threads::RWLock::ReaderLock lm (_lock);
199 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
201 if (!((*x).controllable == 0)) {
202 /* selected automation control */
206 /* stripable itself selected, not just a control belonging to
210 if ((*x).stripable == s->id()) {
219 CoreSelection::selected (boost::shared_ptr<const AutomationControl> c) const
225 Glib::Threads::RWLock::ReaderLock lm (_lock);
227 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
228 if ((*x).controllable == c->id()) {
236 CoreSelection::SelectedStripable::SelectedStripable (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c, int o)
237 : stripable (s ? s->id() : PBD::ID (0))
238 , controllable (c ? c->id() : PBD::ID (0))
243 struct StripableControllerSort {
244 bool operator() (CoreSelection::StripableAutomationControl const &a, CoreSelection::StripableAutomationControl const & b) const {
245 return a.order < b.order;
250 CoreSelection::get_stripables (StripableAutomationControls& sc) const
252 Glib::Threads::RWLock::ReaderLock lm (_lock);
254 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
256 boost::shared_ptr<Stripable> s = session.stripable_by_id ((*x).stripable);
257 boost::shared_ptr<AutomationControl> c;
260 /* some global automation control, not owned by a Stripable */
261 c = session.automation_control_by_id ((*x).controllable);
263 /* automation control owned by a Stripable or one of its children */
264 c = s->automation_control_recurse ((*x).controllable);
268 sc.push_back (StripableAutomationControl (s, c, (*x).order));
272 StripableControllerSort cmp;
273 sort (sc.begin(), sc.end(), cmp);
277 CoreSelection::remove_control_by_id (PBD::ID const & id)
279 Glib::Threads::RWLock::WriterLock lm (_lock);
281 for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
282 if ((*x).controllable == id) {
283 _stripables.erase (x);
290 CoreSelection::remove_stripable_by_id (PBD::ID const & id)
292 Glib::Threads::RWLock::WriterLock lm (_lock);
294 for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ) {
295 if ((*x).stripable == id) {
296 _stripables.erase (x++);
297 /* keep going because there may be more than 1 pair of
298 stripable/automation-control in the selection.
307 CoreSelection::get_state (void)
309 XMLNode* node = new XMLNode (X_("Selection"));
311 Glib::Threads::RWLock::WriterLock lm (_lock);
313 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
314 XMLNode* child = new XMLNode (X_("StripableAutomationControl"));
315 child->set_property (X_("stripable"), (*x).stripable.to_s());
316 child->set_property (X_("control"), (*x).controllable.to_s());
317 child->set_property (X_("order"), (*x).order);
319 node->add_child_nocopy (*child);
325 CoreSelection::set_state (const XMLNode& node, int /* version */)
327 XMLNodeList children (node.children());
328 Glib::Threads::RWLock::WriterLock lm (_lock);
330 _stripables.clear ();
332 for (XMLNodeConstIterator i = children.begin(); i != children.end(); ++i) {
333 if ((*i)->name() != X_("StripableAutomationControl")) {
339 if (!(*i)->get_property (X_("stripable"), s)) {
345 if (!(*i)->get_property (X_("control"), c)) {
351 if (!(*i)->get_property (X_("order"), order)) {
355 SelectedStripable ss (PBD::ID (s), PBD::ID (c), order);
356 _stripables.insert (ss);
359 std::cerr << "set state: " << _stripables.size() << std::endl;
365 CoreSelection::selected () const
367 Glib::Threads::RWLock::ReaderLock lm (_lock);
368 return _stripables.size();