make click on empty canvas area clear selection; change zoom-ot-region to be a toggle...
[ardour.git] / gtk2_ardour / editor_ops.cc
index dda9d36b0e32dc38fa75ba48ceed049d2b09e84a..f3a493e1959f215e55bd361ddab4777c04ea6453 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <unistd.h>
 #include <pbd/basename.h>
 #include <pbd/pthread_utils.h>
 #include <pbd/memento_command.h>
+#include <pbd/whitespace.h>
 
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/choice.h>
+#include <gtkmm2ext/window_title.h>
 
 #include <ardour/audioengine.h>
 #include <ardour/session.h>
@@ -68,6 +69,7 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace sigc;
 using namespace Gtk;
+using namespace Gtkmm2ext;
 using namespace Editing;
 
 /***********************************************************************
@@ -90,17 +92,10 @@ Editor::redo (uint32_t n)
        }
 }
 
-int
-Editor::ensure_cursor (nframes_t *pos)
-{
-       *pos = edit_cursor->current_frame;
-       return 0;
-}
-
 void
 Editor::split_region ()
 {
-       split_region_at (edit_cursor->current_frame);
+       split_region_at (get_preferred_edit_position());
 }
 
 void
@@ -112,21 +107,52 @@ Editor::split_region_at (nframes_t where)
 void
 Editor::split_regions_at (nframes_t where, RegionSelection& regions)
 {
+       if (regions.empty()) {
+               return;
+       }
+
        begin_reversible_command (_("split"));
 
-       snap_to (where);
+       // if splitting a single region, and snap-to is using
+       // region boundaries, don't pay attention to them
+
+       if (regions.size() == 1) {
+               switch (snap_type) {
+               case SnapToRegionStart:
+               case SnapToRegionSync:
+               case SnapToRegionEnd:
+                       break;
+               default:
+                       snap_to (where);
+               }
+       } else {
+               snap_to (where);
+       }
+               
+
        for (RegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
 
                RegionSelection::iterator tmp;
+
+               /* XXX this test needs to be more complicated, to make sure we really
+                  have something to split.
+               */
                
+               if (!(*a)->region()->covers (where)) {
+                       ++a;
+                       continue;
+               }
+
                tmp = a;
                ++tmp;
 
                boost::shared_ptr<Playlist> pl = (*a)->region()->playlist();
 
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*a);
-               if (arv)
+
+               if (arv) {
                        _new_regionviews_show_envelope = arv->envelope_visible();
+               }
                
                if (pl) {
                         XMLNode &before = pl->get_state();
@@ -136,8 +162,8 @@ Editor::split_regions_at (nframes_t where, RegionSelection& regions)
                }
 
                a = tmp;
-    }
-
+       }
+       
        commit_reversible_command ();
        _new_regionviews_show_envelope = false;
 }
@@ -331,6 +357,17 @@ Editor::nudge_forward (bool next)
 
                commit_reversible_command ();
 
