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 c = session.automation_control_by_id ((*x).controllable);
222 c = s->automation_control ((*x).controllable);
226 sc.push_back (StripableAutomationControl (s, c, (*x).order));
230 StripableControllerSort cmp;
231 sort (sc.begin(), sc.end(), cmp);
235 CoreSelection::remove_control_by_id (PBD::ID const & id)
237 Glib::Threads::RWLock::WriterLock lm (_lock);
239 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
240 if ((*x).controllable == id) {
241 _stripables.erase (x);
248 CoreSelection::remove_stripable_by_id (PBD::ID const & id)
250 Glib::Threads::RWLock::WriterLock lm (_lock);
252 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
253 if ((*x).stripable == id) {
254 _stripables.erase (x);
261 CoreSelection::get_state (void)
263 XMLNode* node = new XMLNode (X_("Selection"));
265 Glib::Threads::RWLock::WriterLock lm (_lock);
267 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
268 XMLNode* child = new XMLNode (X_("StripableAutomationControl"));
269 child->set_property (X_("stripable"), (*x).stripable.to_s());
270 child->set_property (X_("control"), (*x).controllable.to_s());
271 child->set_property (X_("order"), (*x).order);
273 node->add_child_nocopy (*child);
279 CoreSelection::set_state (const XMLNode& node, int /* version */)
281 XMLNodeList children (node.children());
282 Glib::Threads::RWLock::WriterLock lm (_lock);
284 _stripables.clear ();
286 for (XMLNodeConstIterator i = children.begin(); i != children.end(); ++i) {
287 if ((*i)->name() != X_("StripableAutomationControl")) {
293 if (!(*i)->get_property (X_("stripable"), s)) {
299 if (!(*i)->get_property (X_("control"), c)) {
305 if (!(*i)->get_property (X_("order"), order)) {
309 SelectedStripable ss (PBD::ID (s), PBD::ID (c), order);
310 _stripables.insert (ss);
313 std::cerr << "set state: " << _stripables.size() << std::endl;
319 CoreSelection::selected () const
321 Glib::Threads::RWLock::ReaderLock lm (_lock);
322 return _stripables.size();