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.
20 #include "pbd/compose.h"
21 #include "pbd/signals.h"
23 #include "ardour/automation_control.h"
24 #include "ardour/debug.h"
25 #include "ardour/selection.h"
26 #include "ardour/session.h"
27 #include "ardour/stripable.h"
29 using namespace ARDOUR;
33 CoreSelection::send_selection_change ()
36 pc.add (Properties::selected);
37 PresentationInfo::send_static_change (pc);
40 CoreSelection::CoreSelection (Session& s)
46 CoreSelection::~CoreSelection ()
51 CoreSelection::toggle (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
53 DEBUG_TRACE (DEBUG::Selection, string_compose ("toggle: s %1 selected %2 c %3 selected %4\n",
54 s, selected (s), c, selected (c)));
55 if ((c && selected (c)) || selected (s)) {
63 CoreSelection::add (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
68 Glib::Threads::RWLock::WriterLock lm (_lock);
70 SelectedStripable ss (s, c, g_atomic_int_add (&selection_order, 1));
72 if (_stripables.insert (ss).second) {
73 DEBUG_TRACE (DEBUG::Selection, string_compose ("added %1/%2 to s/c selection\n", s->name(), c));
76 DEBUG_TRACE (DEBUG::Selection, string_compose ("%1/%2 already in s/c selection\n", s->name(), c));
81 send_selection_change ();
86 CoreSelection::remove (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
90 Glib::Threads::RWLock::WriterLock lm (_lock);
92 SelectedStripable ss (s, c, 0);
94 SelectedStripables::iterator i = _stripables.find (ss);
96 if (i != _stripables.end()) {
97 _stripables.erase (i);
98 DEBUG_TRACE (DEBUG::Selection, string_compose ("removed %1/%2 from s/c selection\n", s, c));
104 send_selection_change ();
109 CoreSelection::set (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
112 Glib::Threads::RWLock::WriterLock lm (_lock);
114 SelectedStripable ss (s, c, g_atomic_int_add (&selection_order, 1));
116 if (_stripables.size() == 1 && _stripables.find (ss) != _stripables.end()) {
120 _stripables.clear ();
121 _stripables.insert (ss);
122 DEBUG_TRACE (DEBUG::Selection, string_compose ("set s/c selection to %1/%2\n", s->name(), c));
125 send_selection_change ();
129 CoreSelection::clear_stripables ()
133 DEBUG_TRACE (DEBUG::Selection, "clearing s/c selection\n");
136 Glib::Threads::RWLock::WriterLock lm (_lock);
138 if (!_stripables.empty()) {
139 _stripables.clear ();
141 DEBUG_TRACE (DEBUG::Selection, "cleared s/c selection\n");
146 send_selection_change ();
151 CoreSelection::selected (boost::shared_ptr<const Stripable> s) const
157 Glib::Threads::RWLock::ReaderLock lm (_lock);
159 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
161 if (!((*x).controllable == 0)) {
162 /* selected automation control */
166 /* stripable itself selected, not just a control belonging to
170 if ((*x).stripable == s->id()) {
179 CoreSelection::selected (boost::shared_ptr<const AutomationControl> c) const
185 Glib::Threads::RWLock::ReaderLock lm (_lock);
187 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
188 if ((*x).controllable == c->id()) {
196 CoreSelection::SelectedStripable::SelectedStripable (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c, int o)
197 : stripable (s ? s->id() : PBD::ID (0))
198 , controllable (c ? c->id() : PBD::ID (0))
203 struct StripableControllerSort {
204 bool operator() (CoreSelection::StripableAutomationControl const &a, CoreSelection::StripableAutomationControl const & b) const {
205 return a.order < b.order;
210 CoreSelection::get_stripables (StripableAutomationControls& sc) const
212 Glib::Threads::RWLock::ReaderLock lm (_lock);
214 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
216 boost::shared_ptr<Stripable> s = session.stripable_by_id ((*x).stripable);
217 boost::shared_ptr<AutomationControl> c;
220 /* some global automation control, not owned by a Stripable */
221 c = session.automation_control_by_id ((*x).controllable);
223 /* automation control owned by a Stripable or one of its children */
224 c = s->automation_control_recurse ((*x).controllable);
228 sc.push_back (StripableAutomationControl (s, c, (*x).order));
232 StripableControllerSort cmp;
233 sort (sc.begin(), sc.end(), cmp);
237 CoreSelection::remove_control_by_id (PBD::ID const & id)
239 Glib::Threads::RWLock::WriterLock lm (_lock);
241 for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
242 if ((*x).controllable == id) {
243 _stripables.erase (x);
250 CoreSelection::remove_stripable_by_id (PBD::ID const & id)
252 Glib::Threads::RWLock::WriterLock lm (_lock);
254 for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ) {
255 if ((*x).stripable == id) {
256 _stripables.erase (x++);
257 /* keep going because there may be more than 1 pair of
258 stripable/automation-control in the selection.
267 CoreSelection::get_state (void)
269 XMLNode* node = new XMLNode (X_("Selection"));
271 Glib::Threads::RWLock::WriterLock lm (_lock);
273 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
274 XMLNode* child = new XMLNode (X_("StripableAutomationControl"));
275 child->set_property (X_("stripable"), (*x).stripable.to_s());
276 child->set_property (X_("control"), (*x).controllable.to_s());
277 child->set_property (X_("order"), (*x).order);
279 node->add_child_nocopy (*child);
285 CoreSelection::set_state (const XMLNode& node, int /* version */)
287 XMLNodeList children (node.children());
288 Glib::Threads::RWLock::WriterLock lm (_lock);
290 _stripables.clear ();
292 for (XMLNodeConstIterator i = children.begin(); i != children.end(); ++i) {
293 if ((*i)->name() != X_("StripableAutomationControl")) {
299 if (!(*i)->get_property (X_("stripable"), s)) {
305 if (!(*i)->get_property (X_("control"), c)) {
311 if (!(*i)->get_property (X_("order"), order)) {
315 SelectedStripable ss (PBD::ID (s), PBD::ID (c), order);
316 _stripables.insert (ss);
319 std::cerr << "set state: " << _stripables.size() << std::endl;
325 CoreSelection::selected () const
327 Glib::Threads::RWLock::ReaderLock lm (_lock);
328 return _stripables.size();