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));
78 DEBUG_TRACE (DEBUG::Selection, string_compose ("%1/%2 already in s/c selection\n", s->name(), c));
83 send_selection_change ();
84 /* send per-object signal to notify interested parties
85 the selection status has changed
88 PropertyChange pc (Properties::selected);
89 s->presentation_info().PropertyChanged (pc);
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));
113 send_selection_change ();
114 /* send per-object signal to notify interested parties
115 the selection status has changed
118 PropertyChange pc (Properties::selected);
119 s->presentation_info().PropertyChanged (pc);
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));
141 send_selection_change ();
143 /* send per-object signal to notify interested parties
144 the selection status has changed
147 PropertyChange pc (Properties::selected);
148 s->presentation_info().PropertyChanged (pc);
153 CoreSelection::clear_stripables ()
156 std::vector<boost::shared_ptr<Stripable> > s;
158 DEBUG_TRACE (DEBUG::Selection, "clearing s/c selection\n");
160 Glib::Threads::RWLock::WriterLock lm (_lock);
162 if (!_stripables.empty()) {
164 s.reserve (_stripables.size());
166 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
167 boost::shared_ptr<Stripable> sp = session.stripable_by_id ((*x).stripable);
173 _stripables.clear ();
176 DEBUG_TRACE (DEBUG::Selection, "cleared s/c selection\n");
181 send_selection_change ();
183 PropertyChange pc (Properties::selected);
185 for (std::vector<boost::shared_ptr<Stripable> >::iterator ss = s.begin(); ss != s.end(); ++ss) {
186 (*ss)->presentation_info().PropertyChanged (pc);
193 CoreSelection::selected (boost::shared_ptr<const Stripable> s) const
199 Glib::Threads::RWLock::ReaderLock lm (_lock);
201 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
203 if (!((*x).controllable == 0)) {
204 /* selected automation control */
208 /* stripable itself selected, not just a control belonging to
212 if ((*x).stripable == s->id()) {
221 CoreSelection::selected (boost::shared_ptr<const AutomationControl> c) const
227 Glib::Threads::RWLock::ReaderLock lm (_lock);
229 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
230 if ((*x).controllable == c->id()) {
238 CoreSelection::SelectedStripable::SelectedStripable (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c, int o)
239 : stripable (s ? s->id() : PBD::ID (0))
240 , controllable (c ? c->id() : PBD::ID (0))
245 struct StripableControllerSort {
246 bool operator() (CoreSelection::StripableAutomationControl const &a, CoreSelection::StripableAutomationControl const & b) const {
247 return a.order < b.order;
252 CoreSelection::get_stripables (StripableAutomationControls& sc) const
254 Glib::Threads::RWLock::ReaderLock lm (_lock);
256 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
258 boost::shared_ptr<Stripable> s = session.stripable_by_id ((*x).stripable);
259 boost::shared_ptr<AutomationControl> c;
262 /* some global automation control, not owned by a Stripable */
263 c = session.automation_control_by_id ((*x).controllable);
265 /* automation control owned by a Stripable or one of its children */
266 c = s->automation_control_recurse ((*x).controllable);
270 sc.push_back (StripableAutomationControl (s, c, (*x).order));
274 StripableControllerSort cmp;
275 sort (sc.begin(), sc.end(), cmp);
279 CoreSelection::remove_control_by_id (PBD::ID const & id)
281 Glib::Threads::RWLock::WriterLock lm (_lock);
283 for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
284 if ((*x).controllable == id) {
285 _stripables.erase (x);
292 CoreSelection::remove_stripable_by_id (PBD::ID const & id)
294 Glib::Threads::RWLock::WriterLock lm (_lock);
296 for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ) {
297 if ((*x).stripable == id) {
298 _stripables.erase (x++);
299 /* keep going because there may be more than 1 pair of
300 stripable/automation-control in the selection.
309 CoreSelection::get_state (void)
311 XMLNode* node = new XMLNode (X_("Selection"));
313 Glib::Threads::RWLock::WriterLock lm (_lock);
315 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
316 XMLNode* child = new XMLNode (X_("StripableAutomationControl"));
317 child->set_property (X_("stripable"), (*x).stripable.to_s());
318 child->set_property (X_("control"), (*x).controllable.to_s());
319 child->set_property (X_("order"), (*x).order);
321 node->add_child_nocopy (*child);
327 CoreSelection::set_state (const XMLNode& node, int /* version */)
329 XMLNodeList children (node.children());
330 Glib::Threads::RWLock::WriterLock lm (_lock);
332 _stripables.clear ();
334 for (XMLNodeConstIterator i = children.begin(); i != children.end(); ++i) {
335 if ((*i)->name() != X_("StripableAutomationControl")) {
341 if (!(*i)->get_property (X_("stripable"), s)) {
347 if (!(*i)->get_property (X_("control"), c)) {
353 if (!(*i)->get_property (X_("order"), order)) {
357 SelectedStripable ss (PBD::ID (s), PBD::ID (c), order);
358 _stripables.insert (ss);
365 CoreSelection::selected () const
367 Glib::Threads::RWLock::ReaderLock lm (_lock);
368 return _stripables.size();