use new action map API instead of ActionManager::get_action
[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 (samplepos_t start, samplepos_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 (samplepos_t start, samplepos_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 (samplecnt_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, samplepos_t start, samplepos_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 (samplepos_t /*start*/, samplepos_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 (samplepos_t start, samplepos_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 (samplepos_t start, samplepos_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 (samplepos_t& s, samplepos_t& e)
1112 {
1113         s = max_samplepos;
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                 StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (*i);
1142                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1143                 if (stv) {
1144                         XMLNode* t = node->add_child (X_("StripableView"));
1145                         t->set_property (X_("id"), stv->stripable()->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         if (!regions.empty()) {
1154                 XMLNode* parent = node->add_child (X_("Regions"));
1155                 for (RegionSelection::const_iterator i = regions.begin(); i != regions.end(); ++i) {
1156                         XMLNode* r = parent->add_child (X_("Region"));
1157                         r->set_property (X_("id"), (*i)->region ()->id ());
1158                 }
1159         }
1160
1161         /* midi region views have thir own internal selection. */
1162         list<pair<PBD::ID, std::set<boost::shared_ptr<Evoral::Note<Temporal::Beats> > > > > rid_notes;
1163         editor->get_per_region_note_selection (rid_notes);
1164
1165         list<pair<PBD::ID, std::set<boost::shared_ptr<Evoral::Note<Temporal::Beats> > > > >::iterator rn_it;
1166         for (rn_it = rid_notes.begin(); rn_it != rid_notes.end(); ++rn_it) {
1167                 XMLNode* n = node->add_child (X_("MIDINotes"));
1168                 n->set_property (X_("region-id"), (*rn_it).first);
1169
1170                 for (std::set<boost::shared_ptr<Evoral::Note<Temporal::Beats> > >::iterator i = (*rn_it).second.begin(); i != (*rn_it).second.end(); ++i) {
1171                         XMLNode* nc = n->add_child(X_("note"));
1172                         nc->set_property(X_("note-id"), (*i)->id());
1173                 }
1174         }
1175
1176         for (PointSelection::const_iterator i = points.begin(); i != points.end(); ++i) {
1177                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (&(*i)->line().trackview);
1178                 if (atv) {
1179
1180                         XMLNode* r = node->add_child (X_("ControlPoint"));
1181                         r->set_property (X_("type"), "track");
1182                         r->set_property (X_("route-id"), atv->parent_stripable()->id ());
1183                         r->set_property (X_("automation-list-id"), (*i)->line().the_list()->id ());
1184                         r->set_property (X_("parameter"), EventTypeMap::instance().to_symbol ((*i)->line().the_list()->parameter ()));
1185                         r->set_property (X_("view-index"), (*i)->view_index());
1186                         continue;
1187                 }
1188
1189                 AudioRegionGainLine* argl = dynamic_cast<AudioRegionGainLine*> (&(*i)->line());
1190                 if (argl) {
1191                         XMLNode* r = node->add_child (X_("ControlPoint"));
1192                         r->set_property (X_("type"), "region");
1193                         r->set_property (X_("region-id"), argl->region_view ().region ()->id ());
1194                         r->set_property (X_("view-index"), (*i)->view_index());
1195                 }
1196
1197         }
1198
1199         for (TimeSelection::const_iterator i = time.begin(); i != time.end(); ++i) {
1200                 XMLNode* t = node->add_child (X_("AudioRange"));
1201                 t->set_property (X_("start"), (*i).start);
1202                 t->set_property (X_("end"), (*i).end);
1203         }
1204
1205         for (MarkerSelection::const_iterator i = markers.begin(); i != markers.end(); ++i) {
1206                 XMLNode* t = node->add_child (X_("Marker"));
1207
1208                 bool is_start;
1209                 Location* loc = editor->find_location_from_marker (*i, is_start);
1210
1211                 t->set_property (X_("id"), loc->id());
1212                 t->set_property (X_("start"), is_start);
1213         }
1214
1215         return *node;
1216 }
1217
1218 int
1219 Selection::set_state (XMLNode const & node, int)
1220 {
1221         if (node.name() != X_("Selection")) {
1222                 return -1;
1223         }
1224
1225         clear_regions ();
1226         clear_midi_notes ();
1227         clear_points ();
1228         clear_time ();
1229         clear_markers ();
1230
1231         /* NOTE: stripable/time-axis-view selection is saved/restored by
1232          * ARDOUR::CoreSelection, not this Selection object
1233          */
1234
1235         PBD::ID id;
1236         XMLNodeList children = node.children ();
1237
1238         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1239                 if ((*i)->name() == X_("Regions")) {
1240                         RegionSelection selected_regions;
1241                         XMLNodeList children = (*i)->children ();
1242                         for (XMLNodeList::const_iterator ci = children.begin(); ci != children.end(); ++ci) {
1243                                 PBD::ID id;
1244
1245                                 if (!(*ci)->get_property (X_("id"), id)) {
1246                                         continue;
1247                                 }
1248
1249                                 RegionSelection rs;
1250                                 editor->get_regionviews_by_id (id, rs);
1251
1252                                 if (!rs.empty ()) {
1253                                         for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
1254                                                 selected_regions.push_back (*i);
1255                                         }
1256                                 } else {
1257                                         /*
1258                                           regionviews haven't been constructed - stash the region IDs
1259                                           so we can identify them in Editor::region_view_added ()
1260                                         */
1261                                         regions.pending.push_back (id);
1262                                 }
1263                         }
1264
1265                         if (!selected_regions.empty()) {
1266                                 add (selected_regions);
1267                         }
1268
1269                 } else if ((*i)->name() == X_("MIDINotes")) {
1270
1271                         if (!(*i)->get_property (X_("region-id"), id)) {
1272                                 assert (false);
1273                         }
1274
1275                         RegionSelection rs;
1276
1277                         editor->get_regionviews_by_id (id, rs); // there could be more than one
1278
1279                         std::list<Evoral::event_id_t> notes;
1280                         XMLNodeList children = (*i)->children ();
1281
1282                         for (XMLNodeList::const_iterator ci = children.begin(); ci != children.end(); ++ci) {
1283                                 Evoral::event_id_t id;
1284                                 if ((*ci)->get_property (X_ ("note-id"), id)) {
1285                                         notes.push_back (id);
1286                                 }
1287                         }
1288
1289                         for (RegionSelection::iterator rsi = rs.begin(); rsi != rs.end(); ++rsi) {
1290                                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*rsi);
1291                                 if (mrv) {
1292                                         mrv->select_notes(notes);
1293                                 }
1294                         }
1295
1296                         if (rs.empty()) {
1297                                 /* regionviews containing these notes don't yet exist on the canvas.*/
1298                                 pending_midi_note_selection.push_back (make_pair (id, notes));
1299                         }
1300
1301                 } else if ((*i)->name() == X_("ControlPoint")) {
1302                         XMLProperty const * prop_type = (*i)->property (X_("type"));
1303
1304                         assert(prop_type);
1305
1306                         if (prop_type->value () == "track") {
1307
1308                                 PBD::ID route_id;
1309                                 PBD::ID alist_id;
1310                                 std::string param;
1311                                 uint32_t view_index;
1312
1313                                 if (!(*i)->get_property (X_("route-id"), route_id) ||
1314                                     !(*i)->get_property (X_("automation-list-id"), alist_id) ||
1315                                     !(*i)->get_property (X_("parameter"), param) ||
1316                                     !(*i)->get_property (X_("view-index"), view_index)) {
1317                                         assert(false);
1318                                 }
1319
1320                                 StripableTimeAxisView* stv = editor->get_stripable_time_axis_by_id (route_id);
1321                                 vector <ControlPoint *> cps;
1322
1323                                 if (stv) {
1324                                         boost::shared_ptr<AutomationTimeAxisView> atv = stv->automation_child (EventTypeMap::instance().from_symbol (param));
1325                                         if (atv) {
1326                                                 list<boost::shared_ptr<AutomationLine> > lines = atv->lines();
1327                                                 for (list<boost::shared_ptr<AutomationLine> > ::iterator li = lines.begin(); li != lines.end(); ++li) {
1328                                                         if ((*li)->the_list()->id() == alist_id) {
1329                                                                 ControlPoint* cp = (*li)->nth(view_index);
1330                                                                 if (cp) {
1331                                                                         cps.push_back (cp);
1332                                                                         cp->show();
1333                                                                 }
1334                                                         }
1335                                                 }
1336                                         }
1337                                 }
1338                                 if (!cps.empty()) {
1339                                         add (cps);
1340                                 }
1341                         } else if (prop_type->value () == "region") {
1342
1343                                 PBD::ID region_id;
1344                                 uint32_t view_index;
1345                                 if (!(*i)->get_property (X_("region-id"), region_id) ||
1346                                     !(*i)->get_property (X_("view-index"), view_index)) {
1347                                         continue;
1348                                 }
1349
1350                                 RegionSelection rs;
1351                                 editor->get_regionviews_by_id (region_id, rs);
1352
1353                                 if (!rs.empty ()) {
1354                                         vector <ControlPoint *> cps;
1355                                         for (RegionSelection::iterator rsi = rs.begin(); rsi != rs.end(); ++rsi) {
1356                                                 AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*rsi);
1357                                                 if (arv) {
1358                                                         boost::shared_ptr<AudioRegionGainLine> gl = arv->get_gain_line ();
1359                                                         ControlPoint* cp = gl->nth(view_index);
1360                                                         if (cp) {
1361                                                                 cps.push_back (cp);
1362                                                                 cp->show();
1363                                                         }
1364                                                 }
1365                                         }
1366                                         if (!cps.empty()) {
1367                                                 add (cps);
1368                                         }
1369                                 }
1370                         }
1371
1372                 } else if ((*i)->name() == X_("AudioRange")) {
1373                         samplepos_t start;
1374                         samplepos_t end;
1375
1376                         if (!(*i)->get_property (X_("start"), start) || !(*i)->get_property (X_("end"), end)) {
1377                                 assert(false);
1378                         }
1379
1380                         add (start, end);
1381
1382                 } else if ((*i)->name() == X_("AutomationView")) {
1383
1384                         std::string param;
1385
1386                         if (!(*i)->get_property (X_("id"), id) || !(*i)->get_property (X_("parameter"), param)) {
1387                                 assert (false);
1388                         }
1389
1390                         StripableTimeAxisView* stv = editor->get_stripable_time_axis_by_id (id);
1391
1392                         if (stv) {
1393                                 boost::shared_ptr<AutomationTimeAxisView> atv = stv->automation_child (EventTypeMap::instance().from_symbol (param));
1394
1395                                 /* the automation could be for an entity that was never saved
1396                                    in the session file. Don't freak out if we can't find
1397                                    it.
1398                                 */
1399
1400                                 if (atv) {
1401                                         add (atv.get());
1402                                 }
1403                         }
1404
1405                 } else if ((*i)->name() == X_("Marker")) {
1406
1407                         bool is_start;
1408                         if (!(*i)->get_property (X_("id"), id) || !(*i)->get_property (X_("start"), is_start)) {
1409                                 assert(false);
1410                         }
1411
1412                         ArdourMarker* m = editor->find_marker_from_location_id (id, is_start);
1413                         if (m) {
1414                                 add (m);
1415                         }
1416
1417                 }
1418
1419         }
1420
1421         return 0;
1422 }
1423
1424 void
1425 Selection::remove_regions (TimeAxisView* t)
1426 {
1427         RegionSelection::iterator i = regions.begin();
1428         while (i != regions.end ()) {
1429                 RegionSelection::iterator tmp = i;
1430                 ++tmp;
1431
1432                 if (&(*i)->get_time_axis_view() == t) {
1433                         remove (*i);
1434                 }
1435
1436                 i = tmp;
1437         }
1438 }
1439
1440 /* TIME AXIS VIEW ... proxy for Stripable/Controllable
1441  *
1442  * public methods just modify the CoreSelection; PresentationInfo::Changed will
1443  * trigger Selection::core_selection_changed() and we will update our own data
1444  * structures there.
1445  */
1446
1447 void
1448 Selection::toggle (const TrackViewList& track_list)
1449 {
1450         TrackViewList t = add_grouped_tracks (track_list);
1451
1452         CoreSelection& selection (editor->session()->selection());
1453         PresentationInfo::ChangeSuspender cs;
1454
1455         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1456                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1457                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1458                 selection.toggle (s, c);
1459         }
1460 }
1461
1462 void
1463 Selection::toggle (TimeAxisView* track)
1464 {
1465         TrackViewList tr;
1466         tr.push_back (track);
1467         toggle (tr);
1468 }
1469
1470 void
1471 Selection::add (TrackViewList const & track_list)
1472 {
1473         TrackViewList t = add_grouped_tracks (track_list);
1474
1475         CoreSelection& selection (editor->session()->selection());
1476         PresentationInfo::ChangeSuspender cs;
1477
1478         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1479                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1480                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1481                 selection.add (s, c);
1482         }
1483 }
1484
1485 void
1486 Selection::add (TimeAxisView* track)
1487 {
1488         TrackViewList tr;
1489         tr.push_back (track);
1490         add (tr);
1491 }
1492
1493 void
1494 Selection::remove (TimeAxisView* track)
1495 {
1496         TrackViewList tvl;
1497         tvl.push_back (track);
1498         remove (tvl);
1499 }
1500
1501 void
1502 Selection::remove (const TrackViewList& t)
1503 {
1504         CoreSelection& selection (editor->session()->selection());
1505         PresentationInfo::ChangeSuspender cs;
1506
1507         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1508                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1509                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1510                 selection.remove (s, c);
1511         }
1512 }
1513
1514 void
1515 Selection::set (TimeAxisView* track)
1516 {
1517         TrackViewList tvl;
1518         tvl.push_back (track);
1519         set (tvl);
1520 }
1521
1522 void
1523 Selection::set (const TrackViewList& track_list)
1524 {
1525         TrackViewList t = add_grouped_tracks (track_list);
1526
1527         CoreSelection& selection (editor->session()->selection());
1528
1529 #if 1 // crazy optmization hack
1530         /* check is the selection actually changed, ignore NO-OPs
1531          *
1532          * There are excessive calls from EditorRoutes::selection_changed():
1533          * Every click calls selection_changed() even if it doesn't change.
1534          * Also re-ordering tracks calls into this due to gtk's odd DnD signal
1535          * messaging (row removed, re-added).
1536          *
1537          * Re-ordering a row results in at least 2 calls to selection_changed()
1538          * without actual change. Calling selection.clear_stripables()
1539          * and re-adding the same tracks every time in turn emits changed signals.
1540          */
1541         bool changed = false;
1542         CoreSelection::StripableAutomationControls sac;
1543         selection.get_stripables (sac);
1544         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1545                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1546                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1547                 bool found = false;
1548                 for (CoreSelection::StripableAutomationControls::iterator j = sac.begin (); j != sac.end (); ++j) {
1549                         if (j->stripable == s && j->controllable == c) {
1550                                 found = true;
1551                                 sac.erase (j);
1552                                 break;
1553                         }
1554                 }
1555                 if (!found) {
1556                         changed = true;
1557                         break;
1558                 }
1559         }
1560         if (!changed && sac.size() == 0) {
1561                 return;
1562         }
1563 #endif
1564
1565         PresentationInfo::ChangeSuspender cs;
1566
1567         selection.clear_stripables ();
1568
1569         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1570                 boost::shared_ptr<Stripable> s = (*i)->stripable ();
1571                 boost::shared_ptr<AutomationControl> c = (*i)->control ();
1572                 selection.add (s, c);
1573         }
1574 }
1575
1576 void
1577 Selection::clear_tracks (bool)
1578 {
1579         if (!manage_libardour_selection) {
1580                 return;
1581         }
1582
1583         Session* s = editor->session();
1584         if (s) {
1585                 CoreSelection& selection (s->selection());
1586                 selection.clear_stripables ();
1587         }
1588 }
1589
1590 bool
1591 Selection::selected (TimeAxisView* tv) const
1592 {
1593         Session* session = editor->session();
1594
1595         if (!session) {
1596                 return false;
1597         }
1598
1599         CoreSelection& selection (session->selection());
1600         boost::shared_ptr<Stripable> s = tv->stripable ();
1601         boost::shared_ptr<AutomationControl> c = tv->control ();
1602
1603         if (c) {
1604                 return selection.selected (c);
1605         }
1606
1607         return selection.selected (s);
1608 }
1609
1610 TrackViewList
1611 Selection::add_grouped_tracks (TrackViewList const & t)
1612 {
1613         TrackViewList added;
1614
1615         for (TrackSelection::const_iterator i = t.begin(); i != t.end(); ++i) {
1616                 if (dynamic_cast<VCATimeAxisView*> (*i)) {
1617                         continue;
1618                 }
1619
1620                 /* select anything in the same select-enabled route group */
1621                 ARDOUR::RouteGroup* rg = (*i)->route_group ();
1622
1623                 if (rg && rg->is_active() && rg->is_select ()) {
1624
1625                         TrackViewList tr = editor->axis_views_from_routes (rg->route_list ());
1626
1627                         for (TrackViewList::iterator j = tr.begin(); j != tr.end(); ++j) {
1628
1629                                 /* Do not add the trackview passed in as an
1630                                  * argument, because we want that to be on the
1631                                  * end of the list.
1632                                  */
1633
1634                                 if (*j != *i) {
1635                                         if (!added.contains (*j)) {
1636                                                 added.push_back (*j);
1637                                         }
1638                                 }
1639                         }
1640                 }
1641         }
1642
1643         /* now add the the trackview's passed in as actual arguments */
1644         added.insert (added.end(), t.begin(), t.end());
1645
1646         return added;
1647 }
1648
1649 #if 0
1650 static void dump_tracks (Selection const & s)
1651 {
1652         cerr << "--TRACKS [" << s.tracks.size() << ']' << ":\n";
1653         for (TrackViewList::const_iterator x = s.tracks.begin(); x != s.tracks.end(); ++x) {
1654                 cerr << (*x)->name() << ' ' << (*x)->stripable() << " C = " << (*x)->control() << endl;
1655         }
1656         cerr << "///\n";
1657 }
1658 #endif
1659
1660 void
1661 Selection::core_selection_changed (PropertyChange const & what_changed)
1662 {
1663         PropertyChange pc;
1664
1665         pc.add (Properties::selected);
1666
1667         if (!what_changed.contains (pc)) {
1668                 return;
1669         }
1670
1671         CoreSelection& selection (editor->session()->selection());
1672
1673         if (selection.selected()) {
1674                 clear_objects(); // enforce object/range exclusivity
1675         }
1676
1677         tracks.clear (); // clear stage for whatever tracks are now selected (maybe none)
1678
1679         CoreSelection::StripableAutomationControls sac;
1680         selection.get_stripables (sac);
1681
1682         for (CoreSelection::StripableAutomationControls::const_iterator i = sac.begin(); i != sac.end(); ++i) {
1683                 AxisView* av;
1684                 TimeAxisView* tav;
1685                 if ((*i).controllable) {
1686                         av = editor->axis_view_by_control ((*i).controllable);
1687                 } else {
1688                         av = editor->axis_view_by_stripable ((*i).stripable);
1689                 }
1690
1691                 tav = dynamic_cast<TimeAxisView*>(av);
1692                 if (tav) {
1693                         tracks.push_back (tav);
1694                 }
1695         }
1696
1697         TracksChanged();
1698 }