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)
751 bool had_regions = !regions.empty();
755 if (Config->get_link_region_and_track_selection()) {
757 /* there were regions before, so we're changing the
758 * region selection (likely), thus link region/track
759 * selection. relevant tracks will get selected
760 * as we ::add() below.
763 // make sure to deselect any automation selections
770 /** Set the start and end time of the time selection, without changing
771 * the list of tracks it applies to.
774 Selection::set (framepos_t start, framepos_t end)
776 if ((start == 0 && end == 0) || end < start) {
781 time.push_back (AudioRange (start, end, next_time_id++));
783 /* reuse the first entry, and remove all the rest */
785 while (time.size() > 1) {
788 time.front().start = start;
789 time.front().end = end;
796 return time.front().id;
799 /** Set the start and end of the range selection. If more than one range
800 * is currently selected, the start of the earliest range and the end of the
801 * latest range are set. If no range is currently selected, this method
802 * selects a single range from start to end.
804 * @param start New start time.
805 * @param end New end time.
808 Selection::set_preserving_all_ranges (framepos_t start, framepos_t end)
810 if ((start == 0 && end == 0) || (end < start)) {
815 time.push_back (AudioRange (start, end, next_time_id++));
817 time.sort (AudioRangeComparator ());
818 time.front().start = start;
819 time.back().end = end;
828 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
835 Selection::selected (Marker* m)
837 return find (markers.begin(), markers.end(), m) != markers.end();
841 Selection::selected (TimeAxisView* tv)
843 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
847 Selection::selected (RegionView* rv)
849 return find (regions.begin(), regions.end(), rv) != regions.end();
853 Selection::empty (bool internal_selection)
855 bool object_level_empty = regions.empty () &&
858 playlists.empty () &&
861 playlists.empty () &&
866 if (!internal_selection) {
867 return object_level_empty;
870 /* this is intended to really only apply when using a Selection
874 return object_level_empty && midi_notes.empty();
878 Selection::toggle (ControlPoint* cp)
880 cp->set_selected (!cp->get_selected ());
881 set_point_selection_from_line (cp->line ());
885 Selection::toggle (vector<ControlPoint*> const & cps)
887 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
888 (*i)->set_selected (!(*i)->get_selected ());
891 set_point_selection_from_line (cps.front()->line ());
895 Selection::toggle (list<Selectable*> const & selectables)
899 vector<RegionView*> rvs;
900 vector<ControlPoint*> cps;
902 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
903 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
905 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
908 fatal << _("programming error: ")
909 << X_("unknown selectable type passed to Selection::toggle()")
925 Selection::set (list<Selectable*> const & selectables)
930 if (Config->get_link_region_and_track_selection ()) {
939 Selection::add (list<Selectable*> const & selectables)
943 vector<RegionView*> rvs;
944 vector<ControlPoint*> cps;
946 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
947 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
949 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
952 fatal << _("programming error: ")
953 << X_("unknown selectable type passed to Selection::add()")
969 Selection::clear_points ()
971 if (!points.empty()) {
978 Selection::add (ControlPoint* cp)
980 cp->set_selected (true);
981 set_point_selection_from_line (cp->line ());
985 Selection::add (vector<ControlPoint*> const & cps)
987 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
988 (*i)->set_selected (true);
991 set_point_selection_from_line (cps.front()->line ());
995 Selection::set (ControlPoint* cp)
997 if (cp->get_selected()) {
1001 /* We're going to set up the PointSelection from the selected ControlPoints
1002 on this point's line, so we need to deselect all ControlPoints before
1006 for (uint32_t i = 0; i < cp->line().npoints(); ++i) {
1007 cp->line().nth (i)->set_selected (false);
1010 vector<ControlPoint*> cps;
1016 Selection::set (Marker* m)
1023 Selection::toggle (Marker* m)
1025 MarkerSelection::iterator i;
1027 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
1035 Selection::remove (Marker* m)
1037 MarkerSelection::iterator i;
1039 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
1046 Selection::add (Marker* m)
1048 if (find (markers.begin(), markers.end(), m) == markers.end()) {
1049 markers.push_back (m);
1055 Selection::add (const list<Marker*>& m)
1057 markers.insert (markers.end(), m.begin(), m.end());
1065 MarkerSelection::range (framepos_t& s, framepos_t& e)
1070 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
1072 if ((*i)->position() < s) {
1073 s = (*i)->position();
1076 if ((*i)->position() > e) {
1077 e = (*i)->position();
1081 s = std::min (s, e);
1082 e = std::max (s, e);
1085 /** Automation control point selection is mostly manipulated using the selected state
1086 * of the ControlPoints themselves. For example, to add a point to a selection, its
1087 * ControlPoint is marked as selected and then this method is called. It sets up
1088 * our PointSelection from the selected ControlPoints of a given AutomationLine.
1090 * We can't use ControlPoints directly in the selection, as we need to express a
1091 * selection of not just a visible ControlPoint but also (possibly) some invisible
1092 * points nearby. Hence the selection stores AutomationRanges, and these are synced
1093 * with ControlPoint selection state using AutomationLine::set_selected_points.
1097 Selection::set_point_selection_from_line (AutomationLine const & line)
1101 AutomationRange current (DBL_MAX, 0, 1, 0, &line.trackview);
1103 for (uint32_t i = 0; i < line.npoints(); ++i) {
1104 ControlPoint const * cp = line.nth (i);
1106 if (cp->get_selected()) {
1107 /* x and y position of this control point in coordinates suitable for
1108 an AutomationRange (ie model time and fraction of track height)
1110 double const x = (*(cp->model()))->when;
1111 double const y = 1 - (cp->get_y() / line.trackview.current_height ());
1113 /* work out the position of a rectangle the size of a control point centred
1117 double const size = cp->size ();
1118 double const x_size = line.time_converter().from (line.trackview.editor().pixel_to_frame (size));
1119 double const y_size = size / line.trackview.current_height ();
1121 double const x1 = max (0.0, x - x_size / 2);
1122 double const x2 = x + x_size / 2;
1123 double const y1 = max (0.0, y - y_size / 2);
1124 double const y2 = y + y_size / 2;
1126 /* extend the current AutomationRange to put this point in */
1127 current.start = min (current.start, x1);
1128 current.end = max (current.end, x2);
1129 current.low_fract = min (current.low_fract, y1);
1130 current.high_fract = max (current.high_fract, y2);
1133 /* this point isn't selected; if the current AutomationRange has some
1134 stuff in it, push it onto the list and make a new one
1136 if (current.start < DBL_MAX) {
1137 points.push_back (current);
1138 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1143 /* Maybe push the current AutomationRange, as above */
1144 if (current.start < DBL_MAX) {
1145 points.push_back (current);
1146 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1149 PointsChanged (); /* EMIT SIGNAL */
1153 Selection::get_state () const
1155 /* XXX: not complete; just sufficient to get track selection state
1156 so that re-opening plugin windows for editor mixer strips works
1159 XMLNode* node = new XMLNode (X_("Selection"));
1161 for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
1162 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
1163 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1165 XMLNode* t = node->add_child (X_("RouteView"));
1166 t->add_property (X_("id"), atoi (rtv->route()->id().to_s().c_str()));
1168 XMLNode* t = node->add_child (X_("AutomationView"));
1169 t->add_property (X_("id"), atoi (atv->parent_route()->id().to_s().c_str()));
1170 t->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->parameter ()));
1174 for (MarkerSelection::const_iterator i = markers.begin(); i != markers.end(); ++i) {
1175 XMLNode* t = node->add_child (X_("Marker"));
1178 Location* loc = editor->find_location_from_marker (*i, is_start);
1180 t->add_property (X_("id"), atoi (loc->id().to_s().c_str()));
1181 t->add_property (X_("start"), is_start ? X_("yes") : X_("no"));
1188 Selection::set_state (XMLNode const & node, int)
1190 if (node.name() != X_("Selection")) {
1194 XMLNodeList children = node.children ();
1195 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1196 if ((*i)->name() == X_("RouteView")) {
1198 XMLProperty* prop_id = (*i)->property (X_("id"));
1200 PBD::ID id (prop_id->value ());
1201 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1206 } else if ((*i)->name() == X_("AutomationView")) {
1208 XMLProperty* prop_id = (*i)->property (X_("id"));
1209 XMLProperty* prop_parameter = (*i)->property (X_("parameter"));
1212 assert (prop_parameter);
1214 PBD::ID id (prop_id->value ());
1215 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1218 boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().new_parameter (prop_parameter->value ()));
1220 /* the automation could be for an entity that was never saved
1221 in the session file. Don't freak out if we can't find
1230 } else if ((*i)->name() == X_("Marker")) {
1232 XMLProperty* prop_id = (*i)->property (X_("id"));
1233 XMLProperty* prop_start = (*i)->property (X_("start"));
1235 assert (prop_start);
1237 PBD::ID id (prop_id->value ());
1238 Marker* m = editor->find_marker_from_location_id (id, string_is_affirmative (prop_start->value ()));
1251 Selection::remove_regions (TimeAxisView* t)
1253 RegionSelection::iterator i = regions.begin();
1254 while (i != regions.end ()) {
1255 RegionSelection::iterator tmp = i;
1258 if (&(*i)->get_time_axis_view() == t) {
1267 Selection::block_tracks_changed (bool yn)
1269 _no_tracks_changed = yn;