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