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