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 ();
111 Selection::dump_region_layers()
113 cerr << "region selection layer dump" << endl;
114 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
115 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
121 Selection::clear_regions ()
123 if (!regions.empty()) {
124 regions.clear_all ();
126 if (Config->get_link_region_and_track_selection()) {
133 Selection::clear_tracks ()
135 if (!tracks.empty()) {
137 if (!_no_tracks_changed) {
144 Selection::clear_midi_notes ()
146 if (!midi_notes.empty()) {
147 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
156 Selection::clear_midi_regions ()
158 if (!midi_regions.empty()) {
159 midi_regions.clear ();
160 MidiRegionsChanged ();
165 Selection::clear_time ()
173 Selection::clear_playlists ()
175 /* Selections own their playlists */
177 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
178 /* selections own their own regions, which are copies of the "originals". make them go away */
179 (*i)->drop_regions ();
183 if (!playlists.empty()) {
190 Selection::clear_lines ()
192 if (!lines.empty()) {
199 Selection::clear_markers ()
201 if (!markers.empty()) {
208 Selection::toggle (boost::shared_ptr<Playlist> pl)
210 PlaylistSelection::iterator i;
212 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
214 playlists.push_back(pl);
223 Selection::toggle (const TrackViewList& track_list)
225 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
231 Selection::toggle (TimeAxisView* track)
233 TrackSelection::iterator i;
235 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
236 tracks.push_back (track);
241 if (!_no_tracks_changed) {
247 Selection::toggle (const MidiNoteSelection& midi_note_list)
249 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
255 Selection::toggle (MidiCutBuffer* midi)
257 MidiNoteSelection::iterator i;
259 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
260 midi_notes.push_back (midi);
262 /* remember that we own the MCB */
264 midi_notes.erase (i);
272 Selection::toggle (RegionView* r)
274 RegionSelection::iterator i;
276 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
286 Selection::toggle (MidiRegionView* mrv)
288 MidiRegionSelection::iterator i;
290 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
293 midi_regions.erase (i);
296 MidiRegionsChanged ();
300 Selection::toggle (vector<RegionView*>& r)
302 RegionSelection::iterator i;
304 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
305 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
316 Selection::toggle (framepos_t start, framepos_t end)
318 AudioRangeComparator cmp;
320 /* XXX this implementation is incorrect */
322 time.push_back (AudioRange (start, end, next_time_id++));
328 return next_time_id - 1;
332 Selection::add (boost::shared_ptr<Playlist> pl)
334 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
336 playlists.push_back(pl);
342 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
344 bool changed = false;
346 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
347 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
349 playlists.push_back (*i);
360 Selection::add (const TrackViewList& track_list)
362 TrackViewList added = tracks.add (track_list);
364 if (!added.empty()) {
365 if (!_no_tracks_changed) {
372 Selection::add (TimeAxisView* track)
375 tr.push_back (track);
380 Selection::add (const MidiNoteSelection& midi_list)
382 const MidiNoteSelection::const_iterator b = midi_list.begin();
383 const MidiNoteSelection::const_iterator e = midi_list.end();
385 if (!midi_list.empty()) {
386 midi_notes.insert (midi_notes.end(), b, e);
392 Selection::add (MidiCutBuffer* midi)
394 /* we take ownership of the MCB */
396 if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
397 midi_notes.push_back (midi);
403 Selection::add (vector<RegionView*>& v)
405 /* XXX This method or the add (const RegionSelection&) needs to go
408 bool changed = false;
410 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
411 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
412 changed = regions.add ((*i));
413 if (Config->get_link_region_and_track_selection() && changed) {
414 add (&(*i)->get_time_axis_view());
425 Selection::add (const RegionSelection& rs)
427 /* XXX This method or the add (const vector<RegionView*>&) needs to go
430 bool changed = false;
432 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
433 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
434 changed = regions.add ((*i));
435 if (Config->get_link_region_and_track_selection() && changed) {
436 add (&(*i)->get_time_axis_view());
447 Selection::add (RegionView* r)
449 if (find (regions.begin(), regions.end(), r) == regions.end()) {
450 bool changed = regions.add (r);
451 if (Config->get_link_region_and_track_selection() && changed) {
452 add (&r->get_time_axis_view());
461 Selection::add (MidiRegionView* mrv)
463 if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
464 midi_regions.push_back (mrv);
465 /* XXX should we do this? */
467 if (Config->get_link_region_and_track_selection()) {
468 add (&mrv->get_time_axis_view());
471 MidiRegionsChanged ();
476 Selection::add (framepos_t start, framepos_t end)
478 AudioRangeComparator cmp;
480 /* XXX this implementation is incorrect */
482 time.push_back (AudioRange (start, end, next_time_id++));
488 return next_time_id - 1;
492 Selection::replace (uint32_t sid, framepos_t start, framepos_t end)
494 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
495 if ((*i).id == sid) {
497 time.push_back (AudioRange(start,end, sid));
499 /* don't consolidate here */
502 AudioRangeComparator cmp;
512 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
514 boost::shared_ptr<ARDOUR::AutomationList> al
515 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
517 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
521 if (find (lines.begin(), lines.end(), al) == lines.end()) {
522 lines.push_back (al);
528 Selection::remove (TimeAxisView* track)
530 list<TimeAxisView*>::iterator i;
531 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
533 if (!_no_tracks_changed) {
540 Selection::remove (const TrackViewList& track_list)
542 bool changed = false;
544 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
546 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
547 if (x != tracks.end()) {
554 if (!_no_tracks_changed) {
561 Selection::remove (const MidiNoteSelection& midi_list)
563 bool changed = false;
565 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
567 MidiNoteSelection::iterator x;
569 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
570 midi_notes.erase (x);
581 Selection::remove (MidiCutBuffer* midi)
583 MidiNoteSelection::iterator x;
585 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
586 /* remember that we own the MCB */
588 midi_notes.erase (x);
594 Selection::remove (boost::shared_ptr<Playlist> track)
596 list<boost::shared_ptr<Playlist> >::iterator i;
597 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
604 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
606 bool changed = false;
608 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
610 list<boost::shared_ptr<Playlist> >::iterator x;
612 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
624 Selection::remove (RegionView* r)
626 if (regions.remove (r)) {
630 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_time_axis_view())) {
631 remove (&r->get_time_axis_view());
636 Selection::remove (MidiRegionView* mrv)
638 MidiRegionSelection::iterator x;
640 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
641 midi_regions.erase (x);
642 MidiRegionsChanged ();
646 /* XXX fix this up ? */
647 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_time_axis_view())) {
648 remove (&r->get_time_axis_view());
655 Selection::remove (uint32_t selection_id)
661 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
662 if ((*i).id == selection_id) {
672 Selection::remove (framepos_t /*start*/, framepos_t /*end*/)
677 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
679 AutomationSelection::iterator i;
680 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
687 Selection::set (TimeAxisView* track)
694 Selection::set (const TrackViewList& track_list)
701 Selection::set (const MidiNoteSelection& midi_list)
708 Selection::set (boost::shared_ptr<Playlist> playlist)
715 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
722 Selection::set (const RegionSelection& rs)
726 RegionsChanged(); /* EMIT SIGNAL */
730 Selection::set (MidiRegionView* mrv)
732 clear_midi_regions ();
737 Selection::set (RegionView* r, bool also_clear_tracks)
740 if (also_clear_tracks && !Config->get_link_region_and_track_selection()) {
741 /* clear_regions() will have done this if the link preference
750 Selection::set (vector<RegionView*>& v)
752 bool had_regions = !regions.empty();
756 if (Config->get_link_region_and_track_selection()) {
758 /* there were regions before, so we're changing the
759 * region selection (likely), thus link region/track
760 * selection. relevant tracks will get selected
761 * as we ::add() below.
764 // make sure to deselect any automation selections
771 /** Set the start and end time of the time selection, without changing
772 * the list of tracks it applies to.
775 Selection::set (framepos_t start, framepos_t end)
777 if ((start == 0 && end == 0) || end < start) {
782 time.push_back (AudioRange (start, end, next_time_id++));
784 /* reuse the first entry, and remove all the rest */
786 while (time.size() > 1) {
789 time.front().start = start;
790 time.front().end = end;
797 return time.front().id;
800 /** Set the start and end of the range selection. If more than one range
801 * is currently selected, the start of the earliest range and the end of the
802 * latest range are set. If no range is currently selected, this method
803 * selects a single range from start to end.
805 * @param start New start time.
806 * @param end New end time.
809 Selection::set_preserving_all_ranges (framepos_t start, framepos_t end)
811 if ((start == 0 && end == 0) || (end < start)) {
816 time.push_back (AudioRange (start, end, next_time_id++));
818 time.sort (AudioRangeComparator ());
819 time.front().start = start;
820 time.back().end = end;
829 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
836 Selection::selected (Marker* m)
838 return find (markers.begin(), markers.end(), m) != markers.end();
842 Selection::selected (TimeAxisView* tv)
844 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
848 Selection::selected (RegionView* rv)
850 return find (regions.begin(), regions.end(), rv) != regions.end();
854 Selection::empty (bool internal_selection)
856 bool object_level_empty = regions.empty () &&
859 playlists.empty () &&
862 playlists.empty () &&
867 if (!internal_selection) {
868 return object_level_empty;
871 /* this is intended to really only apply when using a Selection
875 return object_level_empty && midi_notes.empty();
879 Selection::toggle (ControlPoint* cp)
881 cp->set_selected (!cp->get_selected ());
882 set_point_selection_from_line (cp->line ());
886 Selection::toggle (vector<ControlPoint*> const & cps)
888 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
889 (*i)->set_selected (!(*i)->get_selected ());
892 set_point_selection_from_line (cps.front()->line ());
896 Selection::toggle (list<Selectable*> const & selectables)
900 vector<RegionView*> rvs;
901 vector<ControlPoint*> cps;
903 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
904 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
906 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
909 fatal << _("programming error: ")
910 << X_("unknown selectable type passed to Selection::toggle()")
926 Selection::set (list<Selectable*> const & selectables)
931 if (Config->get_link_region_and_track_selection ()) {
940 Selection::add (list<Selectable*> const & selectables)
944 vector<RegionView*> rvs;
945 vector<ControlPoint*> cps;
947 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
948 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
950 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
953 fatal << _("programming error: ")
954 << X_("unknown selectable type passed to Selection::add()")
970 Selection::clear_points ()
972 if (!points.empty()) {
979 Selection::add (ControlPoint* cp)
981 cp->set_selected (true);
982 set_point_selection_from_line (cp->line ());
986 Selection::add (vector<ControlPoint*> const & cps)
988 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
989 (*i)->set_selected (true);
992 set_point_selection_from_line (cps.front()->line ());
996 Selection::set (ControlPoint* cp)
998 if (cp->get_selected()) {
1002 /* We're going to set up the PointSelection from the selected ControlPoints
1003 on this point's line, so we need to deselect all ControlPoints before
1007 for (uint32_t i = 0; i < cp->line().npoints(); ++i) {
1008 cp->line().nth (i)->set_selected (false);
1011 vector<ControlPoint*> cps;
1017 Selection::set (Marker* m)
1024 Selection::toggle (Marker* m)
1026 MarkerSelection::iterator i;
1028 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
1036 Selection::remove (Marker* m)
1038 MarkerSelection::iterator i;
1040 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
1047 Selection::add (Marker* m)
1049 if (find (markers.begin(), markers.end(), m) == markers.end()) {
1050 markers.push_back (m);
1056 Selection::add (const list<Marker*>& m)
1058 markers.insert (markers.end(), m.begin(), m.end());
1066 MarkerSelection::range (framepos_t& s, framepos_t& e)
1071 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
1073 if ((*i)->position() < s) {
1074 s = (*i)->position();
1077 if ((*i)->position() > e) {
1078 e = (*i)->position();
1082 s = std::min (s, e);
1083 e = std::max (s, e);
1086 /** Automation control point selection is mostly manipulated using the selected state
1087 * of the ControlPoints themselves. For example, to add a point to a selection, its
1088 * ControlPoint is marked as selected and then this method is called. It sets up
1089 * our PointSelection from the selected ControlPoints of a given AutomationLine.
1091 * We can't use ControlPoints directly in the selection, as we need to express a
1092 * selection of not just a visible ControlPoint but also (possibly) some invisible
1093 * points nearby. Hence the selection stores AutomationRanges, and these are synced
1094 * with ControlPoint selection state using AutomationLine::set_selected_points.
1098 Selection::set_point_selection_from_line (AutomationLine const & line)
1102 AutomationRange current (DBL_MAX, 0, 1, 0, &line.trackview);
1104 for (uint32_t i = 0; i < line.npoints(); ++i) {
1105 ControlPoint const * cp = line.nth (i);
1107 if (cp->get_selected()) {
1108 /* x and y position of this control point in coordinates suitable for
1109 an AutomationRange (ie model time and fraction of track height)
1111 double const x = (*(cp->model()))->when;
1112 double const y = 1 - (cp->get_y() / line.trackview.current_height ());
1114 /* work out the position of a rectangle the size of a control point centred
1118 double const size = cp->size ();
1119 double const x_size = line.time_converter().from (line.trackview.editor().pixel_to_frame (size));
1120 double const y_size = size / line.trackview.current_height ();
1122 double const x1 = max (0.0, x - x_size / 2);
1123 double const x2 = x + x_size / 2;
1124 double const y1 = max (0.0, y - y_size / 2);
1125 double const y2 = y + y_size / 2;
1127 /* extend the current AutomationRange to put this point in */
1128 current.start = min (current.start, x1);
1129 current.end = max (current.end, x2);
1130 current.low_fract = min (current.low_fract, y1);
1131 current.high_fract = max (current.high_fract, y2);
1134 /* this point isn't selected; if the current AutomationRange has some
1135 stuff in it, push it onto the list and make a new one
1137 if (current.start < DBL_MAX) {
1138 points.push_back (current);
1139 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1144 /* Maybe push the current AutomationRange, as above */
1145 if (current.start < DBL_MAX) {
1146 points.push_back (current);
1147 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1150 PointsChanged (); /* EMIT SIGNAL */
1154 Selection::get_state () const
1156 /* XXX: not complete; just sufficient to get track selection state
1157 so that re-opening plugin windows for editor mixer strips works
1160 XMLNode* node = new XMLNode (X_("Selection"));
1162 for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
1163 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
1164 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1166 XMLNode* t = node->add_child (X_("RouteView"));
1167 t->add_property (X_("id"), atoi (rtv->route()->id().to_s().c_str()));
1169 XMLNode* t = node->add_child (X_("AutomationView"));
1170 t->add_property (X_("id"), atoi (atv->parent_route()->id().to_s().c_str()));
1171 t->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->parameter ()));
1175 for (MarkerSelection::const_iterator i = markers.begin(); i != markers.end(); ++i) {
1176 XMLNode* t = node->add_child (X_("Marker"));
1179 Location* loc = editor->find_location_from_marker (*i, is_start);
1181 t->add_property (X_("id"), atoi (loc->id().to_s().c_str()));
1182 t->add_property (X_("start"), is_start ? X_("yes") : X_("no"));
1189 Selection::set_state (XMLNode const & node, int)
1191 if (node.name() != X_("Selection")) {
1195 XMLNodeList children = node.children ();
1196 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1197 if ((*i)->name() == X_("RouteView")) {
1199 XMLProperty* prop_id = (*i)->property (X_("id"));
1201 PBD::ID id (prop_id->value ());
1202 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1207 } else if ((*i)->name() == X_("AutomationView")) {
1209 XMLProperty* prop_id = (*i)->property (X_("id"));
1210 XMLProperty* prop_parameter = (*i)->property (X_("parameter"));
1213 assert (prop_parameter);
1215 PBD::ID id (prop_id->value ());
1216 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1219 boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().new_parameter (prop_parameter->value ()));
1221 /* the automation could be for an entity that was never saved
1222 in the session file. Don't freak out if we can't find
1231 } else if ((*i)->name() == X_("Marker")) {
1233 XMLProperty* prop_id = (*i)->property (X_("id"));
1234 XMLProperty* prop_start = (*i)->property (X_("start"));
1236 assert (prop_start);
1238 PBD::ID id (prop_id->value ());
1239 Marker* m = editor->find_marker_from_location_id (id, string_is_affirmative (prop_start->value ()));
1252 Selection::remove_regions (TimeAxisView* t)
1254 RegionSelection::iterator i = regions.begin();
1255 while (i != regions.end ()) {
1256 RegionSelection::iterator tmp = i;
1259 if (&(*i)->get_time_axis_view() == t) {
1268 Selection::block_tracks_changed (bool yn)
1270 _no_tracks_changed = yn;