Fix a bunch of warnings.
[ardour.git] / gtk2_ardour / region_selection.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3
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.
8
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.
13
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.
17 */
18
19 #include <algorithm>
20
21 #include "ardour/region.h"
22
23 #include "region_view.h"
24 #include "region_selection.h"
25 #include "time_axis_view.h"
26
27 using namespace ARDOUR;
28 using namespace PBD;
29 using namespace sigc;
30
31 /** Construct an empty RegionSelection.
32  */
33 RegionSelection::RegionSelection ()
34 {
35         RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
36
37         _current_start = 0;
38         _current_end = 0;
39 }
40
41 /** Copy constructor.
42  *  @param other RegionSelection to copy.
43  */
44 RegionSelection::RegionSelection (const RegionSelection& other)
45         : std::list<RegionView*>()
46         , sigc::trackable(other)
47 {
48         RegionView::RegionViewGoingAway.connect (mem_fun(*this, &RegionSelection::remove_it));
49
50         for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
51                 add (*i);
52         }
53         _current_start = other._current_start;
54         _current_end = other._current_end;
55 }
56
57 /** operator= to set a RegionSelection to be the same as another.
58  *  @param other Other RegionSelection.
59  */
60 RegionSelection&
61 RegionSelection::operator= (const RegionSelection& other)
62 {
63         if (this != &other) {
64
65                 clear_all();
66                 
67                 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
68                         add (*i);
69                 }
70
71                 _current_start = other._current_start;
72                 _current_end = other._current_end;
73         }
74
75         return *this;
76 }
77
78 /** Empty this RegionSelection.
79  */
80 void
81 RegionSelection::clear_all()
82 {
83         clear();
84         _bylayer.clear();
85         _current_start = 0;
86         _current_end = 0;
87 }
88
89 /**
90  *  @param rv RegionView.
91  *  @return true if this selection contains rv.
92  */
93 bool RegionSelection::contains (RegionView* rv) const
94 {
95         return find (begin(), end(), rv) != end();
96 }
97
98 /** Add a region to the selection.
99  *  @param rv Region to add.
100  *  @return false if we already had the region, otherwise true.
101  */
102 bool
103 RegionSelection::add (RegionView* rv)
104 {
105         if (contains (rv)) {
106                 /* we already have it */
107                 return false;
108         }
109
110         if (rv->region()->first_frame() < _current_start || empty()) {
111                 _current_start = rv->region()->first_frame();
112         }
113         
114         if (rv->region()->last_frame() > _current_end || empty()) {
115                 _current_end = rv->region()->last_frame();
116         }
117         
118         push_back (rv);
119
120         /* add to layer sorted list */
121
122         add_to_layer (rv);
123
124         return true;
125 }
126
127 /** Remove a region from the selection.
128  *  @param rv Region to remove.
129  */
130 void
131 RegionSelection::remove_it (RegionView *rv)
132 {
133         remove (rv);
134 }
135
136 /** Remove a region from the selection.
137  *  @param rv Region to remove.
138  *  @return true if the region was in the selection, false if not.
139  */
140 bool
141 RegionSelection::remove (RegionView* rv)
142 {
143         RegionSelection::iterator r;
144
145         if ((r = find (begin(), end(), rv)) != end()) {
146
147                 // remove from layer sorted list
148                 _bylayer.remove (rv);
149                 
150                 if (size() == 1) {
151
152                         /* this is the last one, so when we delete it
153                            we will be empty.
154                         */
155
156                         _current_start = 0;
157                         _current_end = 0;
158
159                 } else {
160                         
161                         boost::shared_ptr<Region> region ((*r)->region());
162                         
163                         if (region->first_frame() == _current_start) {
164                                 
165                                 /* reset current start */
166                                 
167                                 nframes_t ref = max_frames;
168                                 
169                                 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
170                                         if (region->first_frame() < ref) {
171                                                 ref = region->first_frame();
172                                         }
173                                 }
174                                 
175                                 _current_start = ref;
176                                 
177                         }
178                         
179                         if (region->last_frame() == _current_end) {
180
181                                 /* reset current end */
182                                 
183                                 nframes_t ref = 0;
184                                 
185                                 for (RegionSelection::iterator i = begin (); i != end(); ++i) {
186                                         if (region->first_frame() > ref) {
187                                                 ref = region->first_frame();
188                                         }
189                                 }
190                                 
191                                 _current_end = ref;
192                         }
193                 }
194
195                 erase (r);
196
197                 return true;
198         }
199
200         return false;
201 }
202
203 /** Add a region to the list sorted by layer.
204  *  @param rv Region to add.
205  */
206 void
207 RegionSelection::add_to_layer (RegionView * rv)
208 {
209         // insert it into layer sorted position
210
211         list<RegionView*>::iterator i;
212
213         for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
214         {
215                 if (rv->region()->layer() < (*i)->region()->layer()) {
216                         _bylayer.insert(i, rv);
217                         return;
218                 }
219         }
220
221         // insert at end if we get here
222         _bylayer.insert(i, rv);
223 }
224
225 struct RegionSortByTime {
226     bool operator() (const RegionView* a, const RegionView* b) const {
227             return a->region()->position() < b->region()->position();
228     }
229 };
230
231
232 /**
233  *  @param foo List which will be filled with the selection's regions
234  *  sorted by position.
235  */
236 void
237 RegionSelection::by_position (list<RegionView*>& foo) const
238 {
239         list<RegionView*>::const_iterator i;
240         RegionSortByTime sorter;
241
242         for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
243                 foo.push_back (*i);
244         }
245
246         foo.sort (sorter);
247         return;
248 }
249
250 struct RegionSortByTrack {
251     bool operator() (const RegionView* a, const RegionView* b) const {
252             
253             /* really, track and position */
254
255             if (a->get_trackview().order() == b->get_trackview().order()) {
256                     return a->region()->position() < b->region()->position();
257             } else {
258                     return a->get_trackview().order() < b->get_trackview().order();
259             }
260     }
261 };
262
263
264 /**
265  *  @param List which will be filled with the selection's regions
266  *  sorted by track and position.
267  */
268 void
269 RegionSelection::by_track (list<RegionView*>& foo) const
270 {
271         list<RegionView*>::const_iterator i;
272         RegionSortByTrack sorter;
273
274         for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
275                 foo.push_back (*i);
276         }
277
278         foo.sort (sorter);
279         return;
280 }
281
282 /**
283  *  @param Sort the selection by position and track.
284  */
285 void
286 RegionSelection::sort_by_position_and_track ()
287 {
288         RegionSortByTrack sorter;
289         sort (sorter);
290 }
291
292 /**
293  *  @param tv Track.
294  *  @return true if any of the selection's regions are on tv.
295  */
296 bool
297 RegionSelection::involves (const TimeAxisView& tv) const
298 {
299         for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
300                 if (&(*i)->get_trackview() == &tv) {
301                         return true;
302                 }
303         }
304         return false;
305 }
306