initial round of work to support new edit point option, and removal of edit cursor
[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 (RegionView* r)
333 {
334         if (find (regions.begin(), regions.end(), r) == regions.end()) {
335                 regions.add (r);
336                 add (&r->get_trackview());
337                 RegionsChanged ();
338         }
339 }
340
341 void
342 Selection::add (vector<RegionView*>& v)
343 {
344         bool changed = false;
345
346         for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
347                 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
348                         changed = regions.add ((*i));
349                         if (changed) {
350                                 add (&(*i)->get_trackview());
351                         }
352                 }
353         }
354
355         if (changed) {
356                 RegionsChanged ();
357         }
358 }
359
360 long
361 Selection::add (nframes_t start, nframes_t end)
362 {
363         AudioRangeComparator cmp;
364
365         /* XXX this implementation is incorrect */
366
367         time.push_back (AudioRange (start, end, next_time_id++));
368         time.consolidate ();
369         time.sort (cmp);
370         
371         TimeChanged ();
372
373         return next_time_id - 1;
374 }
375
376 void
377 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
378 {
379         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
380                 if ((*i).id == sid) {
381                         time.erase (i);
382                         time.push_back (AudioRange(start,end, sid));
383
384                         /* don't consolidate here */
385
386
387                         AudioRangeComparator cmp;
388                         time.sort (cmp);
389
390                         TimeChanged ();
391                         break;
392                 }
393         }
394 }
395
396 void
397 Selection::add (AutomationList* ac)
398 {
399         if (find (lines.begin(), lines.end(), ac) == lines.end()) {
400                 lines.push_back (ac);
401                 LinesChanged();
402         }
403 }
404
405 void
406 Selection::remove (boost::shared_ptr<Redirect> r)
407 {
408         RedirectSelection::iterator i;
409         if ((i = find (redirects.begin(), redirects.end(), r)) != redirects.end()) {
410                 redirects.erase (i);
411                 RedirectsChanged ();
412         }
413 }
414
415 void
416 Selection::remove (TimeAxisView* track)
417 {
418         list<TimeAxisView*>::iterator i;
419         if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
420                 tracks.erase (i);
421                 TracksChanged();
422         }
423 }
424
425 void
426 Selection::remove (const list<TimeAxisView*>& track_list)
427 {
428         bool changed = false;
429
430         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
431
432                 list<TimeAxisView*>::iterator x;
433
434                 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
435                         tracks.erase (x);
436                         changed = true;
437                 }
438         }
439
440         if (changed) {
441                 TracksChanged();
442         }
443 }
444
445 void
446 Selection::remove (boost::shared_ptr<Playlist> track)
447 {
448         list<boost::shared_ptr<Playlist> >::iterator i;
449         if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
450                 playlists.erase (i);
451                 PlaylistsChanged();
452         }
453 }
454
455 void
456 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
457 {
458         bool changed = false;
459
460         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
461
462                 list<boost::shared_ptr<Playlist> >::iterator x;
463
464                 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
465                         playlists.erase (x);
466                         changed = true;
467                 }
468         }
469
470         if (changed) {
471                 PlaylistsChanged();
472         }
473 }
474
475 void
476 Selection::remove (RegionView* r)
477 {
478         if (regions.remove (r)) {
479                 RegionsChanged ();
480         }
481
482         if (!regions.involves (r->get_trackview())) {
483                 remove (&r->get_trackview());
484         }
485 }
486
487
488 void
489 Selection::remove (uint32_t selection_id)
490 {
491         if (time.empty()) {
492                 return;
493         }
494
495         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
496                 if ((*i).id == selection_id) {
497                         time.erase (i);
498                                                 
499                         TimeChanged ();
500                         break;
501                 }
502         }
503 }
504
505 void
506 Selection::remove (nframes_t start, nframes_t end)
507 {
508 }
509
510 void
511 Selection::remove (AutomationList *ac)
512 {
513         list<AutomationList*>::iterator i;
514         if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
515                 lines.erase (i);
516                 LinesChanged();
517         }
518 }
519
520 void
521 Selection::set (boost::shared_ptr<Redirect> r)
522 {
523         clear_redirects ();
524         add (r);
525 }
526
527 void
528 Selection::set (TimeAxisView* track)
529 {
530         clear_tracks ();
531         add (track);
532 }
533
534 void
535 Selection::set (const list<TimeAxisView*>& track_list)
536 {
537         clear_tracks ();
538         add (track_list);
539 }
540
541 void
542 Selection::set (boost::shared_ptr<Playlist> playlist)
543 {
544         clear_playlists ();
545         add (playlist);
546 }
547
548 void
549 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
550 {
551         clear_playlists ();
552         add (pllist);
553 }
554
555 void
556 Selection::set (RegionView* r)
557 {
558         clear_regions ();
559         clear_tracks ();
560         add (r);
561 }
562
563 void
564 Selection::set (vector<RegionView*>& v)
565 {
566         clear_tracks ();
567         clear_regions ();
568         // make sure to deselect any automation selections
569         clear_points();
570         add (v);
571 }
572
573 long
574 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
575 {
576         if ((start == 0 && end == 0) || end < start) {
577                 return 0;
578         }
579
580         if (time.empty()) {
581                 time.push_back (AudioRange (start, end, next_time_id++));
582         } else {
583                 /* reuse the first entry, and remove all the rest */
584
585                 while (time.size() > 1) {
586                         time.pop_front();
587                 }
588                 time.front().start = start;
589                 time.front().end = end;
590         }
591
592         if (track) {
593                 time.track = track;
594                 time.group = track->edit_group();
595         } else {
596                 time.track = 0;
597                 time.group = 0;
598         }
599
600         time.consolidate ();
601
602         TimeChanged ();
603
604         return time.front().id;
605 }
606
607 void
608 Selection::set (AutomationList *ac)
609 {
610         lines.clear();
611         add (ac);
612 }
613
614 bool
615 Selection::selected (TimeAxisView* tv)
616 {
617         return find (tracks.begin(), tracks.end(), tv) != tracks.end();
618 }
619
620 bool
621 Selection::selected (RegionView* rv)
622 {
623         return find (regions.begin(), regions.end(), rv) != regions.end();
624 }
625
626 bool
627 Selection::empty ()
628 {
629         return regions.empty () &&
630                 tracks.empty () &&
631                 points.empty () && 
632                 playlists.empty () && 
633                 lines.empty () &&
634                 time.empty () &&
635                 playlists.empty () &&
636                 redirects.empty () &&
637                 markers.empty()
638                 ;
639 }
640
641 void
642 Selection::toggle (const vector<AutomationSelectable*>& autos)
643 {
644         for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
645                 if ((*x)->get_selected()) {
646                         points.remove (**x);
647                 } else {
648                         points.push_back (**x);
649                 }
650
651                 delete *x;
652         }
653
654         PointsChanged (); /* EMIT SIGNAL */
655 }
656
657 void
658 Selection::toggle (list<Selectable*>& selectables)
659 {
660         RegionView* rv;
661         AutomationSelectable* as;
662         vector<RegionView*> rvs;
663         vector<AutomationSelectable*> autos;
664
665         for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
666                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
667                         rvs.push_back (rv);
668                 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
669                         autos.push_back (as);
670                 } else {
671                         fatal << _("programming error: ")
672                               << X_("unknown selectable type passed to Selection::toggle()")
673                               << endmsg;
674                         /*NOTREACHED*/
675                 }
676         }
677
678         if (!rvs.empty()) {
679                 toggle (rvs);
680         } 
681
682         if (!autos.empty()) {
683                 toggle (autos);
684         } 
685 }
686
687 void
688 Selection::set (list<Selectable*>& selectables)
689 {
690         clear_regions();
691         clear_points ();
692         add (selectables);
693 }
694
695
696 void
697 Selection::add (list<Selectable*>& selectables)
698 {
699         RegionView* rv;
700         AutomationSelectable* as;
701         vector<RegionView*> rvs;
702         vector<AutomationSelectable*> autos;
703
704         for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
705                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
706                         rvs.push_back (rv);
707                 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
708                         autos.push_back (as);
709                 } else {
710                         fatal << _("programming error: ")
711                               << X_("unknown selectable type passed to Selection::add()")
712                               << endmsg;
713                         /*NOTREACHED*/
714                 }
715         }
716
717         if (!rvs.empty()) {
718                 add (rvs);
719         } 
720
721         if (!autos.empty()) {
722                 add (autos);
723         } 
724 }
725
726 void
727 Selection::clear_points ()
728 {
729         if (!points.empty()) {
730                 points.clear ();
731                 PointsChanged ();
732         }
733 }
734
735 void
736 Selection::add (vector<AutomationSelectable*>& autos)
737 {
738         for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
739                 points.push_back (**i);
740         }
741
742         PointsChanged ();
743 }
744
745 void
746 Selection::set (Marker* m)
747 {
748         clear_markers ();
749         add (m);
750 }
751
752 void
753 Selection::toggle (Marker* m)
754 {
755         MarkerSelection::iterator i;
756         
757         if ((i = find (markers.begin(), markers.end(), m)) == markers.end()) {
758                 add (m);
759         } else {
760                 remove (m);
761         }
762 }
763
764 void
765 Selection::remove (Marker* m)
766 {
767         MarkerSelection::iterator i;
768
769         if ((i = find (markers.begin(), markers.end(), m)) != markers.end()) {
770                 markers.erase (i);
771                 MarkersChanged();
772         }
773 }
774
775
776 void
777 Selection::add (Marker* m)
778 {
779         if (find (markers.begin(), markers.end(), m) == markers.end()) {
780                 markers.push_back (m);
781                 MarkersChanged();
782         }
783 }