Optimize automation-event process splitting
[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 bool RegionSelection::contains (boost::shared_ptr<ARDOUR::Region> region) const
91 {
92         for (const_iterator r = begin (); r != end (); ++r) {
93                 if ((*r)->region () == region) {
94                         return true;
95                 }
96         }
97         return false;
98 }
99
100 /** Add a region to the selection.
101  *  @param rv Region to add.
102  *  @return false if we already had the region or if it cannot be added,
103  *          otherwise true.
104  */
105 bool
106 RegionSelection::add (RegionView* rv)
107 {
108         if (!rv->region()->playlist()) {
109                 /* not attached to a playlist - selection not allowed.
110                    This happens if the user tries to select a region
111                    during a capture pass.
112                 */
113                 return false;
114         }
115
116         if (contains (rv)) {
117                 /* we already have it */
118                 return false;
119         }
120
121         push_back (rv);
122
123         /* add to layer sorted list */
124
125         add_to_layer (rv);
126
127         return true;
128 }
129
130 /** Remove a region from the selection.
131  *  @param rv Region to remove.
132  */
133 void
134 RegionSelection::remove_it (RegionView *rv)
135 {
136         remove (rv);
137 }
138
139 /** Remove a region from the selection.
140  *  @param rv Region to remove.
141  *  @return true if the region was in the selection, false if not.
142  */
143 bool
144 RegionSelection::remove (RegionView* rv)
145 {
146         RegionSelection::iterator r;
147
148         if ((r = find (begin(), end(), rv)) != end()) {
149
150                 // remove from layer sorted list
151                 _bylayer.remove (rv);
152                 pending.remove (rv->region()->id());
153                 erase (r);
154                 return true;
155         }
156
157         return false;
158 }
159
160 /** Add a region to the list sorted by layer.
161  *  @param rv Region to add.
162  */
163 void
164 RegionSelection::add_to_layer (RegionView * rv)
165 {
166         // insert it into layer sorted position
167
168         list<RegionView*>::iterator i;
169
170         for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
171         {
172                 if (rv->region()->layer() < (*i)->region()->layer()) {
173                         _bylayer.insert(i, rv);
174                         return;
175                 }
176         }
177
178         // insert at end if we get here
179         _bylayer.insert(i, rv);
180 }
181
182 struct RegionSortByTime {
183         bool operator() (const RegionView* a, const RegionView* b) const {
184                 return a->region()->position() < b->region()->position();
185         }
186 };
187
188
189 /**
190  *  @param foo List which will be filled with the selection's regions
191  *  sorted by position.
192  */
193 void
194 RegionSelection::by_position (list<RegionView*>& foo) const
195 {
196         list<RegionView*>::const_iterator i;
197         RegionSortByTime sorter;
198
199         for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
200                 foo.push_back (*i);
201         }
202
203         foo.sort (sorter);
204         return;
205 }
206
207 struct RegionSortByTrack {
208         bool operator() (const RegionView* a, const RegionView* b) const {
209
210                 /* really, track and position */
211
212                 if (a->get_time_axis_view().order() == b->get_time_axis_view().order()) {
213                         return a->region()->position() < b->region()->position();
214                 } else {
215                         return a->get_time_axis_view().order() < b->get_time_axis_view().order();
216                 }
217         }
218 };
219
220
221 /**
222  *  @param List which will be filled with the selection's regions
223  *  sorted by track and position.
224  */
225 void
226 RegionSelection::by_track (list<RegionView*>& foo) const
227 {
228         list<RegionView*>::const_iterator i;
229         RegionSortByTrack sorter;
230
231         for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
232                 foo.push_back (*i);
233         }
234
235         foo.sort (sorter);
236         return;
237 }
238
239 /**
240  *  @param Sort the selection by position and track.
241  */
242 void
243 RegionSelection::sort_by_position_and_track ()
244 {
245         RegionSortByTrack sorter;
246         sort (sorter);
247 }
248
249 /**
250  *  @param tv Track.
251  *  @return true if any of the selection's regions are on tv.
252  */
253 bool
254 RegionSelection::involves (const TimeAxisView& tv) const
255 {
256         for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
257                 if (&(*i)->get_time_axis_view() == &tv) {
258                         return true;
259                 }
260         }
261         return false;
262 }
263
264 samplepos_t
265 RegionSelection::start () const
266 {
267         samplepos_t s = max_samplepos;
268         for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
269                 s = min (s, (*i)->region()->position ());
270         }
271
272         if (s == max_samplepos) {
273                 return 0;
274         }
275
276         return s;
277 }
278
279 samplepos_t
280 RegionSelection::end_sample () const
281 {
282         samplepos_t e = 0;
283         for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
284                 e = max (e, (*i)->region()->last_sample ());
285         }
286
287         return e;
288 }
289
290 /** @return the playlists that the regions in the selection are on */
291 set<boost::shared_ptr<Playlist> >
292 RegionSelection::playlists () const
293 {
294         set<boost::shared_ptr<Playlist> > pl;
295         for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
296                 pl.insert ((*i)->region()->playlist ());
297         }
298
299         return pl;
300 }
301
302 size_t
303 RegionSelection::n_midi_regions () const
304 {
305         size_t count = 0;
306
307         for (const_iterator r = begin(); r != end(); ++r) {
308                 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
309                 if (mrv) {
310                         ++count;
311                 }
312         }
313
314         return count;
315 }
316
317 ARDOUR::RegionList
318 RegionSelection::regionlist () const
319 {
320         ARDOUR::RegionList rl;
321         for (const_iterator r = begin (); r != end (); ++r) {
322                 rl.push_back ((*r)->region ());
323         }
324         return rl;
325 }