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 ();
125 if (Config->get_link_region_and_track_selection()) {
132 Selection::clear_tracks ()
134 if (!tracks.empty()) {
136 if (!_no_tracks_changed) {
143 Selection::clear_midi_notes ()
145 if (!midi_notes.empty()) {
146 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
155 Selection::clear_midi_regions ()
157 if (!midi_regions.empty()) {
158 midi_regions.clear ();
159 MidiRegionsChanged ();
164 Selection::clear_time ()
172 Selection::clear_playlists ()
174 /* Selections own their playlists */
176 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
177 /* selections own their own regions, which are copies of the "originals". make them go away */
178 (*i)->drop_regions ();
182 if (!playlists.empty()) {
189 Selection::clear_lines ()
191 if (!lines.empty()) {
198 Selection::clear_markers ()
200 if (!markers.empty()) {
207 Selection::toggle (boost::shared_ptr<Playlist> pl)
209 PlaylistSelection::iterator i;
211 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
213 playlists.push_back(pl);
222 Selection::toggle (const TrackViewList& track_list)
224 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
230 Selection::toggle (TimeAxisView* track)
232 TrackSelection::iterator i;
234 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
235 tracks.push_back (track);
240 if (!_no_tracks_changed) {
246 Selection::toggle (const MidiNoteSelection& midi_note_list)
248 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
254 Selection::toggle (MidiCutBuffer* midi)
256 MidiNoteSelection::iterator i;
258 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
259 midi_notes.push_back (midi);
261 /* remember that we own the MCB */
263 midi_notes.erase (i);
271 Selection::toggle (RegionView* r)
273 RegionSelection::iterator i;
275 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
285 Selection::toggle (MidiRegionView* mrv)
287 MidiRegionSelection::iterator i;
289 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
292 midi_regions.erase (i);
295 MidiRegionsChanged ();
299 Selection::toggle (vector<RegionView*>& r)
301 RegionSelection::iterator i;
303 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
304 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
315 Selection::toggle (framepos_t start, framepos_t end)
317 AudioRangeComparator cmp;
319 /* XXX this implementation is incorrect */
321 time.push_back (AudioRange (start, end, next_time_id++));
327 return next_time_id - 1;
331 Selection::add (boost::shared_ptr<Playlist> pl)
333 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
335 playlists.push_back(pl);
341 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
343 bool changed = false;
345 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
346 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
348 playlists.push_back (*i);
359 Selection::add (const TrackViewList& track_list)
361 TrackViewList added = tracks.add (track_list);
363 if (!added.empty()) {
364 if (!_no_tracks_changed) {
371 Selection::add (TimeAxisView* track)
374 tr.push_back (track);
379 Selection::add (const MidiNoteSelection& midi_list)
381 const MidiNoteSelection::const_iterator b = midi_list.begin();
382 const MidiNoteSelection::const_iterator e = midi_list.end();
384 if (!midi_list.empty()) {
385 midi_notes.insert (midi_notes.end(), b, e);
391 Selection::add (MidiCutBuffer* midi)
393 /* we take ownership of the MCB */
395 if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
396 midi_notes.push_back (midi);
402 Selection::add (vector<RegionView*>& v)
404 /* XXX This method or the add (const RegionSelection&) needs to go
407 bool changed = false;
409 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
410 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
411 changed = regions.add ((*i));
412 if (Config->get_link_region_and_track_selection() && changed) {
413 add (&(*i)->get_time_axis_view());
424 Selection::add (const RegionSelection& rs)
426 /* XXX This method or the add (const vector<RegionView*>&) needs to go
429 bool changed = false;
431 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
432 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
433 changed = regions.add ((*i));
434 if (Config->get_link_region_and_track_selection() && changed) {
435 add (&(*i)->get_time_axis_view());
446 Selection::add (RegionView* r)
448 if (find (regions.begin(), regions.end(), r) == regions.end()) {
449 bool changed = regions.add (r);
450 if (Config->get_link_region_and_track_selection() && changed) {
451 add (&r->get_time_axis_view());
460 Selection::add (MidiRegionView* mrv)
462 if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
463 midi_regions.push_back (mrv);
464 /* XXX should we do this? */
466 if (Config->get_link_region_and_track_selection()) {
467 add (&mrv->get_time_axis_view());
470 MidiRegionsChanged ();
475 Selection::add (framepos_t start, framepos_t end)
477 AudioRangeComparator cmp;
479 /* XXX this implementation is incorrect */
481 time.push_back (AudioRange (start, end, next_time_id++));
487 return next_time_id - 1;
491 Selection::replace (uint32_t sid, framepos_t start, framepos_t end)
493 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
494 if ((*i).id == sid) {
496 time.push_back (AudioRange(start,end, sid));
498 /* don't consolidate here */
501 AudioRangeComparator cmp;
511 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
513 boost::shared_ptr<ARDOUR::AutomationList> al
514 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
516 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
520 if (find (lines.begin(), lines.end(), al) == lines.end()) {
521 lines.push_back (al);
527 Selection::remove (TimeAxisView* track)
529 list<TimeAxisView*>::iterator i;
530 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
532 if (!_no_tracks_changed) {
539 Selection::remove (const TrackViewList& track_list)
541 bool changed = false;
543 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
545 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
546 if (x != tracks.end()) {
553 if (!_no_tracks_changed) {
560 Selection::remove (const MidiNoteSelection& midi_list)
562 bool changed = false;
564 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
566 MidiNoteSelection::iterator x;
568 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
569 midi_notes.erase (x);
580 Selection::remove (MidiCutBuffer* midi)
582 MidiNoteSelection::iterator x;
584 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
585 /* remember that we own the MCB */
587 midi_notes.erase (x);
593 Selection::remove (boost::shared_ptr<Playlist> track)
595 list<boost::shared_ptr<Playlist> >::iterator i;
596 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
603 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
605 bool changed = false;
607 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
609 list<boost::shared_ptr<Playlist> >::iterator x;
611 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
623 Selection::remove (RegionView* r)
625 if (regions.remove (r)) {
629 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_time_axis_view())) {
630 remove (&r->get_time_axis_view());
635 Selection::remove (MidiRegionView* mrv)
637 MidiRegionSelection::iterator x;
639 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
640 midi_regions.erase (x);
641 MidiRegionsChanged ();
645 /* XXX fix this up ? */
646 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_time_axis_view())) {
647 remove (&r->get_time_axis_view());
654 Selection::remove (uint32_t selection_id)
660 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
661 if ((*i).id == selection_id) {
671 Selection::remove (framepos_t /*start*/, framepos_t /*end*/)
676 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
678 AutomationSelection::iterator i;
679 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
686 Selection::set (TimeAxisView* track)
693 Selection::set (const TrackViewList& track_list)
700 Selection::set (const MidiNoteSelection& midi_list)
707 Selection::set (boost::shared_ptr<Playlist> playlist)
714 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
721 Selection::set (const RegionSelection& rs)
725 RegionsChanged(); /* EMIT SIGNAL */
729 Selection::set (MidiRegionView* mrv)
731 clear_midi_regions ();
736 Selection::set (RegionView* r, bool also_clear_tracks)
739 if (also_clear_tracks && !Config->get_link_region_and_track_selection()) {
740 /* clear_regions() will have done this if the link preference
749 Selection::set (vector<RegionView*>& v)
752 if (Config->get_link_region_and_track_selection()) {
754 // make sure to deselect any automation selections
760 /** Set the start and end time of the time selection, without changing
761 * the list of tracks it applies to.
764 Selection::set (framepos_t start, framepos_t end)
766 if ((start == 0 && end == 0) || end < start) {
771 time.push_back (AudioRange (start, end, next_time_id++));
773 /* reuse the first entry, and remove all the rest */
775 while (time.size() > 1) {
778 time.front().start = start;
779 time.front().end = end;
786 return time.front().id;
789 /** Set the start and end of the range selection. If more than one range
790 * is currently selected, the start of the earliest range and the end of the
791 * latest range are set. If no range is currently selected, this method
792 * selects a single range from start to end.
794 * @param start New start time.
795 * @param end New end time.
798 Selection::set_preserving_all_ranges (framepos_t start, framepos_t end)
800 if ((start == 0 && end == 0) || (end < start)) {
805 time.push_back (AudioRange (start, end, next_time_id++));
807 time.sort (AudioRangeComparator ());
808 time.front().start = start;
809 time.back().end = end;
818 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
825 Selection::selected (Marker* m)
827 return find (markers.begin(), markers.end(), m) != markers.end();
831 Selection::selected (TimeAxisView* tv)
833 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
837 Selection::selected (RegionView* rv)
839 return find (regions.begin(), regions.end(), rv) != regions.end();
843 Selection::empty (bool internal_selection)
845 bool object_level_empty = regions.empty () &&
848 playlists.empty () &&
851 playlists.empty () &&
856 if (!internal_selection) {
857 return object_level_empty;
860 /* this is intended to really only apply when using a Selection
864 return object_level_empty && midi_notes.empty();
868 Selection::toggle (ControlPoint* cp)
870 cp->set_selected (!cp->get_selected ());
871 set_point_selection_from_line (cp->line ());
875 Selection::toggle (vector<ControlPoint*> const & cps)
877 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
878 (*i)->set_selected (!(*i)->get_selected ());
881 set_point_selection_from_line (cps.front()->line ());
885 Selection::toggle (list<Selectable*> const & selectables)
889 vector<RegionView*> rvs;
890 vector<ControlPoint*> cps;
892 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
893 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
895 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
898 fatal << _("programming error: ")
899 << X_("unknown selectable type passed to Selection::toggle()")
915 Selection::set (list<Selectable*> const & selectables)
920 if (Config->get_link_region_and_track_selection ()) {
929 Selection::add (list<Selectable*> const & selectables)
933 vector<RegionView*> rvs;
934 vector<ControlPoint*> cps;
936 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
937 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
939 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
942 fatal << _("programming error: ")
943 << X_("unknown selectable type passed to Selection::add()")
959 Selection::clear_points ()
961 if (!points.empty()) {
968 Selection::add (ControlPoint* cp)
970 cp->set_selected (true);
971 set_point_selection_from_line (cp->line ());
975 Selection::add (vector<ControlPoint*> const & cps)
977 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
978 (*i)->set_selected (true);
981 set_point_selection_from_line (cps.front()->line ());
985 Selection::set (ControlPoint* cp)
987 if (cp->get_selected()) {
991 /* We're going to set up the PointSelection from the selected ControlPoints
992 on this point's line, so we need to deselect all ControlPoints before
996 for (uint32_t i = 0; i < cp->line().npoints(); ++i) {
997 cp->line().nth (i)->set_selected (false);
1000 vector<ControlPoint*> cps;
1006 Selection::set (Marker* m)
1013 Selection::toggle (Marker* m)
1015 MarkerSelection::iterator i;
1017 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
1025 Selection::remove (Marker* m)
1027 MarkerSelection::iterator i;
1029 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
1036 Selection::add (Marker* m)
1038 if (find (markers.begin(), markers.end(), m) == markers.end()) {
1039 markers.push_back (m);
1045 Selection::add (const list<Marker*>& m)
1047 markers.insert (markers.end(), m.begin(), m.end());
1055 MarkerSelection::range (framepos_t& s, framepos_t& e)
1060 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
1062 if ((*i)->position() < s) {
1063 s = (*i)->position();
1066 if ((*i)->position() > e) {
1067 e = (*i)->position();
1071 s = std::min (s, e);
1072 e = std::max (s, e);
1075 /** Automation control point selection is mostly manipulated using the selected state
1076 * of the ControlPoints themselves. For example, to add a point to a selection, its
1077 * ControlPoint is marked as selected and then this method is called. It sets up
1078 * our PointSelection from the selected ControlPoints of a given AutomationLine.
1080 * We can't use ControlPoints directly in the selection, as we need to express a
1081 * selection of not just a visible ControlPoint but also (possibly) some invisible
1082 * points nearby. Hence the selection stores AutomationRanges, and these are synced
1083 * with ControlPoint selection state using AutomationLine::set_selected_points.
1087 Selection::set_point_selection_from_line (AutomationLine const & line)
1091 AutomationRange current (DBL_MAX, 0, 1, 0, &line.trackview);
1093 for (uint32_t i = 0; i < line.npoints(); ++i) {
1094 ControlPoint const * cp = line.nth (i);
1096 if (cp->get_selected()) {
1097 /* x and y position of this control point in coordinates suitable for
1098 an AutomationRange (ie model time and fraction of track height)
1100 double const x = (*(cp->model()))->when;
1101 double const y = 1 - (cp->get_y() / line.trackview.current_height ());
1103 /* work out the position of a rectangle the size of a control point centred
1107 double const size = cp->size ();
1108 double const x_size = line.time_converter().from (line.trackview.editor().pixel_to_frame (size));
1109 double const y_size = size / line.trackview.current_height ();
1111 double const x1 = max (0.0, x - x_size / 2);
1112 double const x2 = x + x_size / 2;
1113 double const y1 = max (0.0, y - y_size / 2);
1114 double const y2 = y + y_size / 2;
1116 /* extend the current AutomationRange to put this point in */
1117 current.start = min (current.start, x1);
1118 current.end = max (current.end, x2);
1119 current.low_fract = min (current.low_fract, y1);
1120 current.high_fract = max (current.high_fract, y2);
1123 /* this point isn't selected; if the current AutomationRange has some
1124 stuff in it, push it onto the list and make a new one
1126 if (current.start < DBL_MAX) {
1127 points.push_back (current);
1128 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1133 /* Maybe push the current AutomationRange, as above */
1134 if (current.start < DBL_MAX) {
1135 points.push_back (current);
1136 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1139 PointsChanged (); /* EMIT SIGNAL */
1143 Selection::get_state () const
1145 /* XXX: not complete; just sufficient to get track selection state
1146 so that re-opening plugin windows for editor mixer strips works
1149 XMLNode* node = new XMLNode (X_("Selection"));
1151 for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
1152 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
1153 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1155 XMLNode* t = node->add_child (X_("RouteView"));
1156 t->add_property (X_("id"), atoi (rtv->route()->id().to_s().c_str()));
1158 XMLNode* t = node->add_child (X_("AutomationView"));
1159 t->add_property (X_("id"), atoi (atv->parent_route()->id().to_s().c_str()));
1160 t->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->parameter ()));
1164 for (MarkerSelection::const_iterator i = markers.begin(); i != markers.end(); ++i) {
1165 XMLNode* t = node->add_child (X_("Marker"));
1168 Location* loc = editor->find_location_from_marker (*i, is_start);
1170 t->add_property (X_("id"), atoi (loc->id().to_s().c_str()));
1171 t->add_property (X_("start"), is_start ? X_("yes") : X_("no"));
1178 Selection::set_state (XMLNode const & node, int)
1180 if (node.name() != X_("Selection")) {
1184 XMLNodeList children = node.children ();
1185 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1186 if ((*i)->name() == X_("RouteView")) {
1188 XMLProperty* prop_id = (*i)->property (X_("id"));
1190 PBD::ID id (prop_id->value ());
1191 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1196 } else if ((*i)->name() == X_("AutomationView")) {
1198 XMLProperty* prop_id = (*i)->property (X_("id"));
1199 XMLProperty* prop_parameter = (*i)->property (X_("parameter"));
1202 assert (prop_parameter);
1204 PBD::ID id (prop_id->value ());
1205 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1208 boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().new_parameter (prop_parameter->value ()));
1210 /* the automation could be for an entity that was never saved
1211 in the session file. Don't freak out if we can't find
1220 } else if ((*i)->name() == X_("Marker")) {
1222 XMLProperty* prop_id = (*i)->property (X_("id"));
1223 XMLProperty* prop_start = (*i)->property (X_("start"));
1225 assert (prop_start);
1227 PBD::ID id (prop_id->value ());
1228 Marker* m = editor->find_marker_from_location_id (id, string_is_affirmative (prop_start->value ()));
1241 Selection::remove_regions (TimeAxisView* t)
1243 RegionSelection::iterator i = regions.begin();
1244 while (i != regions.end ()) {
1245 RegionSelection::iterator tmp = i;
1248 if (&(*i)->get_time_axis_view() == t) {
1257 Selection::block_tracks_changed (bool yn)
1259 _no_tracks_changed = yn;