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"
26 #include "ardour/rc_configuration.h"
28 #include "region_view.h"
29 #include "selection.h"
30 #include "selection_templates.h"
31 #include "time_axis_view.h"
32 #include "automation_time_axis.h"
33 #include "public_editor.h"
38 using namespace ARDOUR;
42 struct AudioRangeComparator {
43 bool operator()(AudioRange a, AudioRange b) {
44 return a.start < b.start;
49 Selection::operator= (const Selection& other)
52 regions = other.regions;
53 tracks = other.tracks;
56 midi_regions = other.midi_regions;
57 midi_notes = other.midi_notes;
63 operator== (const Selection& a, const Selection& b)
65 return a.regions == b.regions &&
66 a.tracks == b.tracks &&
67 a.time.track == b.time.track &&
68 a.time.group == b.time.group &&
71 a.playlists == b.playlists &&
72 a.midi_notes == b.midi_notes &&
73 a.midi_regions == b.midi_regions;
76 /** Clear everything from the Selection */
87 clear_midi_regions ();
91 Selection::dump_region_layers()
93 cerr << "region selection layer dump" << endl;
94 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
95 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
101 Selection::clear_regions ()
103 if (!regions.empty()) {
104 regions.clear_all ();
110 Selection::clear_tracks ()
112 if (!tracks.empty()) {
119 Selection::clear_midi_notes ()
121 if (!midi_notes.empty()) {
128 Selection::clear_midi_regions ()
130 if (!midi_regions.empty()) {
131 midi_regions.clear ();
132 MidiRegionsChanged ();
137 Selection::clear_time ()
147 Selection::clear_playlists ()
149 /* Selections own their playlists */
151 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
152 /* selections own their own regions, which are copies of the "originals". make them go away */
153 (*i)->drop_regions ();
157 if (!playlists.empty()) {
164 Selection::clear_lines ()
166 if (!lines.empty()) {
173 Selection::clear_markers ()
175 if (!markers.empty()) {
182 Selection::toggle (boost::shared_ptr<Playlist> pl)
184 PlaylistSelection::iterator i;
186 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
188 playlists.push_back(pl);
197 Selection::toggle (const list<TimeAxisView*>& track_list)
199 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
205 Selection::toggle (TimeAxisView* track)
207 TrackSelection::iterator i;
209 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
210 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
211 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
212 tracks.push_back (track);
221 Selection::toggle (const MidiNoteSelection& midi_note_list)
223 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
229 Selection::toggle (MidiCutBuffer* midi)
231 MidiNoteSelection::iterator i;
233 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
234 midi_notes.push_back (midi);
236 midi_notes.erase (i);
244 Selection::toggle (RegionView* r)
246 RegionSelection::iterator i;
248 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
258 Selection::toggle (MidiRegionView* mrv)
260 MidiRegionSelection::iterator i;
262 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
265 midi_regions.erase (i);
268 MidiRegionsChanged ();
272 Selection::toggle (vector<RegionView*>& r)
274 RegionSelection::iterator i;
276 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
277 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
288 Selection::toggle (nframes_t start, nframes_t end)
290 AudioRangeComparator cmp;
292 /* XXX this implementation is incorrect */
294 time.push_back (AudioRange (start, end, next_time_id++));
300 return next_time_id - 1;
304 Selection::add (boost::shared_ptr<Playlist> pl)
306 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
308 playlists.push_back(pl);
314 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
316 bool changed = false;
318 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
319 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
321 playlists.push_back (*i);
332 Selection::add (const list<TimeAxisView*>& track_list)
334 list<TimeAxisView*> added = tracks.add (track_list);
336 for (list<TimeAxisView*>::const_iterator i = added.begin(); i != added.end(); ++i) {
337 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
338 (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
341 if (!added.empty()) {
347 Selection::add (TimeAxisView* track)
349 if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
350 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
351 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
352 tracks.push_back (track);
358 Selection::add (const MidiNoteSelection& midi_list)
360 const MidiNoteSelection::const_iterator b = midi_list.begin();
361 const MidiNoteSelection::const_iterator e = midi_list.end();
363 if (!midi_list.empty()) {
364 midi_notes.insert (midi_notes.end(), b, e);
370 Selection::add (MidiCutBuffer* midi)
372 if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
373 midi_notes.push_back (midi);
379 Selection::add (vector<RegionView*>& v)
381 /* XXX This method or the add (const RegionSelection&) needs to go
384 bool changed = false;
386 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
387 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
388 changed = regions.add ((*i));
389 if (Config->get_link_region_and_track_selection() && changed) {
390 add (&(*i)->get_trackview());
401 Selection::add (const RegionSelection& rs)
403 /* XXX This method or the add (const vector<RegionView*>&) needs to go
406 bool changed = false;
408 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
409 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
410 changed = regions.add ((*i));
411 if (Config->get_link_region_and_track_selection() && changed) {
412 add (&(*i)->get_trackview());
423 Selection::add (RegionView* r)
425 if (find (regions.begin(), regions.end(), r) == regions.end()) {
427 if (Config->get_link_region_and_track_selection()) {
428 add (&r->get_trackview());
435 Selection::add (MidiRegionView* mrv)
437 if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
438 midi_regions.push_back (mrv);
439 /* XXX should we do this? */
441 if (Config->get_link_region_and_track_selection()) {
442 add (&mrv->get_trackview());
445 MidiRegionsChanged ();
450 Selection::add (nframes_t start, nframes_t end)
452 AudioRangeComparator cmp;
454 /* XXX this implementation is incorrect */
456 time.push_back (AudioRange (start, end, next_time_id++));
462 return next_time_id - 1;
466 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
468 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
469 if ((*i).id == sid) {
471 time.push_back (AudioRange(start,end, sid));
473 /* don't consolidate here */
476 AudioRangeComparator cmp;
486 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
488 boost::shared_ptr<ARDOUR::AutomationList> al
489 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
491 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
495 if (find (lines.begin(), lines.end(), al) == lines.end()) {
496 lines.push_back (al);
502 Selection::remove (TimeAxisView* track)
504 list<TimeAxisView*>::iterator i;
505 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
512 Selection::remove (const list<TimeAxisView*>& track_list)
514 bool changed = false;
516 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
518 list<TimeAxisView*>::iterator x;
520 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
532 Selection::remove (const MidiNoteSelection& midi_list)
534 bool changed = false;
536 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
538 MidiNoteSelection::iterator x;
540 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
541 midi_notes.erase (x);
552 Selection::remove (MidiCutBuffer* midi)
554 MidiNoteSelection::iterator x;
556 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
557 midi_notes.erase (x);
563 Selection::remove (boost::shared_ptr<Playlist> track)
565 list<boost::shared_ptr<Playlist> >::iterator i;
566 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
573 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
575 bool changed = false;
577 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
579 list<boost::shared_ptr<Playlist> >::iterator x;
581 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
593 Selection::remove (RegionView* r)
595 if (regions.remove (r)) {
599 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
600 remove (&r->get_trackview());
605 Selection::remove (MidiRegionView* mrv)
607 MidiRegionSelection::iterator x;
609 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
610 midi_regions.erase (x);
611 MidiRegionsChanged ();
615 /* XXX fix this up ? */
616 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
617 remove (&r->get_trackview());
624 Selection::remove (uint32_t selection_id)
630 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
631 if ((*i).id == selection_id) {
641 Selection::remove (nframes_t /*start*/, nframes_t /*end*/)
646 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
648 AutomationSelection::iterator i;
649 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
656 Selection::set (TimeAxisView* track)
663 Selection::set (const list<TimeAxisView*>& track_list)
670 Selection::set (const MidiNoteSelection& midi_list)
677 Selection::set (boost::shared_ptr<Playlist> playlist)
684 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
691 Selection::set (const RegionSelection& rs)
695 RegionsChanged(); /* EMIT SIGNAL */
699 Selection::set (MidiRegionView* mrv)
701 clear_midi_regions ();
706 Selection::set (RegionView* r, bool also_clear_tracks)
709 if (also_clear_tracks) {
716 Selection::set (vector<RegionView*>& v)
719 if (Config->get_link_region_and_track_selection()) {
721 // make sure to deselect any automation selections
728 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
730 if ((start == 0 && end == 0) || end < start) {
735 time.push_back (AudioRange (start, end, next_time_id++));
737 /* reuse the first entry, and remove all the rest */
739 while (time.size() > 1) {
742 time.front().start = start;
743 time.front().end = end;
748 time.group = track->route_group();
758 return time.front().id;
762 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
769 Selection::selected (Marker* m)
771 return find (markers.begin(), markers.end(), m) != markers.end();
775 Selection::selected (TimeAxisView* tv)
777 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
781 Selection::selected (RegionView* rv)
783 return find (regions.begin(), regions.end(), rv) != regions.end();
787 Selection::empty (bool internal_selection)
789 bool object_level_empty = regions.empty () &&
792 playlists.empty () &&
795 playlists.empty () &&
800 if (!internal_selection) {
801 return object_level_empty;
804 /* this is intended to really only apply when using a Selection
808 return object_level_empty && midi_notes.empty();
812 Selection::toggle (const vector<AutomationSelectable*>& autos)
814 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
815 if ((*x)->get_selected()) {
818 points.push_back (**x);
824 PointsChanged (); /* EMIT SIGNAL */
828 Selection::toggle (list<Selectable*>& selectables)
831 AutomationSelectable* as;
832 vector<RegionView*> rvs;
833 vector<AutomationSelectable*> autos;
835 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
836 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
838 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
839 autos.push_back (as);
841 fatal << _("programming error: ")
842 << X_("unknown selectable type passed to Selection::toggle()")
852 if (!autos.empty()) {
858 Selection::set (list<Selectable*>& selectables)
867 Selection::add (list<Selectable*>& selectables)
870 AutomationSelectable* as;
871 vector<RegionView*> rvs;
872 vector<AutomationSelectable*> autos;
874 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
875 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
877 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
878 autos.push_back (as);
880 fatal << _("programming error: ")
881 << X_("unknown selectable type passed to Selection::add()")
891 if (!autos.empty()) {
897 Selection::clear_points ()
899 if (!points.empty()) {
906 Selection::add (vector<AutomationSelectable*>& autos)
908 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
909 points.push_back (**i);
916 Selection::set (Marker* m)
923 Selection::toggle (Marker* m)
925 MarkerSelection::iterator i;
927 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
935 Selection::remove (Marker* m)
937 MarkerSelection::iterator i;
939 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
946 Selection::add (Marker* m)
948 if (find (markers.begin(), markers.end(), m) == markers.end()) {
950 /* disambiguate which remove() for the compiler */
952 void (Selection::*pmf)(Marker*) = &Selection::remove;
954 m->GoingAway.connect (bind (mem_fun (*this, pmf), m));
956 markers.push_back (m);
962 Selection::add (const list<Marker*>& m)
964 markers.insert (markers.end(), m.begin(), m.end());
969 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
974 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
976 if ((*i)->position() < s) {
977 s = (*i)->position();
980 if ((*i)->position() > e) {
981 e = (*i)->position();