+               
+       } else if (!selection->markers.empty()) {
+
+               bool ignored;
+               Location* loc = find_location_from_marker (selection->markers.front(), ignored);
+
+               if (loc) {
+                       distance = get_nudge_distance (loc->start(), next_distance);
+                       loc->set_start (loc->start() + distance);
+               }
+               
        } else {
                distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
                session->request_locate (playhead_cursor->current_frame + distance);
@@ -460,9 +497,10 @@ void
 Editor::build_region_boundary_cache ()
 {
        nframes_t pos = 0;
-       RegionPoint point;
+       vector<RegionPoint> interesting_points;
        boost::shared_ptr<Region> r;
        TrackViewList tracks;
+       bool at_end = false;
 
        region_boundary_cache.clear ();
 
@@ -472,16 +510,17 @@ Editor::build_region_boundary_cache ()
        
        switch (snap_type) {
        case SnapToRegionStart:
-               point = Start;
+               interesting_points.push_back (Start);
                break;
        case SnapToRegionEnd:
-               point = End;
+               interesting_points.push_back (End);
                break;  
        case SnapToRegionSync:
-               point = SyncPoint;
+               interesting_points.push_back (SyncPoint);
                break;  
        case SnapToRegionBoundary:
-               point = Start;
+               interesting_points.push_back (Start);
+               interesting_points.push_back (End);
                break;  
        default:
                fatal << string_compose (_("build_region_boundary_cache called with snap_type = %1"), snap_type) << endmsg;
@@ -490,71 +529,84 @@ Editor::build_region_boundary_cache ()
        }
        
        TimeAxisView *ontrack = 0;
+       TrackViewList tlist;
 
-       while (pos < session->current_end_frame()) {
+       if (!selection->tracks.empty()) {
+               tlist = selection->tracks;
+       } else {
+               tlist = track_views;
+       }
 
-               if (!selection->tracks.empty()) {
+       while (pos < session->current_end_frame() && !at_end) {
 
-                       if ((r = find_next_region (pos, point, 1, selection->tracks, &ontrack)) == 0) {
-                               break;
-                       }
+               nframes_t rpos;
+               nframes_t lpos = max_frames;
 
-               } else if (clicked_trackview) {
+               for (vector<RegionPoint>::iterator p = interesting_points.begin(); p != interesting_points.end(); ++p) {
 
-                       TrackViewList t;
-                       t.push_back (clicked_trackview);
+                       if ((r = find_next_region (pos, *p, 1, tlist, &ontrack)) == 0) {
+                               if (*p == interesting_points.back()) {
+                                       at_end = true;
+                               }
+                               /* move to next point type */
+                               continue;
+                       }
 
-                       if ((r = find_next_region (pos, point, 1, t, &ontrack)) == 0) {
+                       switch (*p) {
+                       case Start:
+                               rpos = r->first_frame();
                                break;
-                       }
 
-               } else {
+                       case End:
+                               rpos = r->last_frame();
+                               break;  
 
-                       if ((r = find_next_region (pos, point, 1, track_views, &ontrack)) == 0) {
+                       case SyncPoint:
+                               rpos = r->adjust_to_sync (r->first_frame());
+                               break;
+
+                       default:
                                break;
                        }
-               }
+                       
+                       float speed = 1.0f;
+                       AudioTimeAxisView *atav;
+                       
+                       if (ontrack != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(ontrack)) != 0 ) {
+                               if (atav->get_diskstream() != 0) {
+                                       speed = atav->get_diskstream()->speed();
+                               }
+                       }
+                       
+                       rpos = track_frame_to_session_frame (rpos, speed);
 
-               nframes_t rpos;
-               
-               switch (snap_type) {
-               case SnapToRegionStart:
-                       rpos = r->first_frame();
-                       break;
-               case SnapToRegionEnd:
-                       rpos = r->last_frame();
-                       break;  
-               case SnapToRegionSync:
-                       rpos = r->adjust_to_sync (r->first_frame());
-                       break;
+                       if (rpos < lpos) {
+                               lpos = rpos;
+                       }
 
-               case SnapToRegionBoundary:
-                       rpos = r->last_frame();
-                       break;  
-               default:
-                       break;
-               }
-               
-               float speed = 1.0f;
-               AudioTimeAxisView *atav;
+                       /* prevent duplicates, but we don't use set<> because we want to be able
+                          to sort later.
+                       */
 
-               if ( ontrack != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(ontrack)) != 0 ) {
-                       if (atav->get_diskstream() != 0) {
-                               speed = atav->get_diskstream()->speed();
+                       vector<nframes_t>::iterator ri; 
+                       
+                       for (ri = region_boundary_cache.begin(); ri != region_boundary_cache.end(); ++ri) {
+                               if (*ri == rpos) {
+                                       break;
+                               }
                        }
-               }
 
-               rpos = track_frame_to_session_frame(rpos, speed);
-
-               if (region_boundary_cache.empty() || rpos != region_boundary_cache.back()) {
-                       if (snap_type == SnapToRegionBoundary) {
-                               region_boundary_cache.push_back (r->first_frame());
+                       if (ri == region_boundary_cache.end()) {
+                               region_boundary_cache.push_back (rpos);
                        }
-                       region_boundary_cache.push_back (rpos);
                }
 
-               pos = rpos + 1;
+               pos = lpos + 1;
        }
+
+       /* finally sort to be sure that the order is correct */
+
+       sort (region_boundary_cache.begin(), region_boundary_cache.end());
 }
 
 boost::shared_ptr<Region>
@@ -599,6 +651,7 @@ Editor::find_next_region (nframes_t frame, RegionPoint point, int32_t dir, Track
                        rpos = r->adjust_to_sync (r->first_frame());
                        break;
                }
+
                // rpos is a "track frame", converting it to "session frame"
                rpos = track_frame_to_session_frame(rpos, track_speed);
 
@@ -619,6 +672,85 @@ Editor::find_next_region (nframes_t frame, RegionPoint point, int32_t dir, Track
        return ret;
 }
 
+nframes64_t
+Editor::find_next_region_boundary (nframes64_t pos, int32_t dir, const TrackViewList& tracks)
+{
+       nframes64_t distance = max_frames;
+       nframes64_t current_nearest = -1;
+
+       for (TrackViewList::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
+               nframes64_t contender;
+               nframes64_t d;
+
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
+
+               if (!rtv) {
+                       continue;
+               }
+
+               if ((contender = rtv->find_next_region_boundary (pos, dir)) < 0) {
+                       continue;
+               }
+
+               d = ::llabs (pos - contender);
+
+               if (d < distance) {
+                       current_nearest = contender;
+                       distance = d;
+               }
+       }
+       
+       return current_nearest;
+}
+
+void
+Editor::cursor_to_region_boundary (Cursor* cursor, int32_t dir)
+{
+       nframes64_t pos = cursor->current_frame;
+       nframes64_t target;
+
+       if (!session) {
+               return;
+       }
+
+       // so we don't find the current region again..
+       if (dir > 0 || pos > 0) {
+               pos += dir;
+       }
+
+       if (!selection->tracks.empty()) {
+               
+               target = find_next_region_boundary (pos, dir, selection->tracks);
+               
+       } else {
+               
+               target = find_next_region_boundary (pos, dir, track_views);
+       }
+       
+       if (target < 0) {
+               return;
+       }
+
+
+       if (cursor == playhead_cursor) {
+               session->request_locate (target);
+       } else {
+               cursor->set_position (target);
+       }
+}
+
+void
+Editor::cursor_to_next_region_boundary (Cursor* cursor)
+{
+       cursor_to_region_boundary (cursor, 1);
+}
+
+void
+Editor::cursor_to_previous_region_boundary (Cursor* cursor)
+{
+       cursor_to_region_boundary (cursor, -1);
+}
+
 void
 Editor::cursor_to_region_point (Cursor* cursor, RegionPoint point, int32_t dir)
 {
@@ -757,161 +889,400 @@ Editor::cursor_to_selection_end (Cursor *cursor)
 }
 
 void
-Editor::scroll_playhead (bool forward)
+Editor::selected_marker_to_region_boundary (int32_t dir)
 {
-       nframes_t pos = playhead_cursor->current_frame;
-       nframes_t delta = (nframes_t) floor (current_page_frames() / 0.8);
-
-       if (forward) {
-               if (pos == max_frames) {
-                       return;
-               }
+       nframes64_t target;
+       Location* loc;
+       bool ignored;
 
-               if (pos < max_frames - delta) {
-                       pos += delta ;
-               } else {
-                       pos = max_frames;
-               } 
+       if (!session) {
+               return;
+       }
 
-       } else {
+       if (selection->markers.empty()) {
+               nframes64_t mouse;
+               bool ignored;
 
-               if (pos == 0) {
+               if (!mouse_frame (mouse, ignored)) {
                        return;
-               } 
-
-               if (pos > delta) {
-                       pos -= delta;
-               } else {
-                       pos = 0;
                }
+               
+               add_location_mark (mouse);
        }
 
-       session->request_locate (pos);
-}
+       if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
+               return;
+       }
 
-void
-Editor::playhead_backward ()
-{
-       nframes_t pos;
-       nframes_t cnt;
-       float prefix;
-       bool was_floating;
+       nframes64_t pos = loc->start();
 
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       cnt = (nframes_t) prefix;
-               }
+       // so we don't find the current region again..
+       if (dir > 0 || pos > 0) {
+               pos += dir;
        }
 
-       pos = playhead_cursor->current_frame;
-
-       if ((nframes_t) pos < cnt) {
-               pos = 0;
+       if (!selection->tracks.empty()) {
+               
+               target = find_next_region_boundary (pos, dir, selection->tracks);
+               
        } else {
-               pos -= cnt;
+               
+               target = find_next_region_boundary (pos, dir, track_views);
        }
        
-       /* XXX this is completely insane. with the current buffering
-          design, we'll force a complete track buffer flush and
-          reload, just to move 1 sample !!!
-       */
+       if (target < 0) {
+               return;
+       }
 
-       session->request_locate (pos);
+       loc->move_to (target);
 }
 
 void
-Editor::playhead_forward ()
+Editor::selected_marker_to_next_region_boundary ()
 {
-       nframes_t pos;
-       nframes_t cnt;
-       bool was_floating;
-       float prefix;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       cnt = (nframes_t) floor (prefix);
-               }
-       }
-
-       pos = playhead_cursor->current_frame;
-       
-       /* XXX this is completely insane. with the current buffering
-          design, we'll force a complete track buffer flush and
-          reload, just to move 1 sample !!!
-       */
-
-       session->request_locate (pos+cnt);
+       selected_marker_to_region_boundary (1);
 }
 
 void
-Editor::cursor_align (bool playhead_to_edit)
+Editor::selected_marker_to_previous_region_boundary ()
 {
-       if (playhead_to_edit) {
-               if (session) {
-                       session->request_locate (edit_cursor->current_frame);
-               }
-       } else {
-               edit_cursor->set_position (playhead_cursor->current_frame);
-       }
+       selected_marker_to_region_boundary (-1);
 }
 
 void
-Editor::edit_cursor_backward ()
+Editor::selected_marker_to_region_point (RegionPoint point, int32_t dir)
 {
+       boost::shared_ptr<Region> r;
        nframes_t pos;
-       nframes_t cnt;
-       float prefix;
-       bool was_floating;
+       Location* loc;
+       bool ignored;
 
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       cnt = (nframes_t) prefix;
-               }
+       if (!session || selection->markers.empty()) {
+               return;
        }
 
-       pos = edit_cursor->current_frame;
-
-       if ((nframes_t) pos < cnt) {
-               pos = 0;
-       } else {
-               pos -= cnt;
+       if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
+               return;
        }
-       
-       edit_cursor->set_position (pos);
-}
 
-void
-Editor::edit_cursor_forward ()
-{
-       nframes_t pos;
-       nframes_t cnt;
-       bool was_floating;
-       float prefix;
+       TimeAxisView *ontrack = 0;
 
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       cnt = (nframes_t) floor (prefix);
+       pos = loc->start();
+
+       // so we don't find the current region again..
+       if (dir>0 || pos>0)
+               pos+=dir;
+
+       if (!selection->tracks.empty()) {
+               
+               r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
+               
+       } else if (clicked_trackview) {
+               
+               TrackViewList t;
+               t.push_back (clicked_trackview);
+               
+               r = find_next_region (pos, point, dir, t, &ontrack);
+               
+       } else {
+               
+               r = find_next_region (pos, point, dir, track_views, &ontrack);
+       }
+
+       if (r == 0) {
+               return;
+       }
+       
+       switch (point){
+       case Start:
+               pos = r->first_frame ();
+               break;
+
+       case End:
+               pos = r->last_frame ();
+               break;
+
+       case SyncPoint:
+               pos = r->adjust_to_sync (r->first_frame());
+               break;  
+       }
+       
+       float speed = 1.0f;
+       AudioTimeAxisView *atav;
+
+       if ( ontrack != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(ontrack)) != 0 ) {
+               if (atav->get_diskstream() != 0) {
+                       speed = atav->get_diskstream()->speed();
+               }
+       }
+
+       pos = track_frame_to_session_frame(pos, speed);
+
+       loc->move_to (pos);
+}
+
+void
+Editor::selected_marker_to_next_region_point (RegionPoint point)
+{
+       selected_marker_to_region_point (point, 1);
+}
+
+void
+Editor::selected_marker_to_previous_region_point (RegionPoint point)
+{
+       selected_marker_to_region_point (point, -1);
+}
+
+void
+Editor::selected_marker_to_selection_start ()
+{
+       nframes_t pos = 0;
+       Location* loc;
+       bool ignored;
+
+       if (!session || selection->markers.empty()) {
+               return;
+       }
+
+       if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
+               return;
+       }
+
+       switch (mouse_mode) {
+       case MouseObject:
+               if (!selection->regions.empty()) {
+                       pos = selection->regions.start();
+               }
+               break;
+
+       case MouseRange:
+               if (!selection->time.empty()) {
+                       pos = selection->time.start ();
+               }
+               break;
+
+       default:
+               return;
+       }
+
+       loc->move_to (pos);
+}
+
+void
+Editor::selected_marker_to_selection_end ()
+{
+       nframes_t pos = 0;
+       Location* loc;
+       bool ignored;
+
+       if (!session || selection->markers.empty()) {
+               return;
+       }
+
+       if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
+               return;
+       }
+
+       switch (mouse_mode) {
+       case MouseObject:
+               if (!selection->regions.empty()) {
+                       pos = selection->regions.end_frame();
+               }
+               break;
+
+       case MouseRange:
+               if (!selection->time.empty()) {
+                       pos = selection->time.end_frame ();
+               }
+               break;
+
+       default:
+               return;
+       }
+
+       loc->move_to (pos);
+}
+
+void
+Editor::scroll_playhead (bool forward)
+{
+       nframes_t pos = playhead_cursor->current_frame;
+       nframes_t delta = (nframes_t) floor (current_page_frames() / 0.8);
+
+       if (forward) {
+               if (pos == max_frames) {
+                       return;
+               }
+
+               if (pos < max_frames - delta) {
+                       pos += delta ;
+               } else {
+                       pos = max_frames;
+               } 
+
+       } else {
+
+               if (pos == 0) {
+                       return;
+               } 
+
+               if (pos > delta) {
+                       pos -= delta;
+               } else {
+                       pos = 0;
+               }
+       }
+
+       session->request_locate (pos);
+}
+
+void
+Editor::playhead_backward ()
+{
+       nframes_t pos;
+       nframes_t cnt;
+       float prefix;
+       bool was_floating;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               if (was_floating) {
+                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
+               } else {
+                       cnt = (nframes_t) prefix;
+               }
+       }
+
+       pos = playhead_cursor->current_frame;
+
+       if ((nframes_t) pos < cnt) {
+               pos = 0;
+       } else {
+               pos -= cnt;
+       }
+       
+       /* XXX this is completely insane. with the current buffering
+          design, we'll force a complete track buffer flush and
+          reload, just to move 1 sample !!!
+       */
+
+       session->request_locate (pos);
+}
+
+void
+Editor::playhead_forward ()
+{
+       nframes_t pos;
+       nframes_t cnt;
+       bool was_floating;
+       float prefix;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               if (was_floating) {
+                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
+               } else {
+                       cnt = (nframes_t) floor (prefix);
+               }
+       }
+
+       pos = playhead_cursor->current_frame;
+       
+       /* XXX this is completely insane. with the current buffering
+          design, we'll force a complete track buffer flush and
+          reload, just to move 1 sample !!!
+       */
+
+       session->request_locate (pos+cnt);
+}
+
+void
+Editor::cursor_align (bool playhead_to_edit)
+{
+       if (!session) {
+               return;
+       }
+
+       if (playhead_to_edit) {
+
+               if (selection->markers.empty()) {
+                       return;
+               }
+               
+               session->request_locate (selection->markers.front()->position(), session->transport_rolling());
+       
+       } else {
+
+               /* move selected markers to playhead */
+               
+               for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
+                       bool ignored;
+                       
+                       Location* loc = find_location_from_marker (*i, ignored);
+                       
+                       if (loc->is_mark()) {
+                               loc->set_start (playhead_cursor->current_frame);
+                       } else {
+                               loc->set (playhead_cursor->current_frame,
+                                         playhead_cursor->current_frame + loc->length());
+                       }
+               }
+       }
+}
+
+void
+Editor::edit_cursor_backward ()
+{
+       nframes64_t pos;
+       nframes64_t cnt;
+       float prefix;
+       bool was_floating;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               if (was_floating) {
+                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
+               } else {
+                       cnt = (nframes_t) prefix;
+               }
+       }
+
+       if ((pos = get_preferred_edit_position()) < 0) {
+               return;
+       }
+
+       if (pos < cnt) {
+               pos = 0;
+       } else {
+               pos -= cnt;
+       }
+       
+       // EDIT CURSOR edit_cursor->set_position (pos);
+}
+
+void
+Editor::edit_cursor_forward ()
+{
+       //nframes_t pos;
+       nframes_t cnt;
+       bool was_floating;
+       float prefix;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               if (was_floating) {
+                       cnt = (nframes_t) floor (prefix * session->frame_rate ());
+               } else {
+                       cnt = (nframes_t) floor (prefix);
                }
        }
 
