fix load+save of plugin parameter automation
[ardour.git] / gtk2_ardour / selection.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <algorithm>
21 #include <sigc++/bind.h>
22 #include "pbd/error.h"
23 #include "pbd/stacktrace.h"
24
25 #include "ardour/playlist.h"
26 #include "ardour/rc_configuration.h"
27
28 #include "gui_thread.h"
29 #include "midi_cut_buffer.h"
30 #include "region_view.h"
31 #include "selection.h"
32 #include "selection_templates.h"
33 #include "time_axis_view.h"
34 #include "automation_time_axis.h"
35 #include "public_editor.h"
36 #include "control_point.h"
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 struct AudioRangeComparator {
45     bool operator()(AudioRange a, AudioRange b) {
46             return a.start < b.start;
47     }
48 };
49
50 Selection::Selection (const PublicEditor* e)
51         : tracks (e)
52         , editor (e)
53         , next_time_id (0) 
54 {
55         clear ();
56
57         /* we have disambiguate which remove() for the compiler */
58
59         void (Selection::*track_remove)(TimeAxisView*) = &Selection::remove;
60         TimeAxisView::CatchDeletion.connect (*this, MISSING_INVALIDATOR, ui_bind (track_remove, this, _1), gui_context());
61
62         void (Selection::*marker_remove)(Marker*) = &Selection::remove;
63         Marker::CatchDeletion.connect (*this, MISSING_INVALIDATOR, ui_bind (marker_remove, this, _1), gui_context());
64 }       
65
66 #if 0
67 Selection&
68 Selection::operator= (const Selection& other)
69 {
70         if (&other != this) {
71                 regions = other.regions;
72                 tracks = other.tracks;
73                 time = other.time;
74                 lines = other.lines;
75                 midi_regions = other.midi_regions;
76                 midi_notes = other.midi_notes;
77         }
78         return *this;
79 }
80 #endif
81
82 bool
83 operator== (const Selection& a, const Selection& b)
84 {
85         return a.regions == b.regions &&
86                 a.tracks == b.tracks &&
87                 a.time == b.time &&
88                 a.lines == b.lines &&
89                 a.playlists == b.playlists &&
90                 a.midi_notes == b.midi_notes &&
91                 a.midi_regions == b.midi_regions;
92 }
93
94 /** Clear everything from the Selection */
95 void
96 Selection::clear ()
97 {
98         clear_tracks ();
99         clear_regions ();
100         clear_points ();
101         clear_lines();
102         clear_time ();
103         clear_playlists ();
104         clear_midi_notes ();
105         clear_midi_regions ();
106 }
107
108 void
109 Selection::dump_region_layers()
110 {
111         cerr << "region selection layer dump" << endl;
112         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
113                 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
114         }
115 }
116
117
118 void
119 Selection::clear_regions ()
120 {
121         if (!regions.empty()) {
122                 regions.clear_all ();
123                 RegionsChanged();
124         }
125 }
126
127 void
128 Selection::clear_tracks ()
129 {
130         if (!tracks.empty()) {
131                 tracks.clear ();
132                 TracksChanged();
133         }
134 }
135
136 void
137 Selection::clear_midi_notes ()
138 {
139         if (!midi_notes.empty()) {
140                 for (MidiNoteSelection::iterator x = midi_notes.begin(); x != midi_notes.end(); ++x) {
141                         delete *x;
142                 }
143                 midi_notes.clear ();
144                 MidiNotesChanged ();
145         }
146 }
147
148 void
149 Selection::clear_midi_regions ()
150 {
151         if (!midi_regions.empty()) {
152                 midi_regions.clear ();
153                 MidiRegionsChanged ();
154         }
155 }
156
157 void
158 Selection::clear_time ()
159 {
160         time.clear();
161
162         TimeChanged ();
163 }
164
165 void
166 Selection::clear_playlists ()
167 {
168         /* Selections own their playlists */
169
170         for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
171                 /* selections own their own regions, which are copies of the "originals". make them go away */
172                 (*i)->drop_regions ();
173                 (*i)->release ();
174         }
175
176         if (!playlists.empty()) {
177                 playlists.clear ();
178                 PlaylistsChanged();
179         }
180 }
181
182 void
183 Selection::clear_lines ()
184 {
185         if (!lines.empty()) {
186                 lines.clear ();
187                 LinesChanged();
188         }
189 }
190
191 void
192 Selection::clear_markers ()
193 {
194         if (!markers.empty()) {
195                 markers.clear ();
196                 MarkersChanged();
197         }
198 }
199
200 void
201 Selection::toggle (boost::shared_ptr<Playlist> pl)
202 {
203         PlaylistSelection::iterator i;
204
205         if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
206                 pl->use ();
207                 playlists.push_back(pl);
208         } else {
209                 playlists.erase (i);
210         }
211
212         PlaylistsChanged ();
213 }
214
215 void
216 Selection::toggle (const TrackViewList& track_list)
217 {
218         for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
219                 toggle ((*i));
220         }
221 }
222
223 void
224 Selection::toggle (TimeAxisView* track)
225 {
226         TrackSelection::iterator i;
227
228         if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
229                 tracks.push_back (track);
230         } else {
231                 tracks.erase (i);
232         }
233
234         TracksChanged();
235 }
236
237 void
238 Selection::toggle (const MidiNoteSelection& midi_note_list)
239 {
240         for (MidiNoteSelection::const_iterator i = midi_note_list.begin(); i != midi_note_list.end(); ++i) {
241                 toggle ((*i));
242         }
243 }
244
245 void
246 Selection::toggle (MidiCutBuffer* midi)
247 {
248         MidiNoteSelection::iterator i;
249
250         if ((i = find (midi_notes.begin(), midi_notes.end(), midi)) == midi_notes.end()) {
251                 midi_notes.push_back (midi);
252         } else {
253                 /* remember that we own the MCB */
254                 delete *i;
255                 midi_notes.erase (i);
256         }
257
258         MidiNotesChanged();
259 }
260
261
262 void
263 Selection::toggle (RegionView* r)
264 {
265         RegionSelection::iterator i;
266
267         if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
268                 add (r);
269         } else {
270                 remove (*i);
271         }
272
273         RegionsChanged ();
274 }
275
276 void
277 Selection::toggle (MidiRegionView* mrv)
278 {
279         MidiRegionSelection::iterator i;
280
281         if ((i = find (midi_regions.begin(), midi_regions.end(), mrv)) == midi_regions.end()) {
282                 add (mrv);
283         } else {
284                 midi_regions.erase (i);
285         }
286
287         MidiRegionsChanged ();
288 }
289
290 void
291 Selection::toggle (vector<RegionView*>& r)
292 {
293         RegionSelection::iterator i;
294
295         for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
296                 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
297                         add ((*x));
298                 } else {
299                         remove (*x);
300                 }
301         }
302
303         RegionsChanged ();
304 }
305
306 long
307 Selection::toggle (nframes_t start, nframes_t end)
308 {
309         AudioRangeComparator cmp;
310
311         /* XXX this implementation is incorrect */
312
313         time.push_back (AudioRange (start, end, next_time_id++));
314         time.consolidate ();
315         time.sort (cmp);
316
317         TimeChanged ();
318
319         return next_time_id - 1;
320 }
321
322 void
323 Selection::add (boost::shared_ptr<Playlist> pl)
324 {
325         if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
326                 pl->use ();
327                 playlists.push_back(pl);
328                 PlaylistsChanged ();
329         }
330 }
331
332 void
333 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
334 {
335         bool changed = false;
336
337         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
338                 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
339                         (*i)->use ();
340                         playlists.push_back (*i);
341                         changed = true;
342                 }
343         }
344
345         if (changed) {
346                 PlaylistsChanged ();
347         }
348 }
349
350 void
351 Selection::add (const TrackViewList& track_list)
352 {
353         TrackViewList added = tracks.add (track_list);
354
355         if (!added.empty()) {
356                 TracksChanged ();
357         }
358 }
359
360 void
361 Selection::add (TimeAxisView* track)
362 {
363         TrackViewList tr;
364         tr.push_back (track);
365         add (tr);
366 }
367
368 void
369 Selection::add (const MidiNoteSelection& midi_list)
370 {
371         const MidiNoteSelection::const_iterator b = midi_list.begin();
372         const MidiNoteSelection::const_iterator e = midi_list.end();
373
374         if (!midi_list.empty()) {
375                 midi_notes.insert (midi_notes.end(), b, e);
376                 MidiNotesChanged ();
377         }
378 }
379
380 void
381 Selection::add (MidiCutBuffer* midi)
382 {
383         /* we take ownership of the MCB */
384
385         if (find (midi_notes.begin(), midi_notes.end(), midi) == midi_notes.end()) {
386                 midi_notes.push_back (midi);
387                 MidiNotesChanged ();
388         }
389 }
390
391 void
392 Selection::add (vector<RegionView*>& v)
393 {
394         /* XXX This method or the add (const RegionSelection&) needs to go
395          */
396
397         bool changed = false;
398
399         for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
400                 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
401                         changed = regions.add ((*i));
402                         if (Config->get_link_region_and_track_selection() && changed) {
403                                 add (&(*i)->get_trackview());
404                         }
405                 }
406         }
407
408         if (changed) {
409                 RegionsChanged ();
410         }
411 }
412
413 void
414 Selection::add (const RegionSelection& rs)
415 {
416         /* XXX This method or the add (const vector<RegionView*>&) needs to go
417          */
418
419         bool changed = false;
420
421         for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
422                 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
423                         changed = regions.add ((*i));
424                         if (Config->get_link_region_and_track_selection() && changed) {
425                                 add (&(*i)->get_trackview());
426                         }
427                 }
428         }
429
430         if (changed) {
431                 RegionsChanged ();
432         }
433 }
434
435 void
436 Selection::add (RegionView* r)
437 {
438         if (find (regions.begin(), regions.end(), r) == regions.end()) {
439                 regions.add (r);
440                 if (Config->get_link_region_and_track_selection()) {
441                         add (&r->get_trackview());
442                 }
443                 RegionsChanged ();
444         }
445 }
446
447 void
448 Selection::add (MidiRegionView* mrv)
449 {
450         if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
451                 midi_regions.push_back (mrv);
452                 /* XXX should we do this? */
453 #if 0
454                 if (Config->get_link_region_and_track_selection()) {
455                         add (&mrv->get_trackview());
456                 }
457 #endif
458                 MidiRegionsChanged ();
459         }
460 }
461
462 long
463 Selection::add (nframes_t start, nframes_t end)
464 {
465         AudioRangeComparator cmp;
466
467         /* XXX this implementation is incorrect */
468
469         time.push_back (AudioRange (start, end, next_time_id++));
470         time.consolidate ();
471         time.sort (cmp);
472
473         TimeChanged ();
474
475         return next_time_id - 1;
476 }
477
478 void
479 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
480 {
481         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
482                 if ((*i).id == sid) {
483                         time.erase (i);
484                         time.push_back (AudioRange(start,end, sid));
485
486                         /* don't consolidate here */
487
488
489                         AudioRangeComparator cmp;
490                         time.sort (cmp);
491
492                         TimeChanged ();
493                         break;
494                 }
495         }
496 }
497
498 void
499 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
500 {
501         boost::shared_ptr<ARDOUR::AutomationList> al
502                 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
503         if (!al) {
504                 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
505                 return;
506                 return;
507         }
508         if (find (lines.begin(), lines.end(), al) == lines.end()) {
509                 lines.push_back (al);
510                 LinesChanged();
511         }
512 }
513
514 void
515 Selection::remove (TimeAxisView* track)
516 {
517         list<TimeAxisView*>::iterator i;
518         if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
519                 tracks.erase (i);
520                 TracksChanged();
521         }
522 }
523
524 void
525 Selection::remove (const TrackViewList& track_list)
526 {
527         bool changed = false;
528
529         for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
530
531                 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
532                 if (x != tracks.end()) {
533                         tracks.erase (x);
534                         changed = true;
535                 }
536         }
537
538         if (changed) {
539                 TracksChanged();
540         }
541 }
542
543 void
544 Selection::remove (const MidiNoteSelection& midi_list)
545 {
546         bool changed = false;
547
548         for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
549
550                 MidiNoteSelection::iterator x;
551
552                 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
553                         midi_notes.erase (x);
554                         changed = true;
555                 }
556         }
557
558         if (changed) {
559                 MidiNotesChanged();
560         }
561 }
562
563 void
564 Selection::remove (MidiCutBuffer* midi)
565 {
566         MidiNoteSelection::iterator x;
567
568         if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
569                 /* remember that we own the MCB */
570                 delete *x;
571                 midi_notes.erase (x);
572                 MidiNotesChanged ();
573         }
574 }
575
576 void
577 Selection::remove (boost::shared_ptr<Playlist> track)
578 {
579         list<boost::shared_ptr<Playlist> >::iterator i;
580         if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
581                 playlists.erase (i);
582                 PlaylistsChanged();
583         }
584 }
585
586 void
587 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
588 {
589         bool changed = false;
590
591         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
592
593                 list<boost::shared_ptr<Playlist> >::iterator x;
594
595                 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
596                         playlists.erase (x);
597                         changed = true;
598                 }
599         }
600
601         if (changed) {
602                 PlaylistsChanged();
603         }
604 }
605
606 void
607 Selection::remove (RegionView* r)
608 {
609         if (regions.remove (r)) {
610                 RegionsChanged ();
611         }
612
613         if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
614                 remove (&r->get_trackview());
615         }
616 }
617
618 void
619 Selection::remove (MidiRegionView* mrv)
620 {
621         MidiRegionSelection::iterator x;
622
623         if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
624                 midi_regions.erase (x);
625                 MidiRegionsChanged ();
626         }
627
628 #if 0
629         /* XXX fix this up ? */
630         if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
631                 remove (&r->get_trackview());
632         }
633 #endif
634 }
635
636
637 void
638 Selection::remove (uint32_t selection_id)
639 {
640         if (time.empty()) {
641                 return;
642         }
643
644         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
645                 if ((*i).id == selection_id) {
646                         time.erase (i);
647
648                         TimeChanged ();
649                         break;
650                 }
651         }
652 }
653
654 void
655 Selection::remove (nframes_t /*start*/, nframes_t /*end*/)
656 {
657 }
658
659 void
660 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
661 {
662         AutomationSelection::iterator i;
663         if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
664                 lines.erase (i);
665                 LinesChanged();
666         }
667 }
668
669 void
670 Selection::set (TimeAxisView* track)
671 {
672         clear_tracks ();
673         add (track);
674 }
675
676 void
677 Selection::set (const TrackViewList& track_list)
678 {
679         clear_tracks ();
680         add (track_list);
681 }
682
683 void
684 Selection::set (const MidiNoteSelection& midi_list)
685 {
686         clear_midi_notes ();
687         add (midi_list);
688 }
689
690 void
691 Selection::set (boost::shared_ptr<Playlist> playlist)
692 {
693         clear_playlists ();
694         add (playlist);
695 }
696
697 void
698 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
699 {
700         clear_playlists ();
701         add (pllist);
702 }
703
704 void
705 Selection::set (const RegionSelection& rs)
706 {
707         clear_regions();
708         regions = rs;
709         RegionsChanged(); /* EMIT SIGNAL */
710 }
711
712 void
713 Selection::set (MidiRegionView* mrv)
714 {
715         clear_midi_regions ();
716         add (mrv);
717 }
718
719 void
720 Selection::set (RegionView* r, bool also_clear_tracks)
721 {
722         clear_regions ();
723         if (also_clear_tracks) {
724                 clear_tracks ();
725         }
726         add (r);
727 }
728
729 void
730 Selection::set (vector<RegionView*>& v)
731 {
732         clear_regions ();
733         if (Config->get_link_region_and_track_selection()) {
734                 clear_tracks ();
735                 // make sure to deselect any automation selections
736                 clear_points();
737         }
738         add (v);
739 }
740
741 /** Set the start and end time of the time selection, without changing
742  *  the list of tracks it applies to.
743  */
744 long
745 Selection::set (nframes_t start, nframes_t end)
746 {
747         if ((start == 0 && end == 0) || end < start) {
748                 return 0;
749         }
750
751         if (time.empty()) {
752                 time.push_back (AudioRange (start, end, next_time_id++));
753         } else {
754                 /* reuse the first entry, and remove all the rest */
755
756                 while (time.size() > 1) {
757                         time.pop_front();
758                 }
759                 time.front().start = start;
760                 time.front().end = end;
761         }
762
763         time.consolidate ();
764
765         TimeChanged ();
766
767         return time.front().id;
768 }
769
770 void
771 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
772 {
773         lines.clear();
774         add (ac);
775 }
776
777 bool
778 Selection::selected (Marker* m)
779 {
780         return find (markers.begin(), markers.end(), m) != markers.end();
781 }
782
783 bool
784 Selection::selected (TimeAxisView* tv)
785 {
786         return find (tracks.begin(), tracks.end(), tv) != tracks.end();
787 }
788
789 bool
790 Selection::selected (RegionView* rv)
791 {
792         return find (regions.begin(), regions.end(), rv) != regions.end();
793 }
794
795 bool
796 Selection::empty (bool internal_selection)
797 {
798         bool object_level_empty =  regions.empty () &&
799                 tracks.empty () &&
800                 points.empty () &&
801                 playlists.empty () &&
802                 lines.empty () &&
803                 time.empty () &&
804                 playlists.empty () &&
805                 markers.empty() &&
806                 midi_regions.empty()
807                 ;
808
809         if (!internal_selection) {
810                 return object_level_empty;
811         }
812
813         /* this is intended to really only apply when using a Selection
814            as a cut buffer.
815         */
816
817         return object_level_empty && midi_notes.empty();
818 }
819
820 void
821 Selection::toggle (ControlPoint* cp)
822 {
823         cp->set_selected (!cp->get_selected ());
824         set_point_selection_from_line (cp->line ());
825 }
826
827 void
828 Selection::toggle (vector<ControlPoint*> const & cps)
829 {
830         for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
831                 (*i)->set_selected (!(*i)->get_selected ());
832         }
833
834         set_point_selection_from_line (cps.front()->line ());
835 }
836
837 void
838 Selection::toggle (list<Selectable*> const & selectables)
839 {
840         RegionView* rv;
841         ControlPoint* cp;
842         vector<RegionView*> rvs;
843         vector<ControlPoint*> cps;
844
845         for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
846                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
847                         rvs.push_back (rv);
848                 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
849                         cps.push_back (cp);
850                 } else {
851                         fatal << _("programming error: ")
852                               << X_("unknown selectable type passed to Selection::toggle()")
853                               << endmsg;
854                         /*NOTREACHED*/
855                 }
856         }
857
858         if (!rvs.empty()) {
859                 toggle (rvs);
860         }
861
862         if (!cps.empty()) {
863                 toggle (cps);
864         }
865 }
866
867 void
868 Selection::set (list<Selectable*> const & selectables)
869 {
870         clear_regions();
871         clear_points ();
872         add (selectables);
873 }
874
875
876 void
877 Selection::add (list<Selectable*> const & selectables)
878 {
879         RegionView* rv;
880         ControlPoint* cp;
881         vector<RegionView*> rvs;
882         vector<ControlPoint*> cps;
883
884         for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
885                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
886                         rvs.push_back (rv);
887                 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
888                         cps.push_back (cp);
889                 } else {
890                         fatal << _("programming error: ")
891                               << X_("unknown selectable type passed to Selection::add()")
892                               << endmsg;
893                         /*NOTREACHED*/
894                 }
895         }
896
897         if (!rvs.empty()) {
898                 add (rvs);
899         }
900
901         if (!cps.empty()) {
902                 add (cps);
903         }
904 }
905
906 void
907 Selection::clear_points ()
908 {
909         if (!points.empty()) {
910                 points.clear ();
911                 PointsChanged ();
912         }
913 }
914
915 void
916 Selection::add (ControlPoint* cp)
917 {
918         cp->set_selected (true);
919         set_point_selection_from_line (cp->line ());
920 }
921
922 void
923 Selection::add (vector<ControlPoint*> const & cps)
924 {
925         for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
926                 (*i)->set_selected (true);
927         }
928
929         set_point_selection_from_line (cps.front()->line ());
930 }
931
932 void
933 Selection::set (ControlPoint* cp)
934 {
935         if (cp->get_selected()) {
936                 return;
937         }
938
939         /* We're going to set up the PointSelection from the selected ControlPoints
940            on this point's line, so we need to deselect all ControlPoints before
941            we re-add this one.
942         */
943
944         for (uint32_t i = 0; i < cp->line().npoints(); ++i) {
945                 cp->line().nth (i)->set_selected (false);
946         }
947
948         vector<ControlPoint*> cps;
949         cps.push_back (cp);
950         add (cps);
951 }
952
953 void
954 Selection::set (Marker* m)
955 {
956         clear_markers ();
957         add (m);
958 }
959
960 void
961 Selection::toggle (Marker* m)
962 {
963         MarkerSelection::iterator i;
964
965         if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
966                 add (m);
967         } else {
968                 remove (m);
969         }
970 }
971
972 void
973 Selection::remove (Marker* m)
974 {
975         MarkerSelection::iterator i;
976
977         if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
978                 markers.erase (i);
979                 MarkersChanged();
980         }
981 }
982
983 void
984 Selection::add (Marker* m)
985 {
986         if (find (markers.begin(), markers.end(), m) == markers.end()) {
987                 markers.push_back (m);
988                 MarkersChanged();
989         }
990 }
991
992 void
993 Selection::add (const list<Marker*>& m)
994 {
995         markers.insert (markers.end(), m.begin(), m.end());
996         MarkersChanged ();
997 }
998
999 void
1000 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
1001 {
1002         s = max_frames;
1003         e = 0;
1004
1005         for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
1006
1007                 if ((*i)->position() < s) {
1008                         s = (*i)->position();
1009                 }
1010
1011                 if ((*i)->position() > e) {
1012                         e = (*i)->position();
1013                 }
1014         }
1015
1016         s = std::min (s, e);
1017         e = std::max (s, e);
1018 }
1019
1020 /** Automation control point selection is mostly manipulated using the selected state
1021  *  of the ControlPoints themselves.  For example, to add a point to a selection, its
1022  *  ControlPoint is marked as selected and then this method is called.  It sets up
1023  *  our PointSelection from the selected ControlPoints of a given AutomationLine.
1024  *
1025  *  We can't use ControlPoints directly in the selection, as we need to express a
1026  *  selection of not just a visible ControlPoint but also (possibly) some invisible
1027  *  points nearby.  Hence the selection stores AutomationRanges, and these are synced
1028  *  with ControlPoint selection state using AutomationLine::set_selected_points.
1029  */
1030
1031 void
1032 Selection::set_point_selection_from_line (AutomationLine const & line)
1033 {
1034         points.clear ();
1035         
1036         AutomationRange current (DBL_MAX, 0, 1, 0, &line.trackview);
1037
1038         for (uint32_t i = 0; i < line.npoints(); ++i) {
1039                 ControlPoint const * cp = line.nth (i);
1040
1041                 if (cp->get_selected()) {
1042                         /* x and y position of this control point in coordinates suitable for
1043                            an AutomationRange (ie model time and fraction of track height)
1044                         */
1045                         double const x = (*(cp->model()))->when;
1046                         double const y = 1 - (cp->get_y() / line.trackview.current_height ());
1047
1048                         /* work out the position of a rectangle the size of a control point centred
1049                            on this point
1050                         */
1051
1052                         double const size = cp->size ();
1053                         double const x_size = line.time_converter().from (line.trackview.editor().pixel_to_frame (size));
1054                         double const y_size = size / line.trackview.current_height ();
1055                         
1056                         double const x1 = x - x_size / 2;
1057                         double const x2 = x + x_size / 2;
1058                         double const y1 = y - y_size / 2;
1059                         double const y2 = y + y_size / 2;
1060
1061                         /* extend the current AutomationRange to put this point in */
1062                         current.start = min (current.start, x1);
1063                         current.end = max (current.end, x2);
1064                         current.low_fract = min (current.low_fract, y1);
1065                         current.high_fract = max (current.high_fract, y2);
1066
1067                 } else {
1068                         /* this point isn't selected; if the current AutomationRange has some
1069                            stuff in it, push it onto the list and make a new one
1070                         */
1071                         if (current.start < DBL_MAX) {
1072                                 points.push_back (current);
1073                                 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1074                         }
1075                 }
1076         }
1077
1078         /* Maybe push the current AutomationRange, as above */
1079         if (current.start < DBL_MAX) {
1080                 points.push_back (current);
1081                 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1082         }
1083
1084         PointsChanged (); /* EMIT SIGNAL */
1085 }
1086
1087 XMLNode&
1088 Selection::get_state () const
1089 {
1090         /* XXX: not complete; just sufficient to get track selection state
1091            so that re-opening plugin windows for editor mixer strips works
1092         */
1093         
1094         XMLNode* node = new XMLNode (X_("Selection"));
1095
1096         for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
1097                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
1098                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1099                 if (rtv) {
1100                         XMLNode* t = node->add_child (X_("RouteView"));
1101                         t->add_property (X_("id"), atoi (rtv->route()->id().to_s().c_str()));
1102                 } else if (atv) {
1103                         XMLNode* t = node->add_child (X_("AutomationView"));
1104                         t->add_property (X_("id"), atoi (atv->parent_route()->id().to_s().c_str()));
1105                         t->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->control()->parameter ()));
1106                 }
1107         }
1108
1109         return *node;
1110 }
1111
1112 int
1113 Selection::set_state (XMLNode const & node, int)
1114 {
1115         if (node.name() != X_("Selection")) {
1116                 return -1;
1117         }
1118         
1119         XMLNodeList children = node.children ();
1120         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1121                 if ((*i)->name() == X_("RouteView")) {
1122                         
1123                         XMLProperty* prop_id = (*i)->property (X_("id"));
1124                         assert (prop_id);
1125                         PBD::ID id (prop_id->value ());
1126                         RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1127                         assert (rtv);
1128                         add (rtv);
1129                         
1130                 } else if ((*i)->name() == X_("AutomationView")) {
1131                         
1132                         XMLProperty* prop_id = (*i)->property (X_("id"));
1133                         XMLProperty* prop_parameter = (*i)->property (X_("parameter"));
1134
1135                         assert (prop_id);
1136                         assert (prop_parameter);
1137
1138                         PBD::ID id (prop_id->value ());
1139                         RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1140                         assert (rtv);
1141                         
1142                         boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().new_parameter (prop_parameter->value ()));
1143                         
1144                         /* the automation could be for an entity that was never saved
1145                            in the session file. Don't freak out if we can't find
1146                            it.
1147                         */
1148
1149                         if (atv) {
1150                                 add (atv.get());
1151                         }
1152                 }
1153         }
1154
1155         return 0;
1156 }