2 Copyright (C) 2002 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 <sigc++/bind.h>
22 #include "pbd/error.h"
23 #include "pbd/stacktrace.h"
25 #include "ardour/playlist.h"
26 #include "ardour/rc_configuration.h"
28 #include "gui_thread.h"
29 #include "midi_cut_buffer.h"
30 #include "region_view.h"
31 #include "selection.h"
32 #include "selection_templates.h"
33 #include "time_axis_view.h"
34 #include "automation_time_axis.h"
35 #include "public_editor.h"
40 using namespace ARDOUR
;
43 struct AudioRangeComparator
{
44 bool operator()(AudioRange a
, AudioRange b
) {
45 return a
.start
< b
.start
;
49 Selection::Selection (const PublicEditor
* e
)
56 /* we have disambiguate which remove() for the compiler */
58 void (Selection::*track_remove
)(TimeAxisView
*) = &Selection::remove
;
59 TimeAxisView::CatchDeletion
.connect (*this, MISSING_INVALIDATOR
, ui_bind (track_remove
, this, _1
), gui_context());
61 void (Selection::*marker_remove
)(Marker
*) = &Selection::remove
;
62 Marker::CatchDeletion
.connect (*this, MISSING_INVALIDATOR
, ui_bind (marker_remove
, this, _1
), gui_context());
67 Selection::operator= (const Selection
& other
)
70 regions
= other
.regions
;
71 tracks
= other
.tracks
;
74 midi_regions
= other
.midi_regions
;
75 midi_notes
= other
.midi_notes
;
82 operator== (const Selection
& a
, const Selection
& b
)
84 return a
.regions
== b
.regions
&&
85 a
.tracks
== b
.tracks
&&
88 a
.playlists
== b
.playlists
&&
89 a
.midi_notes
== b
.midi_notes
&&
90 a
.midi_regions
== b
.midi_regions
;
93 /** Clear everything from the Selection */
104 clear_midi_regions ();
108 Selection::dump_region_layers()
110 cerr
<< "region selection layer dump" << endl
;
111 for (RegionSelection::iterator i
= regions
.begin(); i
!= regions
.end(); ++i
) {
112 cerr
<< "layer: " << (int)(*i
)->region()->layer() << endl
;
118 Selection::clear_regions ()
120 if (!regions
.empty()) {
121 regions
.clear_all ();
127 Selection::clear_tracks ()
129 if (!tracks
.empty()) {
136 Selection::clear_midi_notes ()
138 if (!midi_notes
.empty()) {
139 for (MidiNoteSelection::iterator x
= midi_notes
.begin(); x
!= midi_notes
.end(); ++x
) {
148 Selection::clear_midi_regions ()
150 if (!midi_regions
.empty()) {
151 midi_regions
.clear ();
152 MidiRegionsChanged ();
157 Selection::clear_time ()
165 Selection::clear_playlists ()
167 /* Selections own their playlists */
169 for (PlaylistSelection::iterator i
= playlists
.begin(); i
!= playlists
.end(); ++i
) {
170 /* selections own their own regions, which are copies of the "originals". make them go away */
171 (*i
)->drop_regions ();
175 if (!playlists
.empty()) {
182 Selection::clear_lines ()
184 if (!lines
.empty()) {
191 Selection::clear_markers ()
193 if (!markers
.empty()) {
200 Selection::toggle (boost::shared_ptr
<Playlist
> pl
)
202 PlaylistSelection::iterator i
;
204 if ((i
= find (playlists
.begin(), playlists
.end(), pl
)) == playlists
.end()) {
206 playlists
.push_back(pl
);
215 Selection::toggle (const TrackViewList
& track_list
)
217 for (TrackViewList::const_iterator i
= track_list
.begin(); i
!= track_list
.end(); ++i
) {
223 Selection::toggle (TimeAxisView
* track
)
225 TrackSelection::iterator i
;
227 if ((i
= find (tracks
.begin(), tracks
.end(), track
)) == tracks
.end()) {
228 tracks
.push_back (track
);
237 Selection::toggle (const MidiNoteSelection
& midi_note_list
)
239 for (MidiNoteSelection::const_iterator i
= midi_note_list
.begin(); i
!= midi_note_list
.end(); ++i
) {
245 Selection::toggle (MidiCutBuffer
* midi
)
247 MidiNoteSelection::iterator i
;
249 if ((i
= find (midi_notes
.begin(), midi_notes
.end(), midi
)) == midi_notes
.end()) {
250 midi_notes
.push_back (midi
);
252 /* remember that we own the MCB */
254 midi_notes
.erase (i
);
262 Selection::toggle (RegionView
* r
)
264 RegionSelection::iterator i
;
266 if ((i
= find (regions
.begin(), regions
.end(), r
)) == regions
.end()) {
276 Selection::toggle (MidiRegionView
* mrv
)
278 MidiRegionSelection::iterator i
;
280 if ((i
= find (midi_regions
.begin(), midi_regions
.end(), mrv
)) == midi_regions
.end()) {
283 midi_regions
.erase (i
);
286 MidiRegionsChanged ();
290 Selection::toggle (vector
<RegionView
*>& r
)
292 RegionSelection::iterator i
;
294 for (vector
<RegionView
*>::iterator x
= r
.begin(); x
!= r
.end(); ++x
) {
295 if ((i
= find (regions
.begin(), regions
.end(), (*x
))) == regions
.end()) {
306 Selection::toggle (nframes_t start
, nframes_t end
)
308 AudioRangeComparator cmp
;
310 /* XXX this implementation is incorrect */
312 time
.push_back (AudioRange (start
, end
, next_time_id
++));
318 return next_time_id
- 1;
322 Selection::add (boost::shared_ptr
<Playlist
> pl
)
324 if (find (playlists
.begin(), playlists
.end(), pl
) == playlists
.end()) {
326 playlists
.push_back(pl
);
332 Selection::add (const list
<boost::shared_ptr
<Playlist
> >& pllist
)
334 bool changed
= false;
336 for (list
<boost::shared_ptr
<Playlist
> >::const_iterator i
= pllist
.begin(); i
!= pllist
.end(); ++i
) {
337 if (find (playlists
.begin(), playlists
.end(), (*i
)) == playlists
.end()) {
339 playlists
.push_back (*i
);
350 Selection::add (const TrackViewList
& track_list
)
352 TrackViewList added
= tracks
.add (track_list
);
354 if (!added
.empty()) {
360 Selection::add (TimeAxisView
* track
)
363 tr
.push_back (track
);
368 Selection::add (const MidiNoteSelection
& midi_list
)
370 const MidiNoteSelection::const_iterator b
= midi_list
.begin();
371 const MidiNoteSelection::const_iterator e
= midi_list
.end();
373 if (!midi_list
.empty()) {
374 midi_notes
.insert (midi_notes
.end(), b
, e
);
380 Selection::add (MidiCutBuffer
* midi
)
382 /* we take ownership of the MCB */
384 if (find (midi_notes
.begin(), midi_notes
.end(), midi
) == midi_notes
.end()) {
385 midi_notes
.push_back (midi
);
391 Selection::add (vector
<RegionView
*>& v
)
393 /* XXX This method or the add (const RegionSelection&) needs to go
396 bool changed
= false;
398 for (vector
<RegionView
*>::iterator i
= v
.begin(); i
!= v
.end(); ++i
) {
399 if (find (regions
.begin(), regions
.end(), (*i
)) == regions
.end()) {
400 changed
= regions
.add ((*i
));
401 if (Config
->get_link_region_and_track_selection() && changed
) {
402 add (&(*i
)->get_trackview());
413 Selection::add (const RegionSelection
& rs
)
415 /* XXX This method or the add (const vector<RegionView*>&) needs to go
418 bool changed
= false;
420 for (RegionSelection::const_iterator i
= rs
.begin(); i
!= rs
.end(); ++i
) {
421 if (find (regions
.begin(), regions
.end(), (*i
)) == regions
.end()) {
422 changed
= regions
.add ((*i
));
423 if (Config
->get_link_region_and_track_selection() && changed
) {
424 add (&(*i
)->get_trackview());
435 Selection::add (RegionView
* r
)
437 if (find (regions
.begin(), regions
.end(), r
) == regions
.end()) {
439 if (Config
->get_link_region_and_track_selection()) {
440 add (&r
->get_trackview());
447 Selection::add (MidiRegionView
* mrv
)
449 if (find (midi_regions
.begin(), midi_regions
.end(), mrv
) == midi_regions
.end()) {
450 midi_regions
.push_back (mrv
);
451 /* XXX should we do this? */
453 if (Config
->get_link_region_and_track_selection()) {
454 add (&mrv
->get_trackview());
457 MidiRegionsChanged ();
462 Selection::add (nframes_t start
, nframes_t end
)
464 AudioRangeComparator cmp
;
466 /* XXX this implementation is incorrect */
468 time
.push_back (AudioRange (start
, end
, next_time_id
++));
474 return next_time_id
- 1;
478 Selection::replace (uint32_t sid
, nframes_t start
, nframes_t end
)
480 for (list
<AudioRange
>::iterator i
= time
.begin(); i
!= time
.end(); ++i
) {
481 if ((*i
).id
== sid
) {
483 time
.push_back (AudioRange(start
,end
, sid
));
485 /* don't consolidate here */
488 AudioRangeComparator cmp
;
498 Selection::add (boost::shared_ptr
<Evoral::ControlList
> cl
)
500 boost::shared_ptr
<ARDOUR::AutomationList
> al
501 = boost::dynamic_pointer_cast
<ARDOUR::AutomationList
>(cl
);
503 warning
<< "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg
;
507 if (find (lines
.begin(), lines
.end(), al
) == lines
.end()) {
508 lines
.push_back (al
);
514 Selection::remove (TimeAxisView
* track
)
516 list
<TimeAxisView
*>::iterator i
;
517 if ((i
= find (tracks
.begin(), tracks
.end(), track
)) != tracks
.end()) {
524 Selection::remove (const TrackViewList
& track_list
)
526 bool changed
= false;
528 for (TrackViewList::const_iterator i
= track_list
.begin(); i
!= track_list
.end(); ++i
) {
530 TrackViewList::iterator x
= find (tracks
.begin(), tracks
.end(), *i
);
531 if (x
!= tracks
.end()) {
543 Selection::remove (const MidiNoteSelection
& midi_list
)
545 bool changed
= false;
547 for (MidiNoteSelection::const_iterator i
= midi_list
.begin(); i
!= midi_list
.end(); ++i
) {
549 MidiNoteSelection::iterator x
;
551 if ((x
= find (midi_notes
.begin(), midi_notes
.end(), (*i
))) != midi_notes
.end()) {
552 midi_notes
.erase (x
);
563 Selection::remove (MidiCutBuffer
* midi
)
565 MidiNoteSelection::iterator x
;
567 if ((x
= find (midi_notes
.begin(), midi_notes
.end(), midi
)) != midi_notes
.end()) {
568 /* remember that we own the MCB */
570 midi_notes
.erase (x
);
576 Selection::remove (boost::shared_ptr
<Playlist
> track
)
578 list
<boost::shared_ptr
<Playlist
> >::iterator i
;
579 if ((i
= find (playlists
.begin(), playlists
.end(), track
)) != playlists
.end()) {
586 Selection::remove (const list
<boost::shared_ptr
<Playlist
> >& pllist
)
588 bool changed
= false;
590 for (list
<boost::shared_ptr
<Playlist
> >::const_iterator i
= pllist
.begin(); i
!= pllist
.end(); ++i
) {
592 list
<boost::shared_ptr
<Playlist
> >::iterator x
;
594 if ((x
= find (playlists
.begin(), playlists
.end(), (*i
))) != playlists
.end()) {
606 Selection::remove (RegionView
* r
)
608 if (regions
.remove (r
)) {
612 if (Config
->get_link_region_and_track_selection() && !regions
.involves (r
->get_trackview())) {
613 remove (&r
->get_trackview());
618 Selection::remove (MidiRegionView
* mrv
)
620 MidiRegionSelection::iterator x
;
622 if ((x
= find (midi_regions
.begin(), midi_regions
.end(), mrv
)) != midi_regions
.end()) {
623 midi_regions
.erase (x
);
624 MidiRegionsChanged ();
628 /* XXX fix this up ? */
629 if (Config
->get_link_region_and_track_selection() && !regions
.involves (r
->get_trackview())) {
630 remove (&r
->get_trackview());
637 Selection::remove (uint32_t selection_id
)
643 for (list
<AudioRange
>::iterator i
= time
.begin(); i
!= time
.end(); ++i
) {
644 if ((*i
).id
== selection_id
) {
654 Selection::remove (nframes_t
/*start*/, nframes_t
/*end*/)
659 Selection::remove (boost::shared_ptr
<ARDOUR::AutomationList
> ac
)
661 AutomationSelection::iterator i
;
662 if ((i
= find (lines
.begin(), lines
.end(), ac
)) != lines
.end()) {
669 Selection::set (TimeAxisView
* track
)
676 Selection::set (const TrackViewList
& track_list
)
683 Selection::set (const MidiNoteSelection
& midi_list
)
690 Selection::set (boost::shared_ptr
<Playlist
> playlist
)
697 Selection::set (const list
<boost::shared_ptr
<Playlist
> >& pllist
)
704 Selection::set (const RegionSelection
& rs
)
708 RegionsChanged(); /* EMIT SIGNAL */
712 Selection::set (MidiRegionView
* mrv
)
714 clear_midi_regions ();
719 Selection::set (RegionView
* r
, bool also_clear_tracks
)
722 if (also_clear_tracks
) {
729 Selection::set (vector
<RegionView
*>& v
)
732 if (Config
->get_link_region_and_track_selection()) {
734 // make sure to deselect any automation selections
740 /** Set the start and end time of the time selection, without changing
741 * the list of tracks it applies to.
744 Selection::set (nframes_t start
, nframes_t end
)
746 if ((start
== 0 && end
== 0) || end
< start
) {
751 time
.push_back (AudioRange (start
, end
, next_time_id
++));
753 /* reuse the first entry, and remove all the rest */
755 while (time
.size() > 1) {
758 time
.front().start
= start
;
759 time
.front().end
= end
;
766 return time
.front().id
;
770 Selection::set (boost::shared_ptr
<Evoral::ControlList
> ac
)
777 Selection::selected (Marker
* m
)
779 return find (markers
.begin(), markers
.end(), m
) != markers
.end();
783 Selection::selected (TimeAxisView
* tv
)
785 return find (tracks
.begin(), tracks
.end(), tv
) != tracks
.end();
789 Selection::selected (RegionView
* rv
)
791 return find (regions
.begin(), regions
.end(), rv
) != regions
.end();
795 Selection::empty (bool internal_selection
)
797 bool object_level_empty
= regions
.empty () &&
800 playlists
.empty () &&
803 playlists
.empty () &&
808 if (!internal_selection
) {
809 return object_level_empty
;
812 /* this is intended to really only apply when using a Selection
816 return object_level_empty
&& midi_notes
.empty();
820 Selection::toggle (const vector
<AutomationSelectable
*>& autos
)
822 for (vector
<AutomationSelectable
*>::const_iterator x
= autos
.begin(); x
!= autos
.end(); ++x
) {
823 if ((*x
)->get_selected()) {
826 points
.push_back (**x
);
832 PointsChanged (); /* EMIT SIGNAL */
836 Selection::toggle (list
<Selectable
*> const & selectables
)
839 AutomationSelectable
* as
;
840 vector
<RegionView
*> rvs
;
841 vector
<AutomationSelectable
*> autos
;
843 for (std::list
<Selectable
*>::const_iterator i
= selectables
.begin(); i
!= selectables
.end(); ++i
) {
844 if ((rv
= dynamic_cast<RegionView
*> (*i
)) != 0) {
846 } else if ((as
= dynamic_cast<AutomationSelectable
*> (*i
)) != 0) {
847 autos
.push_back (as
);
849 fatal
<< _("programming error: ")
850 << X_("unknown selectable type passed to Selection::toggle()")
860 if (!autos
.empty()) {
866 Selection::set (list
<Selectable
*> const & selectables
)
875 Selection::add (list
<Selectable
*> const & selectables
)
878 AutomationSelectable
* as
;
879 vector
<RegionView
*> rvs
;
880 vector
<AutomationSelectable
*> autos
;
882 for (std::list
<Selectable
*>::const_iterator i
= selectables
.begin(); i
!= selectables
.end(); ++i
) {
883 if ((rv
= dynamic_cast<RegionView
*> (*i
)) != 0) {
885 } else if ((as
= dynamic_cast<AutomationSelectable
*> (*i
)) != 0) {
886 autos
.push_back (as
);
888 fatal
<< _("programming error: ")
889 << X_("unknown selectable type passed to Selection::add()")
899 if (!autos
.empty()) {
905 Selection::clear_points ()
907 if (!points
.empty()) {
914 Selection::add (vector
<AutomationSelectable
*>& autos
)
916 for (vector
<AutomationSelectable
*>::iterator i
= autos
.begin(); i
!= autos
.end(); ++i
) {
917 points
.push_back (**i
);
924 Selection::set (Marker
* m
)
931 Selection::toggle (Marker
* m
)
933 MarkerSelection::iterator i
;
935 if ((i
= find (markers
.begin(), markers
.end(), m
)) == markers
.end()) {
943 Selection::remove (Marker
* m
)
945 MarkerSelection::iterator i
;
947 if ((i
= find (markers
.begin(), markers
.end(), m
)) != markers
.end()) {
954 Selection::add (Marker
* m
)
956 if (find (markers
.begin(), markers
.end(), m
) == markers
.end()) {
957 markers
.push_back (m
);
963 Selection::add (const list
<Marker
*>& m
)
965 markers
.insert (markers
.end(), m
.begin(), m
.end());
970 MarkerSelection::range (nframes64_t
& s
, nframes64_t
& e
)
975 for (MarkerSelection::iterator i
= begin(); i
!= end(); ++i
) {
977 if ((*i
)->position() < s
) {
978 s
= (*i
)->position();
981 if ((*i
)->position() > e
) {
982 e
= (*i
)->position();