-       pos = edit_cursor->current_frame;
-       edit_cursor->set_position (pos+cnt);
+       // pos = edit_cursor->current_frame;
+       // EDIT CURSOR edit_cursor->set_position (pos+cnt);
 }
 
 void
@@ -1072,12 +1443,14 @@ Editor::temporal_zoom (gdouble fpu)
 {
        if (!session) return;
        
-       nframes_t current_page = current_page_frames();
-       nframes_t current_leftmost = leftmost_frame;
-       nframes_t current_rightmost;
-       nframes_t current_center;
-       nframes_t new_page;
-       nframes_t leftmost_after_zoom = 0;
+       nframes64_t current_page = current_page_frames();
+       nframes64_t current_leftmost = leftmost_frame;
+       nframes64_t current_rightmost;
+       nframes64_t current_center;
+       nframes64_t new_page;
+       nframes64_t leftmost_after_zoom = 0;
+       nframes64_t where;
+       bool in_track_canvas;
        double nfpu;
 
        nfpu = fpu;
@@ -1116,10 +1489,38 @@ Editor::temporal_zoom (gdouble fpu)
                }
                break;
 
+       case ZoomFocusMouse:
+               /* try to keep the mouse over the same point in the display */
+
+               if (!mouse_frame (where, in_track_canvas)) {
+                       /* use playhead instead */
+                       where = playhead_cursor->current_frame;
+
+                       if (where > new_page/2) {
+                               leftmost_after_zoom = where - (new_page/2);
+                       } else {
+                               leftmost_after_zoom = 0;
+                       }
+
+               } else {
+
+                       double l = - ((new_page * ((where - current_leftmost)/(double)current_page)) - where);
+
+                       if (l < 0) {
+                               leftmost_after_zoom = 0;
+                       } else if (l > max_frames) { 
+                               leftmost_after_zoom = max_frames - new_page;
+                       } else {
+                               leftmost_after_zoom = (nframes64_t) l;
+                       }
+               }
+
+               break;
+
        case ZoomFocusEdit:
-               /* try to keep the edit cursor in the center */
-               if (edit_cursor->current_frame > new_page/2) {
-                       leftmost_after_zoom = edit_cursor->current_frame - (new_page/2);
+               /* try to keep the edit point in the center */
+               if (get_preferred_edit_position() > new_page/2) {
+                       leftmost_after_zoom = get_preferred_edit_position() - (new_page/2);
                } else {
                        leftmost_after_zoom = 0;
                }
@@ -1133,10 +1534,73 @@ Editor::temporal_zoom (gdouble fpu)
 //     session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), current_leftmost, frames_per_unit));
 //     session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_after_zoom, nfpu));
 //     commit_reversible_command ();
+       
+       // cerr << "repos & zoom to " << leftmost_after_zoom << " @ " << nfpu << endl;
 
        reposition_and_zoom (leftmost_after_zoom, nfpu);
 }      
 
+void
+Editor::temporal_zoom_region ()
+{
+
+       nframes64_t start = max_frames;
+       nframes64_t end = 0;
+
+       ensure_entered_region_selected (true);
+
+       if (selection->regions.empty()) {
+               info << _("cannot set loop: no region selected") << endmsg;
+               return;
+       }
+
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               if ((*i)->region()->position() < start) {
+                       start = (*i)->region()->position();
+               }
+               if ((*i)->region()->last_frame() + 1 > end) {
+                       end = (*i)->region()->last_frame() + 1;
+               }
+       }
+
+       /* now comes an "interesting" hack ... make sure we leave a little space
+          at each end of the editor so that the zoom doesn't fit the region
+          precisely to the screen.
+       */
+
+       GdkScreen* screen = gdk_screen_get_default ();
+       gint pixwidth = gdk_screen_get_width (screen);
+       gint mmwidth = gdk_screen_get_width_mm (screen);
+       double pix_per_mm = (double) pixwidth/ (double) mmwidth;
+       double one_centimeter_in_pixels = pix_per_mm * 10.0;
+       nframes_t extra_samples = unit_to_frame (one_centimeter_in_pixels);
+       
+       if (start > extra_samples) {
+               start -= extra_samples;
+       } else {
+               start = 0;
+       } 
+
+       if (max_frames - extra_samples > end) {
+               end += extra_samples;
+       } else {
+               end = max_frames;
+       }
+
+       temporal_zoom_by_frame (start, end, "zoom to region");
+       zoomed_to_region = true;
+}
+
+void
+Editor::toggle_zoom_region ()
+{
+       if (zoomed_to_region) {
+               swap_visual_state ();
+       } else {
+               temporal_zoom_region ();
+       }
+}
+
 void
 Editor::temporal_zoom_selection ()
 {
@@ -1213,387 +1677,87 @@ Editor::temporal_zoom_to_frame (bool coarser, nframes_t frame)
                range_before /= 1.61803399;
        }
 
-       if (new_fpu == frames_per_unit) return;
-
-       nframes_t new_leftmost = frame - (nframes_t)range_before;
-
-       if (new_leftmost > frame) new_leftmost = 0;
-
-//     begin_reversible_command (_("zoom to frame"));
-//     session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
-//     session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
-//     commit_reversible_command ();
-
-       reposition_and_zoom (new_leftmost, new_fpu);
-}
-
-void
-Editor::add_location_from_selection ()
-{
-       string rangename;
-
-       if (selection->time.empty()) {
-               return;
-       }
-
-       if (session == 0 || clicked_trackview == 0) {
-               return;
-       }
-
-       nframes_t start = selection->time[clicked_selection].start;
-       nframes_t end = selection->time[clicked_selection].end;
-
-       session->locations()->next_available_name(rangename,"selection");
-       Location *location = new Location (start, end, rangename, Location::IsRangeMarker);
-
-       session->begin_reversible_command (_("add marker"));
-        XMLNode &before = session->locations()->get_state();
-       session->locations()->add (location, true);
-        XMLNode &after = session->locations()->get_state();
-       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
-       session->commit_reversible_command ();
-}
-
-void
-Editor::add_location_from_playhead_cursor ()
-{
-       string markername;
-
-       nframes_t where = session->audible_frame();
-       
-       session->locations()->next_available_name(markername,"mark");
-       Location *location = new Location (where, where, markername, Location::IsMark);
-       session->begin_reversible_command (_("add marker"));
-        XMLNode &before = session->locations()->get_state();
-       session->locations()->add (location, true);
-        XMLNode &after = session->locations()->get_state();
-       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
-       session->commit_reversible_command ();
-}
-
-void
-Editor::add_location_from_audio_region ()
-{
-       if (selection->regions.empty()) {
-               return;
-       }
-
-       RegionView* rv = *(selection->regions.begin());
-       boost::shared_ptr<Region> region = rv->region();
-       
-       Location *location = new Location (region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
-       session->begin_reversible_command (_("add marker"));
-        XMLNode &before = session->locations()->get_state();
-       session->locations()->add (location, true);
-        XMLNode &after = session->locations()->get_state();
-       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
-       session->commit_reversible_command ();
-}
-
-void
-Editor::select_all_in_track (Selection::Operation op)
-{
-       list<Selectable *> touched;
-
-       if (!clicked_trackview) {
-               return;
-       }
-       
-       clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
-
-       switch (op) {
-       case Selection::Toggle:
-               selection->add (touched);
-               break;
-       case Selection::Set:
-               selection->set (touched);
-               break;
-       case Selection::Extend:
-               /* not defined yet */
-               break;
-       case Selection::Add:
-               selection->add (touched);
-               break;
-       }
-}
-
-void
-Editor::select_all (Selection::Operation op)
-{
-       list<Selectable *> touched;
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
-       }
-       begin_reversible_command (_("select all"));
-       switch (op) {
-       case Selection::Add:
-       case Selection::Toggle:
-               selection->add (touched);
-               break;
-       case Selection::Set:
-               selection->set (touched);
-               break;
-       case Selection::Extend:
-               /* not defined yet */
-               break;
-       }
-       commit_reversible_command ();
-}
-
-void
-Editor::invert_selection_in_track ()
-{
-       list<Selectable *> touched;
-
-       if (!clicked_trackview) {
-               return;
-       }
-       
-       clicked_trackview->get_inverted_selectables (*selection, touched);
-       selection->set (touched);
-}
-
-void
-Editor::invert_selection ()
-{
-       list<Selectable *> touched;
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_inverted_selectables (*selection, touched);
-       }
-
-       selection->set (touched);
-}
-
-bool
-Editor::select_all_within (nframes_t start, nframes_t end, double top, double bot, Selection::Operation op)
-{
-       list<Selectable*> touched;
-       list<Selectable*>::size_type n = 0;
-
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-
-               n = touched.size();
-
-               (*iter)->get_selectables (start, end, top, bot, touched);
-
-               if (n != touched.size()) {
-                       selection->add (*iter);
-               }
-       }
-
-       begin_reversible_command (_("select all within"));
-       switch (op) {
-       case Selection::Add:
-       case Selection::Toggle:
-               selection->add (touched);
-               break;
-       case Selection::Set:
-               selection->set (touched);
-               break;
-       case Selection::Extend:
-               /* not defined yet */
-               break;
-       }
-
-       commit_reversible_command ();
-       return !touched.empty();
-}
-
-void
-Editor::set_selection_from_audio_region ()
-{
-       if (selection->regions.empty()) {
-               return;
-       }
-
-       RegionView* rv = *(selection->regions.begin());
-       boost::shared_ptr<Region> region = rv->region();
-       
-       begin_reversible_command (_("set selection from region"));
-       selection->set (0, region->position(), region->last_frame());
-       commit_reversible_command ();
-
-       set_mouse_mode (Editing::MouseRange, false);
-}
-
-void
-Editor::set_selection_from_punch()
-{
-       Location* location;
-
-       if ((location = session->locations()->auto_punch_location()) == 0)  {
-               return;
-       }
-
-       set_selection_from_range (*location);
-}
-
-void
-Editor::set_selection_from_loop()
-{
-       Location* location;
-
-       if ((location = session->locations()->auto_loop_location()) == 0)  {
-               return;
-       }
-       set_selection_from_range (*location);
-}
-
-void
-Editor::set_selection_from_range (Location& loc)
-{
-       begin_reversible_command (_("set selection from range"));
-       selection->set (0, loc.start(), loc.end());
-       commit_reversible_command ();
-
-       set_mouse_mode (Editing::MouseRange, false);
-}
-
-void
-Editor::select_all_selectables_using_time_selection ()
-{
-       list<Selectable *> touched;
-
-       if (selection->time.empty()) {
-               return;
-       }
-
-       nframes_t start = selection->time[clicked_selection].start;
-       nframes_t end = selection->time[clicked_selection].end;
-
-       if (end - start < 1)  {
-               return;
-       }
-
-       for (TrackViewList::iterator iter = selection->tracks.begin(); iter != selection->tracks.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (start, end - 1, 0, DBL_MAX, touched);
-       }
-
-       begin_reversible_command (_("select all from range"));
-       selection->set (touched);
-       commit_reversible_command ();
-}
-
+       if (new_fpu == frames_per_unit) return;
 
