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 "region_view.h"
25 #include "region_selection.h"
26 #include "time_axis_view.h"
29 using namespace ARDOUR;
32 /** Construct an empty RegionSelection.
34 RegionSelection::RegionSelection ()
36 RegionView::RegionViewGoingAway.connect (death_connection, MISSING_INVALIDATOR, boost::bind (&RegionSelection::remove_it, this, _1), gui_context());
40 * @param other RegionSelection to copy.
42 RegionSelection::RegionSelection (const RegionSelection& other)
43 : std::list<RegionView*>()
45 RegionView::RegionViewGoingAway.connect (death_connection, MISSING_INVALIDATOR, boost::bind (&RegionSelection::remove_it, this, _1), gui_context());
47 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
52 /** operator= to set a RegionSelection to be the same as another.
53 * @param other Other RegionSelection.
56 RegionSelection::operator= (const RegionSelection& other)
62 for (RegionSelection::const_iterator i = other.begin(); i != other.end(); ++i) {
70 /** Empty this RegionSelection.
73 RegionSelection::clear_all()
80 * @param rv RegionView.
81 * @return true if this selection contains rv.
83 bool RegionSelection::contains (RegionView* rv) const
85 return find (begin(), end(), rv) != end();
88 /** Add a region to the selection.
89 * @param rv Region to add.
90 * @return false if we already had the region or if it cannot be added,
94 RegionSelection::add (RegionView* rv)
96 if (!rv->region()->playlist()) {
97 /* not attached to a playlist - selection not allowed.
98 This happens if the user tries to select a region
99 during a capture pass.
105 /* we already have it */
111 /* add to layer sorted list */
118 /** Remove a region from the selection.
119 * @param rv Region to remove.
122 RegionSelection::remove_it (RegionView *rv)
127 /** Remove a region from the selection.
128 * @param rv Region to remove.
129 * @return true if the region was in the selection, false if not.
132 RegionSelection::remove (RegionView* rv)
134 RegionSelection::iterator r;
136 if ((r = find (begin(), end(), rv)) != end()) {
138 // remove from layer sorted list
139 _bylayer.remove (rv);
148 /** Add a region to the list sorted by layer.
149 * @param rv Region to add.
152 RegionSelection::add_to_layer (RegionView * rv)
154 // insert it into layer sorted position
156 list<RegionView*>::iterator i;
158 for (i = _bylayer.begin(); i != _bylayer.end(); ++i)
160 if (rv->region()->layer() < (*i)->region()->layer()) {
161 _bylayer.insert(i, rv);
166 // insert at end if we get here
167 _bylayer.insert(i, rv);
170 struct RegionSortByTime {
171 bool operator() (const RegionView* a, const RegionView* b) const {
172 return a->region()->position() < b->region()->position();
178 * @param foo List which will be filled with the selection's regions
179 * sorted by position.
182 RegionSelection::by_position (list<RegionView*>& foo) const
184 list<RegionView*>::const_iterator i;
185 RegionSortByTime sorter;
187 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
195 struct RegionSortByTrack {
196 bool operator() (const RegionView* a, const RegionView* b) const {
198 /* really, track and position */
200 if (a->get_time_axis_view().order() == b->get_time_axis_view().order()) {
201 return a->region()->position() < b->region()->position();
203 return a->get_time_axis_view().order() < b->get_time_axis_view().order();
210 * @param List which will be filled with the selection's regions
211 * sorted by track and position.
214 RegionSelection::by_track (list<RegionView*>& foo) const
216 list<RegionView*>::const_iterator i;
217 RegionSortByTrack sorter;
219 for (i = _bylayer.begin(); i != _bylayer.end(); ++i) {
228 * @param Sort the selection by position and track.
231 RegionSelection::sort_by_position_and_track ()
233 RegionSortByTrack sorter;
239 * @return true if any of the selection's regions are on tv.
242 RegionSelection::involves (const TimeAxisView& tv) const
244 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
245 if (&(*i)->get_time_axis_view() == &tv) {
253 RegionSelection::start () const
255 framepos_t s = max_framepos;
256 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
257 s = min (s, (*i)->region()->position ());
260 if (s == max_framepos) {
268 RegionSelection::end_frame () const
271 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
272 e = max (e, (*i)->region()->last_frame ());
278 /** @return the playlists that the regions in the selection are on */
279 set<boost::shared_ptr<Playlist> >
280 RegionSelection::playlists () const
282 set<boost::shared_ptr<Playlist> > pl;
283 for (RegionSelection::const_iterator i = begin(); i != end(); ++i) {
284 pl.insert ((*i)->region()->playlist ());