Renamed Insert to Processor and Redirect to IOProcessor.
[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.processors == b.processors;
68 }
69
70 /** Clear everything from the Selection */
71 void
72 Selection::clear ()
73 {
74         clear_tracks ();
75         clear_regions ();
76         clear_points ();
77         clear_lines();
78         clear_time ();
79         clear_playlists ();
80         clear_processors ();
81 }
82
83 void
84 Selection::dump_region_layers()
85 {
86         cerr << "region selection layer dump" << endl;
87         for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
88                 cerr << "layer: " << (int)(*i)->region()->layer() << endl;
89         }
90 }
91
92
93 void
94 Selection::clear_processors ()
95 {
96         if (!processors.empty()) {
97                 processors.clear ();
98                 ProcessorsChanged ();
99         }
100 }
101
102 void
103 Selection::clear_regions ()
104 {
105         if (!regions.empty()) {
106                 regions.clear_all ();
107                 RegionsChanged();
108         }
109 }
110
111 void
112 Selection::clear_tracks ()
113 {
114         if (!tracks.empty()) {
115                 tracks.clear ();
116                 TracksChanged();
117         }
118 }
119
120 void
121 Selection::clear_time ()
122 {
123         time.track = 0;
124         time.group = 0;
125         time.clear();
126
127         TimeChanged ();
128 }
129
130 void
131 Selection::clear_playlists ()
132 {
133         /* Selections own their playlists */
134
135         for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
136                 /* selections own their own regions, which are copies of the "originals". make them go away */
137                 (*i)->drop_regions ();
138                 (*i)->release ();
139         }
140
141         if (!playlists.empty()) {
142                 playlists.clear ();
143                 PlaylistsChanged();
144         }
145 }
146
147 void
148 Selection::clear_lines ()
149 {
150         if (!lines.empty()) {
151                 lines.clear ();
152                 LinesChanged();
153         }
154 }
155
156 void
157 Selection::toggle (boost::shared_ptr<Processor> r)
158 {
159         ProcessorSelection::iterator i;
160
161         if ((i = find (processors.begin(), processors.end(), r)) == processors.end()) {
162                 processors.push_back (r);
163         } else {
164                 processors.erase (i);
165         }
166         ProcessorsChanged();
167
168 }
169
170 void
171 Selection::toggle (boost::shared_ptr<Playlist> pl)
172 {
173         PlaylistSelection::iterator i;
174
175         if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
176                 pl->use ();
177                 playlists.push_back(pl);
178         } else {
179                 playlists.erase (i);
180         }
181
182         PlaylistsChanged ();
183 }
184
185 void
186 Selection::toggle (const list<TimeAxisView*>& track_list)
187 {
188         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
189                 toggle ( (*i) );
190         }
191 }
192
193 void
194 Selection::toggle (TimeAxisView* track)
195 {
196         TrackSelection::iterator i;
197         
198         if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
199                 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
200                 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
201                 tracks.push_back (track);
202         } else {
203                 tracks.erase (i);
204         }
205
206         TracksChanged();
207 }
208
209 void
210 Selection::toggle (RegionView* r)
211 {
212         RegionSelection::iterator i;
213
214         if ((i = find (regions.begin(), regions.end(), r)) == regions.end()) {
215                 add (r);
216         } else {
217                 remove (*i);
218         }
219
220         RegionsChanged ();
221 }
222
223 void
224 Selection::toggle (vector<RegionView*>& r)
225 {
226         RegionSelection::iterator i;
227
228         for (vector<RegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
229                 if ((i = find (regions.begin(), regions.end(), (*x))) == regions.end()) {
230                         add ((*x));
231                 } else {
232                         remove (*x);
233                 }
234         }
235
236         RegionsChanged ();
237 }
238
239 long
240 Selection::toggle (nframes_t start, nframes_t end)
241 {
242         AudioRangeComparator cmp;
243
244         /* XXX this implementation is incorrect */
245
246         time.push_back (AudioRange (start, end, next_time_id++));
247         time.consolidate ();
248         time.sort (cmp);
249         
250         TimeChanged ();
251
252         return next_time_id - 1;
253 }
254
255
256 void
257 Selection::add (boost::shared_ptr<Processor> i)
258 {
259         if (find (processors.begin(), processors.end(), i) == processors.end()) {
260                 processors.push_back (i);
261                 ProcessorsChanged();
262         }
263 }
264
265 void
266 Selection::add (boost::shared_ptr<Playlist> pl)
267 {
268         if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
269                 pl->use ();
270                 playlists.push_back(pl);
271                 PlaylistsChanged ();
272         }
273 }
274
275 void
276 Selection::add (const list<boost::shared_ptr<Playlist> >& pllist)
277 {
278         bool changed = false;
279
280         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
281                 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
282                         (*i)->use ();
283                         playlists.push_back (*i);
284                         changed = true;
285                 }
286         }
287         
288         if (changed) {
289                 PlaylistsChanged ();
290         }
291 }
292
293 void
294 Selection::add (const list<TimeAxisView*>& track_list)
295 {
296         bool changed = false;
297
298         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
299                 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
300                         void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
301                         (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
302                         tracks.push_back (*i);
303                         changed = true;
304                 }
305         }
306         
307         if (changed) {
308                 TracksChanged ();
309         }
310 }
311
312 void
313 Selection::add (TimeAxisView* track)
314 {
315         if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
316                 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
317                 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
318                 tracks.push_back (track);
319                 TracksChanged();
320         }
321 }
322
323 void
324 Selection::add (RegionView* r)
325 {
326         if (find (regions.begin(), regions.end(), r) == regions.end()) {
327                 regions.add (r);
328                 add (&r->get_trackview());
329                 RegionsChanged ();
330         }
331 }
332
333 void
334 Selection::add (vector<RegionView*>& v)
335 {
336         bool changed = false;
337
338         for (vector<RegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
339                 if (find (regions.begin(), regions.end(), (*i)) == regions.end()) {
340                         changed = regions.add ((*i));
341                         if (changed) {
342                                 add (&(*i)->get_trackview());
343                         }
344                 }
345         }
346
347         if (changed) {
348                 RegionsChanged ();
349         }
350 }
351
352 long
353 Selection::add (nframes_t start, nframes_t end)
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 - 1;
366 }
367
368 void
369 Selection::replace (uint32_t sid, nframes_t start, nframes_t end)
370 {
371         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
372                 if ((*i).id == sid) {
373                         time.erase (i);
374                         time.push_back (AudioRange(start,end, sid));
375
376                         /* don't consolidate here */
377
378
379                         AudioRangeComparator cmp;
380                         time.sort (cmp);
381
382                         TimeChanged ();
383                         break;
384                 }
385         }
386 }
387
388 void
389 Selection::add (AutomationList* ac)
390 {
391         if (find (lines.begin(), lines.end(), ac) == lines.end()) {
392                 lines.push_back (ac);
393                 LinesChanged();
394         }
395 }
396
397 void
398 Selection::remove (boost::shared_ptr<Processor> r)
399 {
400         ProcessorSelection::iterator i;
401         if ((i = find (processors.begin(), processors.end(), r)) != processors.end()) {
402                 processors.erase (i);
403                 ProcessorsChanged ();
404         }
405 }
406
407 void
408 Selection::remove (TimeAxisView* track)
409 {
410         list<TimeAxisView*>::iterator i;
411         if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
412                 tracks.erase (i);
413                 TracksChanged();
414         }
415 }
416
417 void
418 Selection::remove (const list<TimeAxisView*>& track_list)
419 {
420         bool changed = false;
421
422         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
423
424                 list<TimeAxisView*>::iterator x;
425
426                 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
427                         tracks.erase (x);
428                         changed = true;
429                 }
430         }
431
432         if (changed) {
433                 TracksChanged();
434         }
435 }
436
437 void
438 Selection::remove (boost::shared_ptr<Playlist> track)
439 {
440         list<boost::shared_ptr<Playlist> >::iterator i;
441         if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
442                 playlists.erase (i);
443                 PlaylistsChanged();
444         }
445 }
446
447 void
448 Selection::remove (const list<boost::shared_ptr<Playlist> >& pllist)
449 {
450         bool changed = false;
451
452         for (list<boost::shared_ptr<Playlist> >::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
453
454                 list<boost::shared_ptr<Playlist> >::iterator x;
455
456                 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
457                         playlists.erase (x);
458                         changed = true;
459                 }
460         }
461
462         if (changed) {
463                 PlaylistsChanged();
464         }
465 }
466
467 void
468 Selection::remove (RegionView* r)
469 {
470         if (regions.remove (r)) {
471                 RegionsChanged ();
472         }
473
474         if (!regions.involves (r->get_trackview())) {
475                 remove (&r->get_trackview());
476         }
477 }
478
479
480 void
481 Selection::remove (uint32_t selection_id)
482 {
483         if (time.empty()) {
484                 return;
485         }
486
487         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
488                 if ((*i).id == selection_id) {
489                         time.erase (i);
490                                                 
491                         TimeChanged ();
492                         break;
493                 }
494         }
495 }
496
497 void
498 Selection::remove (nframes_t start, nframes_t end)
499 {
500 }
501
502 void
503 Selection::remove (AutomationList *ac)
504 {
505         list<AutomationList*>::iterator i;
506         if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
507                 lines.erase (i);
508                 LinesChanged();
509         }
510 }
511
512 void
513 Selection::set (boost::shared_ptr<Processor> i)
514 {
515         clear_processors ();
516         add (i);
517 }
518
519 void
520 Selection::set (TimeAxisView* track)
521 {
522         clear_tracks ();
523         add (track);
524 }
525
526 void
527 Selection::set (const list<TimeAxisView*>& track_list)
528 {
529         clear_tracks ();
530         add (track_list);
531 }
532
533 void
534 Selection::set (boost::shared_ptr<Playlist> playlist)
535 {
536         clear_playlists ();
537         add (playlist);
538 }
539
540 void
541 Selection::set (const list<boost::shared_ptr<Playlist> >& pllist)
542 {
543         clear_playlists ();
544         add (pllist);
545 }
546
547 void
548 Selection::set (RegionView* r)
549 {
550         clear_regions ();
551         clear_tracks ();
552         add (r);
553 }
554
555 void
556 Selection::set (vector<RegionView*>& v)
557 {
558         clear_tracks ();
559         clear_regions ();
560         // make sure to deselect any automation selections
561         clear_points();
562         add (v);
563 }
564
565 long
566 Selection::set (TimeAxisView* track, nframes_t start, nframes_t end)
567 {
568         if ((start == 0 && end == 0) || end < start) {
569                 return 0;
570         }
571
572         if (time.empty()) {
573                 time.push_back (AudioRange (start, end, next_time_id++));
574         } else {
575                 /* reuse the first entry, and remove all the rest */
576
577                 while (time.size() > 1) {
578                         time.pop_front();
579                 }
580                 time.front().start = start;
581                 time.front().end = end;
582         }
583
584         if (track) {
585                 time.track = track;
586                 time.group = track->edit_group();
587         } else {
588                 time.track = 0;
589                 time.group = 0;
590         }
591
592         time.consolidate ();
593
594         TimeChanged ();
595
596         return time.front().id;
597 }
598
599 void
600 Selection::set (AutomationList *ac)
601 {
602         lines.clear();
603         add (ac);
604 }
605
606 bool
607 Selection::selected (TimeAxisView* tv)
608 {
609         return find (tracks.begin(), tracks.end(), tv) != tracks.end();
610 }
611
612 bool
613 Selection::selected (RegionView* rv)
614 {
615         return find (regions.begin(), regions.end(), rv) != regions.end();
616 }
617
618 bool
619 Selection::empty ()
620 {
621         return regions.empty () &&
622                 tracks.empty () &&
623                 points.empty () && 
624                 playlists.empty () && 
625                 lines.empty () &&
626                 time.empty () &&
627                 playlists.empty () &&
628                 processors.empty ()
629                 ;
630 }
631
632 void
633 Selection::toggle (const vector<AutomationSelectable*>& autos)
634 {
635         for (vector<AutomationSelectable*>::const_iterator x = autos.begin(); x != autos.end(); ++x) {
636                 if ((*x)->get_selected()) {
637                         points.remove (**x);
638                 } else {
639                         points.push_back (**x);
640                 }
641
642                 delete *x;
643         }
644
645         PointsChanged (); /* EMIT SIGNAL */
646 }
647
648 void
649 Selection::toggle (list<Selectable*>& selectables)
650 {
651         RegionView* rv;
652         AutomationSelectable* as;
653         vector<RegionView*> rvs;
654         vector<AutomationSelectable*> autos;
655
656         for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
657                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
658                         rvs.push_back (rv);
659                 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
660                         autos.push_back (as);
661                 } else {
662                         fatal << _("programming error: ")
663                               << X_("unknown selectable type passed to Selection::toggle()")
664                               << endmsg;
665                         /*NOTREACHED*/
666                 }
667         }
668
669         if (!rvs.empty()) {
670                 toggle (rvs);
671         } 
672
673         if (!autos.empty()) {
674                 toggle (autos);
675         } 
676 }
677
678 void
679 Selection::set (list<Selectable*>& selectables)
680 {
681         clear_regions();
682         clear_points ();
683         add (selectables);
684 }
685
686
687 void
688 Selection::add (list<Selectable*>& selectables)
689 {
690         RegionView* rv;
691         AutomationSelectable* as;
692         vector<RegionView*> rvs;
693         vector<AutomationSelectable*> autos;
694
695         for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
696                 if ((rv = dynamic_cast<RegionView*> (*i)) != 0) {
697                         rvs.push_back (rv);
698                 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
699                         autos.push_back (as);
700                 } else {
701                         fatal << _("programming error: ")
702                               << X_("unknown selectable type passed to Selection::add()")
703                               << endmsg;
704                         /*NOTREACHED*/
705                 }
706         }
707
708         if (!rvs.empty()) {
709                 add (rvs);
710         } 
711
712         if (!autos.empty()) {
713                 add (autos);
714         } 
715 }
716
717 void
718 Selection::clear_points ()
719 {
720         if (!points.empty()) {
721                 points.clear ();
722                 PointsChanged ();
723         }
724 }
725
726 void
727 Selection::add (vector<AutomationSelectable*>& autos)
728 {
729         for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
730                 points.push_back (**i);
731         }
732
733         PointsChanged ();
734 }