fix import/embed with "sequence files" option
[ardour2.git] / gtk2_ardour / region_selection.cc
blob8074383141fff7d7ca4e17042c80a84c314d37b2
1 /*
2 Copyright (C) 2006 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.
19 #include <algorithm>
21 #include <ardour/region.h>
23 #include "region_view.h"
24 #include "region_selection.h"
25 #include "time_axis_view.h"
27 using namespace ARDOUR;
28 using namespace PBD;
29 using namespace sigc;
32 RegionSelection::RegionSelection ()
34 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
36 _current_start = 0;
37 _current_end = 0;
40 RegionSelection::RegionSelection (const RegionSelection& other)
42 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
44 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
45 add (*i);
47 _current_start = other._current_start;
48 _current_end = other._current_end;
51 RegionSelection&
52 RegionSelection::operator= (const RegionSelection& other)
54 if (this != &other) {
56 clear_all();
58 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
59 add (*i);
62 _current_start = other._current_start;
63 _current_end = other._current_end;
66 return *this;
69 void
70 RegionSelection::clear_all()
72 clear();
73 _bylayer.clear();
74 _current_start = 0;
75 _current_end = 0;
78 bool RegionSelection::contains (RegionView* rv) const
80 return find (begin(), end(), rv) != end();
83 bool
84 RegionSelection::add (RegionView* rv)
86 if (contains (rv)) {
87 /* we already have it */
88 return false;
91 if (rv->region()->first_frame() < _current_start || empty()) {
92 _current_start = rv->region()->first_frame();
95 if (rv->region()->last_frame() > _current_end || empty()) {
96 _current_end = rv->region()->last_frame();
99 push_back (rv);
101 // add to layer sorted list
103 add_to_layer (rv);
105 return true;
108 void
109 RegionSelection::remove_it (RegionView *rv)
111 remove (rv);
114 bool
115 RegionSelection::remove (RegionView* rv)
117 RegionSelection::iterator r;
119 if ((r = find (begin(), end(), rv)) != end()) {
121 // remove from layer sorted list
122 _bylayer.remove (rv);
124 if (size() == 1) {
126 /* this is the last one, so when we delete it
127 we will be empty.
130 _current_start = 0;
131 _current_end = 0;
133 } else {
135 boost::shared_ptr<Region> region ((*r)->region());
137 if (region->first_frame() == _current_start) {
139 /* reset current start */
141 nframes_t ref = max_frames;
143 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
144 if (region->first_frame() < ref) {
145 ref = region->first_frame();
149 _current_start = ref;
153 if (region->last_frame() == _current_end) {
155 /* reset current end */
157 nframes_t ref = 0;
159 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
160 if (region->first_frame() > ref) {
161 ref = region->first_frame();
165 _current_end = ref;
169 erase (r);
171 return true;
174 return false;
177 void
178 RegionSelection::add_to_layer (RegionView * rv)
180 // insert it into layer sorted position
182 list<RegionView*>::iterator i;
184 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
186 if (rv->region()->layer() < (*i)->region()->layer()) {
187 _bylayer.insert(i, rv);
188 return;
192 // insert at end if we get here
193 _bylayer.insert(i, rv);
196 struct RegionSortByTime {
197 bool operator() (const RegionView* a, const RegionView* b) const {
198 return a->region()->position() < b->region()->position();
203 void
204 RegionSelection::by_position (list<RegionView*>& foo) const
206 list<RegionView*>::const_iterator i;
207 RegionSortByTime sorter;
209 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
210 foo.push_back (*i);
213 foo.sort (sorter);
214 return;
217 struct RegionSortByTrack {
218 bool operator() (const RegionView* a, const RegionView* b) const {
220 /* really, track and position */
222 if (a->get_trackview().order == b->get_trackview().order) {
223 return a->region()->position() < b->region()->position();
224 } else {
225 return a->get_trackview().order < b->get_trackview().order;
230 void
231 RegionSelection::by_track (list<RegionView*>& foo) const
233 list<RegionView*>::const_iterator i;
234 RegionSortByTrack sorter;
236 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
237 foo.push_back (*i);
240 foo.sort (sorter);
241 return;
244 void
245 RegionSelection::sort_by_position_and_track ()
247 RegionSortByTrack sorter;
248 sort (sorter);
251 bool
252 RegionSelection::involves (const TimeAxisView& tv) const
254 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
255 if (&(*i)->get_trackview() == &tv) {
256 return true;
259 return false;