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"
32 #include "public_editor.h"
36 using namespace ARDOUR;
40 struct AudioRangeComparator {
41 bool operator()(AudioRange a, AudioRange b) {
42 return a.start < b.start;
47 Selection::operator= (const Selection& other)
50 regions = other.regions;
51 tracks = other.tracks;
59 operator== (const Selection& a, const Selection& b)
61 return a.regions == b.regions &&
62 a.tracks == b.tracks &&
63 a.time.track == b.time.track &&
64 a.time.group == b.time.group &&
67 a.playlists == b.playlists;
70 /** Clear everything from the Selection */
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_regions ()
95 if (!regions.empty()) {
102 Selection::clear_tracks ()
104 if (!tracks.empty()) {
111 Selection::clear_time ()
121 Selection::clear_playlists ()
123 /* Selections own their playlists */
125 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
126 /* selections own their own regions, which are copies of the "originals". make them go away */
127 (*i)->drop_regions ();
131 if (!playlists.empty()) {
138 Selection::clear_lines ()
140 if (!lines.empty()) {
147 Selection::clear_markers ()
149 if (!markers.empty()) {
156 Selection::toggle (boost::shared_ptr<Playlist> pl)
158 PlaylistSelection::iterator i;
160 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
162 playlists.push_back(pl);
171 Selection::toggle (const list<TimeAxisView*>& track_list)
173 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
179 Selection::toggle (TimeAxisView* track)
181 TrackSelection::iterator i;
183 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
184 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
185 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
186 tracks.push_back (track);
195 Selection::toggle (RegionView* r)
197 RegionSelection::iterator i;
199 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
209 Selection::toggle (vector<RegionView*>& r)
211 RegionSelection::iterator i;
213 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
214 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
225 Selection::toggle (nframes_t start, nframes_t end)
227 AudioRangeComparator cmp;
229 /* XXX this implementation is incorrect */
231 time.push_back (AudioRange (start, end, next_time_id++));
237 return next_time_id - 1;
241 Selection::add (boost::shared_ptr<Playlist> pl)
243 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
245 playlists.push_back(pl);
251 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
253 bool changed = false;
255 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
256 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
258 playlists.push_back (*i);
269 Selection::add (const list<TimeAxisView*>& track_list)
271 bool changed = false;
273 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
274 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
275 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
276 (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
277 tracks.push_back (*i);
288 Selection::add (TimeAxisView* track)
290 if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
291 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
292 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
293 tracks.push_back (track);
299 Selection::add (RegionView* r)
301 if (find (regions.begin(), regions.end(), r) == regions.end()) {
303 select_edit_group_regions ();
304 add (&r->get_trackview());
310 Selection::add (vector<RegionView*>& v)
312 bool changed = false;
314 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
315 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
316 changed = regions.add ((*i));
318 add (&(*i)->get_trackview());
324 select_edit_group_regions ();
330 Selection::add (nframes_t start, nframes_t end)
332 AudioRangeComparator cmp;
334 /* XXX this implementation is incorrect */
336 time.push_back (AudioRange (start, end, next_time_id++));
342 return next_time_id - 1;
346 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
348 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
349 if ((*i).id == sid) {
351 time.push_back (AudioRange(start,end, sid));
353 /* don't consolidate here */
356 AudioRangeComparator cmp;
366 Selection::add (AutomationList* ac)
368 if (find (lines.begin(), lines.end(), ac) == lines.end()) {
369 lines.push_back (ac);
375 Selection::remove (TimeAxisView* track)
377 list<TimeAxisView*>::iterator i;
378 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
385 Selection::remove (const list<TimeAxisView*>& track_list)
387 bool changed = false;
389 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
391 list<TimeAxisView*>::iterator x;
393 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
405 Selection::remove (boost::shared_ptr<Playlist> track)
407 list<boost::shared_ptr<Playlist> >::iterator i;
408 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
415 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
417 bool changed = false;
419 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
421 list<boost::shared_ptr<Playlist> >::iterator x;
423 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
435 Selection::remove (RegionView* r)
437 if (regions.remove (r)) {
441 if (!regions.involves (r->get_trackview())) {
442 remove (&r->get_trackview());
448 Selection::remove (uint32_t selection_id)
454 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
455 if ((*i).id == selection_id) {
465 Selection::remove (nframes_t start, nframes_t end)
470 Selection::remove (AutomationList *ac)
472 list<AutomationList*>::iterator i;
473 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
480 Selection::set (TimeAxisView* track)
487 Selection::set (const list<TimeAxisView*>& track_list)
494 Selection::set (boost::shared_ptr<Playlist> playlist)
501 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
508 Selection::set (RegionView* r)
516 Selection::set (vector<RegionView*>& v)
520 // make sure to deselect any automation selections
526 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
528 if ((start == 0 && end == 0) || end < start) {
533 time.push_back (AudioRange (start, end, next_time_id++));
535 /* reuse the first entry, and remove all the rest */
537 while (time.size() > 1) {
540 time.front().start = start;
541 time.front().end = end;
546 time.group = track->edit_group();
556 return time.front().id;
560 Selection::set (AutomationList *ac)
567 Selection::selected (TimeAxisView* tv)
569 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
573 Selection::selected (RegionView* rv)
575 return find (regions.begin(), regions.end(), rv) != regions.end();
581 return regions.empty () &&
584 playlists.empty () &&
587 playlists.empty () &&
593 Selection::toggle (const vector<AutomationSelectable*>& autos)
595 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
596 if ((*x)->get_selected()) {
599 points.push_back (**x);
605 PointsChanged (); /* EMIT SIGNAL */
609 Selection::toggle (list<Selectable*>& selectables)
612 AutomationSelectable* as;
613 vector<RegionView*> rvs;
614 vector<AutomationSelectable*> autos;
616 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
617 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
619 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
620 autos.push_back (as);
622 fatal << _("programming error: ")
623 << X_("unknown selectable type passed to Selection::toggle()")
633 if (!autos.empty()) {
639 Selection::set (list<Selectable*>& selectables)
648 Selection::add (list<Selectable*>& selectables)
651 AutomationSelectable* as;
652 vector<RegionView*> rvs;
653 vector<AutomationSelectable*> autos;
655 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
656 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
658 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
659 autos.push_back (as);
661 fatal << _("programming error: ")
662 << X_("unknown selectable type passed to Selection::add()")
672 if (!autos.empty()) {
678 Selection::clear_points ()
680 if (!points.empty()) {
687 Selection::add (vector<AutomationSelectable*>& autos)
689 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
690 points.push_back (**i);
697 Selection::select_edit_group_regions ()
699 std::set<RegionView*> regions_to_add;
701 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
702 vector<RegionView*> e;
703 editor->get_equivalent_regions (*i, e);
704 for (vector<RegionView*>::iterator j = e.begin(); j != e.end(); ++j) {
705 regions_to_add.insert(*j);
709 for (std::set<RegionView*>::iterator i = regions_to_add.begin(); i != regions_to_add.end(); ++i) {
715 Selection::set (Marker* m)
722 Selection::toggle (Marker* m)
724 MarkerSelection::iterator i;
726 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
734 Selection::remove (Marker* m)
736 MarkerSelection::iterator i;
738 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
746 Selection::add (Marker* m)
748 if (find (markers.begin(), markers.end(), m) == markers.end()) {
749 markers.push_back (m);