-void
-Editor::select_all_selectables_using_punch()
-{
-       Location* location = session->locations()->auto_punch_location();
-       list<Selectable *> touched;
+       nframes_t new_leftmost = frame - (nframes_t)range_before;
 
-       if (location == 0 || (location->end() - location->start() <= 1))  {
-               return;
-       }
+       if (new_leftmost > frame) new_leftmost = 0;
 
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
-       }
-       begin_reversible_command (_("select all from punch"));
-       selection->set (touched);
-       commit_reversible_command ();
+//     begin_reversible_command (_("zoom to frame"));
+//     session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
+//     session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
+//     commit_reversible_command ();
 
+       reposition_and_zoom (new_leftmost, new_fpu);
 }
 
 void
-Editor::select_all_selectables_using_loop()
+Editor::add_location_from_selection ()
 {
-       Location* location = session->locations()->auto_loop_location();
-       list<Selectable *> touched;
+       string rangename;
 
-       if (location == 0 || (location->end() - location->start() <= 1))  {
+       if (selection->time.empty()) {
                return;
        }
 
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (location->start(), location->end() - 1, 0, DBL_MAX, touched);
+       if (session == 0 || clicked_trackview == 0) {
+               return;
        }
-       begin_reversible_command (_("select all from loop"));
-       selection->set (touched);
-       commit_reversible_command ();
 
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
+
+       session->locations()->next_available_name(rangename,"selection");
+       Location *location = new Location (start, end, rangename, Location::IsRangeMarker);
+
+       session->begin_reversible_command (_("add marker"));
+        XMLNode &before = session->locations()->get_state();
+       session->locations()->add (location, true);
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
 }
 
 void
-Editor::select_all_selectables_using_cursor (Cursor *cursor, bool after)
+Editor::add_location_mark (nframes64_t where)
 {
-        nframes_t start;
-       nframes_t end;
-       list<Selectable *> touched;
+       string markername;
 
-       if (after) {
-               begin_reversible_command (_("select all after cursor"));
-               start = cursor->current_frame ;
-               end = session->current_end_frame();
-       } else {
-               if (cursor->current_frame > 0) {
-                       begin_reversible_command (_("select all before cursor"));
-                       start = 0;
-                       end = cursor->current_frame - 1;
-               } else {
-                       return;
-               }
-       }
+       select_new_marker = true;
 
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
-       }
-       selection->set (touched);
-       commit_reversible_command ();
+       session->locations()->next_available_name(markername,"mark");
+       Location *location = new Location (where, where, markername, Location::IsMark);
+       session->begin_reversible_command (_("add marker"));
+        XMLNode &before = session->locations()->get_state();
+       session->locations()->add (location, true);
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
 }
 
 void
-Editor::select_all_selectables_between_cursors (Cursor *cursor, Cursor *other_cursor)
+Editor::add_location_from_playhead_cursor ()
 {
-        nframes_t start;
-       nframes_t end;
-       list<Selectable *> touched;
-       bool  other_cursor_is_first = cursor->current_frame > other_cursor->current_frame;
+       add_location_mark (session->audible_frame());
+}
 
-       if (cursor->current_frame == other_cursor->current_frame) {
+void
+Editor::add_location_from_audio_region ()
+{
+       if (selection->regions.empty()) {
                return;
        }
 
-       begin_reversible_command (_("select all between cursors"));
-       if (other_cursor_is_first) {
-               start = other_cursor->current_frame;
-               end = cursor->current_frame - 1;
-               
-       } else {
-               start = cursor->current_frame;
-               end = other_cursor->current_frame - 1;
-       }
+       RegionView* rv = *(selection->regions.begin());
+       boost::shared_ptr<Region> region = rv->region();
        
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
-       }
-       selection->set (touched);
-       commit_reversible_command ();
+       Location *location = new Location (region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
+       session->begin_reversible_command (_("add marker"));
+        XMLNode &before = session->locations()->get_state();
+       session->locations()->add (location, true);
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
 }
 
 void
@@ -1813,19 +1977,13 @@ Editor::insert_region_list_drag (boost::shared_ptr<AudioRegion> region, int x, i
                return;
        }
        
-       cerr << "drop target playlist, UC  = " << playlist.use_count() << endl;
-
        snap_to (where);
        
        begin_reversible_command (_("insert dragged region"));
         XMLNode &before = playlist->get_state();
-       cerr << "pre add target playlist, UC  = " << playlist.use_count() << endl;
        playlist->add_region (RegionFactory::create (region), where, 1.0);
-       cerr << "post add target playlist, UC  = " << playlist.use_count() << endl;
        session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
        commit_reversible_command ();
-
-       cerr << "post drop target playlist, UC  = " << playlist.use_count() << endl;
 }
 
 void
@@ -1840,6 +1998,10 @@ Editor::insert_region_list_selection (float times)
                if ((tv = dynamic_cast<RouteTimeAxisView*>(selection->tracks.front())) == 0) {
                        return;
                }
+       } else if (entered_track != 0) {
+               if ((tv = dynamic_cast<RouteTimeAxisView*>(entered_track)) == 0) {
+                       return;
+               }
        } else {
                return;
        }
@@ -1866,7 +2028,7 @@ Editor::insert_region_list_selection (float times)
                
                begin_reversible_command (_("insert region"));
                XMLNode &before = playlist->get_state();
-               playlist->add_region ((RegionFactory::create (region)), edit_cursor->current_frame, times);
+               playlist->add_region ((RegionFactory::create (region)), get_preferred_edit_position(), times);
                session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
                commit_reversible_command ();
        } 
@@ -1889,6 +2051,30 @@ Editor::edit_envelope ()
 
 /* PLAYBACK */
 
+void
+Editor::transition_to_rolling (bool fwd)
+{
+       if (!session) {
+               return;
+       }
+
+       switch (Config->get_slave_source()) {
+       case None:
+       case JACK:
+               break;
+       default:
+               /* transport controlled by the master */
+               return;
+       }
+
+       if (session->is_auditioning()) {
+               session->cancel_audition ();
+               return;
+       }
+       
+       session->request_transport_speed (fwd ? 1.0f : -1.0f);
+}
+
 void
 Editor::toggle_playback (bool with_abort)
 {
@@ -1927,9 +2113,9 @@ Editor::play_from_start ()
 }
 
 void
-Editor::play_from_edit_cursor ()
+Editor::play_from_edit_point ()
 {
-       session->request_locate (edit_cursor->current_frame, true);
+       session->request_locate (get_preferred_edit_position(), true);
 }
 
 void
@@ -1942,16 +2128,6 @@ Editor::play_selection ()
        session->request_play_range (true);
 }
 
-void
-Editor::play_selected_region ()
-{
-       if (!selection->regions.empty()) {
-               RegionView *rv = *(selection->regions.begin());
-
-               session->request_bounded_roll (rv->region()->position(), rv->region()->last_frame());   
-       }
-}
-
 void
 Editor::loop_selected_region ()
 {
@@ -2035,55 +2211,54 @@ Editor::edit_region ()
 }
 
 void
-Editor::rename_region ()
+Editor::rename_region()
 {
-       Dialog dialog;
-       Entry  entry;
-       Button ok_button (_("OK"));
-       Button cancel_button (_("Cancel"));
-
        if (selection->regions.empty()) {
                return;
        }
 
-       dialog.set_title (_("ardour: rename region"));
-       dialog.set_name ("RegionRenameWindow");
-       dialog.set_size_request (300, -1);
-       dialog.set_position (Gtk::WIN_POS_MOUSE);
-       dialog.set_modal (true);
+       WindowTitle title (Glib::get_application_name());
+       title += _("Rename Region");
 
-       dialog.get_vbox()->set_border_width (10);
-       dialog.get_vbox()->pack_start (entry);
-       dialog.get_action_area()->pack_start (ok_button);
-       dialog.get_action_area()->pack_start (cancel_button);
+       ArdourDialog d (*this, title.get_string(), true, false);
+       Entry entry;
+       Label label (_("New name:"));
+       HBox hbox;
 
-       entry.set_name ("RegionNameDisplay");
-       ok_button.set_name ("EditorGTKButton");
-       cancel_button.set_name ("EditorGTKButton");
+       hbox.set_spacing (6);
+       hbox.pack_start (label, false, false);
+       hbox.pack_start (entry, true, true);
 
-       region_renamed = false;
+       d.get_vbox()->set_border_width (12);
+       d.get_vbox()->pack_start (hbox, false, false);
 
-       entry.signal_activate().connect (bind (mem_fun(*this, &Editor::rename_region_finished), true));
-       ok_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::rename_region_finished), true));
-       cancel_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::rename_region_finished), false));
+       d.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
+       d.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
 
-       /* recurse */
+       d.set_size_request (300, -1);
+       d.set_position (Gtk::WIN_POS_MOUSE);
 
-       dialog.show_all ();
-       Main::run ();
+       entry.set_text (selection->regions.front()->region()->name());
+       entry.select_region (0, -1);
 
-       if (region_renamed) {
-               (*selection->regions.begin())->region()->set_name (entry.get_text());
-               redisplay_regions ();
-       }
-}
+       entry.signal_activate().connect (bind (mem_fun (d, &Dialog::response), RESPONSE_OK));
+       
+       d.show_all ();
+       
+       entry.grab_focus();
 
-void
-Editor::rename_region_finished (bool status)
+       int ret = d.run();
 
-{
-       region_renamed = status;
-       Main::quit ();
+       d.hide ();
+
+       if (ret == RESPONSE_OK) {
+               std::string str = entry.get_text();
+               strip_whitespace_edges (str);
+               if (!str.empty()) {
+                       selection->regions.front()->region()->set_name (str);
+                       redisplay_regions ();
+               }
+       }
 }
 
 void
