Prepare AutomationTimeAxisView for non-route (VCA) automation
[ardour.git] / gtk2_ardour / selection.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <algorithm>
21 #include <sigc++/bind.h>
22
23 #include "pbd/error.h"
24 #include "pbd/stacktrace.h"
25 #include "pbd/types_convert.h"
26
27 #include "ardour/evoral_types_convert.h"
28 #include "ardour/playlist.h"
29 #include "ardour/rc_configuration.h"
30 #include "ardour/selection.h"
31
32 #include "control_protocol/control_protocol.h"
33
34 #include "audio_region_view.h"
35 #include "debug.h"
36 #include "gui_thread.h"
37 #include "midi_cut_buffer.h"
38 #include "region_gain_line.h"
39 #include "region_view.h"
40 #include "selection.h"
41 #include "selection_templates.h"
42 #include "time_axis_view.h"
43 #include "automation_time_axis.h"
44 #include "public_editor.h"
45 #include "control_point.h"
46 #include "vca_time_axis.h"
47
48 #include "pbd/i18n.h"
49
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
53
54 struct AudioRangeComparator {
55         bool operator()(AudioRange a, AudioRange b) {
56                 return a.start < b.start;
57         }
58 };
59
60 Selection::Selection (const PublicEditor* e, bool mls)
61         : tracks (e)
62         , editor (e)
63         , next_time_id (0)
64         , manage_libardour_selection (mls)
65 {
66         clear ();
67
68         /* we have disambiguate which remove() for the compiler */
69
70         void (Selection::*marker_remove)(ArdourMarker*) = &Selection::remove;
71         ArdourMarker::CatchDeletion.connect (*this, MISSING_INVALIDATOR, boost::bind (marker_remove, this, _1), gui_context());
72
73         void (Selection::*point_remove)(ControlPoint*) = &Selection::remove;
74         ControlPoint::CatchDeletion.connect (*this, MISSING_INVALIDATOR, boost::bind (point_remove, this, _1), gui_context());
75 }
76
77 #if 0
78 Selection&
79 Selection::operator= (const Selection& other)
80 {
81         if (&other != this) {
82                 regions = other.regions;
83                 tracks = other.tracks;
84                 time = other.time;
85                 lines = other.lines;
86                 midi_regions = other.midi_regions;
87                 midi_notes = other.midi_notes;
88         }
89         return *this;
90 }
91 #endif
92
93 bool
94 operator== (const Selection& a, const Selection& b)
95 {
96         return a.regions == b.regions &&
97                 a.tracks == b.tracks &&
98                 a.time == b.time &&
99                 a.lines == b.lines &&
100                 a.playlists == b.playlists &&
101                 a.midi_notes == b.midi_notes &&
102                 a.midi_regions == b.midi_regions;
103 }
104
105 /** Clear everything from the Selection */
106 void
107 Selection::clear ()
108 {
109         clear_tracks ();
110         clear_regions ();
111         clear_points ();
112         clear_lines();
113         clear_time ();
114         clear_playlists ();
115         clear_midi_notes ();
116         clear_midi_regions ();
117         clear_markers ();
118         pending_midi_note_selection.clear();
119 }
120
121 void
122 Selection::clear_objects (bool with_signal)
123 {
124         clear_regions (with_signal);
125         clear_points (with_signal);
126         clear_lines(with_signal);
127         clear_playlists (with_signal);
128         clear_midi_notes (with_signal);
129         clear_midi_regions (with_signal);
130 }
131
132 void
133 Selection::clear_time (bool with_signal)
134 {
135         time.clear();
136         if (with_signal) {
137                 TimeChanged ();
138         }
139 }
140
141 void
142 Selection::dump_region_layers()
143 {
144         cerr << "region selection layer dump" << endl;
145         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
146                 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
147         }
148 }
149
150
151 void
152 Selection::clear_regions (bool with_signal)
153 {
154         if (!regions.empty()) {
155                 regions.clear_all ();
156                 if (with_signal) {
157                         RegionsChanged();
158                 }
159         }
160 }
161
162 void
163 Selection::clear_midi_notes (bool with_signal)
164 {
165         if (!midi_notes.empty()) {
166                 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
167                         delete *x;
168                 }
169                 midi_notes.clear ();
170                 if (with_signal) {
171                         MidiNotesChanged ();
172                 }
173         }
174
175         // clear note selections for MRV's that have note selections
176         // this will cause the MRV to be removed from the list
177         for (MidiRegionSelection::iterator i = midi_regions.begin();
178              i != midi_regions.end();) {
179                 MidiRegionSelection::iterator tmp = i;
180                 ++tmp;
181                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*i);
182                 if (mrv) {
183                         mrv->clear_selection();
184                 }
185                 i = tmp;
186         }
187 }
188
189 void
190 Selection::clear_midi_regions (bool with_signal)
191 {
192         if (!midi_regions.empty()) {
193                 midi_regions.clear ();
194                 if (with_signal) {
195                         MidiRegionsChanged ();
196                 }
197         }
198 }
199
200 void
201 Selection::clear_playlists (bool with_signal)
202 {
203         /* Selections own their playlists */
204
205         for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
206                 /* selections own their own regions, which are copies of the "originals". make them go away */
207                 (*i)->drop_regions ();
208                 (*i)->release ();
209         }
210
211         if (!playlists.empty()) {
212                 playlists.clear ();
213                 if (with_signal) {
214                         PlaylistsChanged();
215                 }
216         }
217 }
218
219 void
220 Selection::clear_lines (bool with_signal)
221 {
222         if (!lines.empty()) {
223                 lines.clear ();
224                 if (with_signal) {
225                         LinesChanged();
226                 }
227         }
228 }
229
230 void
231 Selection::clear_markers (bool with_signal)
232 {
233         if (!markers.empty()) {
234                 markers.clear ();
235                 if (with_signal) {
236                         MarkersChanged();
237                 }
238         }
239 }
240
241 void
242 Selection::toggle (boost::shared_ptr<Playlist> pl)
243 {
244         clear_time();  //enforce object/range exclusivity
245         clear_tracks();  //enforce object/track exclusivity
246
247         PlaylistSelection::iterator i;
248
249         if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
250                 pl->use ();
251                 playlists.push_back(pl);
252         } else {
253                 playlists.erase (i);
254         }
255
256         PlaylistsChanged ();
257 }
258
259 void
260 Selection::toggle (const MidiNoteSelection& midi_note_list)
261 {
262         clear_time();  //enforce object/range exclusivity
263         clear_tracks();  //enforce object/track exclusivity
264
265         for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
266                 toggle ((*i));
267         }
268 }
269
270 void
271 Selection::toggle (MidiCutBuffer* midi)
272 {
273         MidiNoteSelection::iterator i;
274
275         if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
276                 midi_notes.push_back (midi);
277         } else {
278                 /* remember that we own the MCB */
279                 delete *i;
280                 midi_notes.erase (i);
281         }
282
283         MidiNotesChanged();
284 }
285
286
287 void
288 Selection::toggle (RegionView* r)
289 {
290         clear_time();  //enforce object/range exclusivity
291         clear_tracks();  //enforce object/track exclusivity
292
293         RegionSelection::iterator i;
294
295         if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
296                 add (r);
297         } else {
298                 remove (*i);
299         }
300
301         RegionsChanged ();
302 }
303
304 void
305 Selection::toggle (MidiRegionView* mrv)
306 {
307         clear_time();   //enforce object/range exclusivity
308         clear_tracks();  //enforce object/track exclusivity
309
310         MidiRegionSelection::iterator i;
311
312         if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
313                 add (mrv);
314         } else {
315                 midi_regions.erase (i);
316         }
317
318         MidiRegionsChanged ();
319 }
320
321 void
322 Selection::toggle (vector<RegionView*>& r)
323 {
324         clear_time();  //enforce object/range exclusivity
325         clear_tracks();  //enforce object/track exclusivity
326
327         RegionSelection::iterator i;
328
329         for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
330                 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
331                         add ((*x));
332                 } else {
333                         remove (*x);
334                 }
335         }
336
337         RegionsChanged ();
338 }
339
340 long
341 Selection::toggle (framepos_t start, framepos_t end)
342 {
343         clear_objects();  //enforce object/range exclusivity
344
345         AudioRangeComparator cmp;
346
347         /* XXX this implementation is incorrect */
348
349         time.push_back (AudioRange (start, end, ++next_time_id));
350         time.consolidate ();
351         time.sort (cmp);
352
353         TimeChanged ();
354
355         return next_time_id;
356 }
357
358 void
359 Selection::add (boost::shared_ptr<Playlist> pl)
360 {
361
362         if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
363                 clear_time();  //enforce object/range exclusivity
364                 clear_tracks();  //enforce object/track exclusivity
365                 pl->use ();
366                 playlists.push_back(pl);
367                 PlaylistsChanged ();
368         }
369 }
370
371 void
372 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
373 {
374         bool changed = false;
375
376         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
377                 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
378                         (*i)->use ();
379                         playlists.push_back (*i);
380                         changed = true;
381                 }
382         }
383
384         if (changed) {
385                 clear_time();  //enforce object/range exclusivity
386                 clear_tracks();  //enforce object/track exclusivity
387                 PlaylistsChanged ();
388         }
389 }
390
391 void
392 Selection::add (const MidiNoteSelection& midi_list)
393 {
394         const MidiNoteSelection::const_iterator b = midi_list.begin();
395         const MidiNoteSelection::const_iterator e = midi_list.end();
396
397         if (!midi_list.empty()) {
398                 clear_time();  //enforce object/range exclusivity
399                 clear_tracks();  //enforce object/track exclusivity
400                 midi_notes.insert (midi_notes.end(), b, e);
401                 MidiNotesChanged ();
402         }
403 }
404
405 void
406 Selection::add (MidiCutBuffer* midi)
407 {
408         /* we take ownership of the MCB */
409
410         if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
411                 midi_notes.push_back (midi);
412                 MidiNotesChanged ();
413         }
414 }
415
416 void
417 Selection::add (vector<RegionView*>& v)
418 {
419         /* XXX This method or the add (const RegionSelection&) needs to go
420          */
421
422         bool changed = false;
423
424         for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
425                 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
426                         changed = regions.add ((*i));
427                 }
428         }
429
430         if (changed) {
431                 clear_time();  //enforce object/range exclusivity
432                 clear_tracks();  //enforce object/track exclusivity
433                 RegionsChanged ();
434         }
435 }
436
437 void
438 Selection::add (const RegionSelection& rs)
439 {
440         /* XXX This method or the add (const vector<RegionView*>&) needs to go
441          */
442
443         bool changed = false;
444
445         for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
446                 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
447                         changed = regions.add ((*i));
448                 }
449         }
450
451         if (changed) {
452                 clear_time();  //enforce object/range exclusivity
453                 clear_tracks();  //enforce object/track exclusivity
454                 RegionsChanged ();
455         }
456 }
457
458 void
459 Selection::add (RegionView* r)
460 {
461         if (find (regions.begin(), regions.end(), r) == regions.end()) {
462                 bool changed = regions.add (r);
463                 if (changed) {
464                         clear_time();  //enforce object/range exclusivity
465                         clear_tracks();  //enforce object/track exclusivity
466                         RegionsChanged ();
467                 }
468         }
469 }
470
471 void
472 Selection::add (MidiRegionView* mrv)
473 {
474         DEBUG_TRACE(DEBUG::Selection, string_compose("Selection::add MRV %1\n", mrv));
475
476         clear_time();  //enforce object/range exclusivity
477         clear_tracks();  //enforce object/track exclusivity
478
479         if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
480                 midi_regions.push_back (mrv);
481                 /* XXX should we do this? */
482                 MidiRegionsChanged ();
483         }
484 }
485
486 long
487 Selection::add (framepos_t start, framepos_t end)
488 {
489         clear_objects();  //enforce object/range exclusivity
490
491         AudioRangeComparator cmp;
492
493         /* XXX this implementation is incorrect */
494
495         time.push_back (AudioRange (start, end, ++next_time_id));
496         time.consolidate ();
497         time.sort (cmp);
498
499         TimeChanged ();
500
501         return next_time_id;
502 }
503
504 void
505 Selection::move_time (framecnt_t distance)
506 {
507         if (distance == 0) {
508                 return;
509         }
510
511         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
512                 (*i).start += distance;
513                 (*i).end += distance;
514         }
515
516         TimeChanged ();
517 }
518
519 void
520 Selection::replace (uint32_t sid, framepos_t start, framepos_t end)
521 {
522         clear_objects();  //enforce object/range exclusivity
523
524         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
525                 if ((*i).id == sid) {
526                         time.erase (i);
527                         time.push_back (AudioRange(start,end, sid));
528
529                         /* don't consolidate here */
530
531
532                         AudioRangeComparator cmp;
533                         time.sort (cmp);
534
535                         TimeChanged ();
536                         break;
537                 }
538         }
539 }
540
541 void
542 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
543 {
544         boost::shared_ptr<ARDOUR::AutomationList> al = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
545
546         if (!al) {
547                 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
548                 return;
549         }
550
551         if (!cl->empty()) {
552                 clear_time();  //enforce object/range exclusivity
553                 clear_tracks();  //enforce object/track exclusivity
554         }
555
556         /* The original may change so we must store a copy (not a pointer) here.
557          * e.g AutomationLine rewrites the list with gain mapping.
558          * the downside is that we can't perfom duplicate checks.
559          * This code was changed in response to #6842
560          */
561         lines.push_back (boost::shared_ptr<ARDOUR::AutomationList> (new ARDOUR::AutomationList(*al)));
562         LinesChanged();
563 }
564
565 void
566 Selection::remove (ControlPoint* p)
567 {
568         PointSelection::iterator i = find (points.begin(), points.end(), p);
569         if (i != points.end ()) {
570                 points.erase (i);
571         }
572 }
573
574 void
575 Selection::remove (const MidiNoteSelection& midi_list)
576 {
577         bool changed = false;
578
579         for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
580
581                 MidiNoteSelection::iterator x;
582
583                 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
584                         midi_notes.erase (x);
585                         changed = true;
586                 }
587         }
588
589         if (changed) {
590                 MidiNotesChanged();
591         }
592 }
593
594 void
595 Selection::remove (MidiCutBuffer* midi)
596 {
597         MidiNoteSelection::iterator x;
598
599         if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
600                 /* remember that we own the MCB */
601                 delete *x;
602                 midi_notes.erase (x);
603                 MidiNotesChanged ();
604         }
605 }
606
607 void
608 Selection::remove (boost::shared_ptr<Playlist> track)
609 {
610         list<boost::shared_ptr<Playlist> >::iterator i;
611         if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
612                 playlists.erase (i);
613                 PlaylistsChanged();
614         }
615 }
616
617 void
618 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
619 {
620         bool changed = false;
621
622         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
623
624                 list<boost::shared_ptr<Playlist> >::iterator x;
625
626                 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
627                         playlists.erase (x);
628                         changed = true;
629                 }
630         }
631
632         if (changed) {
633                 PlaylistsChanged();
634         }
635 }
636
637 void
638 Selection::remove (RegionView* r)
639 {
640         if (regions.remove (r)) {
641                 RegionsChanged ();
642         }
643 }
644
645 void
646 Selection::remove (MidiRegionView* mrv)
647 {
648         DEBUG_TRACE(DEBUG::Selection, string_compose("Selection::remove MRV %1\n", mrv));
649
650         MidiRegionSelection::iterator x;
651
652         if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
653                 midi_regions.erase (x);
654                 MidiRegionsChanged ();
655         }
656 }
657
658 void
659 Selection::remove (uint32_t selection_id)
660 {
661         if (time.empty()) {
662                 return;
663         }
664
665         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
666                 if ((*i).id == selection_id) {
667                         time.erase (i);
668
669                         TimeChanged ();
670                         break;
671                 }
672         }
673 }
674
675 void
676 Selection::remove (framepos_t /*start*/, framepos_t /*end*/)
677 {
678 }
679
680 void
681 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
682 {
683         AutomationSelection::iterator i;
684         if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
685                 lines.erase (i);
686                 LinesChanged();
687         }
688 }
689
690 void
691 Selection::set (const MidiNoteSelection& midi_list)
692 {
693         if (!midi_list.empty()) {
694                 clear_time ();  //enforce region/object exclusivity
695                 clear_tracks();  //enforce object/track exclusivity
696         }
697         clear_objects ();
698         add (midi_list);
699 }
700
701 void
702 Selection::set (boost::shared_ptr<Playlist> playlist)
703 {
704         if (playlist) {
705                 clear_time ();  //enforce region/object exclusivity
706                 clear_tracks();  //enforce object/track exclusivity
707         }
708         clear_objects ();
709         add (playlist);
710 }
711
712 void
713 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
714 {
715         if (!pllist.empty()) {
716                 clear_time();  //enforce region/object exclusivity
717         }
718         clear_objects ();
719         add (pllist);
720 }
721
722 void
723 Selection::set (const RegionSelection& rs)
724 {
725         if (!rs.empty()) {
726                 clear_time();  //enforce region/object exclusivity
727                 clear_tracks();  //enforce object/track exclusivity
728         }
729         clear_objects();
730         regions = rs;
731         RegionsChanged(); /* EMIT SIGNAL */
732 }
733
734 void
735 Selection::set (MidiRegionView* mrv)
736 {
737         if (mrv) {
738                 clear_time();  //enforce region/object exclusivity
739                 clear_tracks();  //enforce object/track exclusivity
740         }
741         clear_objects ();
742         add (mrv);
743 }
744
745 void
746 Selection::set (RegionView* r, bool /*also_clear_tracks*/)
747 {
748         if (r) {
749                 clear_time();  //enforce region/object exclusivity
750                 clear_tracks();  //enforce object/track exclusivity
751         }
752         clear_objects ();
753         add (r);
754 }
755
756 void
757 Selection::set (vector<RegionView*>& v)
758 {
759         if (!v.empty()) {
760                 clear_time();  //enforce region/object exclusivity
761                 clear_tracks();  //enforce object/track exclusivity
762         }
763
764         clear_objects();
765
766         add (v);
767 }
768
769 /** Set the start and end time of the time selection, without changing
770  *  the list of tracks it applies to.
771  */
772 long
773 Selection::set (framepos_t start, framepos_t end)
774 {
775         clear_objects();  //enforce region/object exclusivity
776         clear_time();
777
778         if ((start == 0 && end == 0) || end < start) {
779                 return 0;
780         }
781
782         if (time.empty()) {
783                 time.push_back (AudioRange (start, end, ++next_time_id));
784         } else {
785                 /* reuse the first entry, and remove all the rest */
786
787                 while (time.size() > 1) {
788                         time.pop_front();
789                 }
790                 time.front().start = start;
791                 time.front().end = end;
792         }
793
794         time.consolidate ();
795
796         TimeChanged ();
797
798         return time.front().id;
799 }
800
801 /** Set the start and end of the range selection.  If more than one range
802  *  is currently selected, the start of the earliest range and the end of the
803  *  latest range are set.  If no range is currently selected, this method
804  *  selects a single range from start to end.
805  *
806  *  @param start New start time.
807  *  @param end New end time.
808  */
809 void
810 Selection::set_preserving_all_ranges (framepos_t start, framepos_t end)
811 {
812         clear_objects();  //enforce region/object exclusivity
813
814         if ((start == 0 && end == 0) || (end < start)) {
815                 return;
816         }
817
818         if (time.empty ()) {
819                 time.push_back (AudioRange (start, end, ++next_time_id));
820         } else {
821                 time.sort (AudioRangeComparator ());
822                 time.front().start = start;
823                 time.back().end = end;
824         }
825
826         time.consolidate ();
827
828         TimeChanged ();
829 }
830
831 void
832 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
833 {
834         clear_time();  //enforce region/object exclusivity
835         clear_tracks();  //enforce object/track exclusivity
836         clear_objects();
837
838         add (ac);
839 }
840
841 bool
842 Selection::selected (ArdourMarker* m) const
843 {
844         return find (markers.begin(), markers.end(), m) != markers.end();
845 }
846
847 bool
848 Selection::selected (RegionView* rv) const
849 {
850         return find (regions.begin(), regions.end(), rv) != regions.end();
851 }
852
853 bool
854 Selection::selected (ControlPoint* cp) const
855 {
856         return find (points.begin(), points.end(), cp) != points.end();
857 }
858
859 bool
860 Selection::empty (bool internal_selection)
861 {
862         bool object_level_empty =  regions.empty () &&
863                 tracks.empty () &&
864                 points.empty () &&
865                 playlists.empty () &&
866                 lines.empty () &&
867                 time.empty () &&
868                 playlists.empty () &&
869                 markers.empty() &&
870                 midi_regions.empty()
871                 ;
872
873         if (!internal_selection) {
874                 return object_level_empty;
875         }
876
877         /* this is intended to really only apply when using a Selection
878            as a cut buffer.
879         */
880
881         return object_level_empty && midi_notes.empty() && points.empty();
882 }
883
884 void
885 Selection::toggle (ControlPoint* cp)
886 {
887         clear_time();  //enforce region/object exclusivity
888         clear_tracks();  //enforce object/track exclusivity
889
890         cp->set_selected (!cp->selected ());
891         PointSelection::iterator i = find (points.begin(), points.end(), cp);
892         if (i == points.end()) {
893                 points.push_back (cp);
894         } else {
895                 points.erase (i);
896         }
897
898         PointsChanged (); /* EMIT SIGNAL */
899 }
900
901 void
902 Selection::toggle (vector<ControlPoint*> const & cps)
903 {
904         clear_time();  //enforce region/object exclusivity
905         clear_tracks();  //enforce object/track exclusivity
906
907         for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
908                 toggle (*i);
909         }
910 }
911
912 void
913 Selection::toggle (list<Selectable*> const & selectables)
914 {
915         clear_time();  //enforce region/object exclusivity
916         clear_tracks();  //enforce object/track exclusivity
917
918         RegionView* rv;
919         ControlPoint* cp;
920         vector<RegionView*> rvs;
921         vector<ControlPoint*> cps;
922
923         for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
924                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
925                         rvs.push_back (rv);
926                 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
927                         cps.push_back (cp);
928                 } else {
929                         fatal << _("programming error: ")
930                               << X_("unknown selectable type passed to Selection::toggle()")
931                               << endmsg;
932                         abort(); /*NOTREACHED*/
933                 }
934         }
935
936         if (!rvs.empty()) {
937                 toggle (rvs);
938         }
939
940         if (!cps.empty()) {
941                 toggle (cps);
942         }
943 }
944
945 void
946 Selection::set (list<Selectable*> const & selectables)
947 {
948         clear_time ();  //enforce region/object exclusivity
949         clear_tracks();  //enforce object/track exclusivity
950         clear_objects ();
951
952         add (selectables);
953 }
954
955 void
956 Selection::add (PointSelection const & s)
957 {
958         clear_time ();  //enforce region/object exclusivity
959         clear_tracks();  //enforce object/track exclusivity
960
961         for (PointSelection::const_iterator i = s.begin(); i != s.end(); ++i) {
962                 points.push_back (*i);
963         }
964 }
965
966 void
967 Selection::add (list<Selectable*> const & selectables)
968 {
969         clear_time ();  //enforce region/object exclusivity
970         clear_tracks();  //enforce object/track exclusivity
971
972         RegionView* rv;
973         ControlPoint* cp;
974         vector<RegionView*> rvs;
975         vector<ControlPoint*> cps;
976
977         for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
978                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
979                         rvs.push_back (rv);
980                 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
981                         cps.push_back (cp);
982                 } else {
983                         fatal << _("programming error: ")
984                               << X_("unknown selectable type passed to Selection::add()")
985                               << endmsg;
986                         abort(); /*NOTREACHED*/
987                 }
988         }
989
990         if (!rvs.empty()) {
991                 add (rvs);
992         }
993
994         if (!cps.empty()) {
995                 add (cps);
996         }
997 }
998
999 void
1000 Selection::clear_points (bool with_signal)
1001 {
1002         if (!points.empty()) {
1003                 points.clear ();
1004                 if (with_signal) {
1005                         PointsChanged ();
1006                 }
1007         }
1008 }
1009
1010 void
1011 Selection::add (ControlPoint* cp)
1012 {
1013         clear_time ();  //enforce region/object exclusivity
1014         clear_tracks();  //enforce object/track exclusivity
1015
1016         cp->set_selected (true);
1017         points.push_back (cp);
1018         PointsChanged (); /* EMIT SIGNAL */
1019 }
1020
1021 void
1022 Selection::add (vector<ControlPoint*> const & cps)
1023 {
1024         clear_time ();  //enforce region/object exclusivity
1025         clear_tracks();  //enforce object/track exclusivity
1026
1027         for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
1028                 (*i)->set_selected (true);
1029                 points.push_back (*i);
1030         }
1031         PointsChanged (); /* EMIT SIGNAL */
1032 }
1033
1034 void
1035 Selection::set (ControlPoint* cp)
1036 {
1037         clear_time ();  //enforce region/object exclusivity
1038         clear_tracks();  //enforce object/track exclusivity
1039
1040         if (cp->selected () && points.size () == 1) {
1041                 return;
1042         }
1043
1044         for (uint32_t i = 0; i < cp->line().npoints(); ++i) {
1045                 cp->line().nth (i)->set_selected (false);
1046         }
1047
1048         clear_objects ();
1049         add (cp);
1050 }
1051
1052 void
1053 Selection::set (ArdourMarker* m)
1054 {
1055         clear_time ();  //enforce region/object exclusivity
1056         clear_tracks();  //enforce object/track exclusivity
1057         markers.clear ();
1058
1059         add (m);
1060 }
1061
1062 void
1063 Selection::toggle (ArdourMarker* m)
1064 {
1065         MarkerSelection::iterator i;
1066
1067         if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
1068                 add (m);
1069         } else {
1070                 remove (m);
1071         }
1072 }
1073
1074 void
1075 Selection::remove (ArdourMarker* m)
1076 {
1077         MarkerSelection::iterator i;
1078
1079         if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
1080                 markers.erase (i);
1081                 MarkersChanged();
1082         }
1083 }
1084
1085 void
1086 Selection::add (ArdourMarker* m)
1087 {
1088         clear_time ();  //enforce region/object exclusivity
1089         clear_tracks();  //enforce object/track exclusivity
1090
1091         if (find (markers.begin(), markers.end(), m) == markers.end()) {
1092                 markers.push_back (m);
1093                 MarkersChanged();
1094         }
1095 }
1096
1097 void
1098 Selection::add (const list<ArdourMarker*>& m)
1099 {
1100         clear_time ();  //enforce region/object exclusivity
1101         clear_tracks();  //enforce object/track exclusivity
1102
1103         markers.insert (markers.end(), m.begin(), m.end());
1104         markers.sort ();
1105         markers.unique ();
1106
1107         MarkersChanged ();
1108 }
1109
1110 void
1111 MarkerSelection::range (framepos_t& s, framepos_t& e)
1112 {
1113         s = max_framepos;
1114         e = 0;
1115
1116         for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
1117
1118                 if ((*i)->position() < s) {
1119                         s = (*i)->position();
1120                 }
1121
1122                 if ((*i)->position() > e) {
1123                         e = (*i)->position();
1124                 }
1125         }
1126
1127         s = std::min (s, e);
1128         e = std::max (s, e);
1129 }
1130
1131 XMLNode&
1132 Selection::get_state () const
1133 {
1134         /* XXX: not complete; just sufficient to get track selection state
1135            so that re-opening plugin windows for editor mixer strips works
1136         */
1137
1138         XMLNode* node = new XMLNode (X_("Selection"));
1139
1140         for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
1141                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
1142                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1143                 if (rtv) {
1144                         XMLNode* t = node->add_child (X_("RouteView"));
1145                         t->set_property (X_("id"), rtv->route()->id ());
1146                 } else if (atv) {
1147                         XMLNode* t = node->add_child (X_("AutomationView"));
1148                         t->set_property (X_("id"), atv->parent_stripable()->id ());
1149                         t->set_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->parameter ()));
1150                 }
1151         }
1152
1153         for (RegionSelection::const_iterator i = regions.begin(); i != regions.end(); ++i) {
1154                 XMLNode* r = node->add_child (X_("Region"));
1155                 r->set_property (X_("id"), (*i)->region ()->id ());
1156         }
1157
1158         /* midi region views have thir own internal selection. */
1159         list<pair<PBD::ID, std::set<boost::shared_ptr<Evoral::Note<Evoral::Beats> > > > > rid_notes;
1160         editor->get_per_region_note_selection (rid_notes);
1161
1162         list<pair<PBD::ID, std::set<boost::shared_ptr<Evoral::Note<Evoral::Beats> > > > >::iterator rn_it;
1163         for (rn_it = rid_notes.begin(); rn_it != rid_notes.end(); ++rn_it) {
1164                 XMLNode* n = node->add_child (X_("MIDINotes"));
1165                 n->set_property (X_("region-id"), (*rn_it).first);
1166
1167                 for (std::set<boost::shared_ptr<Evoral::Note<Evoral::Beats> > >::iterator i = (*rn_it).second.begin(); i != (*rn_it).second.end(); ++i) {
1168                         XMLNode* nc = n->add_child(X_("note"));
1169                         nc->set_property(X_("note-id"), (*i)->id());
1170                 }
1171         }
1172
1173         for (PointSelection::const_iterator i = points.begin(); i != points.end(); ++i) {
1174                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (&(*i)->line().trackview);
1175                 if (atv) {
1176
1177                         XMLNode* r = node->add_child (X_("ControlPoint"));
1178                         r->set_property (X_("type"), "track");
1179                         r->set_property (X_("route-id"), atv->parent_stripable()->id ());
1180                         r->set_property (X_("automation-list-id"), (*i)->line().the_list()->id ());
1181                         r->set_property (X_("parameter"), EventTypeMap::instance().to_symbol ((*i)->line().the_list()->parameter ()));
1182                         r->set_property (X_("view-index"), (*i)->view_index());
1183                         continue;
1184                 }
1185
1186                 AudioRegionGainLine* argl = dynamic_cast<AudioRegionGainLine*> (&(*i)->line());
1187                 if (argl) {
1188                         XMLNode* r = node->add_child (X_("ControlPoint"));
1189                         r->set_property (X_("type"), "region");
1190                         r->set_property (X_("region-id"), argl->region_view ().region ()->id ());
1191                         r->set_property (X_("view-index"), (*i)->view_index());
1192                 }
1193
1194         }
1195
1196         for (TimeSelection::const_iterator i = time.begin(); i != time.end(); ++i) {
1197                 XMLNode* t = node->add_child (X_("AudioRange"));
1198                 t->set_property (X_("start"), (*i).start);
1199                 t->set_property (X_("end"), (*i).end);
1200         }
1201
1202         for (MarkerSelection::const_iterator i = markers.begin(); i != markers.end(); ++i) {
1203                 XMLNode* t = node->add_child (X_("Marker"));
1204
1205                 bool is_start;
1206                 Location* loc = editor->find_location_from_marker (*i, is_start);
1207
1208                 t->set_property (X_("id"), loc->id());
1209                 t->set_property (X_("start"), is_start);
1210         }
1211
1212         return *node;
1213 }
1214
1215 int
1216 Selection::set_state (XMLNode const & node, int)
1217 {
1218         if (node.name() != X_("Selection")) {
1219                 return -1;
1220         }
1221
1222         clear_regions ();
1223         clear_midi_notes ();
1224         clear_points ();
1225         clear_time ();
1226         clear_markers ();
1227
1228         RegionSelection selected_regions;
1229
1230         /* NOTE: stripable/time-axis-view selection is saved/restored by
1231          * ARDOUR::CoreSelection, not this Selection object
1232          */
1233
1234         PBD::ID id;
1235         XMLNodeList children = node.children ();
1236
1237         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1238                 if ((*i)->name() == X_("Region")) {
1239
1240                         if (!(*i)->get_property (X_("id"), id)) {
1241                                 assert(false);
1242                         }
1243
1244                         RegionSelection rs;
1245                         editor->get_regionviews_by_id (id, rs);
1246
1247                         if (!rs.empty ()) {
1248                                 selected_regions.insert (selected_regions.end(), rs.begin(), rs.end());
1249                         } else {
1250                                 /*
1251                                   regionviews haven't been constructed - stash the region IDs
1252                                   so we can identify them in Editor::region_view_added ()
1253                                 */
1254                                 regions.pending.push_back (id);
1255                         }
1256
1257                 } else if ((*i)->name() == X_("MIDINotes")) {
1258
1259                         if (!(*i)->get_property (X_("region-id"), id)) {
1260                                 assert (false);
1261                         }
1262
1263                         RegionSelection rs;
1264
1265                         editor->get_regionviews_by_id (id, rs); // there could be more than one
1266
1267                         std::list<Evoral::event_id_t> notes;
1268                         XMLNodeList children = (*i)->children ();
1269
1270                         for (XMLNodeList::const_iterator ci = children.begin(); ci != children.end(); ++ci) {
1271                                 Evoral::event_id_t id;
1272                                 if ((*ci)->get_property (X_ ("note-id"), id)) {
1273                                         notes.push_back (id);
1274                                 }
1275                         }
1276
1277                         for (RegionSelection::iterator rsi = rs.begin(); rsi != rs.end(); ++rsi) {
1278                                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*rsi);
1279                                 if (mrv) {
1280                                         mrv->select_notes(notes);
1281                                 }
1282                         }
1283
1284                         if (rs.empty()) {
1285                                 /* regionviews containing these notes don't yet exist on the canvas.*/
1286                                 pending_midi_note_selection.push_back (make_pair (id, notes));
1287                         }
1288
1289                 } else if  ((*i)->name() == X_("ControlPoint")) {
1290                         XMLProperty const * prop_type = (*i)->property (X_("type"));
1291
1292                         assert(prop_type);
1293
1294                         if (prop_type->value () == "track") {
1295
1296                                 PBD::ID route_id;
1297                                 PBD::ID alist_id;
1298                                 std::string param;
1299                                 uint32_t view_index;
1300
1301                                 if (!(*i)->get_property (X_("route-id"), route_id) ||
1302                                     !(*i)->get_property (X_("automation-list-id"), alist_id) ||
1303                                     !(*i)->get_property (X_("parameter"), param) ||
1304                                     !(*i)->get_property (X_("view-index"), view_index)) {
1305                                         assert(false);
1306                                 }
1307
1308                                 RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (route_id); // XXX may also be VCA
1309                                 vector <ControlPoint *> cps;
1310
1311                                 if (rtv) {
1312                                         boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().from_symbol (param));
1313                                         if (atv) {
1314                                                 list<boost::shared_ptr<AutomationLine> > lines = atv->lines();
1315                                                 for (list<boost::shared_ptr<AutomationLine> > ::iterator li = lines.begin(); li != lines.end(); ++li) {
1316                                                         if ((*li)->the_list()->id() == alist_id) {
1317                                                                 ControlPoint* cp = (*li)->nth(view_index);
1318                                                                 if (cp) {
1319                                                                         cps.push_back (cp);
1320                                                                         cp->show();
1321                                                                 }
1322                                                         }
1323                                                 }
1324                                         }
1325                                 }
1326                                 if (!cps.empty()) {
1327                                         add (cps);
1328                                 }
1329                         } else if (prop_type->value () == "region") {
1330
1331                                 PBD::ID region_id;
1332                                 uint32_t view_index;
1333                                 if (!(*i)->get_property (X_("region-id"), region_id) ||
1334                                     !(*i)->get_property (X_("view-index"), view_index)) {
1335                                         continue;
1336                                 }
1337
1338                                 RegionSelection rs;
1339                                 editor->get_regionviews_by_id (region_id, rs);
1340
1341                                 if (!rs.empty ()) {
1342                                         vector <ControlPoint *> cps;
1343                                         for (RegionSelection::iterator rsi = rs.begin(); rsi != rs.end(); ++rsi) {
1344                                                 AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*rsi);
1345                                                 if (arv) {
1346                                                         boost::shared_ptr<AudioRegionGainLine> gl = arv->get_gain_line ();
1347                                                         ControlPoint* cp = gl->nth(view_index);
1348                                                         if (cp) {
1349                                                                 cps.push_back (cp);
1350                                                                 cp->show();
1351                                                         }
1352                                                 }
1353                                         }
1354                                         if (!cps.empty()) {
1355                                                 add (cps);
1356                                         }
1357                                 }
1358                         }
1359
1360                 } else if  ((*i)->name() == X_("AudioRange")) {
1361                         framepos_t start;
1362                         framepos_t end;
1363
1364                         if (!(*i)->get_property (X_("start"), start) || !(*i)->get_property (X_("end"), end)) {
1365                                 assert(false);
1366                         }
1367                         set_preserving_all_ranges (start, end);
1368
1369                 } else if ((*i)->name() == X_("AutomationView")) {
1370
1371                         std::string param;
1372
1373                         if (!(*i)->get_property (X_("id"), id) || !(*i)->get_property (X_("parameter"), param)) {
1374                                 assert (false);
1375                         }
1376
1377                         RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1378
1379                         if (rtv) {
1380                                 boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().from_symbol (param));
1381
1382                                 /* the automation could be for an entity that was never saved
1383                                    in the session file. Don't freak out if we can't find
1384                                    it.
1385                                 */
1386
1387                                 if (atv) {
1388                                         add (atv.get());
1389                                 }
1390                         }
1391
1392                 } else if ((*i)->name() == X_("Marker")) {
1393
1394                         bool is_start;
1395                         if (!(*i)->get_property (X_("id"), id) || !(*i)->get_property (X_("start"), is_start)) {
1396                                 assert(false);
1397                         }
1398
1399                         ArdourMarker* m = editor->find_marker_from_location_id (id, is_start);
1400                         if (m) {
1401                                 add (m);
1402                         }
1403
1404                 }
1405
1406         }
1407
1408         // now add regions to selection at once
1409         add (selected_regions);
1410
1411         return 0;
1412 }
1413
1414 void
1415 Selection::remove_regions (TimeAxisView* t)
1416 {
1417         RegionSelection::iterator i = regions.begin();
1418         while (i != regions.end ()) {
1419                 RegionSelection::iterator tmp = i;
1420                 ++tmp;
1421
1422                 if (&(*i)->get_time_axis_view() == t) {
1423                         remove (*i);
1424                 }
1425
1426                 i = tmp;
1427         }
1428 }
1429
1430 /* TIME AXIS VIEW ... proxy for Stripable/Controllable
1431  *
1432  * public methods just modify the CoreSelection; PresentationInfo::Changed will
1433  * trigger Selection::core_selection_changed() and we will update our own data
1434  * structures there.
1435  */
1436
1437 void
1438 Selection::toggle (const TrackViewList& track_list)
1439 {
1440         TrackViewList t = add_grouped_tracks (track_list);
1441
1442         CoreSelection& selection (editor->session()->selection());
1443         PresentationInfo::ChangeSuspender cs;
1444
1445         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1446                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1447                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1448                 selection.toggle (s, c);
1449         }
1450 }
1451
1452 void
1453 Selection::toggle (TimeAxisView* track)
1454 {
1455         if (dynamic_cast<VCATimeAxisView*> (track)) {
1456                 return;
1457         }
1458
1459         TrackViewList tr;
1460         tr.push_back (track);
1461         toggle (tr);
1462 }
1463
1464 void
1465 Selection::add (TrackViewList const & track_list)
1466 {
1467         TrackViewList t = add_grouped_tracks (track_list);
1468
1469         CoreSelection& selection (editor->session()->selection());
1470         PresentationInfo::ChangeSuspender cs;
1471
1472         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1473                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1474                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1475                 selection.add (s, c);
1476         }
1477 }
1478
1479 void
1480 Selection::add (TimeAxisView* track)
1481 {
1482         if (dynamic_cast<VCATimeAxisView*> (track)) {
1483                 return;
1484         }
1485
1486         TrackViewList tr;
1487         tr.push_back (track);
1488         add (tr);
1489 }
1490
1491 void
1492 Selection::remove (TimeAxisView* track)
1493 {
1494         if (dynamic_cast<VCATimeAxisView*> (track)) {
1495                 return;
1496         }
1497
1498         TrackViewList tvl;
1499         tvl.push_back (track);
1500         remove (tvl);
1501 }
1502
1503 void
1504 Selection::remove (const TrackViewList& t)
1505 {
1506         CoreSelection& selection (editor->session()->selection());
1507         PresentationInfo::ChangeSuspender cs;
1508
1509         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1510                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1511                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1512                 selection.remove (s, c);
1513         }
1514 }
1515
1516 void
1517 Selection::set (TimeAxisView* track)
1518 {
1519         if (dynamic_cast<VCATimeAxisView*> (track)) {
1520                 return;
1521         }
1522
1523         TrackViewList tvl;
1524         tvl.push_back (track);
1525         set (tvl);
1526 }
1527
1528 void
1529 Selection::set (const TrackViewList& track_list)
1530 {
1531         TrackViewList t = add_grouped_tracks (track_list);
1532
1533         CoreSelection& selection (editor->session()->selection());
1534         PresentationInfo::ChangeSuspender cs;
1535
1536         selection.clear_stripables ();
1537
1538         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1539                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1540                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1541                 selection.add (s, c);
1542         }
1543 }
1544
1545 void
1546 Selection::clear_tracks (bool)
1547 {
1548         if (!manage_libardour_selection) {
1549                 return;
1550         }
1551
1552         Session* s = editor->session();
1553         if (s) {
1554                 CoreSelection& selection (s->selection());
1555                 selection.clear_stripables ();
1556         }
1557 }
1558
1559 bool
1560 Selection::selected (TimeAxisView* tv) const
1561 {
1562         Session* session = editor->session();
1563
1564         if (!session) {
1565                 return false;
1566         }
1567
1568         CoreSelection& selection (session->selection());
1569         boost::shared_ptr<Stripable> s = tv->stripable ();
1570         boost::shared_ptr<AutomationControl> c = tv->control ();
1571
1572         if (c) {
1573                 return selection.selected (c);
1574         }
1575
1576         return selection.selected (s);
1577 }
1578
1579 TrackViewList
1580 Selection::add_grouped_tracks (TrackViewList const & t)
1581 {
1582         TrackViewList added;
1583
1584         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1585                 if (dynamic_cast<VCATimeAxisView*> (*i)) {
1586                         continue;
1587                 }
1588
1589                 /* select anything in the same select-enabled route group */
1590                 ARDOUR::RouteGroup* rg = (*i)->route_group ();
1591
1592                 if (rg && rg->is_active() && rg->is_select ()) {
1593
1594                         TrackViewList tr = editor->axis_views_from_routes (rg->route_list ());
1595
1596                         for (TrackViewList::iterator j = tr.begin(); j != tr.end(); ++j) {
1597
1598                                 /* Do not add the trackview passed in as an
1599                                  * argument, because we want that to be on the
1600                                  * end of the list.
1601                                  */
1602
1603                                 if (*j != *i) {
1604                                         if (!added.contains (*j)) {
1605                                                 added.push_back (*j);
1606                                         }
1607                                 }
1608                         }
1609                 }
1610         }
1611
1612         /* now add the the trackview's passed in as actual arguments */
1613         added.insert (added.end(), t.begin(), t.end());
1614
1615         return added;
1616 }
1617
1618 #if 0
1619 static void dump_tracks (Selection const & s)
1620 {
1621         cerr << "--TRACKS [" << s.tracks.size() << ']' << ":\n";
1622         for (TrackViewList::const_iterator x = s.tracks.begin(); x != s.tracks.end(); ++x) {
1623                 cerr << (*x)->name() << ' ' << (*x)->stripable() << " C = " << (*x)->control() << endl;
1624         }
1625         cerr << "///\n";
1626 }
1627 #endif
1628
1629 void
1630 Selection::core_selection_changed (PropertyChange const & what_changed)
1631 {
1632         PropertyChange pc;
1633
1634         pc.add (Properties::selected);
1635
1636         if (!what_changed.contains (pc)) {
1637                 return;
1638         }
1639
1640         CoreSelection& selection (editor->session()->selection());
1641
1642         if (selection.selected()) {
1643                 clear_objects();  // enforce object/range exclusivity
1644         }
1645
1646         tracks.clear (); // clear stage for whatever tracks are now selected (maybe none)
1647
1648         CoreSelection::StripableAutomationControls sac;
1649         selection.get_stripables (sac);
1650
1651         for (CoreSelection::StripableAutomationControls::const_iterator i = sac.begin(); i != sac.end(); ++i) {
1652                 AxisView* av;
1653                 TimeAxisView* tav;
1654                 if ((*i).controllable) {
1655                         av = editor->axis_view_by_control ((*i).controllable);
1656                 } else {
1657                         av = editor->axis_view_by_stripable ((*i).stripable);
1658                 }
1659
1660                 tav = dynamic_cast<TimeAxisView*>(av);
1661                 if (tav) {
1662                         tracks.push_back (tav);
1663                 }
1664         }
1665 }