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"
36 #include "control_point.h"
41 using namespace ARDOUR;
44 struct AudioRangeComparator {
45 bool operator()(AudioRange a, AudioRange b) {
46 return a.start < b.start;
50 Selection::Selection (const PublicEditor* e)
54 , _no_tracks_changed (false)
58 /* we have disambiguate which remove() for the compiler */
60 void (Selection::*track_remove)(TimeAxisView*) = &Selection::remove;
61 TimeAxisView::CatchDeletion.connect (*this, MISSING_INVALIDATOR, ui_bind (track_remove, this, _1), gui_context());
63 void (Selection::*marker_remove)(Marker*) = &Selection::remove;
64 Marker::CatchDeletion.connect (*this, MISSING_INVALIDATOR, ui_bind (marker_remove, this, _1), gui_context());
69 Selection::operator= (const Selection& other)
72 regions = other.regions;
73 tracks = other.tracks;
76 midi_regions = other.midi_regions;
77 midi_notes = other.midi_notes;
84 operator== (const Selection& a, const Selection& b)
86 return a.regions == b.regions &&
87 a.tracks == b.tracks &&
90 a.playlists == b.playlists &&
91 a.midi_notes == b.midi_notes &&
92 a.midi_regions == b.midi_regions;
95 /** Clear everything from the Selection */
106 clear_midi_regions ();
110 Selection::dump_region_layers()
112 cerr << "region selection layer dump" << endl;
113 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
114 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
120 Selection::clear_regions ()
122 if (!regions.empty()) {
123 regions.clear_all ();
129 Selection::clear_tracks ()
131 if (!tracks.empty()) {
133 if (!_no_tracks_changed) {
140 Selection::clear_midi_notes ()
142 if (!midi_notes.empty()) {
143 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
152 Selection::clear_midi_regions ()
154 if (!midi_regions.empty()) {
155 midi_regions.clear ();
156 MidiRegionsChanged ();
161 Selection::clear_time ()
169 Selection::clear_playlists ()
171 /* Selections own their playlists */
173 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
174 /* selections own their own regions, which are copies of the "originals". make them go away */
175 (*i)->drop_regions ();
179 if (!playlists.empty()) {
186 Selection::clear_lines ()
188 if (!lines.empty()) {
195 Selection::clear_markers ()
197 if (!markers.empty()) {
204 Selection::toggle (boost::shared_ptr<Playlist> pl)
206 PlaylistSelection::iterator i;
208 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
210 playlists.push_back(pl);
219 Selection::toggle (const TrackViewList& track_list)
221 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
227 Selection::toggle (TimeAxisView* track)
229 TrackSelection::iterator i;
231 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
232 tracks.push_back (track);
237 if (!_no_tracks_changed) {
243 Selection::toggle (const MidiNoteSelection& midi_note_list)
245 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
251 Selection::toggle (MidiCutBuffer* midi)
253 MidiNoteSelection::iterator i;
255 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
256 midi_notes.push_back (midi);
258 /* remember that we own the MCB */
260 midi_notes.erase (i);
268 Selection::toggle (RegionView* r)
270 RegionSelection::iterator i;
272 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
282 Selection::toggle (MidiRegionView* mrv)
284 MidiRegionSelection::iterator i;
286 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
289 midi_regions.erase (i);
292 MidiRegionsChanged ();
296 Selection::toggle (vector<RegionView*>& r)
298 RegionSelection::iterator i;
300 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
301 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
312 Selection::toggle (framepos_t start, framepos_t end)
314 AudioRangeComparator cmp;
316 /* XXX this implementation is incorrect */
318 time.push_back (AudioRange (start, end, next_time_id++));
324 return next_time_id - 1;
328 Selection::add (boost::shared_ptr<Playlist> pl)
330 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
332 playlists.push_back(pl);
338 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
340 bool changed = false;
342 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
343 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
345 playlists.push_back (*i);
356 Selection::add (const TrackViewList& track_list)
358 TrackViewList added = tracks.add (track_list);
360 if (!added.empty()) {
361 if (!_no_tracks_changed) {
368 Selection::add (TimeAxisView* track)
371 tr.push_back (track);
376 Selection::add (const MidiNoteSelection& midi_list)
378 const MidiNoteSelection::const_iterator b = midi_list.begin();
379 const MidiNoteSelection::const_iterator e = midi_list.end();
381 if (!midi_list.empty()) {
382 midi_notes.insert (midi_notes.end(), b, e);
388 Selection::add (MidiCutBuffer* midi)
390 /* we take ownership of the MCB */
392 if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
393 midi_notes.push_back (midi);
399 Selection::add (vector<RegionView*>& v)
401 /* XXX This method or the add (const RegionSelection&) needs to go
404 bool changed = false;
406 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
407 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
408 changed = regions.add ((*i));
409 if (Config->get_link_region_and_track_selection() && changed) {
410 add (&(*i)->get_time_axis_view());
421 Selection::add (const RegionSelection& rs)
423 /* XXX This method or the add (const vector<RegionView*>&) needs to go
426 bool changed = false;
428 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
429 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
430 changed = regions.add ((*i));
431 if (Config->get_link_region_and_track_selection() && changed) {
432 add (&(*i)->get_time_axis_view());
443 Selection::add (RegionView* r)
445 if (find (regions.begin(), regions.end(), r) == regions.end()) {
446 bool changed = regions.add (r);
447 if (Config->get_link_region_and_track_selection() && changed) {
448 add (&r->get_time_axis_view());
457 Selection::add (MidiRegionView* mrv)
459 if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
460 midi_regions.push_back (mrv);
461 /* XXX should we do this? */
463 if (Config->get_link_region_and_track_selection()) {
464 add (&mrv->get_time_axis_view());
467 MidiRegionsChanged ();
472 Selection::add (framepos_t start, framepos_t end)
474 AudioRangeComparator cmp;
476 /* XXX this implementation is incorrect */
478 time.push_back (AudioRange (start, end, next_time_id++));
484 return next_time_id - 1;
488 Selection::replace (uint32_t sid, framepos_t start, framepos_t end)
490 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
491 if ((*i).id == sid) {
493 time.push_back (AudioRange(start,end, sid));
495 /* don't consolidate here */
498 AudioRangeComparator cmp;
508 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
510 boost::shared_ptr<ARDOUR::AutomationList> al
511 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
513 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
517 if (find (lines.begin(), lines.end(), al) == lines.end()) {
518 lines.push_back (al);
524 Selection::remove (TimeAxisView* track)
526 list<TimeAxisView*>::iterator i;
527 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
529 if (!_no_tracks_changed) {
536 Selection::remove (const TrackViewList& track_list)
538 bool changed = false;
540 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
542 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
543 if (x != tracks.end()) {
550 if (!_no_tracks_changed) {
557 Selection::remove (const MidiNoteSelection& midi_list)
559 bool changed = false;
561 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
563 MidiNoteSelection::iterator x;
565 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
566 midi_notes.erase (x);
577 Selection::remove (MidiCutBuffer* midi)
579 MidiNoteSelection::iterator x;
581 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
582 /* remember that we own the MCB */
584 midi_notes.erase (x);
590 Selection::remove (boost::shared_ptr<Playlist> track)
592 list<boost::shared_ptr<Playlist> >::iterator i;
593 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
600 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
602 bool changed = false;
604 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
606 list<boost::shared_ptr<Playlist> >::iterator x;
608 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
620 Selection::remove (RegionView* r)
622 if (regions.remove (r)) {
626 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_time_axis_view())) {
627 remove (&r->get_time_axis_view());
632 Selection::remove (MidiRegionView* mrv)
634 MidiRegionSelection::iterator x;
636 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
637 midi_regions.erase (x);
638 MidiRegionsChanged ();
642 /* XXX fix this up ? */
643 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_time_axis_view())) {
644 remove (&r->get_time_axis_view());
651 Selection::remove (uint32_t selection_id)
657 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
658 if ((*i).id == selection_id) {
668 Selection::remove (framepos_t /*start*/, framepos_t /*end*/)
673 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
675 AutomationSelection::iterator i;
676 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
683 Selection::set (TimeAxisView* track)
690 Selection::set (const TrackViewList& track_list)
697 Selection::set (const MidiNoteSelection& midi_list)
704 Selection::set (boost::shared_ptr<Playlist> playlist)
711 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
718 Selection::set (const RegionSelection& rs)
722 RegionsChanged(); /* EMIT SIGNAL */
726 Selection::set (MidiRegionView* mrv)
728 clear_midi_regions ();
733 Selection::set (RegionView* r, bool also_clear_tracks)
736 if (also_clear_tracks) {
743 Selection::set (vector<RegionView*>& v)
746 if (Config->get_link_region_and_track_selection()) {
748 // make sure to deselect any automation selections
754 /** Set the start and end time of the time selection, without changing
755 * the list of tracks it applies to.
758 Selection::set (framepos_t start, framepos_t end)
760 if ((start == 0 && end == 0) || end < start) {
765 time.push_back (AudioRange (start, end, next_time_id++));
767 /* reuse the first entry, and remove all the rest */
769 while (time.size() > 1) {
772 time.front().start = start;
773 time.front().end = end;
780 return time.front().id;
783 /** Set the start and end of the range selection. If more than one range
784 * is currently selected, the start of the earliest range and the end of the
785 * latest range are set. If no range is currently selected, this method
786 * selects a single range from start to end.
788 * @param start New start time.
789 * @param end New end time.
792 Selection::set_preserving_all_ranges (framepos_t start, framepos_t end)
794 if ((start == 0 && end == 0) || (end < start)) {
799 time.push_back (AudioRange (start, end, next_time_id++));
801 time.sort (AudioRangeComparator ());
802 time.front().start = start;
803 time.back().end = end;
812 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
819 Selection::selected (Marker* m)
821 return find (markers.begin(), markers.end(), m) != markers.end();
825 Selection::selected (TimeAxisView* tv)
827 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
831 Selection::selected (RegionView* rv)
833 return find (regions.begin(), regions.end(), rv) != regions.end();
837 Selection::empty (bool internal_selection)
839 bool object_level_empty = regions.empty () &&
842 playlists.empty () &&
845 playlists.empty () &&
850 if (!internal_selection) {
851 return object_level_empty;
854 /* this is intended to really only apply when using a Selection
858 return object_level_empty && midi_notes.empty();
862 Selection::toggle (ControlPoint* cp)
864 cp->set_selected (!cp->get_selected ());
865 set_point_selection_from_line (cp->line ());
869 Selection::toggle (vector<ControlPoint*> const & cps)
871 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
872 (*i)->set_selected (!(*i)->get_selected ());
875 set_point_selection_from_line (cps.front()->line ());
879 Selection::toggle (list<Selectable*> const & selectables)
883 vector<RegionView*> rvs;
884 vector<ControlPoint*> cps;
886 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
887 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
889 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
892 fatal << _("programming error: ")
893 << X_("unknown selectable type passed to Selection::toggle()")
909 Selection::set (list<Selectable*> const & selectables)
914 if (Config->get_link_region_and_track_selection ()) {
923 Selection::add (list<Selectable*> const & selectables)
927 vector<RegionView*> rvs;
928 vector<ControlPoint*> cps;
930 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
931 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
933 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
936 fatal << _("programming error: ")
937 << X_("unknown selectable type passed to Selection::add()")
953 Selection::clear_points ()
955 if (!points.empty()) {
962 Selection::add (ControlPoint* cp)
964 cp->set_selected (true);
965 set_point_selection_from_line (cp->line ());
969 Selection::add (vector<ControlPoint*> const & cps)
971 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
972 (*i)->set_selected (true);
975 set_point_selection_from_line (cps.front()->line ());
979 Selection::set (ControlPoint* cp)
981 if (cp->get_selected()) {
985 /* We're going to set up the PointSelection from the selected ControlPoints
986 on this point's line, so we need to deselect all ControlPoints before
990 for (uint32_t i = 0; i < cp->line().npoints(); ++i) {
991 cp->line().nth (i)->set_selected (false);
994 vector<ControlPoint*> cps;
1000 Selection::set (Marker* m)
1007 Selection::toggle (Marker* m)
1009 MarkerSelection::iterator i;
1011 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
1019 Selection::remove (Marker* m)
1021 MarkerSelection::iterator i;
1023 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
1030 Selection::add (Marker* m)
1032 if (find (markers.begin(), markers.end(), m) == markers.end()) {
1033 markers.push_back (m);
1039 Selection::add (const list<Marker*>& m)
1041 markers.insert (markers.end(), m.begin(), m.end());
1046 MarkerSelection::range (framepos_t& s, framepos_t& e)
1051 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
1053 if ((*i)->position() < s) {
1054 s = (*i)->position();
1057 if ((*i)->position() > e) {
1058 e = (*i)->position();
1062 s = std::min (s, e);
1063 e = std::max (s, e);
1066 /** Automation control point selection is mostly manipulated using the selected state
1067 * of the ControlPoints themselves. For example, to add a point to a selection, its
1068 * ControlPoint is marked as selected and then this method is called. It sets up
1069 * our PointSelection from the selected ControlPoints of a given AutomationLine.
1071 * We can't use ControlPoints directly in the selection, as we need to express a
1072 * selection of not just a visible ControlPoint but also (possibly) some invisible
1073 * points nearby. Hence the selection stores AutomationRanges, and these are synced
1074 * with ControlPoint selection state using AutomationLine::set_selected_points.
1078 Selection::set_point_selection_from_line (AutomationLine const & line)
1082 AutomationRange current (DBL_MAX, 0, 1, 0, &line.trackview);
1084 for (uint32_t i = 0; i < line.npoints(); ++i) {
1085 ControlPoint const * cp = line.nth (i);
1087 if (cp->get_selected()) {
1088 /* x and y position of this control point in coordinates suitable for
1089 an AutomationRange (ie model time and fraction of track height)
1091 double const x = (*(cp->model()))->when;
1092 double const y = 1 - (cp->get_y() / line.trackview.current_height ());
1094 /* work out the position of a rectangle the size of a control point centred
1098 double const size = cp->size ();
1099 double const x_size = line.time_converter().from (line.trackview.editor().pixel_to_frame (size));
1100 double const y_size = size / line.trackview.current_height ();
1102 double const x1 = max (0.0, x - x_size / 2);
1103 double const x2 = x + x_size / 2;
1104 double const y1 = max (0.0, y - y_size / 2);
1105 double const y2 = y + y_size / 2;
1107 /* extend the current AutomationRange to put this point in */
1108 current.start = min (current.start, x1);
1109 current.end = max (current.end, x2);
1110 current.low_fract = min (current.low_fract, y1);
1111 current.high_fract = max (current.high_fract, y2);
1114 /* this point isn't selected; if the current AutomationRange has some
1115 stuff in it, push it onto the list and make a new one
1117 if (current.start < DBL_MAX) {
1118 points.push_back (current);
1119 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1124 /* Maybe push the current AutomationRange, as above */
1125 if (current.start < DBL_MAX) {
1126 points.push_back (current);
1127 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1130 PointsChanged (); /* EMIT SIGNAL */
1134 Selection::get_state () const
1136 /* XXX: not complete; just sufficient to get track selection state
1137 so that re-opening plugin windows for editor mixer strips works
1140 XMLNode* node = new XMLNode (X_("Selection"));
1142 for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
1143 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
1144 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1146 XMLNode* t = node->add_child (X_("RouteView"));
1147 t->add_property (X_("id"), atoi (rtv->route()->id().to_s().c_str()));
1149 XMLNode* t = node->add_child (X_("AutomationView"));
1150 t->add_property (X_("id"), atoi (atv->parent_route()->id().to_s().c_str()));
1151 t->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->parameter ()));
1155 for (MarkerSelection::const_iterator i = markers.begin(); i != markers.end(); ++i) {
1156 XMLNode* t = node->add_child (X_("Marker"));
1159 Location* loc = editor->find_location_from_marker (*i, is_start);
1161 t->add_property (X_("id"), atoi (loc->id().to_s().c_str()));
1162 t->add_property (X_("start"), is_start ? X_("yes") : X_("no"));
1169 Selection::set_state (XMLNode const & node, int)
1171 if (node.name() != X_("Selection")) {
1175 XMLNodeList children = node.children ();
1176 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1177 if ((*i)->name() == X_("RouteView")) {
1179 XMLProperty* prop_id = (*i)->property (X_("id"));
1181 PBD::ID id (prop_id->value ());
1182 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1187 } else if ((*i)->name() == X_("AutomationView")) {
1189 XMLProperty* prop_id = (*i)->property (X_("id"));
1190 XMLProperty* prop_parameter = (*i)->property (X_("parameter"));
1193 assert (prop_parameter);
1195 PBD::ID id (prop_id->value ());
1196 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1199 boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().new_parameter (prop_parameter->value ()));
1201 /* the automation could be for an entity that was never saved
1202 in the session file. Don't freak out if we can't find
1211 } else if ((*i)->name() == X_("Marker")) {
1213 XMLProperty* prop_id = (*i)->property (X_("id"));
1214 XMLProperty* prop_start = (*i)->property (X_("start"));
1216 assert (prop_start);
1218 PBD::ID id (prop_id->value ());
1219 Marker* m = editor->find_marker_from_location_id (id, string_is_affirmative (prop_start->value ()));
1232 Selection::remove_regions (TimeAxisView* t)
1234 RegionSelection::iterator i = regions.begin();
1235 while (i != regions.end ()) {
1236 RegionSelection::iterator tmp = i;
1239 if (&(*i)->get_time_axis_view() == t) {
1248 Selection::block_tracks_changed (bool yn)
1250 _no_tracks_changed = yn;