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"
28 using namespace ARDOUR;
32 /** Construct an empty RegionSelection.
34 RegionSelection::RegionSelection ()
36 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
43 * @param other RegionSelection to copy.
45 RegionSelection::RegionSelection (const RegionSelection& other)
46 : std::list<RegionView*>()
47 , sigc::trackable(other)
49 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
51 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
54 _current_start = other._current_start;
55 _current_end = other._current_end;
58 /** operator= to set a RegionSelection to be the same as another.
59 * @param other Other RegionSelection.
62 RegionSelection::operator= (const RegionSelection& other)
68 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
72 _current_start = other._current_start;
73 _current_end = other._current_end;
79 /** Empty this RegionSelection.
82 RegionSelection::clear_all()
91 * @param rv RegionView.
92 * @return true if this selection contains rv.
94 bool RegionSelection::contains (RegionView* rv) const
96 return find (begin(), end(), rv) != end();
99 /** Add a region to the selection.
100 * @param rv Region to add.
101 * @return false if we already had the region, otherwise true.
104 RegionSelection::add (RegionView* rv)
107 /* we already have it */
111 if (rv->region()->first_frame() < _current_start || empty()) {
112 _current_start = rv->region()->first_frame();
115 if (rv->region()->last_frame() > _current_end || empty()) {
116 _current_end = rv->region()->last_frame();
121 /* add to layer sorted list */
128 /** Remove a region from the selection.
129 * @param rv Region to remove.
132 RegionSelection::remove_it (RegionView *rv)
137 /** Remove a region from the selection.
138 * @param rv Region to remove.
139 * @return true if the region was in the selection, false if not.
142 RegionSelection::remove (RegionView* rv)
144 RegionSelection::iterator r;
146 if ((r = find (begin(), end(), rv)) != end()) {
148 // remove from layer sorted list
149 _bylayer.remove (rv);
153 /* this is the last one, so when we delete it
162 boost::shared_ptr<Region> region ((*r)->region());
164 if (region->first_frame() == _current_start) {
166 /* reset current start */
168 nframes_t ref = max_frames;
170 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
171 if (region->first_frame() < ref) {
172 ref = region->first_frame();
176 _current_start = ref;
180 if (region->last_frame() == _current_end) {
182 /* reset current end */
186 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
187 if (region->first_frame() > ref) {
188 ref = region->first_frame();
204 /** Add a region to the list sorted by layer.
205 * @param rv Region to add.
208 RegionSelection::add_to_layer (RegionView * rv)
210 // insert it into layer sorted position
212 list<RegionView*>::iterator i;
214 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
216 if (rv->region()->layer() < (*i)->region()->layer()) {
217 _bylayer.insert(i, rv);
222 // insert at end if we get here
223 _bylayer.insert(i, rv);
226 struct RegionSortByTime {
227 bool operator() (const RegionView* a, const RegionView* b) const {
228 return a->region()->position() < b->region()->position();
234 * @param foo List which will be filled with the selection's regions
235 * sorted by position.
238 RegionSelection::by_position (list<RegionView*>& foo) const
240 list<RegionView*>::const_iterator i;
241 RegionSortByTime sorter;
243 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
251 struct RegionSortByTrack {
252 bool operator() (const RegionView* a, const RegionView* b) const {
254 /* really, track and position */
256 if (a->get_trackview().order() == b->get_trackview().order()) {
257 return a->region()->position() < b->region()->position();
259 return a->get_trackview().order() < b->get_trackview().order();
266 * @param List which will be filled with the selection's regions
267 * sorted by track and position.
270 RegionSelection::by_track (list<RegionView*>& foo) const
272 list<RegionView*>::const_iterator i;
273 RegionSortByTrack sorter;
275 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
284 * @param Sort the selection by position and track.
287 RegionSelection::sort_by_position_and_track ()
289 RegionSortByTrack sorter;
295 * @return true if any of the selection's regions are on tv.
298 RegionSelection::involves (const TimeAxisView& tv) const
300 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
301 if (&(*i)->get_trackview() == &tv) {