@@ -2106,12 +2281,27 @@ Editor::audition_playlist_region_via_route (boost::shared_ptr<Region> region, Ro
 }
 
 void
-Editor::audition_selected_region ()
+Editor::play_selected_region ()
 {
-       if (!selection->regions.empty()) {
-               RegionView* rv = *(selection->regions.begin());
-               session->audition_region (rv->region());
+       nframes64_t start = max_frames;
+       nframes64_t end = 0;
+
+       ensure_entered_region_selected (true);
+
+       if (selection->regions.empty()) {
+               return;
+       }
+
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               if ((*i)->region()->position() < start) {
+                       start = (*i)->region()->position();
+               }
+               if ((*i)->region()->last_frame() + 1 > end) {
+                       end = (*i)->region()->last_frame() + 1;
+               }
        }
+
+       session->request_bounded_roll (start, end);
 }
 
 void
@@ -2232,17 +2422,22 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<AudioRegion> >& n
 void
 Editor::split_multichannel_region ()
 {
-       vector<AudioRegion*> v;
-
-       AudioRegionView* clicked_arv = dynamic_cast<AudioRegionView*>(clicked_regionview);
-       
-       if (!clicked_arv || clicked_arv->audio_region()->n_channels() < 2) {
+       if (selection->regions.empty()) {
                return;
        }
 
-       clicked_arv->audio_region()->separate_by_channel (*session, v);
+       vector<boost::shared_ptr<AudioRegion> > v;
+
+       for (list<RegionView*>::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
+
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*x);
+               
+               if (!arv || arv->audio_region()->n_channels() < 2) {
+                       continue;
+               }
 
-       /* nothing else to do, really */
+               (arv)->audio_region()->separate_by_channel (*session, v);
+       }
 }
 
 void
@@ -2252,16 +2447,23 @@ Editor::new_region_from_selection ()
        cancel_selection ();
 }
 
-void
-Editor::separate_region_from_selection ()
+static void
+add_if_covered (RegionView* rv, const AudioRange* ar, RegionSelection* rs)
 {
-       bool doing_undo = false;
-
-       if (selection->time.empty()) {
-               return;
+       switch (rv->region()->coverage (ar->start, ar->end - 1)) {
+       case OverlapNone:
+               break;
+       default:
+               rs->push_back (rv);
        }
+}
 
+void
+Editor::separate_regions_between (const TimeSelection& ts)
+{
+       bool in_command = false;
        boost::shared_ptr<Playlist> playlist;
+       RegionSelection new_selection;
                
        sort_track_selection ();
 
@@ -2272,157 +2474,210 @@ Editor::separate_region_from_selection ()
                if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
 
                        if (atv->is_audio_track()) {
+
+                               /* no edits to destructive tracks */
+
+                               if (atv->audio_track()->audio_diskstream()->destructive()) {
+                                       continue;
+                               }
                                        
                                if ((playlist = atv->playlist()) != 0) {
-                                       if (!doing_undo) {
-                                               begin_reversible_command (_("separate"));
-                                               doing_undo = true;
-                                       }
+
+
                                         XMLNode *before;
-                                       if (doing_undo) 
-                                            before = &(playlist->get_state());
-                       
+                                       bool got_some;
+
+                                       before = &(playlist->get_state());
+                                       got_some = false;
+
                                        /* XXX need to consider musical time selections here at some point */
 
                                        double speed = atv->get_diskstream()->speed();
 
-                                       for (list<AudioRange>::iterator t = selection->time.begin(); t != selection->time.end(); ++t) {
+
+                                       for (list<AudioRange>::const_iterator t = ts.begin(); t != ts.end(); ++t) {
+
+                                               sigc::connection c = atv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
+                                               latest_regionviews.clear ();
+
                                                playlist->partition ((nframes_t)((*t).start * speed), (nframes_t)((*t).end * speed), true);
+
+                                               c.disconnect ();
+
+                                               if (!latest_regionviews.empty()) {
+                                                       
+                                                       got_some = true;
+
+                                                       atv->view()->foreach_regionview (bind (sigc::ptr_fun (add_if_covered), &(*t), &new_selection));
+                                                       
+                                                       if (!in_command) {
+                                                               begin_reversible_command (_("separate"));
+                                                               in_command = true;
+                                                       }
+                                                       
+                                                       session->add_command(new MementoCommand<Playlist>(*playlist, before, &playlist->get_state()));
+                                                       
+                                               } 
                                        }
 
-                                       if (doing_undo) 
-                                            session->add_command(new MementoCommand<Playlist>(*playlist, before, &playlist->get_state()));
+                                       if (!got_some) {
+                                               delete before;
+                                       }
                                }
                        }
                }
        }
 
-       if (doing_undo) commit_reversible_command ();
+       if (in_command) {
+               selection->set (new_selection);
+               set_mouse_mode (MouseObject);
+
+               commit_reversible_command ();
+       }
 }
 
 void
-Editor::separate_regions_using_location (Location& loc)
+Editor::separate_region_from_selection ()
 {
-       bool doing_undo = false;
-
-       if (loc.is_mark()) {
-               return;
-       }
-
-       boost::shared_ptr<Playlist> playlist;
-
-       /* XXX i'm unsure as to whether this should operate on selected tracks only 
-          or the entire enchillada. uncomment the below line to correct the behaviour 
-          (currently set for all tracks)
+       /* preferentially use *all* ranges in the time selection if we're in range mode
+          to allow discontiguous operation, since get_edit_op_range() currently
+          returns a single range.
        */
+       if (mouse_mode == MouseRange && !selection->time.empty()) {
 
-       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {    
-       //for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-
-               AudioTimeAxisView* atv;
+               separate_regions_between (selection->time);
 
-               if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
+       } else {
 
-                       if (atv->is_audio_track()) {
-                                       
-                               if ((playlist = atv->playlist()) != 0) {
-                                        XMLNode *before;
-                                       if (!doing_undo) {
-                                               begin_reversible_command (_("separate"));
-                                               doing_undo = true;
-                                       }
-                                       if (doing_undo) 
-                                            before = &(playlist->get_state());
-                                            
+               nframes64_t start;
+               nframes64_t end;
+               
+               if (get_edit_op_range (start, end)) {
                        
-                                       /* XXX need to consider musical time selections here at some point */
-
-                                       double speed = atv->get_diskstream()->speed();
+                       AudioRange ar (start, end, 1);
+                       TimeSelection ts;
+                       ts.push_back (ar);
 
+                       /* force track selection */
 
-                                       playlist->partition ((nframes_t)(loc.start() * speed), (nframes_t)(loc.end() * speed), true);
-                                       if (doing_undo) 
-                                            session->add_command(new MementoCommand<Playlist>(*playlist, before, &playlist->get_state()));
-                               }
-                       }
+                       ensure_entered_region_selected ();
+                       
+                       separate_regions_between (ts);
                }
        }
-
-       if (doing_undo) commit_reversible_command ();
 }
 
 void
-Editor::crop_region_to_selection ()
+Editor::separate_regions_using_location (Location& loc)
 {
-       if (selection->time.empty()) {
+       if (loc.is_mark()) {
                return;
        }
 
-       vector<boost::shared_ptr<Playlist> > playlists;
-       boost::shared_ptr<Playlist> playlist;
+       AudioRange ar (loc.start(), loc.end(), 1);
+       TimeSelection ts;
 
-       if (clicked_trackview != 0) {
+       ts.push_back (ar);
 
-               if ((playlist = clicked_trackview->playlist()) == 0) {
-                       return;
-               }
+       separate_regions_between (ts);
+}
 
-               playlists.push_back (playlist);
+void
+Editor::crop_region_to_selection ()
+{
+       ensure_entered_region_selected (true);
 
-       } else {
-               
-               sort_track_selection ();
+       if (!selection->time.empty()) {
 
-               for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               crop_region_to (selection->time.start(), selection->time.end_frame());
 
-                       AudioTimeAxisView* atv;
+       } else {
 
-                       if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
+               nframes64_t start;
+               nframes64_t end;
 
-                               if (atv->is_audio_track()) {
-                                       
-                                       if ((playlist = atv->playlist()) != 0) {
-                                               playlists.push_back (playlist);
-                                       }
-                               }
-                       }
+               if (get_edit_op_range (start, end)) {
+                       crop_region_to (start, end);
                }
        }
+               
+}              
 
-       if (!playlists.empty()) {
-
-               nframes_t start;
-               nframes_t end;
-               nframes_t cnt;
-
-               begin_reversible_command (_("trim to selection"));
+void
+Editor::crop_region_to (nframes_t start, nframes_t end)
+{
+       vector<boost::shared_ptr<Playlist> > playlists;
+       boost::shared_ptr<Playlist> playlist;
+       TrackSelection* ts;
 
-               for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
-                       
-                       boost::shared_ptr<Region> region;
+       if (selection->tracks.empty()) {
+               ts = &track_views;
+       } else {
+               sort_track_selection ();
+               ts = &selection->tracks;
+       }
+       
+       for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
+               
+               AudioTimeAxisView* atv;
+               
+               if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
                        
-                       start = selection->time.start();
+                       if (atv->is_audio_track()) {
+                               
+                               /* no edits to destructive tracks */
 
-                       if ((region = (*i)->top_region_at(start)) == 0) {
-                               continue;
+                               if (atv->audio_track()->audio_diskstream()->destructive()) {
+                                       continue;
+                               }
+
+                               if ((playlist = atv->playlist()) != 0) {
+                                       playlists.push_back (playlist);
+                               }
                        }
-                       
-                       /* now adjust lengths to that we do the right thing
-                          if the selection extends beyond the region
-                       */
-                       
-                       start = max (start, region->position());
-                       end = min (selection->time.end_frame(), start + region->length() - 1);
-                       cnt = end - start + 1;
+               }
+       }
 
-                        XMLNode &before = (*i)->get_state();
-                       region->trim_to (start, cnt, this);
-                        XMLNode &after = (*i)->get_state();
-                       session->add_command (new MementoCommand<Playlist>(*(*i), &before, &after));
+       if (playlists.empty()) {
+               return;
+       }
+               
+       nframes_t the_start;
+       nframes_t the_end;
+       nframes_t cnt;
+       
+       begin_reversible_command (_("trim to selection"));
+       
+       for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+               
+               boost::shared_ptr<Region> region;
+       
+               the_start = start;
+       
+               if ((region = (*i)->top_region_at(the_start)) == 0) {
+                       continue;
                }
-
-               commit_reversible_command ();
+               
+               /* now adjust lengths to that we do the right thing
+                  if the selection extends beyond the region
+               */
+               
+               the_start = max (the_start, region->position());
+               if (max_frames - the_start < region->length()) {
+                       the_end = the_start + region->length() - 1;
+               } else {
+                       the_end = max_frames;
+               }
+               the_end = min (end, the_end);
+               cnt = the_end - the_start + 1;
+               
+               XMLNode &before = (*i)->get_state();
+               region->trim_to (the_start, cnt, this);
+               XMLNode &after = (*i)->get_state();
+               session->add_command (new MementoCommand<Playlist>(*(*i), &before, &after));
        }
+       
+       commit_reversible_command ();
 }              
 
 void
@@ -2517,40 +2772,40 @@ Editor::region_fill_selection ()
 }
 
 void
