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