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 "gui_thread.h"
29 #include "midi_cut_buffer.h"
30 #include "region_view.h"
31 #include "selection.h"
32 #include "selection_templates.h"
33 #include "time_axis_view.h"
34 #include "automation_time_axis.h"
35 #include "public_editor.h"
40 using namespace ARDOUR;
43 struct AudioRangeComparator {
44 bool operator()(AudioRange a, AudioRange b) {
45 return a.start < b.start;
51 Selection::operator= (const Selection& other)
54 regions = other.regions;
55 tracks = other.tracks;
58 midi_regions = other.midi_regions;
59 midi_notes = other.midi_notes;
66 operator== (const Selection& a, const Selection& b)
68 return a.regions == b.regions &&
69 a.tracks == b.tracks &&
72 a.playlists == b.playlists &&
73 a.midi_notes == b.midi_notes &&
74 a.midi_regions == b.midi_regions;
77 /** Clear everything from the Selection */
88 clear_midi_regions ();
92 Selection::dump_region_layers()
94 cerr << "region selection layer dump" << endl;
95 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
96 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
102 Selection::clear_regions ()
104 if (!regions.empty()) {
105 regions.clear_all ();
111 Selection::clear_tracks ()
113 if (!tracks.empty()) {
120 Selection::clear_midi_notes ()
122 if (!midi_notes.empty()) {
123 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
132 Selection::clear_midi_regions ()
134 if (!midi_regions.empty()) {
135 midi_regions.clear ();
136 MidiRegionsChanged ();
141 Selection::clear_time ()
149 Selection::clear_playlists ()
151 /* Selections own their playlists */
153 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
154 /* selections own their own regions, which are copies of the "originals". make them go away */
155 (*i)->drop_regions ();
159 if (!playlists.empty()) {
166 Selection::clear_lines ()
168 if (!lines.empty()) {
175 Selection::clear_markers ()
177 if (!markers.empty()) {
184 Selection::toggle (boost::shared_ptr<Playlist> pl)
186 PlaylistSelection::iterator i;
188 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
190 playlists.push_back(pl);
199 Selection::toggle (const TrackViewList& track_list)
201 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
207 Selection::toggle (TimeAxisView* track)
209 TrackSelection::iterator i;
211 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
212 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
213 track->GoingAway.connect (*this, boost::bind (pmf, this, track), gui_context());
214 tracks.push_back (track);
223 Selection::toggle (const MidiNoteSelection& midi_note_list)
225 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
231 Selection::toggle (MidiCutBuffer* midi)
233 MidiNoteSelection::iterator i;
235 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
236 midi_notes.push_back (midi);
238 /* remember that we own the MCB */
240 midi_notes.erase (i);
248 Selection::toggle (RegionView* r)
250 RegionSelection::iterator i;
252 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
262 Selection::toggle (MidiRegionView* mrv)
264 MidiRegionSelection::iterator i;
266 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
269 midi_regions.erase (i);
272 MidiRegionsChanged ();
276 Selection::toggle (vector<RegionView*>& r)
278 RegionSelection::iterator i;
280 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
281 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
292 Selection::toggle (nframes_t start, nframes_t end)
294 AudioRangeComparator cmp;
296 /* XXX this implementation is incorrect */
298 time.push_back (AudioRange (start, end, next_time_id++));
304 return next_time_id - 1;
308 Selection::add (boost::shared_ptr<Playlist> pl)
310 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
312 playlists.push_back(pl);
318 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
320 bool changed = false;
322 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
323 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
325 playlists.push_back (*i);
336 Selection::add (const TrackViewList& track_list)
338 TrackViewList added = tracks.add (track_list);
340 for (list<TimeAxisView*>::const_iterator i = added.begin(); i != added.end(); ++i) {
341 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
342 (*i)->GoingAway.connect (*this, boost::bind (pmf, this, (*i)), gui_context());
345 if (!added.empty()) {
351 Selection::add (TimeAxisView* track)
354 tr.push_back (track);
359 Selection::add (const MidiNoteSelection& midi_list)
361 const MidiNoteSelection::const_iterator b = midi_list.begin();
362 const MidiNoteSelection::const_iterator e = midi_list.end();
364 if (!midi_list.empty()) {
365 midi_notes.insert (midi_notes.end(), b, e);
371 Selection::add (MidiCutBuffer* midi)
373 /* we take ownership of the MCB */
375 if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
376 midi_notes.push_back (midi);
382 Selection::add (vector<RegionView*>& v)
384 /* XXX This method or the add (const RegionSelection&) needs to go
387 bool changed = false;
389 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
390 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
391 changed = regions.add ((*i));
392 if (Config->get_link_region_and_track_selection() && changed) {
393 add (&(*i)->get_trackview());
404 Selection::add (const RegionSelection& rs)
406 /* XXX This method or the add (const vector<RegionView*>&) needs to go
409 bool changed = false;
411 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
412 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
413 changed = regions.add ((*i));
414 if (Config->get_link_region_and_track_selection() && changed) {
415 add (&(*i)->get_trackview());
426 Selection::add (RegionView* r)
428 if (find (regions.begin(), regions.end(), r) == regions.end()) {
430 if (Config->get_link_region_and_track_selection()) {
431 add (&r->get_trackview());
438 Selection::add (MidiRegionView* mrv)
440 if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
441 midi_regions.push_back (mrv);
442 /* XXX should we do this? */
444 if (Config->get_link_region_and_track_selection()) {
445 add (&mrv->get_trackview());
448 MidiRegionsChanged ();
453 Selection::add (nframes_t start, nframes_t end)
455 AudioRangeComparator cmp;
457 /* XXX this implementation is incorrect */
459 time.push_back (AudioRange (start, end, next_time_id++));
465 return next_time_id - 1;
469 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
471 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
472 if ((*i).id == sid) {
474 time.push_back (AudioRange(start,end, sid));
476 /* don't consolidate here */
479 AudioRangeComparator cmp;
489 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
491 boost::shared_ptr<ARDOUR::AutomationList> al
492 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
494 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
498 if (find (lines.begin(), lines.end(), al) == lines.end()) {
499 lines.push_back (al);
505 Selection::remove (TimeAxisView* track)
507 list<TimeAxisView*>::iterator i;
508 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
515 Selection::remove (const TrackViewList& track_list)
517 bool changed = false;
519 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
521 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
522 if (x != tracks.end()) {
534 Selection::remove (const MidiNoteSelection& midi_list)
536 bool changed = false;
538 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
540 MidiNoteSelection::iterator x;
542 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
543 midi_notes.erase (x);
554 Selection::remove (MidiCutBuffer* midi)
556 MidiNoteSelection::iterator x;
558 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
559 /* remember that we own the MCB */
561 midi_notes.erase (x);
567 Selection::remove (boost::shared_ptr<Playlist> track)
569 list<boost::shared_ptr<Playlist> >::iterator i;
570 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
577 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
579 bool changed = false;
581 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
583 list<boost::shared_ptr<Playlist> >::iterator x;
585 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
597 Selection::remove (RegionView* r)
599 if (regions.remove (r)) {
603 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
604 remove (&r->get_trackview());
609 Selection::remove (MidiRegionView* mrv)
611 MidiRegionSelection::iterator x;
613 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
614 midi_regions.erase (x);
615 MidiRegionsChanged ();
619 /* XXX fix this up ? */
620 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
621 remove (&r->get_trackview());
628 Selection::remove (uint32_t selection_id)
634 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
635 if ((*i).id == selection_id) {
645 Selection::remove (nframes_t /*start*/, nframes_t /*end*/)
650 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
652 AutomationSelection::iterator i;
653 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
660 Selection::set (TimeAxisView* track)
667 Selection::set (const TrackViewList& track_list)
674 Selection::set (const MidiNoteSelection& midi_list)
681 Selection::set (boost::shared_ptr<Playlist> playlist)
688 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
695 Selection::set (const RegionSelection& rs)
699 RegionsChanged(); /* EMIT SIGNAL */
703 Selection::set (MidiRegionView* mrv)
705 clear_midi_regions ();
710 Selection::set (RegionView* r, bool also_clear_tracks)
713 if (also_clear_tracks) {
720 Selection::set (vector<RegionView*>& v)
723 if (Config->get_link_region_and_track_selection()) {
725 // make sure to deselect any automation selections
731 /** Set the start and end time of the time selection, without changing
732 * the list of tracks it applies to.
735 Selection::set (nframes_t start, nframes_t end)
737 if ((start == 0 && end == 0) || end < start) {
742 time.push_back (AudioRange (start, end, next_time_id++));
744 /* reuse the first entry, and remove all the rest */
746 while (time.size() > 1) {
749 time.front().start = start;
750 time.front().end = end;
757 return time.front().id;
761 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
768 Selection::selected (Marker* m)
770 return find (markers.begin(), markers.end(), m) != markers.end();
774 Selection::selected (TimeAxisView* tv)
776 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
780 Selection::selected (RegionView* rv)
782 return find (regions.begin(), regions.end(), rv) != regions.end();
786 Selection::empty (bool internal_selection)
788 bool object_level_empty = regions.empty () &&
791 playlists.empty () &&
794 playlists.empty () &&
799 if (!internal_selection) {
800 return object_level_empty;
803 /* this is intended to really only apply when using a Selection
807 return object_level_empty && midi_notes.empty();
811 Selection::toggle (const vector<AutomationSelectable*>& autos)
813 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
814 if ((*x)->get_selected()) {
817 points.push_back (**x);
823 PointsChanged (); /* EMIT SIGNAL */
827 Selection::toggle (list<Selectable*>& selectables)
830 AutomationSelectable* as;
831 vector<RegionView*> rvs;
832 vector<AutomationSelectable*> autos;
834 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
835 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
837 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
838 autos.push_back (as);
840 fatal << _("programming error: ")
841 << X_("unknown selectable type passed to Selection::toggle()")
851 if (!autos.empty()) {
857 Selection::set (list<Selectable*>& selectables)
866 Selection::add (list<Selectable*>& selectables)
869 AutomationSelectable* as;
870 vector<RegionView*> rvs;
871 vector<AutomationSelectable*> autos;
873 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
874 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
876 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
877 autos.push_back (as);
879 fatal << _("programming error: ")
880 << X_("unknown selectable type passed to Selection::add()")
890 if (!autos.empty()) {
896 Selection::clear_points ()
898 if (!points.empty()) {
905 Selection::add (vector<AutomationSelectable*>& autos)
907 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
908 points.push_back (**i);
915 Selection::set (Marker* m)
922 Selection::toggle (Marker* m)
924 MarkerSelection::iterator i;
926 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
934 Selection::remove (Marker* m)
936 MarkerSelection::iterator i;
938 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
945 Selection::add (Marker* m)
947 if (find (markers.begin(), markers.end(), m) == markers.end()) {
949 /* disambiguate which remove() for the compiler */
951 void (Selection::*pmf)(Marker*) = &Selection::remove;
953 m->GoingAway.connect (*this, boost::bind (pmf, this, m), gui_context());
955 markers.push_back (m);
961 Selection::add (const list<Marker*>& m)
963 markers.insert (markers.end(), m.begin(), m.end());
968 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
973 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
975 if ((*i)->position() < s) {
976 s = (*i)->position();
979 if ((*i)->position() > e) {
980 e = (*i)->position();