-Editor::set_a_regions_sync_position (boost::shared_ptr<Region> region, nframes_t position)
+Editor::set_region_sync_from_edit_point ()
 {
-
-       if (!region->covers (position)) {
-         error << _("Programming error. that region doesn't cover that position") << __FILE__ << " +" << __LINE__ << endmsg;
-               return;
-       }
-       begin_reversible_command (_("set region sync position"));
-        XMLNode &before = region->playlist()->get_state();
-       region->set_sync_position (position);
-        XMLNode &after = region->playlist()->get_state();
-       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
-       commit_reversible_command ();
+       nframes64_t where = get_preferred_edit_position ();
+       ensure_entered_region_selected (true);
+       set_sync_point (where, selection->regions);
 }
 
 void
-Editor::set_region_sync_from_edit_cursor ()
+Editor::set_sync_point (nframes64_t where, const RegionSelection& rs)
 {
-       if (clicked_regionview == 0) {
-               return;
-       }
+       bool in_command = false;
 
-       if (!clicked_regionview->region()->covers (edit_cursor->current_frame)) {
-               error << _("Place the edit cursor at the desired sync point") << endmsg;
-               return;
+       for (RegionSelection::const_iterator r = rs.begin(); r != rs.end(); ++r) {
+               
+               if (!(*r)->region()->covers (where)) {
+                       continue;
+               }
+
+               boost::shared_ptr<Region> region ((*r)->region());
+
+               if (!in_command) {
+                       begin_reversible_command (_("set sync point"));
+                       in_command = true;
+               }
+
+               XMLNode &before = region->playlist()->get_state();
+               region->set_sync_position (get_preferred_edit_position());
+               XMLNode &after = region->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
        }
 
-       boost::shared_ptr<Region> region (clicked_regionview->region());
-       begin_reversible_command (_("set sync from edit cursor"));
-        XMLNode &before = region->playlist()->get_state();
-       region->set_sync_position (edit_cursor->current_frame);
-        XMLNode &after = region->playlist()->get_state();
-       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
-       commit_reversible_command ();
+       if (in_command) {
+               commit_reversible_command ();
+       }
 }
 
 void
@@ -2586,13 +2841,33 @@ Editor::naturalize ()
 void
 Editor::align (RegionPoint what)
 {
-       align_selection (what, edit_cursor->current_frame);
+       ensure_entered_region_selected ();
+
+       nframes64_t where = get_preferred_edit_position();
+
+       if (!selection->regions.empty()) {
+               align_selection (what, where, selection->regions);
+       } else {
+
+               RegionSelection rs;
+               rs = get_regions_at (where, selection->tracks);
+               align_selection (what, where, rs);
+       }
 }
 
 void
 Editor::align_relative (RegionPoint what)
 {
-       align_selection_relative (what, edit_cursor->current_frame);
+       nframes64_t where = get_preferred_edit_position();
+
+       if (!selection->regions.empty()) {
+               align_selection_relative (what, where, selection->regions);
+       } else {
+
+               RegionSelection rs;
+               rs = get_regions_at (where, selection->tracks);
+               align_selection_relative (what, where, rs);
+       }
 }
 
 struct RegionSortByTime {
@@ -2602,9 +2877,9 @@ struct RegionSortByTime {
 };
 
 void
-Editor::align_selection_relative (RegionPoint point, nframes_t position)
+Editor::align_selection_relative (RegionPoint point, nframes_t position, const RegionSelection& rs)
 {
-       if (selection->regions.empty()) {
+       if (rs.empty()) {
                return;
        }
 
@@ -2613,7 +2888,7 @@ Editor::align_selection_relative (RegionPoint point, nframes_t position)
        int dir;
 
        list<RegionView*> sorted;
-       selection->regions.by_position (sorted);
+       rs.by_position (sorted);
        boost::shared_ptr<Region> r ((*sorted.begin())->region());
 
        switch (point) {
@@ -2640,7 +2915,7 @@ Editor::align_selection_relative (RegionPoint point, nframes_t position)
 
        begin_reversible_command (_("align selection (relative)"));
 
-       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+       for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
 
                boost::shared_ptr<Region> region ((*i)->region());
 
@@ -2661,15 +2936,15 @@ Editor::align_selection_relative (RegionPoint point, nframes_t position)
 }
 
 void
-Editor::align_selection (RegionPoint point, nframes_t position)
+Editor::align_selection (RegionPoint point, nframes_t position, const RegionSelection& rs)
 {
-       if (selection->regions.empty()) {
+       if (rs.empty()) {
                return;
        }
 
        begin_reversible_command (_("align selection"));
 
-       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+       for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
                align_region_internal ((*i)->region(), point, position);
        }
 
@@ -2710,54 +2985,157 @@ Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint poi
 }      
 
 void
-Editor::trim_region_to_edit_cursor ()
+Editor::trim_region_to_loop ()
 {
-       if (clicked_regionview == 0) {
+       Location* loc = session->locations()->auto_loop_location();
+       if (!loc) {
+               return;
+       }
+       trim_region_to_location (*loc, _("trim to loop"));
+}
+
+void
+Editor::trim_region_to_punch ()
+{
+       Location* loc = session->locations()->auto_punch_location();
+       if (!loc) {
                return;
        }
+       trim_region_to_location (*loc, _("trim to punch"));
+}
+
+void
+Editor::trim_region_to_location (const Location& loc, const char* str)
+{
+       ensure_entered_region_selected ();
 
-       boost::shared_ptr<Region> region (clicked_regionview->region());
+       RegionSelection& rs (get_regions_for_action ());
 
-       float speed = 1.0f;
-       AudioTimeAxisView *atav;
+       begin_reversible_command (str);
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!arv) {
+                       continue;
+               }
+
+               /* require region to span proposed trim */
+
+               switch (arv->region()->coverage (loc.start(), loc.end())) {
+               case OverlapInternal:
+                       break;
+               default:
+                       continue;
+               }
+                               
+               AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
+
+               if (!atav) {
+                       return;
+               }
+
+               float speed = 1.0;
+               nframes_t start;
+               nframes_t end;
 
-       if ( clicked_trackview != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(clicked_trackview)) != 0 ) {
                if (atav->get_diskstream() != 0) {
                        speed = atav->get_diskstream()->speed();
                }
-       }
 
-       begin_reversible_command (_("trim to edit"));
-        XMLNode &before = region->playlist()->get_state();
-       region->trim_end( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
-        XMLNode &after = region->playlist()->get_state();
-       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
+               start = session_frame_to_track_frame (loc.start(), speed);
+               end = session_frame_to_track_frame (loc.end(), speed);
+
+               XMLNode &before = arv->region()->playlist()->get_state();
+               arv->region()->trim_to (start, (end - start), this);
+               XMLNode &after = arv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*(arv->region()->playlist()), &before, &after));
+       }
+               
        commit_reversible_command ();
 }
 
 void
-Editor::trim_region_from_edit_cursor ()
+Editor::trim_region_to_edit_point ()
 {
-       if (clicked_regionview == 0) {
-               return;
-       }
+       RegionSelection& rs (get_regions_for_action ());
+       nframes64_t where = get_preferred_edit_position();
 
-       boost::shared_ptr<Region> region (clicked_regionview->region());
+       begin_reversible_command (_("trim region start to edit point"));
 
-       float speed = 1.0f;
-       AudioTimeAxisView *atav;
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!arv) {
+                       continue;
+               }
+
+               /* require region to cover trim */
+
+               if (!arv->region()->covers (where)) {
+                       continue;
+               }
+
+               AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
+
+               if (!atav) {
+                       return;
+               }
+
+               float speed = 1.0;
 
-       if ( clicked_trackview != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(clicked_trackview)) != 0 ) {
                if (atav->get_diskstream() != 0) {
                        speed = atav->get_diskstream()->speed();
                }
+
+               XMLNode &before = arv->region()->playlist()->get_state();
+               arv->region()->trim_end( session_frame_to_track_frame(where, speed), this);
+               XMLNode &after = arv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*(arv->region()->playlist()), &before, &after));
        }
+               
+       commit_reversible_command ();
+}
 
-       begin_reversible_command (_("trim to edit"));
-        XMLNode &before = region->playlist()->get_state();
-       region->trim_front ( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
-        XMLNode &after = region->playlist()->get_state();
-       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
+void
+Editor::trim_region_from_edit_point ()
+{
+       RegionSelection& rs (get_regions_for_action ());
+       nframes64_t where = get_preferred_edit_position();
+
+       begin_reversible_command (_("trim region end to edit point"));
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!arv) {
+                       continue;
+               }
+
+               /* require region to cover trim */
+
+               if (!arv->region()->covers (where)) {
+                       continue;
+               }
+
+               AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
+
+               if (!atav) {
+                       return;
+               }
+
+               float speed = 1.0;
+
+               if (atav->get_diskstream() != 0) {
+                       speed = atav->get_diskstream()->speed();
+               }
+
+               XMLNode &before = arv->region()->playlist()->get_state();
+               arv->region()->trim_front ( session_frame_to_track_frame(where, speed), this);
+               XMLNode &after = arv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*(arv->region()->playlist()), &before, &after));
+       }
+               
        commit_reversible_command ();
 }
 
@@ -2804,8 +3182,10 @@ Editor::freeze_route ()
        if (interthread_progress_window == 0) {
                build_interthread_progress_window ();
        }
-       
-       interthread_progress_window->set_title (_("ardour: freeze"));
+
+       WindowTitle title(Glib::get_application_name());
+       title += _("Freeze");
+       interthread_progress_window->set_title (title.get_string());
        interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
        interthread_progress_window->show_all ();
        interthread_progress_bar.set_fraction (0.0f);
