"finalmunge" patch from nick, plus work on editor region list and actions infrastruct...
[ardour.git] / gtk2_ardour / editor_ops.cc
1 /*
2     Copyright (C) 2000-2004 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 <unistd.h>
22
23 #include <cstdlib>
24 #include <cmath>
25 #include <string>
26 #include <map>
27
28 #include <sndfile.h>
29
30 #include <pbd/error.h>
31 #include <pbd/basename.h>
32 #include <pbd/pthread_utils.h>
33
34 #include <gtkmm2ext/utils.h>
35 #include <gtkmm2ext/choice.h>
36
37 #include <ardour/audioengine.h>
38 #include <ardour/session.h>
39 #include <ardour/audioplaylist.h>
40 #include <ardour/audioregion.h>
41 #include <ardour/diskstream.h>
42 #include <ardour/filesource.h>
43 #include <ardour/sndfilesource.h>
44 #include <ardour/utils.h>
45 #include <ardour/location.h>
46 #include <ardour/named_selection.h>
47 #include <ardour/audio_track.h>
48 #include <ardour/audioplaylist.h>
49 #include <ardour/region_factory.h>
50 #include <ardour/reverse.h>
51
52 #include "ardour_ui.h"
53 #include "editor.h"
54 #include "time_axis_view.h"
55 #include "audio_time_axis.h"
56 #include "automation_time_axis.h"
57 #include "streamview.h"
58 #include "regionview.h"
59 #include "rgb_macros.h"
60 #include "selection_templates.h"
61 #include "selection.h"
62 #include "library_ui.h"
63 #include "editing.h"
64 #include "gtk-custom-hruler.h"
65
66 #include "i18n.h"
67
68 using namespace std;
69 using namespace ARDOUR;
70 using namespace sigc;
71 using namespace Gtk;
72 using namespace Editing;
73
74 /***********************************************************************
75   Editor operations
76  ***********************************************************************/
77
78 void
79 Editor::undo (uint32_t n)
80 {
81         if (session) {
82                 session->undo (n);
83         }
84 }
85
86 void
87 Editor::redo (uint32_t n)
88 {
89         if (session) {
90                 session->redo (n);
91         }
92 }
93
94 int
95 Editor::ensure_cursor (jack_nframes_t *pos)
96 {
97         *pos = edit_cursor->current_frame;
98         return 0;
99 }
100
101 void
102 Editor::split_region ()
103 {
104         split_region_at (edit_cursor->current_frame);
105 }
106
107 void
108 Editor::split_region_at (jack_nframes_t where)
109 {
110         split_regions_at (where, selection->audio_regions);
111 }
112
113 void
114 Editor::split_regions_at (jack_nframes_t where, AudioRegionSelection& regions)
115 {
116         begin_reversible_command (_("split"));
117
118         snap_to (where);
119         for (AudioRegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
120
121                 AudioRegionSelection::iterator tmp;
122                 
123                 tmp = a;
124                 ++tmp;
125
126                 Playlist* pl = (*a)->region.playlist();
127
128                 _new_regionviews_show_envelope = (*a)->envelope_visible();
129                 
130                 if (pl) {
131                         session->add_undo (pl->get_memento());
132                         pl->split_region ((*a)->region, where);
133                         session->add_redo_no_execute (pl->get_memento());
134                 }
135
136                 a = tmp;
137     }
138
139         commit_reversible_command ();
140         _new_regionviews_show_envelope = false;
141 }
142
143 void
144 Editor::remove_clicked_region ()
145 {
146         if (clicked_audio_trackview == 0 || clicked_regionview == 0) {
147                 return;
148         }
149
150         Playlist* playlist = clicked_audio_trackview->playlist();
151         
152         begin_reversible_command (_("remove region"));
153         session->add_undo (playlist->get_memento());
154         playlist->remove_region (&clicked_regionview->region);
155         session->add_redo_no_execute (playlist->get_memento());
156         commit_reversible_command ();
157 }
158
159 void
160 Editor::destroy_clicked_region ()
161 {
162         int32_t selected = selection->audio_regions.size();
163
164         if (!session || clicked_regionview == 0 && selected == 0) {
165                 return;
166         }
167
168         vector<string> choices;
169         string prompt;
170         
171         prompt  = string_compose (_(" This is destructive, will possibly delete audio files\n\
172 It cannot be undone\n\
173 Do you really want to destroy %1 ?"),
174                            (selected > 1 ? 
175                             _("these regions") : _("this region")));
176
177         if (selected > 1) {
178                 choices.push_back (_("Yes, destroy them."));
179         } else {
180                 choices.push_back (_("Yes, destroy it."));
181         }
182
183         choices.push_back (_("No, do nothing."));
184
185         Gtkmm2ext::Choice prompter (prompt, choices);
186
187         prompter.chosen.connect (Gtk::Main::quit.slot());
188         prompter.show_all ();
189
190         Gtk::Main::run ();
191
192         if (prompter.get_choice() != 0) {
193                 return;
194         }
195
196         if (selected > 0) {
197                 list<Region*> r;
198
199                 for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
200                         r.push_back (&(*i)->region);
201                 }
202
203                 session->destroy_regions (r);
204
205         } else if (clicked_regionview) {
206                 session->destroy_region (&clicked_regionview->region);
207         } 
208 }
209
210 AudioRegion *
211 Editor::select_region_for_operation (int dir, TimeAxisView **tv)
212 {
213         AudioRegionView* rv;
214         AudioRegion *region;
215         jack_nframes_t start = 0;
216
217         if (selection->time.start () == selection->time.end_frame ()) {
218                 
219                 /* no current selection-> is there a selected regionview? */
220
221                 if (selection->audio_regions.empty()) {
222                         return 0;
223                 }
224
225         } 
226
227         region = 0;
228
229         if (!selection->audio_regions.empty()) {
230
231                 rv = *(selection->audio_regions.begin());
232                 (*tv) = &rv->get_time_axis_view();
233                 region = &rv->region;
234
235         } else if (!selection->tracks.empty()) {
236
237                 (*tv) = selection->tracks.front();
238
239                 AudioTimeAxisView* atv;
240
241                 if ((atv = dynamic_cast<AudioTimeAxisView*> (*tv)) != 0) {
242                         Playlist *pl;
243                         
244                         if ((pl = atv->playlist()) == 0) {
245                                 return 0;
246                         }
247                         
248                         region = dynamic_cast<AudioRegion*> (pl->top_region_at (start));
249                 }
250         } 
251         
252         return region;
253 }
254         
255 void
256 Editor::extend_selection_to_end_of_region (bool next)
257 {
258         TimeAxisView *tv;
259         Region *region;
260         jack_nframes_t start;
261
262         if ((region = select_region_for_operation (next ? 1 : 0, &tv)) == 0) {
263                 return;
264         }
265
266         if (region && selection->time.start () == selection->time.end_frame ()) {
267                 start = region->position();
268         } else {
269                 start = selection->time.start ();
270         }
271
272         /* Try to leave the selection with the same route if possible */
273
274         if ((tv = selection->time.track) == 0) {
275                 return;
276         }
277
278         begin_reversible_command (_("extend selection"));
279         selection->set (tv, start, region->position() + region->length());
280         commit_reversible_command ();
281 }
282
283 void
284 Editor::extend_selection_to_start_of_region (bool previous)
285 {
286         TimeAxisView *tv;
287         Region *region;
288         jack_nframes_t end;
289
290         if ((region = select_region_for_operation (previous ? -1 : 0, &tv)) == 0) {
291                 return;
292         }
293
294         if (region && selection->time.start () == selection->time.end_frame ()) {
295                 end = region->position() + region->length();
296         } else {
297                 end = selection->time.end_frame ();
298         }
299
300         /* Try to leave the selection with the same route if possible */
301         
302         if ((tv = selection->time.track) == 0) {
303                 return;
304         }
305
306         begin_reversible_command (_("extend selection"));
307         selection->set (tv, region->position(), end);
308         commit_reversible_command ();
309 }
310
311
312 void
313 Editor::nudge_forward (bool next)
314 {
315         jack_nframes_t distance;
316         jack_nframes_t next_distance;
317
318         if (!session) return;
319         
320         if (!selection->audio_regions.empty()) {
321
322                 begin_reversible_command (_("nudge forward"));
323
324                 for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
325                         AudioRegion& r ((*i)->region);
326                         
327                         distance = get_nudge_distance (r.position(), next_distance);
328
329                         if (next) {
330                                 distance = next_distance;
331                         }
332
333                         session->add_undo (r.playlist()->get_memento());
334                         r.set_position (r.position() + distance, this);
335                         session->add_redo_no_execute (r.playlist()->get_memento());
336                 }
337
338                 commit_reversible_command ();
339
340         } else {
341                 distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
342                 session->request_locate (playhead_cursor->current_frame + distance);
343         }
344 }
345                 
346 void
347 Editor::nudge_backward (bool next)
348 {
349         jack_nframes_t distance;
350         jack_nframes_t next_distance;
351
352         if (!session) return;
353         
354         if (!selection->audio_regions.empty()) {
355
356                 begin_reversible_command (_("nudge forward"));
357
358                 for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
359                         AudioRegion& r ((*i)->region);
360
361                         distance = get_nudge_distance (r.position(), next_distance);
362                         
363                         if (next) {
364                                 distance = next_distance;
365                         }
366
367                         session->add_undo (r.playlist()->get_memento());
368                         
369                         if (r.position() > distance) {
370                                 r.set_position (r.position() - distance, this);
371                         } else {
372                                 r.set_position (0, this);
373                         }
374                         session->add_redo_no_execute (r.playlist()->get_memento());
375                 }
376
377                 commit_reversible_command ();
378
379         } else {
380
381                 distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
382
383                 if (playhead_cursor->current_frame > distance) {
384                         session->request_locate (playhead_cursor->current_frame - distance);
385                 } else {
386                         session->request_locate (0);
387                 }
388         }
389 }
390
391 void
392 Editor::nudge_forward_capture_offset ()
393 {
394         jack_nframes_t distance;
395
396         if (!session) return;
397         
398         if (!selection->audio_regions.empty()) {
399
400                 begin_reversible_command (_("nudge forward"));
401
402                 distance = session->worst_output_latency();
403
404                 for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
405                         AudioRegion& r ((*i)->region);
406                         
407                         session->add_undo (r.playlist()->get_memento());
408                         r.set_position (r.position() + distance, this);
409                         session->add_redo_no_execute (r.playlist()->get_memento());
410                 }
411
412                 commit_reversible_command ();
413
414         } 
415 }
416                 
417 void
418 Editor::nudge_backward_capture_offset ()
419 {
420         jack_nframes_t distance;
421
422         if (!session) return;
423         
424         if (!selection->audio_regions.empty()) {
425
426                 begin_reversible_command (_("nudge forward"));
427
428                 distance = session->worst_output_latency();
429
430                 for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
431                         AudioRegion& r ((*i)->region);
432
433                         session->add_undo (r.playlist()->get_memento());
434                         
435                         if (r.position() > distance) {
436                                 r.set_position (r.position() - distance, this);
437                         } else {
438                                 r.set_position (0, this);
439                         }
440                         session->add_redo_no_execute (r.playlist()->get_memento());
441                 }
442
443                 commit_reversible_command ();
444         }
445 }
446
447 /* DISPLAY MOTION */
448
449 void
450 Editor::move_to_start ()
451 {
452         session->request_locate (0);
453 }
454
455 void
456 Editor::move_to_end ()
457 {
458
459         session->request_locate (session->current_end_frame());
460 }
461
462 void
463 Editor::build_region_boundary_cache ()
464 {
465         jack_nframes_t pos = 0;
466         RegionPoint point;
467         Region *r;
468         TrackViewList tracks;
469
470         region_boundary_cache.clear ();
471
472         if (session == 0) {
473                 return;
474         }
475         
476         switch (snap_type) {
477         case SnapToRegionStart:
478                 point = Start;
479                 break;
480         case SnapToRegionEnd:
481                 point = End;
482                 break;  
483         case SnapToRegionSync:
484                 point = SyncPoint;
485                 break;  
486         case SnapToRegionBoundary:
487                 point = Start;
488                 break;  
489         default:
490                 fatal << string_compose (_("build_region_boundary_cache called with snap_type = %1"), snap_type) << endmsg;
491                 /*NOTREACHED*/
492                 return;
493         }
494         
495         TimeAxisView *ontrack = 0;
496
497         while (pos < session->current_end_frame()) {
498
499                 if (!selection->tracks.empty()) {
500
501                         if ((r = find_next_region (pos, point, 1, selection->tracks, &ontrack)) == 0) {
502                                 break;
503                         }
504
505                 } else if (clicked_trackview) {
506
507                         TrackViewList t;
508                         t.push_back (clicked_trackview);
509
510                         if ((r = find_next_region (pos, point, 1, t, &ontrack)) == 0) {
511                                 break;
512                         }
513
514                 } else {
515
516                         if ((r = find_next_region (pos, point, 1, track_views, &ontrack)) == 0) {
517                                 break;
518                         }
519                 }
520
521                 jack_nframes_t rpos;
522                 
523                 switch (snap_type) {
524                 case SnapToRegionStart:
525                         rpos = r->first_frame();
526                         break;
527                 case SnapToRegionEnd:
528                         rpos = r->last_frame();
529                         break;  
530                 case SnapToRegionSync:
531                         rpos = r->adjust_to_sync (r->first_frame());
532                         break;
533
534                 case SnapToRegionBoundary:
535                         rpos = r->last_frame();
536                         break;  
537                 default:
538                         break;
539                 }
540                 
541                 float speed = 1.0f;
542                 AudioTimeAxisView *atav;
543
544                 if ( ontrack != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(ontrack)) != 0 ) {
545                         if (atav->get_diskstream() != 0) {
546                                 speed = atav->get_diskstream()->speed();
547                         }
548                 }
549
550                 rpos = (jack_nframes_t) floor ( (float) rpos / speed );
551
552                 if (region_boundary_cache.empty() || rpos != region_boundary_cache.back()) {
553                         if (snap_type == SnapToRegionBoundary) {
554                                 region_boundary_cache.push_back (r->first_frame());
555                         }
556                         region_boundary_cache.push_back (rpos);
557                 }
558
559                 pos = rpos + 1;
560         }
561 }
562
563 Region*
564 Editor::find_next_region (jack_nframes_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
565 {
566         TrackViewList::iterator i;
567         jack_nframes_t closest = max_frames;
568         Region* ret = 0;
569         jack_nframes_t rpos = 0;
570
571         float track_speed;
572         jack_nframes_t track_frame;
573         AudioTimeAxisView *atav;
574
575         for (i = tracks.begin(); i != tracks.end(); ++i) {
576
577                 jack_nframes_t distance;
578                 Region* r;
579
580                 track_speed = 1.0f;
581                 if ( (atav = dynamic_cast<AudioTimeAxisView*>(*i)) != 0 ) {
582                         if (atav->get_diskstream()!=0)
583                                 track_speed = atav->get_diskstream()->speed();
584                 }
585
586                 track_frame = (jack_nframes_t) floor ( (float) frame * track_speed );
587
588                 if ((r = (*i)->find_next_region (track_frame, point, dir)) == 0) {
589                         continue;
590                 }
591
592                 switch (point) {
593                 case Start:
594                         rpos = r->first_frame ();
595                         break;
596
597                 case End:
598                         rpos = r->last_frame ();
599                         break;
600
601                 case SyncPoint:
602                         rpos = r->adjust_to_sync (r->first_frame());
603                         break;
604                 }
605                 // rpos is a "track frame", converting it to "time frame"
606                 rpos = (jack_nframes_t) floor ( (float) rpos / track_speed );
607
608                 if (rpos > frame) {
609                         distance = rpos - frame;
610                 } else {
611                         distance = frame - rpos;
612                 }
613
614                 if (distance < closest) {
615                         closest = distance;
616                         if (ontrack != 0)
617                                 *ontrack = (*i);
618                         ret = r;
619                 }
620         }
621
622         return ret;
623 }
624
625 void
626 Editor::cursor_to_region_point (Cursor* cursor, RegionPoint point, int32_t dir)
627 {
628         Region* r;
629         jack_nframes_t pos = cursor->current_frame;
630
631         if (!session) {
632                 return;
633         }
634
635         TimeAxisView *ontrack = 0;
636
637         // so we don't find the current region again..
638         if (dir>0 || pos>0)
639                 pos+=dir;
640
641         if (!selection->tracks.empty()) {
642                 
643                 r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
644                 
645         } else if (clicked_trackview) {
646                 
647                 TrackViewList t;
648                 t.push_back (clicked_trackview);
649                 
650                 r = find_next_region (pos, point, dir, t, &ontrack);
651                 
652         } else {
653                 
654                 r = find_next_region (pos, point, dir, track_views, &ontrack);
655         }
656
657         if (r == 0) {
658                 return;
659         }
660         
661         switch (point){
662         case Start:
663                 pos = r->first_frame ();
664                 break;
665
666         case End:
667                 pos = r->last_frame ();
668                 break;
669
670         case SyncPoint:
671                 pos = r->adjust_to_sync (r->first_frame());
672                 break;  
673         }
674         
675         float speed = 1.0f;
676         AudioTimeAxisView *atav;
677
678         if ( ontrack != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(ontrack)) != 0 ) {
679                 if (atav->get_diskstream() != 0) {
680                         speed = atav->get_diskstream()->speed();
681                 }
682         }
683
684         pos = (jack_nframes_t) floor ( (float) pos / speed );
685         
686         
687         if (cursor == playhead_cursor) {
688                 session->request_locate (pos);
689         } else {
690                 cursor->set_position (pos);
691         }
692 }
693
694 void
695 Editor::cursor_to_next_region_point (Cursor* cursor, RegionPoint point)
696 {
697         cursor_to_region_point (cursor, point, 1);
698 }
699
700 void
701 Editor::cursor_to_previous_region_point (Cursor* cursor, RegionPoint point)
702 {
703         cursor_to_region_point (cursor, point, -1);
704 }
705
706 void
707 Editor::cursor_to_selection_start (Cursor *cursor)
708 {
709         jack_nframes_t pos = 0;
710         switch (mouse_mode) {
711         case MouseObject:
712                 if (!selection->audio_regions.empty()) {
713                         pos = selection->audio_regions.start();
714                 }
715                 break;
716
717         case MouseRange:
718                 if (!selection->time.empty()) {
719                         pos = selection->time.start ();
720                 }
721                 break;
722
723         default:
724                 return;
725         }
726
727         if (cursor == playhead_cursor) {
728                 session->request_locate (pos);
729         } else {
730                 cursor->set_position (pos);
731         }
732 }
733
734 void
735 Editor::cursor_to_selection_end (Cursor *cursor)
736 {
737         jack_nframes_t pos = 0;
738
739         switch (mouse_mode) {
740         case MouseObject:
741                 if (!selection->audio_regions.empty()) {
742                         pos = selection->audio_regions.end_frame();
743                 }
744                 break;
745
746         case MouseRange:
747                 if (!selection->time.empty()) {
748                         pos = selection->time.end_frame ();
749                 }
750                 break;
751
752         default:
753                 return;
754         }
755
756         if (cursor == playhead_cursor) {
757                 session->request_locate (pos);
758         } else {
759                 cursor->set_position (pos);
760         }
761 }
762
763 void
764 Editor::playhead_backward ()
765 {
766         jack_nframes_t pos;
767         jack_nframes_t cnt;
768         float prefix;
769         bool was_floating;
770
771         if (get_prefix (prefix, was_floating)) {
772                 cnt = 1;
773         } else {
774                 if (was_floating) {
775                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
776                 } else {
777                         cnt = (jack_nframes_t) prefix;
778                 }
779         }
780
781         pos = playhead_cursor->current_frame;
782
783         if ((jack_nframes_t) pos < cnt) {
784                 pos = 0;
785         } else {
786                 pos -= cnt;
787         }
788         
789         /* XXX this is completely insane. with the current buffering
790            design, we'll force a complete track buffer flush and
791            reload, just to move 1 sample !!!
792         */
793
794         session->request_locate (pos);
795 }
796
797 void
798 Editor::playhead_forward ()
799 {
800         jack_nframes_t pos;
801         jack_nframes_t cnt;
802         bool was_floating;
803         float prefix;
804
805         if (get_prefix (prefix, was_floating)) {
806                 cnt = 1;
807         } else {
808                 if (was_floating) {
809                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
810                 } else {
811                         cnt = (jack_nframes_t) floor (prefix);
812                 }
813         }
814
815         pos = playhead_cursor->current_frame;
816         
817         /* XXX this is completely insane. with the current buffering
818            design, we'll force a complete track buffer flush and
819            reload, just to move 1 sample !!!
820         */
821
822         session->request_locate (pos+cnt);
823 }
824
825 void
826 Editor::cursor_align (bool playhead_to_edit)
827 {
828         if (playhead_to_edit) {
829                 if (session) {
830                         session->request_locate (edit_cursor->current_frame);
831                 }
832         } else {
833                 edit_cursor->set_position (playhead_cursor->current_frame);
834         }
835 }
836
837 void
838 Editor::edit_cursor_backward ()
839 {
840         jack_nframes_t pos;
841         jack_nframes_t cnt;
842         float prefix;
843         bool was_floating;
844
845         if (get_prefix (prefix, was_floating)) {
846                 cnt = 1;
847         } else {
848                 if (was_floating) {
849                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
850                 } else {
851                         cnt = (jack_nframes_t) prefix;
852                 }
853         }
854
855         pos = edit_cursor->current_frame;
856
857         if ((jack_nframes_t) pos < cnt) {
858                 pos = 0;
859         } else {
860                 pos -= cnt;
861         }
862         
863         edit_cursor->set_position (pos);
864 }
865
866 void
867 Editor::edit_cursor_forward ()
868 {
869         jack_nframes_t pos;
870         jack_nframes_t cnt;
871         bool was_floating;
872         float prefix;
873
874         if (get_prefix (prefix, was_floating)) {
875                 cnt = 1;
876         } else {
877                 if (was_floating) {
878                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
879                 } else {
880                         cnt = (jack_nframes_t) floor (prefix);
881                 }
882         }
883
884         pos = edit_cursor->current_frame;
885         edit_cursor->set_position (pos+cnt);
886 }
887
888 void
889 Editor::goto_frame ()
890 {
891         float prefix;
892         bool was_floating;
893         jack_nframes_t frame;
894
895         if (get_prefix (prefix, was_floating)) {
896                 return;
897         }
898
899         if (was_floating) {
900                 frame = (jack_nframes_t) floor (prefix * session->frame_rate());
901         } else {
902                 frame = (jack_nframes_t) floor (prefix);
903         }
904
905         session->request_locate (frame);
906 }
907
908 void
909 Editor::scroll_backward (float pages)
910 {
911         jack_nframes_t frame;
912         jack_nframes_t one_page = (jack_nframes_t) rint (canvas_width * frames_per_unit);
913         bool was_floating;
914         float prefix;
915         jack_nframes_t cnt;
916         
917         if (get_prefix (prefix, was_floating)) {
918                 cnt = (jack_nframes_t) floor (pages * one_page);
919         } else {
920                 if (was_floating) {
921                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate());
922                 } else {
923                         cnt = (jack_nframes_t) floor (prefix * one_page);
924                 }
925         }
926
927         if (leftmost_frame < cnt) {
928                 frame = 0;
929         } else {
930                 frame = leftmost_frame - cnt;
931         }
932
933         reposition_x_origin (frame);
934 }
935
936 void
937 Editor::scroll_forward (float pages)
938 {
939         jack_nframes_t frame;
940         jack_nframes_t one_page = (jack_nframes_t) rint (canvas_width * frames_per_unit);
941         bool was_floating;
942         float prefix;
943         jack_nframes_t cnt;
944         
945         if (get_prefix (prefix, was_floating)) {
946                 cnt = (jack_nframes_t) floor (pages * one_page);
947         } else {
948                 if (was_floating) {
949                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate());
950                 } else {
951                         cnt = (jack_nframes_t) floor (prefix * one_page);
952                 }
953         }
954
955         if (ULONG_MAX - cnt < leftmost_frame) {
956                 frame = ULONG_MAX - cnt;
957         } else {
958                 frame = leftmost_frame + cnt;
959         }
960
961         reposition_x_origin (frame);
962 }
963
964 void
965 Editor::scroll_tracks_down ()
966 {
967         float prefix;
968         bool was_floating;
969         int cnt;
970
971         if (get_prefix (prefix, was_floating)) {
972                 cnt = 1;
973         } else {
974                 cnt = (int) floor (prefix);
975         }
976
977         Gtk::Adjustment *adj = track_canvas_scroller.get_vadjustment();
978         adj->set_value (adj->get_value() + (cnt * adj->get_page_size()));
979 }
980
981 void
982 Editor::scroll_tracks_up ()
983 {
984         float prefix;
985         bool was_floating;
986         int cnt;
987
988         if (get_prefix (prefix, was_floating)) {
989                 cnt = 1;
990         } else {
991                 cnt = (int) floor (prefix);
992         }
993
994         Gtk::Adjustment *adj = track_canvas_scroller.get_vadjustment();
995         adj->set_value (adj->get_value() - (cnt * adj->get_page_size()));
996 }
997
998 void
999 Editor::scroll_tracks_down_line ()
1000 {
1001         edit_vscrollbar.default_vmotion (0, 10);
1002 }
1003
1004 void
1005 Editor::scroll_tracks_up_line ()
1006 {
1007         edit_vscrollbar.default_vmotion (0, -10);
1008 }
1009
1010 /* ZOOM */
1011
1012 void
1013 Editor::temporal_zoom_step (bool coarser)
1014 {
1015         double nfpu;
1016
1017         nfpu = frames_per_unit;
1018         
1019         if (coarser) { 
1020                 nfpu *= 2.0;
1021         } else { 
1022                 nfpu = max(1.0,(nfpu/2.0));
1023         }
1024
1025         temporal_zoom (nfpu);
1026 }       
1027
1028 void
1029 Editor::temporal_zoom (gdouble fpu)
1030 {
1031         if (!session) return;
1032         
1033         jack_nframes_t current_page = current_page_frames();
1034         jack_nframes_t current_leftmost = leftmost_frame;
1035         jack_nframes_t current_rightmost;
1036         jack_nframes_t current_center;
1037         jack_nframes_t new_page;
1038         jack_nframes_t leftmost_after_zoom = 0;
1039         double nfpu;
1040
1041         nfpu = fpu;
1042         
1043         new_page = (jack_nframes_t) floor (canvas_width * nfpu);
1044
1045         switch (zoom_focus) {
1046         case ZoomFocusLeft:
1047                 leftmost_after_zoom = current_leftmost;
1048                 break;
1049                 
1050         case ZoomFocusRight:
1051                 current_rightmost = leftmost_frame + current_page;
1052                 if (current_rightmost > new_page) {
1053                         leftmost_after_zoom = current_rightmost - new_page;
1054                 } else {
1055                         leftmost_after_zoom = 0;
1056                 }
1057                 break;
1058                 
1059         case ZoomFocusCenter:
1060                 current_center = current_leftmost + (current_page/2); 
1061                 if (current_center > (new_page/2)) {
1062                         leftmost_after_zoom = current_center - (new_page / 2);
1063                 } else {
1064                         leftmost_after_zoom = 0;
1065                 }
1066                 break;
1067                 
1068         case ZoomFocusPlayhead:
1069                 /* try to keep the playhead in the center */
1070                 if (playhead_cursor->current_frame > new_page/2) {
1071                         leftmost_after_zoom = playhead_cursor->current_frame - (new_page/2);
1072                 } else {
1073                         leftmost_after_zoom = 0;
1074                 }
1075                 break;
1076
1077         case ZoomFocusEdit:
1078                 /* try to keep the edit cursor in the center */
1079                 if (edit_cursor->current_frame > leftmost_frame + (new_page/2)) {
1080                         leftmost_after_zoom = edit_cursor->current_frame - (new_page/2);
1081                 } else {
1082                         leftmost_after_zoom = 0;
1083                 }
1084                 break;
1085                 
1086         }
1087  
1088         // leftmost_after_zoom = min (leftmost_after_zoom, session->current_end_frame());
1089
1090 //      begin_reversible_command (_("zoom"));
1091 //      session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), current_leftmost, frames_per_unit));
1092 //      session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_after_zoom, nfpu));
1093 //      commit_reversible_command ();
1094
1095         reposition_and_zoom (leftmost_after_zoom, nfpu);
1096 }       
1097
1098 void
1099 Editor::temporal_zoom_selection ()
1100 {
1101         if (!selection) return;
1102         
1103         if (selection->time.empty()) {
1104                 return;
1105         }
1106
1107         jack_nframes_t start = selection->time[clicked_selection].start;
1108         jack_nframes_t end = selection->time[clicked_selection].end;
1109
1110         temporal_zoom_by_frame (start, end, "zoom to selection");
1111 }
1112
1113 void
1114 Editor::temporal_zoom_session ()
1115 {
1116         if (session) {
1117                 temporal_zoom_by_frame (0, session->current_end_frame(), "zoom to session");
1118         }
1119 }
1120
1121 void
1122 Editor::temporal_zoom_by_frame (jack_nframes_t start, jack_nframes_t end, string op)
1123 {
1124         if (!session) return;
1125
1126         if ((start == 0 && end == 0) || end < start) {
1127                 return;
1128         }
1129
1130         jack_nframes_t range = end - start;
1131
1132         double new_fpu = (double)range / (double)canvas_width;
1133 //      double p2 = 1.0;
1134
1135 //      while (p2 < new_fpu) {
1136 //              p2 *= 2.0;
1137 //      }
1138 //      new_fpu = p2;
1139         
1140         jack_nframes_t new_page = (jack_nframes_t) floor (canvas_width * new_fpu);
1141         jack_nframes_t middle = (jack_nframes_t) floor( (double)start + ((double)range / 2.0f ));
1142         jack_nframes_t new_leftmost = (jack_nframes_t) floor( (double)middle - ((double)new_page/2.0f));
1143
1144         if (new_leftmost > middle) new_leftmost = 0;
1145
1146 //      begin_reversible_command (op);
1147 //      session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
1148 //      session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
1149 //      commit_reversible_command ();
1150
1151         reposition_and_zoom (new_leftmost, new_fpu);
1152 }
1153
1154 void 
1155 Editor::temporal_zoom_to_frame (bool coarser, jack_nframes_t frame)
1156 {
1157         if (!session) return;
1158         
1159         jack_nframes_t range_before = frame - leftmost_frame;
1160         double new_fpu;
1161         
1162         new_fpu = frames_per_unit;
1163         
1164         if (coarser) { 
1165                 new_fpu *= 2.0;
1166                 range_before *= 2;
1167         } else { 
1168                 new_fpu = max(1.0,(new_fpu/2.0));
1169                 range_before /= 2;
1170         }
1171
1172         if (new_fpu == frames_per_unit) return;
1173
1174         jack_nframes_t new_leftmost = frame - range_before;
1175
1176         if (new_leftmost > frame) new_leftmost = 0;
1177
1178 //      begin_reversible_command (_("zoom to frame"));
1179 //      session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
1180 //      session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
1181 //      commit_reversible_command ();
1182
1183         reposition_and_zoom (new_leftmost, new_fpu);
1184 }
1185
1186 void
1187 Editor::select_all_in_track (bool add)
1188 {
1189         list<Selectable *> touched;
1190
1191         if (!clicked_trackview) {
1192                 return;
1193         }
1194         
1195         clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
1196
1197         if (add) {
1198                 selection->add (touched);
1199         } else {
1200                 selection->set (touched);
1201         }
1202 }
1203
1204 void
1205 Editor::select_all (bool add)
1206 {
1207         list<Selectable *> touched;
1208         
1209         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1210                 if ((*iter)->hidden()) {
1211                         continue;
1212                 }
1213                 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
1214         }
1215
1216         if (add) {
1217                 selection->add (touched);
1218         } else {
1219                 selection->set (touched);
1220         }
1221
1222 }
1223
1224 void
1225 Editor::invert_selection_in_track ()
1226 {
1227         list<Selectable *> touched;
1228
1229         if (!clicked_trackview) {
1230                 return;
1231         }
1232         
1233         clicked_trackview->get_inverted_selectables (*selection, touched);
1234         selection->set (touched);
1235 }
1236
1237 void
1238 Editor::invert_selection ()
1239 {
1240         list<Selectable *> touched;
1241         
1242         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1243                 if ((*iter)->hidden()) {
1244                         continue;
1245                 }
1246                 (*iter)->get_inverted_selectables (*selection, touched);
1247         }
1248
1249         selection->set (touched);
1250 }
1251
1252 bool
1253 Editor::select_all_within (jack_nframes_t start, jack_nframes_t end, double top, double bot, bool add)
1254 {
1255         list<Selectable *> touched;
1256         
1257         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1258                 if ((*iter)->hidden()) {
1259                         continue;
1260                 }
1261                 (*iter)->get_selectables (start, end, top, bot, touched);
1262         }
1263
1264         if (add) {
1265                 selection->add (touched);
1266         } else {
1267                 selection->set (touched);
1268         }
1269
1270         return !touched.empty();
1271 }
1272
1273 void
1274 Editor::set_selection_from_punch()
1275 {
1276         Location* location;
1277
1278         if ((location = session->locations()->auto_punch_location()) == 0)  {
1279                 return;
1280         }
1281
1282         set_selection_from_range (*location);
1283 }
1284
1285 void
1286 Editor::set_selection_from_loop()
1287 {
1288         Location* location;
1289
1290         if ((location = session->locations()->auto_loop_location()) == 0)  {
1291                 return;
1292         }
1293
1294         set_selection_from_range (*location);
1295 }
1296
1297 void
1298 Editor::set_selection_from_range (Location& range)
1299 {
1300         if (clicked_trackview == 0) {
1301                 return;
1302         }
1303         
1304         begin_reversible_command (_("set selection from range"));
1305         selection->set (0, range.start(), range.end());
1306         commit_reversible_command ();
1307 }
1308
1309 void
1310 Editor::amplitude_zoom_step (bool in)
1311 {
1312         gdouble zoom = 1.0;
1313
1314         if (in) {
1315                 zoom *= 2.0;
1316         } else {
1317                 if (zoom > 2.0) {
1318                         zoom /= 2.0;
1319                 } else {
1320                         zoom = 1.0;
1321                 }
1322         }
1323
1324 #ifdef FIX_FOR_CANVAS
1325         /* XXX DO SOMETHING */
1326 #endif
1327 }       
1328
1329
1330 /* DELETION */
1331
1332
1333 void
1334 Editor::delete_sample_forward ()
1335 {
1336 }
1337
1338 void
1339 Editor::delete_sample_backward ()
1340 {
1341 }
1342
1343 void
1344 Editor::delete_screen ()
1345 {
1346 }
1347
1348 /* SEARCH */
1349
1350 void
1351 Editor::search_backwards ()
1352 {
1353         /* what ? */
1354 }
1355
1356 void
1357 Editor::search_forwards ()
1358 {
1359         /* what ? */
1360 }
1361
1362 /* MARKS */
1363
1364 void
1365 Editor::jump_forward_to_mark ()
1366 {
1367         if (!session) {
1368                 return;
1369         }
1370         
1371         Location *location = session->locations()->first_location_after (playhead_cursor->current_frame);
1372
1373         if (location) {
1374                 session->request_locate (location->start(), session->transport_rolling());
1375         } else {
1376                 session->request_locate (session->current_end_frame());
1377         }
1378 }
1379
1380 void
1381 Editor::jump_backward_to_mark ()
1382 {
1383         if (!session) {
1384                 return;
1385         }
1386
1387         Location *location = session->locations()->first_location_before (playhead_cursor->current_frame);
1388         
1389         if (location) {
1390                 session->request_locate (location->start(), session->transport_rolling());
1391         } else {
1392                 session->request_locate (0);
1393         }
1394 }
1395
1396 void
1397 Editor::set_mark ()
1398 {
1399         jack_nframes_t pos;
1400         float prefix;
1401         bool was_floating;
1402
1403         if (get_prefix (prefix, was_floating)) {
1404                 pos = session->audible_frame ();
1405         } else {
1406                 if (was_floating) {
1407                         pos = (jack_nframes_t) floor (prefix * session->frame_rate ());
1408                 } else {
1409                         pos = (jack_nframes_t) floor (prefix);
1410                 }
1411         }
1412
1413         session->locations()->add (new Location (pos, 0, "mark", Location::IsMark), true);
1414 }
1415
1416 void
1417 Editor::clear_markers ()
1418 {
1419         if (session) {
1420                 session->begin_reversible_command (_("clear markers"));
1421                 session->add_undo (session->locations()->get_memento());
1422                 session->locations()->clear_markers ();
1423                 session->add_redo_no_execute (session->locations()->get_memento());
1424                 session->commit_reversible_command ();
1425         }
1426 }
1427
1428 void
1429 Editor::clear_ranges ()
1430 {
1431         if (session) {
1432                 session->begin_reversible_command (_("clear ranges"));
1433                 session->add_undo (session->locations()->get_memento());
1434                 
1435                 Location * looploc = session->locations()->auto_loop_location();
1436                 Location * punchloc = session->locations()->auto_punch_location();
1437                 
1438                 session->locations()->clear_ranges ();
1439                 // re-add these
1440                 if (looploc) session->locations()->add (looploc);
1441                 if (punchloc) session->locations()->add (punchloc);
1442                 
1443                 session->add_redo_no_execute (session->locations()->get_memento());
1444                 session->commit_reversible_command ();
1445         }
1446 }
1447
1448 void
1449 Editor::clear_locations ()
1450 {
1451         session->begin_reversible_command (_("clear locations"));
1452         session->add_undo (session->locations()->get_memento());
1453         session->locations()->clear ();
1454         session->add_redo_no_execute (session->locations()->get_memento());
1455         session->commit_reversible_command ();
1456         session->locations()->clear ();
1457 }
1458
1459 /* INSERT/REPLACE */
1460
1461 void
1462 Editor::insert_region_list_drag (AudioRegion& region)
1463 {
1464         gint x, y;
1465         double wx, wy;
1466         double cx, cy;
1467         TimeAxisView *tv;
1468         jack_nframes_t where;
1469         AudioTimeAxisView *atv = 0;
1470         Playlist *playlist;
1471         
1472         track_canvas->get_pointer (x, y);
1473
1474         gnome_canvas_window_to_world (GNOME_CANVAS(track_gnome_canvas), x, y, &wx, &wy);
1475         
1476         GdkEvent event;
1477         event.type = GDK_BUTTON_RELEASE;
1478         event.button.x = wx;
1479         event.button.y = wy;
1480         
1481         where = event_frame (&event, &cx, &cy);
1482
1483         if (where < leftmost_frame || where > leftmost_frame + current_page_frames()) {
1484                 /* clearly outside canvas area */
1485                 return;
1486         }
1487         
1488         if ((tv = trackview_by_y_position (cy)) == 0) {
1489                 return;
1490         }
1491         
1492         if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) == 0) {
1493                 return;
1494         }
1495
1496         if ((playlist = atv->playlist()) == 0) {
1497                 return;
1498         }
1499         
1500         snap_to (where);
1501         
1502         begin_reversible_command (_("insert dragged region"));
1503         session->add_undo (playlist->get_memento());
1504         playlist->add_region (*(new AudioRegion (region)), where, 1.0);
1505         session->add_redo_no_execute (playlist->get_memento());
1506         commit_reversible_command ();
1507 }
1508
1509 void
1510 Editor::insert_region_list_selection (float times)
1511 {
1512         AudioTimeAxisView *tv = 0;
1513         Playlist *playlist;
1514
1515         if (clicked_audio_trackview != 0) {
1516                 tv = clicked_audio_trackview;
1517         } else if (!selection->tracks.empty()) {
1518                 if ((tv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front())) == 0) {
1519                         return;
1520                 }
1521         } else {
1522                 return;
1523         }
1524
1525         if ((playlist = tv->playlist()) == 0) {
1526                 return;
1527         }
1528         
1529         Gtk::CTree_Helpers::SelectionList& selected = region_list_display.selection();
1530         
1531         if (selected.empty()) {
1532                 return;
1533         }
1534         
1535         Region* region = reinterpret_cast<Region *> (selected.front().get_data ());
1536
1537         begin_reversible_command (_("insert region"));
1538         session->add_undo (playlist->get_memento());
1539         playlist->add_region (*(createRegion (*region)), edit_cursor->current_frame, times);
1540         session->add_redo_no_execute (playlist->get_memento());
1541         commit_reversible_command ();
1542 }
1543
1544
1545 /* BUILT-IN EFFECTS */
1546
1547 void
1548 Editor::reverse_selection ()
1549 {
1550
1551 }
1552
1553 /* GAIN ENVELOPE EDITING */
1554
1555 void
1556 Editor::edit_envelope ()
1557 {
1558 }
1559
1560 /* PLAYBACK */
1561
1562 void
1563 Editor::toggle_playback (bool with_abort)
1564 {
1565         if (!session) {
1566                 return;
1567         }
1568
1569         switch (session->slave_source()) {
1570         case Session::None:
1571         case Session::JACK:
1572                 break;
1573         default:
1574                 /* transport controlled by the master */
1575                 return;
1576         }
1577
1578         if (session->is_auditioning()) {
1579                 session->cancel_audition ();
1580                 return;
1581         }
1582         
1583         if (session->transport_rolling()) {
1584                 session->request_stop (with_abort);
1585                 if (session->get_auto_loop()) {
1586                         session->request_auto_loop (false);
1587                 }
1588         } else {
1589                 session->request_transport_speed (1.0f);
1590         }
1591 }
1592
1593 void
1594 Editor::play_from_start ()
1595 {
1596         session->request_locate (0, true);
1597 }
1598
1599 void
1600 Editor::play_selection ()
1601 {
1602         if (selection->time.empty()) {
1603                 return;
1604         }
1605
1606         session->request_play_range (true);
1607 }
1608
1609 void
1610 Editor::play_selected_region ()
1611 {
1612         if (!selection->audio_regions.empty()) {
1613                 AudioRegionView *rv = *(selection->audio_regions.begin());
1614
1615                 session->request_bounded_roll (rv->region.position(), rv->region.last_frame()); 
1616         }
1617 }
1618
1619 void
1620 Editor::toggle_loop_playback ()
1621 {
1622         if (session) {
1623                 session->request_auto_loop (true);
1624         }
1625 }
1626
1627 void
1628 Editor::loop_selected_region ()
1629 {
1630         if (!selection->audio_regions.empty()) {
1631                 AudioRegionView *rv = *(selection->audio_regions.begin());
1632                 Location* tll;
1633
1634                 if ((tll = transport_loop_location()) != 0)  {
1635
1636                         tll->set (rv->region.position(), rv->region.last_frame());
1637                         
1638                         // enable looping, reposition and start rolling
1639
1640                         session->request_auto_loop (true);
1641                         session->request_locate (tll->start(), false);
1642                         session->request_transport_speed (1.0f);
1643                 }
1644         }
1645 }
1646
1647 void
1648 Editor::play_location (Location& location)
1649 {
1650         if (location.start() <= location.end()) {
1651                 return;
1652         }
1653
1654         session->request_bounded_roll (location.start(), location.end());
1655 }
1656
1657 void
1658 Editor::loop_location (Location& location)
1659 {
1660         if (location.start() <= location.end()) {
1661                 return;
1662         }
1663
1664         Location* tll;
1665
1666         if ((tll = transport_loop_location()) != 0) {
1667                 tll->set (location.start(), location.end());
1668
1669                 // enable looping, reposition and start rolling
1670                 session->request_auto_loop (true);
1671                 session->request_locate (tll->start(), true);
1672         }
1673 }
1674
1675 void 
1676 Editor::toggle_region_mute ()
1677 {
1678         if (clicked_regionview) {
1679                 clicked_regionview->region.set_muted (!clicked_regionview->region.muted());
1680         } else if (!selection->audio_regions.empty()) {
1681                 bool yn = ! (*selection->audio_regions.begin())->region.muted();
1682                 selection->foreach_audio_region (&AudioRegion::set_muted, yn);
1683         }
1684 }
1685
1686 void
1687 Editor::toggle_region_opaque ()
1688 {
1689         if (clicked_regionview) {
1690                 clicked_regionview->region.set_opaque (!clicked_regionview->region.opaque());
1691         } else if (!selection->audio_regions.empty()) {
1692                 bool yn = ! (*selection->audio_regions.begin())->region.opaque();
1693                 selection->foreach_audio_region (&Region::set_opaque, yn);
1694         }
1695 }
1696
1697 void
1698 Editor::raise_region ()
1699 {
1700         selection->foreach_audio_region (&Region::raise);
1701 }
1702
1703 void
1704 Editor::raise_region_to_top ()
1705 {
1706         selection->foreach_audio_region (&Region::raise_to_top);
1707 }
1708
1709 void
1710 Editor::lower_region ()
1711 {
1712         selection->foreach_audio_region (&Region::lower);
1713 }
1714
1715 void
1716 Editor::lower_region_to_bottom ()
1717 {
1718         selection->foreach_audio_region (&Region::lower_to_bottom);
1719 }
1720
1721 void
1722 Editor::edit_region ()
1723 {
1724         if (clicked_regionview == 0) {
1725                 return;
1726         }
1727         
1728         clicked_regionview->show_region_editor ();
1729 }
1730
1731 void
1732 Editor::rename_region ()
1733 {
1734         Dialog dialog;
1735         Entry  entry;
1736         Button ok_button (_("OK"));
1737         Button cancel_button (_("Cancel"));
1738
1739         if (selection->audio_regions.empty()) {
1740                 return;
1741         }
1742
1743         dialog.set_title (_("ardour: rename region"));
1744         dialog.set_name ("RegionRenameWindow");
1745         dialog.set_size_request (300, -1);
1746         dialog.set_position (Gtk::WIN_POS_MOUSE);
1747         dialog.set_modal (true);
1748
1749         dialog.get_vbox()->set_border_width (10);
1750         dialog.get_vbox()->pack_start (entry);
1751         dialog.get_action_area()->pack_start (ok_button);
1752         dialog.get_action_area()->pack_start (cancel_button);
1753
1754         entry.set_name ("RegionNameDisplay");
1755         ok_button.set_name ("EditorGTKButton");
1756         cancel_button.set_name ("EditorGTKButton");
1757
1758         region_renamed = false;
1759
1760         entry.signal_activate().connect (bind (mem_fun(*this, &Editor::rename_region_finished), true));
1761         ok_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::rename_region_finished), true));
1762         cancel_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::rename_region_finished), false));
1763
1764         /* recurse */
1765
1766         dialog.show_all ();
1767         ARDOUR_UI::instance()->allow_focus (true);
1768         Main::run ();
1769         ARDOUR_UI::instance()->allow_focus (false);
1770
1771         if (region_renamed) {
1772                 (*selection->audio_regions.begin())->region.set_name (entry.get_text());
1773                 redisplay_regions ();
1774         }
1775 }
1776
1777 void
1778 Editor::rename_region_finished (bool status)
1779
1780 {
1781         region_renamed = status;
1782         Main::quit ();
1783 }
1784
1785 void
1786 Editor::audition_playlist_region_via_route (AudioRegion& region, Route& route)
1787 {
1788         if (session->is_auditioning()) {
1789                 session->cancel_audition ();
1790         } 
1791
1792         // note: some potential for creativity here, because region doesn't
1793         // have to belong to the playlist that Route is handling
1794
1795         // bool was_soloed = route.soloed();
1796
1797         route.set_solo (true, this);
1798         
1799         session->request_bounded_roll (region.position(), region.position() + region.length());
1800         
1801         /* XXX how to unset the solo state ? */
1802 }
1803
1804 void
1805 Editor::audition_selected_region ()
1806 {
1807         if (!selection->audio_regions.empty()) {
1808                 AudioRegionView* rv = *(selection->audio_regions.begin());
1809                 session->audition_region (rv->region);
1810         }
1811 }
1812
1813 void
1814 Editor::audition_playlist_region_standalone (AudioRegion& region)
1815 {
1816         session->audition_region (region);
1817 }
1818
1819 void
1820 Editor::build_interthread_progress_window ()
1821 {
1822         interthread_progress_window = new ArdourDialog (X_("interthread progress"));
1823
1824         interthread_progress_bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
1825         
1826         interthread_progress_vbox.set_border_width (10);
1827         interthread_progress_vbox.set_spacing (5);
1828         interthread_progress_vbox.pack_start (interthread_progress_label, false, false);
1829         interthread_progress_vbox.pack_start (interthread_progress_bar,false, false);
1830         interthread_progress_vbox.pack_start (interthread_cancel_button,false, false);
1831
1832         interthread_cancel_button.add (interthread_cancel_label);
1833
1834         interthread_cancel_button.signal_clicked().connect (mem_fun(*this, &Editor::interthread_cancel_clicked));
1835         
1836         interthread_progress_window->set_modal (true);
1837         interthread_progress_window->set_default_size (200, 100);
1838         interthread_progress_window->add (interthread_progress_vbox);
1839 }
1840
1841 void
1842 Editor::interthread_cancel_clicked ()
1843 {
1844         if (current_interthread_info) {
1845                 current_interthread_info->cancel = true;
1846         }
1847 }
1848
1849 void *
1850 Editor::_import_thread (void *arg)
1851 {
1852         PBD::ThreadCreated (pthread_self(), X_("Import"));
1853
1854         Editor *ed = (Editor *) arg;
1855         return ed->import_thread ();
1856 }
1857
1858 void *
1859 Editor::import_thread ()
1860 {
1861         session->import_audiofile (import_status);
1862         return 0;
1863 }
1864
1865 gint
1866 Editor::import_progress_timeout (void *arg)
1867 {
1868         interthread_progress_label.set_text (import_status.doing_what);
1869
1870         if (import_status.freeze) {
1871                 interthread_cancel_button.set_sensitive(false);
1872         } else {
1873                 interthread_cancel_button.set_sensitive(true);
1874         }
1875
1876         if (import_status.doing_what == "building peak files") {
1877                 interthread_progress_bar.set_activity_mode (true);
1878                 return FALSE;
1879         } else {
1880                 interthread_progress_bar.set_fraction (import_status.progress/100);
1881         }
1882
1883         return !(import_status.done || import_status.cancel);
1884 }
1885
1886 void
1887 Editor::import_audio (bool as_tracks)
1888 {
1889         if (session == 0) {
1890                 warning << _("You can't import an audiofile until you have a session loaded.") << endmsg;
1891                 return;
1892         }
1893
1894         SoundFileSelector& sfdb (ARDOUR_UI::instance()->get_sfdb_window());
1895         sigc::connection c;
1896         string str;
1897
1898         if (as_tracks) {
1899                 c = sfdb.Action.connect (bind (mem_fun(*this, &Editor::do_import), true));
1900                 str =_("Import selected as tracks");
1901         } else {
1902                 c = sfdb.Action.connect (bind (mem_fun(*this, &Editor::do_import), false));
1903                 str = _("Import selected to region list");
1904         }
1905
1906         sfdb.run (str, true);
1907         c.disconnect ();
1908 }
1909
1910 void
1911 Editor::catch_new_audio_region (AudioRegion* ar)
1912 {
1913         last_audio_region = ar;
1914 }
1915
1916 void
1917 Editor::do_import (vector<string> paths, bool split, bool as_tracks)
1918 {
1919         sigc::connection c;
1920         
1921         /* SFDB sets "multichan" to true to indicate "split channels"
1922            so reverse the setting to match the way libardour
1923            interprets it.
1924         */
1925         
1926         import_status.multichan = !split;
1927
1928         if (interthread_progress_window == 0) {
1929                 build_interthread_progress_window ();
1930         }
1931         
1932         interthread_progress_window->set_title (_("ardour: audio import in progress"));
1933         interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
1934         interthread_progress_window->show_all ();
1935         interthread_progress_bar.set_fraction (0.0f);
1936         interthread_cancel_label.set_text (_("Cancel Import"));
1937         current_interthread_info = &import_status;
1938
1939         c = session->AudioRegionAdded.connect (mem_fun(*this, &Editor::catch_new_audio_region));
1940
1941         for (vector<string>::iterator i = paths.begin(); i != paths.end(); ++i ) {
1942
1943                 interthread_progress_window->set_title (string_compose (_("ardour: importing %1"), (*i)));
1944         
1945                 import_status.pathname = (*i);
1946                 import_status.done = false;
1947                 import_status.cancel = false;
1948                 import_status.freeze = false;
1949                 import_status.done = 0.0;
1950                 
1951                 interthread_progress_connection = 
1952                   Glib::signal_timeout().connect (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
1953                 
1954                 last_audio_region = 0;
1955                 
1956                 pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
1957                 pthread_detach (import_status.thread);
1958                 
1959                 while (!(import_status.done || import_status.cancel)) {
1960                         gtk_main_iteration ();
1961                 }
1962                 
1963                 import_status.done = true;
1964                 interthread_progress_connection.disconnect ();
1965
1966                 if (as_tracks && last_audio_region != 0) {
1967                         uint32_t channels = last_audio_region->n_channels();
1968
1969                         AudioTrack* at = session->new_audio_track (channels, channels);
1970                         AudioRegion* copy = new AudioRegion (*last_audio_region);
1971                         at->disk_stream().playlist()->add_region (*copy, 0);
1972                 }
1973         }
1974
1975         c.disconnect ();
1976         interthread_progress_window->hide_all ();
1977 }
1978
1979 int
1980 Editor::reject_because_rate_differs (string path, SF_INFO& finfo, string action, bool multiple_pending)
1981 {
1982         if (!session) {
1983                 return 1;
1984         }
1985
1986         if (finfo.samplerate != (int) session->frame_rate()) {
1987                 vector<string> choices;
1988
1989                 choices.push_back (string_compose (_("%1 it anyway"), action));
1990
1991                 if (multiple_pending) {
1992                         /* XXX assumptions about sentence structure
1993                            here for translators. Sorry.
1994                         */
1995                         choices.push_back (string_compose (_("Don't %1 it"), action));
1996                         choices.push_back (string_compose (_("%1 all without questions"), action));
1997                         choices.push_back (_("Cancel entire import"));
1998                 } else {
1999                         choices.push_back (_("Cancel"));
2000                 }
2001
2002                 Gtkmm2ext::Choice rate_choice (
2003                         string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
2004                         choices);
2005
2006                 rate_choice.chosen.connect (Main::quit.slot());
2007                 rate_choice.show_all ();
2008
2009                 Main::run ();
2010
2011                 switch (rate_choice.get_choice()) {
2012                 case 0: /* do it anyway */
2013                         return 0;
2014                 case 1: /* don't import this one */
2015                         return 1;
2016                 case 2: /* do the rest without asking */
2017                         return -1;
2018                 case 3: /* stop a multi-file import */
2019                 default:
2020                         return -2;
2021                 }
2022         }
2023
2024         return 0;
2025 }
2026
2027 void 
2028 Editor::embed_audio ()
2029 {
2030         if (session == 0) {
2031                 warning << _("You can't embed an audiofile until you have a session loaded.") << endmsg;
2032                 return;
2033         }
2034
2035         SoundFileSelector& sfdb (ARDOUR_UI::instance()->get_sfdb_window());
2036         sigc::connection c = sfdb.Action.connect (mem_fun(*this, &Editor::do_embed_sndfiles));
2037
2038         sfdb.run (_("Add to External Region list"), true);
2039
2040         c.disconnect ();
2041 }
2042
2043 void
2044 Editor::do_embed_sndfiles (vector<string> paths, bool split)
2045 {
2046         bool multiple_files = paths.size() > 1;
2047         bool check_sample_rate = true;
2048
2049         for (vector<string>::iterator i = paths.begin(); i != paths.end(); ++i) {
2050                 embed_sndfile (*i, split, multiple_files, check_sample_rate);
2051         }
2052
2053         session->save_state ("");
2054 }
2055
2056 void
2057 Editor::embed_sndfile (string path, bool split, bool multiple_files, bool& check_sample_rate)
2058 {
2059         SndFileSource *source = 0; /* keep g++ quiet */
2060         AudioRegion::SourceList sources;
2061         string idspec;
2062         string linked_path;
2063         SNDFILE *sf;
2064         SF_INFO finfo;
2065
2066         /* lets see if we can link it into the session */
2067         
2068         linked_path = session->sound_dir();
2069         linked_path += PBD::basename (path);
2070
2071         if (link (path.c_str(), linked_path.c_str()) == 0) {
2072
2073                 /* there are many reasons why link(2) might have failed.
2074                    but if it succeeds, we now have a link in the
2075                    session sound dir that will protect against
2076                    unlinking of the original path. nice.
2077                 */
2078
2079                 path = linked_path;
2080         }
2081
2082         memset (&finfo, 0, sizeof(finfo));
2083
2084         /* note that we temporarily truncated _id at the colon */
2085         
2086         if ((sf = sf_open (path.c_str(), SFM_READ, &finfo)) == 0) {
2087                 char errbuf[256];
2088                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
2089                 error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), selection, errbuf) << endmsg;
2090                 return;
2091         }
2092         sf_close (sf);
2093         sf = 0;
2094         
2095         if (check_sample_rate) {
2096                 switch (reject_because_rate_differs (path, finfo, "Embed", multiple_files)) {
2097                 case 0:
2098                         break;
2099                 case 1:
2100                         return;
2101                 case -1:
2102                         check_sample_rate = false;
2103                         break;
2104                         
2105                 case -2:
2106                 default:
2107                         return;
2108                 }
2109         }
2110
2111         track_canvas_scroller.get_window()->set_cursor (GDK_WATCH);
2112         ARDOUR_UI::instance()->flush_pending ();
2113
2114         /* make the proper number of channels in the region */
2115
2116         for (int n=0; n < finfo.channels; ++n)
2117         {
2118                 idspec = path;
2119                 idspec += string_compose(":%1", n);
2120                 
2121                 try {
2122                         source = new SndFileSource (idspec.c_str());
2123                         sources.push_back(source);
2124                 } 
2125
2126                 catch (failed_constructor& err) {
2127                         error << string_compose(_("could not open %1"), path) << endmsg;
2128                         goto out;
2129                 }
2130
2131                 ARDOUR_UI::instance()->flush_pending ();
2132         }
2133
2134         if (sources.size() > 0) {
2135
2136                 string region_name = PBD::basename_nosuffix (path);
2137                 region_name += "-0";
2138
2139                 /* The created region isn't dropped.  It emits a signal
2140                    that is picked up by the session. 
2141                 */
2142
2143                 new AudioRegion (sources, 0, sources[0]->length(), region_name, 0,
2144                                  Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
2145                 
2146                 /* make sure we can see it in the list */
2147
2148                 Gtk::CTree_Helpers::RowList::iterator external_node;
2149                 external_node = region_list_display.rows().begin();
2150                 ++external_node; /* its the second node, always */
2151                 external_node->expand_recursive ();
2152
2153                 ARDOUR_UI::instance()->flush_pending ();
2154         }
2155
2156   out:
2157         track_canvas_scroller.get_window()->set_cursor (current_canvas_cursor);
2158 }
2159
2160 void
2161 Editor::insert_sndfile (bool as_tracks)
2162 {
2163         SoundFileSelector& sfdb (ARDOUR_UI::instance()->get_sfdb_window());
2164         sigc::connection c;
2165         string str;
2166
2167         if (as_tracks) {
2168
2169                 c = sfdb.Action.connect (mem_fun(*this, &Editor::insert_paths_as_new_tracks));
2170                 str = _("Insert selected as new tracks");
2171
2172         } else {
2173
2174                 jack_nframes_t pos;
2175
2176                 if (clicked_audio_trackview == 0) {
2177                         return;
2178                 }
2179
2180                 if (ensure_cursor (&pos)) {
2181                         return;
2182                 }
2183
2184                 c = sfdb.Action.connect (bind (mem_fun(*this, &Editor::do_insert_sndfile), pos));
2185                 str = _("Insert selected");
2186         }
2187
2188         sfdb.run (str, false);
2189         c.disconnect ();
2190 }
2191
2192 void
2193 Editor::insert_paths_as_new_tracks (vector<string> paths, bool split)
2194 {
2195         SNDFILE *sf;
2196         SF_INFO finfo;
2197         bool multiple_files;
2198         bool check_sample_rate = true;
2199
2200         multiple_files = paths.size() > 1;      
2201
2202         for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
2203                 
2204                 memset (&finfo, 0, sizeof(finfo));
2205                 
2206                 if ((sf = sf_open ((*p).c_str(), SFM_READ, &finfo)) == 0) {
2207                         char errbuf[256];
2208                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
2209                         error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), (*p), errbuf) << endmsg;
2210                         continue;
2211                 }
2212                 
2213                 sf_close (sf);
2214                 sf = 0;
2215                 
2216                 /* add a new track */
2217                 
2218                 if (check_sample_rate) {
2219                         switch (reject_because_rate_differs (*p, finfo, "Insert", multiple_files)) {
2220                         case 0:
2221                                 break;
2222                         case 1:
2223                                 continue;
2224                         case -1:
2225                                 check_sample_rate = false;
2226                                 break;
2227                                 
2228                         case -2:
2229                                 return;
2230                         }
2231                 }
2232                 
2233                 uint32_t input_chan = finfo.channels;
2234                 uint32_t output_chan;
2235                 
2236                 if (session->get_output_auto_connect() & Session::AutoConnectMaster) {
2237                         output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
2238                 } else {
2239                         output_chan = input_chan;
2240                 }
2241                 
2242                 (void) session->new_audio_track (input_chan, output_chan);
2243
2244
2245                 /* get the last (most recently added) track view */
2246         
2247                 AudioTimeAxisView* tv;
2248         
2249                 if ((tv = dynamic_cast<AudioTimeAxisView*>(track_views.back())) == 0) {
2250                         fatal << _("programming error: ")
2251                               << X_("last trackview after new_audio_track is not an audio track!")
2252                               << endmsg;
2253                         /*NOTREACHED*/
2254                 }
2255                 
2256                 jack_nframes_t pos = 0;
2257                 insert_sndfile_into (*p, true, tv, pos, false);
2258         }
2259 }
2260
2261 void
2262 Editor::do_insert_sndfile (vector<string> paths, bool split, jack_nframes_t pos)
2263 {
2264         for (vector<string>::iterator x = paths.begin(); x != paths.end(); ++x) {
2265                 insert_sndfile_into (*x, !split, clicked_audio_trackview, pos);
2266         }
2267 }
2268
2269 void
2270 Editor::insert_sndfile_into (string path, bool multi, AudioTimeAxisView* tv, jack_nframes_t& pos, bool prompt)
2271 {
2272         SndFileSource *source = 0; /* keep g++ quiet */
2273         AudioRegion::SourceList sources;
2274         string idspec;
2275         SNDFILE *sf;
2276         SF_INFO finfo;
2277
2278         memset (&finfo, 0, sizeof(finfo));
2279
2280         /* note that we temporarily truncated _id at the colon */
2281         
2282         if ((sf = sf_open (path.c_str(), SFM_READ, &finfo)) == 0) {
2283                 char errbuf[256];
2284                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
2285                 error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), path, errbuf) << endmsg;
2286                 return;
2287         }
2288         sf_close (sf);
2289         sf = 0;
2290         
2291         if (prompt && (reject_because_rate_differs (path, finfo, "Insert", false) != 0)) {
2292                 return;
2293         }
2294
2295         track_canvas_scroller.get_window()->set_cursor (GDK_WATCH);
2296         ARDOUR_UI::instance()->flush_pending ();
2297
2298         /* make the proper number of channels in the region */
2299
2300         for (int n=0; n < finfo.channels; ++n)
2301         {
2302                 idspec = path;
2303                 idspec += string_compose(":%1", n);
2304
2305                 try {
2306                         source = new SndFileSource (idspec.c_str());
2307                         sources.push_back(source);
2308                 } 
2309
2310                 catch (failed_constructor& err) {
2311                         error << string_compose(_("could not open %1"), path) << endmsg;
2312                         goto out;
2313                 }
2314
2315                 ARDOUR_UI::instance()->flush_pending ();
2316         }
2317
2318         if (sources.size() > 0) {
2319
2320                 string region_name = region_name_from_path (PBD::basename (path));
2321                 
2322                 AudioRegion *region = new AudioRegion (sources, 0, sources[0]->length(), region_name, 
2323                                                        0, /* irrelevant these days */
2324                                                        Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
2325
2326                 begin_reversible_command (_("insert sndfile"));
2327                 session->add_undo (tv->playlist()->get_memento());
2328                 tv->playlist()->add_region (*region, pos);
2329                 session->add_redo_no_execute (tv->playlist()->get_memento());
2330                 commit_reversible_command ();
2331                 
2332                 pos += sources[0]->length();
2333
2334                 ARDOUR_UI::instance()->flush_pending ();
2335         }
2336
2337   out:
2338         track_canvas_scroller.get_window()->set_cursor (current_canvas_cursor);
2339         return;
2340 }
2341
2342 void
2343 Editor::region_from_selection ()
2344 {
2345         if (clicked_trackview == 0) {
2346                 return;
2347         }
2348
2349         if (selection->time.empty()) {
2350                 return;
2351         }
2352
2353         jack_nframes_t start = selection->time[clicked_selection].start;
2354         jack_nframes_t end = selection->time[clicked_selection].end;
2355
2356         jack_nframes_t selection_cnt = end - start + 1;
2357         
2358         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
2359
2360                 AudioRegion *region;
2361                 AudioRegion *current;
2362                 Region* current_r;
2363                 Playlist *pl;
2364
2365                 jack_nframes_t internal_start;
2366                 string new_name;
2367
2368                 if ((pl = (*i)->playlist()) == 0) {
2369                         continue;
2370                 }
2371
2372                 if ((current_r = pl->top_region_at (start)) == 0) {
2373                         continue;
2374                 }
2375
2376                 if ((current = dynamic_cast<AudioRegion*> (current_r)) != 0) {
2377                         internal_start = start - current->position();
2378                         session->region_name (new_name, current->name(), true);
2379                         region = new AudioRegion (*current, internal_start, selection_cnt, new_name);
2380                 }
2381         }
2382 }       
2383
2384 void
2385 Editor::create_region_from_selection (vector<AudioRegion *>& new_regions)
2386 {
2387         if (selection->time.empty() || selection->tracks.empty()) {
2388                 return;
2389         }
2390
2391         jack_nframes_t start = selection->time[clicked_selection].start;
2392         jack_nframes_t end = selection->time[clicked_selection].end;
2393         
2394         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
2395
2396                 AudioRegion* current;
2397                 Region* current_r;
2398                 Playlist* playlist;
2399                 jack_nframes_t internal_start;
2400                 string new_name;
2401
2402                 if ((playlist = (*i)->playlist()) == 0) {
2403                         continue;
2404                 }
2405
2406                 if ((current_r = playlist->top_region_at(start)) == 0) {
2407                         continue;
2408                 }
2409
2410                 if ((current = dynamic_cast<AudioRegion*>(current_r)) == 0) {
2411                         continue;
2412                 }
2413         
2414                 internal_start = start - current->position();
2415                 session->region_name (new_name, current->name(), true);
2416                 
2417                 new_regions.push_back (new AudioRegion (*current, internal_start, end - start + 1, new_name));
2418         }
2419 }
2420
2421 void
2422 Editor::split_multichannel_region ()
2423 {
2424         vector<AudioRegion*> v;
2425
2426         if (!clicked_regionview || clicked_regionview->region.n_channels() < 2) {
2427                 return;
2428         }
2429
2430         clicked_regionview->region.separate_by_channel (*session, v);
2431
2432         /* nothing else to do, really */
2433 }
2434
2435 void
2436 Editor::new_region_from_selection ()
2437 {
2438         region_from_selection ();
2439         cancel_selection ();
2440 }
2441
2442 void
2443 Editor::separate_region_from_selection ()
2444 {
2445         bool doing_undo = false;
2446
2447         if (selection->time.empty()) {
2448                 return;
2449         }
2450
2451         Playlist *playlist;
2452                 
2453         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
2454
2455                 AudioTimeAxisView* atv;
2456
2457                 if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
2458
2459                         if (atv->is_audio_track()) {
2460                                         
2461                                 if ((playlist = atv->playlist()) != 0) {
2462                                         if (!doing_undo) {
2463                                                 begin_reversible_command (_("separate"));
2464                                                 doing_undo = true;
2465                                         }
2466                                         if (doing_undo) session->add_undo ((playlist)->get_memento());
2467                         
2468                                         /* XXX need to consider musical time selections here at some point */
2469
2470                                         double speed = atv->get_diskstream()->speed();
2471
2472                                         for (list<AudioRange>::iterator t = selection->time.begin(); t != selection->time.end(); ++t) {
2473                                                 playlist->partition ((jack_nframes_t)((*t).start * speed), (jack_nframes_t)((*t).end * speed), true);
2474                                         }
2475
2476                                         if (doing_undo) session->add_redo_no_execute (playlist->get_memento());
2477                                 }
2478                         }
2479                 }
2480         }
2481
2482         if (doing_undo) commit_reversible_command ();
2483 }
2484
2485 void
2486 Editor::crop_region_to_selection ()
2487 {
2488         if (selection->time.empty()) {
2489                 return;
2490         }
2491
2492         vector<Playlist*> playlists;
2493         Playlist *playlist;
2494
2495         if (clicked_trackview != 0) {
2496
2497                 if ((playlist = clicked_trackview->playlist()) == 0) {
2498                         return;
2499                 }
2500
2501                 playlists.push_back (playlist);
2502
2503         } else {
2504                 
2505                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
2506
2507                         AudioTimeAxisView* atv;
2508
2509                         if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
2510
2511                                 if (atv->is_audio_track()) {
2512                                         
2513                                         if ((playlist = atv->playlist()) != 0) {
2514                                                 playlists.push_back (playlist);
2515                                         }
2516                                 }
2517                         }
2518                 }
2519         }
2520
2521         if (!playlists.empty()) {
2522
2523                 jack_nframes_t start;
2524                 jack_nframes_t end;
2525                 jack_nframes_t cnt;
2526
2527                 begin_reversible_command (_("trim to selection"));
2528
2529                 for (vector<Playlist*>::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2530                         
2531                         Region *region;
2532                         
2533                         start = selection->time.start();
2534
2535                         if ((region = (*i)->top_region_at(start)) == 0) {
2536                                 continue;
2537                         }
2538                         
2539                         /* now adjust lengths to that we do the right thing
2540                            if the selection extends beyond the region
2541                         */
2542                         
2543                         start = max (start, region->position());
2544                         end = min (selection->time.end_frame(), start + region->length() - 1);
2545                         cnt = end - start + 1;
2546
2547                         session->add_undo ((*i)->get_memento());
2548                         region->trim_to (start, cnt, this);
2549                         session->add_redo_no_execute ((*i)->get_memento());
2550                 }
2551
2552                 commit_reversible_command ();
2553         }
2554 }               
2555
2556 void
2557 Editor::region_fill_track ()
2558 {
2559         jack_nframes_t end;
2560
2561         if (!session || selection->audio_regions.empty()) {
2562                 return;
2563         }
2564
2565         end = session->current_end_frame ();
2566
2567         begin_reversible_command (_("region fill"));
2568
2569         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
2570
2571                 AudioRegion& region ((*i)->region);
2572                 Playlist* pl = region.playlist();
2573
2574                 if (end <= region.last_frame()) {
2575                         return;
2576                 }
2577
2578                 double times = (double) (end - region.last_frame()) / (double) region.length();
2579
2580                 if (times == 0) {
2581                         return;
2582                 }
2583
2584                 session->add_undo (pl->get_memento());
2585                 pl->add_region (*(new AudioRegion (region)), region.last_frame(), times);
2586                 session->add_redo_no_execute (pl->get_memento());
2587         }
2588
2589         commit_reversible_command ();
2590 }
2591
2592 void
2593 Editor::region_fill_selection ()
2594 {
2595         if (clicked_audio_trackview == 0 || !clicked_audio_trackview->is_audio_track()) {
2596                 return;
2597         }
2598
2599         if (selection->time.empty()) {
2600                 return;
2601         }
2602
2603         Region *region;
2604
2605         Gtk::CTree_Helpers::SelectionList& selected = region_list_display.selection();
2606         
2607         if (selected.empty()) {
2608                 return;
2609         }
2610
2611         region = reinterpret_cast<Region *> (selected.front().get_data());
2612
2613         jack_nframes_t start = selection->time[clicked_selection].start;
2614         jack_nframes_t end = selection->time[clicked_selection].end;
2615
2616         Playlist *playlist; 
2617
2618         if (selection->tracks.empty()) {
2619                 return;
2620         }
2621
2622         jack_nframes_t selection_length = end - start;
2623         float times = (float)selection_length / region->length();
2624         
2625         begin_reversible_command (_("fill selection"));
2626         
2627         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
2628
2629                 if ((playlist = (*i)->playlist()) == 0) {
2630                         continue;
2631                 }               
2632                 
2633                 session->add_undo (playlist->get_memento());
2634                 playlist->add_region (*(createRegion (*region)), start, times);
2635                 session->add_redo_no_execute (playlist->get_memento());
2636         }
2637         
2638         commit_reversible_command ();                   
2639 }
2640         
2641 void
2642 Editor::set_region_sync_from_edit_cursor ()
2643 {
2644         if (clicked_regionview == 0) {
2645                 return;
2646         }
2647
2648         if (!clicked_regionview->region.covers (edit_cursor->current_frame)) {
2649                 error << _("Place the edit cursor at the desired sync point") << endmsg;
2650                 return;
2651         }
2652
2653         Region& region (clicked_regionview->region);
2654
2655         begin_reversible_command (_("set sync from edit cursor"));
2656         session->add_undo (region.playlist()->get_memento());
2657         region.set_sync_position (edit_cursor->current_frame);
2658         session->add_redo_no_execute (region.playlist()->get_memento());
2659         commit_reversible_command ();
2660 }
2661
2662 void
2663 Editor::remove_region_sync ()
2664 {
2665         if (clicked_regionview) {
2666                 Region& region (clicked_regionview->region);
2667                 begin_reversible_command (_("remove sync"));
2668                 session->add_undo (region.playlist()->get_memento());
2669                 region.clear_sync_position ();
2670                 session->add_redo_no_execute (region.playlist()->get_memento());
2671                 commit_reversible_command ();
2672         }
2673 }
2674
2675 void
2676 Editor::naturalize ()
2677 {
2678         if (selection->audio_regions.empty()) {
2679                 return;
2680         }
2681         begin_reversible_command (_("naturalize"));
2682         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
2683                 session->add_undo ((*i)->region.get_memento());
2684                 (*i)->region.move_to_natural_position (this);
2685                 session->add_redo_no_execute ((*i)->region.get_memento());
2686         }
2687         commit_reversible_command ();
2688 }
2689
2690 void
2691 Editor::align (RegionPoint what)
2692 {
2693         align_selection (what, edit_cursor->current_frame);
2694 }
2695
2696 void
2697 Editor::align_relative (RegionPoint what)
2698 {
2699         align_selection_relative (what, edit_cursor->current_frame);
2700 }
2701
2702 struct RegionSortByTime {
2703     bool operator() (const AudioRegionView* a, const AudioRegionView* b) {
2704             return a->region.position() < b->region.position();
2705     }
2706 };
2707
2708 void
2709 Editor::align_selection_relative (RegionPoint point, jack_nframes_t position)
2710 {
2711         if (selection->audio_regions.empty()) {
2712                 return;
2713         }
2714
2715         jack_nframes_t distance;
2716         jack_nframes_t pos = 0;
2717         int dir;
2718
2719         list<AudioRegionView*> sorted;
2720         selection->audio_regions.by_position (sorted);
2721         Region& r ((*sorted.begin())->region);
2722
2723         switch (point) {
2724         case Start:
2725                 pos = r.first_frame ();
2726                 break;
2727
2728         case End:
2729                 pos = r.last_frame();
2730                 break;
2731
2732         case SyncPoint:
2733                 pos = r.adjust_to_sync (r.first_frame());
2734                 break;  
2735         }
2736
2737         if (pos > position) {
2738                 distance = pos - position;
2739                 dir = -1;
2740         } else {
2741                 distance = position - pos;
2742                 dir = 1;
2743         }
2744
2745         begin_reversible_command (_("align selection (relative)"));
2746
2747         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
2748
2749                 Region& region ((*i)->region);
2750
2751                 session->add_undo (region.playlist()->get_memento());
2752                 
2753                 if (dir > 0) {
2754                         region.set_position (region.position() + distance, this);
2755                 } else {
2756                         region.set_position (region.position() - distance, this);
2757                 }
2758
2759                 session->add_redo_no_execute (region.playlist()->get_memento());
2760
2761         }
2762
2763         commit_reversible_command ();
2764 }
2765
2766 void
2767 Editor::align_selection (RegionPoint point, jack_nframes_t position)
2768 {
2769         if (selection->audio_regions.empty()) {
2770                 return;
2771         }
2772
2773         begin_reversible_command (_("align selection"));
2774
2775         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
2776                 align_region_internal ((*i)->region, point, position);
2777         }
2778
2779         commit_reversible_command ();
2780 }
2781
2782 void
2783 Editor::align_region (Region& region, RegionPoint point, jack_nframes_t position)
2784 {
2785         begin_reversible_command (_("align region"));
2786         align_region_internal (region, point, position);
2787         commit_reversible_command ();
2788 }
2789
2790 void
2791 Editor::align_region_internal (Region& region, RegionPoint point, jack_nframes_t position)
2792 {
2793         session->add_undo (region.playlist()->get_memento());
2794
2795         switch (point) {
2796         case SyncPoint:
2797                 region.set_position (region.adjust_to_sync (position), this);
2798                 break;
2799
2800         case End:
2801                 if (position > region.length()) {
2802                         region.set_position (position - region.length(), this);
2803                 }
2804                 break;
2805
2806         case Start:
2807                 region.set_position (position, this);
2808                 break;
2809         }
2810
2811         session->add_redo_no_execute (region.playlist()->get_memento());
2812 }       
2813
2814 void
2815 Editor::trim_region_to_edit_cursor ()
2816 {
2817         if (clicked_regionview == 0) {
2818                 return;
2819         }
2820
2821         Region& region (clicked_regionview->region);
2822
2823         float speed = 1.0f;
2824         AudioTimeAxisView *atav;
2825
2826         if ( clicked_trackview != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(clicked_trackview)) != 0 ) {
2827                 if (atav->get_diskstream() != 0) {
2828                         speed = atav->get_diskstream()->speed();
2829                 }
2830         }
2831
2832         begin_reversible_command (_("trim to edit"));
2833         session->add_undo (region.playlist()->get_memento());
2834         region.trim_end ( (jack_nframes_t) floor( (float)edit_cursor->current_frame * speed), this);
2835         session->add_redo_no_execute (region.playlist()->get_memento());
2836         commit_reversible_command ();
2837 }
2838
2839 void
2840 Editor::trim_region_from_edit_cursor ()
2841 {
2842         if (clicked_regionview == 0) {
2843                 return;
2844         }
2845
2846         Region& region (clicked_regionview->region);
2847
2848         float speed = 1.0f;
2849         AudioTimeAxisView *atav;
2850
2851         if ( clicked_trackview != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(clicked_trackview)) != 0 ) {
2852                 if (atav->get_diskstream() != 0) {
2853                         speed = atav->get_diskstream()->speed();
2854                 }
2855         }
2856
2857         begin_reversible_command (_("trim to edit"));
2858         session->add_undo (region.playlist()->get_memento());
2859         region.trim_front ( (jack_nframes_t) floor( (float)edit_cursor->current_frame * speed), this);
2860         session->add_redo_no_execute (region.playlist()->get_memento());
2861         commit_reversible_command ();
2862 }
2863
2864 void
2865 Editor::unfreeze_route ()
2866 {
2867         if (clicked_audio_trackview == 0 || !clicked_audio_trackview->is_audio_track()) {
2868                 return;
2869         }
2870         
2871         clicked_audio_trackview->audio_track()->unfreeze ();
2872 }
2873
2874 void*
2875 Editor::_freeze_thread (void* arg)
2876 {
2877         PBD::ThreadCreated (pthread_self(), X_("Freeze"));
2878         return static_cast<Editor*>(arg)->freeze_thread ();
2879 }
2880
2881 void*
2882 Editor::freeze_thread ()
2883 {
2884         clicked_audio_trackview->audio_track()->freeze (*current_interthread_info);
2885         return 0;
2886 }
2887
2888 gint
2889 Editor::freeze_progress_timeout (void *arg)
2890 {
2891         interthread_progress_bar.set_fraction (current_interthread_info->progress/100);
2892         return !(current_interthread_info->done || current_interthread_info->cancel);
2893 }
2894
2895 void
2896 Editor::freeze_route ()
2897 {
2898         if (clicked_audio_trackview == 0 || !clicked_audio_trackview->is_audio_track()) {
2899                 return;
2900         }
2901         
2902         InterThreadInfo itt;
2903
2904         if (interthread_progress_window == 0) {
2905                 build_interthread_progress_window ();
2906         }
2907         
2908         interthread_progress_window->set_title (_("ardour: freeze"));
2909         interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
2910         interthread_progress_window->show_all ();
2911         interthread_progress_bar.set_percentage (0.0f);
2912         interthread_progress_label.set_text ("");
2913         interthread_cancel_label.set_text (_("Cancel Freeze"));
2914         current_interthread_info = &itt;
2915
2916         interthread_progress_connection = 
2917           Glib::signal_timeout().connect (bind (mem_fun(*this, &Editor::freeze_progress_timeout), (gpointer) 0), 100);
2918
2919         itt.done = false;
2920         itt.cancel = false;
2921         itt.progress = 0.0f;
2922
2923         pthread_create (&itt.thread, 0, _freeze_thread, this);
2924
2925         track_canvas_scroller.get_window()->set_cursor (GDK_WATCH);
2926
2927         while (!itt.done && !itt.cancel) {
2928                 gtk_main_iteration ();
2929         }
2930
2931         interthread_progress_connection.disconnect ();
2932         interthread_progress_window->hide_all ();
2933         current_interthread_info = 0;
2934         track_canvas_scroller.get_window()->set_cursor (current_canvas_cursor);
2935 }
2936
2937 void
2938 Editor::bounce_range_selection ()
2939 {
2940         if (selection->time.empty()) {
2941                 return;
2942         }
2943
2944         TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
2945
2946         jack_nframes_t start = selection->time[clicked_selection].start;
2947         jack_nframes_t end = selection->time[clicked_selection].end;
2948         jack_nframes_t cnt = end - start + 1;
2949         
2950         begin_reversible_command (_("bounce range"));
2951
2952         for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
2953
2954                 AudioTimeAxisView* atv;
2955
2956                 if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) == 0) {
2957                         continue;
2958                 }
2959                 
2960                 Playlist* playlist;
2961                 
2962                 if ((playlist = atv->playlist()) == 0) {
2963                         return;
2964                 }
2965
2966                 InterThreadInfo itt;
2967                 
2968                 itt.done = false;
2969                 itt.cancel = false;
2970                 itt.progress = false;
2971                 
2972                 session->add_undo (playlist->get_memento());
2973                 atv->audio_track()->bounce_range (start, cnt, itt);
2974                 session->add_redo_no_execute (playlist->get_memento());
2975         }
2976         
2977         commit_reversible_command ();
2978         
2979         delete views;
2980 }
2981
2982 void
2983 Editor::cut ()
2984 {
2985         cut_copy (Cut);
2986 }
2987
2988 void
2989 Editor::copy ()
2990 {
2991         cut_copy (Copy);
2992 }
2993
2994 void 
2995 Editor::cut_copy (CutCopyOp op)
2996 {
2997         /* only cancel selection if cut/copy is successful.*/
2998
2999         string opname;
3000
3001         switch (op) {
3002         case Cut:
3003                 opname = _("cut");
3004                 break;
3005         case Copy:
3006                 opname = _("copy");
3007                 break;
3008         case Clear:
3009                 opname = _("clear");
3010                 break;
3011         }
3012         
3013         cut_buffer->clear ();
3014
3015         switch (current_mouse_mode()) {
3016         case MouseObject: 
3017                 if (!selection->audio_regions.empty() || !selection->points.empty()) {
3018
3019                         begin_reversible_command (opname + _(" objects"));
3020
3021                         if (!selection->audio_regions.empty()) {
3022                                 
3023                                 cut_copy_regions (op);
3024                                 
3025                                 if (op == Cut) {
3026                                         selection->clear_audio_regions ();
3027                                 }
3028                         }
3029
3030                         if (!selection->points.empty()) {
3031                                 cut_copy_points (op);
3032
3033                                 if (op == Cut) {
3034                                         selection->clear_points ();
3035                                 }
3036                         }
3037
3038                         commit_reversible_command ();   
3039                 }
3040                 break;
3041                 
3042         case MouseRange:
3043                 if (!selection->time.empty()) {
3044
3045                         begin_reversible_command (opname + _(" range"));
3046                         cut_copy_ranges (op);
3047                         commit_reversible_command ();
3048
3049                         if (op == Cut) {
3050                                 selection->clear_time ();
3051                         }
3052                         
3053                 }
3054                 break;
3055                 
3056         default:
3057                 break;
3058         }
3059 }
3060
3061 void
3062 Editor::cut_copy_points (CutCopyOp op)
3063 {
3064         for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
3065
3066                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
3067
3068                 if (atv) {
3069                         atv->cut_copy_clear_objects (selection->points, op);
3070                 } 
3071         }
3072 }
3073
3074 void
3075 Editor::cut_copy_regions (CutCopyOp op)
3076 {
3077         typedef map<AudioPlaylist*,AudioPlaylist*> PlaylistMapping;
3078         PlaylistMapping pmap;
3079         jack_nframes_t first_position = max_frames;
3080         set<Playlist*> freezelist;
3081         pair<set<Playlist*>::iterator,bool> insert_result;
3082
3083         for (AudioRegionSelection::iterator x = selection->audio_regions.begin(); x != selection->audio_regions.end(); ++x) {
3084                 first_position = min ((*x)->region.position(), first_position);
3085
3086                 if (op == Cut || op == Clear) {
3087                         AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region.playlist());
3088                         if (pl) {
3089                                 insert_result = freezelist.insert (pl);
3090                                 if (insert_result.second) {
3091                                         pl->freeze ();
3092                                         session->add_undo (pl->get_memento());
3093                                 }
3094                         }
3095                 }
3096         }
3097
3098         for (AudioRegionSelection::iterator x = selection->audio_regions.begin(); x != selection->audio_regions.end(); ) {
3099
3100                 AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region.playlist());
3101                 AudioPlaylist* npl;
3102                 AudioRegionSelection::iterator tmp;
3103                 
3104                 tmp = x;
3105                 ++tmp;
3106
3107                 if (pl) {
3108
3109                         PlaylistMapping::iterator pi = pmap.find (pl);
3110                         
3111                         if (pi == pmap.end()) {
3112                                 npl = new AudioPlaylist (*session, "cutlist", true);
3113                                 npl->freeze();
3114                                 pmap[pl] = npl;
3115                         } else {
3116                                 npl = pi->second;
3117                         }
3118
3119                         switch (op) {
3120                         case Cut:
3121                                 npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
3122                                 pl->remove_region (&((*x)->region));
3123                                 break;
3124
3125                         case Copy:
3126                                 npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
3127                                 break;
3128
3129                         case Clear:
3130                                 pl->remove_region (&((*x)->region));
3131                                 break;
3132                         }
3133                 }
3134
3135                 x = tmp;
3136         }
3137
3138         list<Playlist*> foo;
3139
3140         for (PlaylistMapping::iterator i = pmap.begin(); i != pmap.end(); ++i) {
3141                 foo.push_back (i->second);
3142         }
3143
3144         if (!foo.empty()) {
3145                 cut_buffer->set (foo);
3146         }
3147         
3148         for (set<Playlist*>::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
3149                 (*pl)->thaw ();
3150                 session->add_redo_no_execute ((*pl)->get_memento());
3151         }
3152 }
3153
3154 void
3155 Editor::cut_copy_ranges (CutCopyOp op)
3156 {
3157         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3158                 (*i)->cut_copy_clear (*selection, op);
3159         }
3160 }
3161
3162 void
3163 Editor::paste (float times)
3164 {
3165         paste_internal (edit_cursor->current_frame, times);
3166 }
3167
3168 void
3169 Editor::mouse_paste ()
3170 {
3171         gint x, y;
3172         double wx, wy;
3173         track_canvas->get_pointer (x, y);
3174
3175         gnome_canvas_window_to_world (GNOME_CANVAS(track_gnome_canvas), x, y, &wx, &wy);
3176         
3177         GdkEvent event;
3178         event.type = GDK_BUTTON_RELEASE;
3179         event.button.x = wx;
3180         event.button.y = wy;
3181         
3182         jack_nframes_t where = event_frame (&event, 0, 0);
3183         snap_to (where);
3184         paste_internal (where, 1);
3185 }
3186
3187 void
3188 Editor::paste_internal (jack_nframes_t position, float times)
3189 {
3190         bool commit = false;
3191
3192         if (cut_buffer->empty() || selection->tracks.empty()) {
3193                 return;
3194         }
3195
3196         if (position == max_frames) {
3197                 position = edit_cursor->current_frame;
3198         }
3199
3200         begin_reversible_command (_("paste"));
3201
3202         TrackSelection::iterator i;
3203         size_t nth;
3204
3205         for (nth = 0, i = selection->tracks.begin(); i != selection->tracks.end(); ++i, ++nth) {
3206                 
3207                 /* undo/redo is handled by individual tracks */
3208
3209                 if ((*i)->paste (position, times, *cut_buffer, nth)) {
3210                         commit = true;
3211                 }
3212         }
3213
3214         if (commit) {
3215                 commit_reversible_command ();
3216         }
3217 }
3218
3219 void
3220 Editor::paste_named_selection (float times)
3221 {
3222         Gtk::CList_Helpers::SelectionList& selected = named_selection_display.selection();
3223         TrackSelection::iterator i;
3224
3225         if (selected.empty() || selection->tracks.empty()) {
3226                 return;
3227         }
3228
3229         NamedSelection* ns = static_cast<NamedSelection*> (selected.front()->get_data ());
3230         list<Playlist*>::iterator chunk;
3231         list<Playlist*>::iterator tmp;
3232
3233         chunk = ns->playlists.begin();
3234                 
3235         begin_reversible_command (_("paste chunk"));
3236
3237         for (i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3238                 
3239                 AudioTimeAxisView* atv;
3240                 Playlist* pl;
3241                 AudioPlaylist* apl;
3242
3243                 if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) == 0) {
3244                         continue;
3245                 }
3246
3247                 if ((pl = atv->playlist()) == 0) {
3248                         continue;
3249                 }
3250
3251                 if ((apl = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
3252                         continue;
3253                 }
3254
3255                 tmp = chunk;
3256                 ++tmp;
3257
3258                 session->add_undo (apl->get_memento());
3259                 apl->paste (**chunk, edit_cursor->current_frame, times);
3260                 session->add_redo_no_execute (apl->get_memento());
3261
3262                 if (tmp != ns->playlists.end()) {
3263                         chunk = tmp;
3264                 }
3265         }
3266
3267         commit_reversible_command();
3268 }
3269
3270 void
3271 Editor::duplicate_some_regions (AudioRegionSelection& regions, float times)
3272 {
3273         Playlist *playlist; 
3274         
3275         begin_reversible_command (_("duplicate region"));
3276
3277         for (AudioRegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
3278
3279                 Region& r ((*i)->region);
3280                 
3281                 playlist = (*i)->region.playlist();
3282                 session->add_undo (playlist->get_memento());
3283                 playlist->duplicate (r, r.last_frame(), times);
3284                 session->add_redo_no_execute (playlist->get_memento());
3285
3286         }
3287
3288         commit_reversible_command ();
3289 }
3290
3291 void
3292 Editor::duplicate_selection (float times)
3293 {
3294         if (selection->time.empty() || selection->tracks.empty()) {
3295                 return;
3296         }
3297
3298         Playlist *playlist; 
3299         vector<AudioRegion*> new_regions;
3300         vector<AudioRegion*>::iterator ri;
3301                 
3302         create_region_from_selection (new_regions);
3303
3304         if (new_regions.empty()) {
3305                 return;
3306         }
3307         
3308         begin_reversible_command (_("duplicate selection"));
3309
3310         ri = new_regions.begin();
3311
3312         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3313                 if ((playlist = (*i)->playlist()) == 0) {
3314                         continue;
3315                 }
3316                 session->add_undo (playlist->get_memento());
3317                 playlist->duplicate (**ri, selection->time[clicked_selection].end, times);
3318                 session->add_redo_no_execute (playlist->get_memento());
3319
3320                 ++ri;
3321                 if (ri == new_regions.end()) {
3322                         --ri;
3323                 }
3324         }
3325
3326         commit_reversible_command ();
3327 }
3328
3329 void
3330 Editor::center_playhead ()
3331 {
3332         float page = canvas_width * frames_per_unit;
3333
3334         center_screen_internal (playhead_cursor->current_frame, page);
3335 }
3336
3337 void
3338 Editor::center_edit_cursor ()
3339 {
3340         float page = canvas_width * frames_per_unit;
3341
3342         center_screen_internal (edit_cursor->current_frame, page);
3343 }
3344
3345 void
3346 Editor::clear_playlist (Playlist& playlist)
3347 {
3348         begin_reversible_command (_("clear playlist"));
3349         session->add_undo (playlist.get_memento());
3350         playlist.clear ();
3351         session->add_redo_no_execute (playlist.get_memento());
3352         commit_reversible_command ();
3353 }
3354
3355 void
3356 Editor::nudge_track (bool use_edit_cursor, bool forwards)
3357 {
3358         Playlist *playlist; 
3359         jack_nframes_t distance;
3360         jack_nframes_t next_distance;
3361         jack_nframes_t start;
3362
3363         if (use_edit_cursor) {
3364                 start = edit_cursor->current_frame;
3365         } else {
3366                 start = 0;
3367         }
3368
3369         if ((distance = get_nudge_distance (start, next_distance)) == 0) {
3370                 return;
3371         }
3372         
3373         if (selection->tracks.empty()) {
3374                 return;
3375         }
3376         
3377         begin_reversible_command (_("nudge track"));
3378         
3379         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3380
3381                 if ((playlist = (*i)->playlist()) == 0) {
3382                         continue;
3383                 }               
3384                 
3385                 session->add_undo (playlist->get_memento());
3386                 playlist->nudge_after (start, distance, forwards);
3387                 session->add_redo_no_execute (playlist->get_memento());
3388         }
3389         
3390         commit_reversible_command ();                   
3391 }
3392
3393 void
3394 Editor::toggle_xfades_active ()
3395 {
3396         if (session) {
3397                 session->set_crossfades_active (!session->get_crossfades_active());
3398         }
3399 }
3400
3401 void
3402 Editor::toggle_follow_playhead ()
3403 {
3404         set_follow_playhead (!_follow_playhead);
3405 }
3406
3407 void
3408 Editor::set_xfade_visibility (bool yn)
3409 {
3410         
3411 }
3412
3413 void
3414 Editor::toggle_xfade_visibility ()
3415 {
3416         set_xfade_visibility (!xfade_visibility());
3417 }
3418
3419 void
3420 Editor::remove_last_capture ()
3421 {
3422         vector<string> choices;
3423         string prompt;
3424         
3425         if (!session) {
3426                 return;
3427         }
3428
3429         if (Config->get_verify_remove_last_capture()) {
3430                 prompt  = _("Do you really want to destroy the last capture?"
3431                             "\n(This is destructive and cannot be undone)");
3432
3433                 choices.push_back (_("Yes, destroy it."));
3434                 choices.push_back (_("No, do nothing."));
3435
3436                 Gtkmm2ext::Choice prompter (prompt, choices);
3437
3438                 prompter.chosen.connect (Gtk::Main::quit.slot());
3439                 prompter.show_all ();
3440
3441                 Gtk::Main::run ();
3442
3443                 if (prompter.get_choice() == 0) {
3444                         session->remove_last_capture ();
3445                 }
3446         } else {
3447                 session->remove_last_capture();
3448         }
3449 }
3450
3451 void
3452 Editor::normalize_region ()
3453 {
3454         if (!session) {
3455                 return;
3456         }
3457
3458         if (selection->audio_regions.empty()) {
3459                 return;
3460         }
3461
3462         begin_reversible_command (_("normalize"));
3463
3464         track_canvas_scroller.get_window()->set_cursor (wait_cursor);
3465         gdk_flush ();
3466
3467         for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
3468                 session->add_undo ((*r)->region.get_memento());
3469                 (*r)->region.normalize_to (0.0f);
3470                 session->add_redo_no_execute ((*r)->region.get_memento());
3471         }
3472
3473         commit_reversible_command ();
3474         gdk_window_set_cursor (track_canvas_scroller.get_window()->gobj(), current_canvas_cursor);
3475 }
3476
3477
3478 void
3479 Editor::denormalize_region ()
3480 {
3481         if (!session) {
3482                 return;
3483         }
3484
3485         if (selection->audio_regions.empty()) {
3486                 return;
3487         }
3488
3489         begin_reversible_command ("denormalize");
3490
3491         for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
3492                 session->add_undo ((*r)->region.get_memento());
3493                 (*r)->region.set_scale_amplitude (1.0f);
3494                 session->add_redo_no_execute ((*r)->region.get_memento());
3495         }
3496
3497         commit_reversible_command ();
3498 }
3499
3500
3501 void
3502 Editor::reverse_region ()
3503 {
3504         if (!session) {
3505                 return;
3506         }
3507
3508         Reverse rev (*session);
3509         apply_filter (rev, _("reverse regions"));
3510 }
3511
3512 void
3513 Editor::apply_filter (AudioFilter& filter, string command)
3514 {
3515         if (selection->audio_regions.empty()) {
3516                 return;
3517         }
3518
3519         begin_reversible_command (command);
3520
3521         track_canvas_scroller.get_window()->set_cursor (wait_cursor);
3522         gdk_flush ();
3523
3524         for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ) {
3525
3526                 AudioRegion& region ((*r)->region);
3527                 Playlist* playlist = region.playlist();
3528
3529                 AudioRegionSelection::iterator tmp;
3530                 
3531                 tmp = r;
3532                 ++tmp;
3533
3534                 if (region.apply (filter) == 0) {
3535
3536                         session->add_undo (playlist->get_memento());
3537                         playlist->replace_region (region, *(filter.results.front()), region.position());
3538                         session->add_redo_no_execute (playlist->get_memento());
3539                 } else {
3540                         goto out;
3541                 }
3542
3543                 r = tmp;
3544         }
3545
3546         commit_reversible_command ();
3547         selection->audio_regions.clear ();
3548
3549   out:
3550         gdk_window_set_cursor (track_canvas_scroller.get_window()->gobj(), current_canvas_cursor);
3551 }
3552
3553 void
3554 Editor::region_selection_op (void (Region::*pmf)(void))
3555 {
3556         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3557                 ((*i)->region.*pmf)();
3558         }
3559 }
3560
3561
3562 void
3563 Editor::region_selection_op (void (Region::*pmf)(void*), void *arg)
3564 {
3565         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3566                 ((*i)->region.*pmf)(arg);
3567         }
3568 }
3569
3570 void
3571 Editor::region_selection_op (void (Region::*pmf)(bool), bool yn)
3572 {
3573         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3574                 ((*i)->region.*pmf)(yn);
3575         }
3576 }
3577
3578 void
3579 Editor::external_edit_region ()
3580 {
3581         if (!clicked_regionview) {
3582                 return;
3583         }
3584
3585         /* more to come */
3586 }
3587
3588 void
3589 Editor::brush (jack_nframes_t pos)
3590 {
3591         AudioRegionSelection sel;
3592         snap_to (pos);
3593
3594         if (selection->audio_regions.empty()) {
3595                 /* XXX get selection from region list */
3596         } else { 
3597                 sel = selection->audio_regions;
3598         }
3599
3600         if (sel.empty()) {
3601                 return;
3602         }
3603
3604         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3605                 mouse_brush_insert_region ((*i), pos);
3606         }
3607 }
3608
3609 void
3610 Editor::toggle_gain_envelope_visibility ()
3611 {
3612         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3613                 (*i)->set_envelope_visible (!(*i)->envelope_visible());
3614         }
3615 }
3616
3617 void
3618 Editor::toggle_gain_envelope_active ()
3619 {
3620         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3621                 AudioRegion* ar = dynamic_cast<AudioRegion*>(&(*i)->region);
3622                 if (ar) {
3623                         ar->set_envelope_active (true);
3624                 }
3625         }
3626 }