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>
27 #include "region_view.h"
28 #include "selection.h"
29 #include "selection_templates.h"
30 #include "time_axis_view.h"
31 #include "automation_time_axis.h"
35 using namespace ARDOUR;
39 struct AudioRangeComparator {
40 bool operator()(AudioRange a, AudioRange b) {
41 return a.start < b.start;
46 Selection::operator= (const Selection& other)
49 regions = other.regions;
50 tracks = other.tracks;
58 operator== (const Selection& a, const Selection& b)
60 return a.regions == b.regions &&
61 a.tracks == b.tracks &&
62 a.time.track == b.time.track &&
63 a.time.group == b.time.group &&
66 a.playlists == b.playlists &&
67 a.redirects == b.redirects;
83 Selection::dump_region_layers()
85 cerr << "region selection layer dump" << endl;
86 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
87 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
93 Selection::clear_redirects ()
95 if (!redirects.empty()) {
102 Selection::clear_regions ()
104 if (!regions.empty()) {
105 regions.clear_all ();
111 Selection::clear_tracks ()
113 if (!tracks.empty()) {
120 Selection::clear_time ()
130 Selection::clear_playlists ()
132 /* Selections own their playlists */
134 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
135 /* selections own their own regions, which are copies of the "originals". make them go away */
136 (*i)->drop_regions ();
140 if (!playlists.empty()) {
147 Selection::clear_lines ()
149 if (!lines.empty()) {
156 Selection::clear_markers ()
158 if (!markers.empty()) {
165 Selection::toggle (boost::shared_ptr<Redirect> r)
167 RedirectSelection::iterator i;
169 if ((i = find (redirects.begin(), redirects.end(), r)) == redirects.end()) {
170 redirects.push_back (r);
179 Selection::toggle (boost::shared_ptr<Playlist> pl)
181 PlaylistSelection::iterator i;
183 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
185 playlists.push_back(pl);
194 Selection::toggle (const list<TimeAxisView*>& track_list)
196 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
202 Selection::toggle (TimeAxisView* track)
204 TrackSelection::iterator i;
206 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
207 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
208 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
209 tracks.push_back (track);
218 Selection::toggle (RegionView* r)
220 RegionSelection::iterator i;
222 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
232 Selection::toggle (vector<RegionView*>& r)
234 RegionSelection::iterator i;
236 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
237 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
248 Selection::toggle (nframes_t start, nframes_t end)
250 AudioRangeComparator cmp;
252 /* XXX this implementation is incorrect */
254 time.push_back (AudioRange (start, end, next_time_id++));
260 return next_time_id - 1;
265 Selection::add (boost::shared_ptr<Redirect> r)
267 if (find (redirects.begin(), redirects.end(), r) == redirects.end()) {
268 redirects.push_back (r);
274 Selection::add (boost::shared_ptr<Playlist> pl)
276 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
278 playlists.push_back(pl);
284 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
286 bool changed = false;
288 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
289 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
291 playlists.push_back (*i);
302 Selection::add (const list<TimeAxisView*>& track_list)
304 bool changed = false;
306 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
307 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
308 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
309 (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
310 tracks.push_back (*i);
321 Selection::add (TimeAxisView* track)
323 if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
324 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
325 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
326 tracks.push_back (track);
332 Selection::add (RegionView* r)
334 if (find (regions.begin(), regions.end(), r) == regions.end()) {
336 add (&r->get_trackview());
342 Selection::add (vector<RegionView*>& v)
344 bool changed = false;
346 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
347 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
348 changed = regions.add ((*i));
350 add (&(*i)->get_trackview());
361 Selection::add (nframes_t start, nframes_t end)
363 AudioRangeComparator cmp;
365 /* XXX this implementation is incorrect */
367 time.push_back (AudioRange (start, end, next_time_id++));
373 return next_time_id - 1;
377 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
379 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
380 if ((*i).id == sid) {
382 time.push_back (AudioRange(start,end, sid));
384 /* don't consolidate here */
387 AudioRangeComparator cmp;
397 Selection::add (AutomationList* ac)
399 if (find (lines.begin(), lines.end(), ac) == lines.end()) {
400 lines.push_back (ac);
406 Selection::remove (boost::shared_ptr<Redirect> r)
408 RedirectSelection::iterator i;
409 if ((i = find (redirects.begin(), redirects.end(), r)) != redirects.end()) {
416 Selection::remove (TimeAxisView* track)
418 list<TimeAxisView*>::iterator i;
419 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
426 Selection::remove (const list<TimeAxisView*>& track_list)
428 bool changed = false;
430 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
432 list<TimeAxisView*>::iterator x;
434 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
446 Selection::remove (boost::shared_ptr<Playlist> track)
448 list<boost::shared_ptr<Playlist> >::iterator i;
449 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
456 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
458 bool changed = false;
460 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
462 list<boost::shared_ptr<Playlist> >::iterator x;
464 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
476 Selection::remove (RegionView* r)
478 if (regions.remove (r)) {
482 if (!regions.involves (r->get_trackview())) {
483 remove (&r->get_trackview());
489 Selection::remove (uint32_t selection_id)
495 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
496 if ((*i).id == selection_id) {
506 Selection::remove (nframes_t start, nframes_t end)
511 Selection::remove (AutomationList *ac)
513 list<AutomationList*>::iterator i;
514 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
521 Selection::set (boost::shared_ptr<Redirect> r)
528 Selection::set (TimeAxisView* track)
535 Selection::set (const list<TimeAxisView*>& track_list)
542 Selection::set (boost::shared_ptr<Playlist> playlist)
549 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
556 Selection::set (RegionView* r)
564 Selection::set (vector<RegionView*>& v)
568 // make sure to deselect any automation selections
574 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
576 if ((start == 0 && end == 0) || end < start) {
581 time.push_back (AudioRange (start, end, next_time_id++));
583 /* reuse the first entry, and remove all the rest */
585 while (time.size() > 1) {
588 time.front().start = start;
589 time.front().end = end;
594 time.group = track->edit_group();
604 return time.front().id;
608 Selection::set (AutomationList *ac)
615 Selection::selected (TimeAxisView* tv)
617 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
621 Selection::selected (RegionView* rv)
623 return find (regions.begin(), regions.end(), rv) != regions.end();
629 return regions.empty () &&
632 playlists.empty () &&
635 playlists.empty () &&
636 redirects.empty () &&
642 Selection::toggle (const vector<AutomationSelectable*>& autos)
644 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
645 if ((*x)->get_selected()) {
648 points.push_back (**x);
654 PointsChanged (); /* EMIT SIGNAL */
658 Selection::toggle (list<Selectable*>& selectables)
661 AutomationSelectable* as;
662 vector<RegionView*> rvs;
663 vector<AutomationSelectable*> autos;
665 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
666 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
668 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
669 autos.push_back (as);
671 fatal << _("programming error: ")
672 << X_("unknown selectable type passed to Selection::toggle()")
682 if (!autos.empty()) {
688 Selection::set (list<Selectable*>& selectables)
697 Selection::add (list<Selectable*>& selectables)
700 AutomationSelectable* as;
701 vector<RegionView*> rvs;
702 vector<AutomationSelectable*> autos;
704 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
705 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
707 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
708 autos.push_back (as);
710 fatal << _("programming error: ")
711 << X_("unknown selectable type passed to Selection::add()")
721 if (!autos.empty()) {
727 Selection::clear_points ()
729 if (!points.empty()) {
736 Selection::add (vector<AutomationSelectable*>& autos)
738 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
739 points.push_back (**i);
746 Selection::set (Marker* m)
753 Selection::toggle (Marker* m)
755 MarkerSelection::iterator i;
757 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
765 Selection::remove (Marker* m)
767 MarkerSelection::iterator i;
769 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
777 Selection::add (Marker* m)
779 if (find (markers.begin(), markers.end(), m) == markers.end()) {
780 markers.push_back (m);