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;
69 /** Clear everything from the Selection */
82 Selection::dump_region_layers()
84 cerr << "region selection layer dump" << endl;
85 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
86 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
92 Selection::clear_regions ()
94 if (!regions.empty()) {
101 Selection::clear_tracks ()
103 if (!tracks.empty()) {
110 Selection::clear_time ()
120 Selection::clear_playlists ()
122 /* Selections own their playlists */
124 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
125 /* selections own their own regions, which are copies of the "originals". make them go away */
126 (*i)->drop_regions ();
130 if (!playlists.empty()) {
137 Selection::clear_lines ()
139 if (!lines.empty()) {
146 Selection::toggle (boost::shared_ptr<Playlist> pl)
148 PlaylistSelection::iterator i;
150 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
152 playlists.push_back(pl);
161 Selection::toggle (const list<TimeAxisView*>& track_list)
163 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
169 Selection::toggle (TimeAxisView* track)
171 TrackSelection::iterator i;
173 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
174 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
175 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
176 tracks.push_back (track);
185 Selection::toggle (RegionView* r)
187 RegionSelection::iterator i;
189 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
199 Selection::toggle (vector<RegionView*>& r)
201 RegionSelection::iterator i;
203 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
204 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
215 Selection::toggle (nframes_t start, nframes_t end)
217 AudioRangeComparator cmp;
219 /* XXX this implementation is incorrect */
221 time.push_back (AudioRange (start, end, next_time_id++));
227 return next_time_id - 1;
231 Selection::add (boost::shared_ptr<Playlist> pl)
233 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
235 playlists.push_back(pl);
241 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
243 bool changed = false;
245 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
246 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
248 playlists.push_back (*i);
259 Selection::add (const list<TimeAxisView*>& track_list)
261 bool changed = false;
263 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
264 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
265 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
266 (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
267 tracks.push_back (*i);
278 Selection::add (TimeAxisView* track)
280 if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
281 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
282 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
283 tracks.push_back (track);
289 Selection::add (RegionView* r)
291 if (find (regions.begin(), regions.end(), r) == regions.end()) {
293 add (&r->get_trackview());
299 Selection::add (vector<RegionView*>& v)
301 bool changed = false;
303 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
304 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
305 changed = regions.add ((*i));
307 add (&(*i)->get_trackview());
318 Selection::add (nframes_t start, nframes_t end)
320 AudioRangeComparator cmp;
322 /* XXX this implementation is incorrect */
324 time.push_back (AudioRange (start, end, next_time_id++));
330 return next_time_id - 1;
334 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
336 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
337 if ((*i).id == sid) {
339 time.push_back (AudioRange(start,end, sid));
341 /* don't consolidate here */
344 AudioRangeComparator cmp;
354 Selection::add (AutomationList* ac)
356 if (find (lines.begin(), lines.end(), ac) == lines.end()) {
357 lines.push_back (ac);
363 Selection::remove (TimeAxisView* track)
365 list<TimeAxisView*>::iterator i;
366 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
373 Selection::remove (const list<TimeAxisView*>& track_list)
375 bool changed = false;
377 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
379 list<TimeAxisView*>::iterator x;
381 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
393 Selection::remove (boost::shared_ptr<Playlist> track)
395 list<boost::shared_ptr<Playlist> >::iterator i;
396 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
403 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
405 bool changed = false;
407 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
409 list<boost::shared_ptr<Playlist> >::iterator x;
411 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
423 Selection::remove (RegionView* r)
425 if (regions.remove (r)) {
429 if (!regions.involves (r->get_trackview())) {
430 remove (&r->get_trackview());
436 Selection::remove (uint32_t selection_id)
442 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
443 if ((*i).id == selection_id) {
453 Selection::remove (nframes_t start, nframes_t end)
458 Selection::remove (AutomationList *ac)
460 list<AutomationList*>::iterator i;
461 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
468 Selection::set (TimeAxisView* track)
475 Selection::set (const list<TimeAxisView*>& track_list)
482 Selection::set (boost::shared_ptr<Playlist> playlist)
489 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
496 Selection::set (RegionView* r)
504 Selection::set (vector<RegionView*>& v)
508 // make sure to deselect any automation selections
514 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
516 if ((start == 0 && end == 0) || end < start) {
521 time.push_back (AudioRange (start, end, next_time_id++));
523 /* reuse the first entry, and remove all the rest */
525 while (time.size() > 1) {
528 time.front().start = start;
529 time.front().end = end;
534 time.group = track->edit_group();
544 return time.front().id;
548 Selection::set (AutomationList *ac)
555 Selection::selected (TimeAxisView* tv)
557 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
561 Selection::selected (RegionView* rv)
563 return find (regions.begin(), regions.end(), rv) != regions.end();
569 return regions.empty () &&
572 playlists.empty () &&
580 Selection::toggle (const vector<AutomationSelectable*>& autos)
582 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
583 if ((*x)->get_selected()) {
586 points.push_back (**x);
592 PointsChanged (); /* EMIT SIGNAL */
596 Selection::toggle (list<Selectable*>& selectables)
599 AutomationSelectable* as;
600 vector<RegionView*> rvs;
601 vector<AutomationSelectable*> autos;
603 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
604 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
606 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
607 autos.push_back (as);
609 fatal << _("programming error: ")
610 << X_("unknown selectable type passed to Selection::toggle()")
620 if (!autos.empty()) {
626 Selection::set (list<Selectable*>& selectables)
635 Selection::add (list<Selectable*>& selectables)
638 AutomationSelectable* as;
639 vector<RegionView*> rvs;
640 vector<AutomationSelectable*> autos;
642 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
643 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
645 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
646 autos.push_back (as);
648 fatal << _("programming error: ")
649 << X_("unknown selectable type passed to Selection::add()")
659 if (!autos.empty()) {
665 Selection::clear_points ()
667 if (!points.empty()) {
674 Selection::add (vector<AutomationSelectable*>& autos)
676 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
677 points.push_back (**i);