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 &&
69 a.playlists == b.playlists &&
70 a.midi_notes == b.midi_notes &&
71 a.midi_regions == b.midi_regions;
74 /** Clear everything from the Selection */
85 clear_midi_regions ();
89 Selection::dump_region_layers()
91 cerr << "region selection layer dump" << endl;
92 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
93 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
99 Selection::clear_regions ()
101 if (!regions.empty()) {
102 regions.clear_all ();
108 Selection::clear_tracks ()
110 if (!tracks.empty()) {
117 Selection::clear_midi_notes ()
119 if (!midi_notes.empty()) {
120 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
129 Selection::clear_midi_regions ()
131 if (!midi_regions.empty()) {
132 midi_regions.clear ();
133 MidiRegionsChanged ();
138 Selection::clear_time ()
146 Selection::clear_playlists ()
148 /* Selections own their playlists */
150 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
151 /* selections own their own regions, which are copies of the "originals". make them go away */
152 (*i)->drop_regions ();
156 if (!playlists.empty()) {
163 Selection::clear_lines ()
165 if (!lines.empty()) {
172 Selection::clear_markers ()
174 if (!markers.empty()) {
181 Selection::toggle (boost::shared_ptr<Playlist> pl)
183 PlaylistSelection::iterator i;
185 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
187 playlists.push_back(pl);
196 Selection::toggle (const TrackViewList& track_list)
198 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
204 Selection::toggle (TimeAxisView* track)
206 TrackSelection::iterator i;
208 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
209 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
210 track->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), track));
211 tracks.push_back (track);
220 Selection::toggle (const MidiNoteSelection& midi_note_list)
222 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
228 Selection::toggle (MidiCutBuffer* midi)
230 MidiNoteSelection::iterator i;
232 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
233 midi_notes.push_back (midi);
235 /* remember that we own the MCB */
237 midi_notes.erase (i);
245 Selection::toggle (RegionView* r)
247 RegionSelection::iterator i;
249 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
259 Selection::toggle (MidiRegionView* mrv)
261 MidiRegionSelection::iterator i;
263 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
266 midi_regions.erase (i);
269 MidiRegionsChanged ();
273 Selection::toggle (vector<RegionView*>& r)
275 RegionSelection::iterator i;
277 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
278 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
289 Selection::toggle (nframes_t start, nframes_t end)
291 AudioRangeComparator cmp;
293 /* XXX this implementation is incorrect */
295 time.push_back (AudioRange (start, end, next_time_id++));
301 return next_time_id - 1;
305 Selection::add (boost::shared_ptr<Playlist> pl)
307 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
309 playlists.push_back(pl);
315 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
317 bool changed = false;
319 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
320 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
322 playlists.push_back (*i);
333 Selection::add (const TrackViewList& track_list)
335 TrackViewList added = tracks.add (track_list);
337 for (list<TimeAxisView*>::const_iterator i = added.begin(); i != added.end(); ++i) {
338 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
339 (*i)->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), (*i)));
342 if (!added.empty()) {
348 Selection::add (TimeAxisView* track)
351 tr.push_back (track);
356 Selection::add (const MidiNoteSelection& midi_list)
358 const MidiNoteSelection::const_iterator b = midi_list.begin();
359 const MidiNoteSelection::const_iterator e = midi_list.end();
361 if (!midi_list.empty()) {
362 midi_notes.insert (midi_notes.end(), b, e);
368 Selection::add (MidiCutBuffer* midi)
370 /* we take ownership of the MCB */
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 TrackViewList& track_list)
514 bool changed = false;
516 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
518 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
519 if (x != tracks.end()) {
531 Selection::remove (const MidiNoteSelection& midi_list)
533 bool changed = false;
535 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
537 MidiNoteSelection::iterator x;
539 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
540 midi_notes.erase (x);
551 Selection::remove (MidiCutBuffer* midi)
553 MidiNoteSelection::iterator x;
555 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
556 /* remember that we own the MCB */
558 midi_notes.erase (x);
564 Selection::remove (boost::shared_ptr<Playlist> track)
566 list<boost::shared_ptr<Playlist> >::iterator i;
567 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
574 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
576 bool changed = false;
578 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
580 list<boost::shared_ptr<Playlist> >::iterator x;
582 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
594 Selection::remove (RegionView* r)
596 if (regions.remove (r)) {
600 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
601 remove (&r->get_trackview());
606 Selection::remove (MidiRegionView* mrv)
608 MidiRegionSelection::iterator x;
610 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
611 midi_regions.erase (x);
612 MidiRegionsChanged ();
616 /* XXX fix this up ? */
617 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
618 remove (&r->get_trackview());
625 Selection::remove (uint32_t selection_id)
631 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
632 if ((*i).id == selection_id) {
642 Selection::remove (nframes_t /*start*/, nframes_t /*end*/)
647 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
649 AutomationSelection::iterator i;
650 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
657 Selection::set (TimeAxisView* track)
664 Selection::set (const TrackViewList& track_list)
671 Selection::set (const MidiNoteSelection& midi_list)
678 Selection::set (boost::shared_ptr<Playlist> playlist)
685 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
692 Selection::set (const RegionSelection& rs)
696 RegionsChanged(); /* EMIT SIGNAL */
700 Selection::set (MidiRegionView* mrv)
702 clear_midi_regions ();
707 Selection::set (RegionView* r, bool also_clear_tracks)
710 if (also_clear_tracks) {
717 Selection::set (vector<RegionView*>& v)
720 if (Config->get_link_region_and_track_selection()) {
722 // make sure to deselect any automation selections
728 /** Set the start and end time of the time selection, without changing
729 * the list of tracks it applies to.
732 Selection::set (nframes_t start, nframes_t end)
734 if ((start == 0 && end == 0) || end < start) {
739 time.push_back (AudioRange (start, end, next_time_id++));
741 /* reuse the first entry, and remove all the rest */
743 while (time.size() > 1) {
746 time.front().start = start;
747 time.front().end = end;
754 return time.front().id;
758 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
765 Selection::selected (Marker* m)
767 return find (markers.begin(), markers.end(), m) != markers.end();
771 Selection::selected (TimeAxisView* tv)
773 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
777 Selection::selected (RegionView* rv)
779 return find (regions.begin(), regions.end(), rv) != regions.end();
783 Selection::empty (bool internal_selection)
785 bool object_level_empty = regions.empty () &&
788 playlists.empty () &&
791 playlists.empty () &&
796 if (!internal_selection) {
797 return object_level_empty;
800 /* this is intended to really only apply when using a Selection
804 return object_level_empty && midi_notes.empty();
808 Selection::toggle (const vector<AutomationSelectable*>& autos)
810 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
811 if ((*x)->get_selected()) {
814 points.push_back (**x);
820 PointsChanged (); /* EMIT SIGNAL */
824 Selection::toggle (list<Selectable*>& selectables)
827 AutomationSelectable* as;
828 vector<RegionView*> rvs;
829 vector<AutomationSelectable*> autos;
831 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
832 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
834 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
835 autos.push_back (as);
837 fatal << _("programming error: ")
838 << X_("unknown selectable type passed to Selection::toggle()")
848 if (!autos.empty()) {
854 Selection::set (list<Selectable*>& selectables)
863 Selection::add (list<Selectable*>& selectables)
866 AutomationSelectable* as;
867 vector<RegionView*> rvs;
868 vector<AutomationSelectable*> autos;
870 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
871 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
873 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
874 autos.push_back (as);
876 fatal << _("programming error: ")
877 << X_("unknown selectable type passed to Selection::add()")
887 if (!autos.empty()) {
893 Selection::clear_points ()
895 if (!points.empty()) {
902 Selection::add (vector<AutomationSelectable*>& autos)
904 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
905 points.push_back (**i);
912 Selection::set (Marker* m)
919 Selection::toggle (Marker* m)
921 MarkerSelection::iterator i;
923 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
931 Selection::remove (Marker* m)
933 MarkerSelection::iterator i;
935 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
942 Selection::add (Marker* m)
944 if (find (markers.begin(), markers.end(), m) == markers.end()) {
946 /* disambiguate which remove() for the compiler */
948 void (Selection::*pmf)(Marker*) = &Selection::remove;
950 m->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), m));
952 markers.push_back (m);
958 Selection::add (const list<Marker*>& m)
960 markers.insert (markers.end(), m.begin(), m.end());
965 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
970 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
972 if ((*i)->position() < s) {
973 s = (*i)->position();
976 if ((*i)->position() > e) {
977 e = (*i)->position();