Important engine bugfixes: Ladspa plugin parameter automation crackles
[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 "sfdb_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 = track_frame_to_session_frame(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 = session_frame_to_track_frame(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 "session frame"
606                 rpos = track_frame_to_session_frame(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 = track_frame_to_session_frame(pos, speed);
685         
686         if (cursor == playhead_cursor) {
687                 session->request_locate (pos);
688         } else {
689                 cursor->set_position (pos);
690         }
691 }
692
693 void
694 Editor::cursor_to_next_region_point (Cursor* cursor, RegionPoint point)
695 {
696         cursor_to_region_point (cursor, point, 1);
697 }
698
699 void
700 Editor::cursor_to_previous_region_point (Cursor* cursor, RegionPoint point)
701 {
702         cursor_to_region_point (cursor, point, -1);
703 }
704
705 void
706 Editor::cursor_to_selection_start (Cursor *cursor)
707 {
708         jack_nframes_t pos = 0;
709         switch (mouse_mode) {
710         case MouseObject:
711                 if (!selection->audio_regions.empty()) {
712                         pos = selection->audio_regions.start();
713                 }
714                 break;
715
716         case MouseRange:
717                 if (!selection->time.empty()) {
718                         pos = selection->time.start ();
719                 }
720                 break;
721
722         default:
723                 return;
724         }
725
726         if (cursor == playhead_cursor) {
727                 session->request_locate (pos);
728         } else {
729                 cursor->set_position (pos);
730         }
731 }
732
733 void
734 Editor::cursor_to_selection_end (Cursor *cursor)
735 {
736         jack_nframes_t pos = 0;
737
738         switch (mouse_mode) {
739         case MouseObject:
740                 if (!selection->audio_regions.empty()) {
741                         pos = selection->audio_regions.end_frame();
742                 }
743                 break;
744
745         case MouseRange:
746                 if (!selection->time.empty()) {
747                         pos = selection->time.end_frame ();
748                 }
749                 break;
750
751         default:
752                 return;
753         }
754
755         if (cursor == playhead_cursor) {
756                 session->request_locate (pos);
757         } else {
758                 cursor->set_position (pos);
759         }
760 }
761
762 void
763 Editor::playhead_backward ()
764 {
765         jack_nframes_t pos;
766         jack_nframes_t cnt;
767         float prefix;
768         bool was_floating;
769
770         if (get_prefix (prefix, was_floating)) {
771                 cnt = 1;
772         } else {
773                 if (was_floating) {
774                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
775                 } else {
776                         cnt = (jack_nframes_t) prefix;
777                 }
778         }
779
780         pos = playhead_cursor->current_frame;
781
782         if ((jack_nframes_t) pos < cnt) {
783                 pos = 0;
784         } else {
785                 pos -= cnt;
786         }
787         
788         /* XXX this is completely insane. with the current buffering
789            design, we'll force a complete track buffer flush and
790            reload, just to move 1 sample !!!
791         */
792
793         session->request_locate (pos);
794 }
795
796 void
797 Editor::playhead_forward ()
798 {
799         jack_nframes_t pos;
800         jack_nframes_t cnt;
801         bool was_floating;
802         float prefix;
803
804         if (get_prefix (prefix, was_floating)) {
805                 cnt = 1;
806         } else {
807                 if (was_floating) {
808                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
809                 } else {
810                         cnt = (jack_nframes_t) floor (prefix);
811                 }
812         }
813
814         pos = playhead_cursor->current_frame;
815         
816         /* XXX this is completely insane. with the current buffering
817            design, we'll force a complete track buffer flush and
818            reload, just to move 1 sample !!!
819         */
820
821         session->request_locate (pos+cnt);
822 }
823
824 void
825 Editor::cursor_align (bool playhead_to_edit)
826 {
827         if (playhead_to_edit) {
828                 if (session) {
829                         session->request_locate (edit_cursor->current_frame);
830                 }
831         } else {
832                 edit_cursor->set_position (playhead_cursor->current_frame);
833         }
834 }
835
836 void
837 Editor::edit_cursor_backward ()
838 {
839         jack_nframes_t pos;
840         jack_nframes_t cnt;
841         float prefix;
842         bool was_floating;
843
844         if (get_prefix (prefix, was_floating)) {
845                 cnt = 1;
846         } else {
847                 if (was_floating) {
848                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
849                 } else {
850                         cnt = (jack_nframes_t) prefix;
851                 }
852         }
853
854         pos = edit_cursor->current_frame;
855
856         if ((jack_nframes_t) pos < cnt) {
857                 pos = 0;
858         } else {
859                 pos -= cnt;
860         }
861         
862         edit_cursor->set_position (pos);
863 }
864
865 void
866 Editor::edit_cursor_forward ()
867 {
868         jack_nframes_t pos;
869         jack_nframes_t cnt;
870         bool was_floating;
871         float prefix;
872
873         if (get_prefix (prefix, was_floating)) {
874                 cnt = 1;
875         } else {
876                 if (was_floating) {
877                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
878                 } else {
879                         cnt = (jack_nframes_t) floor (prefix);
880                 }
881         }
882
883         pos = edit_cursor->current_frame;
884         edit_cursor->set_position (pos+cnt);
885 }
886
887 void
888 Editor::goto_frame ()
889 {
890         float prefix;
891         bool was_floating;
892         jack_nframes_t frame;
893
894         if (get_prefix (prefix, was_floating)) {
895                 return;
896         }
897
898         if (was_floating) {
899                 frame = (jack_nframes_t) floor (prefix * session->frame_rate());
900         } else {
901                 frame = (jack_nframes_t) floor (prefix);
902         }
903
904         session->request_locate (frame);
905 }
906
907 void
908 Editor::scroll_backward (float pages)
909 {
910         jack_nframes_t frame;
911         jack_nframes_t one_page = (jack_nframes_t) rint (canvas_width * frames_per_unit);
912         bool was_floating;
913         float prefix;
914         jack_nframes_t cnt;
915         
916         if (get_prefix (prefix, was_floating)) {
917                 cnt = (jack_nframes_t) floor (pages * one_page);
918         } else {
919                 if (was_floating) {
920                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate());
921                 } else {
922                         cnt = (jack_nframes_t) floor (prefix * one_page);
923                 }
924         }
925
926         if (leftmost_frame < cnt) {
927                 frame = 0;
928         } else {
929                 frame = leftmost_frame - cnt;
930         }
931
932         reposition_x_origin (frame);
933 }
934
935 void
936 Editor::scroll_forward (float pages)
937 {
938         jack_nframes_t frame;
939         jack_nframes_t one_page = (jack_nframes_t) rint (canvas_width * frames_per_unit);
940         bool was_floating;
941         float prefix;
942         jack_nframes_t cnt;
943         
944         if (get_prefix (prefix, was_floating)) {
945                 cnt = (jack_nframes_t) floor (pages * one_page);
946         } else {
947                 if (was_floating) {
948                         cnt = (jack_nframes_t) floor (prefix * session->frame_rate());
949                 } else {
950                         cnt = (jack_nframes_t) floor (prefix * one_page);
951                 }
952         }
953
954         if (ULONG_MAX - cnt < leftmost_frame) {
955                 frame = ULONG_MAX - cnt;
956         } else {
957                 frame = leftmost_frame + cnt;
958         }
959
960         reposition_x_origin (frame);
961 }
962
963 void
964 Editor::scroll_tracks_down ()
965 {
966         float prefix;
967         bool was_floating;
968         int cnt;
969
970         if (get_prefix (prefix, was_floating)) {
971                 cnt = 1;
972         } else {
973                 cnt = (int) floor (prefix);
974         }
975
976         Gtk::Adjustment *adj = track_canvas_scroller.get_vadjustment();
977         adj->set_value (adj->get_value() + (cnt * adj->get_page_size()));
978 }
979
980 void
981 Editor::scroll_tracks_up ()
982 {
983         float prefix;
984         bool was_floating;
985         int cnt;
986
987         if (get_prefix (prefix, was_floating)) {
988                 cnt = 1;
989         } else {
990                 cnt = (int) floor (prefix);
991         }
992
993         Gtk::Adjustment *adj = track_canvas_scroller.get_vadjustment();
994         adj->set_value (adj->get_value() - (cnt * adj->get_page_size()));
995 }
996
997 void
998 Editor::scroll_tracks_down_line ()
999 {
1000         Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
1001         adj->set_value (adj->get_value() + 10);
1002 }
1003
1004 void
1005 Editor::scroll_tracks_up_line ()
1006 {
1007         Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
1008         adj->set_value (adj->get_value() - 10);
1009 }
1010
1011 /* ZOOM */
1012
1013 void
1014 Editor::temporal_zoom_step (bool coarser)
1015 {
1016         double nfpu;
1017
1018         nfpu = frames_per_unit;
1019         
1020         if (coarser) { 
1021                 nfpu *= 2.0;
1022         } else { 
1023                 nfpu = max(1.0,(nfpu/2.0));
1024         }
1025
1026         temporal_zoom (nfpu);
1027 }       
1028
1029 void
1030 Editor::temporal_zoom (gdouble fpu)
1031 {
1032         if (!session) return;
1033         
1034         jack_nframes_t current_page = current_page_frames();
1035         jack_nframes_t current_leftmost = leftmost_frame;
1036         jack_nframes_t current_rightmost;
1037         jack_nframes_t current_center;
1038         jack_nframes_t new_page;
1039         jack_nframes_t leftmost_after_zoom = 0;
1040         double nfpu;
1041
1042         nfpu = fpu;
1043         
1044         new_page = (jack_nframes_t) floor (canvas_width * nfpu);
1045
1046         switch (zoom_focus) {
1047         case ZoomFocusLeft:
1048                 leftmost_after_zoom = current_leftmost;
1049                 break;
1050                 
1051         case ZoomFocusRight:
1052                 current_rightmost = leftmost_frame + current_page;
1053                 if (current_rightmost > new_page) {
1054                         leftmost_after_zoom = current_rightmost - new_page;
1055                 } else {
1056                         leftmost_after_zoom = 0;
1057                 }
1058                 break;
1059                 
1060         case ZoomFocusCenter:
1061                 current_center = current_leftmost + (current_page/2); 
1062                 if (current_center > (new_page/2)) {
1063                         leftmost_after_zoom = current_center - (new_page / 2);
1064                 } else {
1065                         leftmost_after_zoom = 0;
1066                 }
1067                 break;
1068                 
1069         case ZoomFocusPlayhead:
1070                 /* try to keep the playhead in the center */
1071                 if (playhead_cursor->current_frame > new_page/2) {
1072                         leftmost_after_zoom = playhead_cursor->current_frame - (new_page/2);
1073                 } else {
1074                         leftmost_after_zoom = 0;
1075                 }
1076                 break;
1077
1078         case ZoomFocusEdit:
1079                 /* try to keep the edit cursor in the center */
1080                 if (edit_cursor->current_frame > leftmost_frame + (new_page/2)) {
1081                         leftmost_after_zoom = edit_cursor->current_frame - (new_page/2);
1082                 } else {
1083                         leftmost_after_zoom = 0;
1084                 }
1085                 break;
1086                 
1087         }
1088  
1089         // leftmost_after_zoom = min (leftmost_after_zoom, session->current_end_frame());
1090
1091 //      begin_reversible_command (_("zoom"));
1092 //      session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), current_leftmost, frames_per_unit));
1093 //      session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_after_zoom, nfpu));
1094 //      commit_reversible_command ();
1095
1096         reposition_and_zoom (leftmost_after_zoom, nfpu);
1097 }       
1098
1099 void
1100 Editor::temporal_zoom_selection ()
1101 {
1102         if (!selection) return;
1103         
1104         if (selection->time.empty()) {
1105                 return;
1106         }
1107
1108         jack_nframes_t start = selection->time[clicked_selection].start;
1109         jack_nframes_t end = selection->time[clicked_selection].end;
1110
1111         temporal_zoom_by_frame (start, end, "zoom to selection");
1112 }
1113
1114 void
1115 Editor::temporal_zoom_session ()
1116 {
1117         if (session) {
1118                 temporal_zoom_by_frame (0, session->current_end_frame(), "zoom to session");
1119         }
1120 }
1121
1122 void
1123 Editor::temporal_zoom_by_frame (jack_nframes_t start, jack_nframes_t end, string op)
1124 {
1125         if (!session) return;
1126
1127         if ((start == 0 && end == 0) || end < start) {
1128                 return;
1129         }
1130
1131         jack_nframes_t range = end - start;
1132
1133         double new_fpu = (double)range / (double)canvas_width;
1134 //      double p2 = 1.0;
1135
1136 //      while (p2 < new_fpu) {
1137 //              p2 *= 2.0;
1138 //      }
1139 //      new_fpu = p2;
1140         
1141         jack_nframes_t new_page = (jack_nframes_t) floor (canvas_width * new_fpu);
1142         jack_nframes_t middle = (jack_nframes_t) floor( (double)start + ((double)range / 2.0f ));
1143         jack_nframes_t new_leftmost = (jack_nframes_t) floor( (double)middle - ((double)new_page/2.0f));
1144
1145         if (new_leftmost > middle) new_leftmost = 0;
1146
1147 //      begin_reversible_command (op);
1148 //      session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
1149 //      session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
1150 //      commit_reversible_command ();
1151
1152         reposition_and_zoom (new_leftmost, new_fpu);
1153 }
1154
1155 void 
1156 Editor::temporal_zoom_to_frame (bool coarser, jack_nframes_t frame)
1157 {
1158         if (!session) return;
1159         
1160         jack_nframes_t range_before = frame - leftmost_frame;
1161         double new_fpu;
1162         
1163         new_fpu = frames_per_unit;
1164         
1165         if (coarser) { 
1166                 new_fpu *= 2.0;
1167                 range_before *= 2;
1168         } else { 
1169                 new_fpu = max(1.0,(new_fpu/2.0));
1170                 range_before /= 2;
1171         }
1172
1173         if (new_fpu == frames_per_unit) return;
1174
1175         jack_nframes_t new_leftmost = frame - range_before;
1176
1177         if (new_leftmost > frame) new_leftmost = 0;
1178
1179 //      begin_reversible_command (_("zoom to frame"));
1180 //      session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
1181 //      session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
1182 //      commit_reversible_command ();
1183
1184         reposition_and_zoom (new_leftmost, new_fpu);
1185 }
1186
1187 void
1188 Editor::select_all_in_track (bool add)
1189 {
1190         list<Selectable *> touched;
1191
1192         if (!clicked_trackview) {
1193                 return;
1194         }
1195         
1196         clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
1197
1198         if (add) {
1199                 selection->add (touched);
1200         } else {
1201                 selection->set (touched);
1202         }
1203 }
1204
1205 void
1206 Editor::select_all (bool add)
1207 {
1208         list<Selectable *> touched;
1209         
1210         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1211                 if ((*iter)->hidden()) {
1212                         continue;
1213                 }
1214                 (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
1215         }
1216
1217         if (add) {
1218                 selection->add (touched);
1219         } else {
1220                 selection->set (touched);
1221         }
1222
1223 }
1224
1225 void
1226 Editor::invert_selection_in_track ()
1227 {
1228         list<Selectable *> touched;
1229
1230         if (!clicked_trackview) {
1231                 return;
1232         }
1233         
1234         clicked_trackview->get_inverted_selectables (*selection, touched);
1235         selection->set (touched);
1236 }
1237
1238 void
1239 Editor::invert_selection ()
1240 {
1241         list<Selectable *> touched;
1242         
1243         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1244                 if ((*iter)->hidden()) {
1245                         continue;
1246                 }
1247                 (*iter)->get_inverted_selectables (*selection, touched);
1248         }
1249
1250         selection->set (touched);
1251 }
1252
1253 bool
1254 Editor::select_all_within (jack_nframes_t start, jack_nframes_t end, double top, double bot, bool add)
1255 {
1256         list<Selectable *> touched;
1257         
1258         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
1259                 if ((*iter)->hidden()) {
1260                         continue;
1261                 }
1262                 (*iter)->get_selectables (start, end, top, bot, touched);
1263         }
1264
1265         if (add) {
1266                 selection->add (touched);
1267         } else {
1268                 selection->set (touched);
1269         }
1270
1271         return !touched.empty();
1272 }
1273
1274 void
1275 Editor::set_selection_from_punch()
1276 {
1277         Location* location;
1278
1279         if ((location = session->locations()->auto_punch_location()) == 0)  {
1280                 return;
1281         }
1282
1283         set_selection_from_range (*location);
1284 }
1285
1286 void
1287 Editor::set_selection_from_loop()
1288 {
1289         Location* location;
1290
1291         if ((location = session->locations()->auto_loop_location()) == 0)  {
1292                 return;
1293         }
1294
1295         set_selection_from_range (*location);
1296 }
1297
1298 void
1299 Editor::set_selection_from_range (Location& range)
1300 {
1301         if (clicked_trackview == 0) {
1302                 return;
1303         }
1304         
1305         begin_reversible_command (_("set selection from range"));
1306         selection->set (0, range.start(), range.end());
1307         commit_reversible_command ();
1308 }
1309
1310 void
1311 Editor::amplitude_zoom_step (bool in)
1312 {
1313         gdouble zoom = 1.0;
1314
1315         if (in) {
1316                 zoom *= 2.0;
1317         } else {
1318                 if (zoom > 2.0) {
1319                         zoom /= 2.0;
1320                 } else {
1321                         zoom = 1.0;
1322                 }
1323         }
1324
1325 #ifdef FIX_FOR_CANVAS
1326         /* XXX DO SOMETHING */
1327 #endif
1328 }       
1329
1330
1331 /* DELETION */
1332
1333
1334 void
1335 Editor::delete_sample_forward ()
1336 {
1337 }
1338
1339 void
1340 Editor::delete_sample_backward ()
1341 {
1342 }
1343
1344 void
1345 Editor::delete_screen ()
1346 {
1347 }
1348
1349 /* SEARCH */
1350
1351 void
1352 Editor::search_backwards ()
1353 {
1354         /* what ? */
1355 }
1356
1357 void
1358 Editor::search_forwards ()
1359 {
1360         /* what ? */
1361 }
1362
1363 /* MARKS */
1364
1365 void
1366 Editor::jump_forward_to_mark ()
1367 {
1368         if (!session) {
1369                 return;
1370         }
1371         
1372         Location *location = session->locations()->first_location_after (playhead_cursor->current_frame);
1373
1374         if (location) {
1375                 session->request_locate (location->start(), session->transport_rolling());
1376         } else {
1377                 session->request_locate (session->current_end_frame());
1378         }
1379 }
1380
1381 void
1382 Editor::jump_backward_to_mark ()
1383 {
1384         if (!session) {
1385                 return;
1386         }
1387
1388         Location *location = session->locations()->first_location_before (playhead_cursor->current_frame);
1389         
1390         if (location) {
1391                 session->request_locate (location->start(), session->transport_rolling());
1392         } else {
1393                 session->request_locate (0);
1394         }
1395 }
1396
1397 void
1398 Editor::set_mark ()
1399 {
1400         jack_nframes_t pos;
1401         float prefix;
1402         bool was_floating;
1403
1404         if (get_prefix (prefix, was_floating)) {
1405                 pos = session->audible_frame ();
1406         } else {
1407                 if (was_floating) {
1408                         pos = (jack_nframes_t) floor (prefix * session->frame_rate ());
1409                 } else {
1410                         pos = (jack_nframes_t) floor (prefix);
1411                 }
1412         }
1413
1414         session->locations()->add (new Location (pos, 0, "mark", Location::IsMark), true);
1415 }
1416
1417 void
1418 Editor::clear_markers ()
1419 {
1420         if (session) {
1421                 session->begin_reversible_command (_("clear markers"));
1422                 session->add_undo (session->locations()->get_memento());
1423                 session->locations()->clear_markers ();
1424                 session->add_redo_no_execute (session->locations()->get_memento());
1425                 session->commit_reversible_command ();
1426         }
1427 }
1428
1429 void
1430 Editor::clear_ranges ()
1431 {
1432         if (session) {
1433                 session->begin_reversible_command (_("clear ranges"));
1434                 session->add_undo (session->locations()->get_memento());
1435                 
1436                 Location * looploc = session->locations()->auto_loop_location();
1437                 Location * punchloc = session->locations()->auto_punch_location();
1438                 
1439                 session->locations()->clear_ranges ();
1440                 // re-add these
1441                 if (looploc) session->locations()->add (looploc);
1442                 if (punchloc) session->locations()->add (punchloc);
1443                 
1444                 session->add_redo_no_execute (session->locations()->get_memento());
1445                 session->commit_reversible_command ();
1446         }
1447 }
1448
1449 void
1450 Editor::clear_locations ()
1451 {
1452         session->begin_reversible_command (_("clear locations"));
1453         session->add_undo (session->locations()->get_memento());
1454         session->locations()->clear ();
1455         session->add_redo_no_execute (session->locations()->get_memento());
1456         session->commit_reversible_command ();
1457         session->locations()->clear ();
1458 }
1459
1460 /* INSERT/REPLACE */
1461
1462 void
1463 Editor::insert_region_list_drag (AudioRegion& region)
1464 {
1465         double x, y;
1466         double wx, wy;
1467         double cx, cy;
1468         TimeAxisView *tv;
1469         jack_nframes_t where;
1470         AudioTimeAxisView *atv = 0;
1471         Playlist *playlist;
1472         
1473         track_canvas.get_pointer (x, y);
1474
1475         track_canvas.window_to_world (x, y, wx, wy);
1476
1477         GdkEvent event;
1478         event.type = GDK_BUTTON_RELEASE;
1479         event.button.x = wx;
1480         event.button.y = wy;
1481         
1482         where = event_frame (&event, &cx, &cy);
1483
1484         if (where < leftmost_frame || where > leftmost_frame + current_page_frames()) {
1485                 /* clearly outside canvas area */
1486                 return;
1487         }
1488         
1489         if ((tv = trackview_by_y_position (cy)) == 0) {
1490                 return;
1491         }
1492         
1493         if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) == 0) {
1494                 return;
1495         }
1496
1497         if ((playlist = atv->playlist()) == 0) {
1498                 return;
1499         }
1500         
1501         snap_to (where);
1502         
1503         begin_reversible_command (_("insert dragged region"));
1504         session->add_undo (playlist->get_memento());
1505         playlist->add_region (*(new AudioRegion (region)), where, 1.0);
1506         session->add_redo_no_execute (playlist->get_memento());
1507         commit_reversible_command ();
1508 }
1509
1510 void
1511 Editor::insert_region_list_selection (float times)
1512 {
1513         AudioTimeAxisView *tv = 0;
1514         Playlist *playlist;
1515
1516         if (clicked_audio_trackview != 0) {
1517                 tv = clicked_audio_trackview;
1518         } else if (!selection->tracks.empty()) {
1519                 if ((tv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front())) == 0) {
1520                         return;
1521                 }
1522         } else {
1523                 return;
1524         }
1525
1526         if ((playlist = tv->playlist()) == 0) {
1527                 return;
1528         }
1529         
1530         Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
1531         
1532         if (selected.count_selected_rows() != 1) {
1533                 return;
1534         }
1535         
1536         TreeModel::iterator i = region_list_display.get_selection()->get_selected();
1537         Region* region = (*i)[region_list_columns.region];
1538
1539         begin_reversible_command (_("insert region"));
1540         session->add_undo (playlist->get_memento());
1541         playlist->add_region (*(createRegion (*region)), edit_cursor->current_frame, times);
1542         session->add_redo_no_execute (playlist->get_memento());
1543         commit_reversible_command ();
1544 }
1545
1546
1547 /* BUILT-IN EFFECTS */
1548
1549 void
1550 Editor::reverse_selection ()
1551 {
1552
1553 }
1554
1555 /* GAIN ENVELOPE EDITING */
1556
1557 void
1558 Editor::edit_envelope ()
1559 {
1560 }
1561
1562 /* PLAYBACK */
1563
1564 void
1565 Editor::toggle_playback (bool with_abort)
1566 {
1567         if (!session) {
1568                 return;
1569         }
1570
1571         switch (session->slave_source()) {
1572         case Session::None:
1573         case Session::JACK:
1574                 break;
1575         default:
1576                 /* transport controlled by the master */
1577                 return;
1578         }
1579
1580         if (session->is_auditioning()) {
1581                 session->cancel_audition ();
1582                 return;
1583         }
1584         
1585         if (session->transport_rolling()) {
1586                 session->request_stop (with_abort);
1587                 if (session->get_auto_loop()) {
1588                         session->request_auto_loop (false);
1589                 }
1590         } else {
1591                 session->request_transport_speed (1.0f);
1592         }
1593 }
1594
1595 void
1596 Editor::play_from_start ()
1597 {
1598         session->request_locate (0, true);
1599 }
1600
1601 void
1602 Editor::play_selection ()
1603 {
1604         if (selection->time.empty()) {
1605                 return;
1606         }
1607
1608         session->request_play_range (true);
1609 }
1610
1611 void
1612 Editor::play_selected_region ()
1613 {
1614         if (!selection->audio_regions.empty()) {
1615                 AudioRegionView *rv = *(selection->audio_regions.begin());
1616
1617                 session->request_bounded_roll (rv->region.position(), rv->region.last_frame()); 
1618         }
1619 }
1620
1621 void
1622 Editor::toggle_loop_playback ()
1623 {
1624         if (session) {
1625                 session->request_auto_loop (true);
1626         }
1627 }
1628
1629 void
1630 Editor::loop_selected_region ()
1631 {
1632         if (!selection->audio_regions.empty()) {
1633                 AudioRegionView *rv = *(selection->audio_regions.begin());
1634                 Location* tll;
1635
1636                 if ((tll = transport_loop_location()) != 0)  {
1637
1638                         tll->set (rv->region.position(), rv->region.last_frame());
1639                         
1640                         // enable looping, reposition and start rolling
1641
1642                         session->request_auto_loop (true);
1643                         session->request_locate (tll->start(), false);
1644                         session->request_transport_speed (1.0f);
1645                 }
1646         }
1647 }
1648
1649 void
1650 Editor::play_location (Location& location)
1651 {
1652         if (location.start() <= location.end()) {
1653                 return;
1654         }
1655
1656         session->request_bounded_roll (location.start(), location.end());
1657 }
1658
1659 void
1660 Editor::loop_location (Location& location)
1661 {
1662         if (location.start() <= location.end()) {
1663                 return;
1664         }
1665
1666         Location* tll;
1667
1668         if ((tll = transport_loop_location()) != 0) {
1669                 tll->set (location.start(), location.end());
1670
1671                 // enable looping, reposition and start rolling
1672                 session->request_auto_loop (true);
1673                 session->request_locate (tll->start(), true);
1674         }
1675 }
1676
1677 void 
1678 Editor::toggle_region_mute ()
1679 {
1680         if (clicked_regionview) {
1681                 clicked_regionview->region.set_muted (!clicked_regionview->region.muted());
1682         } else if (!selection->audio_regions.empty()) {
1683                 bool yn = ! (*selection->audio_regions.begin())->region.muted();
1684                 selection->foreach_audio_region (&AudioRegion::set_muted, yn);
1685         }
1686 }
1687
1688 void
1689 Editor::toggle_region_opaque ()
1690 {
1691         if (clicked_regionview) {
1692                 clicked_regionview->region.set_opaque (!clicked_regionview->region.opaque());
1693         } else if (!selection->audio_regions.empty()) {
1694                 bool yn = ! (*selection->audio_regions.begin())->region.opaque();
1695                 selection->foreach_audio_region (&Region::set_opaque, yn);
1696         }
1697 }
1698
1699 void
1700 Editor::raise_region ()
1701 {
1702         selection->foreach_audio_region (&Region::raise);
1703 }
1704
1705 void
1706 Editor::raise_region_to_top ()
1707 {
1708         selection->foreach_audio_region (&Region::raise_to_top);
1709 }
1710
1711 void
1712 Editor::lower_region ()
1713 {
1714         selection->foreach_audio_region (&Region::lower);
1715 }
1716
1717 void
1718 Editor::lower_region_to_bottom ()
1719 {
1720         selection->foreach_audio_region (&Region::lower_to_bottom);
1721 }
1722
1723 void
1724 Editor::edit_region ()
1725 {
1726         if (clicked_regionview == 0) {
1727                 return;
1728         }
1729         
1730         clicked_regionview->show_region_editor ();
1731 }
1732
1733 void
1734 Editor::rename_region ()
1735 {
1736         Dialog dialog;
1737         Entry  entry;
1738         Button ok_button (_("OK"));
1739         Button cancel_button (_("Cancel"));
1740
1741         if (selection->audio_regions.empty()) {
1742                 return;
1743         }
1744
1745         dialog.set_title (_("ardour: rename region"));
1746         dialog.set_name ("RegionRenameWindow");
1747         dialog.set_size_request (300, -1);
1748         dialog.set_position (Gtk::WIN_POS_MOUSE);
1749         dialog.set_modal (true);
1750
1751         dialog.get_vbox()->set_border_width (10);
1752         dialog.get_vbox()->pack_start (entry);
1753         dialog.get_action_area()->pack_start (ok_button);
1754         dialog.get_action_area()->pack_start (cancel_button);
1755
1756         entry.set_name ("RegionNameDisplay");
1757         ok_button.set_name ("EditorGTKButton");
1758         cancel_button.set_name ("EditorGTKButton");
1759
1760         region_renamed = false;
1761
1762         entry.signal_activate().connect (bind (mem_fun(*this, &Editor::rename_region_finished), true));
1763         ok_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::rename_region_finished), true));
1764         cancel_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::rename_region_finished), false));
1765
1766         /* recurse */
1767
1768         dialog.show_all ();
1769         ARDOUR_UI::instance()->allow_focus (true);
1770         Main::run ();
1771         ARDOUR_UI::instance()->allow_focus (false);
1772
1773         if (region_renamed) {
1774                 (*selection->audio_regions.begin())->region.set_name (entry.get_text());
1775                 redisplay_regions ();
1776         }
1777 }
1778
1779 void
1780 Editor::rename_region_finished (bool status)
1781
1782 {
1783         region_renamed = status;
1784         Main::quit ();
1785 }
1786
1787 void
1788 Editor::audition_playlist_region_via_route (AudioRegion& region, Route& route)
1789 {
1790         if (session->is_auditioning()) {
1791                 session->cancel_audition ();
1792         } 
1793
1794         // note: some potential for creativity here, because region doesn't
1795         // have to belong to the playlist that Route is handling
1796
1797         // bool was_soloed = route.soloed();
1798
1799         route.set_solo (true, this);
1800         
1801         session->request_bounded_roll (region.position(), region.position() + region.length());
1802         
1803         /* XXX how to unset the solo state ? */
1804 }
1805
1806 void
1807 Editor::audition_selected_region ()
1808 {
1809         if (!selection->audio_regions.empty()) {
1810                 AudioRegionView* rv = *(selection->audio_regions.begin());
1811                 session->audition_region (rv->region);
1812         }
1813 }
1814
1815 void
1816 Editor::audition_playlist_region_standalone (AudioRegion& region)
1817 {
1818         session->audition_region (region);
1819 }
1820
1821 void
1822 Editor::build_interthread_progress_window ()
1823 {
1824         interthread_progress_window = new ArdourDialog (X_("interthread progress"));
1825
1826         interthread_progress_bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
1827         
1828         interthread_progress_vbox.set_border_width (10);
1829         interthread_progress_vbox.set_spacing (5);
1830         interthread_progress_vbox.pack_start (interthread_progress_label, false, false);
1831         interthread_progress_vbox.pack_start (interthread_progress_bar,false, false);
1832         interthread_progress_vbox.pack_start (interthread_cancel_button,false, false);
1833
1834         interthread_cancel_button.add (interthread_cancel_label);
1835
1836         interthread_cancel_button.signal_clicked().connect (mem_fun(*this, &Editor::interthread_cancel_clicked));
1837         
1838         interthread_progress_window->set_modal (true);
1839         interthread_progress_window->set_default_size (200, 100);
1840         interthread_progress_window->add (interthread_progress_vbox);
1841 }
1842
1843 void
1844 Editor::interthread_cancel_clicked ()
1845 {
1846         if (current_interthread_info) {
1847                 current_interthread_info->cancel = true;
1848         }
1849 }
1850
1851 void *
1852 Editor::_import_thread (void *arg)
1853 {
1854         PBD::ThreadCreated (pthread_self(), X_("Import"));
1855
1856         Editor *ed = (Editor *) arg;
1857         return ed->import_thread ();
1858 }
1859
1860 void *
1861 Editor::import_thread ()
1862 {
1863         session->import_audiofile (import_status);
1864         return 0;
1865 }
1866
1867 gint
1868 Editor::import_progress_timeout (void *arg)
1869 {
1870         interthread_progress_label.set_text (import_status.doing_what);
1871
1872         if (import_status.freeze) {
1873                 interthread_cancel_button.set_sensitive(false);
1874         } else {
1875                 interthread_cancel_button.set_sensitive(true);
1876         }
1877
1878         if (import_status.doing_what == "building peak files") {
1879                 interthread_progress_bar.pulse ();
1880                 return FALSE;
1881         } else {
1882                 interthread_progress_bar.set_fraction (import_status.progress/100);
1883         }
1884
1885         return !(import_status.done || import_status.cancel);
1886 }
1887
1888 void
1889 Editor::import_audio (bool as_tracks)
1890 {
1891         if (session == 0) {
1892                 warning << _("You can't import an audiofile until you have a session loaded.") << endmsg;
1893                 return;
1894         }
1895
1896         string str;
1897
1898         if (as_tracks) {
1899                 str =_("Import selected as tracks");
1900         } else {
1901                 str = _("Import selected to region list");
1902         }
1903
1904         SoundFileOmega sfdb (str);
1905         sfdb.Imported.connect (bind (mem_fun (*this, &Editor::do_import), as_tracks));
1906
1907         sfdb.run();
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         SoundFileOmega sfdb (_("Add to External Region list"));
2036         sfdb.Embedded.connect (mem_fun (*this, &Editor::do_embed_sndfiles));
2037
2038         sfdb.run ();
2039 }
2040
2041 void
2042 Editor::do_embed_sndfiles (vector<string> paths, bool split)
2043 {
2044         bool multiple_files = paths.size() > 1;
2045         bool check_sample_rate = true;
2046
2047         for (vector<string>::iterator i = paths.begin(); i != paths.end(); ++i) {
2048                 embed_sndfile (*i, split, multiple_files, check_sample_rate);
2049         }
2050
2051         session->save_state ("");
2052 }
2053
2054 void
2055 Editor::embed_sndfile (string path, bool split, bool multiple_files, bool& check_sample_rate)
2056 {
2057         SndFileSource *source = 0; /* keep g++ quiet */
2058         AudioRegion::SourceList sources;
2059         string idspec;
2060         string linked_path;
2061         SNDFILE *sf;
2062         SF_INFO finfo;
2063
2064         /* lets see if we can link it into the session */
2065         
2066         linked_path = session->sound_dir();
2067         linked_path += PBD::basename (path);
2068
2069         if (link (path.c_str(), linked_path.c_str()) == 0) {
2070
2071                 /* there are many reasons why link(2) might have failed.
2072                    but if it succeeds, we now have a link in the
2073                    session sound dir that will protect against
2074                    unlinking of the original path. nice.
2075                 */
2076
2077                 path = linked_path;
2078         }
2079
2080         memset (&finfo, 0, sizeof(finfo));
2081
2082         /* note that we temporarily truncated _id at the colon */
2083         
2084         if ((sf = sf_open (path.c_str(), SFM_READ, &finfo)) == 0) {
2085                 char errbuf[256];
2086                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
2087                 error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), selection, errbuf) << endmsg;
2088                 return;
2089         }
2090         sf_close (sf);
2091         sf = 0;
2092         
2093         if (check_sample_rate) {
2094                 switch (reject_because_rate_differs (path, finfo, "Embed", multiple_files)) {
2095                 case 0:
2096                         break;
2097                 case 1:
2098                         return;
2099                 case -1:
2100                         check_sample_rate = false;
2101                         break;
2102                         
2103                 case -2:
2104                 default:
2105                         return;
2106                 }
2107         }
2108
2109         track_canvas_scroller.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
2110         ARDOUR_UI::instance()->flush_pending ();
2111
2112         /* make the proper number of channels in the region */
2113
2114         for (int n=0; n < finfo.channels; ++n)
2115         {
2116                 idspec = path;
2117                 idspec += string_compose(":%1", n);
2118                 
2119                 try {
2120                         source = new SndFileSource (idspec.c_str());
2121                         sources.push_back(source);
2122                 } 
2123
2124                 catch (failed_constructor& err) {
2125                         error << string_compose(_("could not open %1"), path) << endmsg;
2126                         goto out;
2127                 }
2128
2129                 ARDOUR_UI::instance()->flush_pending ();
2130         }
2131
2132         if (sources.size() > 0) {
2133
2134                 string region_name = PBD::basename_nosuffix (path);
2135                 region_name += "-0";
2136
2137                 /* The created region isn't dropped.  It emits a signal
2138                    that is picked up by the session. 
2139                 */
2140
2141                 new AudioRegion (sources, 0, sources[0]->length(), region_name, 0,
2142                                  Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
2143                 
2144                 /* make sure we can see it in the list */
2145
2146                 /* its the second node, always */
2147
2148                 // GTK2FIX ?? is it still always the 2nd node
2149
2150                 TreeModel::Path path ("2");
2151                 region_list_display.expand_row (path, true);
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::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         Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
2606
2607         if (selected.count_selected_rows() != 1) {
2608                 return;
2609         }
2610
2611         TreeModel::iterator i = region_list_display.get_selection()->get_selected();
2612         region = (*i)[region_list_columns.region];
2613
2614         jack_nframes_t start = selection->time[clicked_selection].start;
2615         jack_nframes_t end = selection->time[clicked_selection].end;
2616
2617         Playlist *playlist; 
2618
2619         if (selection->tracks.empty()) {
2620                 return;
2621         }
2622
2623         jack_nframes_t selection_length = end - start;
2624         float times = (float)selection_length / region->length();
2625         
2626         begin_reversible_command (_("fill selection"));
2627         
2628         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
2629
2630                 if ((playlist = (*i)->playlist()) == 0) {
2631                         continue;
2632                 }               
2633                 
2634                 session->add_undo (playlist->get_memento());
2635                 playlist->add_region (*(createRegion (*region)), start, times);
2636                 session->add_redo_no_execute (playlist->get_memento());
2637         }
2638         
2639         commit_reversible_command ();                   
2640 }
2641         
2642 void
2643 Editor::set_region_sync_from_edit_cursor ()
2644 {
2645         if (clicked_regionview == 0) {
2646                 return;
2647         }
2648
2649         if (!clicked_regionview->region.covers (edit_cursor->current_frame)) {
2650                 error << _("Place the edit cursor at the desired sync point") << endmsg;
2651                 return;
2652         }
2653
2654         Region& region (clicked_regionview->region);
2655
2656         begin_reversible_command (_("set sync from edit cursor"));
2657         session->add_undo (region.playlist()->get_memento());
2658         region.set_sync_position (edit_cursor->current_frame);
2659         session->add_redo_no_execute (region.playlist()->get_memento());
2660         commit_reversible_command ();
2661 }
2662
2663 void
2664 Editor::remove_region_sync ()
2665 {
2666         if (clicked_regionview) {
2667                 Region& region (clicked_regionview->region);
2668                 begin_reversible_command (_("remove sync"));
2669                 session->add_undo (region.playlist()->get_memento());
2670                 region.clear_sync_position ();
2671                 session->add_redo_no_execute (region.playlist()->get_memento());
2672                 commit_reversible_command ();
2673         }
2674 }
2675
2676 void
2677 Editor::naturalize ()
2678 {
2679         if (selection->audio_regions.empty()) {
2680                 return;
2681         }
2682         begin_reversible_command (_("naturalize"));
2683         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
2684                 session->add_undo ((*i)->region.get_memento());
2685                 (*i)->region.move_to_natural_position (this);
2686                 session->add_redo_no_execute ((*i)->region.get_memento());
2687         }
2688         commit_reversible_command ();
2689 }
2690
2691 void
2692 Editor::align (RegionPoint what)
2693 {
2694         align_selection (what, edit_cursor->current_frame);
2695 }
2696
2697 void
2698 Editor::align_relative (RegionPoint what)
2699 {
2700         align_selection_relative (what, edit_cursor->current_frame);
2701 }
2702
2703 struct RegionSortByTime {
2704     bool operator() (const AudioRegionView* a, const AudioRegionView* b) {
2705             return a->region.position() < b->region.position();
2706     }
2707 };
2708
2709 void
2710 Editor::align_selection_relative (RegionPoint point, jack_nframes_t position)
2711 {
2712         if (selection->audio_regions.empty()) {
2713                 return;
2714         }
2715
2716         jack_nframes_t distance;
2717         jack_nframes_t pos = 0;
2718         int dir;
2719
2720         list<AudioRegionView*> sorted;
2721         selection->audio_regions.by_position (sorted);
2722         Region& r ((*sorted.begin())->region);
2723
2724         switch (point) {
2725         case Start:
2726                 pos = r.first_frame ();
2727                 break;
2728
2729         case End:
2730                 pos = r.last_frame();
2731                 break;
2732
2733         case SyncPoint:
2734                 pos = r.adjust_to_sync (r.first_frame());
2735                 break;  
2736         }
2737
2738         if (pos > position) {
2739                 distance = pos - position;
2740                 dir = -1;
2741         } else {
2742                 distance = position - pos;
2743                 dir = 1;
2744         }
2745
2746         begin_reversible_command (_("align selection (relative)"));
2747
2748         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
2749
2750                 Region& region ((*i)->region);
2751
2752                 session->add_undo (region.playlist()->get_memento());
2753                 
2754                 if (dir > 0) {
2755                         region.set_position (region.position() + distance, this);
2756                 } else {
2757                         region.set_position (region.position() - distance, this);
2758                 }
2759
2760                 session->add_redo_no_execute (region.playlist()->get_memento());
2761
2762         }
2763
2764         commit_reversible_command ();
2765 }
2766
2767 void
2768 Editor::align_selection (RegionPoint point, jack_nframes_t position)
2769 {
2770         if (selection->audio_regions.empty()) {
2771                 return;
2772         }
2773
2774         begin_reversible_command (_("align selection"));
2775
2776         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
2777                 align_region_internal ((*i)->region, point, position);
2778         }
2779
2780         commit_reversible_command ();
2781 }
2782
2783 void
2784 Editor::align_region (Region& region, RegionPoint point, jack_nframes_t position)
2785 {
2786         begin_reversible_command (_("align region"));
2787         align_region_internal (region, point, position);
2788         commit_reversible_command ();
2789 }
2790
2791 void
2792 Editor::align_region_internal (Region& region, RegionPoint point, jack_nframes_t position)
2793 {
2794         session->add_undo (region.playlist()->get_memento());
2795
2796         switch (point) {
2797         case SyncPoint:
2798                 region.set_position (region.adjust_to_sync (position), this);
2799                 break;
2800
2801         case End:
2802                 if (position > region.length()) {
2803                         region.set_position (position - region.length(), this);
2804                 }
2805                 break;
2806
2807         case Start:
2808                 region.set_position (position, this);
2809                 break;
2810         }
2811
2812         session->add_redo_no_execute (region.playlist()->get_memento());
2813 }       
2814
2815 void
2816 Editor::trim_region_to_edit_cursor ()
2817 {
2818         if (clicked_regionview == 0) {
2819                 return;
2820         }
2821
2822         Region& region (clicked_regionview->region);
2823
2824         float speed = 1.0f;
2825         AudioTimeAxisView *atav;
2826
2827         if ( clicked_trackview != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(clicked_trackview)) != 0 ) {
2828                 if (atav->get_diskstream() != 0) {
2829                         speed = atav->get_diskstream()->speed();
2830                 }
2831         }
2832
2833         begin_reversible_command (_("trim to edit"));
2834         session->add_undo (region.playlist()->get_memento());
2835         region.trim_end( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
2836         session->add_redo_no_execute (region.playlist()->get_memento());
2837         commit_reversible_command ();
2838 }
2839
2840 void
2841 Editor::trim_region_from_edit_cursor ()
2842 {
2843         if (clicked_regionview == 0) {
2844                 return;
2845         }
2846
2847         Region& region (clicked_regionview->region);
2848
2849         float speed = 1.0f;
2850         AudioTimeAxisView *atav;
2851
2852         if ( clicked_trackview != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(clicked_trackview)) != 0 ) {
2853                 if (atav->get_diskstream() != 0) {
2854                         speed = atav->get_diskstream()->speed();
2855                 }
2856         }
2857
2858         begin_reversible_command (_("trim to edit"));
2859         session->add_undo (region.playlist()->get_memento());
2860         region.trim_end( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
2861         session->add_redo_no_execute (region.playlist()->get_memento());
2862         commit_reversible_command ();
2863 }
2864
2865 void
2866 Editor::unfreeze_route ()
2867 {
2868         if (clicked_audio_trackview == 0 || !clicked_audio_trackview->is_audio_track()) {
2869                 return;
2870         }
2871         
2872         clicked_audio_trackview->audio_track()->unfreeze ();
2873 }
2874
2875 void*
2876 Editor::_freeze_thread (void* arg)
2877 {
2878         PBD::ThreadCreated (pthread_self(), X_("Freeze"));
2879         return static_cast<Editor*>(arg)->freeze_thread ();
2880 }
2881
2882 void*
2883 Editor::freeze_thread ()
2884 {
2885         clicked_audio_trackview->audio_track()->freeze (*current_interthread_info);
2886         return 0;
2887 }
2888
2889 gint
2890 Editor::freeze_progress_timeout (void *arg)
2891 {
2892         interthread_progress_bar.set_fraction (current_interthread_info->progress/100);
2893         return !(current_interthread_info->done || current_interthread_info->cancel);
2894 }
2895
2896 void
2897 Editor::freeze_route ()
2898 {
2899         if (clicked_audio_trackview == 0 || !clicked_audio_trackview->is_audio_track()) {
2900                 return;
2901         }
2902         
2903         InterThreadInfo itt;
2904
2905         if (interthread_progress_window == 0) {
2906                 build_interthread_progress_window ();
2907         }
2908         
2909         interthread_progress_window->set_title (_("ardour: freeze"));
2910         interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
2911         interthread_progress_window->show_all ();
2912         interthread_progress_bar.set_fraction (0.0f);
2913         interthread_progress_label.set_text ("");
2914         interthread_cancel_label.set_text (_("Cancel Freeze"));
2915         current_interthread_info = &itt;
2916
2917         interthread_progress_connection = 
2918           Glib::signal_timeout().connect (bind (mem_fun(*this, &Editor::freeze_progress_timeout), (gpointer) 0), 100);
2919
2920         itt.done = false;
2921         itt.cancel = false;
2922         itt.progress = 0.0f;
2923
2924         pthread_create (&itt.thread, 0, _freeze_thread, this);
2925
2926         track_canvas_scroller.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
2927
2928         while (!itt.done && !itt.cancel) {
2929                 gtk_main_iteration ();
2930         }
2931
2932         interthread_progress_connection.disconnect ();
2933         interthread_progress_window->hide_all ();
2934         current_interthread_info = 0;
2935         track_canvas_scroller.get_window()->set_cursor (*current_canvas_cursor);
2936 }
2937
2938 void
2939 Editor::bounce_range_selection ()
2940 {
2941         if (selection->time.empty()) {
2942                 return;
2943         }
2944
2945         TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
2946
2947         jack_nframes_t start = selection->time[clicked_selection].start;
2948         jack_nframes_t end = selection->time[clicked_selection].end;
2949         jack_nframes_t cnt = end - start + 1;
2950         
2951         begin_reversible_command (_("bounce range"));
2952
2953         for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
2954
2955                 AudioTimeAxisView* atv;
2956
2957                 if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) == 0) {
2958                         continue;
2959                 }
2960                 
2961                 Playlist* playlist;
2962                 
2963                 if ((playlist = atv->playlist()) == 0) {
2964                         return;
2965                 }
2966
2967                 InterThreadInfo itt;
2968                 
2969                 itt.done = false;
2970                 itt.cancel = false;
2971                 itt.progress = false;
2972                 
2973                 session->add_undo (playlist->get_memento());
2974                 atv->audio_track()->bounce_range (start, cnt, itt);
2975                 session->add_redo_no_execute (playlist->get_memento());
2976         }
2977         
2978         commit_reversible_command ();
2979         
2980         delete views;
2981 }
2982
2983 void
2984 Editor::cut ()
2985 {
2986         cut_copy (Cut);
2987 }
2988
2989 void
2990 Editor::copy ()
2991 {
2992         cut_copy (Copy);
2993 }
2994
2995 void 
2996 Editor::cut_copy (CutCopyOp op)
2997 {
2998         /* only cancel selection if cut/copy is successful.*/
2999
3000         string opname;
3001
3002         switch (op) {
3003         case Cut:
3004                 opname = _("cut");
3005                 break;
3006         case Copy:
3007                 opname = _("copy");
3008                 break;
3009         case Clear:
3010                 opname = _("clear");
3011                 break;
3012         }
3013         
3014         cut_buffer->clear ();
3015
3016         switch (current_mouse_mode()) {
3017         case MouseObject: 
3018                 if (!selection->audio_regions.empty() || !selection->points.empty()) {
3019
3020                         begin_reversible_command (opname + _(" objects"));
3021
3022                         if (!selection->audio_regions.empty()) {
3023                                 
3024                                 cut_copy_regions (op);
3025                                 
3026                                 if (op == Cut) {
3027                                         selection->clear_audio_regions ();
3028                                 }
3029                         }
3030
3031                         if (!selection->points.empty()) {
3032                                 cut_copy_points (op);
3033
3034                                 if (op == Cut) {
3035                                         selection->clear_points ();
3036                                 }
3037                         }
3038
3039                         commit_reversible_command ();   
3040                 }
3041                 break;
3042                 
3043         case MouseRange:
3044                 if (!selection->time.empty()) {
3045
3046                         begin_reversible_command (opname + _(" range"));
3047                         cut_copy_ranges (op);
3048                         commit_reversible_command ();
3049
3050                         if (op == Cut) {
3051                                 selection->clear_time ();
3052                         }
3053                         
3054                 }
3055                 break;
3056                 
3057         default:
3058                 break;
3059         }
3060 }
3061
3062 void
3063 Editor::cut_copy_points (CutCopyOp op)
3064 {
3065         for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
3066
3067                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
3068
3069                 if (atv) {
3070                         atv->cut_copy_clear_objects (selection->points, op);
3071                 } 
3072         }
3073 }
3074
3075 void
3076 Editor::cut_copy_regions (CutCopyOp op)
3077 {
3078         typedef std::map<AudioPlaylist*,AudioPlaylist*> PlaylistMapping;
3079         PlaylistMapping pmap;
3080         jack_nframes_t first_position = max_frames;
3081         set<Playlist*> freezelist;
3082         pair<set<Playlist*>::iterator,bool> insert_result;
3083
3084         for (AudioRegionSelection::iterator x = selection->audio_regions.begin(); x != selection->audio_regions.end(); ++x) {
3085                 first_position = min ((*x)->region.position(), first_position);
3086
3087                 if (op == Cut || op == Clear) {
3088                         AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region.playlist());
3089                         if (pl) {
3090                                 insert_result = freezelist.insert (pl);
3091                                 if (insert_result.second) {
3092                                         pl->freeze ();
3093                                         session->add_undo (pl->get_memento());
3094                                 }
3095                         }
3096                 }
3097         }
3098
3099         for (AudioRegionSelection::iterator x = selection->audio_regions.begin(); x != selection->audio_regions.end(); ) {
3100
3101                 AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region.playlist());
3102                 AudioPlaylist* npl;
3103                 AudioRegionSelection::iterator tmp;
3104                 
3105                 tmp = x;
3106                 ++tmp;
3107
3108                 if (pl) {
3109
3110                         PlaylistMapping::iterator pi = pmap.find (pl);
3111                         
3112                         if (pi == pmap.end()) {
3113                                 npl = new AudioPlaylist (*session, "cutlist", true);
3114                                 npl->freeze();
3115                                 pmap[pl] = npl;
3116                         } else {
3117                                 npl = pi->second;
3118                         }
3119
3120                         switch (op) {
3121                         case Cut:
3122                                 npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
3123                                 pl->remove_region (&((*x)->region));
3124                                 break;
3125
3126                         case Copy:
3127                                 npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
3128                                 break;
3129
3130                         case Clear:
3131                                 pl->remove_region (&((*x)->region));
3132                                 break;
3133                         }
3134                 }
3135
3136                 x = tmp;
3137         }
3138
3139         list<Playlist*> foo;
3140
3141         for (PlaylistMapping::iterator i = pmap.begin(); i != pmap.end(); ++i) {
3142                 foo.push_back (i->second);
3143         }
3144
3145         if (!foo.empty()) {
3146                 cut_buffer->set (foo);
3147         }
3148         
3149         for (set<Playlist*>::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
3150                 (*pl)->thaw ();
3151                 session->add_redo_no_execute ((*pl)->get_memento());
3152         }
3153 }
3154
3155 void
3156 Editor::cut_copy_ranges (CutCopyOp op)
3157 {
3158         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3159                 (*i)->cut_copy_clear (*selection, op);
3160         }
3161 }
3162
3163 void
3164 Editor::paste (float times)
3165 {
3166         paste_internal (edit_cursor->current_frame, times);
3167 }
3168
3169 void
3170 Editor::mouse_paste ()
3171 {
3172         double x, y;
3173         double wx, wy;
3174         track_canvas.get_pointer (x, y);
3175         track_canvas.window_to_world (x, y, wx, wy);
3176         GdkEvent event;
3177         event.type = GDK_BUTTON_RELEASE;
3178         event.button.x = wx;
3179         event.button.y = wy;
3180         
3181         jack_nframes_t where = event_frame (&event, 0, 0);
3182         snap_to (where);
3183         paste_internal (where, 1);
3184 }
3185
3186 void
3187 Editor::paste_internal (jack_nframes_t position, float times)
3188 {
3189         bool commit = false;
3190
3191         if (cut_buffer->empty() || selection->tracks.empty()) {
3192                 return;
3193         }
3194
3195         if (position == max_frames) {
3196                 position = edit_cursor->current_frame;
3197         }
3198
3199         begin_reversible_command (_("paste"));
3200
3201         TrackSelection::iterator i;
3202         size_t nth;
3203
3204         for (nth = 0, i = selection->tracks.begin(); i != selection->tracks.end(); ++i, ++nth) {
3205                 
3206                 /* undo/redo is handled by individual tracks */
3207
3208                 if ((*i)->paste (position, times, *cut_buffer, nth)) {
3209                         commit = true;
3210                 }
3211         }
3212
3213         if (commit) {
3214                 commit_reversible_command ();
3215         }
3216 }
3217
3218 void
3219 Editor::paste_named_selection (float times)
3220 {
3221         TrackSelection::iterator i;
3222
3223         Glib::RefPtr<TreeSelection> selected = named_selection_display.get_selection();
3224
3225         if (selected.count_selected_rows() == 0 || selection->tracks.empty()) {
3226                 return;
3227         }
3228
3229         TreeModel::iterator i = selected->get_selected();
3230         NamedSelection* ns = (*i)[named_selection_columns.selection];
3231
3232         list<Playlist*>::iterator chunk;
3233         list<Playlist*>::iterator tmp;
3234
3235         chunk = ns->playlists.begin();
3236                 
3237         begin_reversible_command (_("paste chunk"));
3238
3239         for (i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3240                 
3241                 AudioTimeAxisView* atv;
3242                 Playlist* pl;
3243                 AudioPlaylist* apl;
3244
3245                 if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) == 0) {
3246                         continue;
3247                 }
3248
3249                 if ((pl = atv->playlist()) == 0) {
3250                         continue;
3251                 }
3252
3253                 if ((apl = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
3254                         continue;
3255                 }
3256
3257                 tmp = chunk;
3258                 ++tmp;
3259
3260                 session->add_undo (apl->get_memento());
3261                 apl->paste (**chunk, edit_cursor->current_frame, times);
3262                 session->add_redo_no_execute (apl->get_memento());
3263
3264                 if (tmp != ns->playlists.end()) {
3265                         chunk = tmp;
3266                 }
3267         }
3268
3269         commit_reversible_command();
3270 }
3271
3272 void
3273 Editor::duplicate_some_regions (AudioRegionSelection& regions, float times)
3274 {
3275         Playlist *playlist; 
3276         
3277         begin_reversible_command (_("duplicate region"));
3278
3279         for (AudioRegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
3280
3281                 Region& r ((*i)->region);
3282                 
3283                 playlist = (*i)->region.playlist();
3284                 session->add_undo (playlist->get_memento());
3285                 playlist->duplicate (r, r.last_frame(), times);
3286                 session->add_redo_no_execute (playlist->get_memento());
3287
3288         }
3289
3290         commit_reversible_command ();
3291 }
3292
3293 void
3294 Editor::duplicate_selection (float times)
3295 {
3296         if (selection->time.empty() || selection->tracks.empty()) {
3297                 return;
3298         }
3299
3300         Playlist *playlist; 
3301         vector<AudioRegion*> new_regions;
3302         vector<AudioRegion*>::iterator ri;
3303                 
3304         create_region_from_selection (new_regions);
3305
3306         if (new_regions.empty()) {
3307                 return;
3308         }
3309         
3310         begin_reversible_command (_("duplicate selection"));
3311
3312         ri = new_regions.begin();
3313
3314         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3315                 if ((playlist = (*i)->playlist()) == 0) {
3316                         continue;
3317                 }
3318                 session->add_undo (playlist->get_memento());
3319                 playlist->duplicate (**ri, selection->time[clicked_selection].end, times);
3320                 session->add_redo_no_execute (playlist->get_memento());
3321
3322                 ++ri;
3323                 if (ri == new_regions.end()) {
3324                         --ri;
3325                 }
3326         }
3327
3328         commit_reversible_command ();
3329 }
3330
3331 void
3332 Editor::center_playhead ()
3333 {
3334         float page = canvas_width * frames_per_unit;
3335
3336         center_screen_internal (playhead_cursor->current_frame, page);
3337 }
3338
3339 void
3340 Editor::center_edit_cursor ()
3341 {
3342         float page = canvas_width * frames_per_unit;
3343
3344         center_screen_internal (edit_cursor->current_frame, page);
3345 }
3346
3347 void
3348 Editor::clear_playlist (Playlist& playlist)
3349 {
3350         begin_reversible_command (_("clear playlist"));
3351         session->add_undo (playlist.get_memento());
3352         playlist.clear ();
3353         session->add_redo_no_execute (playlist.get_memento());
3354         commit_reversible_command ();
3355 }
3356
3357 void
3358 Editor::nudge_track (bool use_edit_cursor, bool forwards)
3359 {
3360         Playlist *playlist; 
3361         jack_nframes_t distance;
3362         jack_nframes_t next_distance;
3363         jack_nframes_t start;
3364
3365         if (use_edit_cursor) {
3366                 start = edit_cursor->current_frame;
3367         } else {
3368                 start = 0;
3369         }
3370
3371         if ((distance = get_nudge_distance (start, next_distance)) == 0) {
3372                 return;
3373         }
3374         
3375         if (selection->tracks.empty()) {
3376                 return;
3377         }
3378         
3379         begin_reversible_command (_("nudge track"));
3380         
3381         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3382
3383                 if ((playlist = (*i)->playlist()) == 0) {
3384                         continue;
3385                 }               
3386                 
3387                 session->add_undo (playlist->get_memento());
3388                 playlist->nudge_after (start, distance, forwards);
3389                 session->add_redo_no_execute (playlist->get_memento());
3390         }
3391         
3392         commit_reversible_command ();                   
3393 }
3394
3395 void
3396 Editor::toggle_xfades_active ()
3397 {
3398         if (session) {
3399                 session->set_crossfades_active (!session->get_crossfades_active());
3400         }
3401 }
3402
3403 void
3404 Editor::toggle_follow_playhead ()
3405 {
3406         set_follow_playhead (!_follow_playhead);
3407 }
3408
3409 void
3410 Editor::set_xfade_visibility (bool yn)
3411 {
3412         
3413 }
3414
3415 void
3416 Editor::toggle_xfade_visibility ()
3417 {
3418         set_xfade_visibility (!xfade_visibility());
3419 }
3420
3421 void
3422 Editor::remove_last_capture ()
3423 {
3424         vector<string> choices;
3425         string prompt;
3426         
3427         if (!session) {
3428                 return;
3429         }
3430
3431         if (Config->get_verify_remove_last_capture()) {
3432                 prompt  = _("Do you really want to destroy the last capture?"
3433                             "\n(This is destructive and cannot be undone)");
3434
3435                 choices.push_back (_("Yes, destroy it."));
3436                 choices.push_back (_("No, do nothing."));
3437
3438                 Gtkmm2ext::Choice prompter (prompt, choices);
3439                 prompter.done.connect (Gtk::Main::quit.slot());
3440                 prompter.show_all ();
3441
3442                 Gtk::Main::run ();
3443
3444                 if (prompter.get_choice() == 0) {
3445                         session->remove_last_capture ();
3446                 }
3447         } else {
3448                 session->remove_last_capture();
3449         }
3450 }
3451
3452 void
3453 Editor::normalize_region ()
3454 {
3455         if (!session) {
3456                 return;
3457         }
3458
3459         if (selection->audio_regions.empty()) {
3460                 return;
3461         }
3462
3463         begin_reversible_command (_("normalize"));
3464
3465         track_canvas_scroller.get_window()->set_cursor (*wait_cursor);
3466         gdk_flush ();
3467
3468         for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
3469                 session->add_undo ((*r)->region.get_memento());
3470                 (*r)->region.normalize_to (0.0f);
3471                 session->add_redo_no_execute ((*r)->region.get_memento());
3472         }
3473
3474         commit_reversible_command ();
3475         track_canvas_scroller.get_window()->set_cursor (*current_canvas_cursor);
3476 }
3477
3478
3479 void
3480 Editor::denormalize_region ()
3481 {
3482         if (!session) {
3483                 return;
3484         }
3485
3486         if (selection->audio_regions.empty()) {
3487                 return;
3488         }
3489
3490         begin_reversible_command ("denormalize");
3491
3492         for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
3493                 session->add_undo ((*r)->region.get_memento());
3494                 (*r)->region.set_scale_amplitude (1.0f);
3495                 session->add_redo_no_execute ((*r)->region.get_memento());
3496         }
3497
3498         commit_reversible_command ();
3499 }
3500
3501
3502 void
3503 Editor::reverse_region ()
3504 {
3505         if (!session) {
3506                 return;
3507         }
3508
3509         Reverse rev (*session);
3510         apply_filter (rev, _("reverse regions"));
3511 }
3512
3513 void
3514 Editor::apply_filter (AudioFilter& filter, string command)
3515 {
3516         if (selection->audio_regions.empty()) {
3517                 return;
3518         }
3519
3520         begin_reversible_command (command);
3521
3522         track_canvas_scroller.get_window()->set_cursor (*wait_cursor);
3523         gdk_flush ();
3524
3525         for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ) {
3526
3527                 AudioRegion& region ((*r)->region);
3528                 Playlist* playlist = region.playlist();
3529
3530                 AudioRegionSelection::iterator tmp;
3531                 
3532                 tmp = r;
3533                 ++tmp;
3534
3535                 if (region.apply (filter) == 0) {
3536
3537                         session->add_undo (playlist->get_memento());
3538                         playlist->replace_region (region, *(filter.results.front()), region.position());
3539                         session->add_redo_no_execute (playlist->get_memento());
3540                 } else {
3541                         goto out;
3542                 }
3543
3544                 r = tmp;
3545         }
3546
3547         commit_reversible_command ();
3548         selection->audio_regions.clear ();
3549
3550   out:
3551         track_canvas_scroller.get_window()->set_cursor (*current_canvas_cursor);
3552 }
3553
3554 void
3555 Editor::region_selection_op (void (Region::*pmf)(void))
3556 {
3557         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3558                 ((*i)->region.*pmf)();
3559         }
3560 }
3561
3562
3563 void
3564 Editor::region_selection_op (void (Region::*pmf)(void*), void *arg)
3565 {
3566         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3567                 ((*i)->region.*pmf)(arg);
3568         }
3569 }
3570
3571 void
3572 Editor::region_selection_op (void (Region::*pmf)(bool), bool yn)
3573 {
3574         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3575                 ((*i)->region.*pmf)(yn);
3576         }
3577 }
3578
3579 void
3580 Editor::external_edit_region ()
3581 {
3582         if (!clicked_regionview) {
3583                 return;
3584         }
3585
3586         /* more to come */
3587 }
3588
3589 void
3590 Editor::brush (jack_nframes_t pos)
3591 {
3592         AudioRegionSelection sel;
3593         snap_to (pos);
3594
3595         if (selection->audio_regions.empty()) {
3596                 /* XXX get selection from region list */
3597         } else { 
3598                 sel = selection->audio_regions;
3599         }
3600
3601         if (sel.empty()) {
3602                 return;
3603         }
3604
3605         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3606                 mouse_brush_insert_region ((*i), pos);
3607         }
3608 }
3609
3610 void
3611 Editor::toggle_gain_envelope_visibility ()
3612 {
3613         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3614                 (*i)->set_envelope_visible (!(*i)->envelope_visible());
3615         }
3616 }
3617
3618 void
3619 Editor::toggle_gain_envelope_active ()
3620 {
3621         for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
3622                 AudioRegion* ar = dynamic_cast<AudioRegion*>(&(*i)->region);
3623                 if (ar) {
3624                         ar->set_envelope_active (true);
3625                 }
3626         }
3627 }