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::toggle (boost::shared_ptr<Playlist> pl)
149 PlaylistSelection::iterator i;
151 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
153 playlists.push_back(pl);
162 Selection::toggle (const list<TimeAxisView*>& track_list)
164 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
170 Selection::toggle (TimeAxisView* track)
172 TrackSelection::iterator i;
174 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
175 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
176 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
177 tracks.push_back (track);
186 Selection::toggle (RegionView* r)
188 RegionSelection::iterator i;
190 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
200 Selection::toggle (vector<RegionView*>& r)
202 RegionSelection::iterator i;
204 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
205 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
216 Selection::toggle (nframes_t start, nframes_t end)
218 AudioRangeComparator cmp;
220 /* XXX this implementation is incorrect */
222 time.push_back (AudioRange (start, end, next_time_id++));
228 return next_time_id - 1;
232 Selection::add (boost::shared_ptr<Playlist> pl)
234 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
236 playlists.push_back(pl);
242 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
244 bool changed = false;
246 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
247 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
249 playlists.push_back (*i);
260 Selection::add (const list<TimeAxisView*>& track_list)
262 bool changed = false;
264 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
265 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
266 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
267 (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
268 tracks.push_back (*i);
279 Selection::add (TimeAxisView* track)
281 if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
282 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
283 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
284 tracks.push_back (track);
290 Selection::add (RegionView* r)
292 if (find (regions.begin(), regions.end(), r) == regions.end()) {
294 select_edit_group_regions ();
295 add (&r->get_trackview());
301 Selection::add (vector<RegionView*>& v)
303 bool changed = false;
305 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
306 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
307 changed = regions.add ((*i));
309 add (&(*i)->get_trackview());
315 select_edit_group_regions ();
321 Selection::add (nframes_t start, nframes_t end)
323 AudioRangeComparator cmp;
325 /* XXX this implementation is incorrect */
327 time.push_back (AudioRange (start, end, next_time_id++));
333 return next_time_id - 1;
337 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
339 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
340 if ((*i).id == sid) {
342 time.push_back (AudioRange(start,end, sid));
344 /* don't consolidate here */
347 AudioRangeComparator cmp;
357 Selection::add (AutomationList* ac)
359 if (find (lines.begin(), lines.end(), ac) == lines.end()) {
360 lines.push_back (ac);
366 Selection::remove (TimeAxisView* track)
368 list<TimeAxisView*>::iterator i;
369 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
376 Selection::remove (const list<TimeAxisView*>& track_list)
378 bool changed = false;
380 for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
382 list<TimeAxisView*>::iterator x;
384 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
396 Selection::remove (boost::shared_ptr<Playlist> track)
398 list<boost::shared_ptr<Playlist> >::iterator i;
399 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
406 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
408 bool changed = false;
410 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
412 list<boost::shared_ptr<Playlist> >::iterator x;
414 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
426 Selection::remove (RegionView* r)
428 if (regions.remove (r)) {
432 if (!regions.involves (r->get_trackview())) {
433 remove (&r->get_trackview());
439 Selection::remove (uint32_t selection_id)
445 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
446 if ((*i).id == selection_id) {
456 Selection::remove (nframes_t start, nframes_t end)
461 Selection::remove (AutomationList *ac)
463 list<AutomationList*>::iterator i;
464 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
471 Selection::set (TimeAxisView* track)
478 Selection::set (const list<TimeAxisView*>& track_list)
485 Selection::set (boost::shared_ptr<Playlist> playlist)
492 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
499 Selection::set (RegionView* r)
507 Selection::set (vector<RegionView*>& v)
511 // make sure to deselect any automation selections
517 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
519 if ((start == 0 && end == 0) || end < start) {
524 time.push_back (AudioRange (start, end, next_time_id++));
526 /* reuse the first entry, and remove all the rest */
528 while (time.size() > 1) {
531 time.front().start = start;
532 time.front().end = end;
537 time.group = track->edit_group();
547 return time.front().id;
551 Selection::set (AutomationList *ac)
558 Selection::selected (TimeAxisView* tv)
560 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
564 Selection::selected (RegionView* rv)
566 return find (regions.begin(), regions.end(), rv) != regions.end();
572 return regions.empty () &&
575 playlists.empty () &&
583 Selection::toggle (const vector<AutomationSelectable*>& autos)
585 for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
586 if ((*x)->get_selected()) {
589 points.push_back (**x);
595 PointsChanged (); /* EMIT SIGNAL */
599 Selection::toggle (list<Selectable*>& selectables)
602 AutomationSelectable* as;
603 vector<RegionView*> rvs;
604 vector<AutomationSelectable*> autos;
606 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
607 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
609 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
610 autos.push_back (as);
612 fatal << _("programming error: ")
613 << X_("unknown selectable type passed to Selection::toggle()")
623 if (!autos.empty()) {
629 Selection::set (list<Selectable*>& selectables)
638 Selection::add (list<Selectable*>& selectables)
641 AutomationSelectable* as;
642 vector<RegionView*> rvs;
643 vector<AutomationSelectable*> autos;
645 for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
646 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
648 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
649 autos.push_back (as);
651 fatal << _("programming error: ")
652 << X_("unknown selectable type passed to Selection::add()")
662 if (!autos.empty()) {
668 Selection::clear_points ()
670 if (!points.empty()) {
677 Selection::add (vector<AutomationSelectable*>& autos)
679 for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
680 points.push_back (**i);
687 Selection::select_edit_group_regions ()
689 std::set<RegionView*> regions_to_add;
691 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
692 vector<RegionView*> e;
693 editor->get_equivalent_regions (*i, e);
694 for (vector<RegionView*>::iterator j = e.begin(); j != e.end(); ++j) {
695 regions_to_add.insert(*j);
699 for (std::set<RegionView*>::iterator i = regions_to_add.begin(); i != regions_to_add.end(); ++i) {