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.
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;
32 * Construct an empty RegionSelection.
35 RegionSelection::RegionSelection ()
37 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
45 * @param other RegionSelection to copy.
48 RegionSelection::RegionSelection (const RegionSelection& other)
50 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
52 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
55 _current_start = other._current_start;
56 _current_end = other._current_end;
60 * operator= to set a RegionSelection to be the same as another.
61 * @param other Other RegionSelection.
65 RegionSelection::operator= (const RegionSelection& other)
71 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
75 _current_start = other._current_start;
76 _current_end = other._current_end;
83 * Empty this RegionSelection.
87 RegionSelection::clear_all()
96 * @param rv RegionView.
97 * @return true if this selection contains rv.
100 bool RegionSelection::contains (RegionView* rv) const
102 return find (begin(), end(), rv) != end();
106 * Add a region to the selection.
107 * @param rv Region to add.
108 * @return false if we already had the region, otherwise true.
112 RegionSelection::add (RegionView* rv)
115 /* we already have it */
119 if (rv->region()->first_frame() < _current_start || empty()) {
120 _current_start = rv->region()->first_frame();
123 if (rv->region()->last_frame() > _current_end || empty()) {
124 _current_end = rv->region()->last_frame();
129 /* add to layer sorted list */
137 * Remove a region from the selection.
138 * @param rv Region to remove.
142 RegionSelection::remove_it (RegionView *rv)
148 * Remove a region from the selection.
149 * @param rv Region to remove.
150 * @return true if the region was in the selection, false if not.
154 RegionSelection::remove (RegionView* rv)
156 RegionSelection::iterator r;
158 if ((r = find (begin(), end(), rv)) != end()) {
160 // remove from layer sorted list
161 _bylayer.remove (rv);
165 /* this is the last one, so when we delete it
174 boost::shared_ptr<Region> region ((*r)->region());
176 if (region->first_frame() == _current_start) {
178 /* reset current start */
180 nframes_t ref = max_frames;
182 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
183 if (region->first_frame() < ref) {
184 ref = region->first_frame();
188 _current_start = ref;
192 if (region->last_frame() == _current_end) {
194 /* reset current end */
198 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
199 if (region->first_frame() > ref) {
200 ref = region->first_frame();
217 * Add a region to the list sorted by layer.
218 * @param rv Region to add.
222 RegionSelection::add_to_layer (RegionView * rv)
224 // insert it into layer sorted position
226 list<RegionView*>::iterator i;
228 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
230 if (rv->region()->layer() < (*i)->region()->layer()) {
231 _bylayer.insert(i, rv);
236 // insert at end if we get here
237 _bylayer.insert(i, rv);
240 struct RegionSortByTime {
241 bool operator() (const RegionView* a, const RegionView* b) const {
242 return a->region()->position() < b->region()->position();
248 * @param foo List which will be filled with the selection's regions
249 * sorted by position.
253 RegionSelection::by_position (list<RegionView*>& foo) const
255 list<RegionView*>::const_iterator i;
256 RegionSortByTime sorter;
258 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
266 struct RegionSortByTrack {
267 bool operator() (const RegionView* a, const RegionView* b) const {
269 /* really, track and position */
271 if (a->get_trackview().order == b->get_trackview().order) {
272 return a->region()->position() < b->region()->position();
274 return a->get_trackview().order < b->get_trackview().order;
281 * @param List which will be filled with the selection's regions
282 * sorted by track and position.
286 RegionSelection::by_track (list<RegionView*>& foo) const
288 list<RegionView*>::const_iterator i;
289 RegionSortByTrack sorter;
291 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
300 * @param Sort the selection by position and track.
304 RegionSelection::sort_by_position_and_track ()
306 RegionSortByTrack sorter;
312 * @return true if any of the selection's regions are on tv.
316 RegionSelection::involves (const TimeAxisView& tv) const
318 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
319 if (&(*i)->get_trackview() == &tv) {