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