Add option to insert time on all a track's playlists (#4304).
[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 */
19
20 /* Note: public Editor methods are documented in public_editor.h */
21
22 #include <unistd.h>
23
24 #include <cstdlib>
25 #include <cmath>
26 #include <string>
27 #include <map>
28 #include <set>
29
30 #include "pbd/error.h"
31 #include "pbd/basename.h"
32 #include "pbd/pthread_utils.h"
33 #include "pbd/memento_command.h"
34 #include "pbd/whitespace.h"
35 #include "pbd/stateful_diff_command.h"
36
37 #include <gtkmm2ext/utils.h>
38 #include <gtkmm2ext/choice.h>
39 #include <gtkmm2ext/popup.h>
40
41 #include "ardour/audioengine.h"
42 #include "ardour/session.h"
43 #include "ardour/audioplaylist.h"
44 #include "ardour/audioregion.h"
45 #include "ardour/audio_diskstream.h"
46 #include "ardour/utils.h"
47 #include "ardour/location.h"
48 #include "ardour/audio_track.h"
49 #include "ardour/audioplaylist.h"
50 #include "ardour/region_factory.h"
51 #include "ardour/playlist_factory.h"
52 #include "ardour/reverse.h"
53 #include "ardour/transient_detector.h"
54 #include "ardour/dB.h"
55 #include "ardour/quantize.h"
56 #include "ardour/strip_silence.h"
57 #include "ardour/route_group.h"
58 #include "ardour/operations.h"
59 #include "ardour/session_playlists.h"
60
61 #include "ardour_ui.h"
62 #include "debug.h"
63 #include "editor.h"
64 #include "time_axis_view.h"
65 #include "route_time_axis.h"
66 #include "audio_time_axis.h"
67 #include "automation_time_axis.h"
68 #include "streamview.h"
69 #include "audio_streamview.h"
70 #include "audio_region_view.h"
71 #include "midi_region_view.h"
72 #include "rgb_macros.h"
73 #include "selection_templates.h"
74 #include "selection.h"
75 #include "editing.h"
76 #include "gtk-custom-hruler.h"
77 #include "gui_thread.h"
78 #include "keyboard.h"
79 #include "utils.h"
80 #include "editor_drag.h"
81 #include "strip_silence_dialog.h"
82 #include "editor_routes.h"
83 #include "editor_regions.h"
84 #include "quantize_dialog.h"
85 #include "interthread_progress_window.h"
86 #include "insert_time_dialog.h"
87 #include "normalize_dialog.h"
88 #include "editor_cursors.h"
89 #include "mouse_cursors.h"
90 #include "patch_change_dialog.h"
91 #include "transpose_dialog.h"
92
93 #include "i18n.h"
94
95 using namespace std;
96 using namespace ARDOUR;
97 using namespace PBD;
98 using namespace Gtk;
99 using namespace Gtkmm2ext;
100 using namespace Editing;
101 using Gtkmm2ext::Keyboard;
102
103 /***********************************************************************
104   Editor operations
105  ***********************************************************************/
106
107 void
108 Editor::undo (uint32_t n)
109 {
110         if (_session) {
111                 _session->undo (n);
112         }
113 }
114
115 void
116 Editor::redo (uint32_t n)
117 {
118         if (_session) {
119                 _session->redo (n);
120         }
121 }
122
123 void
124 Editor::split_regions_at (framepos_t where, RegionSelection& regions)
125 {
126         bool frozen = false;
127
128         list <boost::shared_ptr<Playlist > > used_playlists;
129
130         if (regions.empty()) {
131                 return;
132         }
133
134         begin_reversible_command (_("split"));
135
136         // if splitting a single region, and snap-to is using
137         // region boundaries, don't pay attention to them
138
139         if (regions.size() == 1) {
140                 switch (_snap_type) {
141                 case SnapToRegionStart:
142                 case SnapToRegionSync:
143                 case SnapToRegionEnd:
144                         break;
145                 default:
146                         snap_to (where);
147                 }
148         } else {
149                 snap_to (where);
150
151                 frozen = true;
152                 EditorFreeze(); /* Emit Signal */
153         }
154
155         for (RegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
156
157                 RegionSelection::iterator tmp;
158
159                 /* XXX this test needs to be more complicated, to make sure we really
160                    have something to split.
161                 */
162
163                 if (!(*a)->region()->covers (where)) {
164                         ++a;
165                         continue;
166                 }
167
168                 tmp = a;
169                 ++tmp;
170
171                 boost::shared_ptr<Playlist> pl = (*a)->region()->playlist();
172
173                 if (!pl) {
174                         a = tmp;
175                         continue;
176                 }
177
178                 if (!pl->frozen()) {
179                         /* we haven't seen this playlist before */
180
181                         /* remember used playlists so we can thaw them later */
182                         used_playlists.push_back(pl);
183                         pl->freeze();
184                 }
185
186                 if (pl) {
187                         pl->clear_changes ();
188                         pl->split_region ((*a)->region(), where);
189                         _session->add_command (new StatefulDiffCommand (pl));
190                 }
191
192                 a = tmp;
193         }
194
195         while (used_playlists.size() > 0) {
196                 list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
197                 (*i)->thaw();
198                 used_playlists.pop_front();
199         }
200
201         commit_reversible_command ();
202
203         if (frozen){
204                 EditorThaw(); /* Emit Signal */
205         }
206 }
207
208 /** Move one extreme of the current range selection.  If more than one range is selected,
209  *  the start of the earliest range or the end of the latest range is moved.
210  *
211  *  @param move_end true to move the end of the current range selection, false to move
212  *  the start.
213  *  @param next true to move the extreme to the next region boundary, false to move to
214  *  the previous.
215  */
216 void
217 Editor::move_range_selection_start_or_end_to_region_boundary (bool move_end, bool next)
218 {
219         if (selection->time.start() == selection->time.end_frame()) {
220                 return;
221         }
222
223         framepos_t start = selection->time.start ();
224         framepos_t end = selection->time.end_frame ();
225
226         /* the position of the thing we may move */
227         framepos_t pos = move_end ? end : start;
228         int dir = next ? 1 : -1;
229
230         /* so we don't find the current region again */
231         if (dir > 0 || pos > 0) {
232                 pos += dir;
233         }
234
235         framepos_t const target = get_region_boundary (pos, dir, true, false);
236         if (target < 0) {
237                 return;
238         }
239
240         if (move_end) {
241                 end = target;
242         } else {
243                 start = target;
244         }
245
246         if (end < start) {
247                 return;
248         }
249
250         begin_reversible_command (_("alter selection"));
251         selection->set_preserving_all_ranges (start, end);
252         commit_reversible_command ();
253 }
254
255 bool
256 Editor::nudge_forward_release (GdkEventButton* ev)
257 {
258         if (ev->state & Keyboard::PrimaryModifier) {
259                 nudge_forward (false, true);
260         } else {
261                 nudge_forward (false, false);
262         }
263         return false;
264 }
265
266 bool
267 Editor::nudge_backward_release (GdkEventButton* ev)
268 {
269         if (ev->state & Keyboard::PrimaryModifier) {
270                 nudge_backward (false, true);
271         } else {
272                 nudge_backward (false, false);
273         }
274         return false;
275 }
276
277
278 void
279 Editor::nudge_forward (bool next, bool force_playhead)
280 {
281         framepos_t distance;
282         framepos_t next_distance;
283
284         if (!_session) {
285                 return;
286         }
287
288         RegionSelection rs = get_regions_from_selection_and_entered ();
289
290         if (!force_playhead && !rs.empty()) {
291
292                 begin_reversible_command (_("nudge regions forward"));
293
294                 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
295                         boost::shared_ptr<Region> r ((*i)->region());
296
297                         distance = get_nudge_distance (r->position(), next_distance);
298
299                         if (next) {
300                                 distance = next_distance;
301                         }
302
303                         r->clear_changes ();
304                         r->set_position (r->position() + distance);
305                         _session->add_command (new StatefulDiffCommand (r));
306                 }
307
308                 commit_reversible_command ();
309
310
311         } else if (!force_playhead && !selection->markers.empty()) {
312
313                 bool is_start;
314
315                 begin_reversible_command (_("nudge location forward"));
316
317                 for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
318
319                         Location* loc = find_location_from_marker ((*i), is_start);
320
321                         if (loc) {
322
323                                 XMLNode& before (loc->get_state());
324
325                                 if (is_start) {
326                                         distance = get_nudge_distance (loc->start(), next_distance);
327                                         if (next) {
328                                                 distance = next_distance;
329                                         }
330                                         if (max_framepos - distance > loc->start() + loc->length()) {
331                                                 loc->set_start (loc->start() + distance);
332                                         } else {
333                                                 loc->set_start (max_framepos - loc->length());
334                                         }
335                                 } else {
336                                         distance = get_nudge_distance (loc->end(), next_distance);
337                                         if (next) {
338                                                 distance = next_distance;
339                                         }
340                                         if (max_framepos - distance > loc->end()) {
341                                                 loc->set_end (loc->end() + distance);
342                                         } else {
343                                                 loc->set_end (max_framepos);
344                                         }
345                                 }
346                                 XMLNode& after (loc->get_state());
347                                 _session->add_command (new MementoCommand<Location>(*loc, &before, &after));
348                         }
349                 }
350
351                 commit_reversible_command ();
352
353         } else {
354                 distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
355                 _session->request_locate (playhead_cursor->current_frame + distance);
356         }
357 }
358
359 void
360 Editor::nudge_backward (bool next, bool force_playhead)
361 {
362         framepos_t distance;
363         framepos_t next_distance;
364
365         if (!_session) {
366                 return;
367         }
368
369         RegionSelection rs = get_regions_from_selection_and_entered ();
370
371         if (!force_playhead && !rs.empty()) {
372
373                 begin_reversible_command (_("nudge regions backward"));
374
375                 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
376                         boost::shared_ptr<Region> r ((*i)->region());
377
378                         distance = get_nudge_distance (r->position(), next_distance);
379
380                         if (next) {
381                                 distance = next_distance;
382                         }
383
384                         r->clear_changes ();
385
386                         if (r->position() > distance) {
387                                 r->set_position (r->position() - distance);
388                         } else {
389                                 r->set_position (0);
390                         }
391                         _session->add_command (new StatefulDiffCommand (r));
392                 }
393
394                 commit_reversible_command ();
395
396         } else if (!force_playhead && !selection->markers.empty()) {
397
398                 bool is_start;
399
400                 begin_reversible_command (_("nudge location forward"));
401
402                 for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
403
404                         Location* loc = find_location_from_marker ((*i), is_start);
405
406                         if (loc) {
407
408                                 XMLNode& before (loc->get_state());
409
410                                 if (is_start) {
411                                         distance = get_nudge_distance (loc->start(), next_distance);
412                                         if (next) {
413                                                 distance = next_distance;
414                                         }
415                                         if (distance < loc->start()) {
416                                                 loc->set_start (loc->start() - distance);
417                                         } else {
418                                                 loc->set_start (0);
419                                         }
420                                 } else {
421                                         distance = get_nudge_distance (loc->end(), next_distance);
422
423                                         if (next) {
424                                                 distance = next_distance;
425                                         }
426
427                                         if (distance < loc->end() - loc->length()) {
428                                                 loc->set_end (loc->end() - distance);
429                                         } else {
430                                                 loc->set_end (loc->length());
431                                         }
432                                 }
433
434                                 XMLNode& after (loc->get_state());
435                                 _session->add_command (new MementoCommand<Location>(*loc, &before, &after));
436                         }
437                 }
438
439                 commit_reversible_command ();
440
441         } else {
442
443                 distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
444
445                 if (playhead_cursor->current_frame > distance) {
446                         _session->request_locate (playhead_cursor->current_frame - distance);
447                 } else {
448                         _session->goto_start();
449                 }
450         }
451 }
452
453 void
454 Editor::nudge_forward_capture_offset ()
455 {
456         RegionSelection rs = get_regions_from_selection_and_entered ();
457
458         if (!_session || rs.empty()) {
459                 return;
460         }
461
462         begin_reversible_command (_("nudge forward"));
463
464         framepos_t const distance = _session->worst_output_latency();
465
466         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
467                 boost::shared_ptr<Region> r ((*i)->region());
468
469                 r->clear_changes ();
470                 r->set_position (r->position() + distance);
471                 _session->add_command(new StatefulDiffCommand (r));
472         }
473
474         commit_reversible_command ();
475 }
476
477 void
478 Editor::nudge_backward_capture_offset ()
479 {
480         RegionSelection rs = get_regions_from_selection_and_entered ();
481
482         if (!_session || rs.empty()) {
483                 return;
484         }
485
486         begin_reversible_command (_("nudge forward"));
487
488         framepos_t const distance = _session->worst_output_latency();
489
490         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
491                 boost::shared_ptr<Region> r ((*i)->region());
492
493                 r->clear_changes ();
494
495                 if (r->position() > distance) {
496                         r->set_position (r->position() - distance);
497                 } else {
498                         r->set_position (0);
499                 }
500                 _session->add_command(new StatefulDiffCommand (r));
501         }
502
503         commit_reversible_command ();
504 }
505
506 /* DISPLAY MOTION */
507
508 void
509 Editor::move_to_start ()
510 {
511         _session->goto_start ();
512 }
513
514 void
515 Editor::move_to_end ()
516 {
517
518         _session->request_locate (_session->current_end_frame());
519 }
520
521 void
522 Editor::build_region_boundary_cache ()
523 {
524         framepos_t pos = 0;
525         vector<RegionPoint> interesting_points;
526         boost::shared_ptr<Region> r;
527         TrackViewList tracks;
528         bool at_end = false;
529
530         region_boundary_cache.clear ();
531
532         if (_session == 0) {
533                 return;
534         }
535
536         switch (_snap_type) {
537         case SnapToRegionStart:
538                 interesting_points.push_back (Start);
539                 break;
540         case SnapToRegionEnd:
541                 interesting_points.push_back (End);
542                 break;
543         case SnapToRegionSync:
544                 interesting_points.push_back (SyncPoint);
545                 break;
546         case SnapToRegionBoundary:
547                 interesting_points.push_back (Start);
548                 interesting_points.push_back (End);
549                 break;
550         default:
551                 fatal << string_compose (_("build_region_boundary_cache called with snap_type = %1"), _snap_type) << endmsg;
552                 /*NOTREACHED*/
553                 return;
554         }
555
556         TimeAxisView *ontrack = 0;
557         TrackViewList tlist;
558
559         if (!selection->tracks.empty()) {
560                 tlist = selection->tracks;
561         } else {
562                 tlist = track_views;
563         }
564
565         while (pos < _session->current_end_frame() && !at_end) {
566
567                 framepos_t rpos;
568                 framepos_t lpos = max_framepos;
569
570                 for (vector<RegionPoint>::iterator p = interesting_points.begin(); p != interesting_points.end(); ++p) {
571
572                         if ((r = find_next_region (pos, *p, 1, tlist, &ontrack)) == 0) {
573                                 if (*p == interesting_points.back()) {
574                                         at_end = true;
575                                 }
576                                 /* move to next point type */
577                                 continue;
578                         }
579
580                         switch (*p) {
581                         case Start:
582                                 rpos = r->first_frame();
583                                 break;
584
585                         case End:
586                                 rpos = r->last_frame();
587                                 break;
588
589                         case SyncPoint:
590                                 rpos = r->sync_position ();
591                                 break;
592
593                         default:
594                                 break;
595                         }
596
597                         float speed = 1.0f;
598                         RouteTimeAxisView *rtav;
599
600                         if (ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0 ) {
601                                 if (rtav->track() != 0) {
602                                         speed = rtav->track()->speed();
603                                 }
604                         }
605
606                         rpos = track_frame_to_session_frame (rpos, speed);
607
608                         if (rpos < lpos) {
609                                 lpos = rpos;
610                         }
611
612                         /* prevent duplicates, but we don't use set<> because we want to be able
613                            to sort later.
614                         */
615
616                         vector<framepos_t>::iterator ri;
617
618                         for (ri = region_boundary_cache.begin(); ri != region_boundary_cache.end(); ++ri) {
619                                 if (*ri == rpos) {
620                                         break;
621                                 }
622                         }
623
624                         if (ri == region_boundary_cache.end()) {
625                                 region_boundary_cache.push_back (rpos);
626                         }
627                 }
628
629                 pos = lpos + 1;
630         }
631
632         /* finally sort to be sure that the order is correct */
633
634         sort (region_boundary_cache.begin(), region_boundary_cache.end());
635 }
636
637 boost::shared_ptr<Region>
638 Editor::find_next_region (framepos_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
639 {
640         TrackViewList::iterator i;
641         framepos_t closest = max_framepos;
642         boost::shared_ptr<Region> ret;
643         framepos_t rpos = 0;
644
645         float track_speed;
646         framepos_t track_frame;
647         RouteTimeAxisView *rtav;
648
649         for (i = tracks.begin(); i != tracks.end(); ++i) {
650
651                 framecnt_t distance;
652                 boost::shared_ptr<Region> r;
653
654                 track_speed = 1.0f;
655                 if ( (rtav = dynamic_cast<RouteTimeAxisView*>(*i)) != 0 ) {
656                         if (rtav->track()!=0)
657                                 track_speed = rtav->track()->speed();
658                 }
659
660                 track_frame = session_frame_to_track_frame(frame, track_speed);
661
662                 if ((r = (*i)->find_next_region (track_frame, point, dir)) == 0) {
663                         continue;
664                 }
665
666                 switch (point) {
667                 case Start:
668                         rpos = r->first_frame ();
669                         break;
670
671                 case End:
672                         rpos = r->last_frame ();
673                         break;
674
675                 case SyncPoint:
676                         rpos = r->sync_position ();
677                         break;
678                 }
679
680                 // rpos is a "track frame", converting it to "_session frame"
681                 rpos = track_frame_to_session_frame(rpos, track_speed);
682
683                 if (rpos > frame) {
684                         distance = rpos - frame;
685                 } else {
686                         distance = frame - rpos;
687                 }
688
689                 if (distance < closest) {
690                         closest = distance;
691                         if (ontrack != 0)
692                                 *ontrack = (*i);
693                         ret = r;
694                 }
695         }
696
697         return ret;
698 }
699
700 framepos_t
701 Editor::find_next_region_boundary (framepos_t pos, int32_t dir, const TrackViewList& tracks)
702 {
703         framecnt_t distance = max_framepos;
704         framepos_t current_nearest = -1;
705
706         for (TrackViewList::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
707                 framepos_t contender;
708                 framecnt_t d;
709
710                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
711
712                 if (!rtv) {
713                         continue;
714                 }
715
716                 if ((contender = rtv->find_next_region_boundary (pos, dir)) < 0) {
717                         continue;
718                 }
719
720                 d = ::llabs (pos - contender);
721
722                 if (d < distance) {
723                         current_nearest = contender;
724                         distance = d;
725                 }
726         }
727
728         return current_nearest;
729 }
730
731 framepos_t
732 Editor::get_region_boundary (framepos_t pos, int32_t dir, bool with_selection, bool only_onscreen)
733 {
734         framepos_t target;
735         TrackViewList tvl;
736
737         if (with_selection && Config->get_region_boundaries_from_selected_tracks()) {
738
739                 if (!selection->tracks.empty()) {
740
741                         target = find_next_region_boundary (pos, dir, selection->tracks);
742
743                 } else {
744
745                         if (only_onscreen || Config->get_region_boundaries_from_onscreen_tracks()) {
746                                 get_onscreen_tracks (tvl);
747                                 target = find_next_region_boundary (pos, dir, tvl);
748                         } else {
749                                 target = find_next_region_boundary (pos, dir, track_views);
750                         }
751                 }
752
753         } else {
754
755                 if (only_onscreen || Config->get_region_boundaries_from_onscreen_tracks()) {
756                         get_onscreen_tracks (tvl);
757                         target = find_next_region_boundary (pos, dir, tvl);
758                 } else {
759                         target = find_next_region_boundary (pos, dir, track_views);
760                 }
761         }
762
763         return target;
764 }
765
766 void
767 Editor::cursor_to_region_boundary (bool with_selection, int32_t dir)
768 {
769         framepos_t pos = playhead_cursor->current_frame;
770         framepos_t target;
771
772         if (!_session) {
773                 return;
774         }
775
776         // so we don't find the current region again..
777         if (dir > 0 || pos > 0) {
778                 pos += dir;
779         }
780
781         if ((target = get_region_boundary (pos, dir, with_selection, false)) < 0) {
782                 return;
783         }
784
785         _session->request_locate (target);
786 }
787
788 void
789 Editor::cursor_to_next_region_boundary (bool with_selection)
790 {
791         cursor_to_region_boundary (with_selection, 1);
792 }
793
794 void
795 Editor::cursor_to_previous_region_boundary (bool with_selection)
796 {
797         cursor_to_region_boundary (with_selection, -1);
798 }
799
800 void
801 Editor::cursor_to_region_point (EditorCursor* cursor, RegionPoint point, int32_t dir)
802 {
803         boost::shared_ptr<Region> r;
804         framepos_t pos = cursor->current_frame;
805
806         if (!_session) {
807                 return;
808         }
809
810         TimeAxisView *ontrack = 0;
811
812         // so we don't find the current region again..
813         if (dir>0 || pos>0)
814                 pos+=dir;
815
816         if (!selection->tracks.empty()) {
817
818                 r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
819
820         } else if (clicked_axisview) {
821
822                 TrackViewList t;
823                 t.push_back (clicked_axisview);
824
825                 r = find_next_region (pos, point, dir, t, &ontrack);
826
827         } else {
828
829                 r = find_next_region (pos, point, dir, track_views, &ontrack);
830         }
831
832         if (r == 0) {
833                 return;
834         }
835
836         switch (point) {
837         case Start:
838                 pos = r->first_frame ();
839                 break;
840
841         case End:
842                 pos = r->last_frame ();
843                 break;
844
845         case SyncPoint:
846                 pos = r->sync_position ();
847                 break;
848         }
849
850         float speed = 1.0f;
851         RouteTimeAxisView *rtav;
852
853         if ( ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0 ) {
854                 if (rtav->track() != 0) {
855                         speed = rtav->track()->speed();
856                 }
857         }
858
859         pos = track_frame_to_session_frame(pos, speed);
860
861         if (cursor == playhead_cursor) {
862                 _session->request_locate (pos);
863         } else {
864                 cursor->set_position (pos);
865         }
866 }
867
868 void
869 Editor::cursor_to_next_region_point (EditorCursor* cursor, RegionPoint point)
870 {
871         cursor_to_region_point (cursor, point, 1);
872 }
873
874 void
875 Editor::cursor_to_previous_region_point (EditorCursor* cursor, RegionPoint point)
876 {
877         cursor_to_region_point (cursor, point, -1);
878 }
879
880 void
881 Editor::cursor_to_selection_start (EditorCursor *cursor)
882 {
883         framepos_t pos = 0;
884
885         switch (mouse_mode) {
886         case MouseObject:
887                 if (!selection->regions.empty()) {
888                         pos = selection->regions.start();
889                 }
890                 break;
891
892         case MouseRange:
893                 if (!selection->time.empty()) {
894                         pos = selection->time.start ();
895                 }
896                 break;
897
898         default:
899                 return;
900         }
901
902         if (cursor == playhead_cursor) {
903                 _session->request_locate (pos);
904         } else {
905                 cursor->set_position (pos);
906         }
907 }
908
909 void
910 Editor::cursor_to_selection_end (EditorCursor *cursor)
911 {
912         framepos_t pos = 0;
913
914         switch (mouse_mode) {
915         case MouseObject:
916                 if (!selection->regions.empty()) {
917                         pos = selection->regions.end_frame();
918                 }
919                 break;
920
921         case MouseRange:
922                 if (!selection->time.empty()) {
923                         pos = selection->time.end_frame ();
924                 }
925                 break;
926
927         default:
928                 return;
929         }
930
931         if (cursor == playhead_cursor) {
932                 _session->request_locate (pos);
933         } else {
934                 cursor->set_position (pos);
935         }
936 }
937
938 void
939 Editor::selected_marker_to_region_boundary (bool with_selection, int32_t dir)
940 {
941         framepos_t target;
942         Location* loc;
943         bool ignored;
944
945         if (!_session) {
946                 return;
947         }
948
949         if (selection->markers.empty()) {
950                 framepos_t mouse;
951                 bool ignored;
952
953                 if (!mouse_frame (mouse, ignored)) {
954                         return;
955                 }
956
957                 add_location_mark (mouse);
958         }
959
960         if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
961                 return;
962         }
963
964         framepos_t pos = loc->start();
965
966         // so we don't find the current region again..
967         if (dir > 0 || pos > 0) {
968                 pos += dir;
969         }
970
971         if ((target = get_region_boundary (pos, dir, with_selection, false)) < 0) {
972                 return;
973         }
974
975         loc->move_to (target);
976 }
977
978 void
979 Editor::selected_marker_to_next_region_boundary (bool with_selection)
980 {
981         selected_marker_to_region_boundary (with_selection, 1);
982 }
983
984 void
985 Editor::selected_marker_to_previous_region_boundary (bool with_selection)
986 {
987         selected_marker_to_region_boundary (with_selection, -1);
988 }
989
990 void
991 Editor::selected_marker_to_region_point (RegionPoint point, int32_t dir)
992 {
993         boost::shared_ptr<Region> r;
994         framepos_t pos;
995         Location* loc;
996         bool ignored;
997
998         if (!_session || selection->markers.empty()) {
999                 return;
1000         }
1001
1002         if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
1003                 return;
1004         }
1005
1006         TimeAxisView *ontrack = 0;
1007
1008         pos = loc->start();
1009
1010         // so we don't find the current region again..
1011         if (dir>0 || pos>0)
1012                 pos+=dir;
1013
1014         if (!selection->tracks.empty()) {
1015
1016                 r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
1017
1018         } else {
1019
1020                 r = find_next_region (pos, point, dir, track_views, &ontrack);
1021         }
1022
1023         if (r == 0) {
1024                 return;
1025         }
1026
1027         switch (point) {
1028         case Start:
1029                 pos = r->first_frame ();
1030                 break;
1031
1032         case End:
1033                 pos = r->last_frame ();
1034                 break;
1035
1036         case SyncPoint:
1037                 pos = r->adjust_to_sync (r->first_frame());
1038                 break;
1039         }
1040
1041         float speed = 1.0f;
1042         RouteTimeAxisView *rtav;
1043
1044         if (ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0) {
1045                 if (rtav->track() != 0) {
1046                         speed = rtav->track()->speed();
1047                 }
1048         }
1049
1050         pos = track_frame_to_session_frame(pos, speed);
1051
1052         loc->move_to (pos);
1053 }
1054
1055 void
1056 Editor::selected_marker_to_next_region_point (RegionPoint point)
1057 {
1058         selected_marker_to_region_point (point, 1);
1059 }
1060
1061 void
1062 Editor::selected_marker_to_previous_region_point (RegionPoint point)
1063 {
1064         selected_marker_to_region_point (point, -1);
1065 }
1066
1067 void
1068 Editor::selected_marker_to_selection_start ()
1069 {
1070         framepos_t pos = 0;
1071         Location* loc;
1072         bool ignored;
1073
1074         if (!_session || selection->markers.empty()) {
1075                 return;
1076         }
1077
1078         if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
1079                 return;
1080         }
1081
1082         switch (mouse_mode) {
1083         case MouseObject:
1084                 if (!selection->regions.empty()) {
1085                         pos = selection->regions.start();
1086                 }
1087                 break;
1088
1089         case MouseRange:
1090                 if (!selection->time.empty()) {
1091                         pos = selection->time.start ();
1092                 }
1093                 break;
1094
1095         default:
1096                 return;
1097         }
1098
1099         loc->move_to (pos);
1100 }
1101
1102 void
1103 Editor::selected_marker_to_selection_end ()
1104 {
1105         framepos_t pos = 0;
1106         Location* loc;
1107         bool ignored;
1108
1109         if (!_session || selection->markers.empty()) {
1110                 return;
1111         }
1112
1113         if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
1114                 return;
1115         }
1116
1117         switch (mouse_mode) {
1118         case MouseObject:
1119                 if (!selection->regions.empty()) {
1120                         pos = selection->regions.end_frame();
1121                 }
1122                 break;
1123
1124         case MouseRange:
1125                 if (!selection->time.empty()) {
1126                         pos = selection->time.end_frame ();
1127                 }
1128                 break;
1129
1130         default:
1131                 return;
1132         }
1133
1134         loc->move_to (pos);
1135 }
1136
1137 void
1138 Editor::scroll_playhead (bool forward)
1139 {
1140         framepos_t pos = playhead_cursor->current_frame;
1141         framecnt_t delta = (framecnt_t) floor (current_page_frames() / 0.8);
1142
1143         if (forward) {
1144                 if (pos == max_framepos) {
1145                         return;
1146                 }
1147
1148                 if (pos < max_framepos - delta) {
1149                         pos += delta ;
1150                 } else {
1151                         pos = max_framepos;
1152                 }
1153
1154         } else {
1155
1156                 if (pos == 0) {
1157                         return;
1158                 }
1159
1160                 if (pos > delta) {
1161                         pos -= delta;
1162                 } else {
1163                         pos = 0;
1164                 }
1165         }
1166
1167         _session->request_locate (pos);
1168 }
1169
1170 void
1171 Editor::cursor_align (bool playhead_to_edit)
1172 {
1173         if (!_session) {
1174                 return;
1175         }
1176
1177         if (playhead_to_edit) {
1178
1179                 if (selection->markers.empty()) {
1180                         return;
1181                 }
1182
1183                 _session->request_locate (selection->markers.front()->position(), _session->transport_rolling());
1184
1185         } else {
1186                 /* move selected markers to playhead */
1187
1188                 for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
1189                         bool ignored;
1190
1191                         Location* loc = find_location_from_marker (*i, ignored);
1192
1193                         if (loc->is_mark()) {
1194                                 loc->set_start (playhead_cursor->current_frame);
1195                         } else {
1196                                 loc->set (playhead_cursor->current_frame,
1197                                           playhead_cursor->current_frame + loc->length());
1198                         }
1199                 }
1200         }
1201 }
1202
1203 void
1204 Editor::scroll_backward (float pages)
1205 {
1206         framepos_t const one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
1207         framepos_t const cnt = (framepos_t) floor (pages * one_page);
1208
1209         framepos_t frame;
1210         if (leftmost_frame < cnt) {
1211                 frame = 0;
1212         } else {
1213                 frame = leftmost_frame - cnt;
1214         }
1215
1216         reset_x_origin (frame);
1217 }
1218
1219 void
1220 Editor::scroll_forward (float pages)
1221 {
1222         framepos_t const one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
1223         framepos_t const cnt = (framepos_t) floor (pages * one_page);
1224
1225         framepos_t frame;
1226         if (max_framepos - cnt < leftmost_frame) {
1227                 frame = max_framepos - cnt;
1228         } else {
1229                 frame = leftmost_frame + cnt;
1230         }
1231
1232         reset_x_origin (frame);
1233 }
1234
1235 void
1236 Editor::scroll_tracks_down ()
1237 {
1238         double vert_value = vertical_adjustment.get_value() + vertical_adjustment.get_page_size();
1239         if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
1240                 vert_value = vertical_adjustment.get_upper() - _canvas_height;
1241         }
1242
1243         vertical_adjustment.set_value (vert_value);
1244 }
1245
1246 void
1247 Editor::scroll_tracks_up ()
1248 {
1249         vertical_adjustment.set_value (vertical_adjustment.get_value() - vertical_adjustment.get_page_size());
1250 }
1251
1252 void
1253 Editor::scroll_tracks_down_line ()
1254 {
1255         double vert_value = vertical_adjustment.get_value() + 60;
1256
1257         if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
1258                 vert_value = vertical_adjustment.get_upper() - _canvas_height;
1259         }
1260
1261         vertical_adjustment.set_value (vert_value);
1262 }
1263
1264 void
1265 Editor::scroll_tracks_up_line ()
1266 {
1267         reset_y_origin (vertical_adjustment.get_value() - 60);
1268 }
1269
1270 /* ZOOM */
1271
1272 void
1273 Editor::tav_zoom_step (bool coarser)
1274 {
1275         ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_step, coarser)
1276
1277         _routes->suspend_redisplay ();
1278
1279         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1280                 TimeAxisView *tv = (static_cast<TimeAxisView*>(*i));
1281                         tv->step_height (coarser);
1282         }
1283
1284         _routes->resume_redisplay ();
1285 }
1286
1287 void
1288 Editor::temporal_zoom_step (bool coarser)
1289 {
1290         ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_step, coarser)
1291
1292         double nfpu;
1293
1294         nfpu = frames_per_unit;
1295
1296         if (coarser) {
1297                 nfpu *= 1.61803399;
1298         } else {
1299                 nfpu = max(1.0,(nfpu/1.61803399));
1300         }
1301
1302         temporal_zoom (nfpu);
1303 }
1304
1305 void
1306 Editor::temporal_zoom (gdouble fpu)
1307 {
1308         if (!_session) return;
1309
1310         framepos_t current_page = current_page_frames();
1311         framepos_t current_leftmost = leftmost_frame;
1312         framepos_t current_rightmost;
1313         framepos_t current_center;
1314         framepos_t new_page_size;
1315         framepos_t half_page_size;
1316         framepos_t leftmost_after_zoom = 0;
1317         framepos_t where;
1318         bool in_track_canvas;
1319         double nfpu;
1320         double l;
1321
1322         /* XXX this limit is also in ::set_frames_per_unit() */
1323
1324         if (frames_per_unit <= 1.0 && fpu <= frames_per_unit) {
1325                 return;
1326         }
1327
1328         nfpu = fpu;
1329
1330         new_page_size = (framepos_t) floor (_canvas_width * nfpu);
1331         half_page_size = new_page_size / 2;
1332
1333         switch (zoom_focus) {
1334         case ZoomFocusLeft:
1335                 leftmost_after_zoom = current_leftmost;
1336                 break;
1337
1338         case ZoomFocusRight:
1339                 current_rightmost = leftmost_frame + current_page;
1340                 if (current_rightmost < new_page_size) {
1341                         leftmost_after_zoom = 0;
1342                 } else {
1343                         leftmost_after_zoom = current_rightmost - new_page_size;
1344                 }
1345                 break;
1346
1347         case ZoomFocusCenter:
1348                 current_center = current_leftmost + (current_page/2);
1349                 if (current_center < half_page_size) {
1350                         leftmost_after_zoom = 0;
1351                 } else {
1352                         leftmost_after_zoom = current_center - half_page_size;
1353                 }
1354                 break;
1355
1356         case ZoomFocusPlayhead:
1357                 /* centre playhead */
1358                 l = playhead_cursor->current_frame - (new_page_size * 0.5);
1359
1360                 if (l < 0) {
1361                         leftmost_after_zoom = 0;
1362                 } else if (l > max_framepos) {
1363                         leftmost_after_zoom = max_framepos - new_page_size;
1364                 } else {
1365                         leftmost_after_zoom = (framepos_t) l;
1366                 }
1367                 break;
1368
1369         case ZoomFocusMouse:
1370                 /* try to keep the mouse over the same point in the display */
1371
1372                 if (!mouse_frame (where, in_track_canvas)) {
1373                         /* use playhead instead */
1374                         where = playhead_cursor->current_frame;
1375
1376                         if (where < half_page_size) {
1377                                 leftmost_after_zoom = 0;
1378                         } else {
1379                                 leftmost_after_zoom = where - half_page_size;
1380                         }
1381
1382                 } else {
1383
1384                         l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
1385
1386                         if (l < 0) {
1387                                 leftmost_after_zoom = 0;
1388                         } else if (l > max_framepos) {
1389                                 leftmost_after_zoom = max_framepos - new_page_size;
1390                         } else {
1391                                 leftmost_after_zoom = (framepos_t) l;
1392                         }
1393                 }
1394
1395                 break;
1396
1397         case ZoomFocusEdit:
1398                 /* try to keep the edit point in the same place */
1399                 where = get_preferred_edit_position ();
1400
1401                 if (where > 0) {
1402
1403                         double l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
1404
1405                         if (l < 0) {
1406                                 leftmost_after_zoom = 0;
1407                         } else if (l > max_framepos) {
1408                                 leftmost_after_zoom = max_framepos - new_page_size;
1409                         } else {
1410                                 leftmost_after_zoom = (framepos_t) l;
1411                         }
1412
1413                 } else {
1414                         /* edit point not defined */
1415                         return;
1416                 }
1417                 break;
1418
1419         }
1420
1421         // leftmost_after_zoom = min (leftmost_after_zoom, _session->current_end_frame());
1422
1423         reposition_and_zoom (leftmost_after_zoom, nfpu);
1424 }
1425
1426 void
1427 Editor::temporal_zoom_region (bool both_axes)
1428 {
1429         framepos_t start = max_framepos;
1430         framepos_t end = 0;
1431         set<TimeAxisView*> tracks;
1432
1433         RegionSelection rs = get_regions_from_selection_and_entered ();
1434
1435         if (rs.empty()) {
1436                 return;
1437         }
1438
1439         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
1440
1441                 if ((*i)->region()->position() < start) {
1442                         start = (*i)->region()->position();
1443                 }
1444
1445                 if ((*i)->region()->last_frame() + 1 > end) {
1446                         end = (*i)->region()->last_frame() + 1;
1447                 }
1448
1449                 tracks.insert (&((*i)->get_time_axis_view()));
1450         }
1451
1452         /* now comes an "interesting" hack ... make sure we leave a little space
1453            at each end of the editor so that the zoom doesn't fit the region
1454            precisely to the screen.
1455         */
1456
1457         GdkScreen* screen = gdk_screen_get_default ();
1458         gint pixwidth = gdk_screen_get_width (screen);
1459         gint mmwidth = gdk_screen_get_width_mm (screen);
1460         double pix_per_mm = (double) pixwidth/ (double) mmwidth;
1461         double one_centimeter_in_pixels = pix_per_mm * 10.0;
1462
1463         if ((start == 0 && end == 0) || end < start) {
1464                 return;
1465         }
1466
1467         framepos_t range = end - start;
1468         double new_fpu = (double)range / (double)_canvas_width;
1469         framepos_t extra_samples = (framepos_t) floor (one_centimeter_in_pixels * new_fpu);
1470
1471         if (start > extra_samples) {
1472                 start -= extra_samples;
1473         } else {
1474                 start = 0;
1475         }
1476
1477         if (max_framepos - extra_samples > end) {
1478                 end += extra_samples;
1479         } else {
1480                 end = max_framepos;
1481         }
1482
1483         if (both_axes) {
1484                 /* save visual state with track states included, and prevent
1485                    set_frames_per_unit() from doing it again.
1486                 */
1487                 undo_visual_stack.push_back (current_visual_state(true));
1488                 no_save_visual = true;
1489         }
1490
1491         temporal_zoom_by_frame (start, end, "zoom to region");
1492
1493         if (both_axes) {
1494                 uint32_t per_track_height = (uint32_t) floor ((_canvas_height - canvas_timebars_vsize - 10.0) / tracks.size());
1495
1496                 /* set visible track heights appropriately */
1497
1498                 for (set<TimeAxisView*>::iterator t = tracks.begin(); t != tracks.end(); ++t) {
1499                         (*t)->set_height (per_track_height);
1500                 }
1501
1502                 /* hide irrelevant tracks */
1503
1504                 _routes->suspend_redisplay ();
1505
1506                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1507                         if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
1508                                 hide_track_in_display (*i);
1509                         }
1510                 }
1511
1512                 _routes->resume_redisplay ();
1513
1514                 vertical_adjustment.set_value (0.0);
1515                 no_save_visual = false;
1516         }
1517
1518         redo_visual_stack.push_back (current_visual_state());
1519 }
1520
1521 void
1522 Editor::zoom_to_region (bool both_axes)
1523 {
1524         temporal_zoom_region (both_axes);
1525 }
1526
1527 void
1528 Editor::temporal_zoom_selection ()
1529 {
1530         if (!selection) return;
1531
1532         if (selection->time.empty()) {
1533                 return;
1534         }
1535
1536         framepos_t start = selection->time[clicked_selection].start;
1537         framepos_t end = selection->time[clicked_selection].end;
1538
1539         temporal_zoom_by_frame (start, end, "zoom to selection");
1540 }
1541
1542 void
1543 Editor::temporal_zoom_session ()
1544 {
1545         ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_session)
1546
1547         if (_session) {
1548                 framecnt_t const l = _session->current_end_frame() - _session->current_start_frame();
1549                 double s = _session->current_start_frame() - l * 0.01;
1550                 if (s < 0) {
1551                         s = 0;
1552                 }
1553                 framecnt_t const e = _session->current_end_frame() + l * 0.01;
1554                 temporal_zoom_by_frame (framecnt_t (s), e, "zoom to _session");
1555         }
1556 }
1557
1558 void
1559 Editor::temporal_zoom_by_frame (framepos_t start, framepos_t end, const string & /*op*/)
1560 {
1561         if (!_session) return;
1562
1563         if ((start == 0 && end == 0) || end < start) {
1564                 return;
1565         }
1566
1567         framepos_t range = end - start;
1568
1569         double new_fpu = (double)range / (double)_canvas_width;
1570
1571         framepos_t new_page = (framepos_t) floor (_canvas_width * new_fpu);
1572         framepos_t middle = (framepos_t) floor( (double)start + ((double)range / 2.0f ));
1573         framepos_t new_leftmost = (framepos_t) floor( (double)middle - ((double)new_page/2.0f));
1574
1575         if (new_leftmost > middle) {
1576                 new_leftmost = 0;
1577         }
1578
1579         reposition_and_zoom (new_leftmost, new_fpu);
1580 }
1581
1582 void
1583 Editor::temporal_zoom_to_frame (bool coarser, framepos_t frame)
1584 {
1585         if (!_session) {
1586                 return;
1587         }
1588         double range_before = frame - leftmost_frame;
1589         double new_fpu;
1590
1591         new_fpu = frames_per_unit;
1592
1593         if (coarser) {
1594                 new_fpu *= 1.61803399;
1595                 range_before *= 1.61803399;
1596         } else {
1597                 new_fpu = max(1.0,(new_fpu/1.61803399));
1598                 range_before /= 1.61803399;
1599         }
1600
1601         if (new_fpu == frames_per_unit)  {
1602                 return;
1603         }
1604
1605         framepos_t new_leftmost = frame - (framepos_t)range_before;
1606
1607         if (new_leftmost > frame) {
1608                 new_leftmost = 0;
1609         }
1610
1611         if (new_leftmost < 0) {
1612                 new_leftmost = 0;
1613         }
1614
1615         reposition_and_zoom (new_leftmost, new_fpu);
1616 }
1617
1618
1619 bool
1620 Editor::choose_new_marker_name(string &name) {
1621
1622         if (!Config->get_name_new_markers()) {
1623                 /* don't prompt user for a new name */
1624                 return true;
1625         }
1626
1627         ArdourPrompter dialog (true);
1628
1629         dialog.set_prompt (_("New Name:"));
1630
1631         dialog.set_title (_("New Location Marker"));
1632
1633         dialog.set_name ("MarkNameWindow");
1634         dialog.set_size_request (250, -1);
1635         dialog.set_position (Gtk::WIN_POS_MOUSE);
1636
1637         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1638         dialog.set_initial_text (name);
1639
1640         dialog.show ();
1641
1642         switch (dialog.run ()) {
1643         case RESPONSE_ACCEPT:
1644                 break;
1645         default:
1646                 return false;
1647         }
1648
1649         dialog.get_result(name);
1650         return true;
1651
1652 }
1653
1654
1655 void
1656 Editor::add_location_from_selection ()
1657 {
1658         string rangename;
1659
1660         if (selection->time.empty()) {
1661                 return;
1662         }
1663
1664         if (_session == 0 || clicked_axisview == 0) {
1665                 return;
1666         }
1667
1668         framepos_t start = selection->time[clicked_selection].start;
1669         framepos_t end = selection->time[clicked_selection].end;
1670
1671         _session->locations()->next_available_name(rangename,"selection");
1672         Location *location = new Location (*_session, start, end, rangename, Location::IsRangeMarker);
1673
1674         _session->begin_reversible_command (_("add marker"));
1675         XMLNode &before = _session->locations()->get_state();
1676         _session->locations()->add (location, true);
1677         XMLNode &after = _session->locations()->get_state();
1678         _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1679         _session->commit_reversible_command ();
1680 }
1681
1682 void
1683 Editor::add_location_mark (framepos_t where)
1684 {
1685         string markername;
1686
1687         select_new_marker = true;
1688
1689         _session->locations()->next_available_name(markername,"mark");
1690         if (!choose_new_marker_name(markername)) {
1691                 return;
1692         }
1693         Location *location = new Location (*_session, where, where, markername, Location::IsMark);
1694         _session->begin_reversible_command (_("add marker"));
1695         XMLNode &before = _session->locations()->get_state();
1696         _session->locations()->add (location, true);
1697         XMLNode &after = _session->locations()->get_state();
1698         _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1699         _session->commit_reversible_command ();
1700 }
1701
1702 void
1703 Editor::add_location_from_playhead_cursor ()
1704 {
1705         add_location_mark (_session->audible_frame());
1706 }
1707
1708 /** Add a range marker around each selected region */
1709 void
1710 Editor::add_locations_from_region ()
1711 {
1712         RegionSelection rs = get_regions_from_selection_and_entered ();
1713
1714         if (rs.empty()) {
1715                 return;
1716         }
1717
1718         _session->begin_reversible_command (selection->regions.size () > 1 ? _("add markers") : _("add marker"));
1719         XMLNode &before = _session->locations()->get_state();
1720
1721         for (RegionSelection::iterator i = rs.begin (); i != rs.end (); ++i) {
1722
1723                 boost::shared_ptr<Region> region = (*i)->region ();
1724
1725                 Location *location = new Location (*_session, region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
1726
1727                 _session->locations()->add (location, true);
1728         }
1729
1730         XMLNode &after = _session->locations()->get_state();
1731         _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1732         _session->commit_reversible_command ();
1733 }
1734
1735 /** Add a single range marker around all selected regions */
1736 void
1737 Editor::add_location_from_region ()
1738 {
1739         RegionSelection rs = get_regions_from_selection_and_entered ();
1740
1741         if (rs.empty()) {
1742                 return;
1743         }
1744
1745         _session->begin_reversible_command (_("add marker"));
1746         XMLNode &before = _session->locations()->get_state();
1747
1748         string markername;
1749
1750         if (rs.size() > 1) {
1751                 _session->locations()->next_available_name(markername, "regions");
1752         } else {
1753                 RegionView* rv = *(rs.begin());
1754                 boost::shared_ptr<Region> region = rv->region();
1755                 markername = region->name();
1756         }
1757
1758         if (!choose_new_marker_name(markername)) {
1759                 return;
1760         }
1761
1762         // single range spanning all selected
1763         Location *location = new Location (*_session, selection->regions.start(), selection->regions.end_frame(), markername, Location::IsRangeMarker);
1764         _session->locations()->add (location, true);
1765
1766         XMLNode &after = _session->locations()->get_state();
1767         _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1768         _session->commit_reversible_command ();
1769 }
1770
1771 /* MARKS */
1772
1773 void
1774 Editor::jump_forward_to_mark ()
1775 {
1776         if (!_session) {
1777                 return;
1778         }
1779
1780         Location *location = _session->locations()->first_location_after (playhead_cursor->current_frame);
1781
1782         if (location) {
1783                 _session->request_locate (location->start(), _session->transport_rolling());
1784         } else {
1785                 _session->request_locate (_session->current_end_frame());
1786         }
1787 }
1788
1789 void
1790 Editor::jump_backward_to_mark ()
1791 {
1792         if (!_session) {
1793                 return;
1794         }
1795
1796         Location *location = _session->locations()->first_location_before (playhead_cursor->current_frame);
1797
1798         if (location) {
1799                 _session->request_locate (location->start(), _session->transport_rolling());
1800         } else {
1801                 _session->goto_start ();
1802         }
1803 }
1804
1805 void
1806 Editor::set_mark ()
1807 {
1808         framepos_t const pos = _session->audible_frame ();
1809
1810         string markername;
1811         _session->locations()->next_available_name (markername, "mark");
1812
1813         if (!choose_new_marker_name (markername)) {
1814                 return;
1815         }
1816
1817         _session->locations()->add (new Location (*_session, pos, 0, markername, Location::IsMark), true);
1818 }
1819
1820 void
1821 Editor::clear_markers ()
1822 {
1823         if (_session) {
1824                 _session->begin_reversible_command (_("clear markers"));
1825                 XMLNode &before = _session->locations()->get_state();
1826                 _session->locations()->clear_markers ();
1827                 XMLNode &after = _session->locations()->get_state();
1828                 _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1829                 _session->commit_reversible_command ();
1830         }
1831 }
1832
1833 void
1834 Editor::clear_ranges ()
1835 {
1836         if (_session) {
1837                 _session->begin_reversible_command (_("clear ranges"));
1838                 XMLNode &before = _session->locations()->get_state();
1839
1840                 Location * looploc = _session->locations()->auto_loop_location();
1841                 Location * punchloc = _session->locations()->auto_punch_location();
1842
1843                 _session->locations()->clear_ranges ();
1844                 // re-add these
1845                 if (looploc) _session->locations()->add (looploc);
1846                 if (punchloc) _session->locations()->add (punchloc);
1847
1848                 XMLNode &after = _session->locations()->get_state();
1849                 _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1850                 _session->commit_reversible_command ();
1851         }
1852 }
1853
1854 void
1855 Editor::clear_locations ()
1856 {
1857         _session->begin_reversible_command (_("clear locations"));
1858         XMLNode &before = _session->locations()->get_state();
1859         _session->locations()->clear ();
1860         XMLNode &after = _session->locations()->get_state();
1861         _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1862         _session->commit_reversible_command ();
1863         _session->locations()->clear ();
1864 }
1865
1866 void
1867 Editor::unhide_markers ()
1868 {
1869         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
1870                 Location *l = (*i).first;
1871                 if (l->is_hidden() && l->is_mark()) {
1872                         l->set_hidden(false, this);
1873                 }
1874         }
1875 }
1876
1877 void
1878 Editor::unhide_ranges ()
1879 {
1880         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
1881                 Location *l = (*i).first;
1882                 if (l->is_hidden() && l->is_range_marker()) {
1883                         l->set_hidden(false, this);
1884                 }
1885         }
1886 }
1887
1888 /* INSERT/REPLACE */
1889
1890 void
1891 Editor::insert_region_list_drag (boost::shared_ptr<Region> region, int x, int y)
1892 {
1893         double wx, wy;
1894         double cx, cy;
1895         framepos_t where;
1896         RouteTimeAxisView *rtv = 0;
1897         boost::shared_ptr<Playlist> playlist;
1898
1899         track_canvas->window_to_world (x, y, wx, wy);
1900
1901         GdkEvent event;
1902         event.type = GDK_BUTTON_RELEASE;
1903         event.button.x = wx;
1904         event.button.y = wy;
1905
1906         where = event_frame (&event, &cx, &cy);
1907
1908         if (where < leftmost_frame || where > leftmost_frame + current_page_frames()) {
1909                 /* clearly outside canvas area */
1910                 return;
1911         }
1912
1913         std::pair<TimeAxisView*, int> tv = trackview_by_y_position (cy);
1914         if (tv.first == 0) {
1915                 return;
1916         }
1917
1918         if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
1919                 return;
1920         }
1921
1922         if ((playlist = rtv->playlist()) == 0) {
1923                 return;
1924         }
1925
1926         snap_to (where);
1927
1928         begin_reversible_command (_("insert dragged region"));
1929         playlist->clear_changes ();
1930         playlist->add_region (RegionFactory::create (region, true), where, 1.0);
1931         _session->add_command(new StatefulDiffCommand (playlist));
1932         commit_reversible_command ();
1933 }
1934
1935 void
1936 Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y)
1937 {
1938         double wx, wy;
1939         double cx, cy;
1940         RouteTimeAxisView *dest_rtv = 0;
1941         RouteTimeAxisView *source_rtv = 0;
1942
1943         track_canvas->window_to_world (x, y, wx, wy);
1944         wx += horizontal_position ();
1945         wy += vertical_adjustment.get_value();
1946
1947         GdkEvent event;
1948         event.type = GDK_BUTTON_RELEASE;
1949         event.button.x = wx;
1950         event.button.y = wy;
1951
1952         event_frame (&event, &cx, &cy);
1953
1954         std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (cy);
1955         if (tv.first == 0) {
1956                 return;
1957         }
1958
1959         if ((dest_rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
1960                 return;
1961         }
1962
1963         /* use this drag source to add underlay to a track. But we really don't care
1964            about the Route, only the view of the route, so find it first */
1965         for(TrackViewList::iterator it = track_views.begin(); it != track_views.end(); ++it) {
1966                 if((source_rtv = dynamic_cast<RouteTimeAxisView*>(*it)) == 0) {
1967                         continue;
1968                 }
1969
1970                 if(source_rtv->route() == route && source_rtv != dest_rtv) {
1971                         dest_rtv->add_underlay(source_rtv->view());
1972                         break;
1973                 }
1974         }
1975 }
1976
1977 void
1978 Editor::insert_region_list_selection (float times)
1979 {
1980         RouteTimeAxisView *tv = 0;
1981         boost::shared_ptr<Playlist> playlist;
1982
1983         if (clicked_routeview != 0) {
1984                 tv = clicked_routeview;
1985         } else if (!selection->tracks.empty()) {
1986                 if ((tv = dynamic_cast<RouteTimeAxisView*>(selection->tracks.front())) == 0) {
1987                         return;
1988                 }
1989         } else if (entered_track != 0) {
1990                 if ((tv = dynamic_cast<RouteTimeAxisView*>(entered_track)) == 0) {
1991                         return;
1992                 }
1993         } else {
1994                 return;
1995         }
1996
1997         if ((playlist = tv->playlist()) == 0) {
1998                 return;
1999         }
2000
2001         boost::shared_ptr<Region> region = _regions->get_single_selection ();
2002         if (region == 0) {
2003                 return;
2004         }
2005
2006         begin_reversible_command (_("insert region"));
2007         playlist->clear_changes ();
2008         playlist->add_region ((RegionFactory::create (region, true)), get_preferred_edit_position(), times);
2009         _session->add_command(new StatefulDiffCommand (playlist));
2010         commit_reversible_command ();
2011 }
2012
2013 /* BUILT-IN EFFECTS */
2014
2015 void
2016 Editor::reverse_selection ()
2017 {
2018
2019 }
2020
2021 /* GAIN ENVELOPE EDITING */
2022
2023 void
2024 Editor::edit_envelope ()
2025 {
2026 }
2027
2028 /* PLAYBACK */
2029
2030 void
2031 Editor::transition_to_rolling (bool fwd)
2032 {
2033         if (!_session) {
2034                 return;
2035         }
2036
2037         if (_session->config.get_external_sync()) {
2038                 switch (_session->config.get_sync_source()) {
2039                 case JACK:
2040                         break;
2041                 default:
2042                         /* transport controlled by the master */
2043                         return;
2044                 }
2045         }
2046
2047         if (_session->is_auditioning()) {
2048                 _session->cancel_audition ();
2049                 return;
2050         }
2051
2052         _session->request_transport_speed (fwd ? 1.0f : -1.0f);
2053 }
2054
2055 void
2056 Editor::play_from_start ()
2057 {
2058         _session->request_locate (_session->current_start_frame(), true);
2059 }
2060
2061 void
2062 Editor::play_from_edit_point ()
2063 {
2064         _session->request_locate (get_preferred_edit_position(), true);
2065 }
2066
2067 void
2068 Editor::play_from_edit_point_and_return ()
2069 {
2070         framepos_t start_frame;
2071         framepos_t return_frame;
2072
2073         start_frame = get_preferred_edit_position (true);
2074
2075         if (_session->transport_rolling()) {
2076                 _session->request_locate (start_frame, false);
2077                 return;
2078         }
2079
2080         /* don't reset the return frame if its already set */
2081
2082         if ((return_frame = _session->requested_return_frame()) < 0) {
2083                 return_frame = _session->audible_frame();
2084         }
2085
2086         if (start_frame >= 0) {
2087                 _session->request_roll_at_and_return (start_frame, return_frame);
2088         }
2089 }
2090
2091 void
2092 Editor::play_selection ()
2093 {
2094         if (selection->time.empty()) {
2095                 return;
2096         }
2097
2098         _session->request_play_range (&selection->time, true);
2099 }
2100
2101 void
2102 Editor::play_location (Location& location)
2103 {
2104         if (location.start() <= location.end()) {
2105                 return;
2106         }
2107
2108         _session->request_bounded_roll (location.start(), location.end());
2109 }
2110
2111 void
2112 Editor::loop_location (Location& location)
2113 {
2114         if (location.start() <= location.end()) {
2115                 return;
2116         }
2117
2118         Location* tll;
2119
2120         if ((tll = transport_loop_location()) != 0) {
2121                 tll->set (location.start(), location.end());
2122
2123                 // enable looping, reposition and start rolling
2124                 _session->request_play_loop (true);
2125                 _session->request_locate (tll->start(), true);
2126         }
2127 }
2128
2129 void
2130 Editor::raise_region ()
2131 {
2132         selection->foreach_region (&Region::raise);
2133 }
2134
2135 void
2136 Editor::raise_region_to_top ()
2137 {
2138         selection->foreach_region (&Region::raise_to_top);
2139 }
2140
2141 void
2142 Editor::lower_region ()
2143 {
2144         selection->foreach_region (&Region::lower);
2145 }
2146
2147 void
2148 Editor::lower_region_to_bottom ()
2149 {
2150         selection->foreach_region (&Region::lower_to_bottom);
2151 }
2152
2153 /** Show the region editor for the selected regions */
2154 void
2155 Editor::show_region_properties ()
2156 {
2157         selection->foreach_regionview (&RegionView::show_region_editor);
2158 }
2159
2160 /** Show the midi list editor for the selected MIDI regions */
2161 void
2162 Editor::show_midi_list_editor ()
2163 {
2164         selection->foreach_midi_regionview (&MidiRegionView::show_list_editor);
2165 }
2166
2167 void
2168 Editor::rename_region ()
2169 {
2170         RegionSelection rs = get_regions_from_selection_and_entered ();
2171
2172         if (rs.empty()) {
2173                 return;
2174         }
2175
2176         ArdourDialog d (*this, _("Rename Region"), true, false);
2177         Entry entry;
2178         Label label (_("New name:"));
2179         HBox hbox;
2180
2181         hbox.set_spacing (6);
2182         hbox.pack_start (label, false, false);
2183         hbox.pack_start (entry, true, true);
2184
2185         d.get_vbox()->set_border_width (12);
2186         d.get_vbox()->pack_start (hbox, false, false);
2187
2188         d.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
2189         d.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
2190
2191         d.set_size_request (300, -1);
2192         d.set_position (Gtk::WIN_POS_MOUSE);
2193
2194         entry.set_text (selection->regions.front()->region()->name());
2195         entry.select_region (0, -1);
2196
2197         entry.signal_activate().connect (sigc::bind (sigc::mem_fun (d, &Dialog::response), RESPONSE_OK));
2198
2199         d.show_all ();
2200
2201         entry.grab_focus();
2202
2203         int const ret = d.run();
2204
2205         d.hide ();
2206
2207         if (ret != RESPONSE_OK) {
2208                 return;
2209         }
2210
2211         std::string str = entry.get_text();
2212         strip_whitespace_edges (str);
2213         if (!str.empty()) {
2214                 rs.front()->region()->set_name (str);
2215                 _regions->redisplay ();
2216         }
2217 }
2218
2219 void
2220 Editor::audition_playlist_region_via_route (boost::shared_ptr<Region> region, Route& route)
2221 {
2222         if (_session->is_auditioning()) {
2223                 _session->cancel_audition ();
2224         }
2225
2226         // note: some potential for creativity here, because region doesn't
2227         // have to belong to the playlist that Route is handling
2228
2229         // bool was_soloed = route.soloed();
2230
2231         route.set_solo (true, this);
2232
2233         _session->request_bounded_roll (region->position(), region->position() + region->length());
2234
2235         /* XXX how to unset the solo state ? */
2236 }
2237
2238 /** Start an audition of the first selected region */
2239 void
2240 Editor::play_edit_range ()
2241 {
2242         framepos_t start, end;
2243
2244         if (get_edit_op_range (start, end)) {
2245                 _session->request_bounded_roll (start, end);
2246         }
2247 }
2248
2249 void
2250 Editor::play_selected_region ()
2251 {
2252         framepos_t start = max_framepos;
2253         framepos_t end = 0;
2254
2255         RegionSelection rs = get_regions_from_selection_and_entered ();
2256
2257         if (rs.empty()) {
2258                 return;
2259         }
2260
2261         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2262                 if ((*i)->region()->position() < start) {
2263                         start = (*i)->region()->position();
2264                 }
2265                 if ((*i)->region()->last_frame() + 1 > end) {
2266                         end = (*i)->region()->last_frame() + 1;
2267                 }
2268         }
2269
2270         _session->request_bounded_roll (start, end);
2271 }
2272
2273 void
2274 Editor::audition_playlist_region_standalone (boost::shared_ptr<Region> region)
2275 {
2276         _session->audition_region (region);
2277 }
2278
2279 void
2280 Editor::region_from_selection ()
2281 {
2282         if (clicked_axisview == 0) {
2283                 return;
2284         }
2285
2286         if (selection->time.empty()) {
2287                 return;
2288         }
2289
2290         framepos_t start = selection->time[clicked_selection].start;
2291         framepos_t end = selection->time[clicked_selection].end;
2292
2293         TrackViewList tracks = get_tracks_for_range_action ();
2294
2295         framepos_t selection_cnt = end - start + 1;
2296
2297         for (TrackSelection::iterator i = tracks.begin(); i != tracks.end(); ++i) {
2298                 boost::shared_ptr<Region> current;
2299                 boost::shared_ptr<Playlist> pl;
2300                 framepos_t internal_start;
2301                 string new_name;
2302
2303                 if ((pl = (*i)->playlist()) == 0) {
2304                         continue;
2305                 }
2306
2307                 if ((current = pl->top_region_at (start)) == 0) {
2308                         continue;
2309                 }
2310
2311                 internal_start = start - current->position();
2312                 RegionFactory::region_name (new_name, current->name(), true);
2313
2314                 PropertyList plist;
2315
2316                 plist.add (ARDOUR::Properties::start, current->start() + internal_start);
2317                 plist.add (ARDOUR::Properties::length, selection_cnt);
2318                 plist.add (ARDOUR::Properties::name, new_name);
2319                 plist.add (ARDOUR::Properties::layer, 0);
2320
2321                 boost::shared_ptr<Region> region (RegionFactory::create (current, plist));
2322         }
2323 }
2324
2325 void
2326 Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_regions)
2327 {
2328         if (selection->time.empty() || selection->tracks.empty()) {
2329                 return;
2330         }
2331
2332         framepos_t start = selection->time[clicked_selection].start;
2333         framepos_t end = selection->time[clicked_selection].end;
2334
2335         sort_track_selection ();
2336
2337         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
2338                 boost::shared_ptr<Region> current;
2339                 boost::shared_ptr<Playlist> playlist;
2340                 framepos_t internal_start;
2341                 string new_name;
2342
2343                 if ((playlist = (*i)->playlist()) == 0) {
2344                         continue;
2345                 }
2346
2347                 if ((current = playlist->top_region_at(start)) == 0) {
2348                         continue;
2349                 }
2350
2351                 internal_start = start - current->position();
2352                 RegionFactory::region_name (new_name, current->name(), true);
2353
2354                 PropertyList plist;
2355
2356                 plist.add (ARDOUR::Properties::start, current->start() + internal_start);
2357                 plist.add (ARDOUR::Properties::length, end - start + 1);
2358                 plist.add (ARDOUR::Properties::name, new_name);
2359
2360                 new_regions.push_back (RegionFactory::create (current, plist));
2361         }
2362 }
2363
2364 void
2365 Editor::split_multichannel_region ()
2366 {
2367         RegionSelection rs = get_regions_from_selection_and_entered ();
2368
2369         if (rs.empty()) {
2370                 return;
2371         }
2372
2373         vector< boost::shared_ptr<Region> > v;
2374
2375         for (list<RegionView*>::iterator x = rs.begin(); x != rs.end(); ++x) {
2376                 (*x)->region()->separate_by_channel (*_session, v);
2377         }
2378 }
2379
2380 void
2381 Editor::new_region_from_selection ()
2382 {
2383         region_from_selection ();
2384         cancel_selection ();
2385 }
2386
2387 static void
2388 add_if_covered (RegionView* rv, const AudioRange* ar, RegionSelection* rs)
2389 {
2390         switch (rv->region()->coverage (ar->start, ar->end - 1)) {
2391         case OverlapNone:
2392                 break;
2393         default:
2394                 rs->push_back (rv);
2395         }
2396 }
2397
2398 /** Return either:
2399  *    - selected tracks, or if there are none...
2400  *    - tracks containing selected regions, or if there are none...
2401  *    - all tracks
2402  * @return tracks.
2403  */
2404 TrackViewList
2405 Editor::get_tracks_for_range_action () const
2406 {
2407         TrackViewList t;
2408
2409         if (selection->tracks.empty()) {
2410
2411                 /* use tracks with selected regions */
2412
2413                 RegionSelection rs = selection->regions;
2414
2415                 for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2416                         TimeAxisView* tv = &(*i)->get_time_axis_view();
2417
2418                         if (!t.contains (tv)) {
2419                                 t.push_back (tv);
2420                         }
2421                 }
2422
2423                 if (t.empty()) {
2424                         /* no regions and no tracks: use all tracks */
2425                         t = track_views;
2426                 }
2427
2428         } else {
2429
2430                 t = selection->tracks;
2431         }
2432
2433         return t;
2434 }
2435
2436 void
2437 Editor::separate_regions_between (const TimeSelection& ts)
2438 {
2439         bool in_command = false;
2440         boost::shared_ptr<Playlist> playlist;
2441         RegionSelection new_selection;
2442
2443         TrackViewList tmptracks = get_tracks_for_range_action ();
2444         sort_track_selection (&tmptracks);
2445
2446         for (TrackSelection::iterator i = tmptracks.begin(); i != tmptracks.end(); ++i) {
2447
2448                 RouteTimeAxisView* rtv;
2449
2450                 if ((rtv = dynamic_cast<RouteTimeAxisView*> ((*i))) != 0) {
2451
2452                         if (rtv->is_track()) {
2453
2454                                 /* no edits to destructive tracks */
2455
2456                                 if (rtv->track()->destructive()) {
2457                                         continue;
2458                                 }
2459
2460                                 if ((playlist = rtv->playlist()) != 0) {
2461
2462                                         playlist->clear_changes ();
2463
2464                                         /* XXX need to consider musical time selections here at some point */
2465
2466                                         double speed = rtv->track()->speed();
2467
2468
2469                                         for (list<AudioRange>::const_iterator t = ts.begin(); t != ts.end(); ++t) {
2470
2471                                                 sigc::connection c = rtv->view()->RegionViewAdded.connect (
2472                                                                 sigc::mem_fun(*this, &Editor::collect_new_region_view));
2473
2474                                                 latest_regionviews.clear ();
2475
2476                                                 playlist->partition ((framepos_t)((*t).start * speed),
2477                                                                 (framepos_t)((*t).end * speed), false);
2478
2479                                                 c.disconnect ();
2480
2481                                                 if (!latest_regionviews.empty()) {
2482
2483                                                         rtv->view()->foreach_regionview (sigc::bind (
2484                                                                                 sigc::ptr_fun (add_if_covered),
2485                                                                                 &(*t), &new_selection));
2486
2487                                                         if (!in_command) {
2488                                                                 begin_reversible_command (_("separate"));
2489                                                                 in_command = true;
2490                                                         }
2491
2492                                                         /* pick up changes to existing regions */
2493
2494                                                         vector<Command*> cmds;
2495                                                         playlist->rdiff (cmds);
2496                                                         _session->add_commands (cmds);
2497
2498                                                         /* pick up changes to the playlist itself (adds/removes)
2499                                                          */
2500
2501                                                         _session->add_command(new StatefulDiffCommand (playlist));
2502                                                 }
2503                                         }
2504                                 }
2505                         }
2506                 }
2507         }
2508
2509         if (in_command) {
2510                 selection->set (new_selection);
2511                 set_mouse_mode (MouseObject);
2512
2513                 commit_reversible_command ();
2514         }
2515 }
2516
2517 struct PlaylistState {
2518     boost::shared_ptr<Playlist> playlist;
2519     XMLNode*  before;
2520 };
2521
2522 /** Take tracks from get_tracks_for_range_action and cut any regions
2523  *  on those tracks so that the tracks are empty over the time
2524  *  selection.
2525  */
2526 void
2527 Editor::separate_region_from_selection ()
2528 {
2529         /* preferentially use *all* ranges in the time selection if we're in range mode
2530            to allow discontiguous operation, since get_edit_op_range() currently
2531            returns a single range.
2532         */
2533
2534         if (mouse_mode == MouseRange && !selection->time.empty()) {
2535
2536                 separate_regions_between (selection->time);
2537
2538         } else {
2539
2540                 framepos_t start;
2541                 framepos_t end;
2542
2543                 if (get_edit_op_range (start, end)) {
2544
2545                         AudioRange ar (start, end, 1);
2546                         TimeSelection ts;
2547                         ts.push_back (ar);
2548
2549                         separate_regions_between (ts);
2550                 }
2551         }
2552 }
2553
2554 void
2555 Editor::separate_region_from_punch ()
2556 {
2557         Location* loc  = _session->locations()->auto_punch_location();
2558         if (loc) {
2559                 separate_regions_using_location (*loc);
2560         }
2561 }
2562
2563 void
2564 Editor::separate_region_from_loop ()
2565 {
2566         Location* loc  = _session->locations()->auto_loop_location();
2567         if (loc) {
2568                 separate_regions_using_location (*loc);
2569         }
2570 }
2571
2572 void
2573 Editor::separate_regions_using_location (Location& loc)
2574 {
2575         if (loc.is_mark()) {
2576                 return;
2577         }
2578
2579         AudioRange ar (loc.start(), loc.end(), 1);
2580         TimeSelection ts;
2581
2582         ts.push_back (ar);
2583
2584         separate_regions_between (ts);
2585 }
2586
2587 /** Separate regions under the selected region */
2588 void
2589 Editor::separate_under_selected_regions ()
2590 {
2591         vector<PlaylistState> playlists;
2592
2593         RegionSelection rs;
2594
2595         rs = get_regions_from_selection_and_entered();
2596
2597         if (!_session || rs.empty()) {
2598                 return;
2599         }
2600
2601         begin_reversible_command (_("separate region under"));
2602
2603         list<boost::shared_ptr<Region> > regions_to_remove;
2604
2605         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2606                 // we can't just remove the region(s) in this loop because
2607                 // this removes them from the RegionSelection, and they thus
2608                 // disappear from underneath the iterator, and the ++i above
2609                 // SEGVs in a puzzling fashion.
2610
2611                 // so, first iterate over the regions to be removed from rs and
2612                 // add them to the regions_to_remove list, and then
2613                 // iterate over the list to actually remove them.
2614
2615                 regions_to_remove.push_back ((*i)->region());
2616         }
2617
2618         for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
2619
2620                 boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
2621
2622                 if (!playlist) {
2623                         // is this check necessary?
2624                         continue;
2625                 }
2626
2627                 vector<PlaylistState>::iterator i;
2628
2629                 //only take state if this is a new playlist.
2630                 for (i = playlists.begin(); i != playlists.end(); ++i) {
2631                         if ((*i).playlist == playlist) {
2632                                 break;
2633                         }
2634                 }
2635
2636                 if (i == playlists.end()) {
2637
2638                         PlaylistState before;
2639                         before.playlist = playlist;
2640                         before.before = &playlist->get_state();
2641
2642                         playlist->freeze ();
2643                         playlists.push_back(before);
2644                 }
2645
2646                 //Partition on the region bounds
2647                 playlist->partition ((*rl)->first_frame() - 1, (*rl)->last_frame() + 1, true);
2648
2649                 //Re-add region that was just removed due to the partition operation
2650                 playlist->add_region( (*rl), (*rl)->first_frame() );
2651         }
2652
2653         vector<PlaylistState>::iterator pl;
2654
2655         for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
2656                 (*pl).playlist->thaw ();
2657                 _session->add_command(new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
2658         }
2659
2660         commit_reversible_command ();
2661 }
2662
2663 void
2664 Editor::crop_region_to_selection ()
2665 {
2666         if (!selection->time.empty()) {
2667
2668                 crop_region_to (selection->time.start(), selection->time.end_frame());
2669
2670         } else {
2671
2672                 framepos_t start;
2673                 framepos_t end;
2674
2675                 if (get_edit_op_range (start, end)) {
2676                         crop_region_to (start, end);
2677                 }
2678         }
2679
2680 }
2681
2682 void
2683 Editor::crop_region_to (framepos_t start, framepos_t end)
2684 {
2685         vector<boost::shared_ptr<Playlist> > playlists;
2686         boost::shared_ptr<Playlist> playlist;
2687         TrackViewList* ts;
2688
2689         if (selection->tracks.empty()) {
2690                 ts = &track_views;
2691         } else {
2692                 sort_track_selection ();
2693                 ts = &selection->tracks;
2694         }
2695
2696         for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
2697
2698                 RouteTimeAxisView* rtv;
2699
2700                 if ((rtv = dynamic_cast<RouteTimeAxisView*> ((*i))) != 0) {
2701
2702                         boost::shared_ptr<Track> t = rtv->track();
2703
2704                         if (t != 0 && ! t->destructive()) {
2705
2706                                 if ((playlist = rtv->playlist()) != 0) {
2707                                         playlists.push_back (playlist);
2708                                 }
2709                         }
2710                 }
2711         }
2712
2713         if (playlists.empty()) {
2714                 return;
2715         }
2716
2717         framepos_t the_start;
2718         framepos_t the_end;
2719         framepos_t cnt;
2720
2721         begin_reversible_command (_("trim to selection"));
2722
2723         for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
2724
2725                 boost::shared_ptr<Region> region;
2726
2727                 the_start = start;
2728
2729                 if ((region = (*i)->top_region_at(the_start)) == 0) {
2730                         continue;
2731                 }
2732
2733                 /* now adjust lengths to that we do the right thing
2734                    if the selection extends beyond the region
2735                 */
2736
2737                 the_start = max (the_start, (framepos_t) region->position());
2738                 if (max_framepos - the_start < region->length()) {
2739                         the_end = the_start + region->length() - 1;
2740                 } else {
2741                         the_end = max_framepos;
2742                 }
2743                 the_end = min (end, the_end);
2744                 cnt = the_end - the_start + 1;
2745
2746                 region->clear_changes ();
2747                 region->trim_to (the_start, cnt);
2748                 _session->add_command (new StatefulDiffCommand (region));
2749         }
2750
2751         commit_reversible_command ();
2752 }
2753
2754 void
2755 Editor::region_fill_track ()
2756 {
2757         RegionSelection rs = get_regions_from_selection_and_entered ();
2758
2759         if (!_session || rs.empty()) {
2760                 return;
2761         }
2762
2763         framepos_t const end = _session->current_end_frame ();
2764
2765         begin_reversible_command (Operations::region_fill);
2766
2767         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2768
2769                 boost::shared_ptr<Region> region ((*i)->region());
2770
2771                 boost::shared_ptr<Playlist> pl = region->playlist();
2772
2773                 if (end <= region->last_frame()) {
2774                         return;
2775                 }
2776
2777                 double times = (double) (end - region->last_frame()) / (double) region->length();
2778
2779                 if (times == 0) {
2780                         return;
2781                 }
2782
2783                 pl->clear_changes ();
2784                 pl->add_region (RegionFactory::create (region, true), region->last_frame(), times);
2785                 _session->add_command (new StatefulDiffCommand (pl));
2786         }
2787
2788         commit_reversible_command ();
2789 }
2790
2791 void
2792 Editor::region_fill_selection ()
2793 {
2794         if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
2795                 return;
2796         }
2797
2798         if (selection->time.empty()) {
2799                 return;
2800         }
2801
2802         boost::shared_ptr<Region> region = _regions->get_single_selection ();
2803         if (region == 0) {
2804                 return;
2805         }
2806
2807         framepos_t start = selection->time[clicked_selection].start;
2808         framepos_t end = selection->time[clicked_selection].end;
2809
2810         boost::shared_ptr<Playlist> playlist;
2811
2812         if (selection->tracks.empty()) {
2813                 return;
2814         }
2815
2816         framepos_t selection_length = end - start;
2817         float times = (float)selection_length / region->length();
2818
2819         begin_reversible_command (Operations::fill_selection);
2820
2821         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
2822
2823                 if ((playlist = (*i)->playlist()) == 0) {
2824                         continue;
2825                 }
2826
2827                 playlist->clear_changes ();
2828                 playlist->add_region (RegionFactory::create (region, true), start, times);
2829                 _session->add_command (new StatefulDiffCommand (playlist));
2830         }
2831
2832         commit_reversible_command ();
2833 }
2834
2835 void
2836 Editor::set_region_sync_position ()
2837 {
2838         set_sync_point (get_preferred_edit_position (), get_regions_from_selection_and_edit_point ());
2839 }
2840
2841 void
2842 Editor::set_sync_point (framepos_t where, const RegionSelection& rs)
2843 {
2844         bool in_command = false;
2845
2846         for (RegionSelection::const_iterator r = rs.begin(); r != rs.end(); ++r) {
2847
2848                 if (!(*r)->region()->covers (where)) {
2849                         continue;
2850                 }
2851
2852                 boost::shared_ptr<Region> region ((*r)->region());
2853
2854                 if (!in_command) {
2855                         begin_reversible_command (_("set sync point"));
2856                         in_command = true;
2857                 }
2858
2859                 region->clear_changes ();
2860                 region->set_sync_position (where);
2861                 _session->add_command(new StatefulDiffCommand (region));
2862         }
2863
2864         if (in_command) {
2865                 commit_reversible_command ();
2866         }
2867 }
2868
2869 /** Remove the sync positions of the selection */
2870 void
2871 Editor::remove_region_sync ()
2872 {
2873         RegionSelection rs = get_regions_from_selection_and_entered ();
2874
2875         if (rs.empty()) {
2876                 return;
2877         }
2878
2879         begin_reversible_command (_("remove region sync"));
2880
2881         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2882
2883                 (*i)->region()->clear_changes ();
2884                 (*i)->region()->clear_sync_position ();
2885                 _session->add_command(new StatefulDiffCommand ((*i)->region()));
2886         }
2887
2888         commit_reversible_command ();
2889 }
2890
2891 void
2892 Editor::naturalize_region ()
2893 {
2894         RegionSelection rs = get_regions_from_selection_and_entered ();
2895
2896         if (rs.empty()) {
2897                 return;
2898         }
2899
2900         if (rs.size() > 1) {
2901                 begin_reversible_command (_("move regions to original position"));
2902         } else {
2903                 begin_reversible_command (_("move region to original position"));
2904         }
2905
2906         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
2907                 (*i)->region()->clear_changes ();
2908                 (*i)->region()->move_to_natural_position ();
2909                 _session->add_command (new StatefulDiffCommand ((*i)->region()));
2910         }
2911
2912         commit_reversible_command ();
2913 }
2914
2915 void
2916 Editor::align_regions (RegionPoint what)
2917 {
2918         RegionSelection const rs = get_regions_from_selection_and_edit_point ();
2919
2920         if (rs.empty()) {
2921                 return;
2922         }
2923
2924         begin_reversible_command (_("align selection"));
2925
2926         framepos_t const position = get_preferred_edit_position ();
2927
2928         for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
2929                 align_region_internal ((*i)->region(), what, position);
2930         }
2931
2932         commit_reversible_command ();
2933 }
2934
2935 struct RegionSortByTime {
2936     bool operator() (const RegionView* a, const RegionView* b) {
2937             return a->region()->position() < b->region()->position();
2938     }
2939 };
2940
2941 void
2942 Editor::align_regions_relative (RegionPoint point)
2943 {
2944         RegionSelection const rs = get_regions_from_selection_and_edit_point ();
2945
2946         if (rs.empty()) {
2947                 return;
2948         }
2949
2950         framepos_t const position = get_preferred_edit_position ();
2951
2952         framepos_t distance = 0;
2953         framepos_t pos = 0;
2954         int dir = 1;
2955
2956         list<RegionView*> sorted;
2957         rs.by_position (sorted);
2958
2959         boost::shared_ptr<Region> r ((*sorted.begin())->region());
2960
2961         switch (point) {
2962         case Start:
2963                 pos = position;
2964                 if (position > r->position()) {
2965                         distance = position - r->position();
2966                 } else {
2967                         distance = r->position() - position;
2968                         dir = -1;
2969                 }
2970                 break;
2971
2972         case End:
2973                 if (position > r->last_frame()) {
2974                         distance = position - r->last_frame();
2975                         pos = r->position() + distance;
2976                 } else {
2977                         distance = r->last_frame() - position;
2978                         pos = r->position() - distance;
2979                         dir = -1;
2980                 }
2981                 break;
2982
2983         case SyncPoint:
2984                 pos = r->adjust_to_sync (position);
2985                 if (pos > r->position()) {
2986                         distance = pos - r->position();
2987                 } else {
2988                         distance = r->position() - pos;
2989                         dir = -1;
2990                 }
2991                 break;
2992         }
2993
2994         if (pos == r->position()) {
2995                 return;
2996         }
2997
2998         begin_reversible_command (_("align selection (relative)"));
2999
3000         /* move first one specially */
3001
3002         r->clear_changes ();
3003         r->set_position (pos);
3004         _session->add_command(new StatefulDiffCommand (r));
3005
3006         /* move rest by the same amount */
3007
3008         sorted.pop_front();
3009
3010         for (list<RegionView*>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
3011
3012                 boost::shared_ptr<Region> region ((*i)->region());
3013
3014                 region->clear_changes ();
3015
3016                 if (dir > 0) {
3017                         region->set_position (region->position() + distance);
3018                 } else {
3019                         region->set_position (region->position() - distance);
3020                 }
3021
3022                 _session->add_command(new StatefulDiffCommand (region));
3023
3024         }
3025
3026         commit_reversible_command ();
3027 }
3028
3029 void
3030 Editor::align_region (boost::shared_ptr<Region> region, RegionPoint point, framepos_t position)
3031 {
3032         begin_reversible_command (_("align region"));
3033         align_region_internal (region, point, position);
3034         commit_reversible_command ();
3035 }
3036
3037 void
3038 Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint point, framepos_t position)
3039 {
3040         region->clear_changes ();
3041
3042         switch (point) {
3043         case SyncPoint:
3044                 region->set_position (region->adjust_to_sync (position));
3045                 break;
3046
3047         case End:
3048                 if (position > region->length()) {
3049                         region->set_position (position - region->length());
3050                 }
3051                 break;
3052
3053         case Start:
3054                 region->set_position (position);
3055                 break;
3056         }
3057
3058         _session->add_command(new StatefulDiffCommand (region));
3059 }
3060
3061 void
3062 Editor::trim_region_front ()
3063 {
3064         trim_region (true);
3065 }
3066
3067 void
3068 Editor::trim_region_back ()
3069 {
3070         trim_region (false);
3071 }
3072
3073 void
3074 Editor::trim_region (bool front)
3075 {
3076         framepos_t where = get_preferred_edit_position();
3077         RegionSelection rs = get_regions_from_selection_and_edit_point ();
3078
3079         cerr << "trim regions\n";
3080
3081         if (rs.empty()) {
3082                 cerr << " no regions\n";
3083                 return;
3084         }
3085
3086         cerr << "where = " << where << endl;
3087
3088         begin_reversible_command (front ? _("trim front") : _("trim back"));
3089
3090         for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
3091                 if (!(*i)->region()->locked()) {
3092
3093                         (*i)->region()->clear_changes ();
3094
3095                         if (front) {
3096                                 (*i)->region()->trim_front (where);
3097                         } else {
3098                                 (*i)->region()->trim_end (where);
3099                         }
3100
3101                         _session->add_command (new StatefulDiffCommand ((*i)->region()));
3102                 }
3103         }
3104
3105         commit_reversible_command ();
3106 }
3107
3108 /** Trim the end of the selected regions to the position of the edit cursor */
3109 void
3110 Editor::trim_region_to_loop ()
3111 {
3112         Location* loc = _session->locations()->auto_loop_location();
3113         if (!loc) {
3114                 return;
3115         }
3116         trim_region_to_location (*loc, _("trim to loop"));
3117 }
3118
3119 void
3120 Editor::trim_region_to_punch ()
3121 {
3122         Location* loc = _session->locations()->auto_punch_location();
3123         if (!loc) {
3124                 return;
3125         }
3126         trim_region_to_location (*loc, _("trim to punch"));
3127 }
3128
3129 void
3130 Editor::trim_region_to_location (const Location& loc, const char* str)
3131 {
3132         RegionSelection rs = get_regions_from_selection_and_entered ();
3133
3134         begin_reversible_command (str);
3135
3136         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
3137                 RegionView* rv = (*x);
3138
3139                 /* require region to span proposed trim */
3140                 switch (rv->region()->coverage (loc.start(), loc.end())) {
3141                 case OverlapInternal:
3142                         break;
3143                 default:
3144                         continue;
3145                 }
3146
3147                 RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
3148                 if (!tav) {
3149                         return;
3150                 }
3151
3152                 float speed = 1.0;
3153                 framepos_t start;
3154                 framepos_t end;
3155
3156                 if (tav->track() != 0) {
3157                         speed = tav->track()->speed();
3158                 }
3159
3160                 start = session_frame_to_track_frame (loc.start(), speed);
3161                 end = session_frame_to_track_frame (loc.end(), speed);
3162
3163                 rv->region()->clear_changes ();
3164                 rv->region()->trim_to (start, (end - start));
3165                 _session->add_command(new StatefulDiffCommand (rv->region()));
3166         }
3167
3168         commit_reversible_command ();
3169 }
3170
3171 void
3172 Editor::trim_region_to_previous_region_end ()
3173 {
3174         return trim_to_region(false);
3175 }
3176
3177 void
3178 Editor::trim_region_to_next_region_start ()
3179 {
3180         return trim_to_region(true);
3181 }
3182
3183 void
3184 Editor::trim_to_region(bool forward)
3185 {
3186         RegionSelection rs = get_regions_from_selection_and_entered ();
3187
3188         begin_reversible_command (_("trim to region"));
3189
3190         boost::shared_ptr<Region> next_region;
3191
3192         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
3193
3194                 AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
3195
3196                 if (!arv) {
3197                         continue;
3198                 }
3199
3200                 AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
3201
3202                 if (!atav) {
3203                         return;
3204                 }
3205
3206                 float speed = 1.0;
3207
3208                 if (atav->track() != 0) {
3209                         speed = atav->track()->speed();
3210                 }
3211
3212
3213                 boost::shared_ptr<Region> region = arv->region();
3214                 boost::shared_ptr<Playlist> playlist (region->playlist());
3215
3216                 region->clear_changes ();
3217
3218                 if (forward) {
3219
3220                     next_region = playlist->find_next_region (region->first_frame(), Start, 1);
3221
3222                     if (!next_region) {
3223                         continue;
3224                     }
3225
3226                     region->trim_end((framepos_t) ( (next_region->first_frame() - 1) * speed));
3227                     arv->region_changed (PropertyChange (ARDOUR::Properties::length));
3228                 }
3229                 else {
3230
3231                     next_region = playlist->find_next_region (region->first_frame(), Start, 0);
3232
3233                     if(!next_region){
3234                         continue;
3235                     }
3236
3237                     region->trim_front((framepos_t) ((next_region->last_frame() + 1) * speed));
3238
3239                     arv->region_changed (ARDOUR::bounds_change);
3240                 }
3241
3242                 _session->add_command(new StatefulDiffCommand (region));
3243         }
3244
3245         commit_reversible_command ();
3246 }
3247
3248 void
3249 Editor::unfreeze_route ()
3250 {
3251         if (clicked_routeview == 0 || !clicked_routeview->is_track()) {
3252                 return;
3253         }
3254
3255         clicked_routeview->track()->unfreeze ();
3256 }
3257
3258 void*
3259 Editor::_freeze_thread (void* arg)
3260 {
3261         SessionEvent::create_per_thread_pool ("freeze events", 64);
3262
3263         return static_cast<Editor*>(arg)->freeze_thread ();
3264 }
3265
3266 void*
3267 Editor::freeze_thread ()
3268 {
3269         clicked_routeview->audio_track()->freeze_me (*current_interthread_info);
3270         current_interthread_info->done = true;
3271         return 0;
3272 }
3273
3274 void
3275 Editor::freeze_route ()
3276 {
3277         if (!_session) {
3278                 return;
3279         }
3280
3281         /* stop transport before we start. this is important */
3282
3283         _session->request_transport_speed (0.0);
3284
3285         if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
3286                 return;
3287         }
3288
3289         if (!clicked_routeview->track()->bounceable()) {
3290                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (clicked_routeview);
3291                 if (rtv && !rtv->track()->bounceable()) {
3292                         MessageDialog d (
3293                                 _("This route cannot be frozen because it has more outputs than inputs.  "
3294                                   "You can fix this by increasing the number of inputs.")
3295                                 );
3296                         d.set_title (_("Cannot freeze"));
3297                         d.run ();
3298                         return;
3299                 }
3300         }
3301
3302         InterThreadInfo itt;
3303         current_interthread_info = &itt;
3304
3305         InterthreadProgressWindow ipw (current_interthread_info, _("Freeze"), _("Cancel Freeze"));
3306
3307         pthread_create_and_store (X_("freezer"), &itt.thread, _freeze_thread, this);
3308
3309         set_canvas_cursor (_cursors->wait);
3310
3311         while (!itt.done && !itt.cancel) {
3312                 gtk_main_iteration ();
3313         }
3314
3315         current_interthread_info = 0;
3316         set_canvas_cursor (current_canvas_cursor);
3317 }
3318
3319 void
3320 Editor::bounce_range_selection (bool replace, bool enable_processing)
3321 {
3322         if (selection->time.empty()) {
3323                 return;
3324         }
3325
3326         TrackSelection views = selection->tracks;
3327
3328         for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
3329                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
3330                 if (rtv && rtv->track() && replace && enable_processing && !rtv->track()->bounceable()) {
3331                         MessageDialog d (
3332                                 _("You can't perform this operation because the processing of the signal "
3333                                   "will cause one or more of the tracks will end up with a region with more channels than this track has inputs.\n\n"
3334                                   "You can do this without processing, which is a different operation.")
3335                                 );
3336                         d.set_title (_("Cannot bounce"));
3337                         d.run ();
3338                         return;
3339                 }
3340         }
3341
3342         framepos_t start = selection->time[clicked_selection].start;
3343         framepos_t end = selection->time[clicked_selection].end;
3344         framepos_t cnt = end - start + 1;
3345
3346         begin_reversible_command (_("bounce range"));
3347
3348         for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
3349
3350                 RouteTimeAxisView* rtv;
3351
3352                 if ((rtv = dynamic_cast<RouteTimeAxisView*> (*i)) == 0) {
3353                         continue;
3354                 }
3355
3356                 boost::shared_ptr<Playlist> playlist;
3357
3358                 if ((playlist = rtv->playlist()) == 0) {
3359                         return;
3360                 }
3361
3362                 InterThreadInfo itt;
3363
3364                 playlist->clear_changes ();
3365                 playlist->clear_owned_changes ();
3366
3367                 boost::shared_ptr<Region> r = rtv->track()->bounce_range (start, start+cnt, itt, enable_processing);
3368
3369                 if (replace) {
3370                         list<AudioRange> ranges;
3371                         ranges.push_back (AudioRange (start, start+cnt, 0));
3372                         playlist->cut (ranges); // discard result
3373                         playlist->add_region (r, start);
3374                 }
3375
3376                 vector<Command*> cmds;
3377                 playlist->rdiff (cmds);
3378                 _session->add_commands (cmds);
3379
3380                 _session->add_command (new StatefulDiffCommand (playlist));
3381         }
3382
3383         commit_reversible_command ();
3384 }
3385
3386 /** Delete selected regions, automation points or a time range */
3387 void
3388 Editor::delete_ ()
3389 {
3390         cut_copy (Delete);
3391 }
3392
3393 /** Cut selected regions, automation points or a time range */
3394 void
3395 Editor::cut ()
3396 {
3397         cut_copy (Cut);
3398 }
3399
3400 /** Copy selected regions, automation points or a time range */
3401 void
3402 Editor::copy ()
3403 {
3404         cut_copy (Copy);
3405 }
3406
3407
3408 /** @return true if a Cut, Copy or Clear is possible */
3409 bool
3410 Editor::can_cut_copy () const
3411 {
3412         switch (current_mouse_mode()) {
3413
3414         case MouseObject:
3415                 if (!selection->regions.empty() || !selection->points.empty()) {
3416                         return true;
3417                 }
3418                 break;
3419
3420         case MouseRange:
3421                 if (!selection->time.empty()) {
3422                         return true;
3423                 }
3424                 break;
3425
3426         default:
3427                 break;
3428         }
3429
3430         return false;
3431 }
3432
3433
3434 /** Cut, copy or clear selected regions, automation points or a time range.
3435  * @param op Operation (Cut, Copy or Clear)
3436  */
3437 void
3438 Editor::cut_copy (CutCopyOp op)
3439 {
3440         /* only cancel selection if cut/copy is successful.*/
3441
3442         string opname;
3443
3444         switch (op) {
3445         case Delete:
3446                 opname = _("delete");
3447                 break;
3448         case Cut:
3449                 opname = _("cut");
3450                 break;
3451         case Copy:
3452                 opname = _("copy");
3453                 break;
3454         case Clear:
3455                 opname = _("clear");
3456                 break;
3457         }
3458
3459         /* if we're deleting something, and the mouse is still pressed,
3460            the thing we started a drag for will be gone when we release
3461            the mouse button(s). avoid this. see part 2 at the end of
3462            this function.
3463         */
3464
3465         if (op == Delete || op == Cut || op == Clear) {
3466                 if (_drags->active ()) {
3467                         _drags->abort ();
3468                 }
3469         }
3470
3471         cut_buffer->clear ();
3472
3473         if (entered_marker) {
3474
3475                 /* cut/delete op while pointing at a marker */
3476
3477                 bool ignored;
3478                 Location* loc = find_location_from_marker (entered_marker, ignored);
3479
3480                 if (_session && loc) {
3481                         Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::really_remove_marker), loc));
3482                 }
3483
3484                 _drags->abort ();
3485                 return;
3486         }
3487
3488         if (internal_editing()) {
3489
3490                 switch (current_mouse_mode()) {
3491                 case MouseObject:
3492                 case MouseRange:
3493                         cut_copy_midi (op);
3494                         break;
3495                 default:
3496                         break;
3497                 }
3498
3499         } else {
3500
3501                 RegionSelection rs;
3502
3503                 /* we only want to cut regions if some are selected */
3504
3505                 if (!selection->regions.empty()) {
3506                         rs = selection->regions;
3507                 }
3508
3509                 switch (current_mouse_mode()) {
3510                 case MouseObject:
3511                         if (!rs.empty() || !selection->points.empty()) {
3512
3513                                 begin_reversible_command (opname + _(" objects"));
3514
3515                                 if (!rs.empty()) {
3516                                         cut_copy_regions (op, rs);
3517
3518                                         if (op == Cut || op == Delete) {
3519                                                 selection->clear_regions ();
3520                                         }
3521                                 }
3522
3523                                 if (!selection->points.empty()) {
3524                                         cut_copy_points (op);
3525
3526                                         if (op == Cut || op == Delete) {
3527                                                 selection->clear_points ();
3528                                         }
3529                                 }
3530                                 commit_reversible_command ();
3531                                 break; // terminate case statement here
3532                         }
3533                         if (!selection->time.empty()) {
3534                                 /* don't cause suprises */
3535                                 break;
3536                         }
3537                         // fall thru if there was nothing selected
3538
3539                 case MouseRange:
3540                         if (selection->time.empty()) {
3541                                 framepos_t start, end;
3542                                 if (!get_edit_op_range (start, end)) {
3543                                         return;
3544                                 }
3545                                 selection->set (start, end);
3546                         }
3547
3548                         begin_reversible_command (opname + _(" range"));
3549                         cut_copy_ranges (op);
3550                         commit_reversible_command ();
3551
3552                         if (op == Cut || op == Delete) {
3553                                 selection->clear_time ();
3554                         }
3555
3556                         break;
3557
3558                 default:
3559                         break;
3560                 }
3561         }
3562
3563         if (op == Delete || op == Cut || op == Clear) {
3564                 _drags->abort ();
3565         }
3566 }
3567
3568 /** Cut, copy or clear selected automation points.
3569  * @param op Operation (Cut, Copy or Clear)
3570  */
3571 void
3572 Editor::cut_copy_points (CutCopyOp op)
3573 {
3574         for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
3575
3576                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>((*i).track);
3577                 _last_cut_copy_source_track = atv;
3578
3579                 if (atv) {
3580                         atv->cut_copy_clear_objects (selection->points, op);
3581                 }
3582         }
3583 }
3584
3585 /** Cut, copy or clear selected automation points.
3586  * @param op Operation (Cut, Copy or Clear)
3587  */
3588 void
3589 Editor::cut_copy_midi (CutCopyOp op)
3590 {
3591         for (MidiRegionSelection::iterator i = selection->midi_regions.begin(); i != selection->midi_regions.end(); ++i) {
3592                 MidiRegionView* mrv = *i;
3593                 mrv->cut_copy_clear (op);
3594         }
3595 }
3596
3597
3598
3599 struct lt_playlist {
3600     bool operator () (const PlaylistState& a, const PlaylistState& b) {
3601             return a.playlist < b.playlist;
3602     }
3603 };
3604
3605 struct PlaylistMapping {
3606     TimeAxisView* tv;
3607     boost::shared_ptr<Playlist> pl;
3608
3609     PlaylistMapping (TimeAxisView* tvp) : tv (tvp) {}
3610 };
3611
3612 /** Remove `clicked_regionview' */
3613 void
3614 Editor::remove_clicked_region ()
3615 {
3616         if (clicked_routeview == 0 || clicked_regionview == 0) {
3617                 return;
3618         }
3619
3620         boost::shared_ptr<Playlist> playlist = clicked_routeview->playlist();
3621
3622         begin_reversible_command (_("remove region"));
3623         playlist->clear_changes ();
3624         playlist->remove_region (clicked_regionview->region());
3625         _session->add_command(new StatefulDiffCommand (playlist));
3626         commit_reversible_command ();
3627 }
3628
3629
3630 /** Remove the selected regions */
3631 void
3632 Editor::remove_selected_regions ()
3633 {
3634         RegionSelection rs = get_regions_from_selection_and_entered ();
3635
3636         if (!_session || rs.empty()) {
3637                 return;
3638         }
3639
3640         begin_reversible_command (_("remove region"));
3641
3642         list<boost::shared_ptr<Region> > regions_to_remove;
3643
3644         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
3645                 // we can't just remove the region(s) in this loop because
3646                 // this removes them from the RegionSelection, and they thus
3647                 // disappear from underneath the iterator, and the ++i above
3648                 // SEGVs in a puzzling fashion.
3649
3650                 // so, first iterate over the regions to be removed from rs and
3651                 // add them to the regions_to_remove list, and then
3652                 // iterate over the list to actually remove them.
3653
3654                 regions_to_remove.push_back ((*i)->region());
3655         }
3656
3657         vector<boost::shared_ptr<Playlist> > playlists;
3658
3659         for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
3660
3661                 boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
3662
3663                 if (!playlist) {
3664                         // is this check necessary?
3665                         continue;
3666                 }
3667
3668                 vector<boost::shared_ptr<Playlist> >::iterator i;
3669
3670                 //only prep history if this is a new playlist.
3671                 for (i = playlists.begin(); i != playlists.end(); ++i) {
3672                         if ((*i) == playlist) {
3673                                 break;
3674                         }
3675                 }
3676
3677                 if (i == playlists.end()) {
3678
3679                         playlist->clear_changes ();
3680                         playlist->freeze ();
3681
3682                         playlists.push_back (playlist);
3683                 }
3684
3685                 playlist->remove_region (*rl);
3686         }
3687
3688         vector<boost::shared_ptr<Playlist> >::iterator pl;
3689
3690         for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
3691                 (*pl)->thaw ();
3692                 _session->add_command(new StatefulDiffCommand (*pl));
3693         }
3694
3695         commit_reversible_command ();
3696 }
3697
3698 /** Cut, copy or clear selected regions.
3699  * @param op Operation (Cut, Copy or Clear)
3700  */
3701 void
3702 Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
3703 {
3704         /* we can't use a std::map here because the ordering is important, and we can't trivially sort
3705            a map when we want ordered access to both elements. i think.
3706         */
3707
3708         vector<PlaylistMapping> pmap;
3709
3710         framepos_t first_position = max_framepos;
3711
3712         typedef set<boost::shared_ptr<Playlist> > FreezeList;
3713         FreezeList freezelist;
3714
3715         /* get ordering correct before we cut/copy */
3716
3717         rs.sort_by_position_and_track ();
3718
3719         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
3720
3721                 first_position = min ((framepos_t) (*x)->region()->position(), first_position);
3722
3723                 if (op == Cut || op == Clear || op == Delete) {
3724                         boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
3725
3726                         if (pl) {
3727                                 FreezeList::iterator fl;
3728
3729                                 // only take state if this is a new playlist.
3730                                 for (fl = freezelist.begin(); fl != freezelist.end(); ++fl) {
3731                                         if ((*fl) == pl) {
3732                                                 break;
3733                                         }
3734                                 }
3735
3736                                 if (fl == freezelist.end()) {
3737                                         pl->clear_changes();
3738                                         pl->freeze ();
3739                                         freezelist.insert (pl);
3740                                 }
3741                         }
3742                 }
3743
3744                 TimeAxisView* tv = &(*x)->get_time_axis_view();
3745                 vector<PlaylistMapping>::iterator z;
3746
3747                 for (z = pmap.begin(); z != pmap.end(); ++z) {
3748                         if ((*z).tv == tv) {
3749                                 break;
3750                         }
3751                 }
3752
3753                 if (z == pmap.end()) {
3754                         pmap.push_back (PlaylistMapping (tv));
3755                 }
3756         }
3757
3758         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ) {
3759
3760                 boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
3761
3762                 if (!pl) {
3763                         /* region not yet associated with a playlist (e.g. unfinished
3764                            capture pass.
3765                         */
3766                         ++x;
3767                         continue;
3768                 }
3769
3770                 TimeAxisView& tv = (*x)->get_time_axis_view();
3771                 boost::shared_ptr<Playlist> npl;
3772                 RegionSelection::iterator tmp;
3773
3774                 tmp = x;
3775                 ++tmp;
3776
3777                 if (op != Delete) {
3778
3779                         vector<PlaylistMapping>::iterator z;
3780                         
3781                         for (z = pmap.begin(); z != pmap.end(); ++z) {
3782                                 if ((*z).tv == &tv) {
3783                                         break;
3784                                 }
3785                         }
3786                         
3787                         assert (z != pmap.end());
3788                         
3789                         if (!(*z).pl) {
3790                                 npl = PlaylistFactory::create (pl->data_type(), *_session, "cutlist", true);
3791                                 npl->freeze();
3792                                 (*z).pl = npl;
3793                         } else {
3794                                 npl = (*z).pl;
3795                         }
3796                 }
3797
3798                 boost::shared_ptr<Region> r = (*x)->region();
3799                 boost::shared_ptr<Region> _xx;
3800
3801                 assert (r != 0);
3802
3803                 switch (op) {
3804                 case Delete:
3805                         pl->remove_region (r);
3806                         break;
3807                         
3808                 case Cut:
3809                         _xx = RegionFactory::create (r);
3810                         npl->add_region (_xx, r->position() - first_position);
3811                         pl->remove_region (r);
3812                         break;
3813
3814                 case Copy:
3815                         /* copy region before adding, so we're not putting same object into two different playlists */
3816                         npl->add_region (RegionFactory::create (r), r->position() - first_position);
3817                         break;
3818
3819                 case Clear:
3820                         pl->remove_region (r);  
3821                         break;
3822                 }
3823
3824                 x = tmp;
3825         }
3826
3827         if (op != Delete) {
3828
3829                 list<boost::shared_ptr<Playlist> > foo;
3830                 
3831                 /* the pmap is in the same order as the tracks in which selected regions occured */
3832                 
3833                 for (vector<PlaylistMapping>::iterator i = pmap.begin(); i != pmap.end(); ++i) {
3834                         if ((*i).pl) {
3835                                 (*i).pl->thaw();
3836                                 foo.push_back ((*i).pl);
3837                         }
3838                 }
3839                 
3840                 if (!foo.empty()) {
3841                         cut_buffer->set (foo);
3842                 }
3843                 
3844                 if (pmap.empty()) {
3845                         _last_cut_copy_source_track = 0;
3846                 } else {
3847                         _last_cut_copy_source_track = pmap.front().tv;
3848                 }
3849         }
3850
3851         for (FreezeList::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
3852                 (*pl)->thaw ();
3853                 _session->add_command (new StatefulDiffCommand (*pl));
3854         }
3855 }
3856
3857 void
3858 Editor::cut_copy_ranges (CutCopyOp op)
3859 {
3860         TrackViewList* ts;
3861         TrackViewList entered;
3862
3863         /* Sort the track selection now, so that it if is used, the playlists
3864            selected by the calls below to cut_copy_clear are in the order that
3865            their tracks appear in the editor.  This makes things like paste
3866            of ranges work properly.
3867         */
3868         sort_track_selection (&selection->tracks);
3869
3870         if (selection->tracks.empty()) {
3871                 if (!entered_track) {
3872                         return;
3873                 }
3874                 entered.push_back (entered_track);
3875                 ts = &entered;
3876         } else {
3877                 ts = &selection->tracks;
3878         }
3879
3880         for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
3881                 (*i)->cut_copy_clear (*selection, op);
3882         }
3883 }
3884
3885 void
3886 Editor::paste (float times)
3887 {
3888         DEBUG_TRACE (DEBUG::CutNPaste, "paste to preferred edit pos\n");
3889         paste_internal (get_preferred_edit_position(), times);
3890 }
3891
3892 void
3893 Editor::mouse_paste ()
3894 {
3895         framepos_t where;
3896         bool ignored;
3897
3898         if (!mouse_frame (where, ignored)) {
3899                 return;
3900         }
3901
3902         snap_to (where);
3903         paste_internal (where, 1);
3904 }
3905
3906 void
3907 Editor::paste_internal (framepos_t position, float times)
3908 {
3909         DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("apparent paste position is %1\n", position));
3910
3911         if (internal_editing()) {
3912                 if (cut_buffer->midi_notes.empty()) {
3913                         return;
3914                 }
3915         } else {
3916                 if (cut_buffer->empty()) {
3917                         return;
3918                 }
3919         }
3920
3921         if (position == max_framepos) {
3922                 position = get_preferred_edit_position();
3923                 DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("preferred edit position is %1\n", position));
3924         }
3925
3926         TrackViewList ts;
3927         TrackViewList::iterator i;
3928         size_t nth;
3929
3930         /* get everything in the correct order */
3931
3932         if (!selection->tracks.empty()) {
3933                 /* there are some selected tracks, so paste to them */
3934                 sort_track_selection ();
3935                 ts = selection->tracks;
3936         } else if (_last_cut_copy_source_track) {
3937                 /* otherwise paste to the track that the cut/copy came from;
3938                    see discussion in mantis #3333.
3939                 */
3940                 ts.push_back (_last_cut_copy_source_track);
3941         }
3942
3943         if (internal_editing ()) {
3944
3945                 /* undo/redo is handled by individual tracks/regions */
3946
3947                 for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
3948
3949                         RegionSelection rs;
3950                         RegionSelection::iterator r;
3951                         MidiNoteSelection::iterator cb;
3952
3953                         get_regions_at (rs, position, ts);
3954
3955                         for (cb = cut_buffer->midi_notes.begin(), r = rs.begin();
3956                              cb != cut_buffer->midi_notes.end() && r != rs.end(); ++r) {
3957                                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*r);
3958                                 if (mrv) {
3959                                         mrv->paste (position, times, **cb);
3960                                         ++cb;
3961                                 }
3962                         }
3963                 }
3964
3965         } else {
3966
3967                 /* we do redo (do you do voodoo?) */
3968
3969                 begin_reversible_command (Operations::paste);
3970
3971                 for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
3972                         (*i)->paste (position, times, *cut_buffer, nth);
3973                 }
3974
3975                 commit_reversible_command ();
3976         }
3977 }
3978
3979 void
3980 Editor::duplicate_some_regions (RegionSelection& regions, float times)
3981 {
3982         boost::shared_ptr<Playlist> playlist;
3983         RegionSelection sel = regions; // clear (below) may  clear the argument list if its the current region selection
3984         RegionSelection foo;
3985
3986         framepos_t const start_frame = regions.start ();
3987         framepos_t const end_frame = regions.end_frame ();
3988
3989         begin_reversible_command (Operations::duplicate_region);
3990
3991         selection->clear_regions ();
3992
3993         for (RegionSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
3994
3995                 boost::shared_ptr<Region> r ((*i)->region());
3996
3997                 TimeAxisView& tv = (*i)->get_time_axis_view();
3998                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&tv);
3999                 latest_regionviews.clear ();
4000                 sigc::connection c = rtv->view()->RegionViewAdded.connect (sigc::mem_fun(*this, &Editor::collect_new_region_view));
4001
4002                 playlist = (*i)->region()->playlist();
4003                 playlist->clear_changes ();
4004                 playlist->duplicate (r, end_frame + (r->first_frame() - start_frame), times);
4005                 _session->add_command(new StatefulDiffCommand (playlist));
4006
4007                 c.disconnect ();
4008
4009                 foo.insert (foo.end(), latest_regionviews.begin(), latest_regionviews.end());
4010         }
4011
4012         commit_reversible_command ();
4013
4014         if (!foo.empty()) {
4015                 selection->set (foo);
4016         }
4017 }
4018
4019 void
4020 Editor::duplicate_selection (float times)
4021 {
4022         if (selection->time.empty() || selection->tracks.empty()) {
4023                 return;
4024         }
4025
4026         boost::shared_ptr<Playlist> playlist;
4027         vector<boost::shared_ptr<Region> > new_regions;
4028         vector<boost::shared_ptr<Region> >::iterator ri;
4029
4030         create_region_from_selection (new_regions);
4031
4032         if (new_regions.empty()) {
4033                 return;
4034         }
4035
4036         begin_reversible_command (_("duplicate selection"));
4037
4038         ri = new_regions.begin();
4039
4040         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4041                 if ((playlist = (*i)->playlist()) == 0) {
4042                         continue;
4043                 }
4044                 playlist->clear_changes ();
4045                 playlist->duplicate (*ri, selection->time[clicked_selection].end, times);
4046                 _session->add_command (new StatefulDiffCommand (playlist));
4047
4048                 ++ri;
4049                 if (ri == new_regions.end()) {
4050                         --ri;
4051                 }
4052         }
4053
4054         commit_reversible_command ();
4055 }
4056
4057 void
4058 Editor::reset_point_selection ()
4059 {
4060         /* reset all selected points to the relevant default value */
4061
4062         for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
4063
4064                 AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>((*i).track);
4065
4066                 if (atv) {
4067                         atv->reset_objects (selection->points);
4068                 }
4069         }
4070 }
4071
4072 void
4073 Editor::center_playhead ()
4074 {
4075         float page = _canvas_width * frames_per_unit;
4076         center_screen_internal (playhead_cursor->current_frame, page);
4077 }
4078
4079 void
4080 Editor::center_edit_point ()
4081 {
4082         float page = _canvas_width * frames_per_unit;
4083         center_screen_internal (get_preferred_edit_position(), page);
4084 }
4085
4086 /** Caller must begin and commit a reversible command */
4087 void
4088 Editor::clear_playlist (boost::shared_ptr<Playlist> playlist)
4089 {
4090         playlist->clear_changes ();
4091         playlist->clear ();
4092         _session->add_command (new StatefulDiffCommand (playlist));
4093 }
4094
4095 void
4096 Editor::nudge_track (bool use_edit, bool forwards)
4097 {
4098         boost::shared_ptr<Playlist> playlist;
4099         framepos_t distance;
4100         framepos_t next_distance;
4101         framepos_t start;
4102
4103         if (use_edit) {
4104                 start = get_preferred_edit_position();
4105         } else {
4106                 start = 0;
4107         }
4108
4109         if ((distance = get_nudge_distance (start, next_distance)) == 0) {
4110                 return;
4111         }
4112
4113         if (selection->tracks.empty()) {
4114                 return;
4115         }
4116
4117         begin_reversible_command (_("nudge track"));
4118
4119         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4120
4121                 if ((playlist = (*i)->playlist()) == 0) {
4122                         continue;
4123                 }
4124
4125                 playlist->clear_changes ();
4126                 playlist->clear_owned_changes ();
4127
4128                 playlist->nudge_after (start, distance, forwards);
4129
4130                 vector<Command*> cmds;
4131
4132                 playlist->rdiff (cmds);
4133                 _session->add_commands (cmds);
4134
4135                 _session->add_command (new StatefulDiffCommand (playlist));
4136         }
4137
4138         commit_reversible_command ();
4139 }
4140
4141 void
4142 Editor::remove_last_capture ()
4143 {
4144         vector<string> choices;
4145         string prompt;
4146
4147         if (!_session) {
4148                 return;
4149         }
4150
4151         if (Config->get_verify_remove_last_capture()) {
4152                 prompt  = _("Do you really want to destroy the last capture?"
4153                             "\n(This is destructive and cannot be undone)");
4154
4155                 choices.push_back (_("No, do nothing."));
4156                 choices.push_back (_("Yes, destroy it."));
4157
4158                 Gtkmm2ext::Choice prompter (_("Destroy last capture"), prompt, choices);
4159
4160                 if (prompter.run () == 1) {
4161                         _session->remove_last_capture ();
4162                         _regions->redisplay ();
4163                 }
4164
4165         } else {
4166                 _session->remove_last_capture();
4167                 _regions->redisplay ();
4168         }
4169 }
4170
4171 void
4172 Editor::normalize_region ()
4173 {
4174         if (!_session) {
4175                 return;
4176         }
4177
4178         RegionSelection rs = get_regions_from_selection_and_entered ();
4179
4180         if (rs.empty()) {
4181                 return;
4182         }
4183
4184         NormalizeDialog dialog (rs.size() > 1);
4185
4186         if (dialog.run () == RESPONSE_CANCEL) {
4187                 return;
4188         }
4189
4190         set_canvas_cursor (_cursors->wait);
4191         gdk_flush ();
4192
4193         /* XXX: should really only count audio regions here */
4194         int const regions = rs.size ();
4195
4196         /* Make a list of the selected audio regions' maximum amplitudes, and also
4197            obtain the maximum amplitude of them all.
4198         */
4199         list<double> max_amps;
4200         double max_amp = 0;
4201         for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
4202                 AudioRegionView const * arv = dynamic_cast<AudioRegionView const *> (*i);
4203                 if (arv) {
4204                         dialog.descend (1.0 / regions);
4205                         double const a = arv->audio_region()->maximum_amplitude (&dialog);
4206
4207                         if (a == -1) {
4208                                 /* the user cancelled the operation */
4209                                 set_canvas_cursor (current_canvas_cursor);
4210                                 return;
4211                         }
4212
4213                         max_amps.push_back (a);
4214                         max_amp = max (max_amp, a);
4215                         dialog.ascend ();
4216                 }
4217         }
4218
4219         begin_reversible_command (_("normalize"));
4220
4221         list<double>::const_iterator a = max_amps.begin ();
4222
4223         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
4224                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (*r);
4225                 if (!arv) {
4226                         continue;
4227                 }
4228
4229                 arv->region()->clear_changes ();
4230
4231                 double const amp = dialog.normalize_individually() ? *a : max_amp;
4232
4233                 arv->audio_region()->normalize (amp, dialog.target ());
4234                 _session->add_command (new StatefulDiffCommand (arv->region()));
4235
4236                 ++a;
4237         }
4238
4239         commit_reversible_command ();
4240         set_canvas_cursor (current_canvas_cursor);
4241 }
4242
4243
4244 void
4245 Editor::reset_region_scale_amplitude ()
4246 {
4247         if (!_session) {
4248                 return;
4249         }
4250
4251         RegionSelection rs = get_regions_from_selection_and_entered ();
4252
4253         if (rs.empty()) {
4254                 return;
4255         }
4256
4257         begin_reversible_command ("reset gain");
4258
4259         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
4260                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
4261                 if (!arv)
4262                         continue;
4263                 arv->region()->clear_changes ();
4264                 arv->audio_region()->set_scale_amplitude (1.0f);
4265                 _session->add_command (new StatefulDiffCommand (arv->region()));
4266         }
4267
4268         commit_reversible_command ();
4269 }
4270
4271 void
4272 Editor::adjust_region_gain (bool up)
4273 {
4274         RegionSelection rs = get_regions_from_selection_and_entered ();
4275
4276         if (!_session || rs.empty()) {
4277                 return;
4278         }
4279
4280         begin_reversible_command ("adjust region gain");
4281
4282         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
4283                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
4284                 if (!arv) {
4285                         continue;
4286                 }
4287
4288                 arv->region()->clear_changes ();
4289
4290                 double dB = accurate_coefficient_to_dB (arv->audio_region()->scale_amplitude ());
4291
4292                 if (up) {
4293                         dB += 1;
4294                 } else {
4295                         dB -= 1;
4296                 }
4297
4298                 arv->audio_region()->set_scale_amplitude (dB_to_coefficient (dB));
4299                 _session->add_command (new StatefulDiffCommand (arv->region()));
4300         }
4301
4302         commit_reversible_command ();
4303 }
4304
4305
4306 void
4307 Editor::reverse_region ()
4308 {
4309         if (!_session) {
4310                 return;
4311         }
4312
4313         Reverse rev (*_session);
4314         apply_filter (rev, _("reverse regions"));
4315 }
4316
4317 void
4318 Editor::strip_region_silence ()
4319 {
4320         if (!_session) {
4321                 return;
4322         }
4323
4324         RegionSelection rs = get_regions_from_selection_and_entered ();
4325
4326         if (rs.empty()) {
4327                 return;
4328         }
4329
4330         std::list<RegionView*> audio_only;
4331
4332         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4333                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (*i);
4334                 if (arv) {
4335                         audio_only.push_back (arv);
4336                 }
4337         }
4338
4339         StripSilenceDialog d (_session, audio_only);
4340         int const r = d.run ();
4341
4342         d.drop_rects ();
4343
4344         if (r == Gtk::RESPONSE_OK) {
4345                 ARDOUR::AudioIntervalMap silences;
4346                 d.silences (silences);
4347                 StripSilence s (*_session, silences, d.fade_length());
4348                 apply_filter (s, _("strip silence"), &d);
4349         }
4350 }
4351
4352 Command*
4353 Editor::apply_midi_note_edit_op_to_region (MidiOperator& op, MidiRegionView& mrv)
4354 {
4355         Evoral::Sequence<Evoral::MusicalTime>::Notes selected;
4356         mrv.selection_as_notelist (selected, true);
4357
4358         vector<Evoral::Sequence<Evoral::MusicalTime>::Notes> v;
4359         v.push_back (selected);
4360
4361         framepos_t pos_frames = mrv.midi_region()->position();
4362         double     pos_beats  = _session->tempo_map().framewalk_to_beats(0, pos_frames);
4363
4364         return op (mrv.midi_region()->model(), pos_beats, v);
4365 }
4366
4367 void
4368 Editor::apply_midi_note_edit_op (MidiOperator& op)
4369 {
4370         Command* cmd;
4371
4372         RegionSelection rs = get_regions_from_selection_and_entered ();
4373
4374         if (rs.empty()) {
4375                 return;
4376         }
4377
4378         begin_reversible_command (op.name ());
4379
4380         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
4381                 RegionSelection::iterator tmp = r;
4382                 ++tmp;
4383
4384                 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
4385
4386                 if (mrv) {
4387                         cmd = apply_midi_note_edit_op_to_region (op, *mrv);
4388                         if (cmd) {
4389                                 (*cmd)();
4390                                 _session->add_command (cmd);
4391                         }
4392                 }
4393
4394                 r = tmp;
4395         }
4396
4397         commit_reversible_command ();
4398 }
4399
4400 void
4401 Editor::fork_region ()
4402 {
4403         RegionSelection rs = get_regions_from_selection_and_entered ();
4404
4405         if (rs.empty()) {
4406                 return;
4407         }
4408
4409         begin_reversible_command (_("Fork Region(s)"));
4410
4411         set_canvas_cursor (_cursors->wait);
4412         gdk_flush ();
4413
4414         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
4415                 RegionSelection::iterator tmp = r;
4416                 ++tmp;
4417
4418                 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*>(*r);
4419
4420                 if (mrv) {
4421                         boost::shared_ptr<Playlist> playlist = mrv->region()->playlist();
4422                         boost::shared_ptr<MidiRegion> newregion = mrv->midi_region()->clone ();
4423
4424                         playlist->clear_changes ();
4425                         playlist->replace_region (mrv->region(), newregion, mrv->region()->position());
4426                         _session->add_command(new StatefulDiffCommand (playlist));
4427                 }
4428
4429                 r = tmp;
4430         }
4431
4432         commit_reversible_command ();
4433
4434         set_canvas_cursor (current_canvas_cursor);
4435 }
4436
4437 void
4438 Editor::quantize_region ()
4439 {
4440         int selected_midi_region_cnt = 0;
4441
4442         if (!_session) {
4443                 return;
4444         }
4445
4446         RegionSelection rs = get_regions_from_selection_and_entered ();
4447
4448         if (rs.empty()) {
4449                 return;
4450         }
4451
4452         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
4453                 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
4454                 if (mrv) {
4455                         selected_midi_region_cnt++;
4456                 }
4457         }
4458
4459         if (selected_midi_region_cnt == 0) {
4460                 return;
4461         }
4462
4463         QuantizeDialog* qd = new QuantizeDialog (*this);
4464
4465         qd->present ();
4466         const int r = qd->run ();
4467         qd->hide ();
4468
4469         if (r == Gtk::RESPONSE_OK) {
4470                 Quantize quant (*_session, Plain,
4471                                 qd->snap_start(), qd->snap_end(),
4472                                 qd->start_grid_size(), qd->end_grid_size(),
4473                                 qd->strength(), qd->swing(), qd->threshold());
4474
4475                 apply_midi_note_edit_op (quant);
4476         }
4477 }
4478
4479 void
4480 Editor::insert_patch_change ()
4481 {
4482         RegionSelection rs = get_regions_from_selection_and_entered ();
4483         if (rs.empty ()) {
4484                 return;
4485         }
4486
4487         framepos_t const p = get_preferred_edit_position (false);
4488
4489         Evoral::PatchChange<Evoral::MusicalTime> empty (0, 0, 0, 0);
4490         PatchChangeDialog d (0, _session, empty, Gtk::Stock::ADD);
4491
4492         if (d.run() == RESPONSE_CANCEL) {
4493                 return;
4494         }
4495
4496         for (RegionSelection::iterator i = rs.begin (); i != rs.end(); ++i) {
4497                 MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*i);
4498                 if (mrv) {
4499                         if (p >= mrv->region()->first_frame() && p <= mrv->region()->last_frame()) {
4500                                 mrv->add_patch_change (p - mrv->region()->position(), d.patch ());
4501                         }
4502                 }
4503         }
4504 }
4505
4506 void
4507 Editor::apply_filter (Filter& filter, string command, ProgressReporter* progress)
4508 {
4509         RegionSelection rs = get_regions_from_selection_and_entered ();
4510
4511         if (rs.empty()) {
4512                 return;
4513         }
4514
4515         begin_reversible_command (command);
4516
4517         set_canvas_cursor (_cursors->wait);
4518         gdk_flush ();
4519
4520         int n = 0;
4521         int const N = rs.size ();
4522
4523         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
4524                 RegionSelection::iterator tmp = r;
4525                 ++tmp;
4526
4527                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
4528                 if (arv) {
4529                         boost::shared_ptr<Playlist> playlist = arv->region()->playlist();
4530
4531                         if (progress) {
4532                                 progress->descend (1.0 / N);
4533                         }
4534
4535                         if (arv->audio_region()->apply (filter, progress) == 0) {
4536
4537                                 playlist->clear_changes ();
4538
4539                                 if (filter.results.empty ()) {
4540
4541                                         /* no regions returned; remove the old one */
4542                                         playlist->remove_region (arv->region ());
4543
4544                                 } else {
4545
4546                                         std::vector<boost::shared_ptr<Region> >::iterator res = filter.results.begin ();
4547
4548                                         /* first region replaces the old one */
4549                                         playlist->replace_region (arv->region(), *res, (*res)->position());
4550                                         ++res;
4551
4552                                         /* add the rest */
4553                                         while (res != filter.results.end()) {
4554                                                 playlist->add_region (*res, (*res)->position());
4555                                                 ++res;
4556                                         }
4557
4558                                 }
4559
4560                                 _session->add_command(new StatefulDiffCommand (playlist));
4561                         } else {
4562                                 goto out;
4563                         }
4564
4565                         if (progress) {
4566                                 progress->ascend ();
4567                         }
4568                 }
4569
4570                 r = tmp;
4571                 ++n;
4572         }
4573
4574         commit_reversible_command ();
4575
4576   out:
4577         set_canvas_cursor (current_canvas_cursor);
4578 }
4579
4580 void
4581 Editor::external_edit_region ()
4582 {
4583         /* more to come */
4584 }
4585
4586 void
4587 Editor::reset_region_gain_envelopes ()
4588 {
4589         RegionSelection rs = get_regions_from_selection_and_entered ();
4590
4591         if (!_session || rs.empty()) {
4592                 return;
4593         }
4594
4595         _session->begin_reversible_command (_("reset region gain"));
4596
4597         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4598                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
4599                 if (arv) {
4600                         boost::shared_ptr<AutomationList> alist (arv->audio_region()->envelope());
4601                         XMLNode& before (alist->get_state());
4602
4603                         arv->audio_region()->set_default_envelope ();
4604                         _session->add_command (new MementoCommand<AutomationList>(*arv->audio_region()->envelope().get(), &before, &alist->get_state()));
4605                 }
4606         }
4607
4608         _session->commit_reversible_command ();
4609 }
4610
4611 void
4612 Editor::toggle_gain_envelope_visibility ()
4613 {
4614         if (_ignore_region_action) {
4615                 return;
4616         }
4617
4618         RegionSelection rs = get_regions_from_selection_and_entered ();
4619
4620         if (!_session || rs.empty()) {
4621                 return;
4622         }
4623
4624         _session->begin_reversible_command (_("region gain envelope visible"));
4625
4626         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4627                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
4628                 if (arv) {
4629                         arv->region()->clear_changes ();
4630                         arv->set_envelope_visible (!arv->envelope_visible());
4631                         _session->add_command (new StatefulDiffCommand (arv->region()));
4632                 }
4633         }
4634
4635         _session->commit_reversible_command ();
4636 }
4637
4638 void
4639 Editor::toggle_gain_envelope_active ()
4640 {
4641         if (_ignore_region_action) {
4642                 return;
4643         }
4644
4645         RegionSelection rs = get_regions_from_selection_and_entered ();
4646
4647         if (!_session || rs.empty()) {
4648                 return;
4649         }
4650
4651         _session->begin_reversible_command (_("region gain envelope active"));
4652
4653         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4654                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
4655                 if (arv) {
4656                         arv->region()->clear_changes ();
4657                         arv->audio_region()->set_envelope_active (!arv->audio_region()->envelope_active());
4658                         _session->add_command (new StatefulDiffCommand (arv->region()));
4659                 }
4660         }
4661
4662         _session->commit_reversible_command ();
4663 }
4664
4665 void
4666 Editor::toggle_region_lock ()
4667 {
4668         if (_ignore_region_action) {
4669                 return;
4670         }
4671
4672         RegionSelection rs = get_regions_from_selection_and_entered ();
4673
4674         if (!_session || rs.empty()) {
4675                 return;
4676         }
4677
4678         _session->begin_reversible_command (_("toggle region lock"));
4679
4680         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4681                 (*i)->region()->clear_changes ();
4682                 (*i)->region()->set_locked (!(*i)->region()->locked());
4683                 _session->add_command (new StatefulDiffCommand ((*i)->region()));
4684         }
4685
4686         _session->commit_reversible_command ();
4687 }
4688
4689 void
4690 Editor::toggle_region_lock_style ()
4691 {
4692         if (_ignore_region_action) {
4693                 return;
4694         }
4695
4696         RegionSelection rs = get_regions_from_selection_and_entered ();
4697
4698         if (!_session || rs.empty()) {
4699                 return;
4700         }
4701
4702         _session->begin_reversible_command (_("region lock style"));
4703
4704         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4705                 (*i)->region()->clear_changes ();
4706                 PositionLockStyle const ns = (*i)->region()->position_lock_style() == AudioTime ? MusicTime : AudioTime;
4707                 (*i)->region()->set_position_lock_style (ns);
4708                 _session->add_command (new StatefulDiffCommand ((*i)->region()));
4709         }
4710
4711         _session->commit_reversible_command ();
4712 }
4713
4714 void
4715 Editor::toggle_opaque_region ()
4716 {
4717         if (_ignore_region_action) {
4718                 return;
4719         }
4720
4721         RegionSelection rs = get_regions_from_selection_and_entered ();
4722
4723         if (!_session || rs.empty()) {
4724                 return;
4725         }
4726
4727         _session->begin_reversible_command (_("change region opacity"));
4728
4729         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
4730                 (*i)->region()->clear_changes ();
4731                 (*i)->region()->set_opaque (!(*i)->region()->opaque());
4732                 _session->add_command (new StatefulDiffCommand ((*i)->region()));
4733         }
4734
4735         _session->commit_reversible_command ();
4736 }
4737
4738 void
4739 Editor::toggle_record_enable ()
4740 {
4741         bool new_state = false;
4742         bool first = true;
4743         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4744                 RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
4745                 if (!rtav)
4746                         continue;
4747                 if (!rtav->is_track())
4748                         continue;
4749
4750                 if (first) {
4751                         new_state = !rtav->track()->record_enabled();
4752                         first = false;
4753                 }
4754
4755                 rtav->track()->set_record_enabled (new_state, this);
4756         }
4757 }
4758
4759 void
4760 Editor::toggle_solo ()
4761 {
4762         bool new_state = false;
4763         bool first = true;
4764         boost::shared_ptr<RouteList> rl (new RouteList);
4765
4766         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4767                 RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
4768
4769                 if (!rtav) {
4770                         continue;
4771                 }
4772
4773                 if (first) {
4774                         new_state = !rtav->route()->soloed ();
4775                         first = false;
4776                 }
4777
4778                 rl->push_back (rtav->route());
4779         }
4780
4781         _session->set_solo (rl, new_state, Session::rt_cleanup, true);
4782 }
4783
4784 void
4785 Editor::toggle_mute ()
4786 {
4787         bool new_state = false;
4788         bool first = true;
4789         boost::shared_ptr<RouteList> rl (new RouteList);
4790
4791         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4792                 RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
4793
4794                 if (!rtav) {
4795                         continue;
4796                 }
4797
4798                 if (first) {
4799                         new_state = !rtav->route()->muted();
4800                         first = false;
4801                 }
4802
4803                 rl->push_back (rtav->route());
4804         }
4805
4806         _session->set_mute (rl, new_state, Session::rt_cleanup, true);
4807 }
4808
4809 void
4810 Editor::toggle_solo_isolate ()
4811 {
4812 }
4813
4814 void
4815 Editor::set_fade_length (bool in)
4816 {
4817         RegionSelection rs = get_regions_from_selection_and_entered ();
4818
4819         if (rs.empty()) {
4820                 return;
4821         }
4822
4823         /* we need a region to measure the offset from the start */
4824
4825         RegionView* rv = rs.front ();
4826
4827         framepos_t pos = get_preferred_edit_position();
4828         framepos_t len;
4829         char const * cmd;
4830
4831         if (pos > rv->region()->last_frame() || pos < rv->region()->first_frame()) {
4832                 /* edit point is outside the relevant region */
4833                 return;
4834         }
4835
4836         if (in) {
4837                 if (pos <= rv->region()->position()) {
4838                         /* can't do it */
4839                         return;
4840                 }
4841                 len = pos - rv->region()->position();
4842                 cmd = _("set fade in length");
4843         } else {
4844                 if (pos >= rv->region()->last_frame()) {
4845                         /* can't do it */
4846                         return;
4847                 }
4848                 len = rv->region()->last_frame() - pos;
4849                 cmd = _("set fade out length");
4850         }
4851
4852         begin_reversible_command (cmd);
4853
4854         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
4855                 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
4856
4857                 if (!tmp) {
4858                         return;
4859                 }
4860
4861                 boost::shared_ptr<AutomationList> alist;
4862                 if (in) {
4863                         alist = tmp->audio_region()->fade_in();
4864                 } else {
4865                         alist = tmp->audio_region()->fade_out();
4866                 }
4867
4868                 XMLNode &before = alist->get_state();
4869
4870                 if (in) {
4871                         tmp->audio_region()->set_fade_in_length (len);
4872                         tmp->audio_region()->set_fade_in_active (true);
4873                 } else {
4874                         tmp->audio_region()->set_fade_out_length (len);
4875                         tmp->audio_region()->set_fade_out_active (true);
4876                 }
4877
4878                 XMLNode &after = alist->get_state();
4879                 _session->add_command(new MementoCommand<AutomationList>(*alist, &before, &after));
4880         }
4881
4882         commit_reversible_command ();
4883 }
4884
4885 void
4886 Editor::set_fade_in_shape (FadeShape shape)
4887 {
4888         RegionSelection rs = get_regions_from_selection_and_entered ();
4889
4890         if (rs.empty()) {
4891                 return;
4892         }
4893
4894         begin_reversible_command (_("set fade in shape"));
4895
4896         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
4897                 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
4898
4899                 if (!tmp) {
4900                         return;
4901                 }
4902
4903                 boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_in();
4904                 XMLNode &before = alist->get_state();
4905
4906                 tmp->audio_region()->set_fade_in_shape (shape);
4907
4908                 XMLNode &after = alist->get_state();
4909                 _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
4910         }
4911
4912         commit_reversible_command ();
4913
4914 }
4915
4916 void
4917 Editor::set_fade_out_shape (FadeShape shape)
4918 {
4919         RegionSelection rs = get_regions_from_selection_and_entered ();
4920
4921         if (rs.empty()) {
4922                 return;
4923         }
4924
4925         begin_reversible_command (_("set fade out shape"));
4926
4927         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
4928                 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
4929
4930                 if (!tmp) {
4931                         return;
4932                 }
4933
4934                 boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_out();
4935                 XMLNode &before = alist->get_state();
4936
4937                 tmp->audio_region()->set_fade_out_shape (shape);
4938
4939                 XMLNode &after = alist->get_state();
4940                 _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
4941         }
4942
4943         commit_reversible_command ();
4944 }
4945
4946 void
4947 Editor::set_fade_in_active (bool yn)
4948 {
4949         RegionSelection rs = get_regions_from_selection_and_entered ();
4950
4951         if (rs.empty()) {
4952                 return;
4953         }
4954
4955         begin_reversible_command (_("set fade in active"));
4956
4957         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
4958                 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
4959
4960                 if (!tmp) {
4961                         return;
4962                 }
4963
4964
4965                 boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
4966
4967                 ar->clear_changes ();
4968                 ar->set_fade_in_active (yn);
4969                 _session->add_command (new StatefulDiffCommand (ar));
4970         }
4971
4972         commit_reversible_command ();
4973 }
4974
4975 void
4976 Editor::set_fade_out_active (bool yn)
4977 {
4978         RegionSelection rs = get_regions_from_selection_and_entered ();
4979
4980         if (rs.empty()) {
4981                 return;
4982         }
4983
4984         begin_reversible_command (_("set fade out active"));
4985
4986         for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
4987                 AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
4988
4989                 if (!tmp) {
4990                         return;
4991                 }
4992
4993                 boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
4994
4995                 ar->clear_changes ();
4996                 ar->set_fade_out_active (yn);
4997                 _session->add_command(new StatefulDiffCommand (ar));
4998         }
4999
5000         commit_reversible_command ();
5001 }
5002
5003 void
5004 Editor::toggle_region_fades (int dir)
5005 {
5006         boost::shared_ptr<AudioRegion> ar;
5007         bool yn;
5008
5009         RegionSelection rs = get_regions_from_selection_and_entered ();
5010
5011         if (rs.empty()) {
5012                 return;
5013         }
5014
5015         RegionSelection::iterator i;
5016         for (i = rs.begin(); i != rs.end(); ++i) {
5017                 if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) != 0) {
5018                         if (dir == -1) {
5019                                 yn = ar->fade_out_active ();
5020                         } else {
5021                                 yn = ar->fade_in_active ();
5022                         }
5023                         break;
5024                 }
5025         }
5026
5027         if (i == rs.end()) {
5028                 return;
5029         }
5030
5031         /* XXX should this undo-able? */
5032
5033         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5034                 if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) == 0) {
5035                         continue;
5036                 }
5037                 if (dir == 1 || dir == 0) {
5038                         ar->set_fade_in_active (!yn);
5039                 }
5040
5041                 if (dir == -1 || dir == 0) {
5042                         ar->set_fade_out_active (!yn);
5043                 }
5044         }
5045 }
5046
5047
5048 /** Update region fade visibility after its configuration has been changed */
5049 void
5050 Editor::update_region_fade_visibility ()
5051 {
5052         bool _fade_visibility = _session->config.get_show_region_fades ();
5053
5054         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
5055                 AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
5056                 if (v) {
5057                         if (_fade_visibility) {
5058                                 v->audio_view()->show_all_fades ();
5059                         } else {
5060                                 v->audio_view()->hide_all_fades ();
5061                         }
5062                 }
5063         }
5064 }
5065
5066 /** Update crossfade visibility after its configuration has been changed */
5067 void
5068 Editor::update_xfade_visibility ()
5069 {
5070         _xfade_visibility = _session->config.get_xfades_visible ();
5071
5072         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
5073                 AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
5074                 if (v) {
5075                         if (_xfade_visibility) {
5076                                 v->show_all_xfades ();
5077                         } else {
5078                                 v->hide_all_xfades ();
5079                         }
5080                 }
5081         }
5082 }
5083
5084 void
5085 Editor::set_edit_point ()
5086 {
5087         framepos_t where;
5088         bool ignored;
5089
5090         if (!mouse_frame (where, ignored)) {
5091                 return;
5092         }
5093
5094         snap_to (where);
5095
5096         if (selection->markers.empty()) {
5097
5098                 mouse_add_new_marker (where);
5099
5100         } else {
5101                 bool ignored;
5102
5103                 Location* loc = find_location_from_marker (selection->markers.front(), ignored);
5104
5105                 if (loc) {
5106                         loc->move_to (where);
5107                 }
5108         }
5109 }
5110
5111 void
5112 Editor::set_playhead_cursor ()
5113 {
5114         if (entered_marker) {
5115                 _session->request_locate (entered_marker->position(), _session->transport_rolling());
5116         } else {
5117                 framepos_t where;
5118                 bool ignored;
5119
5120                 if (!mouse_frame (where, ignored)) {
5121                         return;
5122                 }
5123
5124                 snap_to (where);
5125
5126                 if (_session) {
5127                         _session->request_locate (where, _session->transport_rolling());
5128                 }
5129         }
5130 }
5131
5132 void
5133 Editor::split_region ()
5134 {
5135         if (((mouse_mode == MouseRange) ||
5136              (mouse_mode != MouseObject && _join_object_range_state == JOIN_OBJECT_RANGE_RANGE)) &&
5137             !selection->time.empty()) {
5138                 separate_regions_between (selection->time);
5139                 return;
5140         }
5141
5142         RegionSelection rs = get_regions_from_selection_and_edit_point ();
5143
5144         framepos_t where = get_preferred_edit_position ();
5145
5146         if (rs.empty()) {
5147                 return;
5148         }
5149
5150         split_regions_at (where, rs);
5151 }
5152
5153 void
5154 Editor::ensure_entered_track_selected (bool op_really_wants_one_track_if_none_are_selected)
5155 {
5156         if (entered_track && mouse_mode == MouseObject) {
5157                 if (!selection->tracks.empty()) {
5158                         if (!selection->selected (entered_track)) {
5159                                 selection->add (entered_track);
5160                         }
5161                 } else {
5162                         /* there is no selection, but this operation requires/prefers selected objects */
5163
5164                         if (op_really_wants_one_track_if_none_are_selected) {
5165                                 selection->set (entered_track);
5166                         }
5167                 }
5168         }
5169 }
5170
5171 struct EditorOrderRouteSorter {
5172     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
5173             /* use of ">" forces the correct sort order */
5174             return a->order_key ("editor") < b->order_key ("editor");
5175     }
5176 };
5177
5178 void
5179 Editor::select_next_route()
5180 {
5181         if (selection->tracks.empty()) {
5182                 selection->set (track_views.front());
5183                 return;
5184         }
5185
5186         TimeAxisView* current = selection->tracks.front();
5187
5188         RouteUI *rui;
5189         do {
5190                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
5191                         if (*i == current) {
5192                                 ++i;
5193                                 if (i != track_views.end()) {
5194                                         current = (*i);
5195                                 } else {
5196                                         current = (*(track_views.begin()));
5197                                         //selection->set (*(track_views.begin()));
5198                                 }
5199                                 break;
5200                         }
5201                 }
5202                 rui = dynamic_cast<RouteUI *>(current);
5203         } while ( current->hidden() || (rui != NULL && !rui->route()->active()));
5204
5205         selection->set(current);
5206
5207         ensure_track_visible(current);
5208 }
5209
5210 void
5211 Editor::select_prev_route()
5212 {
5213         if (selection->tracks.empty()) {
5214                 selection->set (track_views.front());
5215                 return;
5216         }
5217
5218         TimeAxisView* current = selection->tracks.front();
5219
5220         RouteUI *rui;
5221         do {
5222                 for (TrackViewList::reverse_iterator i = track_views.rbegin(); i != track_views.rend(); ++i) {
5223                         if (*i == current) {
5224                                 ++i;
5225                                 if (i != track_views.rend()) {
5226                                         current = (*i);
5227                                 } else {
5228                                         current = *(track_views.rbegin());
5229                                 }
5230                                 break;
5231                         }
5232                 }
5233                 rui = dynamic_cast<RouteUI *>(current);
5234         } while ( current->hidden() || (rui != NULL && !rui->route()->active()));
5235
5236         selection->set (current);
5237
5238         ensure_track_visible(current);
5239 }
5240
5241 void
5242 Editor::ensure_track_visible(TimeAxisView *track)
5243 {
5244         if (track->hidden())
5245                 return;
5246
5247         double const current_view_min_y = vertical_adjustment.get_value();
5248         double const current_view_max_y = vertical_adjustment.get_value() + vertical_adjustment.get_page_size() - canvas_timebars_vsize;
5249
5250         double const track_min_y = track->y_position ();
5251         double const track_max_y = track->y_position () + track->effective_height ();
5252
5253         if (track_min_y >= current_view_min_y &&
5254             track_max_y <= current_view_max_y) {
5255                 return;
5256         }
5257
5258         double new_value;
5259
5260         if (track_min_y < current_view_min_y) {
5261                 // Track is above the current view
5262                 new_value = track_min_y;
5263         } else {
5264                 // Track is below the current view
5265                 new_value = track->y_position () + track->effective_height() + canvas_timebars_vsize - vertical_adjustment.get_page_size();
5266         }
5267
5268         vertical_adjustment.set_value(new_value);
5269 }
5270
5271 void
5272 Editor::set_loop_from_selection (bool play)
5273 {
5274         if (_session == 0 || selection->time.empty()) {
5275                 return;
5276         }
5277
5278         framepos_t start = selection->time[clicked_selection].start;
5279         framepos_t end = selection->time[clicked_selection].end;
5280
5281         set_loop_range (start, end,  _("set loop range from selection"));
5282
5283         if (play) {
5284                 _session->request_play_loop (true);
5285                 _session->request_locate (start, true);
5286         }
5287 }
5288
5289 void
5290 Editor::set_loop_from_edit_range (bool play)
5291 {
5292         if (_session == 0) {
5293                 return;
5294         }
5295
5296         framepos_t start;
5297         framepos_t end;
5298
5299         if (!get_edit_op_range (start, end)) {
5300                 return;
5301         }
5302
5303         set_loop_range (start, end,  _("set loop range from edit range"));
5304
5305         if (play) {
5306                 _session->request_play_loop (true);
5307                 _session->request_locate (start, true);
5308         }
5309 }
5310
5311 void
5312 Editor::set_loop_from_region (bool play)
5313 {
5314         framepos_t start = max_framepos;
5315         framepos_t end = 0;
5316
5317         RegionSelection rs = get_regions_from_selection_and_entered ();
5318
5319         if (rs.empty()) {
5320                 return;
5321         }
5322
5323         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5324                 if ((*i)->region()->position() < start) {
5325                         start = (*i)->region()->position();
5326                 }
5327                 if ((*i)->region()->last_frame() + 1 > end) {
5328                         end = (*i)->region()->last_frame() + 1;
5329                 }
5330         }
5331
5332         set_loop_range (start, end, _("set loop range from region"));
5333
5334         if (play) {
5335                 _session->request_play_loop (true);
5336                 _session->request_locate (start, true);
5337         }
5338 }
5339
5340 void
5341 Editor::set_punch_from_selection ()
5342 {
5343         if (_session == 0 || selection->time.empty()) {
5344                 return;
5345         }
5346
5347         framepos_t start = selection->time[clicked_selection].start;
5348         framepos_t end = selection->time[clicked_selection].end;
5349
5350         set_punch_range (start, end,  _("set punch range from selection"));
5351 }
5352
5353 void
5354 Editor::set_punch_from_edit_range ()
5355 {
5356         if (_session == 0) {
5357                 return;
5358         }
5359
5360         framepos_t start;
5361         framepos_t end;
5362
5363         if (!get_edit_op_range (start, end)) {
5364                 return;
5365         }
5366
5367         set_punch_range (start, end,  _("set punch range from edit range"));
5368 }
5369
5370 void
5371 Editor::set_punch_from_region ()
5372 {
5373         framepos_t start = max_framepos;
5374         framepos_t end = 0;
5375
5376         RegionSelection rs = get_regions_from_selection_and_entered ();
5377
5378         if (rs.empty()) {
5379                 return;
5380         }
5381
5382         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5383                 if ((*i)->region()->position() < start) {
5384                         start = (*i)->region()->position();
5385                 }
5386                 if ((*i)->region()->last_frame() + 1 > end) {
5387                         end = (*i)->region()->last_frame() + 1;
5388                 }
5389         }
5390
5391         set_punch_range (start, end, _("set punch range from region"));
5392 }
5393
5394 void
5395 Editor::pitch_shift_region ()
5396 {
5397         RegionSelection rs = get_regions_from_selection_and_entered ();
5398
5399         RegionSelection audio_rs;
5400         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5401                 if (dynamic_cast<AudioRegionView*> (*i)) {
5402                         audio_rs.push_back (*i);
5403                 }
5404         }
5405
5406         if (audio_rs.empty()) {
5407                 return;
5408         }
5409
5410         pitch_shift (audio_rs, 1.2);
5411 }
5412
5413 void
5414 Editor::transpose_region ()
5415 {
5416         RegionSelection rs = get_regions_from_selection_and_entered ();
5417
5418         list<MidiRegionView*> midi_region_views;
5419         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
5420                 MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*i);
5421                 if (mrv) {
5422                         midi_region_views.push_back (mrv);
5423                 }
5424         }
5425
5426         TransposeDialog d;
5427         int const r = d.run ();
5428         if (r != RESPONSE_ACCEPT) {
5429                 return;
5430         }
5431
5432         for (list<MidiRegionView*>::iterator i = midi_region_views.begin(); i != midi_region_views.end(); ++i) {
5433                 (*i)->midi_region()->transpose (d.semitones ());
5434         }
5435 }
5436
5437 void
5438 Editor::set_tempo_from_region ()
5439 {
5440         RegionSelection rs = get_regions_from_selection_and_entered ();
5441
5442         if (!_session || rs.empty()) {
5443                 return;
5444         }
5445
5446         RegionView* rv = rs.front();
5447
5448         define_one_bar (rv->region()->position(), rv->region()->last_frame() + 1);
5449 }
5450
5451 void
5452 Editor::use_range_as_bar ()
5453 {
5454         framepos_t start, end;
5455         if (get_edit_op_range (start, end)) {
5456                 define_one_bar (start, end);
5457         }
5458 }
5459
5460 void
5461 Editor::define_one_bar (framepos_t start, framepos_t end)
5462 {
5463         framepos_t length = end - start;
5464
5465         const Meter& m (_session->tempo_map().meter_at (start));
5466
5467         /* length = 1 bar */
5468
5469         /* now we want frames per beat.
5470            we have frames per bar, and beats per bar, so ...
5471         */
5472
5473         double frames_per_beat = length / m.beats_per_bar();
5474
5475         /* beats per minute = */
5476
5477         double beats_per_minute = (_session->frame_rate() * 60.0) / frames_per_beat;
5478
5479         /* now decide whether to:
5480
5481             (a) set global tempo
5482             (b) add a new tempo marker
5483
5484         */
5485
5486         const TempoSection& t (_session->tempo_map().tempo_section_at (start));
5487
5488         bool do_global = false;
5489
5490         if ((_session->tempo_map().n_tempos() == 1) && (_session->tempo_map().n_meters() == 1)) {
5491
5492                 /* only 1 tempo & 1 meter: ask if the user wants to set the tempo
5493                    at the start, or create a new marker
5494                 */
5495
5496                 vector<string> options;
5497                 options.push_back (_("Cancel"));
5498                 options.push_back (_("Add new marker"));
5499                 options.push_back (_("Set global tempo"));
5500
5501                 Choice c (
5502                         _("Define one bar"),
5503                         _("Do you want to set the global tempo or add a new tempo marker?"),
5504                         options
5505                         );
5506
5507                 c.set_default_response (2);
5508
5509                 switch (c.run()) {
5510                 case 0:
5511                         return;
5512
5513                 case 2:
5514                         do_global = true;
5515                         break;
5516
5517                 default:
5518                         do_global = false;
5519                 }
5520
5521         } else {
5522
5523                 /* more than 1 tempo and/or meter section already, go ahead do the "usual":
5524                    if the marker is at the region starter, change it, otherwise add
5525                    a new tempo marker
5526                 */
5527         }
5528
5529         begin_reversible_command (_("set tempo from region"));
5530         XMLNode& before (_session->tempo_map().get_state());
5531
5532         if (do_global) {
5533                 _session->tempo_map().change_initial_tempo (beats_per_minute, t.note_type());
5534         } else if (t.frame() == start) {
5535                 _session->tempo_map().change_existing_tempo_at (start, beats_per_minute, t.note_type());
5536         } else {
5537                 _session->tempo_map().add_tempo (Tempo (beats_per_minute, t.note_type()), start);
5538         }
5539
5540         XMLNode& after (_session->tempo_map().get_state());
5541
5542         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
5543         commit_reversible_command ();
5544 }
5545
5546 void
5547 Editor::split_region_at_transients ()
5548 {
5549         AnalysisFeatureList positions;
5550
5551         RegionSelection rs = get_regions_from_selection_and_entered ();
5552
5553         if (!_session || rs.empty()) {
5554                 return;
5555         }
5556
5557         _session->begin_reversible_command (_("split regions"));
5558
5559         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ) {
5560
5561                 RegionSelection::iterator tmp;
5562
5563                 tmp = i;
5564                 ++tmp;
5565
5566                 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i)->region());
5567
5568                 if (ar && (ar->get_transients (positions) == 0)) {
5569                         split_region_at_points ((*i)->region(), positions, true);
5570                         positions.clear ();
5571                 }
5572
5573                 i = tmp;
5574         }
5575
5576         _session->commit_reversible_command ();
5577
5578 }
5579
5580 void
5581 Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList& positions, bool can_ferret, bool select_new)
5582 {
5583         bool use_rhythmic_rodent = false;
5584
5585         boost::shared_ptr<Playlist> pl = r->playlist();
5586
5587         list<boost::shared_ptr<Region> > new_regions;
5588
5589         if (!pl) {
5590                 return;
5591         }
5592
5593         if (positions.empty()) {
5594                 return;
5595         }
5596
5597
5598         if (positions.size() > 20 && can_ferret) {
5599                 std::string msgstr = string_compose (_("You are about to split\n%1\ninto %2 pieces.\nThis could take a long time."), r->name(), positions.size() + 1);
5600                 MessageDialog msg (msgstr,
5601                                    false,
5602                                    Gtk::MESSAGE_INFO,
5603                                    Gtk::BUTTONS_OK_CANCEL);
5604
5605                 if (can_ferret) {
5606                         msg.add_button (_("Call for the Ferret!"), RESPONSE_APPLY);
5607                         msg.set_secondary_text (_("Press OK to continue with this split operation\nor ask the Ferret dialog to tune the analysis"));
5608                 } else {
5609                         msg.set_secondary_text (_("Press OK to continue with this split operation"));
5610                 }
5611
5612                 msg.set_title (_("Excessive split?"));
5613                 msg.present ();
5614
5615                 int response = msg.run();
5616                 msg.hide ();
5617
5618                 switch (response) {
5619                 case RESPONSE_OK:
5620                         break;
5621                 case RESPONSE_APPLY:
5622                         use_rhythmic_rodent = true;
5623                         break;
5624                 default:
5625                         return;
5626                 }
5627         }
5628
5629         if (use_rhythmic_rodent) {
5630                 show_rhythm_ferret ();
5631                 return;
5632         }
5633
5634         AnalysisFeatureList::const_iterator x;
5635
5636         pl->clear_changes ();
5637
5638         x = positions.begin();
5639
5640         if (x == positions.end()) {
5641                 return;
5642         }
5643
5644         pl->freeze ();
5645         pl->remove_region (r);
5646
5647         framepos_t pos = 0;
5648
5649         while (x != positions.end()) {
5650
5651                 /* deal with positons that are out of scope of present region bounds */
5652                 if (*x <= 0 || *x > r->length()) {
5653                         ++x;
5654                         continue;
5655                 }
5656
5657                 /* file start = original start + how far we from the initial position ?
5658                  */
5659
5660                 framepos_t file_start = r->start() + pos;
5661
5662                 /* length = next position - current position
5663                  */
5664
5665                 framepos_t len = (*x) - pos;
5666
5667                 /* XXX we do we really want to allow even single-sample regions?
5668                    shouldn't we have some kind of lower limit on region size?
5669                 */
5670
5671                 if (len <= 0) {
5672                         break;
5673                 }
5674
5675                 string new_name;
5676
5677                 if (RegionFactory::region_name (new_name, r->name())) {
5678                         break;
5679                 }
5680
5681                 /* do NOT announce new regions 1 by one, just wait till they are all done */
5682
5683                 PropertyList plist;
5684
5685                 plist.add (ARDOUR::Properties::start, file_start);
5686                 plist.add (ARDOUR::Properties::length, len);
5687                 plist.add (ARDOUR::Properties::name, new_name);
5688                 plist.add (ARDOUR::Properties::layer, 0);
5689
5690                 boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
5691
5692                 pl->add_region (nr, r->position() + pos);
5693
5694                 if (select_new) {
5695                         new_regions.push_front(nr);
5696                 }
5697
5698                 pos += len;
5699                 ++x;
5700         }
5701
5702         string new_name;
5703
5704         RegionFactory::region_name (new_name, r->name());
5705
5706         /* Add the final region */
5707         PropertyList plist;
5708
5709         plist.add (ARDOUR::Properties::start, r->start() + pos);
5710         plist.add (ARDOUR::Properties::length, r->last_frame() - (r->position() + pos) + 1);
5711         plist.add (ARDOUR::Properties::name, new_name);
5712         plist.add (ARDOUR::Properties::layer, 0);
5713
5714         boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
5715         pl->add_region (nr, r->position() + pos);
5716
5717         if (select_new) {
5718                 new_regions.push_front(nr);
5719         }
5720
5721         pl->thaw ();
5722
5723         _session->add_command (new StatefulDiffCommand (pl));
5724
5725         if (select_new) {
5726
5727                 for (list<boost::shared_ptr<Region> >::iterator i = new_regions.begin(); i != new_regions.end(); ++i){
5728                         set_selected_regionview_from_region_list ((*i), Selection::Add);
5729                 }
5730         }
5731 }
5732
5733 void
5734 Editor::place_transient()
5735 {
5736         if (!_session) {
5737                 return;
5738         }
5739
5740         RegionSelection rs = get_regions_from_selection_and_edit_point ();
5741
5742         if (rs.empty()) {
5743                 return;
5744         }
5745
5746         framepos_t where = get_preferred_edit_position();
5747
5748         _session->begin_reversible_command (_("place transient"));
5749
5750         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
5751                 framepos_t position = (*r)->region()->position();
5752                 (*r)->region()->add_transient(where - position);
5753         }
5754
5755         _session->commit_reversible_command ();
5756 }
5757
5758 void
5759 Editor::remove_transient(ArdourCanvas::Item* item)
5760 {
5761         if (!_session) {
5762                 return;
5763         }
5764
5765         ArdourCanvas::Line* _line = reinterpret_cast<ArdourCanvas::Line*> (item);
5766         assert (_line);
5767
5768         AudioRegionView* _arv = reinterpret_cast<AudioRegionView*> (item->get_data ("regionview"));
5769         _arv->remove_transient (*(float*) _line->get_data ("position"));
5770 }
5771
5772 void
5773 Editor::snap_regions_to_grid ()
5774 {
5775         list <boost::shared_ptr<Playlist > > used_playlists;
5776
5777         RegionSelection rs = get_regions_from_selection_and_entered ();
5778
5779         if (!_session || rs.empty()) {
5780                 return;
5781         }
5782
5783         _session->begin_reversible_command (_("snap regions to grid"));
5784
5785         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
5786
5787                 boost::shared_ptr<Playlist> pl = (*r)->region()->playlist();
5788
5789                 if (!pl->frozen()) {
5790                         /* we haven't seen this playlist before */
5791
5792                         /* remember used playlists so we can thaw them later */
5793                         used_playlists.push_back(pl);
5794                         pl->freeze();
5795                 }
5796
5797                 framepos_t start_frame = (*r)->region()->first_frame ();
5798                 snap_to (start_frame);
5799                 (*r)->region()->set_position (start_frame);
5800         }
5801
5802         while (used_playlists.size() > 0) {
5803                 list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
5804                 (*i)->thaw();
5805                 used_playlists.pop_front();
5806         }
5807
5808         _session->commit_reversible_command ();
5809 }
5810
5811 void
5812 Editor::close_region_gaps ()
5813 {
5814         list <boost::shared_ptr<Playlist > > used_playlists;
5815
5816         RegionSelection rs = get_regions_from_selection_and_entered ();
5817
5818         if (!_session || rs.empty()) {
5819                 return;
5820         }
5821
5822         Dialog dialog (_("Close Region Gaps"));
5823
5824         Table table (2, 3);
5825         table.set_spacings (12);
5826         table.set_border_width (12);
5827         Label* l = manage (new Label (_("Crossfade length")));
5828         l->set_alignment (0, 0.5);
5829         table.attach (*l, 0, 1, 0, 1);
5830
5831         SpinButton spin_crossfade (1, 0);
5832         spin_crossfade.set_range (0, 15);
5833         spin_crossfade.set_increments (1, 1);
5834         spin_crossfade.set_value (5);
5835         table.attach (spin_crossfade, 1, 2, 0, 1);
5836
5837         table.attach (*manage (new Label (_("ms"))), 2, 3, 0, 1);
5838
5839         l = manage (new Label (_("Pull-back length")));
5840         l->set_alignment (0, 0.5);
5841         table.attach (*l, 0, 1, 1, 2);
5842
5843         SpinButton spin_pullback (1, 0);
5844         spin_pullback.set_range (0, 100);
5845         spin_pullback.set_increments (1, 1);
5846         spin_pullback.set_value(30);
5847         table.attach (spin_pullback, 1, 2, 1, 2);
5848
5849         table.attach (*manage (new Label (_("ms"))), 2, 3, 1, 2);
5850
5851         dialog.get_vbox()->pack_start (table);
5852         dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
5853         dialog.add_button (_("Ok"), RESPONSE_ACCEPT);
5854         dialog.show_all ();
5855
5856         if (dialog.run () == RESPONSE_CANCEL) {
5857                 return;
5858         }
5859
5860         framepos_t crossfade_len = spin_crossfade.get_value();
5861         framepos_t pull_back_frames = spin_pullback.get_value();
5862
5863         crossfade_len = lrintf (crossfade_len * _session->frame_rate()/1000);
5864         pull_back_frames = lrintf (pull_back_frames * _session->frame_rate()/1000);
5865
5866         /* Iterate over the region list and make adjacent regions overlap by crossfade_len_ms */
5867
5868         _session->begin_reversible_command (_("close region gaps"));
5869
5870         int idx = 0;
5871         boost::shared_ptr<Region> last_region;
5872
5873         rs.sort_by_position_and_track();
5874
5875         for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
5876
5877                 boost::shared_ptr<Playlist> pl = (*r)->region()->playlist();
5878
5879                 if (!pl->frozen()) {
5880                         /* we haven't seen this playlist before */
5881
5882                         /* remember used playlists so we can thaw them later */
5883                         used_playlists.push_back(pl);
5884                         pl->freeze();
5885                 }
5886
5887                 framepos_t position = (*r)->region()->position();
5888
5889                 if (idx == 0 || position < last_region->position()){
5890                         last_region = (*r)->region();
5891                         idx++;
5892                         continue;
5893                 }
5894
5895                 (*r)->region()->trim_front( (position - pull_back_frames));
5896                 last_region->trim_end( (position - pull_back_frames + crossfade_len));
5897
5898                 last_region = (*r)->region();
5899
5900                 idx++;
5901         }
5902
5903         while (used_playlists.size() > 0) {
5904                 list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
5905                 (*i)->thaw();
5906                 used_playlists.pop_front();
5907         }
5908
5909         _session->commit_reversible_command ();
5910 }
5911
5912 void
5913 Editor::tab_to_transient (bool forward)
5914 {
5915         AnalysisFeatureList positions;
5916
5917         RegionSelection rs = get_regions_from_selection_and_entered ();
5918
5919         if (!_session) {
5920                 return;
5921         }
5922
5923         framepos_t pos = _session->audible_frame ();
5924
5925         if (!selection->tracks.empty()) {
5926
5927                 for (TrackSelection::iterator t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
5928
5929                         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*t);
5930
5931                         if (rtv) {
5932                                 boost::shared_ptr<Track> tr = rtv->track();
5933                                 if (tr) {
5934                                         boost::shared_ptr<Playlist> pl = tr->playlist ();
5935                                         if (pl) {
5936                                                 framepos_t result = pl->find_next_transient (pos, forward ? 1 : -1);
5937
5938                                                 if (result >= 0) {
5939                                                         positions.push_back (result);
5940                                                 }
5941                                         }
5942                                 }
5943                         }
5944                 }
5945
5946         } else {
5947
5948                 if (rs.empty()) {
5949                         return;
5950                 }
5951
5952                 for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
5953                         (*r)->region()->get_transients (positions);
5954                 }
5955         }
5956
5957         TransientDetector::cleanup_transients (positions, _session->frame_rate(), 3.0);
5958
5959         if (forward) {
5960                 AnalysisFeatureList::iterator x;
5961
5962                 for (x = positions.begin(); x != positions.end(); ++x) {
5963                         if ((*x) > pos) {
5964                                 break;
5965                         }
5966                 }
5967
5968                 if (x != positions.end ()) {
5969                         _session->request_locate (*x);
5970                 }
5971
5972         } else {
5973                 AnalysisFeatureList::reverse_iterator x;
5974
5975                 for (x = positions.rbegin(); x != positions.rend(); ++x) {
5976                         if ((*x) < pos) {
5977                                 break;
5978                         }
5979                 }
5980
5981                 if (x != positions.rend ()) {
5982                         _session->request_locate (*x);
5983                 }
5984         }
5985 }
5986
5987 void
5988 Editor::playhead_forward_to_grid ()
5989 {
5990         if (!_session) return;
5991         framepos_t pos = playhead_cursor->current_frame;
5992         if (pos < max_framepos - 1) {
5993                 pos += 2;
5994                 snap_to_internal (pos, 1, false);
5995                 _session->request_locate (pos);
5996         }
5997 }
5998
5999
6000 void
6001 Editor::playhead_backward_to_grid ()
6002 {
6003         if (!_session) return;
6004         framepos_t pos = playhead_cursor->current_frame;
6005         if (pos > 2) {
6006                 pos -= 2;
6007                 snap_to_internal (pos, -1, false);
6008                 _session->request_locate (pos);
6009         }
6010 }
6011
6012 void
6013 Editor::set_track_height (Height h)
6014 {
6015         TrackSelection& ts (selection->tracks);
6016
6017         for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
6018                 (*x)->set_height_enum (h);
6019         }
6020 }
6021
6022 void
6023 Editor::toggle_tracks_active ()
6024 {
6025         TrackSelection& ts (selection->tracks);
6026         bool first = true;
6027         bool target = false;
6028
6029         if (ts.empty()) {
6030                 return;
6031         }
6032
6033         for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
6034                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*x);
6035
6036                 if (rtv) {
6037                         if (first) {
6038                                 target = !rtv->_route->active();
6039                                 first = false;
6040                         }
6041                         rtv->_route->set_active (target, this);
6042                 }
6043         }
6044 }
6045
6046 void
6047 Editor::remove_tracks ()
6048 {
6049         TrackSelection& ts (selection->tracks);
6050
6051         if (ts.empty()) {
6052                 return;
6053         }
6054
6055         vector<string> choices;
6056         string prompt;
6057         int ntracks = 0;
6058         int nbusses = 0;
6059         const char* trackstr;
6060         const char* busstr;
6061         vector<boost::shared_ptr<Route> > routes;
6062         bool special_bus = false;
6063
6064         for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
6065                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*x);
6066                 if (rtv) {
6067                         if (rtv->is_track()) {
6068                                 ntracks++;
6069                         } else {
6070                                 nbusses++;
6071                         }
6072                 }
6073                 routes.push_back (rtv->_route);
6074
6075                 if (rtv->route()->is_master() || rtv->route()->is_monitor()) {
6076                         special_bus = true;
6077                 }
6078         }
6079
6080         if (special_bus && !Config->get_allow_special_bus_removal()) {
6081                 MessageDialog msg (_("That would be bad news ...."),
6082                                    false,
6083                                    Gtk::MESSAGE_INFO,
6084                                    Gtk::BUTTONS_OK);
6085                 msg.set_secondary_text (string_compose (_(
6086                                                                 "Removing the master or monitor bus is such a bad idea\n\
6087 that %1 is not going to allow it.\n\
6088 \n\
6089 If you really want to do this sort of thing\n\
6090 edit your ardour.rc file to set the\n\
6091 \"allow-special-bus-removal\" option to be \"yes\""), PROGRAM_NAME));
6092
6093                 msg.present ();
6094                 msg.run ();
6095                 return;
6096         }
6097
6098         if (ntracks + nbusses == 0) {
6099                 return;
6100         }
6101
6102         if (ntracks > 1) {
6103                 trackstr = _("tracks");
6104         } else {
6105                 trackstr = _("track");
6106         }
6107
6108         if (nbusses > 1) {
6109                 busstr = _("busses");
6110         } else {
6111                 busstr = _("bus");
6112         }
6113
6114         if (ntracks) {
6115                 if (nbusses) {
6116                         prompt  = string_compose (_("Do you really want to remove %1 %2 and %3 %4?\n"
6117                                                     "(You may also lose the playlists associated with the %2)\n\n"
6118                                                     "This action cannot be undone, and the session file will be overwritten!"),
6119                                                   ntracks, trackstr, nbusses, busstr);
6120                 } else {
6121                         prompt  = string_compose (_("Do you really want to remove %1 %2?\n"
6122                                                     "(You may also lose the playlists associated with the %2)\n\n"
6123                                                     "This action cannot be undone, and the session file will be overwritten!"),
6124                                                   ntracks, trackstr);
6125                 }
6126         } else if (nbusses) {
6127                 prompt  = string_compose (_("Do you really want to remove %1 %2?\n\n"
6128                                             "This action cannot be undon, and the session file will be overwritten"),
6129                                           nbusses, busstr);
6130         }
6131
6132         choices.push_back (_("No, do nothing."));
6133         if (ntracks + nbusses > 1) {
6134                 choices.push_back (_("Yes, remove them."));
6135         } else {
6136                 choices.push_back (_("Yes, remove it."));
6137         }
6138
6139         string title;
6140         if (ntracks) {
6141                 title = string_compose (_("Remove %1"), trackstr);
6142         } else {
6143                 title = string_compose (_("Remove %1"), busstr);
6144         }
6145
6146         Choice prompter (title, prompt, choices);
6147
6148         if (prompter.run () != 1) {
6149                 return;
6150         }
6151
6152         for (vector<boost::shared_ptr<Route> >::iterator x = routes.begin(); x != routes.end(); ++x) {
6153                 _session->remove_route (*x);
6154         }
6155 }
6156
6157 void
6158 Editor::do_insert_time ()
6159 {
6160         if (selection->tracks.empty()) {
6161                 return;
6162         }
6163
6164         InsertTimeDialog d (*this);
6165         int response = d.run ();
6166
6167         if (response != RESPONSE_OK) {
6168                 return;
6169         }
6170
6171         if (d.distance() == 0) {
6172                 return;
6173         }
6174
6175         InsertTimeOption opt = d.intersected_region_action ();
6176
6177         insert_time (
6178                 get_preferred_edit_position(),
6179                 d.distance(),
6180                 opt,
6181                 d.all_playlists(),
6182                 d.move_glued(),
6183                 d.move_markers(),
6184                 d.move_glued_markers(),
6185                 d.move_locked_markers(),
6186                 d.move_tempos()
6187                 );
6188 }
6189
6190 void
6191 Editor::insert_time (
6192         framepos_t pos, framecnt_t frames, InsertTimeOption opt,
6193         bool all_playlists, bool ignore_music_glue, bool markers_too, bool glued_markers_too, bool locked_markers_too, bool tempo_too
6194         )
6195 {
6196         bool commit = false;
6197
6198         if (Config->get_edit_mode() == Lock) {
6199                 return;
6200         }
6201
6202         begin_reversible_command (_("insert time"));
6203
6204         for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
6205
6206                 /* regions */
6207
6208                 vector<boost::shared_ptr<Playlist> > pl;
6209                 if (all_playlists) {
6210                         RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
6211                         if (rtav) {
6212                                 pl = _session->playlists->playlists_for_track (rtav->track ());
6213                         }
6214                 } else {
6215                         if ((*x)->playlist ()) {
6216                                 pl.push_back ((*x)->playlist ());
6217                         }
6218                 }
6219
6220                 for (vector<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
6221
6222                         (*i)->clear_changes ();
6223                         (*i)->clear_owned_changes ();
6224
6225                         if (opt == SplitIntersected) {
6226                                 (*i)->split (pos);
6227                         }
6228
6229                         (*i)->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
6230
6231                         vector<Command*> cmds;
6232                         (*i)->rdiff (cmds);
6233                         _session->add_commands (cmds);
6234
6235                         _session->add_command (new StatefulDiffCommand (*i));
6236                         commit = true;
6237                 }
6238
6239                 /* automation */
6240                 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
6241                 if (rtav) {
6242                         rtav->route ()->shift (pos, frames);
6243                         commit = true;
6244                 }
6245         }
6246
6247         /* markers */
6248         if (markers_too) {
6249                 bool moved = false;
6250                 XMLNode& before (_session->locations()->get_state());
6251                 Locations::LocationList copy (_session->locations()->list());
6252
6253                 for (Locations::LocationList::iterator i = copy.begin(); i != copy.end(); ++i) {
6254
6255                         Locations::LocationList::const_iterator tmp;
6256
6257                         bool const was_locked = (*i)->locked ();
6258                         if (locked_markers_too) {
6259                                 (*i)->unlock ();
6260                         }
6261
6262                         if ((*i)->position_lock_style() == AudioTime || glued_markers_too) {
6263
6264                                 if ((*i)->start() >= pos) {
6265                                         (*i)->set_start ((*i)->start() + frames);
6266                                         if (!(*i)->is_mark()) {
6267                                                 (*i)->set_end ((*i)->end() + frames);
6268                                         }
6269                                         moved = true;
6270                                 }
6271
6272                         }
6273
6274                         if (was_locked) {
6275                                 (*i)->lock ();
6276                         }
6277                 }
6278
6279                 if (moved) {
6280                         XMLNode& after (_session->locations()->get_state());
6281                         _session->add_command (new MementoCommand<Locations>(*_session->locations(), &before, &after));
6282                 }
6283         }
6284
6285         if (tempo_too) {
6286                 _session->tempo_map().insert_time (pos, frames);
6287         }
6288
6289         if (commit) {
6290                 commit_reversible_command ();
6291         }
6292 }
6293
6294 void
6295 Editor::fit_selected_tracks ()
6296 {
6297         if (!selection->tracks.empty()) {
6298                 fit_tracks (selection->tracks);
6299         } else {
6300                 TrackViewList tvl;
6301
6302                 /* no selected tracks - use tracks with selected regions */
6303
6304                 if (!selection->regions.empty()) {
6305                         for (RegionSelection::iterator r = selection->regions.begin(); r != selection->regions.end(); ++r) {
6306                                 tvl.push_back (&(*r)->get_time_axis_view ());
6307                         }
6308
6309                         if (!tvl.empty()) {
6310                                 fit_tracks (tvl);
6311                         }
6312                 } else if (internal_editing()) {
6313                         /* no selected tracks, or regions, but in internal edit mode, so follow the mouse and use
6314                            the entered track
6315                         */
6316                         if (entered_track) {
6317                                 tvl.push_back (entered_track);
6318                                 fit_tracks (tvl);
6319                         }
6320                 }
6321         }
6322 }
6323
6324 void
6325 Editor::fit_tracks (TrackViewList & tracks)
6326 {
6327         if (tracks.empty()) {
6328                 return;
6329         }
6330
6331         uint32_t child_heights = 0;
6332         int visible_tracks = 0;
6333
6334         for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
6335
6336                 if (!(*t)->marked_for_display()) {
6337                         continue;
6338                 }
6339
6340                 child_heights += (*t)->effective_height() - (*t)->current_height();
6341                 ++visible_tracks;
6342         }
6343
6344         uint32_t h = (uint32_t) floor ((_canvas_height - child_heights - canvas_timebars_vsize) / visible_tracks);
6345         double first_y_pos = DBL_MAX;
6346
6347         if (h < TimeAxisView::preset_height (HeightSmall)) {
6348                 MessageDialog msg (*this, _("There are too many tracks to fit in the current window"));
6349                 /* too small to be displayed */
6350                 return;
6351         }
6352
6353         undo_visual_stack.push_back (current_visual_state());
6354
6355         /* build a list of all tracks, including children */
6356
6357         TrackViewList all;
6358         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
6359                 all.push_back (*i);
6360                 TimeAxisView::Children c = (*i)->get_child_list ();
6361                 for (TimeAxisView::Children::iterator j = c.begin(); j != c.end(); ++j) {
6362                         all.push_back (j->get());
6363                 }
6364         }
6365
6366         /* operate on all tracks, hide unselected ones that are in the middle of selected ones */
6367
6368         bool prev_was_selected = false;
6369         bool is_selected = tracks.contains (all.front());
6370         bool next_is_selected;
6371
6372         for (TrackViewList::iterator t = all.begin(); t != all.end(); ++t) {
6373
6374                 TrackViewList::iterator next;
6375
6376                 next = t;
6377                 ++next;
6378
6379                 if (next != all.end()) {
6380                         next_is_selected = tracks.contains (*next);
6381                 } else {
6382                         next_is_selected = false;
6383                 }
6384
6385                 if ((*t)->marked_for_display ()) {
6386                         if (is_selected) {
6387                                 (*t)->set_height (h);
6388                                 first_y_pos = std::min ((*t)->y_position (), first_y_pos);
6389                         } else {
6390                                 if (prev_was_selected && next_is_selected) {
6391                                         hide_track_in_display (*t);
6392                                 }
6393                         }
6394                 }
6395
6396                 prev_was_selected = is_selected;
6397                 is_selected = next_is_selected;
6398         }
6399
6400         /*
6401            set the controls_layout height now, because waiting for its size
6402            request signal handler will cause the vertical adjustment setting to fail
6403         */
6404
6405         controls_layout.property_height () = full_canvas_height - canvas_timebars_vsize;
6406         vertical_adjustment.set_value (first_y_pos);
6407
6408         redo_visual_stack.push_back (current_visual_state());
6409 }
6410
6411 void
6412 Editor::save_visual_state (uint32_t n)
6413 {
6414         while (visual_states.size() <= n) {
6415                 visual_states.push_back (0);
6416         }
6417
6418         delete visual_states[n];
6419
6420         visual_states[n] = current_visual_state (true);
6421         gdk_beep ();
6422 }
6423
6424 void
6425 Editor::goto_visual_state (uint32_t n)
6426 {
6427         if (visual_states.size() <= n) {
6428                 return;
6429         }
6430
6431         if (visual_states[n] == 0) {
6432                 return;
6433         }
6434
6435         use_visual_state (*visual_states[n]);
6436 }
6437
6438 void
6439 Editor::start_visual_state_op (uint32_t n)
6440 {
6441         if (visual_state_op_connection.empty()) {
6442                 visual_state_op_connection = Glib::signal_timeout().connect (sigc::bind (sigc::mem_fun (*this, &Editor::end_visual_state_op), n), 1000);
6443         }
6444 }
6445
6446 void
6447 Editor::cancel_visual_state_op (uint32_t n)
6448 {
6449         if (!visual_state_op_connection.empty()) {
6450                 visual_state_op_connection.disconnect();
6451                 goto_visual_state (n);
6452         }  else {
6453                 //we land here if called from the menu OR if end_visual_state_op has been called
6454                 //so check if we are already in visual state n
6455                 // XXX not yet checking it at all, but redoing does not hurt
6456                 goto_visual_state (n);
6457         }
6458 }
6459
6460 bool
6461 Editor::end_visual_state_op (uint32_t n)
6462 {
6463         visual_state_op_connection.disconnect();
6464         save_visual_state (n);
6465
6466         PopUp* pup = new PopUp (WIN_POS_MOUSE, 1000, true);
6467         char buf[32];
6468         snprintf (buf, sizeof (buf), _("Saved view %u"), n+1);
6469         pup->set_text (buf);
6470         pup->touch();
6471
6472         return false; // do not call again
6473 }
6474
6475 void
6476 Editor::toggle_region_mute ()
6477 {
6478         if (_ignore_region_action) {
6479                 return;
6480         }
6481
6482         RegionSelection rs = get_regions_from_selection_and_entered ();
6483
6484         if (rs.empty ()) {
6485                 return;
6486         }
6487
6488         if (rs.size() > 1) {
6489                 begin_reversible_command (_("mute regions"));
6490         } else {
6491                 begin_reversible_command (_("mute region"));
6492         }
6493
6494         for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
6495
6496                 (*i)->region()->playlist()->clear_changes ();
6497                 (*i)->region()->set_muted (!(*i)->region()->muted ());
6498                 _session->add_command (new StatefulDiffCommand ((*i)->region()->playlist()));
6499
6500         }
6501
6502         commit_reversible_command ();
6503 }
6504
6505 void
6506 Editor::combine_regions ()
6507 {
6508         /* foreach track with selected regions, take all selected regions
6509            and join them into a new region containing the subregions (as a
6510            playlist)
6511         */
6512
6513         typedef set<RouteTimeAxisView*> RTVS;
6514         RTVS tracks;
6515
6516         if (selection->regions.empty()) {
6517                 return;
6518         }
6519
6520         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
6521                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
6522
6523                 if (rtv) {
6524                         tracks.insert (rtv);
6525                 }
6526         }
6527
6528         begin_reversible_command (_("combine regions"));
6529
6530         vector<RegionView*> new_selection;
6531
6532         for (RTVS::iterator i = tracks.begin(); i != tracks.end(); ++i) {
6533                 RegionView* rv;
6534
6535                 if ((rv = (*i)->combine_regions ()) != 0) {
6536                         new_selection.push_back (rv);
6537                 }
6538         }
6539
6540         selection->clear_regions ();
6541         for (vector<RegionView*>::iterator i = new_selection.begin(); i != new_selection.end(); ++i) {
6542                 selection->add (*i);
6543         }
6544
6545         commit_reversible_command ();
6546 }
6547
6548 void
6549 Editor::uncombine_regions ()
6550 {
6551         typedef set<RouteTimeAxisView*> RTVS;
6552         RTVS tracks;
6553
6554         if (selection->regions.empty()) {
6555                 return;
6556         }
6557
6558         for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
6559                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
6560
6561                 if (rtv) {
6562                         tracks.insert (rtv);
6563                 }
6564         }
6565
6566         begin_reversible_command (_("uncombine regions"));
6567
6568         for (RTVS::iterator i = tracks.begin(); i != tracks.end(); ++i) {
6569                 (*i)->uncombine_regions ();
6570         }
6571
6572         commit_reversible_command ();
6573 }
6574