2 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
3 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
4 * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2014-2016 Nick Mainsbridge <mainsbridge@gmail.com>
7 * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "ardour/region.h"
28 #include "gui_thread.h"
29 #include "midi_region_view.h"
30 #include "region_view.h"
31 #include "region_selection.h"
32 #include "time_axis_view.h"
35 using namespace ARDOUR;
38 /** Construct an empty RegionSelection.
40 RegionSelection::RegionSelection ()
42 RegionView::RegionViewGoingAway.connect (death_connection, MISSING_INVALIDATOR, boost::bind (&RegionSelection::remove_it, this, _1), gui_context());
46 * @param other RegionSelection to copy.
48 RegionSelection::RegionSelection (const RegionSelection& other)
49 : std::list<RegionView*>()
51 RegionView::RegionViewGoingAway.connect (death_connection, MISSING_INVALIDATOR, boost::bind (&RegionSelection::remove_it, this, _1), gui_context());
53 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
58 /** operator= to set a RegionSelection to be the same as another.
59 * @param other Other RegionSelection.
62 RegionSelection::operator= (const RegionSelection& other)
68 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
76 /** Empty this RegionSelection.
79 RegionSelection::clear_all()
87 * @param rv RegionView.
88 * @return true if this selection contains rv.
90 bool RegionSelection::contains (RegionView* rv) const
92 return find (begin(), end(), rv) != end();
95 bool RegionSelection::contains (boost::shared_ptr<ARDOUR::Region> region) const
97 for (const_iterator r = begin (); r != end (); ++r) {
98 if ((*r)->region () == region) {
105 /** Add a region to the selection.
106 * @param rv Region to add.
107 * @return false if we already had the region or if it cannot be added,
111 RegionSelection::add (RegionView* rv)
113 if (!rv->region()->playlist()) {
114 /* not attached to a playlist - selection not allowed.
115 This happens if the user tries to select a region
116 during a capture pass.
122 /* we already have it */
128 /* add to layer sorted list */
135 /** Remove a region from the selection.
136 * @param rv Region to remove.
139 RegionSelection::remove_it (RegionView *rv)
144 /** Remove a region from the selection.
145 * @param rv Region to remove.
146 * @return true if the region was in the selection, false if not.
149 RegionSelection::remove (RegionView* rv)
151 RegionSelection::iterator r;
153 if ((r = find (begin(), end(), rv)) != end()) {
155 // remove from layer sorted list
156 _bylayer.remove (rv);
157 pending.remove (rv->region()->id());
165 /** Add a region to the list sorted by layer.
166 * @param rv Region to add.
169 RegionSelection::add_to_layer (RegionView * rv)
171 // insert it into layer sorted position
173 list<RegionView*>::iterator i;
175 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
177 if (rv->region()->layer() < (*i)->region()->layer()) {
178 _bylayer.insert(i, rv);
183 // insert at end if we get here
184 _bylayer.insert(i, rv);
187 struct RegionSortByTime {
188 bool operator() (const RegionView* a, const RegionView* b) const {
189 return a->region()->position() < b->region()->position();
195 * @param foo List which will be filled with the selection's regions
196 * sorted by position.
199 RegionSelection::by_position (list<RegionView*>& foo) const
201 list<RegionView*>::const_iterator i;
202 RegionSortByTime sorter;
204 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
212 struct RegionSortByTrack {
213 bool operator() (const RegionView* a, const RegionView* b) const {
215 /* really, track and position */
217 if (a->get_time_axis_view().order() == b->get_time_axis_view().order()) {
218 return a->region()->position() < b->region()->position();
220 return a->get_time_axis_view().order() < b->get_time_axis_view().order();
227 * @param List which will be filled with the selection's regions
228 * sorted by track and position.
231 RegionSelection::by_track (list<RegionView*>& foo) const
233 list<RegionView*>::const_iterator i;
234 RegionSortByTrack sorter;
236 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
245 * @param Sort the selection by position and track.
248 RegionSelection::sort_by_position_and_track ()
250 RegionSortByTrack sorter;
256 * @return true if any of the selection's regions are on tv.
259 RegionSelection::involves (const TimeAxisView& tv) const
261 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
262 if (&(*i)->get_time_axis_view() == &tv) {
270 RegionSelection::start () const
272 samplepos_t s = max_samplepos;
273 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
274 s = min (s, (*i)->region()->position ());
277 if (s == max_samplepos) {
285 RegionSelection::end_sample () const
288 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
289 e = max (e, (*i)->region()->last_sample ());
295 /** @return the playlists that the regions in the selection are on */
296 set<boost::shared_ptr<Playlist> >
297 RegionSelection::playlists () const
299 set<boost::shared_ptr<Playlist> > pl;
300 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
301 pl.insert ((*i)->region()->playlist ());
308 RegionSelection::n_midi_regions () const
312 for (const_iterator r = begin(); r != end(); ++r) {
313 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
323 RegionSelection::regionlist () const
325 ARDOUR::RegionList rl;
326 for (const_iterator r = begin (); r != end (); ++r) {
327 rl.push_back ((*r)->region ());