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 "gui_thread.h"
24 #include "region_view.h"
25 #include "region_selection.h"
26 #include "time_axis_view.h"
29 using namespace ARDOUR;
32 /** Construct an empty RegionSelection.
34 RegionSelection::RegionSelection ()
36 RegionView::RegionViewGoingAway.connect (death_connection, MISSING_INVALIDATOR, ui_bind (&RegionSelection::remove_it, this, _1), gui_context());
43 * @param other RegionSelection to copy.
45 RegionSelection::RegionSelection (const RegionSelection& other)
46 : std::list<RegionView*>()
48 RegionView::RegionViewGoingAway.connect (death_connection, MISSING_INVALIDATOR, ui_bind (&RegionSelection::remove_it, this, _1), gui_context());
50 _current_start = other._current_start;
51 _current_end = other._current_end;
53 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
58 /** operator= to set a RegionSelection to be the same as another.
59 * @param other Other RegionSelection.
62 RegionSelection::operator= (const RegionSelection& other)
68 _current_start = other._current_start;
69 _current_end = other._current_end;
71 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
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 or if it cannot be added,
105 RegionSelection::add (RegionView* rv)
107 if (!rv->region()->playlist()) {
108 /* not attached to a playlist - selection not allowed.
109 This happens if the user tries to select a region
110 during a capture pass.
116 /* we already have it */
120 if (rv->region()->first_frame() < _current_start || empty()) {
121 _current_start = rv->region()->first_frame();
124 if (rv->region()->last_frame() > _current_end || empty()) {
125 _current_end = rv->region()->last_frame();
130 /* add to layer sorted list */
137 /** Remove a region from the selection.
138 * @param rv Region to remove.
141 RegionSelection::remove_it (RegionView *rv)
146 /** Remove a region from the selection.
147 * @param rv Region to remove.
148 * @return true if the region was in the selection, false if not.
151 RegionSelection::remove (RegionView* rv)
153 RegionSelection::iterator r;
155 if ((r = find (begin(), end(), rv)) != end()) {
157 // remove from layer sorted list
158 _bylayer.remove (rv);
162 /* this is the last one, so when we delete it
171 boost::shared_ptr<Region> region ((*r)->region());
173 if (region->first_frame() == _current_start) {
175 /* reset current start */
177 nframes_t ref = max_frames;
179 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
180 if (region->first_frame() < ref) {
181 ref = region->first_frame();
185 _current_start = ref;
189 if (region->last_frame() == _current_end) {
191 /* reset current end */
195 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
196 if (region->first_frame() > ref) {
197 ref = region->first_frame();
213 /** Add a region to the list sorted by layer.
214 * @param rv Region to add.
217 RegionSelection::add_to_layer (RegionView * rv)
219 // insert it into layer sorted position
221 list<RegionView*>::iterator i;
223 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
225 if (rv->region()->layer() < (*i)->region()->layer()) {
226 _bylayer.insert(i, rv);
231 // insert at end if we get here
232 _bylayer.insert(i, rv);
235 struct RegionSortByTime {
236 bool operator() (const RegionView* a, const RegionView* b) const {
237 return a->region()->position() < b->region()->position();
243 * @param foo List which will be filled with the selection's regions
244 * sorted by position.
247 RegionSelection::by_position (list<RegionView*>& foo) const
249 list<RegionView*>::const_iterator i;
250 RegionSortByTime sorter;
252 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
260 struct RegionSortByTrack {
261 bool operator() (const RegionView* a, const RegionView* b) const {
263 /* really, track and position */
265 if (a->get_time_axis_view().order() == b->get_time_axis_view().order()) {
266 return a->region()->position() < b->region()->position();
268 return a->get_time_axis_view().order() < b->get_time_axis_view().order();
275 * @param List which will be filled with the selection's regions
276 * sorted by track and position.
279 RegionSelection::by_track (list<RegionView*>& foo) const
281 list<RegionView*>::const_iterator i;
282 RegionSortByTrack sorter;
284 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
293 * @param Sort the selection by position and track.
296 RegionSelection::sort_by_position_and_track ()
298 RegionSortByTrack sorter;
304 * @return true if any of the selection's regions are on tv.
307 RegionSelection::involves (const TimeAxisView& tv) const
309 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
310 if (&(*i)->get_time_axis_view() == &tv) {