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 "midi_cut_buffer.h"
29 #include "region_view.h"
30 #include "selection.h"
31 #include "selection_templates.h"
32 #include "time_axis_view.h"
33 #include "automation_time_axis.h"
34 #include "public_editor.h"
39 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()) {
122 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
131 Selection::clear_midi_regions ()
133 if (!midi_regions.empty()) {
134 midi_regions.clear ();
135 MidiRegionsChanged ();
140 Selection::clear_time ()
150 Selection::clear_playlists ()
152 /* Selections own their playlists */
154 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
155 /* selections own their own regions, which are copies of the "originals". make them go away */
156 (*i)->drop_regions ();
160 if (!playlists.empty()) {
167 Selection::clear_lines ()
169 if (!lines.empty()) {
176 Selection::clear_markers ()
178 if (!markers.empty()) {
185 Selection::toggle (boost::shared_ptr<Playlist> pl)
187 PlaylistSelection::iterator i;
189 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
191 playlists.push_back(pl);
200 Selection::toggle (const TrackViewList& track_list)
202 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
208 Selection::toggle (TimeAxisView* track)
210 TrackSelection::iterator i;
212 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
213 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
214 track->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), track));
215 tracks.push_back (track);
224 Selection::toggle (const MidiNoteSelection& midi_note_list)
226 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
232 Selection::toggle (MidiCutBuffer* midi)
234 MidiNoteSelection::iterator i;
236 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
237 midi_notes.push_back (midi);
239 /* remember that we own the MCB */
241 midi_notes.erase (i);
249 Selection::toggle (RegionView* r)
251 RegionSelection::iterator i;
253 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
263 Selection::toggle (MidiRegionView* mrv)
265 MidiRegionSelection::iterator i;
267 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
270 midi_regions.erase (i);
273 MidiRegionsChanged ();
277 Selection::toggle (vector<RegionView*>& r)
279 RegionSelection::iterator i;
281 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
282 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
293 Selection::toggle (nframes_t start, nframes_t end)
295 AudioRangeComparator cmp;
297 /* XXX this implementation is incorrect */
299 time.push_back (AudioRange (start, end, next_time_id++));
305 return next_time_id - 1;
309 Selection::add (boost::shared_ptr<Playlist> pl)
311 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
313 playlists.push_back(pl);
319 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
321 bool changed = false;
323 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
324 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
326 playlists.push_back (*i);
337 Selection::add (const TrackViewList& track_list)
339 TrackViewList added = tracks.add (track_list);
341 for (list<TimeAxisView*>::const_iterator i = added.begin(); i != added.end(); ++i) {
342 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
343 (*i)->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), (*i)));
346 if (!added.empty()) {
352 Selection::add (TimeAxisView* track)
355 tr.push_back (track);
360 Selection::add (const MidiNoteSelection& midi_list)
362 const MidiNoteSelection::const_iterator b = midi_list.begin();
363 const MidiNoteSelection::const_iterator e = midi_list.end();
365 if (!midi_list.empty()) {
366 midi_notes.insert (midi_notes.end(), b, e);
372 Selection::add (MidiCutBuffer* midi)
374 /* we take ownership of the MCB */
376 if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
377 midi_notes.push_back (midi);
383 Selection::add (vector<RegionView*>& v)
385 /* XXX This method or the add (const RegionSelection&) needs to go
388 bool changed = false;
390 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
391 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
392 changed = regions.add ((*i));
393 if (Config->get_link_region_and_track_selection() && changed) {
394 add (&(*i)->get_trackview());
405 Selection::add (const RegionSelection& rs)
407 /* XXX This method or the add (const vector<RegionView*>&) needs to go
410 bool changed = false;
412 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
413 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
414 changed = regions.add ((*i));
415 if (Config->get_link_region_and_track_selection() && changed) {
416 add (&(*i)->get_trackview());
427 Selection::add (RegionView* r)
429 if (find (regions.begin(), regions.end(), r) == regions.end()) {
431 if (Config->get_link_region_and_track_selection()) {
432 add (&r->get_trackview());
439 Selection::add (MidiRegionView* mrv)
441 if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
442 midi_regions.push_back (mrv);
443 /* XXX should we do this? */
445 if (Config->get_link_region_and_track_selection()) {
446 add (&mrv->get_trackview());
449 MidiRegionsChanged ();
454 Selection::add (nframes_t start, nframes_t end)
456 AudioRangeComparator cmp;
458 /* XXX this implementation is incorrect */
460 time.push_back (AudioRange (start, end, next_time_id++));
466 return next_time_id - 1;
470 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
472 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
473 if ((*i).id == sid) {
475 time.push_back (AudioRange(start,end, sid));
477 /* don't consolidate here */
480 AudioRangeComparator cmp;
490 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
492 boost::shared_ptr<ARDOUR::AutomationList> al
493 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
495 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
499 if (find (lines.begin(), lines.end(), al) == lines.end()) {
500 lines.push_back (al);
506 Selection::remove (TimeAxisView* track)
508 list<TimeAxisView*>::iterator i;
509 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
516 Selection::remove (const TrackViewList& track_list)
518 bool changed = false;
520 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
522 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
523 if (x != tracks.end()) {
535 Selection::remove (const MidiNoteSelection& midi_list)
537 bool changed = false;
539 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
541 MidiNoteSelection::iterator x;
543 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
544 midi_notes.erase (x);
555 Selection::remove (MidiCutBuffer* midi)
557 MidiNoteSelection::iterator x;
559 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
560 /* remember that we own the MCB */
562 midi_notes.erase (x);
568 Selection::remove (boost::shared_ptr<Playlist> track)
570 list<boost::shared_ptr<Playlist> >::iterator i;
571 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
578 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
580 bool changed = false;
582 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
584 list<boost::shared_ptr<Playlist> >::iterator x;
586 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
598 Selection::remove (RegionView* r)
600 if (regions.remove (r)) {
604 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
605 remove (&r->get_trackview());
610 Selection::remove (MidiRegionView* mrv)
612 MidiRegionSelection::iterator x;
614 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
615 midi_regions.erase (x);
616 MidiRegionsChanged ();
620 /* XXX fix this up ? */
621 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
622 remove (&r->get_trackview());
629 Selection::remove (uint32_t selection_id)
635 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
636 if ((*i).id == selection_id) {
646 Selection::remove (nframes_t /*start*/, nframes_t /*end*/)
651 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
653 AutomationSelection::iterator i;
654 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
661 Selection::set (TimeAxisView* track)
668 Selection::set (const TrackViewList& track_list)
675 Selection::set (const MidiNoteSelection& midi_list)
682 Selection::set (boost::shared_ptr<Playlist> playlist)
689 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
696 Selection::set (const RegionSelection& rs)
700 RegionsChanged(); /* EMIT SIGNAL */
704 Selection::set (MidiRegionView* mrv)
706 clear_midi_regions ();
711 Selection::set (RegionView* r, bool also_clear_tracks)
714 if (also_clear_tracks) {
721 Selection::set (vector<RegionView*>& v)
724 if (Config->get_link_region_and_track_selection()) {
726 // make sure to deselect any automation selections
733 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
735 if ((start == 0 && end == 0) || end < start) {
740 time.push_back (AudioRange (start, end, next_time_id++));
742 /* reuse the first entry, and remove all the rest */
744 while (time.size() > 1) {
747 time.front().start = start;
748 time.front().end = end;
753 time.group = track->route_group();
763 return time.front().id;
767 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
774 Selection::selected (Marker* m)
776 return find (markers.begin(), markers.end(), m) != markers.end();
780 Selection::selected (TimeAxisView* tv)
782 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
786 Selection::selected (RegionView* rv)
788 return find (regions.begin(), regions.end(), rv) != regions.end();
792 Selection::empty (bool internal_selection)
794 bool object_level_empty = regions.empty () &&
797 playlists.empty () &&
800 playlists.empty () &&
805 if (!internal_selection) {
806 return object_level_empty;
809 /* this is intended to really only apply when using a Selection
813 return object_level_empty && midi_notes.empty();
817 Selection::toggle (const vector<AutomationSelectable*>& autos)
819 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
820 if ((*x)->get_selected()) {
823 points.push_back (**x);
829 PointsChanged (); /* EMIT SIGNAL */
833 Selection::toggle (list<Selectable*>& selectables)
836 AutomationSelectable* as;
837 vector<RegionView*> rvs;
838 vector<AutomationSelectable*> autos;
840 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
841 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
843 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
844 autos.push_back (as);
846 fatal << _("programming error: ")
847 << X_("unknown selectable type passed to Selection::toggle()")
857 if (!autos.empty()) {
863 Selection::set (list<Selectable*>& selectables)
872 Selection::add (list<Selectable*>& selectables)
875 AutomationSelectable* as;
876 vector<RegionView*> rvs;
877 vector<AutomationSelectable*> autos;
879 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
880 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
882 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
883 autos.push_back (as);
885 fatal << _("programming error: ")
886 << X_("unknown selectable type passed to Selection::add()")
896 if (!autos.empty()) {
902 Selection::clear_points ()
904 if (!points.empty()) {
911 Selection::add (vector<AutomationSelectable*>& autos)
913 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
914 points.push_back (**i);
921 Selection::set (Marker* m)
928 Selection::toggle (Marker* m)
930 MarkerSelection::iterator i;
932 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
940 Selection::remove (Marker* m)
942 MarkerSelection::iterator i;
944 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
951 Selection::add (Marker* m)
953 if (find (markers.begin(), markers.end(), m) == markers.end()) {
955 /* disambiguate which remove() for the compiler */
957 void (Selection::*pmf)(Marker*) = &Selection::remove;
959 m->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), m));
961 markers.push_back (m);
967 Selection::add (const list<Marker*>& m)
969 markers.insert (markers.end(), m.begin(), m.end());
974 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
979 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
981 if ((*i)->position() < s) {
982 s = (*i)->position();
985 if ((*i)->position() > e) {
986 e = (*i)->position();