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)
57 /* we have disambiguate which remove() for the compiler */
59 void (Selection::*track_remove)(TimeAxisView*) = &Selection::remove;
60 TimeAxisView::CatchDeletion.connect (*this, MISSING_INVALIDATOR, ui_bind (track_remove, this, _1), gui_context());
62 void (Selection::*marker_remove)(Marker*) = &Selection::remove;
63 Marker::CatchDeletion.connect (*this, MISSING_INVALIDATOR, ui_bind (marker_remove, this, _1), gui_context());
68 Selection::operator= (const Selection& other)
71 regions = other.regions;
72 tracks = other.tracks;
75 midi_regions = other.midi_regions;
76 midi_notes = other.midi_notes;
83 operator== (const Selection& a, const Selection& b)
85 return a.regions == b.regions &&
86 a.tracks == b.tracks &&
89 a.playlists == b.playlists &&
90 a.midi_notes == b.midi_notes &&
91 a.midi_regions == b.midi_regions;
94 /** Clear everything from the Selection */
105 clear_midi_regions ();
109 Selection::dump_region_layers()
111 cerr << "region selection layer dump" << endl;
112 for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
113 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
119 Selection::clear_regions ()
121 if (!regions.empty()) {
122 regions.clear_all ();
128 Selection::clear_tracks ()
130 if (!tracks.empty()) {
137 Selection::clear_midi_notes ()
139 if (!midi_notes.empty()) {
140 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
149 Selection::clear_midi_regions ()
151 if (!midi_regions.empty()) {
152 midi_regions.clear ();
153 MidiRegionsChanged ();
158 Selection::clear_time ()
166 Selection::clear_playlists ()
168 /* Selections own their playlists */
170 for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
171 /* selections own their own regions, which are copies of the "originals". make them go away */
172 (*i)->drop_regions ();
176 if (!playlists.empty()) {
183 Selection::clear_lines ()
185 if (!lines.empty()) {
192 Selection::clear_markers ()
194 if (!markers.empty()) {
201 Selection::toggle (boost::shared_ptr<Playlist> pl)
203 PlaylistSelection::iterator i;
205 if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
207 playlists.push_back(pl);
216 Selection::toggle (const TrackViewList& track_list)
218 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
224 Selection::toggle (TimeAxisView* track)
226 TrackSelection::iterator i;
228 if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
229 tracks.push_back (track);
238 Selection::toggle (const MidiNoteSelection& midi_note_list)
240 for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
246 Selection::toggle (MidiCutBuffer* midi)
248 MidiNoteSelection::iterator i;
250 if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
251 midi_notes.push_back (midi);
253 /* remember that we own the MCB */
255 midi_notes.erase (i);
263 Selection::toggle (RegionView* r)
265 RegionSelection::iterator i;
267 if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
277 Selection::toggle (MidiRegionView* mrv)
279 MidiRegionSelection::iterator i;
281 if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
284 midi_regions.erase (i);
287 MidiRegionsChanged ();
291 Selection::toggle (vector<RegionView*>& r)
293 RegionSelection::iterator i;
295 for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
296 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
307 Selection::toggle (nframes_t start, nframes_t end)
309 AudioRangeComparator cmp;
311 /* XXX this implementation is incorrect */
313 time.push_back (AudioRange (start, end, next_time_id++));
319 return next_time_id - 1;
323 Selection::add (boost::shared_ptr<Playlist> pl)
325 if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
327 playlists.push_back(pl);
333 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
335 bool changed = false;
337 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
338 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
340 playlists.push_back (*i);
351 Selection::add (const TrackViewList& track_list)
353 TrackViewList added = tracks.add (track_list);
355 if (!added.empty()) {
361 Selection::add (TimeAxisView* track)
364 tr.push_back (track);
369 Selection::add (const MidiNoteSelection& midi_list)
371 const MidiNoteSelection::const_iterator b = midi_list.begin();
372 const MidiNoteSelection::const_iterator e = midi_list.end();
374 if (!midi_list.empty()) {
375 midi_notes.insert (midi_notes.end(), b, e);
381 Selection::add (MidiCutBuffer* midi)
383 /* we take ownership of the MCB */
385 if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
386 midi_notes.push_back (midi);
392 Selection::add (vector<RegionView*>& v)
394 /* XXX This method or the add (const RegionSelection&) needs to go
397 bool changed = false;
399 for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
400 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
401 changed = regions.add ((*i));
402 if (Config->get_link_region_and_track_selection() && changed) {
403 add (&(*i)->get_trackview());
414 Selection::add (const RegionSelection& rs)
416 /* XXX This method or the add (const vector<RegionView*>&) needs to go
419 bool changed = false;
421 for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
422 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
423 changed = regions.add ((*i));
424 if (Config->get_link_region_and_track_selection() && changed) {
425 add (&(*i)->get_trackview());
436 Selection::add (RegionView* r)
438 if (find (regions.begin(), regions.end(), r) == regions.end()) {
440 if (Config->get_link_region_and_track_selection()) {
441 add (&r->get_trackview());
448 Selection::add (MidiRegionView* mrv)
450 if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
451 midi_regions.push_back (mrv);
452 /* XXX should we do this? */
454 if (Config->get_link_region_and_track_selection()) {
455 add (&mrv->get_trackview());
458 MidiRegionsChanged ();
463 Selection::add (nframes_t start, nframes_t end)
465 AudioRangeComparator cmp;
467 /* XXX this implementation is incorrect */
469 time.push_back (AudioRange (start, end, next_time_id++));
475 return next_time_id - 1;
479 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
481 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
482 if ((*i).id == sid) {
484 time.push_back (AudioRange(start,end, sid));
486 /* don't consolidate here */
489 AudioRangeComparator cmp;
499 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
501 boost::shared_ptr<ARDOUR::AutomationList> al
502 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
504 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
508 if (find (lines.begin(), lines.end(), al) == lines.end()) {
509 lines.push_back (al);
515 Selection::remove (TimeAxisView* track)
517 list<TimeAxisView*>::iterator i;
518 if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
525 Selection::remove (const TrackViewList& track_list)
527 bool changed = false;
529 for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
531 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
532 if (x != tracks.end()) {
544 Selection::remove (const MidiNoteSelection& midi_list)
546 bool changed = false;
548 for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
550 MidiNoteSelection::iterator x;
552 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
553 midi_notes.erase (x);
564 Selection::remove (MidiCutBuffer* midi)
566 MidiNoteSelection::iterator x;
568 if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
569 /* remember that we own the MCB */
571 midi_notes.erase (x);
577 Selection::remove (boost::shared_ptr<Playlist> track)
579 list<boost::shared_ptr<Playlist> >::iterator i;
580 if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
587 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
589 bool changed = false;
591 for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
593 list<boost::shared_ptr<Playlist> >::iterator x;
595 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
607 Selection::remove (RegionView* r)
609 if (regions.remove (r)) {
613 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
614 remove (&r->get_trackview());
619 Selection::remove (MidiRegionView* mrv)
621 MidiRegionSelection::iterator x;
623 if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
624 midi_regions.erase (x);
625 MidiRegionsChanged ();
629 /* XXX fix this up ? */
630 if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
631 remove (&r->get_trackview());
638 Selection::remove (uint32_t selection_id)
644 for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
645 if ((*i).id == selection_id) {
655 Selection::remove (nframes_t /*start*/, nframes_t /*end*/)
660 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
662 AutomationSelection::iterator i;
663 if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
670 Selection::set (TimeAxisView* track)
677 Selection::set (const TrackViewList& track_list)
684 Selection::set (const MidiNoteSelection& midi_list)
691 Selection::set (boost::shared_ptr<Playlist> playlist)
698 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
705 Selection::set (const RegionSelection& rs)
709 RegionsChanged(); /* EMIT SIGNAL */
713 Selection::set (MidiRegionView* mrv)
715 clear_midi_regions ();
720 Selection::set (RegionView* r, bool also_clear_tracks)
723 if (also_clear_tracks) {
730 Selection::set (vector<RegionView*>& v)
733 if (Config->get_link_region_and_track_selection()) {
735 // make sure to deselect any automation selections
741 /** Set the start and end time of the time selection, without changing
742 * the list of tracks it applies to.
745 Selection::set (nframes_t start, nframes_t end)
747 if ((start == 0 && end == 0) || end < start) {
752 time.push_back (AudioRange (start, end, next_time_id++));
754 /* reuse the first entry, and remove all the rest */
756 while (time.size() > 1) {
759 time.front().start = start;
760 time.front().end = end;
767 return time.front().id;
771 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
778 Selection::selected (Marker* m)
780 return find (markers.begin(), markers.end(), m) != markers.end();
784 Selection::selected (TimeAxisView* tv)
786 return find (tracks.begin(), tracks.end(), tv) != tracks.end();
790 Selection::selected (RegionView* rv)
792 return find (regions.begin(), regions.end(), rv) != regions.end();
796 Selection::empty (bool internal_selection)
798 bool object_level_empty = regions.empty () &&
801 playlists.empty () &&
804 playlists.empty () &&
809 if (!internal_selection) {
810 return object_level_empty;
813 /* this is intended to really only apply when using a Selection
817 return object_level_empty && midi_notes.empty();
821 Selection::toggle (ControlPoint* cp)
823 cp->set_selected (!cp->get_selected ());
824 set_point_selection_from_line (cp->line ());
828 Selection::toggle (vector<ControlPoint*> const & cps)
830 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
831 (*i)->set_selected (!(*i)->get_selected ());
834 set_point_selection_from_line (cps.front()->line ());
838 Selection::toggle (list<Selectable*> const & selectables)
842 vector<RegionView*> rvs;
843 vector<ControlPoint*> cps;
845 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
846 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
848 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
851 fatal << _("programming error: ")
852 << X_("unknown selectable type passed to Selection::toggle()")
868 Selection::set (list<Selectable*> const & selectables)
873 if (Config->get_link_region_and_track_selection ()) {
882 Selection::add (list<Selectable*> const & selectables)
886 vector<RegionView*> rvs;
887 vector<ControlPoint*> cps;
889 for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
890 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
892 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
895 fatal << _("programming error: ")
896 << X_("unknown selectable type passed to Selection::add()")
912 Selection::clear_points ()
914 if (!points.empty()) {
921 Selection::add (ControlPoint* cp)
923 cp->set_selected (true);
924 set_point_selection_from_line (cp->line ());
928 Selection::add (vector<ControlPoint*> const & cps)
930 for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
931 (*i)->set_selected (true);
934 set_point_selection_from_line (cps.front()->line ());
938 Selection::set (ControlPoint* cp)
940 if (cp->get_selected()) {
944 /* We're going to set up the PointSelection from the selected ControlPoints
945 on this point's line, so we need to deselect all ControlPoints before
949 for (uint32_t i = 0; i < cp->line().npoints(); ++i) {
950 cp->line().nth (i)->set_selected (false);
953 vector<ControlPoint*> cps;
959 Selection::set (Marker* m)
966 Selection::toggle (Marker* m)
968 MarkerSelection::iterator i;
970 if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
978 Selection::remove (Marker* m)
980 MarkerSelection::iterator i;
982 if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
989 Selection::add (Marker* m)
991 if (find (markers.begin(), markers.end(), m) == markers.end()) {
992 markers.push_back (m);
998 Selection::add (const list<Marker*>& m)
1000 markers.insert (markers.end(), m.begin(), m.end());
1005 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
1010 for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
1012 if ((*i)->position() < s) {
1013 s = (*i)->position();
1016 if ((*i)->position() > e) {
1017 e = (*i)->position();
1021 s = std::min (s, e);
1022 e = std::max (s, e);
1025 /** Automation control point selection is mostly manipulated using the selected state
1026 * of the ControlPoints themselves. For example, to add a point to a selection, its
1027 * ControlPoint is marked as selected and then this method is called. It sets up
1028 * our PointSelection from the selected ControlPoints of a given AutomationLine.
1030 * We can't use ControlPoints directly in the selection, as we need to express a
1031 * selection of not just a visible ControlPoint but also (possibly) some invisible
1032 * points nearby. Hence the selection stores AutomationRanges, and these are synced
1033 * with ControlPoint selection state using AutomationLine::set_selected_points.
1037 Selection::set_point_selection_from_line (AutomationLine const & line)
1041 AutomationRange current (DBL_MAX, 0, 1, 0, &line.trackview);
1043 for (uint32_t i = 0; i < line.npoints(); ++i) {
1044 ControlPoint const * cp = line.nth (i);
1046 if (cp->get_selected()) {
1047 /* x and y position of this control point in coordinates suitable for
1048 an AutomationRange (ie model time and fraction of track height)
1050 double const x = (*(cp->model()))->when;
1051 double const y = 1 - (cp->get_y() / line.trackview.current_height ());
1053 /* work out the position of a rectangle the size of a control point centred
1057 double const size = cp->size ();
1058 double const x_size = line.time_converter().from (line.trackview.editor().pixel_to_frame (size));
1059 double const y_size = size / line.trackview.current_height ();
1061 double const x1 = x - x_size / 2;
1062 double const x2 = x + x_size / 2;
1063 double const y1 = y - y_size / 2;
1064 double const y2 = y + y_size / 2;
1066 /* extend the current AutomationRange to put this point in */
1067 current.start = min (current.start, x1);
1068 current.end = max (current.end, x2);
1069 current.low_fract = min (current.low_fract, y1);
1070 current.high_fract = max (current.high_fract, y2);
1073 /* this point isn't selected; if the current AutomationRange has some
1074 stuff in it, push it onto the list and make a new one
1076 if (current.start < DBL_MAX) {
1077 points.push_back (current);
1078 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1083 /* Maybe push the current AutomationRange, as above */
1084 if (current.start < DBL_MAX) {
1085 points.push_back (current);
1086 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1089 PointsChanged (); /* EMIT SIGNAL */
1093 Selection::get_state () const
1095 /* XXX: not complete; just sufficient to get track selection state
1096 so that re-opening plugin windows for editor mixer strips works
1099 XMLNode* node = new XMLNode (X_("Selection"));
1101 for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
1102 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
1103 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1105 XMLNode* t = node->add_child (X_("RouteView"));
1106 t->add_property (X_("id"), atoi (rtv->route()->id().to_s().c_str()));
1108 XMLNode* t = node->add_child (X_("AutomationView"));
1109 t->add_property (X_("id"), atoi (atv->parent_route()->id().to_s().c_str()));
1110 t->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->control()->parameter ()));
1118 Selection::set_state (XMLNode const & node, int)
1120 if (node.name() != X_("Selection")) {
1124 XMLNodeList children = node.children ();
1125 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1126 if ((*i)->name() == X_("RouteView")) {
1128 XMLProperty* prop_id = (*i)->property (X_("id"));
1130 PBD::ID id (prop_id->value ());
1131 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1136 } else if ((*i)->name() == X_("AutomationView")) {
1138 XMLProperty* prop_id = (*i)->property (X_("id"));
1139 XMLProperty* prop_parameter = (*i)->property (X_("parameter"));
1142 assert (prop_parameter);
1144 PBD::ID id (prop_id->value ());
1145 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1148 boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().new_parameter (prop_parameter->value ()));
1150 /* the automation could be for an entity that was never saved
1151 in the session file. Don't freak out if we can't find