2 Copyright (C) 2002 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.
21 #include <sigc++/bind.h>
22 #include "pbd/error.h"
23 #include "pbd/stacktrace.h"
25 #include "ardour/playlist.h"
27 #include "region_view.h"
28 #include "selection.h"
29 #include "selection_templates.h"
30 #include "time_axis_view.h"
31 #include "automation_time_axis.h"
32 #include "public_editor.h"
37 using namespace ARDOUR;
41 struct AudioRangeComparator {
42 bool operator()(AudioRange a, AudioRange b) {
43 return a.start < b.start;
48 Selection::operator= (const Selection& other)
51 regions = other.regions;
52 tracks = other.tracks;
60 operator== (const Selection& a, const Selection& b)
62 return a.regions == b.regions &&
63 a.tracks == b.tracks &&
64 a.time.track == b.time.track &&
65 a.time.group == b.time.group &&
68 a.playlists == b.playlists;
71 /** Clear everything from the Selection */
84 Selection::dump_region_layers()
86 cerr << "region selection layer dump" << endl;
87 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
88 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
94 Selection::clear_regions ()
96 if (!regions.empty()) {
103 Selection::clear_tracks ()
105 if (!tracks.empty()) {
112 Selection::clear_time ()
122 Selection::clear_playlists ()
124 /* Selections own their playlists */
126 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
127 /* selections own their own regions, which are copies of the "originals". make them go away */
128 (*i)->drop_regions ();
132 if (!playlists.empty()) {
139 Selection::clear_lines ()
141 if (!lines.empty()) {
148 Selection::clear_markers ()
150 if (!markers.empty()) {
157 Selection::toggle (boost::shared_ptr<Playlist> pl)
159 PlaylistSelection::iterator i;
161 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
163 playlists.push_back(pl);
172 Selection::toggle (const list<TimeAxisView*>& track_list)
174 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
180 Selection::toggle (TimeAxisView* track)
182 TrackSelection::iterator i;
184 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
185 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
186 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
187 tracks.push_back (track);
196 Selection::toggle (RegionView* r)
198 RegionSelection::iterator i;
200 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
210 Selection::toggle (vector<RegionView*>& r)
212 RegionSelection::iterator i;
214 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
215 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
226 Selection::toggle (nframes_t start, nframes_t end)
228 AudioRangeComparator cmp;
230 /* XXX this implementation is incorrect */
232 time.push_back (AudioRange (start, end, next_time_id++));
238 return next_time_id - 1;
242 Selection::add (boost::shared_ptr<Playlist> pl)
244 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
246 playlists.push_back(pl);
252 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
254 bool changed = false;
256 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
257 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
259 playlists.push_back (*i);
270 Selection::add (const list<TimeAxisView*>& track_list)
272 bool changed = false;
274 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
275 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
276 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
277 (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
278 tracks.push_back (*i);
289 Selection::add (TimeAxisView* track)
291 if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
292 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
293 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
294 tracks.push_back (track);
300 Selection::add (vector<RegionView*>& v)
302 /* XXX This method or the add (const RegionSelection&) needs to go
305 bool changed = false;
307 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
308 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
309 changed = regions.add ((*i));
310 if (Config->get_link_region_and_track_selection() && changed) {
311 add (&(*i)->get_trackview());
322 Selection::add (const RegionSelection& rs)
324 /* XXX This method or the add (const vector<RegionView*>&) needs to go
327 bool changed = false;
329 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
330 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
331 changed = regions.add ((*i));
332 if (Config->get_link_region_and_track_selection() && changed) {
333 add (&(*i)->get_trackview());
339 select_edit_group_regions ();
345 Selection::add (RegionView* r)
347 if (find (regions.begin(), regions.end(), r) == regions.end()) {
349 if (Config->get_link_region_and_track_selection()) {
350 add (&r->get_trackview());
357 Selection::add (nframes_t start, nframes_t end)
359 AudioRangeComparator cmp;
361 /* XXX this implementation is incorrect */
363 time.push_back (AudioRange (start, end, next_time_id++));
369 return next_time_id - 1;
373 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
375 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
376 if ((*i).id == sid) {
378 time.push_back (AudioRange(start,end, sid));
380 /* don't consolidate here */
383 AudioRangeComparator cmp;
393 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
395 boost::shared_ptr<ARDOUR::AutomationList> al
396 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
398 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
402 if (find (lines.begin(), lines.end(), al) == lines.end()) {
403 lines.push_back (al);
409 Selection::remove (TimeAxisView* track)
411 list<TimeAxisView*>::iterator i;
412 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
419 Selection::remove (const list<TimeAxisView*>& track_list)
421 bool changed = false;
423 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
425 list<TimeAxisView*>::iterator x;
427 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
439 Selection::remove (boost::shared_ptr<Playlist> track)
441 list<boost::shared_ptr<Playlist> >::iterator i;
442 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
449 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
451 bool changed = false;
453 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
455 list<boost::shared_ptr<Playlist> >::iterator x;
457 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
469 Selection::remove (RegionView* r)
471 if (regions.remove (r)) {
475 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
476 remove (&r->get_trackview());
482 Selection::remove (uint32_t selection_id)
488 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
489 if ((*i).id == selection_id) {
499 Selection::remove (nframes_t start, nframes_t end)
504 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
506 AutomationSelection::iterator i;
507 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
514 Selection::set (TimeAxisView* track)
521 Selection::set (const list<TimeAxisView*>& track_list)
528 Selection::set (boost::shared_ptr<Playlist> playlist)
535 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
542 Selection::set (const RegionSelection& rs)
546 RegionsChanged(); /* EMIT SIGNAL */
550 Selection::set (RegionView* r, bool also_clear_tracks)
553 if (also_clear_tracks) {
560 Selection::set (vector<RegionView*>& v)
563 if (Config->get_link_region_and_track_selection()) {
565 // make sure to deselect any automation selections
572 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
574 if ((start == 0 && end == 0) || end < start) {
579 time.push_back (AudioRange (start, end, next_time_id++));
581 /* reuse the first entry, and remove all the rest */
583 while (time.size() > 1) {
586 time.front().start = start;
587 time.front().end = end;
592 time.group = track->edit_group();
602 return time.front().id;
606 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
613 Selection::selected (Marker* m)
615 return find (markers.begin(), markers.end(), m) != markers.end();
619 Selection::selected (TimeAxisView* tv)
621 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
625 Selection::selected (RegionView* rv)
627 return find (regions.begin(), regions.end(), rv) != regions.end();
633 return regions.empty () &&
636 playlists.empty () &&
639 playlists.empty () &&
645 Selection::toggle (const vector<AutomationSelectable*>& autos)
647 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
648 if ((*x)->get_selected()) {
651 points.push_back (**x);
657 PointsChanged (); /* EMIT SIGNAL */
661 Selection::toggle (list<Selectable*>& selectables)
664 AutomationSelectable* as;
665 vector<RegionView*> rvs;
666 vector<AutomationSelectable*> autos;
668 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
669 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
671 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
672 autos.push_back (as);
674 fatal << _("programming error: ")
675 << X_("unknown selectable type passed to Selection::toggle()")
685 if (!autos.empty()) {
691 Selection::set (list<Selectable*>& selectables)
700 Selection::add (list<Selectable*>& selectables)
703 AutomationSelectable* as;
704 vector<RegionView*> rvs;
705 vector<AutomationSelectable*> autos;
707 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
708 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
710 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
711 autos.push_back (as);
713 fatal << _("programming error: ")
714 << X_("unknown selectable type passed to Selection::add()")
724 if (!autos.empty()) {
730 Selection::clear_points ()
732 if (!points.empty()) {
739 Selection::add (vector<AutomationSelectable*>& autos)
741 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
742 points.push_back (**i);
749 Selection::select_edit_group_regions ()
751 std::set<RegionView*> regions_to_add;
753 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
754 vector<RegionView*> e;
755 editor->get_equivalent_regions (*i, e);
756 for (vector<RegionView*>::iterator j = e.begin(); j != e.end(); ++j) {
757 regions_to_add.insert(*j);
761 for (std::set<RegionView*>::iterator i = regions_to_add.begin(); i != regions_to_add.end(); ++i) {
767 Selection::set (Marker* m)
774 Selection::toggle (Marker* m)
776 MarkerSelection::iterator i;
778 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
786 Selection::remove (Marker* m)
788 MarkerSelection::iterator i;
790 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
797 Selection::add (Marker* m)
799 if (find (markers.begin(), markers.end(), m) == markers.end()) {
801 /* disambiguate which remove() for the compiler */
803 void (Selection::*pmf)(Marker*) = &Selection::remove;
805 m->GoingAway.connect (bind (mem_fun (*this, pmf), m));
807 markers.push_back (m);
813 Selection::add (const list<Marker*>& m)
815 markers.insert (markers.end(), m.begin(), m.end());
820 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
825 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
827 if ((*i)->position() < s) {
828 s = (*i)->position();
831 if ((*i)->position() > e) {
832 e = (*i)->position();