unfinished work on selection/HiG details, restore range ops destroyed by autoscroll...
[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     $Id$
19 */
20
21 #include <algorithm>
22 #include <sigc++/bind.h>
23 #include <pbd/error.h>
24
25 #include <ardour/playlist.h>
26
27 #include "regionview.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 sigc;
37
38 struct AudioRangeComparator {
39     bool operator()(AudioRange a, AudioRange b) {
40             return a.start < b.start;
41     }
42 };
43
44 Selection&
45 Selection::operator= (const Selection& other)
46 {
47         if (&other != this) {
48                 audio_regions = other.audio_regions;
49                 tracks = other.tracks;
50                 time = other.time;
51                 lines = other.lines;
52         }
53         return *this;
54 }
55
56 bool
57 operator== (const Selection& a, const Selection& b)
58 {
59         return a.audio_regions == b.audio_regions &&
60                 a.tracks == b.tracks &&
61                 a.time.track == b.time.track &&
62                 a.time.group == b.time.group && 
63                 a.time == b.time &&
64                 a.lines == b.lines &&
65                 a.playlists == b.playlists &&
66                 a.redirects == b.redirects;
67 }
68
69 void
70 Selection::clear ()
71 {
72         clear_tracks ();
73         clear_audio_regions ();
74         clear_points ();
75         clear_lines();
76         clear_time ();
77         clear_playlists ();
78         clear_redirects ();
79 }
80
81 void
82 Selection::dump_region_layers()
83 {
84         cerr << "region selection layer dump" << endl;
85         for (AudioRegionSelection::iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
86                 cerr << "layer: " << (int)(*i)->region.layer() << endl;
87         }
88 }
89
90
91 void
92 Selection::clear_redirects ()
93 {
94         if (!redirects.empty()) {
95                 redirects.clear ();
96                 RedirectsChanged ();
97         }
98 }
99
100 void
101 Selection::clear_audio_regions ()
102 {
103         if (!audio_regions.empty()) {
104                 audio_regions.clear_all ();
105                 RegionsChanged();
106         }
107 }
108
109 void
110 Selection::clear_tracks ()
111 {
112         if (!tracks.empty()) {
113                 tracks.clear ();
114                 TracksChanged();
115         }
116 }
117
118 void
119 Selection::clear_time ()
120 {
121         time.track = 0;
122         time.group = 0;
123         time.clear();
124
125         TimeChanged ();
126 }
127
128 void
129 Selection::clear_playlists ()
130 {
131         /* Selections own their playlists */
132
133         for (PlaylistSelection::iterator i = playlists.begin(); i != playlists.end(); ++i) {
134                 (*i)->unref ();
135         }
136
137         if (!playlists.empty()) {
138                 playlists.clear ();
139                 PlaylistsChanged();
140         }
141 }
142
143 void
144 Selection::clear_lines ()
145 {
146         if (!lines.empty()) {
147                 lines.clear ();
148                 LinesChanged();
149         }
150 }
151
152 void
153 Selection::toggle (Redirect* r)
154 {
155         RedirectSelection::iterator i;
156
157         if ((i = find (redirects.begin(), redirects.end(), r)) == redirects.end()) {
158                 redirects.push_back (r);
159         } else {
160                 redirects.erase (i);
161         }
162         RedirectsChanged();
163
164 }
165
166 void
167 Selection::toggle (Playlist* pl)
168 {
169         PlaylistSelection::iterator i;
170
171         if ((i = find (playlists.begin(), playlists.end(), pl)) == playlists.end()) {
172                 pl->ref ();
173                 playlists.push_back(pl);
174         } else {
175                 playlists.erase (i);
176         }
177
178         PlaylistsChanged ();
179 }
180
181 void
182 Selection::toggle (TimeAxisView* track)
183 {
184         TrackSelection::iterator i;
185         
186         if ((i = find (tracks.begin(), tracks.end(), track)) == tracks.end()) {
187                 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
188                 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
189                 tracks.push_back (track);
190         } else {
191                 tracks.erase (i);
192         }
193
194         TracksChanged();
195 }
196
197 void
198 Selection::toggle (AudioRegionView* r)
199 {
200         AudioRegionSelection::iterator i;
201
202         if ((i = find (audio_regions.begin(), audio_regions.end(), r)) == audio_regions.end()) {
203                 audio_regions.add (r);
204         } else {
205                 audio_regions.erase (i);
206         }
207
208         RegionsChanged ();
209 }
210
211 void
212 Selection::toggle (vector<AudioRegionView*>& r)
213 {
214         AudioRegionSelection::iterator i;
215
216         for (vector<AudioRegionView*>::iterator x = r.begin(); x != r.end(); ++x) {
217                 if ((i = find (audio_regions.begin(), audio_regions.end(), (*x))) == audio_regions.end()) {
218                         audio_regions.add ((*x));
219                 } else {
220                         audio_regions.erase (i);
221                 }
222         }
223
224         RegionsChanged ();
225 }
226
227 long
228 Selection::toggle (jack_nframes_t start, jack_nframes_t end)
229 {
230         AudioRangeComparator cmp;
231
232         /* XXX this implementation is incorrect */
233
234         time.push_back (AudioRange (start, end, next_time_id++));
235         time.consolidate ();
236         time.sort (cmp);
237         
238         TimeChanged ();
239
240         return next_time_id - 1;
241 }
242
243
244 void
245 Selection::add (Redirect* r)
246 {
247         if (find (redirects.begin(), redirects.end(), r) == redirects.end()) {
248                 redirects.push_back (r);
249                 RedirectsChanged();
250         }
251 }
252
253 void
254 Selection::add (Playlist* pl)
255 {
256         if (find (playlists.begin(), playlists.end(), pl) == playlists.end()) {
257                 pl->ref ();
258                 playlists.push_back(pl);
259                 PlaylistsChanged ();
260         }
261 }
262
263 void
264 Selection::add (const list<Playlist*>& pllist)
265 {
266         bool changed = false;
267
268         for (list<Playlist*>::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
269                 if (find (playlists.begin(), playlists.end(), (*i)) == playlists.end()) {
270                         (*i)->ref ();
271                         playlists.push_back (*i);
272                         changed = true;
273                 }
274         }
275         
276         if (changed) {
277                 PlaylistsChanged ();
278         }
279 }
280
281 void
282 Selection::add (const list<TimeAxisView*>& track_list)
283 {
284         bool changed = false;
285
286         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
287                 if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
288                         void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
289                         (*i)->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), (*i)));
290                         tracks.push_back (*i);
291                         changed = true;
292                 }
293         }
294         
295         if (changed) {
296                 TracksChanged ();
297         }
298 }
299
300 void
301 Selection::add (TimeAxisView* track)
302 {
303         if (find (tracks.begin(), tracks.end(), track) == tracks.end()) {
304                 void (Selection::*pmf)(TimeAxisView*) = &Selection::remove;
305                 track->GoingAway.connect (sigc::bind (mem_fun (*this, pmf), track));
306                 tracks.push_back (track);
307                 TracksChanged();
308         }
309 }
310
311 void
312 Selection::add (AudioRegionView* r)
313 {
314         if (find (audio_regions.begin(), audio_regions.end(), r) == audio_regions.end()) {
315                 audio_regions.add (r);
316                 RegionsChanged ();
317         }
318 }
319
320 void
321 Selection::add (vector<AudioRegionView*>& v)
322 {
323         bool changed = false;
324
325         for (vector<AudioRegionView*>::iterator i = v.begin(); i != v.end(); ++i) {
326                 if (find (audio_regions.begin(), audio_regions.end(), (*i)) == audio_regions.end()) {
327                         audio_regions.add ((*i));
328                         changed = true;
329                 }
330         }
331
332         if (changed) {
333                 RegionsChanged ();
334         }
335 }
336
337 long
338 Selection::add (jack_nframes_t start, jack_nframes_t end)
339 {
340         AudioRangeComparator cmp;
341
342         /* XXX this implementation is incorrect */
343
344         time.push_back (AudioRange (start, end, next_time_id++));
345         time.consolidate ();
346         time.sort (cmp);
347         
348         TimeChanged ();
349
350         return next_time_id - 1;
351 }
352
353 void
354 Selection::replace (uint32_t sid, jack_nframes_t start, jack_nframes_t end)
355 {
356         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
357                 if ((*i).id == sid) {
358                         time.erase (i);
359                         time.push_back (AudioRange(start,end, sid));
360
361                         /* don't consolidate here */
362
363
364                         AudioRangeComparator cmp;
365                         time.sort (cmp);
366
367                         TimeChanged ();
368                         break;
369                 }
370         }
371 }
372
373 void
374 Selection::add (AutomationList* ac)
375 {
376         if (find (lines.begin(), lines.end(), ac) == lines.end()) {
377                 lines.push_back (ac);
378                 LinesChanged();
379         }
380 }
381
382 void
383 Selection::remove (Redirect* r)
384 {
385         list<Redirect*>::iterator i;
386         if ((i = find (redirects.begin(), redirects.end(), r)) != redirects.end()) {
387                 redirects.erase (i);
388                 RedirectsChanged ();
389         }
390 }
391
392 void
393 Selection::remove (TimeAxisView* track)
394 {
395         list<TimeAxisView*>::iterator i;
396         if ((i = find (tracks.begin(), tracks.end(), track)) != tracks.end()) {
397                 tracks.erase (i);
398                 TracksChanged();
399         }
400 }
401
402 void
403 Selection::remove (const list<TimeAxisView*>& track_list)
404 {
405         bool changed = false;
406
407         for (list<TimeAxisView*>::const_iterator i = track_list.begin(); i != track_list.end(); ++i) {
408
409                 list<TimeAxisView*>::iterator x;
410
411                 if ((x = find (tracks.begin(), tracks.end(), (*i))) != tracks.end()) {
412                         tracks.erase (x);
413                         changed = true;
414                 }
415         }
416
417         if (changed) {
418                 TracksChanged();
419         }
420 }
421
422 void
423 Selection::remove (Playlist* track)
424 {
425         list<Playlist*>::iterator i;
426         if ((i = find (playlists.begin(), playlists.end(), track)) != playlists.end()) {
427                 playlists.erase (i);
428                 PlaylistsChanged();
429         }
430 }
431
432 void
433 Selection::remove (const list<Playlist*>& pllist)
434 {
435         bool changed = false;
436
437         for (list<Playlist*>::const_iterator i = pllist.begin(); i != pllist.end(); ++i) {
438
439                 list<Playlist*>::iterator x;
440
441                 if ((x = find (playlists.begin(), playlists.end(), (*i))) != playlists.end()) {
442                         playlists.erase (x);
443                         changed = true;
444                 }
445         }
446
447         if (changed) {
448                 PlaylistsChanged();
449         }
450 }
451
452 void
453 Selection::remove (AudioRegionView* r)
454 {
455         audio_regions.remove (r);
456         RegionsChanged ();
457 }
458
459
460 void
461 Selection::remove (uint32_t selection_id)
462 {
463         if (time.empty()) {
464                 return;
465         }
466
467         for (list<AudioRange>::iterator i = time.begin(); i != time.end(); ++i) {
468                 if ((*i).id == selection_id) {
469                         time.erase (i);
470                                                 
471                         TimeChanged ();
472                         break;
473                 }
474         }
475 }
476
477 void
478 Selection::remove (jack_nframes_t start, jack_nframes_t end)
479 {
480 }
481
482 void
483 Selection::remove (AutomationList *ac)
484 {
485         list<AutomationList*>::iterator i;
486         if ((i = find (lines.begin(), lines.end(), ac)) != lines.end()) {
487                 lines.erase (i);
488                 LinesChanged();
489         }
490 }
491
492 void
493 Selection::set (Redirect *r)
494 {
495         clear_redirects ();
496         add (r);
497 }
498
499 void
500 Selection::set (TimeAxisView* track)
501 {
502         clear_tracks ();
503         add (track);
504 }
505
506 void
507 Selection::set (const list<TimeAxisView*>& track_list)
508 {
509         clear_tracks ();
510         add (track_list);
511 }
512
513 void
514 Selection::set (Playlist* playlist)
515 {
516         clear_playlists ();
517         add (playlist);
518 }
519
520 void
521 Selection::set (const list<Playlist*>& pllist)
522 {
523         clear_playlists ();
524         add (pllist);
525 }
526
527 void
528 Selection::set (AudioRegionView* r)
529 {
530         clear_audio_regions ();
531         add (r);
532 }
533
534 void
535 Selection::set (vector<AudioRegionView*>& v)
536 {
537
538         clear_audio_regions ();
539         // make sure to deselect any automation selections
540         clear_points();
541         add (v);
542 }
543
544 long
545 Selection::set (TimeAxisView* track, jack_nframes_t start, jack_nframes_t end)
546 {
547         if ((start == 0 && end == 0) || end < start) {
548                 return 0;
549         }
550
551         if (time.empty()) {
552                 time.push_back (AudioRange (start, end, next_time_id++));
553         } else {
554                 /* reuse the first entry, and remove all the rest */
555
556                 while (time.size() > 1) {
557                         time.pop_front();
558                 }
559                 time.front().start = start;
560                 time.front().end = end;
561         }
562
563         if (track) {
564                 time.track = track;
565                 time.group = track->edit_group();
566         } else {
567                 time.track = 0;
568                 time.group = 0;
569         }
570
571         time.consolidate ();
572
573         TimeChanged ();
574
575         return time.front().id;
576 }
577
578 void
579 Selection::set (AutomationList *ac)
580 {
581         lines.clear();
582         add (ac);
583 }
584
585 bool
586 Selection::selected (TimeAxisView* tv)
587 {
588         return find (tracks.begin(), tracks.end(), tv) != tracks.end();
589 }
590
591 bool
592 Selection::selected (AudioRegionView* arv)
593 {
594         return find (audio_regions.begin(), audio_regions.end(), arv) != audio_regions.end();
595 }
596
597 bool
598 Selection::empty ()
599 {
600         return audio_regions.empty () &&
601                 tracks.empty () &&
602                 points.empty () && 
603                 playlists.empty () && 
604                 lines.empty () &&
605                 time.empty () &&
606                 playlists.empty () &&
607                 redirects.empty ()
608                 ;
609 }
610
611 void
612 Selection::set (list<Selectable*>& selectables)
613 {
614         clear_audio_regions();
615         clear_points ();
616         add (selectables);
617 }
618
619 void
620 Selection::add (list<Selectable*>& selectables)
621 {
622         AudioRegionView* arv;
623         AutomationSelectable* as;
624         vector<AudioRegionView*> arvs;
625         vector<AutomationSelectable*> autos;
626
627         for (std::list<Selectable*>::iterator i = selectables.begin(); i != selectables.end(); ++i) {
628                 if ((arv = dynamic_cast<AudioRegionView*> (*i)) != 0) {
629                         arvs.push_back (arv);
630                 } else if ((as = dynamic_cast<AutomationSelectable*> (*i)) != 0) {
631                         autos.push_back (as);
632                 } else {
633                         fatal << _("programming error: ")
634                               << X_("unknown selectable type passed to Selection::set()")
635                               << endmsg;
636                         /*NOTREACHED*/
637                 }
638         }
639
640         if (!arvs.empty()) {
641                 add (arvs);
642         } 
643
644         if (!autos.empty()) {
645                 add (autos);
646         } 
647 }
648
649 void
650 Selection::clear_points ()
651 {
652         if (!points.empty()) {
653                 points.clear ();
654                 PointsChanged ();
655         }
656 }
657
658 void
659 Selection::add (vector<AutomationSelectable*>& autos)
660 {
661         for (vector<AutomationSelectable*>::iterator i = autos.begin(); i != autos.end(); ++i) {
662                 points.push_back (**i);
663                 delete *i;
664         }
665
666         PointsChanged ();
667 }