open un-writable sessions without complaining, and desensitize all/most actions that...
[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
27 #include "region_view.h"
28 #include "selection.h"
29 #include "selection_templates.h"
30 #include "time_axis_view.h"
31 #include "automation_time_axis.h"
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace PBD;
37 using namespace sigc;
38
39 struct AudioRangeComparator {
40     bool operator()(AudioRange a, AudioRange b) {
41             return a.start < b.start;
42     }
43 };
44
45 Selection&
46 Selection::operator= (const Selection& other)
47 {
48         if (&other != this) {
49                 regions = other.regions;
50                 tracks = other.tracks;
51                 time = other.time;
52                 lines = other.lines;
53         }
54         return *this;
55 }
56
57 bool
58 operator== (const Selection& a, const Selection& b)
59 {
60         return a.regions == b.regions &&
61                 a.tracks == b.tracks &&
62                 a.time.track == b.time.track &&
63                 a.time.group == b.time.group && 
64                 a.time == b.time &&
65                 a.lines == b.lines &&
66                 a.playlists == b.playlists &&
67                 a.redirects == b.redirects;
68 }
69
70 void
71 Selection::clear ()
72 {
73         clear_tracks ();
74         clear_regions ();
75         clear_points ();
76         clear_lines();
77         clear_time ();
78         clear_playlists ();
79         clear_redirects ();
80 }
81
82 void
83 Selection::dump_region_layers()
84 {
85         cerr << "region selection layer dump" << endl;
86         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
87                 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
88         }
89 }
90
91
92 void
93 Selection::clear_redirects ()
94 {
95         if (!redirects.empty()) {
96                 redirects.clear ();
97                 RedirectsChanged ();
98         }
99 }
100
101 void
102 Selection::clear_regions ()
103 {
104         if (!regions.empty()) {
105                 regions.clear_all ();
106                 RegionsChanged();
107         }
108 }
109
110 void
111 Selection::clear_tracks ()
112 {
113         if (!tracks.empty()) {
114                 tracks.clear ();
115                 TracksChanged();
116         }
117 }
118
119 void
120 Selection::clear_time ()
121 {
122         time.track = 0;
123         time.group = 0;
124         time.clear();
125
126         TimeChanged ();
127 }
128
129 void
130 Selection::clear_playlists ()
131 {
132         /* Selections own their playlists */
133
134         for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
135                 /* selections own their own regions, which are copies of the "originals". make them go away */
136                 (*i)->drop_regions ();
137                 (*i)->release ();
138         }
139
140         if (!playlists.empty()) {
141                 playlists.clear ();
142                 PlaylistsChanged();
143         }
144 }
145
146 void
147 Selection::clear_lines ()
148 {
149         if (!lines.empty()) {
150                 lines.clear ();
151                 LinesChanged();
152         }
153 }
154
155 void
156 Selection::clear_markers ()
157 {
158         if (!markers.empty()) {
159                 markers.clear ();
160                 MarkersChanged();
161         }
162 }
163
164 void
165 Selection::toggle (boost::shared_ptr<Redirect> r)
166 {
167         RedirectSelection::iterator i;
168
169         if ((i = find (redirects.begin(), redirects.end(), r)) == redirects.end()) {
170                 redirects.push_back (r);
171         } else {
172                 redirects.erase (i);
173         }
174         RedirectsChanged();
175
176 }
177
178 void
179 Selection::toggle (boost::shared_ptr<Playlist> pl)
180 {
181         PlaylistSelection::iterator i;
182
183         if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
184                 pl->use ();
185                 playlists.push_back(pl);
186         } else {
187                 playlists.erase (i);
188         }
189
190         PlaylistsChanged ();
191 }
192
193 void
194 Selection::toggle (const list<TimeAxisView*>& track_list)
195 {
196         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
197                 toggle ( (*i) );
198         }
199 }
200
201 void
202 Selection::toggle (TimeAxisView* track)
203 {
204         TrackSelection::iterator i;
205         
206         if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
207                 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
208                 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
209                 tracks.push_back (track);
210         } else {
211                 tracks.erase (i);
212         }
213
214         TracksChanged();
215 }
216
217 void
218 Selection::toggle (RegionView* r)
219 {
220         RegionSelection::iterator i;
221
222         if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
223                 add (r);
224         } else {
225                 remove (*i);
226         }
227
228         RegionsChanged ();
229 }
230
231 void
232 Selection::toggle (vector<RegionView*>& r)
233 {
234         RegionSelection::iterator i;
235
236         for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
237                 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
238                         add ((*x));
239                 } else {
240                         remove (*x);
241                 }
242         }
243
244         RegionsChanged ();
245 }
246
247 long
248 Selection::toggle (nframes_t start, nframes_t end)
249 {
250         AudioRangeComparator cmp;
251
252         /* XXX this implementation is incorrect */
253
254         time.push_back (AudioRange (start, end, next_time_id++));
255         time.consolidate ();
256         time.sort (cmp);
257         
258         TimeChanged ();
259
260         return next_time_id - 1;
261 }
262
263
264 void
265 Selection::add (boost::shared_ptr<Redirect> r)
266 {
267         if (find (redirects.begin(), redirects.end(), r) == redirects.end()) {
268                 redirects.push_back (r);
269                 RedirectsChanged();
270         }
271 }
272
273 void
274 Selection::add (boost::shared_ptr<Playlist> pl)
275 {
276         if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
277                 pl->use ();
278                 playlists.push_back(pl);
279                 PlaylistsChanged ();
280         }
281 }
282
283 void
284 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
285 {
286         bool changed = false;
287
288         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
289                 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
290                         (*i)->use ();
291                         playlists.push_back (*i);
292                         changed = true;
293                 }
294         }
295         
296         if (changed) {
297                 PlaylistsChanged ();
298         }
299 }
300
301 void
302 Selection::add (const list<TimeAxisView*>& track_list)
303 {
304         bool changed = false;
305
306         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
307                 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
308                         void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
309                         (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
310                         tracks.push_back (*i);
311                         changed = true;
312                 }
313         }
314         
315         if (changed) {
316                 TracksChanged ();
317         }
318 }
319
320 void
321 Selection::add (TimeAxisView* track)
322 {
323         if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
324                 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
325                 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
326                 tracks.push_back (track);
327                 TracksChanged();
328         }
329 }
330
331 void
332 Selection::add (vector<RegionView*>& v)
333 {
334         /* XXX This method or the add (const RegionSelection&) needs to go
335          */
336
337         bool changed = false;
338         
339         for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
340                 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
341                         changed = regions.add ((*i));
342                         if (Config->get_link_region_and_track_selection() && changed) {
343                                 add (&(*i)->get_trackview());
344                         }
345                 }
346         }
347
348         if (changed) {
349                 RegionsChanged ();
350         }
351 }
352
353 void
354 Selection::add (const RegionSelection& rs)
355 {
356         /* XXX This method or the add (const vector<RegionView*>&) needs to go
357          */
358
359         bool changed = false;
360         
361         for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
362                 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
363                         changed = regions.add ((*i));
364                         if (Config->get_link_region_and_track_selection() && changed) {
365                                 add (&(*i)->get_trackview());
366                         }
367                 }
368         }
369         
370         if (changed) {
371                 RegionsChanged ();
372         }
373 }
374
375 void
376 Selection::add (RegionView* r)
377 {
378         if (find (regions.begin(), regions.end(), r) == regions.end()) {
379                 regions.add (r);
380                 if (Config->get_link_region_and_track_selection()) {
381                         add (&r->get_trackview());
382                 }
383                 RegionsChanged ();
384         }
385 }
386
387 long
388 Selection::add (nframes_t start, nframes_t end)
389 {
390         AudioRangeComparator cmp;
391
392         /* XXX this implementation is incorrect */
393
394         time.push_back (AudioRange (start, end, next_time_id++));
395         time.consolidate ();
396         time.sort (cmp);
397         
398         TimeChanged ();
399
400         return next_time_id - 1;
401 }
402
403 void
404 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
405 {
406         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
407                 if ((*i).id == sid) {
408                         time.erase (i);
409                         time.push_back (AudioRange(start,end, sid));
410
411                         /* don't consolidate here */
412
413
414                         AudioRangeComparator cmp;
415                         time.sort (cmp);
416
417                         TimeChanged ();
418                         break;
419                 }
420         }
421 }
422
423 void
424 Selection::add (AutomationList* ac)
425 {
426         if (find (lines.begin(), lines.end(), ac) == lines.end()) {
427                 lines.push_back (ac);
428                 LinesChanged();
429         }
430 }
431
432 void
433 Selection::remove (boost::shared_ptr<Redirect> r)
434 {
435         RedirectSelection::iterator i;
436         if ((i = find (redirects.begin(), redirects.end(), r)) != redirects.end()) {
437                 redirects.erase (i);
438                 RedirectsChanged ();
439         }
440 }
441
442 void
443 Selection::remove (TimeAxisView* track)
444 {
445         list<TimeAxisView*>::iterator i;
446         if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
447                 tracks.erase (i);
448                 TracksChanged();
449         }
450 }
451
452 void
453 Selection::remove (const list<TimeAxisView*>& track_list)
454 {
455         bool changed = false;
456
457         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
458
459                 list<TimeAxisView*>::iterator x;
460
461                 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
462                         tracks.erase (x);
463                         changed = true;
464                 }
465         }
466
467         if (changed) {
468                 TracksChanged();
469         }
470 }
471
472 void
473 Selection::remove (boost::shared_ptr<Playlist> track)
474 {
475         list<boost::shared_ptr<Playlist> >::iterator i;
476         if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
477                 playlists.erase (i);
478                 PlaylistsChanged();
479         }
480 }
481
482 void
483 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
484 {
485         bool changed = false;
486
487         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
488
489                 list<boost::shared_ptr<Playlist> >::iterator x;
490
491                 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
492                         playlists.erase (x);
493                         changed = true;
494                 }
495         }
496
497         if (changed) {
498                 PlaylistsChanged();
499         }
500 }
501
502 void
503 Selection::remove (RegionView* r)
504 {
505         if (regions.remove (r)) {
506                 RegionsChanged ();
507         }
508
509         if (Config->get_link_region_and_track_selection() && !regions.involves (r->get_trackview())) {
510                 remove (&r->get_trackview());
511         }
512 }
513
514
515 void
516 Selection::remove (uint32_t selection_id)
517 {
518         if (time.empty()) {
519                 return;
520         }
521
522         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
523                 if ((*i).id == selection_id) {
524                         time.erase (i);
525                                                 
526                         TimeChanged ();
527                         break;
528                 }
529         }
530 }
531
532 void
533 Selection::remove (nframes_t start, nframes_t end)
534 {
535 }
536
537 void
538 Selection::remove (AutomationList *ac)
539 {
540         list<AutomationList*>::iterator i;
541         if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
542                 lines.erase (i);
543                 LinesChanged();
544         }
545 }
546
547 void
548 Selection::set (boost::shared_ptr<Redirect> r)
549 {
550         clear_redirects ();
551         add (r);
552 }
553
554 void
555 Selection::set (TimeAxisView* track)
556 {
557         clear_tracks ();
558         add (track);
559 }
560
561 void
562 Selection::set (const list<TimeAxisView*>& track_list)
563 {
564         clear_tracks ();
565         add (track_list);
566 }
567
568 void
569 Selection::set (boost::shared_ptr<Playlist> playlist)
570 {
571         clear_playlists ();
572         add (playlist);
573 }
574
575 void
576 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
577 {
578         clear_playlists ();
579         add (pllist);
580 }
581
582 void
583 Selection::set (const RegionSelection& rs)
584 {
585         clear_regions();
586         regions = rs;
587         RegionsChanged(); /* EMIT SIGNAL */
588 }
589
590 void
591 Selection::set (RegionView* r, bool also_clear_tracks)
592 {
593         clear_regions ();
594         if (also_clear_tracks) {
595                 clear_tracks ();
596         }
597         add (r);
598 }
599
600 void
601 Selection::set (vector<RegionView*>& v)
602 {
603         clear_regions ();
604         if (Config->get_link_region_and_track_selection()) {
605                 clear_tracks ();
606                 // make sure to deselect any automation selections
607                 clear_points();
608         }
609         add (v);
610 }
611
612 long
613 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
614 {
615         if ((start == 0 && end == 0) || end < start) {
616                 return 0;
617         }
618
619         if (time.empty()) {
620                 time.push_back (AudioRange (start, end, next_time_id++));
621         } else {
622                 /* reuse the first entry, and remove all the rest */
623
624                 while (time.size() > 1) {
625                         time.pop_front();
626                 }
627                 time.front().start = start;
628                 time.front().end = end;
629         }
630
631         if (track) {
632                 time.track = track;
633                 time.group = track->edit_group();
634         } else {
635                 time.track = 0;
636                 time.group = 0;
637         }
638
639         time.consolidate ();
640
641         TimeChanged ();
642
643         return time.front().id;
644 }
645
646 void
647 Selection::set (AutomationList *ac)
648 {
649         lines.clear();
650         add (ac);
651 }
652
653 bool
654 Selection::selected (Marker* m)
655 {
656         return find (markers.begin(), markers.end(), m) != markers.end();
657 }
658
659 bool
660 Selection::selected (TimeAxisView* tv)
661 {
662         return find (tracks.begin(), tracks.end(), tv) != tracks.end();
663 }
664
665 bool
666 Selection::selected (RegionView* rv)
667 {
668         return find (regions.begin(), regions.end(), rv) != regions.end();
669 }
670
671 bool
672 Selection::empty ()
673 {
674         return regions.empty () &&
675                 tracks.empty () &&
676                 points.empty () && 
677                 playlists.empty () && 
678                 lines.empty () &&
679                 time.empty () &&
680                 playlists.empty () &&
681                 redirects.empty () &&
682                 markers.empty()
683                 ;
684 }
685
686 void
687 Selection::toggle (const vector<AutomationSelectable*>& autos)
688 {
689         for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
690                 if ((*x)->get_selected()) {
691                         points.remove (**x);
692                 } else {
693                         points.push_back (**x);
694                 }
695
696                 delete *x;
697         }
698
699         PointsChanged (); /* EMIT SIGNAL */
700 }
701
702 void
703 Selection::toggle (list<Selectable*>& selectables)
704 {
705         RegionView* rv;
706         AutomationSelectable* as;
707         vector<RegionView*> rvs;
708         vector<AutomationSelectable*> autos;
709
710         for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
711                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
712                         rvs.push_back (rv);
713                 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
714                         autos.push_back (as);
715                 } else {
716                         fatal << _("programming error: ")
717                               << X_("unknown selectable type passed to Selection::toggle()")
718                               << endmsg;
719                         /*NOTREACHED*/
720                 }
721         }
722
723         if (!rvs.empty()) {
724                 toggle (rvs);
725         } 
726
727         if (!autos.empty()) {
728                 toggle (autos);
729         } 
730 }
731
732 void
733 Selection::set (list<Selectable*>& selectables)
734 {
735         clear_regions();
736         clear_points ();
737         add (selectables);
738 }
739
740
741 void
742 Selection::add (list<Selectable*>& selectables)
743 {
744         RegionView* rv;
745         AutomationSelectable* as;
746         vector<RegionView*> rvs;
747         vector<AutomationSelectable*> autos;
748
749         for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
750                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
751                         rvs.push_back (rv);
752                 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
753                         autos.push_back (as);
754                 } else {
755                         fatal << _("programming error: ")
756                               << X_("unknown selectable type passed to Selection::add()")
757                               << endmsg;
758                         /*NOTREACHED*/
759                 }
760         }
761
762         if (!rvs.empty()) {
763                 add (rvs);
764         } 
765
766         if (!autos.empty()) {
767                 add (autos);
768         } 
769 }
770
771 void
772 Selection::clear_points ()
773 {
774         if (!points.empty()) {
775                 points.clear ();
776                 PointsChanged ();
777         }
778 }
779
780 void
781 Selection::add (vector<AutomationSelectable*>& autos)
782 {
783         for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
784                 points.push_back (**i);
785         }
786
787         PointsChanged ();
788 }
789
790 void
791 Selection::set (Marker* m)
792 {
793         clear_markers ();
794         add (m);
795 }
796
797 void
798 Selection::toggle (Marker* m)
799 {
800         MarkerSelection::iterator i;
801         
802         if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
803                 add (m);
804         } else {
805                 remove (m);
806         }
807 }
808
809 void
810 Selection::remove (Marker* m)
811 {
812         MarkerSelection::iterator i;
813
814         if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
815                 markers.erase (i);
816                 MarkersChanged();
817         }
818 }
819
820 void
821 Selection::add (Marker* m)
822 {
823         if (find (markers.begin(), markers.end(), m) == markers.end()) {
824
825                 /* disambiguate which remove() for the compiler */
826                 
827                 void (Selection::*pmf)(Marker*) = &Selection::remove;
828
829                 m->GoingAway.connect (bind (mem_fun (*this, pmf), m));
830
831                 markers.push_back (m);
832                 MarkersChanged();
833         }
834 }
835
836 void
837 Selection::add (const list<Marker*>& m)
838 {
839         markers.insert (markers.end(), m.begin(), m.end());
840         MarkersChanged ();
841 }
842
843 void
844 MarkerSelection::range (nframes64_t& s, nframes64_t& e)
845 {
846         s = max_frames;
847         e = 0;
848
849         for (MarkerSelection::iterator i = begin(); i != end(); ++i) {
850
851                 if ((*i)->position() < s) {
852                         s = (*i)->position();
853                 } 
854
855                 if ((*i)->position() > e) {
856                         e = (*i)->position();
857                 }
858         }
859
860         s = std::min (s, e);
861         e = std::max (s, e);
862 }