@@ -2847,15 +3227,15 @@ Editor::bounce_range_selection ()
                return;
        }
 
-       TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
+       TrackSelection views = selection->tracks;
 
        nframes_t start = selection->time[clicked_selection].start;
        nframes_t end = selection->time[clicked_selection].end;
        nframes_t cnt = end - start + 1;
-       
+
        begin_reversible_command (_("bounce range"));
 
-       for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
+       for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
 
                AudioTimeAxisView* atv;
 
@@ -2874,7 +3254,7 @@ Editor::bounce_range_selection ()
                itt.done = false;
                itt.cancel = false;
                itt.progress = false;
-               
+
                 XMLNode &before = playlist->get_state();
                atv->audio_track()->bounce_range (start, cnt, itt);
                 XMLNode &after = playlist->get_state();
@@ -2882,8 +3262,6 @@ Editor::bounce_range_selection ()
        }
        
        commit_reversible_command ();
-       
-       delete views;
 }
 
 void
@@ -2919,14 +3297,29 @@ Editor::cut_copy (CutCopyOp op)
        
        cut_buffer->clear ();
 
+       if (entered_marker) {
+
+               /* cut/delete op while pointing at a marker */
+
+               bool ignored;
+               Location* loc = find_location_from_marker (entered_marker, ignored);
+
+               if (session && loc) {
+                       Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::really_remove_marker), loc));
+               }
+
+               return;
+       }
+
        switch (current_mouse_mode()) {
        case MouseObject: 
+               cerr << "cutting in object mode\n";
                if (!selection->regions.empty() || !selection->points.empty()) {
 
                        begin_reversible_command (opname + _(" objects"));
 
                        if (!selection->regions.empty()) {
-                               
+                               cerr << "have regions to cut" << endl;
                                cut_copy_regions (op);
                                
                                if (op == Cut) {
@@ -2943,21 +3336,34 @@ Editor::cut_copy (CutCopyOp op)
                        }
 
                        commit_reversible_command ();   
+                       break; // terminate case statement here
+               } 
+               cerr << "nope, now cutting time range" << endl;
+               if (!selection->time.empty()) {
+                       /* don't cause suprises */
+                       break;
                }
-               break;
+               // fall thru if there was nothing selected
                
        case MouseRange:
-               if (!selection->time.empty()) {
-
-                       begin_reversible_command (opname + _(" range"));
-                       cut_copy_ranges (op);
-                       commit_reversible_command ();
-
-                       if (op == Cut) {
-                               selection->clear_time ();
+               if (selection->time.empty()) {
+                       nframes64_t start, end;
+                       cerr << "no time selection, get edit op range" << endl;
+                       if (!get_edit_op_range (start, end)) {
+                               cerr << "no edit op range" << endl;
+                               return;
                        }
+                       selection->set ((TimeAxisView*) 0, start, end);
+               }
                        
+               begin_reversible_command (opname + _(" range"));
+               cut_copy_ranges (op);
+               commit_reversible_command ();
+               
+               if (op == Cut) {
+                       selection->clear_time ();
                }
+
                break;
                
        default:
@@ -3084,19 +3490,22 @@ Editor::cut_copy_regions (CutCopyOp op)
                }
                
                boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>((*x)->region());
+               boost::shared_ptr<Region> _xx;
                
                switch (op) {
                case Cut:
                        if (!ar) break;
                        
-                       npl->add_region (RegionFactory::create (ar), (*x)->region()->position() - first_position);
+                       _xx = RegionFactory::create ((*x)->region());
+                       npl->add_region (_xx, (*x)->region()->position() - first_position);
                        pl->remove_region (((*x)->region()));
                        break;
                        
                case Copy:
                        if (!ar) break;
-                       
-                       npl->add_region (RegionFactory::create (ar), (*x)->region()->position() - first_position);
+
+                       /* copy region before adding, so we're not putting same object into two different playlists */
+                       npl->add_region (RegionFactory::create ((*x)->region()), (*x)->region()->position() - first_position);
                        break;
                        
                case Clear:
@@ -3116,10 +3525,11 @@ Editor::cut_copy_regions (CutCopyOp op)
                foo.push_back ((*i).pl);
        }
        
+
        if (!foo.empty()) {
                cut_buffer->set (foo);
        }
-       
+
        for (set<PlaylistState, lt_playlist>::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
                (*pl).playlist->thaw ();
                session->add_command (new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
@@ -3129,7 +3539,15 @@ Editor::cut_copy_regions (CutCopyOp op)
 void
 Editor::cut_copy_ranges (CutCopyOp op)
 {
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+       TrackSelection* ts;
+
+       if (selection->tracks.empty()) {
+               ts = &track_views;
+       } else {
+               ts = &selection->tracks;
+       }
+
+       for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
                (*i)->cut_copy_clear (*selection, op);
        }
 }
@@ -3137,26 +3555,19 @@ Editor::cut_copy_ranges (CutCopyOp op)
 void
 Editor::paste (float times)
 {
-       paste_internal (edit_cursor->current_frame, times);
+       paste_internal (get_preferred_edit_position(), times);
 }
 
 void
 Editor::mouse_paste ()
 {
-       int x, y;
-       double wx, wy;
+       nframes64_t where;
+       bool ignored;
 
-       track_canvas.get_pointer (x, y);
-       track_canvas.window_to_world (x, y, wx, wy);
-       wx += horizontal_adjustment.get_value();
-       wy += vertical_adjustment.get_value();
+       if (!mouse_frame (where, ignored)) {
+               return;
+       }
 
-       GdkEvent event;
-       event.type = GDK_BUTTON_RELEASE;
-       event.button.x = wx;
-       event.button.y = wy;
-       
-       nframes_t where = event_frame (&event, 0, 0);
        snap_to (where);
        paste_internal (where, 1);
 }
@@ -3166,24 +3577,31 @@ Editor::paste_internal (nframes_t position, float times)
 {
        bool commit = false;
 
-       if (cut_buffer->empty() || selection->tracks.empty()) {
+       if (cut_buffer->empty()) {
                return;
        }
 
        if (position == max_frames) {
-               position = edit_cursor->current_frame;
+               position = get_preferred_edit_position();
        }
 
        begin_reversible_command (_("paste"));
 
+       TrackSelection ts;
        TrackSelection::iterator i;
        size_t nth;
 
        /* get everything in the correct order */
 
-       sort_track_selection ();
 
-       for (nth = 0, i = selection->tracks.begin(); i != selection->tracks.end(); ++i, ++nth) {
+       if (!selection->tracks.empty()) {
+               sort_track_selection ();
+               ts = selection->tracks;
+       } else if (entered_track) {
+               ts.push_back (entered_track);
+       }
+
+       for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
 
                /* undo/redo is handled by individual tracks */
 
@@ -3242,7 +3660,7 @@ Editor::paste_named_selection (float times)
                ++tmp;
 
                 XMLNode &before = apl->get_state();
-               apl->paste (*chunk, edit_cursor->current_frame, times);
+               apl->paste (*chunk, get_preferred_edit_position(), times);
                session->add_command(new MementoCommand<AudioPlaylist>(*apl, &before, &apl->get_state()));
 
                if (tmp != ns->playlists.end()) {
@@ -3257,8 +3675,9 @@ void
 Editor::duplicate_some_regions (RegionSelection& regions, float times)
 {
        boost::shared_ptr<Playlist> playlist; 
-       RegionSelection sel = regions; // clear (below) will clear the argument list
-               
+       RegionSelection sel = regions; // clear (below) may  clear the argument list if its the current region selection
+       RegionSelection foo;
+
        begin_reversible_command (_("duplicate region"));
 
        selection->clear_regions ();
@@ -3269,22 +3688,25 @@ Editor::duplicate_some_regions (RegionSelection& regions, float times)
 
                TimeAxisView& tv = (*i)->get_time_axis_view();
                AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (&tv);
+
+               latest_regionviews.clear ();
                sigc::connection c = atv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
                
                playlist = (*i)->region()->playlist();
                 XMLNode &before = playlist->get_state();
-               playlist->duplicate (r, r->last_frame(), times);
+               playlist->duplicate (r, r->last_frame() + 1, times);
                session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
 
                c.disconnect ();
-
-               if (latest_regionview) {
-                       selection->add (latest_regionview);
-               }
-       }
                
+               foo.insert (foo.end(), latest_regionviews.begin(), latest_regionviews.end());
+       }
 
        commit_reversible_command ();
+
+       if (!foo.empty()) {
+               selection->set (foo);
+       }
 }
 
 void
@@ -3345,16 +3767,14 @@ void
 Editor::center_playhead ()
 {
        float page = canvas_width * frames_per_unit;
-
        center_screen_internal (playhead_cursor->current_frame, page);
 }
 
 void
-Editor::center_edit_cursor ()
+Editor::center_edit_point ()
 {
        float page = canvas_width * frames_per_unit;
-
-       center_screen_internal (edit_cursor->current_frame, page);
+       center_screen_internal (get_preferred_edit_position(), page);
 }
 
 void
@@ -3369,15 +3789,15 @@ Editor::clear_playlist (boost::shared_ptr<Playlist> playlist)
 }
 
 void
-Editor::nudge_track (bool use_edit_cursor, bool forwards)
+Editor::nudge_track (bool use_edit, bool forwards)
 {
        boost::shared_ptr<Playlist> playlist; 
        nframes_t distance;
        nframes_t next_distance;
        nframes_t start;
 
-       if (use_edit_cursor) {
-               start = edit_cursor->current_frame;
+       if (use_edit) {
+               start = get_preferred_edit_position();
        } else {
                start = 0;
        }
@@ -3671,31 +4091,133 @@ Editor::toggle_region_lock ()
 }
 
 void
-Editor::toggle_region_mute ()
+Editor::toggle_region_mute ()
+{
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       bool x = region_mute_item->get_active();
+                       if (x != arv->audio_region()->muted()) {
+                               arv->audio_region()->set_muted (x);
+                       }
+               }
+       }
+}
+
+void
+Editor::toggle_region_opaque ()
+{
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       bool x = region_opaque_item->get_active();
+                       if (x != arv->audio_region()->opaque()) {
+                               arv->audio_region()->set_opaque (x);
+                       }
+               }
+       }
+}
+
+void
+Editor::set_fade_length (bool in)
+{
+       ensure_entered_region_selected (true);
+
+       /* we need a region to measure the offset from the start */
+
+       RegionView* rv;
+
+       if (entered_regionview) {
+               rv = entered_regionview;
+       } else if (!selection->regions.empty()) {
+               rv = selection->regions.front();
+       } else {
+               return;
+       }
+
+       nframes64_t pos = get_preferred_edit_position();
+       nframes_t len;
+       char* cmd;
+
+       if (in) {
+               if (pos <= rv->region()->position()) {
+                       /* can't do it */
+                       return;
+               }
+               len = pos - rv->region()->position();
+               cmd = _("set fade in length");
+       } else {
+               if (pos >= rv->region()->last_frame()) {
+                       /* can't do it */
+                       return;
+               }
+               len = rv->region()->last_frame() - pos;
+               cmd = _("set fade out length");
+       }
+
+       begin_reversible_command (cmd);
+
+       RegionSelection& rs (get_regions_for_action());
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!tmp) {
+                       return;
+               }
+
+               AutomationList& alist = tmp->audio_region()->fade_in();
+               XMLNode &before = alist.get_state();
+
+               if (in) {
+                       tmp->audio_region()->set_fade_in_length (len);
+               } else {
+                       tmp->audio_region()->set_fade_out_length (len);
+               }
+               
+               XMLNode &after = alist.get_state();
+               session->add_command(new MementoCommand<AutomationList>(alist, &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+
+void
+Editor::toggle_fade_active (bool in)
 {
-       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
-               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
-               if (arv) {
-                       bool x = region_mute_item->get_active();
-                       if (x != arv->audio_region()->muted()) {
-                               arv->audio_region()->set_muted (x);
-                       }
-               }
+       ensure_entered_region_selected (true);
+
+       if (selection->regions.empty()) {
+               return;
        }
-}
 
-void
-Editor::toggle_region_opaque ()
-{
-       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
-               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
-               if (arv) {
-                       bool x = region_opaque_item->get_active();
-                       if (x != arv->audio_region()->opaque()) {
-                               arv->audio_region()->set_opaque (x);
-                       }
+       const char* cmd = (in ? _("toggle fade in active") : _("toggle fade out active"));
+
+       begin_reversible_command (cmd);
+
+       for (RegionSelection::iterator x = selection->regions.begin(); x != selection->regions.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!tmp) {
+                       return;
                }
+
+               boost::shared_ptr<AudioRegion> region (tmp->audio_region());
+
+               XMLNode &before = region->get_state();
+
+               if (in) {
+                       region->set_fade_in_active (!region->fade_in_active());
+               } else {
+                       region->set_fade_out_active (!region->fade_out_active());
+               }
+
+               XMLNode &after = region->get_state();
+               session->add_command(new MementoCommand<AudioRegion>(*region.get(), &before, &after));
        }
+
+       commit_reversible_command ();
 }
 
 void
@@ -3793,3 +4315,340 @@ Editor::set_fade_out_active (bool yn)
        }
 }
 
+
+/** Update crossfade visibility after its configuration has been changed */
+void
+Editor::update_xfade_visibility ()
+{
+       _xfade_visibility = Config->get_xfades_visible ();
+       
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
+               if (v) {
+                       if (_xfade_visibility) {
+                               v->show_all_xfades ();
+                       } else {
+                               v->hide_all_xfades ();
+                       }
+               }
+       }
+}
+
+void
+Editor::set_edit_point ()
+{
+       nframes64_t where;
+       bool ignored;
+
+       if (!mouse_frame (where, ignored)) {
+               return;
+       }
+       
+       snap_to (where);
+
+       if (selection->markers.empty()) {
+               
+               mouse_add_new_marker (where);
+
+       } else {
+               bool ignored;
+
+               Location* loc = find_location_from_marker (selection->markers.front(), ignored);
+
+               if (loc) {
+                       loc->move_to (where);
+               }
+       }
+}
+
+void
+Editor::set_playhead_cursor ()
+{
+       if (entered_marker) {
+               session->request_locate (entered_marker->position(), session->transport_rolling());
+       } else {
+               nframes64_t where;
+               bool ignored;
+
+               if (!mouse_frame (where, ignored)) {
+                       return;
+               }
+                       
+               snap_to (where);
+               
+               if (session) {
+                       session->request_locate (where, session->transport_rolling());
+               }
+       }
+}
+
+void
+Editor::split ()
+{
+       ensure_entered_region_selected ();
+
+       nframes64_t where = get_preferred_edit_position();
+
+       if (!selection->regions.empty()) {
+               
+               split_regions_at (where, selection->regions);
+
+       } else {
+               
+               RegionSelection rs;
+               rs = get_regions_at (where, selection->tracks);
+               split_regions_at (where, rs);
+       }
+}
+
+void
+Editor::ensure_entered_track_selected (bool op_really_wants_one_track_if_none_are_selected)
+{
+       if (entered_track && mouse_mode == MouseObject) {
+               if (!selection->tracks.empty()) {
+                       if (!selection->selected (entered_track)) {
+                               selection->add (entered_track);
+                       }
+               } else {
+                       /* there is no selection, but this operation requires/prefers selected objects */
+
+                       if (op_really_wants_one_track_if_none_are_selected) {
+                               selection->set (entered_track);
+                       }
+               }
+       }
+}
+
+void
+Editor::ensure_entered_region_selected (bool op_really_wants_one_region_if_none_are_selected)
+{
+       if (entered_regionview && mouse_mode == MouseObject) {
+
+               /* heuristic:
+
+                  - if there is no existing selection, don't change it. the operation will thus apply to "all"
+
+                  - if there is an existing selection, but the entered regionview isn't in it, add it. this
+                      avoids key-mouse ops on unselected regions from interfering with an existing selection,
+                      but also means that the operation will apply to the pointed-at region.
+               */
+
+               if (!selection->regions.empty()) {
+                       if (find (selection->regions.begin(), selection->regions.end(), entered_regionview) != selection->regions.end()) {
+                               selection->add (entered_regionview);
+                       }
+               } else {
+                       /* there is no selection, but this operation requires/prefers selected objects */
+
+                       if (op_really_wants_one_region_if_none_are_selected) {
+                               selection->set (entered_regionview, false);
+                       }
+               }
+       }
+}
+
+void
+Editor::trim_region_front ()
+{
+       trim_region (true);
+}
+
+void
+Editor::trim_region_back ()
+{
+       trim_region (false);
+}
+
+void
+Editor::trim_region (bool front)
+{
+       nframes64_t where = get_preferred_edit_position();
+       RegionSelection& rs = get_regions_for_action ();
+
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (front ? _("trim front") : _("trim back"));
+
+       for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
+               if (!(*i)->region()->locked()) {
+                       boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
+                       XMLNode &before = pl->get_state();
+                       if (front) {
+                               (*i)->region()->trim_front (where, this);       
+                       } else {
+                               (*i)->region()->trim_end (where, this); 
+                       }
+                       XMLNode &after = pl->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
+               }
+       }
+       commit_reversible_command ();
+}
+
+struct EditorOrderRouteSorter {
+    bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
+           /* use of ">" forces the correct sort order */
+           return a->order_key ("editor") < b->order_key ("editor");
+    }
+};
+
+void
+Editor::select_next_route()
+{
+       if (selection->tracks.empty()) {
+               selection->set (track_views.front());
+               return;
+       }
+
+       TimeAxisView* current = selection->tracks.front();
+
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               if (*i == current) {
+                       ++i;
+                       if (i != track_views.end()) {
+                               selection->set (*i);
+                       } else {
+                               selection->set (*(track_views.begin()));
+                       }
+                       break;
+               }
+       }
+}
+
+void
+Editor::select_prev_route()
+{
+       if (selection->tracks.empty()) {
+               selection->set (track_views.front());
+               return;
+       }
+
+       TimeAxisView* current = selection->tracks.front();
+
+       for (TrackViewList::reverse_iterator i = track_views.rbegin(); i != track_views.rend(); ++i) {
+               if (*i == current) {
+                       ++i;
+                       if (i != track_views.rend()) {
+                               selection->set (*i);
+                       } else {
+                               selection->set (*(track_views.rbegin()));
+                       }
+                       break;
+               }
+       }
+}
+
+void
+Editor::set_loop_from_selection (bool play)
+{
+       if (session == 0 || selection->time.empty()) {
+               return;
+       }
+
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
+       
+       set_loop_range (start, end,  _("set loop range from selection"));
+
+       if (play) {
+               session->request_play_loop (true);
+               session->request_locate (start, true);
+       }
+}
+
+void
+Editor::set_loop_from_edit_range (bool play)
+{
+       if (session == 0) {
+               return;
+       }
+
+       nframes64_t start;
+       nframes64_t end;
+       
+       if (!get_edit_op_range (start, end)) {
+               return;
+       }
+
+       set_loop_range (start, end,  _("set loop range from edit range"));
+
+       if (play) {
+               session->request_play_loop (true);
+               session->request_locate (start, true);
+       }
+}
+
+void
+Editor::set_loop_from_region (bool play)
+{
+       nframes64_t start = max_frames;
+       nframes64_t end = 0;
+
+       ensure_entered_region_selected (true);
+
+       if (selection->regions.empty()) {
+               info << _("cannot set loop: no region selected") << endmsg;
+               return;
+       }
+
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               if ((*i)->region()->position() < start) {
+                       start = (*i)->region()->position();
+               }
+               if ((*i)->region()->last_frame() + 1 > end) {
+                       end = (*i)->region()->last_frame() + 1;
+               }
+       }
+
+       set_loop_range (start, end, _("set loop range from region"));
+
+       if (play) {
+               session->request_play_loop (true);
+               session->request_locate (start, true);
+       }
+}
+
+void
+Editor::set_punch_from_selection ()
+{
+       if (session == 0 || selection->time.empty()) {
+               return;
+       }
+
+       nframes_t start = selection->time[clicked_selection].start;
+       nframes_t end = selection->time[clicked_selection].end;
+       
+       set_punch_range (start, end,  _("set punch range from selection"));
+}
+
+void
+Editor::set_punch_from_edit_range ()
+{
+       if (session == 0) {
+               return;
+       }
+
+       nframes64_t start;
+       nframes64_t end;
+       
+       if (!get_edit_op_range (start, end)) {
+               return;
+       }
+
+       set_punch_range (start, end,  _("set punch range from edit range"));
+}
+
+void
+Editor::pitch_shift_regions ()
+{
+       ensure_entered_region_selected (true);
+       
+       if (selection->regions.empty()) {
+               return;
+       }
+
+       pitch_shift (selection->regions, 1.2);
+}
+