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/route.h"
28 #include "ardour/route_group.h"
29 #include "ardour/selection.h"
30 #include "ardour/session.h"
31 #include "ardour/stripable.h"
35 using namespace ARDOUR;
39 CoreSelection::send_selection_change ()
42 pc.add (Properties::selected);
43 PresentationInfo::send_static_change (pc);
46 CoreSelection::CoreSelection (Session& s)
52 CoreSelection::~CoreSelection ()
56 template<typename IterTypeCore>
58 CoreSelection::select_adjacent_stripable (bool mixer_order, bool routes_only,
59 IterTypeCore (StripableList::*begin_method)(),
60 IterTypeCore (StripableList::*end_method)())
62 if (_stripables.empty()) {
64 /* Pick first acceptable */
66 StripableList stripables;
67 session.get_stripables (stripables);
68 stripables.sort (ARDOUR::Stripable::Sorter (mixer_order));
70 for (StripableList::iterator s = stripables.begin(); s != stripables.end(); ++s) {
71 if (select_stripable_and_maybe_group (*s, true, routes_only, 0)) {
79 /* fetch the current selection so that we can get the most recently selected */
80 StripableAutomationControls selected;
81 get_stripables (selected);
82 boost::shared_ptr<Stripable> last_selected = selected.back().stripable;
84 /* Get all stripables and sort into the appropriate ordering */
85 StripableList stripables;
86 session.get_stripables (stripables);
87 stripables.sort (ARDOUR::Stripable::Sorter (mixer_order));
90 /* Check for a possible selection-affecting route group */
92 RouteGroup* group = 0;
93 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (last_selected);
95 if (r && r->route_group() && r->route_group()->is_select()) {
96 group = r->route_group();
99 bool select_me = false;
101 for (IterTypeCore i = (stripables.*begin_method)(); i != (stripables.*end_method)(); ++i) {
105 if (!this->selected (*i)) { /* not currently selected */
106 if (select_stripable_and_maybe_group (*i, true, routes_only, group)) {
112 if ((*i) == last_selected) {
117 /* no next/previous, wrap around ... find first usable stripable from
118 * the appropriate end.
121 for (IterTypeCore s = (stripables.*begin_method)(); s != (stripables.*end_method)(); ++s) {
123 r = boost::dynamic_pointer_cast<Route> (*s);
125 /* monitor is never selectable anywhere. for now, anyway */
127 if (!routes_only || r) {
128 if (select_stripable_and_maybe_group (*s, true, routes_only, 0)) {
136 CoreSelection::select_next_stripable (bool mixer_order, bool routes_only)
138 select_adjacent_stripable<StripableList::iterator> (mixer_order, routes_only, &StripableList::begin, &StripableList::end);
142 CoreSelection::select_prev_stripable (bool mixer_order, bool routes_only)
144 select_adjacent_stripable<StripableList::reverse_iterator> (mixer_order, routes_only, &StripableList::rbegin, &StripableList::rend);
149 CoreSelection::select_stripable_and_maybe_group (boost::shared_ptr<Stripable> s, bool with_group, bool routes_only, RouteGroup* not_allowed_in_group)
151 boost::shared_ptr<Route> r;
154 /* no selection of hidden stripables (though they can be selected and
158 if (s->is_hidden()) {
162 /* monitor is never selectable */
164 if (s->is_monitor()) {
168 if ((r = boost::dynamic_pointer_cast<Route> (s))) {
170 /* no selection of inactive routes, though they can be selected
180 if (!not_allowed_in_group || !r->route_group() || r->route_group() != not_allowed_in_group) {
182 if (r->route_group() && r->route_group()->is_select()) {
183 boost::shared_ptr<RouteList> rl = r->route_group()->route_list ();
184 for (RouteList::iterator ri = rl->begin(); ri != rl->end(); ++ri) {
191 /* it is important to make the "primary" stripable being selected the last in this
201 set (s, boost::shared_ptr<AutomationControl>());
205 } else if (!routes_only) {
206 set (s, boost::shared_ptr<AutomationControl>());
214 CoreSelection::toggle (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
216 DEBUG_TRACE (DEBUG::Selection, string_compose ("toggle: s %1 selected %2 c %3 selected %4\n",
217 s, selected (s), c, selected (c)));
218 if ((c && selected (c)) || selected (s)) {
226 CoreSelection::set (StripableList& sl)
229 boost::shared_ptr<AutomationControl> no_control;
231 std::vector<boost::shared_ptr<Stripable> > removed;
234 Glib::Threads::RWLock::WriterLock lm (_lock);
236 removed.reserve (_stripables.size());
238 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
239 boost::shared_ptr<Stripable> sp = session.stripable_by_id ((*x).stripable);
241 removed.push_back (sp);
245 _stripables.clear ();
247 for (StripableList::iterator s = sl.begin(); s != sl.end(); ++s) {
249 SelectedStripable ss (*s, no_control, g_atomic_int_add (&selection_order, 1));
251 if (_stripables.insert (ss).second) {
252 DEBUG_TRACE (DEBUG::Selection, string_compose ("set:added %1 to s/c selection\n", (*s)->name()));
255 DEBUG_TRACE (DEBUG::Selection, string_compose ("%1 already in s/c selection\n", (*s)->name()));
260 if (send || !removed.empty()) {
262 send_selection_change ();
264 /* send per-object signal to notify interested parties
265 the selection status has changed
268 PropertyChange pc (Properties::selected);
270 for (std::vector<boost::shared_ptr<Stripable> >::iterator s = removed.begin(); s != removed.end(); ++s) {
271 (*s)->presentation_info().PropertyChanged (pc);
274 for (StripableList::iterator s = sl.begin(); s != sl.end(); ++s) {
275 (*s)->presentation_info().PropertyChanged (pc);
283 CoreSelection::add (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
288 Glib::Threads::RWLock::WriterLock lm (_lock);
290 SelectedStripable ss (s, c, g_atomic_int_add (&selection_order, 1));
292 if (_stripables.insert (ss).second) {
293 DEBUG_TRACE (DEBUG::Selection, string_compose ("added %1/%2 to s/c selection\n", s->name(), c));
296 DEBUG_TRACE (DEBUG::Selection, string_compose ("%1/%2 already in s/c selection\n", s->name(), c));
301 send_selection_change ();
302 /* send per-object signal to notify interested parties
303 the selection status has changed
306 PropertyChange pc (Properties::selected);
307 s->presentation_info().PropertyChanged (pc);
313 CoreSelection::remove (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
317 Glib::Threads::RWLock::WriterLock lm (_lock);
319 SelectedStripable ss (s, c, 0);
321 SelectedStripables::iterator i = _stripables.find (ss);
323 if (i != _stripables.end()) {
324 _stripables.erase (i);
325 DEBUG_TRACE (DEBUG::Selection, string_compose ("removed %1/%2 from s/c selection\n", s, c));
331 send_selection_change ();
332 /* send per-object signal to notify interested parties
333 the selection status has changed
336 PropertyChange pc (Properties::selected);
337 s->presentation_info().PropertyChanged (pc);
343 CoreSelection::set (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c)
346 Glib::Threads::RWLock::WriterLock lm (_lock);
348 SelectedStripable ss (s, c, g_atomic_int_add (&selection_order, 1));
350 if (_stripables.size() == 1 && _stripables.find (ss) != _stripables.end()) {
354 _stripables.clear ();
355 _stripables.insert (ss);
356 DEBUG_TRACE (DEBUG::Selection, string_compose ("set s/c selection to %1/%2\n", s->name(), c));
359 send_selection_change ();
361 /* send per-object signal to notify interested parties
362 the selection status has changed
365 PropertyChange pc (Properties::selected);
366 s->presentation_info().PropertyChanged (pc);
371 CoreSelection::clear_stripables ()
374 std::vector<boost::shared_ptr<Stripable> > s;
376 DEBUG_TRACE (DEBUG::Selection, "clearing s/c selection\n");
378 Glib::Threads::RWLock::WriterLock lm (_lock);
380 if (!_stripables.empty()) {
382 s.reserve (_stripables.size());
384 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
385 boost::shared_ptr<Stripable> sp = session.stripable_by_id ((*x).stripable);
391 _stripables.clear ();
394 DEBUG_TRACE (DEBUG::Selection, "cleared s/c selection\n");
399 send_selection_change ();
401 PropertyChange pc (Properties::selected);
403 for (std::vector<boost::shared_ptr<Stripable> >::iterator ss = s.begin(); ss != s.end(); ++ss) {
404 (*ss)->presentation_info().PropertyChanged (pc);
411 CoreSelection::selected (boost::shared_ptr<const Stripable> s) const
417 Glib::Threads::RWLock::ReaderLock lm (_lock);
419 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
421 if (!((*x).controllable == 0)) {
422 /* selected automation control */
426 /* stripable itself selected, not just a control belonging to
430 if ((*x).stripable == s->id()) {
439 CoreSelection::selected (boost::shared_ptr<const AutomationControl> c) const
445 Glib::Threads::RWLock::ReaderLock lm (_lock);
447 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
448 if ((*x).controllable == c->id()) {
456 CoreSelection::SelectedStripable::SelectedStripable (boost::shared_ptr<Stripable> s, boost::shared_ptr<AutomationControl> c, int o)
457 : stripable (s ? s->id() : PBD::ID (0))
458 , controllable (c ? c->id() : PBD::ID (0))
463 struct StripableControllerSort {
464 bool operator() (CoreSelection::StripableAutomationControl const &a, CoreSelection::StripableAutomationControl const & b) const {
465 return a.order < b.order;
470 CoreSelection::get_stripables (StripableAutomationControls& sc) const
472 Glib::Threads::RWLock::ReaderLock lm (_lock);
474 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
476 boost::shared_ptr<Stripable> s = session.stripable_by_id ((*x).stripable);
477 boost::shared_ptr<AutomationControl> c;
480 /* some global automation control, not owned by a Stripable */
481 c = session.automation_control_by_id ((*x).controllable);
483 /* automation control owned by a Stripable or one of its children */
484 c = s->automation_control_recurse ((*x).controllable);
488 sc.push_back (StripableAutomationControl (s, c, (*x).order));
492 StripableControllerSort cmp;
493 sort (sc.begin(), sc.end(), cmp);
497 CoreSelection::remove_control_by_id (PBD::ID const & id)
499 Glib::Threads::RWLock::WriterLock lm (_lock);
501 for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
502 if ((*x).controllable == id) {
503 _stripables.erase (x);
510 CoreSelection::remove_stripable_by_id (PBD::ID const & id)
512 Glib::Threads::RWLock::WriterLock lm (_lock);
514 for (SelectedStripables::iterator x = _stripables.begin(); x != _stripables.end(); ) {
515 if ((*x).stripable == id) {
516 _stripables.erase (x++);
517 /* keep going because there may be more than 1 pair of
518 stripable/automation-control in the selection.
527 CoreSelection::get_state (void)
529 XMLNode* node = new XMLNode (X_("Selection"));
531 Glib::Threads::RWLock::WriterLock lm (_lock);
533 for (SelectedStripables::const_iterator x = _stripables.begin(); x != _stripables.end(); ++x) {
534 XMLNode* child = new XMLNode (X_("StripableAutomationControl"));
535 child->set_property (X_("stripable"), (*x).stripable.to_s());
536 child->set_property (X_("control"), (*x).controllable.to_s());
537 child->set_property (X_("order"), (*x).order);
539 node->add_child_nocopy (*child);
545 CoreSelection::set_state (const XMLNode& node, int /* version */)
547 XMLNodeList children (node.children());
548 Glib::Threads::RWLock::WriterLock lm (_lock);
550 _stripables.clear ();
552 for (XMLNodeConstIterator i = children.begin(); i != children.end(); ++i) {
553 if ((*i)->name() != X_("StripableAutomationControl")) {
559 if (!(*i)->get_property (X_("stripable"), s)) {
565 if (!(*i)->get_property (X_("control"), c)) {
571 if (!(*i)->get_property (X_("order"), order)) {
575 SelectedStripable ss (PBD::ID (s), PBD::ID (c), order);
576 _stripables.insert (ss);
583 CoreSelection::selected () const
585 Glib::Threads::RWLock::ReaderLock lm (_lock);
586 return _stripables.size();