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 _current_start
= other
._current_start
;
52 _current_end
= other
._current_end
;
54 for (RegionSelection::const_iterator i
= other
.begin(); i
!= other
.end(); ++i
) {
59 /** operator= to set a RegionSelection to be the same as another.
60 * @param other Other RegionSelection.
63 RegionSelection::operator= (const RegionSelection
& other
)
69 _current_start
= other
._current_start
;
70 _current_end
= other
._current_end
;
72 for (RegionSelection::const_iterator i
= other
.begin(); i
!= other
.end(); ++i
) {
80 /** Empty this RegionSelection.
83 RegionSelection::clear_all()
92 * @param rv RegionView.
93 * @return true if this selection contains rv.
95 bool RegionSelection::contains (RegionView
* rv
) const
97 return find (begin(), end(), rv
) != end();
100 /** Add a region to the selection.
101 * @param rv Region to add.
102 * @return false if we already had the region, otherwise true.
105 RegionSelection::add (RegionView
* rv
)
108 /* we already have it */
112 if (rv
->region()->first_frame() < _current_start
|| empty()) {
113 _current_start
= rv
->region()->first_frame();
116 if (rv
->region()->last_frame() > _current_end
|| empty()) {
117 _current_end
= rv
->region()->last_frame();
122 /* add to layer sorted list */
129 /** Remove a region from the selection.
130 * @param rv Region to remove.
133 RegionSelection::remove_it (RegionView
*rv
)
138 /** Remove a region from the selection.
139 * @param rv Region to remove.
140 * @return true if the region was in the selection, false if not.
143 RegionSelection::remove (RegionView
* rv
)
145 RegionSelection::iterator r
;
147 if ((r
= find (begin(), end(), rv
)) != end()) {
149 // remove from layer sorted list
150 _bylayer
.remove (rv
);
154 /* this is the last one, so when we delete it
163 boost::shared_ptr
<Region
> region ((*r
)->region());
165 if (region
->first_frame() == _current_start
) {
167 /* reset current start */
169 nframes_t ref
= max_frames
;
171 for (RegionSelection::iterator i
= begin (); i
!= end(); ++i
) {
172 if (region
->first_frame() < ref
) {
173 ref
= region
->first_frame();
177 _current_start
= ref
;
181 if (region
->last_frame() == _current_end
) {
183 /* reset current end */
187 for (RegionSelection::iterator i
= begin (); i
!= end(); ++i
) {
188 if (region
->first_frame() > ref
) {
189 ref
= region
->first_frame();
205 /** Add a region to the list sorted by layer.
206 * @param rv Region to add.
209 RegionSelection::add_to_layer (RegionView
* rv
)
211 // insert it into layer sorted position
213 list
<RegionView
*>::iterator i
;
215 for (i
= _bylayer
.begin(); i
!= _bylayer
.end(); ++i
)
217 if (rv
->region()->layer() < (*i
)->region()->layer()) {
218 _bylayer
.insert(i
, rv
);
223 // insert at end if we get here
224 _bylayer
.insert(i
, rv
);
227 struct RegionSortByTime
{
228 bool operator() (const RegionView
* a
, const RegionView
* b
) const {
229 return a
->region()->position() < b
->region()->position();
235 * @param foo List which will be filled with the selection's regions
236 * sorted by position.
239 RegionSelection::by_position (list
<RegionView
*>& foo
) const
241 list
<RegionView
*>::const_iterator i
;
242 RegionSortByTime sorter
;
244 for (i
= _bylayer
.begin(); i
!= _bylayer
.end(); ++i
) {
252 struct RegionSortByTrack
{
253 bool operator() (const RegionView
* a
, const RegionView
* b
) const {
255 /* really, track and position */
257 if (a
->get_trackview().order() == b
->get_trackview().order()) {
258 return a
->region()->position() < b
->region()->position();
260 return a
->get_trackview().order() < b
->get_trackview().order();
267 * @param List which will be filled with the selection's regions
268 * sorted by track and position.
271 RegionSelection::by_track (list
<RegionView
*>& foo
) const
273 list
<RegionView
*>::const_iterator i
;
274 RegionSortByTrack sorter
;
276 for (i
= _bylayer
.begin(); i
!= _bylayer
.end(); ++i
) {
285 * @param Sort the selection by position and track.
288 RegionSelection::sort_by_position_and_track ()
290 RegionSortByTrack sorter
;
296 * @return true if any of the selection's regions are on tv.
299 RegionSelection::involves (const TimeAxisView
& tv
) const
301 for (RegionSelection::const_iterator i
= begin(); i
!= end(); ++i
) {
302 if (&(*i
)->get_trackview() == &tv
) {