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