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;
31 /** Construct an empty RegionSelection.
33 RegionSelection::RegionSelection ()
35 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
42 * @param other RegionSelection to copy.
44 RegionSelection::RegionSelection (const RegionSelection& other)
45 : std::list<RegionView*>()
46 , sigc::trackable(other)
48 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
50 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
53 _current_start = other._current_start;
54 _current_end = other._current_end;
57 /** operator= to set a RegionSelection to be the same as another.
58 * @param other Other RegionSelection.
61 RegionSelection::operator= (const RegionSelection& other)
67 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
71 _current_start = other._current_start;
72 _current_end = other._current_end;
78 /** Empty this RegionSelection.
81 RegionSelection::clear_all()
90 * @param rv RegionView.
91 * @return true if this selection contains rv.
93 bool RegionSelection::contains (RegionView* rv) const
95 return find (begin(), end(), rv) != end();
98 /** Add a region to the selection.
99 * @param rv Region to add.
100 * @return false if we already had the region, otherwise true.
103 RegionSelection::add (RegionView* rv)
106 /* we already have it */
110 if (rv->region()->first_frame() < _current_start || empty()) {
111 _current_start = rv->region()->first_frame();
114 if (rv->region()->last_frame() > _current_end || empty()) {
115 _current_end = rv->region()->last_frame();
120 /* add to layer sorted list */
127 /** Remove a region from the selection.
128 * @param rv Region to remove.
131 RegionSelection::remove_it (RegionView *rv)
136 /** Remove a region from the selection.
137 * @param rv Region to remove.
138 * @return true if the region was in the selection, false if not.
141 RegionSelection::remove (RegionView* rv)
143 RegionSelection::iterator r;
145 if ((r = find (begin(), end(), rv)) != end()) {
147 // remove from layer sorted list
148 _bylayer.remove (rv);
152 /* this is the last one, so when we delete it
161 boost::shared_ptr<Region> region ((*r)->region());
163 if (region->first_frame() == _current_start) {
165 /* reset current start */
167 nframes_t ref = max_frames;
169 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
170 if (region->first_frame() < ref) {
171 ref = region->first_frame();
175 _current_start = ref;
179 if (region->last_frame() == _current_end) {
181 /* reset current end */
185 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
186 if (region->first_frame() > ref) {
187 ref = region->first_frame();
203 /** Add a region to the list sorted by layer.
204 * @param rv Region to add.
207 RegionSelection::add_to_layer (RegionView * rv)
209 // insert it into layer sorted position
211 list<RegionView*>::iterator i;
213 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
215 if (rv->region()->layer() < (*i)->region()->layer()) {
216 _bylayer.insert(i, rv);
221 // insert at end if we get here
222 _bylayer.insert(i, rv);
225 struct RegionSortByTime {
226 bool operator() (const RegionView* a, const RegionView* b) const {
227 return a->region()->position() < b->region()->position();
233 * @param foo List which will be filled with the selection's regions
234 * sorted by position.
237 RegionSelection::by_position (list<RegionView*>& foo) const
239 list<RegionView*>::const_iterator i;
240 RegionSortByTime sorter;
242 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
250 struct RegionSortByTrack {
251 bool operator() (const RegionView* a, const RegionView* b) const {
253 /* really, track and position */
255 if (a->get_trackview().order() == b->get_trackview().order()) {
256 return a->region()->position() < b->region()->position();
258 return a->get_trackview().order() < b->get_trackview().order();
265 * @param List which will be filled with the selection's regions
266 * sorted by track and position.
269 RegionSelection::by_track (list<RegionView*>& foo) const
271 list<RegionView*>::const_iterator i;
272 RegionSortByTrack sorter;
274 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
283 * @param Sort the selection by position and track.
286 RegionSelection::sort_by_position_and_track ()
288 RegionSortByTrack sorter;
294 * @return true if any of the selection's regions are on tv.
297 RegionSelection::involves (const TimeAxisView& tv) const
299 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
300 if (&(*i)->get_trackview() == &tv) {