Limit clock displays to 30days (720hours) by default.
[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                         c = session.automation_control_by_id ((*x).controllable);
221                 } else {
222                         c = s->automation_control ((*x).controllable);
223                 }
224
225                 if (s || c) {
226                         sc.push_back (StripableAutomationControl (s, c, (*x).order));
227                 }
228         }
229
230         StripableControllerSort cmp;
231         sort (sc.begin(), sc.end(), cmp);
232 }
233
234 void
235 CoreSelection::remove_control_by_id (PBD::ID const & id)
236 {
237         Glib::Threads::RWLock::WriterLock lm (_lock);
238
239         for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
240                 if ((*x).controllable == id) {
241                         _stripables.erase (x);
242                         return;
243                 }
244         }
245 }
246
247 void
248 CoreSelection::remove_stripable_by_id (PBD::ID const & id)
249 {
250         Glib::Threads::RWLock::WriterLock lm (_lock);
251
252         for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ) {
253                 if ((*x).stripable == id) {
254                         x = _stripables.erase (x);
255                         /* keep going because there may be more than 1 pair of
256                            stripable/automation-control in the selection.
257                         */
258                 } else {
259                         ++x;
260                 }
261         }
262 }
263
264 XMLNode&
265 CoreSelection::get_state (void)
266 {
267         XMLNode* node = new XMLNode (X_("Selection"));
268
269         Glib::Threads::RWLock::WriterLock lm (_lock);
270
271         for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
272                 XMLNode* child = new XMLNode (X_("StripableAutomationControl"));
273                 child->set_property (X_("stripable"), (*x).stripable.to_s());
274                 child->set_property (X_("control"), (*x).controllable.to_s());
275                 child->set_property (X_("order"), (*x).order);
276
277                 node->add_child_nocopy (*child);
278         }
279
280         return *node;
281 }
282 int
283 CoreSelection::set_state (const XMLNode& node, int /* version */)
284 {
285         XMLNodeList children (node.children());
286         Glib::Threads::RWLock::WriterLock lm (_lock);
287
288         _stripables.clear ();
289
290         for (XMLNodeConstIterator i = children.begin(); i != children.end(); ++i) {
291                 if ((*i)->name() != X_("StripableAutomationControl")) {
292                         continue;
293                 }
294
295                 std::string s;
296
297                 if (!(*i)->get_property (X_("stripable"), s)) {
298                         continue;
299                 }
300
301                 std::string c;
302
303                 if (!(*i)->get_property (X_("control"), c)) {
304                         continue;
305                 }
306
307                 int order;
308
309                 if (!(*i)->get_property (X_("order"), order)) {
310                         continue;
311                 }
312
313                 SelectedStripable ss (PBD::ID (s), PBD::ID (c), order);
314                 _stripables.insert (ss);
315         }
316
317         std::cerr << "set state: " << _stripables.size() << std::endl;
318
319         return 0;
320 }
321
322 uint32_t
323 CoreSelection::selected () const
324 {
325         Glib::Threads::RWLock::ReaderLock lm (_lock);
326         return _stripables.size();
327 }
328