2286c8375174e2129c3bd571d7f4d19247332245
[ardour.git] / libs / ardour / selection.cc
1 /*
2   Copyright (C) 2017 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include "pbd/compose.h"
21 #include "pbd/signals.h"
22
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"
28
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 void
33 CoreSelection::send_selection_change ()
34 {
35         PropertyChange pc;
36         pc.add (Properties::selected);
37         PresentationInfo::send_static_change (pc);
38 }
39
40 CoreSelection::CoreSelection (Session& s)
41         : session (s)
42         , selection_order (0)
43 {
44 }
45
46 CoreSelection::~CoreSelection ()
47 {
48 }
49
50 void
51 CoreSelection::toggle (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
52 {
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)) {
56                 remove (s, c);
57         } else {
58                 add (s, c);
59         }
60 }
61
62 void
63 CoreSelection::add (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
64 {
65         bool send = false;
66
67         {
68                 Glib::Threads::RWLock::WriterLock lm (_lock);
69
70                 SelectedStripable ss (s, c, g_atomic_int_add (&selection_order, 1));
71
72                 if (_stripables.insert (ss).second) {
73                         DEBUG_TRACE (DEBUG::Selection, string_compose ("added %1/%2 to s/c selection\n", s->name(), c));
74                         send = true;
75                 } else {
76                         DEBUG_TRACE (DEBUG::Selection, string_compose ("%1/%2 already in s/c selection\n", s->name(), c));
77                 }
78         }
79
80         if (send) {
81                 send_selection_change ();
82         }
83 }
84
85 void
86 CoreSelection::remove (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
87 {
88         bool send = false;
89         {
90                 Glib::Threads::RWLock::WriterLock lm (_lock);
91
92                 SelectedStripable ss (s, c, 0);
93
94                 SelectedStripables::iterator i = _stripables.find (ss);
95
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));
99                         send = true;
100                 }
101         }
102
103         if (send) {
104                 send_selection_change ();
105         }
106 }
107
108 void
109 CoreSelection::set (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
110 {
111         {
112                 Glib::Threads::RWLock::WriterLock lm (_lock);
113
114                 SelectedStripable ss (s, c, g_atomic_int_add (&selection_order, 1));
115
116                 if (_stripables.size() == 1 && _stripables.find (ss) != _stripables.end()) {
117                         return;
118                 }
119
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));
123         }
124
125         send_selection_change ();
126 }
127
128 void
129 CoreSelection::clear_stripables ()
130 {
131         bool send = false;
132
133         DEBUG_TRACE (DEBUG::Selection, "clearing s/c selection\n");
134
135         {
136                 Glib::Threads::RWLock::WriterLock lm (_lock);
137
138                 if (!_stripables.empty()) {
139                         _stripables.clear ();
140                         send = true;
141                         DEBUG_TRACE (DEBUG::Selection, "cleared s/c selection\n");
142                 }
143         }
144
145         if (send) {
146                 send_selection_change ();
147         }
148 }
149
150 bool
151 CoreSelection::selected (boost::shared_ptr<const Stripable> s) const
152 {
153         if (!s) {
154                 return false;
155         }
156
157         Glib::Threads::RWLock::ReaderLock lm (_lock);
158
159         for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
160
161                 if (!((*x).controllable == 0)) {
162                         /* selected automation control */
163                         continue;
164                 }
165
166                 /* stripable itself selected, not just a control belonging to
167                  * it
168                  */
169
170                 if ((*x).stripable == s->id()) {
171                         return true;
172                 }
173         }
174
175         return false;
176 }
177
178 bool
179 CoreSelection::selected (boost::shared_ptr<const AutomationControl> c) const
180 {
181         if (!c) {
182                 return false;
183         }
184
185         Glib::Threads::RWLock::ReaderLock lm (_lock);
186
187         for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
188                 if ((*x).controllable == c->id()) {
189                         return true;
190                 }
191         }
192
193         return false;
194 }
195
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))
199         , order (o)
200 {
201 }
202
203 struct StripableControllerSort {
204         bool operator() (CoreSelection::StripableAutomationControl const &a, CoreSelection::StripableAutomationControl const & b) const {
205                 return a.order < b.order;
206         }
207 };
208
209 void
210 CoreSelection::get_stripables (StripableAutomationControls& sc) const
211 {
212         Glib::Threads::RWLock::ReaderLock lm (_lock);
213
214         for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
215
216                 boost::shared_ptr<Stripable> s = session.stripable_by_id ((*x).stripable);
217                 boost::shared_ptr<AutomationControl> c;
218
219                 if (!s) {
220                         /* some global automation control, not owned by a Stripable */
221                         c = session.automation_control_by_id ((*x).controllable);
222                 } else {
223                         /* automation control owned by a Stripable or one of its children */
224                         c = s->automation_control_recurse ((*x).controllable);
225                 }
226
227                 if (s || c) {
228                         sc.push_back (StripableAutomationControl (s, c, (*x).order));
229                 }
230         }
231
232         StripableControllerSort cmp;
233         sort (sc.begin(), sc.end(), cmp);
234 }
235
236 void
237 CoreSelection::remove_control_by_id (PBD::ID const & id)
238 {
239         Glib::Threads::RWLock::WriterLock lm (_lock);
240
241         for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
242                 if ((*x).controllable == id) {
243                         _stripables.erase (x);
244                         return;
245                 }
246         }
247 }
248
249 void
250 CoreSelection::remove_stripable_by_id (PBD::ID const & id)
251 {
252         Glib::Threads::RWLock::WriterLock lm (_lock);
253
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.
259                         */
260                 } else {
261                         ++x;
262                 }
263         }
264 }
265
266 XMLNode&
267 CoreSelection::get_state (void)
268 {
269         XMLNode* node = new XMLNode (X_("Selection"));
270
271         Glib::Threads::RWLock::WriterLock lm (_lock);
272
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);
278
279                 node->add_child_nocopy (*child);
280         }
281
282         return *node;
283 }
284 int
285 CoreSelection::set_state (const XMLNode& node, int /* version */)
286 {
287         XMLNodeList children (node.children());
288         Glib::Threads::RWLock::WriterLock lm (_lock);
289
290         _stripables.clear ();
291
292         for (XMLNodeConstIterator i = children.begin(); i != children.end(); ++i) {
293                 if ((*i)->name() != X_("StripableAutomationControl")) {
294                         continue;
295                 }
296
297                 std::string s;
298
299                 if (!(*i)->get_property (X_("stripable"), s)) {
300                         continue;
301                 }
302
303                 std::string c;
304
305                 if (!(*i)->get_property (X_("control"), c)) {
306                         continue;
307                 }
308
309                 int order;
310
311                 if (!(*i)->get_property (X_("order"), order)) {
312                         continue;
313                 }
314
315                 SelectedStripable ss (PBD::ID (s), PBD::ID (c), order);
316                 _stripables.insert (ss);
317         }
318
319         std::cerr << "set state: " << _stripables.size() << std::endl;
320
321         return 0;
322 }
323
324 uint32_t
325 CoreSelection::selected () const
326 {
327         Glib::Threads::RWLock::ReaderLock lm (_lock);
328         return _stripables.size();
329 }
330