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