Remove idiocy.
[ardour2.git] / gtk2_ardour / region_selection.cc
blob20dd8c1d24fbddccf3b9b73ee0d5af38f9d90ba4
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 "region_view.h"
24 #include "region_selection.h"
25 #include "time_axis_view.h"
27 using namespace std;
28 using namespace ARDOUR;
29 using namespace PBD;
30 using namespace sigc;
32 /** Construct an empty RegionSelection.
34 RegionSelection::RegionSelection ()
36 RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
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*>()
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) {
55 add (*i);
59 /** operator= to set a RegionSelection to be the same as another.
60 * @param other Other RegionSelection.
62 RegionSelection&
63 RegionSelection::operator= (const RegionSelection& other)
65 if (this != &other) {
67 clear_all();
69 _current_start = other._current_start;
70 _current_end = other._current_end;
72 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
73 add (*i);
77 return *this;
80 /** Empty this RegionSelection.
82 void
83 RegionSelection::clear_all()
85 clear();
86 _bylayer.clear();
87 _current_start = 0;
88 _current_end = 0;
91 /**
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.
104 bool
105 RegionSelection::add (RegionView* rv)
107 if (contains (rv)) {
108 /* we already have it */
109 return false;
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();
120 push_back (rv);
122 /* add to layer sorted list */
124 add_to_layer (rv);
126 return true;
129 /** Remove a region from the selection.
130 * @param rv Region to remove.
132 void
133 RegionSelection::remove_it (RegionView *rv)
135 remove (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.
142 bool
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);
152 if (size() == 1) {
154 /* this is the last one, so when we delete it
155 we will be empty.
158 _current_start = 0;
159 _current_end = 0;
161 } else {
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 */
185 nframes_t ref = 0;
187 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
188 if (region->first_frame() > ref) {
189 ref = region->first_frame();
193 _current_end = ref;
197 erase (r);
199 return true;
202 return false;
205 /** Add a region to the list sorted by layer.
206 * @param rv Region to add.
208 void
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);
219 return;
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.
238 void
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) {
245 foo.push_back (*i);
248 foo.sort (sorter);
249 return;
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();
259 } else {
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.
270 void
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) {
277 foo.push_back (*i);
280 foo.sort (sorter);
281 return;
285 * @param Sort the selection by position and track.
287 void
288 RegionSelection::sort_by_position_and_track ()
290 RegionSortByTrack sorter;
291 sort (sorter);
295 * @param tv Track.
296 * @return true if any of the selection's regions are on tv.
298 bool
299 RegionSelection::involves (const TimeAxisView& tv) const
301 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
302 if (&(*i)->get_trackview() == &tv) {
303 return true;
306 return false;