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