Prevent the point selection straying -ve when control points are positioned close...
[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 (framepos_t start, framepos_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_time_axis_view());
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_time_axis_view());
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                 bool changed = regions.add (r);
440                 if (Config->get_link_region_and_track_selection() && changed) {
441                         add (&r->get_time_axis_view());
442                 }
443                 if (changed) {
444                         RegionsChanged ();
445                 }
446         }
447 }
448
449 void
450 Selection::add (MidiRegionView* mrv)
451 {
452         if (find (midi_regions.begin(), midi_regions.end(), mrv) == midi_regions.end()) {
453                 midi_regions.push_back (mrv);
454                 /* XXX should we do this? */
455 #if 0
456                 if (Config->get_link_region_and_track_selection()) {
457                         add (&mrv->get_time_axis_view());
458                 }
459 #endif
460                 MidiRegionsChanged ();
461         }
462 }
463
464 long
465 Selection::add (framepos_t start, framepos_t end)
466 {
467         AudioRangeComparator cmp;
468
469         /* XXX this implementation is incorrect */
470
471         time.push_back (AudioRange (start, end, next_time_id++));
472         time.consolidate ();
473         time.sort (cmp);
474
475         TimeChanged ();
476
477         return next_time_id - 1;
478 }
479
480 void
481 Selection::replace (uint32_t sid, framepos_t start, framepos_t end)
482 {
483         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
484                 if ((*i).id == sid) {
485                         time.erase (i);
486                         time.push_back (AudioRange(start,end, sid));
487
488                         /* don't consolidate here */
489
490
491                         AudioRangeComparator cmp;
492                         time.sort (cmp);
493
494                         TimeChanged ();
495                         break;
496                 }
497         }
498 }
499
500 void
501 Selection::add (boost::shared_ptr<Evoral::ControlList> cl)
502 {
503         boost::shared_ptr<ARDOUR::AutomationList> al
504                 = boost::dynamic_pointer_cast<ARDOUR::AutomationList>(cl);
505         if (!al) {
506                 warning << "Programming error: Selected list is not an ARDOUR::AutomationList" << endmsg;
507                 return;
508                 return;
509         }
510         if (find (lines.begin(), lines.end(), al) == lines.end()) {
511                 lines.push_back (al);
512                 LinesChanged();
513         }
514 }
515
516 void
517 Selection::remove (TimeAxisView* track)
518 {
519         list<TimeAxisView*>::iterator i;
520         if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
521                 tracks.erase (i);
522                 TracksChanged();
523         }
524 }
525
526 void
527 Selection::remove (const TrackViewList& track_list)
528 {
529         bool changed = false;
530
531         for (TrackViewList::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
532
533                 TrackViewList::iterator x = find (tracks.begin(), tracks.end(), *i);
534                 if (x != tracks.end()) {
535                         tracks.erase (x);
536                         changed = true;
537                 }
538         }
539
540         if (changed) {
541                 TracksChanged();
542         }
543 }
544
545 void
546 Selection::remove (const MidiNoteSelection& midi_list)
547 {
548         bool changed = false;
549
550         for (MidiNoteSelection::const_iterator i = midi_list.begin(); i != midi_list.end(); ++i) {
551
552                 MidiNoteSelection::iterator x;
553
554                 if ((x = find (midi_notes.begin(), midi_notes.end(), (*i))) != midi_notes.end()) {
555                         midi_notes.erase (x);
556                         changed = true;
557                 }
558         }
559
560         if (changed) {
561                 MidiNotesChanged();
562         }
563 }
564
565 void
566 Selection::remove (MidiCutBuffer* midi)
567 {
568         MidiNoteSelection::iterator x;
569
570         if ((x = find (midi_notes.begin(), midi_notes.end(), midi)) != midi_notes.end()) {
571                 /* remember that we own the MCB */
572                 delete *x;
573                 midi_notes.erase (x);
574                 MidiNotesChanged ();
575         }
576 }
577
578 void
579 Selection::remove (boost::shared_ptr<Playlist> track)
580 {
581         list<boost::shared_ptr<Playlist> >::iterator i;
582         if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
583                 playlists.erase (i);
584                 PlaylistsChanged();
585         }
586 }
587
588 void
589 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
590 {
591         bool changed = false;
592
593         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
594
595                 list<boost::shared_ptr<Playlist> >::iterator x;
596
597                 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
598                         playlists.erase (x);
599                         changed = true;
600                 }
601         }
602
603         if (changed) {
604                 PlaylistsChanged();
605         }
606 }
607
608 void
609 Selection::remove (RegionView* r)
610 {
611         if (regions.remove (r)) {
612                 RegionsChanged ();
613         }
614
615         if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_time_axis_view())) {
616                 remove (&r->get_time_axis_view());
617         }
618 }
619
620 void
621 Selection::remove (MidiRegionView* mrv)
622 {
623         MidiRegionSelection::iterator x;
624
625         if ((x = find (midi_regions.begin(), midi_regions.end(), mrv)) != midi_regions.end()) {
626                 midi_regions.erase (x);
627                 MidiRegionsChanged ();
628         }
629
630 #if 0
631         /* XXX fix this up ? */
632         if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_time_axis_view())) {
633                 remove (&r->get_time_axis_view());
634         }
635 #endif
636 }
637
638
639 void
640 Selection::remove (uint32_t selection_id)
641 {
642         if (time.empty()) {
643                 return;
644         }
645
646         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
647                 if ((*i).id == selection_id) {
648                         time.erase (i);
649
650                         TimeChanged ();
651                         break;
652                 }
653         }
654 }
655
656 void
657 Selection::remove (framepos_t /*start*/, framepos_t /*end*/)
658 {
659 }
660
661 void
662 Selection::remove (boost::shared_ptr<ARDOUR::AutomationList> ac)
663 {
664         AutomationSelection::iterator i;
665         if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
666                 lines.erase (i);
667                 LinesChanged();
668         }
669 }
670
671 void
672 Selection::set (TimeAxisView* track)
673 {
674         clear_tracks ();
675         add (track);
676 }
677
678 void
679 Selection::set (const TrackViewList& track_list)
680 {
681         clear_tracks ();
682         add (track_list);
683 }
684
685 void
686 Selection::set (const MidiNoteSelection& midi_list)
687 {
688         clear_midi_notes ();
689         add (midi_list);
690 }
691
692 void
693 Selection::set (boost::shared_ptr<Playlist> playlist)
694 {
695         clear_playlists ();
696         add (playlist);
697 }
698
699 void
700 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
701 {
702         clear_playlists ();
703         add (pllist);
704 }
705
706 void
707 Selection::set (const RegionSelection& rs)
708 {
709         clear_regions();
710         regions = rs;
711         RegionsChanged(); /* EMIT SIGNAL */
712 }
713
714 void
715 Selection::set (MidiRegionView* mrv)
716 {
717         clear_midi_regions ();
718         add (mrv);
719 }
720
721 void
722 Selection::set (RegionView* r, bool also_clear_tracks)
723 {
724         clear_regions ();
725         if (also_clear_tracks) {
726                 clear_tracks ();
727         }
728         add (r);
729 }
730
731 void
732 Selection::set (vector<RegionView*>& v)
733 {
734         clear_regions ();
735         if (Config->get_link_region_and_track_selection()) {
736                 clear_tracks ();
737                 // make sure to deselect any automation selections
738                 clear_points();
739         }
740         add (v);
741 }
742
743 /** Set the start and end time of the time selection, without changing
744  *  the list of tracks it applies to.
745  */
746 long
747 Selection::set (framepos_t start, framepos_t end)
748 {
749         if ((start == 0 && end == 0) || end < start) {
750                 return 0;
751         }
752
753         if (time.empty()) {
754                 time.push_back (AudioRange (start, end, next_time_id++));
755         } else {
756                 /* reuse the first entry, and remove all the rest */
757
758                 while (time.size() > 1) {
759                         time.pop_front();
760                 }
761                 time.front().start = start;
762                 time.front().end = end;
763         }
764
765         time.consolidate ();
766
767         TimeChanged ();
768
769         return time.front().id;
770 }
771
772 /** Set the start and end of the range selection.  If more than one range
773  *  is currently selected, the start of the earliest range and the end of the
774  *  latest range are set.  If no range is currently selected, this method
775  *  selects a single range from start to end.
776  *
777  *  @param start New start time.
778  *  @param end New end time.
779  */
780 void
781 Selection::set_preserving_all_ranges (framepos_t start, framepos_t end)
782 {
783         if ((start == 0 && end == 0) || (end < start)) {
784                 return;
785         }
786
787         if (time.empty ()) {
788                 time.push_back (AudioRange (start, end, next_time_id++));
789         } else {
790                 time.sort (AudioRangeComparator ());
791                 time.front().start = start;
792                 time.back().end = end;
793         }
794
795         time.consolidate ();
796         
797         TimeChanged ();
798 }
799
800 void
801 Selection::set (boost::shared_ptr<Evoral::ControlList> ac)
802 {
803         lines.clear();
804         add (ac);
805 }
806
807 bool
808 Selection::selected (Marker* m)
809 {
810         return find (markers.begin(), markers.end(), m) != markers.end();
811 }
812
813 bool
814 Selection::selected (TimeAxisView* tv)
815 {
816         return find (tracks.begin(), tracks.end(), tv) != tracks.end();
817 }
818
819 bool
820 Selection::selected (RegionView* rv)
821 {
822         return find (regions.begin(), regions.end(), rv) != regions.end();
823 }
824
825 bool
826 Selection::empty (bool internal_selection)
827 {
828         bool object_level_empty =  regions.empty () &&
829                 tracks.empty () &&
830                 points.empty () &&
831                 playlists.empty () &&
832                 lines.empty () &&
833                 time.empty () &&
834                 playlists.empty () &&
835                 markers.empty() &&
836                 midi_regions.empty()
837                 ;
838
839         if (!internal_selection) {
840                 return object_level_empty;
841         }
842
843         /* this is intended to really only apply when using a Selection
844            as a cut buffer.
845         */
846
847         return object_level_empty && midi_notes.empty();
848 }
849
850 void
851 Selection::toggle (ControlPoint* cp)
852 {
853         cp->set_selected (!cp->get_selected ());
854         set_point_selection_from_line (cp->line ());
855 }
856
857 void
858 Selection::toggle (vector<ControlPoint*> const & cps)
859 {
860         for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
861                 (*i)->set_selected (!(*i)->get_selected ());
862         }
863
864         set_point_selection_from_line (cps.front()->line ());
865 }
866
867 void
868 Selection::toggle (list<Selectable*> const & selectables)
869 {
870         RegionView* rv;
871         ControlPoint* cp;
872         vector<RegionView*> rvs;
873         vector<ControlPoint*> cps;
874
875         for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
876                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
877                         rvs.push_back (rv);
878                 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
879                         cps.push_back (cp);
880                 } else {
881                         fatal << _("programming error: ")
882                               << X_("unknown selectable type passed to Selection::toggle()")
883                               << endmsg;
884                         /*NOTREACHED*/
885                 }
886         }
887
888         if (!rvs.empty()) {
889                 toggle (rvs);
890         }
891
892         if (!cps.empty()) {
893                 toggle (cps);
894         }
895 }
896
897 void
898 Selection::set (list<Selectable*> const & selectables)
899 {
900         clear_regions();
901         clear_points ();
902
903         if (Config->get_link_region_and_track_selection ()) {
904                 clear_tracks ();
905         }
906         
907         add (selectables);
908 }
909
910
911 void
912 Selection::add (list<Selectable*> const & selectables)
913 {
914         RegionView* rv;
915         ControlPoint* cp;
916         vector<RegionView*> rvs;
917         vector<ControlPoint*> cps;
918
919         for (std::list<Selectable*>::const_iterator i = selectables.begin(); i != selectables.end(); ++i) {
920                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
921                         rvs.push_back (rv);
922                 } else if ((cp = dynamic_cast<ControlPoint*> (*i)) != 0) {
923                         cps.push_back (cp);
924                 } else {
925                         fatal << _("programming error: ")
926                               << X_("unknown selectable type passed to Selection::add()")
927                               << endmsg;
928                         /*NOTREACHED*/
929                 }
930         }
931
932         if (!rvs.empty()) {
933                 add (rvs);
934         }
935
936         if (!cps.empty()) {
937                 add (cps);
938         }
939 }
940
941 void
942 Selection::clear_points ()
943 {
944         if (!points.empty()) {
945                 points.clear ();
946                 PointsChanged ();
947         }
948 }
949
950 void
951 Selection::add (ControlPoint* cp)
952 {
953         cp->set_selected (true);
954         set_point_selection_from_line (cp->line ());
955 }
956
957 void
958 Selection::add (vector<ControlPoint*> const & cps)
959 {
960         for (vector<ControlPoint*>::const_iterator i = cps.begin(); i != cps.end(); ++i) {
961                 (*i)->set_selected (true);
962         }
963
964         set_point_selection_from_line (cps.front()->line ());
965 }
966
967 void
968 Selection::set (ControlPoint* cp)
969 {
970         if (cp->get_selected()) {
971                 return;
972         }
973
974         /* We're going to set up the PointSelection from the selected ControlPoints
975            on this point's line, so we need to deselect all ControlPoints before
976            we re-add this one.
977         */
978
979         for (uint32_t i = 0; i < cp->line().npoints(); ++i) {
980                 cp->line().nth (i)->set_selected (false);
981         }
982
983         vector<ControlPoint*> cps;
984         cps.push_back (cp);
985         add (cps);
986 }
987
988 void
989 Selection::set (Marker* m)
990 {
991         clear_markers ();
992         add (m);
993 }
994
995 void
996 Selection::toggle (Marker* m)
997 {
998         MarkerSelection::iterator i;
999
1000         if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
1001                 add (m);
1002         } else {
1003                 remove (m);
1004         }
1005 }
1006
1007 void
1008 Selection::remove (Marker* m)
1009 {
1010         MarkerSelection::iterator i;
1011
1012         if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
1013                 markers.erase (i);
1014                 MarkersChanged();
1015         }
1016 }
1017
1018 void
1019 Selection::add (Marker* m)
1020 {
1021         if (find (markers.begin(), markers.end(), m) == markers.end()) {
1022                 markers.push_back (m);
1023                 MarkersChanged();
1024         }
1025 }
1026
1027 void
1028 Selection::add (const list<Marker*>& m)
1029 {
1030         markers.insert (markers.end(), m.begin(), m.end());
1031         MarkersChanged ();
1032 }
1033
1034 void
1035 MarkerSelection::range (framepos_t& s, framepos_t& e)
1036 {
1037         s = max_framepos;
1038         e = 0;
1039
1040         for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
1041
1042                 if ((*i)->position() < s) {
1043                         s = (*i)->position();
1044                 }
1045
1046                 if ((*i)->position() > e) {
1047                         e = (*i)->position();
1048                 }
1049         }
1050
1051         s = std::min (s, e);
1052         e = std::max (s, e);
1053 }
1054
1055 /** Automation control point selection is mostly manipulated using the selected state
1056  *  of the ControlPoints themselves.  For example, to add a point to a selection, its
1057  *  ControlPoint is marked as selected and then this method is called.  It sets up
1058  *  our PointSelection from the selected ControlPoints of a given AutomationLine.
1059  *
1060  *  We can't use ControlPoints directly in the selection, as we need to express a
1061  *  selection of not just a visible ControlPoint but also (possibly) some invisible
1062  *  points nearby.  Hence the selection stores AutomationRanges, and these are synced
1063  *  with ControlPoint selection state using AutomationLine::set_selected_points.
1064  */
1065
1066 void
1067 Selection::set_point_selection_from_line (AutomationLine const & line)
1068 {
1069         points.clear ();
1070         
1071         AutomationRange current (DBL_MAX, 0, 1, 0, &line.trackview);
1072
1073         for (uint32_t i = 0; i < line.npoints(); ++i) {
1074                 ControlPoint const * cp = line.nth (i);
1075
1076                 if (cp->get_selected()) {
1077                         /* x and y position of this control point in coordinates suitable for
1078                            an AutomationRange (ie model time and fraction of track height)
1079                         */
1080                         double const x = (*(cp->model()))->when;
1081                         double const y = 1 - (cp->get_y() / line.trackview.current_height ());
1082
1083                         /* work out the position of a rectangle the size of a control point centred
1084                            on this point
1085                         */
1086
1087                         double const size = cp->size ();
1088                         double const x_size = line.time_converter().from (line.trackview.editor().pixel_to_frame (size));
1089                         double const y_size = size / line.trackview.current_height ();
1090                         
1091                         double const x1 = max (0.0, x - x_size / 2);
1092                         double const x2 = x + x_size / 2;
1093                         double const y1 = max (0.0, y - y_size / 2);
1094                         double const y2 = y + y_size / 2;
1095
1096                         /* extend the current AutomationRange to put this point in */
1097                         current.start = min (current.start, x1);
1098                         current.end = max (current.end, x2);
1099                         current.low_fract = min (current.low_fract, y1);
1100                         current.high_fract = max (current.high_fract, y2);
1101
1102                 } else {
1103                         /* this point isn't selected; if the current AutomationRange has some
1104                            stuff in it, push it onto the list and make a new one
1105                         */
1106                         if (current.start < DBL_MAX) {
1107                                 points.push_back (current);
1108                                 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1109                         }
1110                 }
1111         }
1112
1113         /* Maybe push the current AutomationRange, as above */
1114         if (current.start < DBL_MAX) {
1115                 points.push_back (current);
1116                 current = AutomationRange (DBL_MAX, 0, 1, 0, &line.trackview);
1117         }
1118
1119         PointsChanged (); /* EMIT SIGNAL */
1120 }
1121
1122 XMLNode&
1123 Selection::get_state () const
1124 {
1125         /* XXX: not complete; just sufficient to get track selection state
1126            so that re-opening plugin windows for editor mixer strips works
1127         */
1128         
1129         XMLNode* node = new XMLNode (X_("Selection"));
1130
1131         for (TrackSelection::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
1132                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
1133                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*> (*i);
1134                 if (rtv) {
1135                         XMLNode* t = node->add_child (X_("RouteView"));
1136                         t->add_property (X_("id"), atoi (rtv->route()->id().to_s().c_str()));
1137                 } else if (atv) {
1138                         XMLNode* t = node->add_child (X_("AutomationView"));
1139                         t->add_property (X_("id"), atoi (atv->parent_route()->id().to_s().c_str()));
1140                         t->add_property (X_("parameter"), EventTypeMap::instance().to_symbol (atv->parameter ()));
1141                 }
1142         }
1143
1144         return *node;
1145 }
1146
1147 int
1148 Selection::set_state (XMLNode const & node, int)
1149 {
1150         if (node.name() != X_("Selection")) {
1151                 return -1;
1152         }
1153         
1154         XMLNodeList children = node.children ();
1155         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
1156                 if ((*i)->name() == X_("RouteView")) {
1157                         
1158                         XMLProperty* prop_id = (*i)->property (X_("id"));
1159                         assert (prop_id);
1160                         PBD::ID id (prop_id->value ());
1161                         RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1162                         if (rtv) {
1163                                 add (rtv);
1164                         }
1165                         
1166                 } else if ((*i)->name() == X_("AutomationView")) {
1167                         
1168                         XMLProperty* prop_id = (*i)->property (X_("id"));
1169                         XMLProperty* prop_parameter = (*i)->property (X_("parameter"));
1170
1171                         assert (prop_id);
1172                         assert (prop_parameter);
1173
1174                         PBD::ID id (prop_id->value ());
1175                         RouteTimeAxisView* rtv = editor->get_route_view_by_route_id (id);
1176
1177                         if (rtv) {
1178                                 boost::shared_ptr<AutomationTimeAxisView> atv = rtv->automation_child (EventTypeMap::instance().new_parameter (prop_parameter->value ()));
1179                         
1180                                 /* the automation could be for an entity that was never saved
1181                                    in the session file. Don't freak out if we can't find
1182                                    it.
1183                                 */
1184                                 
1185                                 if (atv) {
1186                                         add (atv.get());
1187                                 }
1188                         }
1189                 }
1190         }
1191
1192         return 0;
1193 }
1194
1195 void
1196 Selection::remove_regions (TimeAxisView* t)
1197 {
1198         RegionSelection::iterator i = regions.begin();
1199         while (i != regions.end ()) {
1200                 RegionSelection::iterator tmp = i;
1201                 ++tmp;
1202
1203                 if (&(*i)->get_time_axis_view() == t) {
1204                         remove (*i);
1205                 }
1206
1207                 i = tmp;
1208         }
1209 }