fix math bug with numthreads computation
[ardour2.git] / gtk2_ardour / region_selection.cc
blob9ac96d682ba35db73a54687c48dbe9862593c4f4
1 /*
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.
19 #include <algorithm>
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"
28 using namespace std;
29 using namespace ARDOUR;
30 using namespace PBD;
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());
38 _current_start = 0;
39 _current_end = 0;
42 /** Copy constructor.
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) {
54 add (*i);
58 /** operator= to set a RegionSelection to be the same as another.
59 * @param other Other RegionSelection.
61 RegionSelection&
62 RegionSelection::operator= (const RegionSelection& other)
64 if (this != &other) {
66 clear_all();
68 _current_start = other._current_start;
69 _current_end = other._current_end;
71 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
72 add (*i);
76 return *this;
79 /** Empty this RegionSelection.
81 void
82 RegionSelection::clear_all()
84 clear();
85 _bylayer.clear();
86 _current_start = 0;
87 _current_end = 0;
90 /**
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.
103 bool
104 RegionSelection::add (RegionView* rv)
106 if (contains (rv)) {
107 /* we already have it */
108 return false;
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();
119 push_back (rv);
121 /* add to layer sorted list */
123 add_to_layer (rv);
125 return true;
128 /** Remove a region from the selection.
129 * @param rv Region to remove.
131 void
132 RegionSelection::remove_it (RegionView *rv)
134 remove (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.
141 bool
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);
151 if (size() == 1) {
153 /* this is the last one, so when we delete it
154 we will be empty.
157 _current_start = 0;
158 _current_end = 0;
160 } else {
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 */
184 nframes_t ref = 0;
186 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
187 if (region->first_frame() > ref) {
188 ref = region->first_frame();
192 _current_end = ref;
196 erase (r);
198 return true;
201 return false;
204 /** Add a region to the list sorted by layer.
205 * @param rv Region to add.
207 void
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);
218 return;
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.
237 void
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) {
244 foo.push_back (*i);
247 foo.sort (sorter);
248 return;
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();
258 } else {
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.
269 void
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) {
276 foo.push_back (*i);
279 foo.sort (sorter);
280 return;
284 * @param Sort the selection by position and track.
286 void
287 RegionSelection::sort_by_position_and_track ()
289 RegionSortByTrack sorter;
290 sort (sorter);
294 * @param tv Track.
295 * @return true if any of the selection's regions are on tv.
297 bool
298 RegionSelection::involves (const TimeAxisView& tv) const
300 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
301 if (&(*i)->get_trackview() == &tv) {
302 return true;
305 return false;