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;
43 struct AudioRangeComparator {
44 bool operator()(AudioRange a, AudioRange b) {
45 return a.start < b.start;
50 Selection::operator= (const Selection& other)
53 regions = other.regions;
54 tracks = other.tracks;
57 midi_regions = other.midi_regions;
58 midi_notes = other.midi_notes;
64 operator== (const Selection& a, const Selection& b)
66 return a.regions == b.regions &&
67 a.tracks == b.tracks &&
68 a.time.track == b.time.track &&
69 a.time.group == b.time.group &&
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 ()
151 Selection::clear_playlists ()
153 /* Selections own their playlists */
155 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
156 /* selections own their own regions, which are copies of the "originals". make them go away */
157 (*i)->drop_regions ();
161 if (!playlists.empty()) {
168 Selection::clear_lines ()
170 if (!lines.empty()) {
177 Selection::clear_markers ()
179 if (!markers.empty()) {
186 Selection::toggle (boost::shared_ptr<Playlist> pl)
188 PlaylistSelection::iterator i;
190 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
192 playlists.push_back(pl);
201 Selection::toggle (const list<TimeAxisView*>& track_list)
203 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
209 Selection::toggle (TimeAxisView* track)
211 TrackSelection::iterator i;
213 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
214 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
215 track->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), track));
216 tracks.push_back (track);
225 Selection::toggle (const MidiNoteSelection& midi_note_list)
227 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
233 Selection::toggle (MidiCutBuffer* midi)
235 MidiNoteSelection::iterator i;
237 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
238 midi_notes.push_back (midi);
240 /* remember that we own the MCB */
242 midi_notes.erase (i);
250 Selection::toggle (RegionView* r)
252 RegionSelection::iterator i;
254 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
264 Selection::toggle (MidiRegionView* mrv)
266 MidiRegionSelection::iterator i;
268 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
271 midi_regions.erase (i);
274 MidiRegionsChanged ();
278 Selection::toggle (vector<RegionView*>& r)
280 RegionSelection::iterator i;
282 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
283 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
294 Selection::toggle (nframes_t start, nframes_t end)
296 AudioRangeComparator cmp;
298 /* XXX this implementation is incorrect */
300 time.push_back (AudioRange (start, end, next_time_id++));
306 return next_time_id - 1;
310 Selection::add (boost::shared_ptr<Playlist> pl)
312 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
314 playlists.push_back(pl);
320 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
322 bool changed = false;
324 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
325 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
327 playlists.push_back (*i);
338 Selection::add (const list<TimeAxisView*>& track_list)
340 list<TimeAxisView*> added = tracks.add (track_list);
342 for (list<TimeAxisView*>::const_iterator i = added.begin(); i != added.end(); ++i) {
343 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
344 (*i)->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), (*i)));
347 if (!added.empty()) {
353 Selection::add (TimeAxisView* track)
355 if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
356 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
357 track->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), track));
358 tracks.push_back (track);
364 Selection::add (const MidiNoteSelection& midi_list)
366 const MidiNoteSelection::const_iterator b = midi_list.begin();
367 const MidiNoteSelection::const_iterator e = midi_list.end();
369 if (!midi_list.empty()) {
370 midi_notes.insert (midi_notes.end(), b, e);
376 Selection::add (MidiCutBuffer* midi)
378 /* we take ownership of the MCB */
380 if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
381 midi_notes.push_back (midi);
387 Selection::add (vector<RegionView*>& v)
389 /* XXX This method or the add (const RegionSelection&) needs to go
392 bool changed = false;
394 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
395 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
396 changed = regions.add ((*i));
397 if (Config->get_link_region_and_track_selection() && changed) {
398 add (&(*i)->get_trackview());
409 Selection::add (const RegionSelection& rs)
411 /* XXX This method or the add (const vector<RegionView*>&) needs to go
414 bool changed = false;
416 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
417 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
418 changed = regions.add ((*i));
419 if (Config->get_link_region_and_track_selection() && changed) {
420 add (&(*i)->get_trackview());
431 Selection::add (RegionView* r)
433 if (find (regions.begin(), regions.end(), r) == regions.end()) {
435 if (Config->get_link_region_and_track_selection()) {
436 add (&r->get_trackview());
443 Selection::add (MidiRegionView* mrv)
445 if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
446 midi_regions.push_back (mrv);
447 /* XXX should we do this? */
449 if (Config->get_link_region_and_track_selection()) {
450 add (&mrv->get_trackview());
453 MidiRegionsChanged ();
458 Selection::add (nframes_t start, nframes_t end)
460 AudioRangeComparator cmp;
462 /* XXX this implementation is incorrect */
464 time.push_back (AudioRange (start, end, next_time_id++));
470 return next_time_id - 1;
474 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
476 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
477 if ((*i).id == sid) {
479 time.push_back (AudioRange(start,end, sid));
481 /* don't consolidate here */
484 AudioRangeComparator cmp;
494 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
496 boost::shared_ptr<ARDOUR::AutomationList> al
497 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
499 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
503 if (find (lines.begin(), lines.end(), al) == lines.end()) {
504 lines.push_back (al);
510 Selection::remove (TimeAxisView* track)
512 list<TimeAxisView*>::iterator i;
513 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
520 Selection::remove (const list<TimeAxisView*>& track_list)
522 bool changed = false;
524 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
526 list<TimeAxisView*>::iterator x;
528 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
540 Selection::remove (const MidiNoteSelection& midi_list)
542 bool changed = false;
544 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
546 MidiNoteSelection::iterator x;
548 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
549 midi_notes.erase (x);
560 Selection::remove (MidiCutBuffer* midi)
562 MidiNoteSelection::iterator x;
564 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
565 /* remember that we own the MCB */
567 midi_notes.erase (x);
573 Selection::remove (boost::shared_ptr<Playlist> track)
575 list<boost::shared_ptr<Playlist> >::iterator i;
576 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
583 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
585 bool changed = false;
587 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
589 list<boost::shared_ptr<Playlist> >::iterator x;
591 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
603 Selection::remove (RegionView* r)
605 if (regions.remove (r)) {
609 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
610 remove (&r->get_trackview());
615 Selection::remove (MidiRegionView* mrv)
617 MidiRegionSelection::iterator x;
619 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
620 midi_regions.erase (x);
621 MidiRegionsChanged ();
625 /* XXX fix this up ? */
626 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
627 remove (&r->get_trackview());
634 Selection::remove (uint32_t selection_id)
640 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
641 if ((*i).id == selection_id) {
651 Selection::remove (nframes_t /*start*/, nframes_t /*end*/)
656 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
658 AutomationSelection::iterator i;
659 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
666 Selection::set (TimeAxisView* track)
673 Selection::set (const list<TimeAxisView*>& track_list)
680 Selection::set (const MidiNoteSelection& midi_list)
687 Selection::set (boost::shared_ptr<Playlist> playlist)
694 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
701 Selection::set (const RegionSelection& rs)
705 RegionsChanged(); /* EMIT SIGNAL */
709 Selection::set (MidiRegionView* mrv)
711 clear_midi_regions ();
716 Selection::set (RegionView* r, bool also_clear_tracks)
719 if (also_clear_tracks) {
726 Selection::set (vector<RegionView*>& v)
729 if (Config->get_link_region_and_track_selection()) {
731 // make sure to deselect any automation selections
738 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
740 if ((start == 0 && end == 0) || end < start) {
745 time.push_back (AudioRange (start, end, next_time_id++));
747 /* reuse the first entry, and remove all the rest */
749 while (time.size() > 1) {
752 time.front().start = start;
753 time.front().end = end;
758 time.group = track->route_group();
768 return time.front().id;
772 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
779 Selection::selected (Marker* m)
781 return find (markers.begin(), markers.end(), m) != markers.end();
785 Selection::selected (TimeAxisView* tv)
787 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
791 Selection::selected (RegionView* rv)
793 return find (regions.begin(), regions.end(), rv) != regions.end();
797 Selection::empty (bool internal_selection)
799 bool object_level_empty = regions.empty () &&
802 playlists.empty () &&
805 playlists.empty () &&
810 if (!internal_selection) {
811 return object_level_empty;
814 /* this is intended to really only apply when using a Selection
818 return object_level_empty && midi_notes.empty();
822 Selection::toggle (const vector<AutomationSelectable*>& autos)
824 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
825 if ((*x)->get_selected()) {
828 points.push_back (**x);
834 PointsChanged (); /* EMIT SIGNAL */
838 Selection::toggle (list<Selectable*>& selectables)
841 AutomationSelectable* as;
842 vector<RegionView*> rvs;
843 vector<AutomationSelectable*> autos;
845 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
846 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
848 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
849 autos.push_back (as);
851 fatal << _("programming error: ")
852 << X_("unknown selectable type passed to Selection::toggle()")
862 if (!autos.empty()) {
868 Selection::set (list<Selectable*>& selectables)
877 Selection::add (list<Selectable*>& selectables)
880 AutomationSelectable* as;
881 vector<RegionView*> rvs;
882 vector<AutomationSelectable*> autos;
884 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
885 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
887 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
888 autos.push_back (as);
890 fatal << _("programming error: ")
891 << X_("unknown selectable type passed to Selection::add()")
901 if (!autos.empty()) {
907 Selection::clear_points ()
909 if (!points.empty()) {
916 Selection::add (vector<AutomationSelectable*>& autos)
918 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
919 points.push_back (**i);
926 Selection::set (Marker* m)
933 Selection::toggle (Marker* m)
935 MarkerSelection::iterator i;
937 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
945 Selection::remove (Marker* m)
947 MarkerSelection::iterator i;
949 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
956 Selection::add (Marker* m)
958 if (find (markers.begin(), markers.end(), m) == markers.end()) {
960 /* disambiguate which remove() for the compiler */
962 void (Selection::*pmf)(Marker*) = &Selection::remove;
964 m->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, pmf), m));
966 markers.push_back (m);
972 Selection::add (const list<Marker*>& m)
974 markers.insert (markers.end(), m.begin(), m.end());
979 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
984 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
986 if ((*i)->position() < s) {
987 s = (*i)->position();
990 if ((*i)->position() > e) {
991 e = (*i)->position();