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 /** Add a region to the selection.
91 * @param rv Region to add.
92 * @return false if we already had the region or if it cannot be added,
96 RegionSelection::add (RegionView* rv)
98 if (!rv->region()->playlist()) {
99 /* not attached to a playlist - selection not allowed.
100 This happens if the user tries to select a region
101 during a capture pass.
107 /* we already have it */
113 /* add to layer sorted list */
120 /** Remove a region from the selection.
121 * @param rv Region to remove.
124 RegionSelection::remove_it (RegionView *rv)
129 /** Remove a region from the selection.
130 * @param rv Region to remove.
131 * @return true if the region was in the selection, false if not.
134 RegionSelection::remove (RegionView* rv)
136 RegionSelection::iterator r;
138 if ((r = find (begin(), end(), rv)) != end()) {
140 // remove from layer sorted list
141 _bylayer.remove (rv);
150 /** Add a region to the list sorted by layer.
151 * @param rv Region to add.
154 RegionSelection::add_to_layer (RegionView * rv)
156 // insert it into layer sorted position
158 list<RegionView*>::iterator i;
160 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
162 if (rv->region()->layer() < (*i)->region()->layer()) {
163 _bylayer.insert(i, rv);
168 // insert at end if we get here
169 _bylayer.insert(i, rv);
172 struct RegionSortByTime {
173 bool operator() (const RegionView* a, const RegionView* b) const {
174 return a->region()->position() < b->region()->position();
180 * @param foo List which will be filled with the selection's regions
181 * sorted by position.
184 RegionSelection::by_position (list<RegionView*>& foo) const
186 list<RegionView*>::const_iterator i;
187 RegionSortByTime sorter;
189 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
197 struct RegionSortByTrack {
198 bool operator() (const RegionView* a, const RegionView* b) const {
200 /* really, track and position */
202 if (a->get_time_axis_view().order() == b->get_time_axis_view().order()) {
203 return a->region()->position() < b->region()->position();
205 return a->get_time_axis_view().order() < b->get_time_axis_view().order();
212 * @param List which will be filled with the selection's regions
213 * sorted by track and position.
216 RegionSelection::by_track (list<RegionView*>& foo) const
218 list<RegionView*>::const_iterator i;
219 RegionSortByTrack sorter;
221 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
230 * @param Sort the selection by position and track.
233 RegionSelection::sort_by_position_and_track ()
235 RegionSortByTrack sorter;
241 * @return true if any of the selection's regions are on tv.
244 RegionSelection::involves (const TimeAxisView& tv) const
246 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
247 if (&(*i)->get_time_axis_view() == &tv) {
255 RegionSelection::start () const
257 framepos_t s = max_framepos;
258 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
259 s = min (s, (*i)->region()->position ());
262 if (s == max_framepos) {
270 RegionSelection::end_frame () const
273 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
274 e = max (e, (*i)->region()->last_frame ());
280 /** @return the playlists that the regions in the selection are on */
281 set<boost::shared_ptr<Playlist> >
282 RegionSelection::playlists () const
284 set<boost::shared_ptr<Playlist> > pl;
285 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
286 pl.insert ((*i)->region()->playlist ());
293 RegionSelection::n_midi_regions () const
297 for (const_iterator r = begin(); r != end(); ++r) {
298 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);