2 Copyright (C) 2006 Paul Davis
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.
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.
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.
21 #include "ardour/region.h"
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"
30 using namespace ARDOUR;
33 /** Construct an empty RegionSelection.
35 RegionSelection::RegionSelection ()
37 RegionView::RegionViewGoingAway.connect (death_connection, MISSING_INVALIDATOR, boost::bind (&RegionSelection::remove_it, this, _1), gui_context());
41 * @param other RegionSelection to copy.
43 RegionSelection::RegionSelection (const RegionSelection& other)
44 : std::list<RegionView*>()
46 RegionView::RegionViewGoingAway.connect (death_connection, MISSING_INVALIDATOR, boost::bind (&RegionSelection::remove_it, this, _1), gui_context());
48 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
53 /** operator= to set a RegionSelection to be the same as another.
54 * @param other Other RegionSelection.
57 RegionSelection::operator= (const RegionSelection& other)
63 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
71 /** Empty this RegionSelection.
74 RegionSelection::clear_all()
82 * @param rv RegionView.
83 * @return true if this selection contains rv.
85 bool RegionSelection::contains (RegionView* rv) const
87 return find (begin(), end(), rv) != end();
90 bool RegionSelection::contains (boost::shared_ptr<ARDOUR::Region> region) const
92 for (const_iterator r = begin (); r != end (); ++r) {
93 if ((*r)->region () == region) {
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,
106 RegionSelection::add (RegionView* rv)
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.
117 /* we already have it */
123 /* add to layer sorted list */
130 /** Remove a region from the selection.
131 * @param rv Region to remove.
134 RegionSelection::remove_it (RegionView *rv)
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.
144 RegionSelection::remove (RegionView* rv)
146 RegionSelection::iterator r;
148 if ((r = find (begin(), end(), rv)) != end()) {
150 // remove from layer sorted list
151 _bylayer.remove (rv);
152 pending.remove (rv->region()->id());
160 /** Add a region to the list sorted by layer.
161 * @param rv Region to add.
164 RegionSelection::add_to_layer (RegionView * rv)
166 // insert it into layer sorted position
168 list<RegionView*>::iterator i;
170 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
172 if (rv->region()->layer() < (*i)->region()->layer()) {
173 _bylayer.insert(i, rv);
178 // insert at end if we get here
179 _bylayer.insert(i, rv);
182 struct RegionSortByTime {
183 bool operator() (const RegionView* a, const RegionView* b) const {
184 return a->region()->position() < b->region()->position();
190 * @param foo List which will be filled with the selection's regions
191 * sorted by position.
194 RegionSelection::by_position (list<RegionView*>& foo) const
196 list<RegionView*>::const_iterator i;
197 RegionSortByTime sorter;
199 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
207 struct RegionSortByTrack {
208 bool operator() (const RegionView* a, const RegionView* b) const {
210 /* really, track and position */
212 if (a->get_time_axis_view().order() == b->get_time_axis_view().order()) {
213 return a->region()->position() < b->region()->position();
215 return a->get_time_axis_view().order() < b->get_time_axis_view().order();
222 * @param List which will be filled with the selection's regions
223 * sorted by track and position.
226 RegionSelection::by_track (list<RegionView*>& foo) const
228 list<RegionView*>::const_iterator i;
229 RegionSortByTrack sorter;
231 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
240 * @param Sort the selection by position and track.
243 RegionSelection::sort_by_position_and_track ()
245 RegionSortByTrack sorter;
251 * @return true if any of the selection's regions are on tv.
254 RegionSelection::involves (const TimeAxisView& tv) const
256 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
257 if (&(*i)->get_time_axis_view() == &tv) {
265 RegionSelection::start () const
267 framepos_t s = max_framepos;
268 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
269 s = min (s, (*i)->region()->position ());
272 if (s == max_framepos) {
280 RegionSelection::end_frame () const
283 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
284 e = max (e, (*i)->region()->last_frame ());
290 /** @return the playlists that the regions in the selection are on */
291 set<boost::shared_ptr<Playlist> >
292 RegionSelection::playlists () const
294 set<boost::shared_ptr<Playlist> > pl;
295 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
296 pl.insert ((*i)->region()->playlist ());
303 RegionSelection::n_midi_regions () const
307 for (const_iterator r = begin(); r != end(); ++r) {
308 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
318 RegionSelection::regionlist () const
320 ARDOUR::RegionList rl;
321 for (const_iterator r = begin (); r != end (); ++r) {
322 rl.push_back ((*r)->region ());