Handle changed session duration & resizing canvas better wrt end marker, remove unuse...
[ardour.git] / gtk2_ardour / editor_mouse.cc
index e1d320e386f05eb1b50e7cc71cd5c8ae36e79fe9..1a03f66f2427935d32794bd4281fd5d4f0f7731e 100644 (file)
@@ -1,3 +1,4 @@
+
 /*
     Copyright (C) 2000-2001 Paul Davis 
 
@@ -27,6 +28,7 @@
 
 #include <pbd/error.h>
 #include <gtkmm2ext/utils.h>
+#include <gtkmm2ext/tearoff.h>
 #include <pbd/memento_command.h>
 #include <pbd/basename.h>
 
@@ -92,23 +94,15 @@ Editor::mouse_frame (nframes64_t& where, bool& in_track_canvas) const
        pointer_window = canvas_window->get_pointer (x, y, mask);
 
        if (pointer_window == track_canvas->get_bin_window()) {
-
-               track_canvas->window_to_world (x, y, wx, wy);
+               wx = x;
+               wy = y;
                in_track_canvas = true;
 
        } else {
                in_track_canvas = false;
-
-               if (pointer_window == time_canvas->get_bin_window()) {
-                       time_canvas->window_to_world (x, y, wx, wy);
-               } else {
                        return false;
-               }
        }
 
-       wx += horizontal_adjustment.get_value();
-       wy += vertical_adjustment.get_value();
-
        GdkEvent event;
        event.type = GDK_BUTTON_RELEASE;
        event.button.x = wx;
@@ -138,10 +132,16 @@ Editor::event_frame (GdkEvent* event, double* pcx, double* pcy) const
        case GDK_BUTTON_PRESS:
        case GDK_2BUTTON_PRESS:
        case GDK_3BUTTON_PRESS:
-               track_canvas->w2c(event->button.x, event->button.y, *pcx, *pcy);
+
+               *pcx = event->button.x;
+               *pcy = event->button.y;
+               _trackview_group->w2i(*pcx, *pcy);
                break;
        case GDK_MOTION_NOTIFY:
-               track_canvas->w2c(event->motion.x, event->motion.y, *pcx, *pcy);
+       
+               *pcx = event->motion.x;
+               *pcy = event->motion.y;
+               _trackview_group->w2i(*pcx, *pcy);
                break;
        case GDK_ENTER_NOTIFY:
        case GDK_LEAVE_NOTIFY:
@@ -227,9 +227,9 @@ Editor::which_grabber_cursor ()
                return grabber_edit_point_cursor;
                break;
        default:
-               return grabber_cursor;
                break;
        }
+       return grabber_cursor;
 }
 
 void
@@ -349,11 +349,14 @@ Editor::set_mouse_mode (MouseMode m, bool force)
                break;
        }
 
-       if (mouse_mode == MouseNote)
-               midi_toolbar_frame.show();
-       else
-               midi_toolbar_frame.hide();
-
+       if (midi_tools_tearoff) {
+               if (mouse_mode == MouseNote) {
+                       midi_tools_tearoff->show();
+               } else {
+                       midi_tools_tearoff->hide();
+               }
+       }
+       
        ignore_mouse_mode_toggle = false;
        
        set_canvas_cursor ();
@@ -598,6 +601,24 @@ Editor::button_selection (ArdourCanvas::Item* item, GdkEvent* event, ItemType it
 bool
 Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_type)
 {
+       Glib::RefPtr<Gdk::Window> canvas_window = const_cast<Editor*>(this)->track_canvas->get_window();
+       
+       if (canvas_window) {
+               Glib::RefPtr<const Gdk::Window> pointer_window;
+               int x, y;
+               double wx, wy;
+               Gdk::ModifierType mask;
+
+               pointer_window = canvas_window->get_pointer (x, y, mask);
+               
+               if (pointer_window == track_canvas->get_bin_window()) {
+                       track_canvas->window_to_world (x, y, wx, wy);
+                       allow_vertical_scroll = true;
+               } else {
+                       allow_vertical_scroll = false;
+               }
+       }
+
        track_canvas->grab_focus();
 
        if (session && session->actively_recording()) {
@@ -751,8 +772,8 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                        
                                case RegionViewName:
                                        /* rename happens on edit clicks */
-                                               start_trim (clicked_regionview->get_name_highlight(), event);
-                                               return true;
+                                       start_trim (clicked_regionview->get_name_highlight(), event);
+                                       return true;
                                        break;
 
                                case ControlPointItem:
@@ -959,7 +980,7 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
 bool
 Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_type)
 {
-       nframes_t where = event_frame (event, 0, 0);
+       nframes64_t where = event_frame (event, 0, 0);
        AutomationTimeAxisView* atv = 0;
 
        /* no action if we're recording */
@@ -1304,8 +1325,8 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
                        at_x = cp->get_x();
                        at_y = cp->get_y ();
                        cp->item()->i2w (at_x, at_y);
-                       at_x += 20.0;
-                       at_y += 20.0;
+                       at_x += 10.0;
+                       at_y += 10.0;
 
                        fraction = 1.0 - (cp->get_y() / cp->line().height());
 
@@ -1423,7 +1444,7 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
        case MeterBarItem:
        case TempoBarItem:
                if (is_drawable()) {
-                       time_canvas->get_window()->set_cursor (*timebar_cursor);
+                       track_canvas->get_window()->set_cursor (*timebar_cursor);
                }
                break;
 
@@ -1437,7 +1458,7 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
        case MeterMarkerItem:
        case TempoMarkerItem:
                if (is_drawable()) {
-                       time_canvas->get_window()->set_cursor (*timebar_cursor);
+                       track_canvas->get_window()->set_cursor (*timebar_cursor);
                }
                break;
        case FadeInHandleItem:
@@ -1550,7 +1571,7 @@ Editor::leave_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
        case TempoBarItem:
        case MarkerBarItem:
                if (is_drawable()) {
-                       time_canvas->get_window()->set_cursor (*timebar_cursor);
+                       track_canvas->get_window()->set_cursor (*timebar_cursor);
                }
                break;
                
@@ -1567,7 +1588,7 @@ Editor::leave_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
        case TempoMarkerItem:
                
                if (is_drawable()) {
-                       time_canvas->get_window()->set_cursor (*timebar_cursor);
+                       track_canvas->get_window()->set_cursor (*timebar_cursor);
                }
 
                break;
@@ -1702,10 +1723,10 @@ Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item
                   where DISPLAY = :0.0, and given the cost of what the motion
                   event might do, its a good tradeoff.  
                */
-               
+
                track_canvas->get_pointer (x, y);
        } 
-       
+
        if (current_stepping_trackview) {
                /* don't keep the persistent stepped trackview if the mouse moves */
                current_stepping_trackview = 0;
@@ -1762,6 +1783,9 @@ Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item
        case PlayheadCursorItem:
        case MarkerItem:
        case ControlPointItem:
+       case RangeMarkerBarItem:
+       case TransportMarkerBarItem:
+       case CdMarkerBarItem:
        case TempoMarkerItem:
        case MeterMarkerItem:
        case RegionViewNameHighlight:
@@ -1783,13 +1807,13 @@ Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item
          if (drag_info.item && (event->motion.state & Gdk::BUTTON1_MASK ||
                                 (event->motion.state & Gdk::BUTTON2_MASK))) {
                  if (!from_autoscroll) {
-                         maybe_autoscroll (&event->motion);
+                         maybe_autoscroll_horizontally (&event->motion);
                  }
                  (this->*(drag_info.motion_callback)) (item, event);
                  goto handled;
          }
          goto not_handled;
-         
+         break;
        default:
                break;
        }
@@ -1859,11 +1883,8 @@ Editor::finalize_drag ()
        drag_info.last_pointer_frame = 0;
        drag_info.current_pointer_frame = 0;
        drag_info.brushing = false;
-
-       if (drag_info.copied_location) {
-               delete drag_info.copied_location;
-               drag_info.copied_location = 0;
-       }
+       range_marker_drag_rect->hide();
+       drag_info.clear_copied_locations ();
 }
 
 void
@@ -1908,7 +1929,7 @@ Editor::start_grab (GdkEvent* event, Gdk::Cursor *cursor)
        drag_info.want_move_threshold = false;
        drag_info.pointer_frame_offset = 0;
        drag_info.brushing = false;
-       drag_info.copied_location = 0;
+       drag_info.clear_copied_locations ();
 
        drag_info.original_x = 0;
        drag_info.original_y = 0;
@@ -1993,15 +2014,16 @@ Editor::start_fade_in_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        AudioRegionView* arv = static_cast<AudioRegionView*>(drag_info.data);
 
-       drag_info.pointer_frame_offset = drag_info.grab_frame - ((nframes_t) arv->audio_region()->fade_in()->back()->when + arv->region()->position()); 
+
+       drag_info.pointer_frame_offset = drag_info.grab_frame - ((nframes64_t) arv->audio_region()->fade_in()->back()->when + arv->region()->position());       
 }
 
 void
 Editor::fade_in_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        AudioRegionView* arv = static_cast<AudioRegionView*>(drag_info.data);
-       nframes_t pos;
-       nframes_t fade_length;
+       nframes64_t pos;
+       nframes64_t fade_length;
 
        if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pos = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
@@ -2043,8 +2065,8 @@ void
 Editor::fade_in_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        AudioRegionView* arv = static_cast<AudioRegionView*>(drag_info.data);
-       nframes_t pos;
-       nframes_t fade_length;
+       nframes64_t pos;
+       nframes64_t fade_length;
 
        if (drag_info.first_move) return;
 
@@ -2101,15 +2123,15 @@ Editor::start_fade_out_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        AudioRegionView* arv = static_cast<AudioRegionView*>(drag_info.data);
 
-       drag_info.pointer_frame_offset = drag_info.grab_frame - (arv->region()->length() - (nframes_t) arv->audio_region()->fade_out()->back()->when + arv->region()->position());      
+       drag_info.pointer_frame_offset = drag_info.grab_frame - (arv->region()->length() - (nframes64_t) arv->audio_region()->fade_out()->back()->when + arv->region()->position());    
 }
 
 void
 Editor::fade_out_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        AudioRegionView* arv = static_cast<AudioRegionView*>(drag_info.data);
-       nframes_t pos;
-       nframes_t fade_length;
+       nframes64_t pos;
+       nframes64_t fade_length;
 
        if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pos = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
@@ -2155,8 +2177,8 @@ Editor::fade_out_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* eve
        if (drag_info.first_move) return;
 
        AudioRegionView* arv = static_cast<AudioRegionView*>(drag_info.data);
-       nframes_t pos;
-       nframes_t fade_length;
+       nframes64_t pos;
+       nframes64_t fade_length;
 
        if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pos = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
@@ -2239,7 +2261,7 @@ void
 Editor::cursor_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        Cursor* cursor = (Cursor *) drag_info.data;
-       nframes_t adjusted_frame;
+       nframes64_t adjusted_frame;
        
        if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                adjusted_frame = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
@@ -2257,11 +2279,14 @@ Editor::cursor_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        if (adjusted_frame == drag_info.last_pointer_frame) return;
 
        cursor->set_position (adjusted_frame);
-       
-       UpdateAllTransportClocks (cursor->current_frame);
 
        show_verbose_time_cursor (cursor->current_frame, 10);
 
+#ifdef GTKOSX
+       track_canvas->update_now ();
+#endif
+       UpdateAllTransportClocks (cursor->current_frame);
+
        drag_info.last_pointer_frame = adjusted_frame;
        drag_info.first_move = false;
 }
@@ -2292,13 +2317,13 @@ Editor::update_marker_drag_item (Location *location)
                marker_drag_line_points.front().set_x(x1);
                marker_drag_line_points.back().set_x(x1);
                marker_drag_line->property_points() = marker_drag_line_points;
-       }
-       else {
+       } else {
                range_marker_drag_rect->property_x1() = x1;
                range_marker_drag_rect->property_x2() = x2;
        }
 }
 
+
 void
 Editor::start_marker_grab (ArdourCanvas::Item* item, GdkEvent* event)
 {
@@ -2322,7 +2347,6 @@ Editor::start_marker_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        _dragging_edit_point = true;
 
-       drag_info.copied_location = new Location (*location);
        drag_info.pointer_frame_offset = drag_info.grab_frame - (is_start ? location->start() : location->end());       
 
        update_marker_drag_item (location);
@@ -2332,7 +2356,7 @@ Editor::start_marker_grab (ArdourCanvas::Item* item, GdkEvent* event)
                // marker_drag_line->raise_to_top();
        } else {
                range_marker_drag_rect->show();
-               range_marker_drag_rect->raise_to_top();
+               //range_marker_drag_rect->raise_to_top();
        }
 
        if (is_start) {
@@ -2348,35 +2372,74 @@ Editor::start_marker_grab (ArdourCanvas::Item* item, GdkEvent* event)
                selection->toggle (marker);
                break;
        case Selection::Set:
-               selection->set (marker);
+               if (!selection->selected (marker)) {
+                       selection->set (marker);
+               }
                break;
        case Selection::Extend:
-               selection->add (marker);
+       {
+               Locations::LocationList ll;
+               list<Marker*> to_add;
+               nframes64_t s, e;
+               selection->markers.range (s, e);
+               s = min (marker->position(), s);
+               e = max (marker->position(), e);
+               s = min (s, e);
+               e = max (s, e);
+               if (e < max_frames) {
+                       ++e;
+               }
+               session->locations()->find_all_between (s, e, ll, Location::Flags (0));
+               for (Locations::LocationList::iterator i = ll.begin(); i != ll.end(); ++i) {
+                       LocationMarkers* lm = find_location_markers (*i);
+                       if (lm) {
+                               if (lm->start) {
+                                       to_add.push_back (lm->start);
+                               }
+                               if (lm->end) {
+                                       to_add.push_back (lm->end);
+                               }
+                       }
+               }
+               if (!to_add.empty()) {
+                       selection->add (to_add);
+               }
                break;
+       }
        case Selection::Add:
                selection->add (marker);
                break;
        }
+
+       /* set up copies for us to manipulate during the drag */
+
+       drag_info.clear_copied_locations ();
+
+       for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
+               Location  *l = find_location_from_marker (*i, is_start);
+               drag_info.copied_locations.push_back (new Location (*l));
+       }
 }
 
 void
 Editor::marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
-       nframes_t f_delta;      
-       Marker* marker = (Marker *) drag_info.data;
-       Location  *real_location;
-       Location  *copy_location;
+       nframes64_t f_delta = 0;
+       nframes64_t newframe;
        bool is_start;
        bool move_both = false;
+       Marker* dragged_marker = (Marker*) drag_info.data;
+       Marker* marker;
+       Location  *real_location;
+       Location  *copy_location;
 
-       nframes_t newframe;
        if (drag_info.pointer_frame_offset <= drag_info.current_pointer_frame) {
                newframe = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
        } else {
                newframe = 0;
        }
 
-       nframes_t next = newframe;
+       nframes64_t next = newframe;
 
        if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier())) {
                snap_to (newframe, 0, true);
@@ -2386,102 +2449,197 @@ Editor::marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                return;
        }
 
-       /* call this to find out if its the start or end */
-       
-       if ((real_location = find_location_from_marker (marker, is_start)) == 0) {
-               return;
+       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
+               move_both = true;
+       }
+
+       MarkerSelection::iterator i;
+       list<Location*>::iterator x;
+
+       /* find the marker we're dragging, and compute the delta */
+
+       for (i = selection->markers.begin(), x = drag_info.copied_locations.begin(); 
+            x != drag_info.copied_locations.end() && i != selection->markers.end(); 
+            ++i, ++x) {
+
+               copy_location = *x;
+               marker = *i;
+
+               if (marker == dragged_marker) {
+
+                       if ((real_location = find_location_from_marker (marker, is_start)) == 0) {
+                               /* que pasa ?? */
+                               return;
+                       }
+
+                       if (real_location->is_mark()) {
+                               f_delta = newframe - copy_location->start();
+                       } else {
+
+
+                               switch (marker->type()) {
+                               case Marker::Start:
+                               case Marker::LoopStart:
+                               case Marker::PunchIn:
+                                       f_delta = newframe - copy_location->start();
+                                       break;
+
+                               case Marker::End:
+                               case Marker::LoopEnd:
+                               case Marker::PunchOut:
+                                       f_delta = newframe - copy_location->end();
+                                       break;
+                               default:
+                                       /* what kind of marker is this ? */
+                                       return;
+                               }
+                       }
+                       break;
+               }
        }
 
-       if (real_location->locked()) {
+       if (i == selection->markers.end()) {
+               /* hmm, impossible - we didn't find the dragged marker */
                return;
        }
 
-       /* use the copy that we're "dragging" around */
-       
-       copy_location = drag_info.copied_location;
+       /* now move them all */
 
-       f_delta = copy_location->end() - copy_location->start();
-       
-       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
-               move_both = true;
-       }
+       for (i = selection->markers.begin(), x = drag_info.copied_locations.begin(); 
+            x != drag_info.copied_locations.end() && i != selection->markers.end(); 
+            ++i, ++x) {
 
-       if (copy_location->is_mark()) {
-               /* just move it */
+               copy_location = *x;
+               marker = *i;
 
-               copy_location->set_start (newframe);
+               /* call this to find out if its the start or end */
+               
+               if ((real_location = find_location_from_marker (marker, is_start)) == 0) {
+                       continue;
+               }
+               
+               if (real_location->locked()) {
+                       continue;
+               }
 
-       } else {
+               if (copy_location->is_mark()) {
 
-               if (is_start) { // start-of-range marker
+                       /* just move it */
                        
-                       if (move_both) {
-                               copy_location->set_start (newframe);
-                               copy_location->set_end (newframe + f_delta);
-                       } else  if (newframe < copy_location->end()) {
-                               copy_location->set_start (newframe);
-                       } else { 
-                               snap_to (next, 1, true);
-                               copy_location->set_end (next);
-                               copy_location->set_start (newframe);
-                       }
+                       copy_location->set_start (copy_location->start() + f_delta);
+
+               } else {
                        
-               } else { // end marker
+                       nframes64_t new_start = copy_location->start() + f_delta;
+                       nframes64_t new_end = copy_location->end() + f_delta;
                        
-                       if (move_both) {
-                               copy_location->set_end (newframe);
-                               copy_location->set_start (newframe - f_delta);
-                       } else if (newframe > copy_location->start()) {
-                               copy_location->set_end (newframe);
+                       if (is_start) { // start-of-range marker
                                
-                       } else if (newframe > 0) {
-                               snap_to (next, -1, true);
-                               copy_location->set_start (next);
-                               copy_location->set_end (newframe);
+                               if (move_both) {
+                                       copy_location->set_start (new_start);
+                                       copy_location->set_end (new_end);
+                               } else  if (new_start < copy_location->end()) {
+                                       copy_location->set_start (new_start);
+                               } else { 
+                                       snap_to (next, 1, true);
+                                       copy_location->set_end (next);
+                                       copy_location->set_start (newframe);
+                               }
+                               
+                       } else { // end marker
+                               
+                               if (move_both) {
+                                       copy_location->set_end (new_end);
+                                       copy_location->set_start (new_start);
+                               } else if (new_end > copy_location->start()) {
+                                       copy_location->set_end (new_end);
+                               } else if (newframe > 0) {
+                                       snap_to (next, -1, true);
+                                       copy_location->set_start (next);
+                                       copy_location->set_end (newframe);
+                               }
                        }
                }
+               update_marker_drag_item (copy_location);
+
+               LocationMarkers* lm = find_location_markers (real_location);
+
+               if (lm) {
+                       lm->set_position (copy_location->start(), copy_location->end());
+               }
        }
 
        drag_info.last_pointer_frame = drag_info.current_pointer_frame;
        drag_info.first_move = false;
 
-       update_marker_drag_item (copy_location);
-
-       LocationMarkers* lm = find_location_markers (real_location);
-       lm->set_position (copy_location->start(), copy_location->end());
-       edit_point_clock.set (copy_location->start());
+       if (drag_info.copied_locations.empty()) {
+               abort();
+       }
 
+       edit_point_clock.set (drag_info.copied_locations.front()->start());
        show_verbose_time_cursor (newframe, 10);
+
+#ifdef GTKOSX
+       track_canvas->update_now ();
+#endif
+       edit_point_clock.set (copy_location->start());
 }
 
 void
 Editor::marker_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        if (drag_info.first_move) {
-               /* just a click, do nothing but whatever selection occured */
+
+               /* just a click, do nothing but finish
+                  off the selection process
+               */
+
+               Selection::Operation op = Keyboard::selection_type (event->button.state);
+               Marker* marker = (Marker *) drag_info.data;
+
+               switch (op) {
+               case Selection::Set:
+                       if (selection->selected (marker) && selection->markers.size() > 1) {
+                               selection->set (marker);
+                       }
+                       break;
+
+               case Selection::Toggle:
+               case Selection::Extend:
+               case Selection::Add:
+                       break;
+               }
+               
                return;
        }
 
        _dragging_edit_point = false;
        
-       Marker* marker = (Marker *) drag_info.data;
-       bool is_start;
 
        begin_reversible_command ( _("move marker") );
        XMLNode &before = session->locations()->get_state();
-       
-       Location * location = find_location_from_marker (marker, is_start);
-
-       if (location) {
 
-               if (location->locked()) {
-                       return;
-               }
+       MarkerSelection::iterator i;
+       list<Location*>::iterator x;
+       bool is_start;
 
-               if (location->is_mark()) {
-                       location->set_start (drag_info.copied_location->start());
-               } else {
-                       location->set (drag_info.copied_location->start(), drag_info.copied_location->end());
+       for (i = selection->markers.begin(), x = drag_info.copied_locations.begin(); 
+            x != drag_info.copied_locations.end() && i != selection->markers.end(); 
+            ++i, ++x) {
+       
+               Location * location = find_location_from_marker ((*i), is_start);
+               
+               if (location) {
+                       
+                       if (location->locked()) {
+                               return;
+                       }
+                       
+                       if (location->is_mark()) {
+                               location->set_start ((*x)->start());
+                       } else {
+                               location->set ((*x)->start(), (*x)->end());
+                       }
                }
        }
 
@@ -2562,7 +2720,7 @@ void
 Editor::meter_marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        MeterMarker* marker = (MeterMarker *) drag_info.data;
-       nframes_t adjusted_frame;
+       nframes64_t adjusted_frame;
 
        if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                adjusted_frame = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
@@ -2694,7 +2852,7 @@ void
 Editor::tempo_marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        TempoMarker* marker = (TempoMarker *) drag_info.data;
-       nframes_t adjusted_frame;
+       nframes64_t adjusted_frame;
        
        if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                adjusted_frame = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
@@ -2814,9 +2972,9 @@ Editor::start_control_point_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        control_point->line().start_drag (control_point, drag_info.grab_frame, 0);
 
-       double fraction = 1.0 - ((control_point->get_y() - control_point->line().y_position()) / (double)control_point->line().height());
+       float fraction = 1.0 - (control_point->get_y() / control_point->line().height());
        set_verbose_canvas_cursor (control_point->line().get_verbose_cursor_string (fraction), 
-                                  drag_info.current_pointer_x + 20, drag_info.current_pointer_y + 20);
+                                  drag_info.current_pointer_x + 10, drag_info.current_pointer_y + 10);
 
        show_verbose_canvas_cursor ();
 }
@@ -2863,16 +3021,16 @@ Editor::control_point_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent*
 
        cx = max (0.0, cx);
        cy = max (0.0, cy);
-       cy = min ((double) (cp->line().y_position() + cp->line().height()), cy);
+       cy = min ((double) cp->line().height(), cy);
 
        //translate cx to frames
-       nframes_t cx_frames = unit_to_frame (cx);
+       nframes64_t cx_frames = unit_to_frame (cx);
 
        if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier()) && !drag_info.x_constrained) {
                snap_to (cx_frames);
        }
 
-       const double fraction = 1.0 - ((cy - cp->line().y_position()) / (double)cp->line().height());
+       float fraction = 1.0 - (cy / cp->line().height());
 
        bool push;
 
@@ -2939,7 +3097,7 @@ Editor::start_line_grab (AutomationLine* line, GdkEvent* event)
 {
        double cx;
        double cy;
-       nframes_t frame_within_region;
+       nframes64_t frame_within_region;
 
        /* need to get x coordinate in terms of parent (TimeAxisItemView)
           origin.
@@ -2948,7 +3106,7 @@ Editor::start_line_grab (AutomationLine* line, GdkEvent* event)
        cx = event->button.x;
        cy = event->button.y;
        line->parent_group().w2i (cx, cy);
-       frame_within_region = (nframes_t) floor (cx * frames_per_unit);
+       frame_within_region = (nframes64_t) floor (cx * frames_per_unit);
 
        if (!line->control_points_adjacent (frame_within_region, current_line_drag_info.before, 
                                            current_line_drag_info.after)) {
@@ -2963,12 +3121,12 @@ Editor::start_line_grab (AutomationLine* line, GdkEvent* event)
 
        start_grab (event, fader_cursor);
 
-       const double fraction = 1.0 - ((cy - line->y_position()) / (double)line->height());
+       double fraction = 1.0 - (cy / line->height());
 
        line->start_drag (0, drag_info.grab_frame, fraction);
        
        set_verbose_canvas_cursor (line->get_verbose_cursor_string (fraction),
-                                  drag_info.current_pointer_x + 20, drag_info.current_pointer_y + 20);
+                                  drag_info.current_pointer_x + 10, drag_info.current_pointer_y + 10);
        show_verbose_canvas_cursor ();
 }
 
@@ -2983,7 +3141,6 @@ Editor::line_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                dy *= 0.1;
        }
 
-       double cx = drag_info.current_pointer_x;
        double cy = drag_info.grab_y + drag_info.cumulative_y_drag + dy;
 
        // calculate zero crossing point. back off by .01 to stay on the
@@ -3000,12 +3157,10 @@ Editor::line_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
        drag_info.cumulative_y_drag = cy - drag_info.grab_y;
 
-       line->parent_group().w2i (cx, cy);
-
        cy = max (0.0, cy);
        cy = min ((double) line->height(), cy);
 
-       const double fraction = 1.0 - ((cy - line->y_position()) / (double)line->height());
+       double fraction = 1.0 - (cy / line->height());
 
        bool push;
 
@@ -3034,7 +3189,7 @@ Editor::start_region_grab (ArdourCanvas::Item* item, GdkEvent* event)
        if (selection->regions.empty() || clicked_regionview == 0) {
                return;
        }
-
+       _region_motion_group->raise_to_top ();
        drag_info.copy = false;
        drag_info.item = item;
        drag_info.data = clicked_regionview;
@@ -3057,16 +3212,18 @@ Editor::start_region_grab (ArdourCanvas::Item* item, GdkEvent* event)
                speed = tv->get_diskstream()->speed();
        }
        
-       drag_info.last_frame_position = (nframes_t) (clicked_regionview->region()->position() / speed);
+       drag_info.last_frame_position = (nframes64_t) (clicked_regionview->region()->position() / speed);
        drag_info.pointer_frame_offset = drag_info.grab_frame - drag_info.last_frame_position;
        drag_info.source_trackview = &clicked_regionview->get_time_axis_view();
        drag_info.dest_trackview = drag_info.source_trackview;
        // we want a move threshold
        drag_info.want_move_threshold = true;
-       
        show_verbose_time_cursor (drag_info.last_frame_position, 10);
 
        begin_reversible_command (_("move region(s)"));
+
+       /* sync the canvas to what we think is its current state */
+       track_canvas->update_now();
 }
 
 void
@@ -3089,7 +3246,7 @@ Editor::start_region_copy_grab (ArdourCanvas::Item* item, GdkEvent* event)
        if (selection->regions.empty() || clicked_regionview == 0) {
                return;
        }
-
+       _region_motion_group->raise_to_top ();
        drag_info.copy = true;
        drag_info.item = item;
        drag_info.data = clicked_regionview;    
@@ -3106,7 +3263,7 @@ Editor::start_region_copy_grab (ArdourCanvas::Item* item, GdkEvent* event)
        
        drag_info.source_trackview = &clicked_regionview->get_time_axis_view();
        drag_info.dest_trackview = drag_info.source_trackview;
-       drag_info.last_frame_position = (nframes_t) (clicked_regionview->region()->position() / speed);
+       drag_info.last_frame_position = (nframes64_t) (clicked_regionview->region()->position() / speed);
        drag_info.pointer_frame_offset = drag_info.grab_frame - drag_info.last_frame_position;
        // we want a move threshold
        drag_info.want_move_threshold = true;
@@ -3138,7 +3295,7 @@ Editor::start_region_brush_grab (ArdourCanvas::Item* item, GdkEvent* event)
                speed = tv->get_diskstream()->speed();
        }
        
-       drag_info.last_frame_position = (nframes_t) (clicked_regionview->region()->position() / speed);
+       drag_info.last_frame_position = (nframes64_t) (clicked_regionview->region()->position() / speed);
        drag_info.pointer_frame_offset = drag_info.grab_frame - drag_info.last_frame_position;
        drag_info.source_trackview = &clicked_regionview->get_time_axis_view();
        drag_info.dest_trackview = drag_info.source_trackview;
@@ -3161,26 +3318,27 @@ Editor::possibly_copy_regions_during_grab (GdkEvent* event)
                vector<RegionView*> new_regionviews;
                
                for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ++i) {
-
                        RegionView* rv;
                        RegionView* nrv;
-                       
-                       rv = (*i);
 
+                       rv = (*i);
                        AudioRegionView* arv = dynamic_cast<AudioRegionView*>(rv);
                        MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(rv);
+                       
+                       const boost::shared_ptr<const Region> original = rv->region();
+                       boost::shared_ptr<Region> region_copy = RegionFactory::create (original);
 
                        if (arv) {
-                               nrv = new AudioRegionView (*arv);
+                               boost::shared_ptr<AudioRegion> audioregion_copy
+                                       = boost::dynamic_pointer_cast<AudioRegion>(region_copy);
+                               nrv = new AudioRegionView (*arv, audioregion_copy);
                        } else if (mrv) {
-                               nrv = new MidiRegionView (*mrv);
-                       } else {
-                               continue;
-                       }
-                       
-                       const boost::shared_ptr<const Region> original = arv->region();
-                       boost::shared_ptr<Region> region_copy = RegionFactory::create (original);
-                       boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (region_copy);
+                               boost::shared_ptr<MidiRegion> midiregion_copy
+                                       = boost::dynamic_pointer_cast<MidiRegion>(region_copy);
+                               nrv = new MidiRegionView (*mrv, midiregion_copy);
+                       } else {
+                               continue;
+                       }
 
                        nrv->get_canvas_group()->show ();
                        new_regionviews.push_back (nrv);
@@ -3205,6 +3363,14 @@ Editor::possibly_copy_regions_during_grab (GdkEvent* event)
                drag_info.data = new_regionviews.front();
 
                swap_grab (new_regionviews.front()->get_canvas_group (), 0, event->motion.time);
+               /* 
+                  sync the canvas to what we think is its current state 
+                  without it, the canvas seems to 
+                  "forget" to update properly after the upcoming reparent() 
+                  ..only if the mouse is in rapid motion at the time of the grab. 
+                  something to do with regionview creation raking so long?
+                */
+               track_canvas->update_now();
        }
 }
 
@@ -3227,7 +3393,7 @@ Editor::check_region_drag_possible (RouteTimeAxisView** tv)
                hide_verbose_canvas_cursor ();
                return false;
        }
-       
+
        return true;
 }
 
@@ -3308,8 +3474,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        double x_delta;
        double y_delta = 0;
-       RegionView* rv = reinterpret_cast<RegionView*> (drag_info.data); 
-       nframes_t pending_region_position = 0;
+       nframes64_t pending_region_position = 0;
        int32_t pointer_y_span = 0, canvas_pointer_y_span = 0, original_pointer_order;
        int32_t visible_y_high = 0, visible_y_low = 512;  //high meaning higher numbered.. not the height on the screen
        bool clamp_y_axis = false;
@@ -3324,7 +3489,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        }
 
        original_pointer_order = drag_info.dest_trackview->order;
-               
+       
        /************************************************************
             Y-Delta Computation
        ************************************************************/   
@@ -3363,12 +3528,13 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                                        tracks = tracks |= (0x01 << rtv2->order);
                                }
        
-                               height_list[rtv2->order] = (*i)->height;
+                               height_list[rtv2->order] = (*i)->current_height();
                                children = 1;
+
                                if ((children_list = rtv2->get_child_list()).size() > 0) {
                                        for (TimeAxisView::Children::iterator j = children_list.begin(); j != children_list.end(); ++j) { 
                                                tracks = tracks |= (0x01 << (rtv2->order + children));
-                                               height_list[rtv2->order + children] =  (*j)->height;                
+                                               height_list[rtv2->order + children] =  (*j)->current_height();
                                                numtracks++;
                                                children++;     
                                        }
@@ -3405,7 +3571,9 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                        }
 
                        rv2->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
-                       rv2->get_canvas_group()->i2w (ix1, iy1);
+                       rv2->get_canvas_frame()->i2w (ix1, iy1);
+                       iy1 += vertical_adjustment.get_value() - canvas_timebars_vsize;
+
                        TimeAxisView* tvp2 = trackview_by_y_position (iy1);
                        RouteTimeAxisView* rtv2 = dynamic_cast<RouteTimeAxisView*>(tvp2);
 
@@ -3491,18 +3659,17 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        /* compute the amount of pointer motion in frames, and where
           the region would be if we moved it by that much.
        */
-
        if ( drag_info.move_threshold_passed ) {
 
-               if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+               if (drag_info.current_pointer_frame >= drag_info.pointer_frame_offset) {
 
-                       nframes_t sync_frame;
-                       nframes_t sync_offset;
+                       nframes64_t sync_frame;
+                       nframes64_t sync_offset;
                        int32_t sync_dir;
 
                        pending_region_position = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
 
-                       sync_offset = rv->region()->sync_offset (sync_dir);
+                       sync_offset = clicked_regionview->region()->sync_offset (sync_dir);
 
                        /* we don't handle a sync point that lies before zero.
                         */
@@ -3516,7 +3683,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                                        snap_to (sync_frame);   
                                }
            
-                               pending_region_position = rv->region()->adjust_to_sync (sync_frame);
+                               pending_region_position = clicked_regionview->region()->adjust_to_sync (sync_frame);
 
                        } else {
                                pending_region_position = drag_info.last_frame_position;
@@ -3526,10 +3693,10 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                        pending_region_position = 0;
                }
          
-               if (pending_region_position > max_frames - rv->region()->length()) {
+               if (pending_region_position > max_frames - clicked_regionview->region()->length()) {
                        pending_region_position = drag_info.last_frame_position;
                }
-         
+
                // printf ("3: pending_region_position= %lu    %lu\n", pending_region_position, drag_info.last_frame_position );
 
                bool x_move_allowed;
@@ -3545,7 +3712,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                        x_move_allowed = !drag_info.x_constrained;
                }
 
-               if ( pending_region_position != drag_info.last_frame_position && x_move_allowed ) {
+               if (( pending_region_position != drag_info.last_frame_position) && x_move_allowed ) {
 
                        /* now compute the canvas unit distance we need to move the regionview
                           to make it appear at the new location.
@@ -3555,10 +3722,28 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                                x_delta = ((double) (pending_region_position - drag_info.last_frame_position) / frames_per_unit);
                        } else {
                                x_delta = -((double) (drag_info.last_frame_position - pending_region_position) / frames_per_unit);
-                       }
+                               for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ++i) {
+
+                                       RegionView* rv2 = (*i);
+
+                                       // If any regionview is at zero, we need to know so we can stop further leftward motion.
+       
+                                       double ix1, ix2, iy1, iy2;
+                                       rv2->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
+                                       rv2->get_canvas_frame()->i2w (ix1, iy1);
+                       
+                                       if (-x_delta > ix1 + horizontal_adjustment.get_value()) {
+                                               //      do_move = false;
+                                               x_delta = 0;
+                                               pending_region_position = drag_info.last_frame_position;
+                                               break;
+                                       }
+                               }
 
+                       }
+               
                        drag_info.last_frame_position = pending_region_position;
-           
+
                } else {
                        x_delta = 0;
                }
@@ -3578,41 +3763,16 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                   trackviews. nothing to do.
                */
                return;
-       } 
-
-
-       if (x_delta < 0) {
-               for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ++i) {
-
-                       RegionView* rv2 = (*i);
-
-                       // If any regionview is at zero, we need to know so we can stop further leftward motion.
-                       
-                       double ix1, ix2, iy1, iy2;
-                       rv2->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
-                       rv2->get_canvas_group()->i2w (ix1, iy1);
-
-                       if (ix1 <= 1) {
-                               x_delta = 0;
-                               break;
-                       }
-               }
        }
 
        /*************************************************************
            MOTION                                                                    
        ************************************************************/
-
-       bool do_move;
-
+       bool do_move = true;
        if (drag_info.first_move) {
-               if (drag_info.move_threshold_passed) {
-                       do_move = true;
-               } else {
+               if (!drag_info.move_threshold_passed) {
                        do_move = false;
                }
-       } else {
-               do_move = true;
        }
 
        if (do_move) {
@@ -3636,7 +3796,34 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                        */
 
                        rv->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
-                       rv->get_canvas_group()->i2w (ix1, iy1);
+                       rv->get_canvas_frame()->i2w (ix1, iy1);
+                       
+                       cerr << "adjust y from " << iy1 << " using "
+                            << vertical_adjustment.get_value() << " - "
+                            << canvas_timebars_vsize
+                            << endl;
+
+                       iy1 += get_trackview_group_vertical_offset ();;
+
+                       if (drag_info.first_move) {
+
+                               // hide any dependent views 
+       
+                               rv->get_time_axis_view().hide_dependent_views (*rv);
+
+                               /* 
+                                  reparent to a non scrolling group so that we can keep the 
+                                  region selection above all time axis views.
+                                  reparenting means we have to move the rv as the two 
+                                  parent groups have different coordinates.
+                               */
+
+                               rv->get_canvas_group()->property_y() =  iy1 - 1;
+                               rv->get_canvas_group()->reparent(*_region_motion_group);
+
+                               rv->fake_set_opaque (true);
+                       }
+
                        TimeAxisView* tvp2 = trackview_by_y_position (iy1);
                        RouteTimeAxisView* canvas_rtv = dynamic_cast<RouteTimeAxisView*>(tvp2);
                        RouteTimeAxisView* temp_rtv;
@@ -3685,7 +3872,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                  
                                                tvp2 = trackview_by_y_position (iy1 + y_delta);
                                                temp_rtv = dynamic_cast<RouteTimeAxisView*>(tvp2);
-                                               rv->set_y_position_and_height (0, temp_rtv->height);
+                                               rv->set_height (temp_rtv->current_height());
 
                                                /*   if you un-comment the following, the region colours will follow the track colours whilst dragging,
                                                     personally, i think this can confuse things, but never mind.
@@ -3699,44 +3886,10 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                                }
                        }
 
-
-                       /* prevent the regionview from being moved to before 
-                          the zero position on the canvas.
-                       */
-                       /* clamp */
-               
-                       if (x_delta < 0) {
-                               if (-x_delta > ix1) {
-                                       x_delta = -ix1;
-                               }
-                       } else if ((x_delta > 0) && (rv->region()->last_frame() > max_frames - x_delta)) {
-                               x_delta = max_frames - rv->region()->last_frame();
-                       }
-
-
-                       if (drag_info.first_move) {
-
-                               /* hide any dependent views */
-                       
-                               rv->get_time_axis_view().hide_dependent_views (*rv);
-                       
-                               /* this is subtle. raising the regionview itself won't help,
-                                  because raise_to_top() just puts the item on the top of
-                                  its parent's stack. so, we need to put the trackview canvas_display group
-                                  on the top, since its parent is the whole canvas.
-                               */
-                       
-                               rv->get_canvas_group()->raise_to_top();
-                               rv->get_time_axis_view().canvas_display->raise_to_top();
-                               cursor_group->raise_to_top();
-
-                               rv->fake_set_opaque (true);
-                       }
-
                        if (drag_info.brushing) {
                                mouse_brush_insert_region (rv, pending_region_position);
                        } else {
-                               rv->move (x_delta, y_delta);                    
+                               rv->move (x_delta, y_delta);
                        }
 
                } /* foreach region */
@@ -3764,7 +3917,11 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
        vector<RegionView*> new_selection;
        typedef set<boost::shared_ptr<Playlist> > PlaylistSet;
        PlaylistSet modified_playlists;
-       pair<PlaylistSet::iterator,bool> insert_result;
+       PlaylistSet frozen_playlists;
+       list <sigc::connection> modified_playlist_connections;
+       pair<PlaylistSet::iterator,bool> insert_result, frozen_insert_result;
+       nframes64_t drag_delta;
+       bool changed_tracks, changed_position;
 
        /* first_move is set to false if the regionview has been moved in the 
           motion handler. 
@@ -3777,12 +3934,6 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
 
        nocommit = false;
 
-       /* The regionview has been moved at some stage during the grab so we need
-          to account for any mouse movement between this event and the last one. 
-       */      
-
-       region_drag_motion_callback (item, event);
-
        if (Config->get_edit_mode() == Splice && !pre_drag_region_selection.empty()) {
                selection->set (pre_drag_region_selection);
                pre_drag_region_selection.clear ();
@@ -3826,59 +3977,59 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
 
        begin_reversible_command (op_string);
 
+       changed_position = (drag_info.last_frame_position != (nframes64_t) (clicked_regionview->region()->position()));
+       changed_tracks = (trackview_by_y_position (drag_info.current_pointer_y) != &clicked_regionview->get_time_axis_view());
+
+       drag_delta = clicked_regionview->region()->position() - drag_info.last_frame_position;
+
+       track_canvas->update_now ();
+
        for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ) {
                        
                RegionView* rv = (*i);              
                double ix1, ix2, iy1, iy2;
                rv->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
-               rv->get_canvas_group()->i2w (ix1, iy1);
+               rv->get_canvas_frame()->i2w (ix1, iy1);
+               iy1 += vertical_adjustment.get_value() - canvas_timebars_vsize;
+
                TimeAxisView* dest_tv = trackview_by_y_position (iy1);
                RouteTimeAxisView* dest_rtv = dynamic_cast<RouteTimeAxisView*>(dest_tv);
-               double speed;
-               bool changed_tracks;
-               bool changed_position;
-               nframes_t where;
+               nframes64_t where;
 
                if (rv->region()->locked()) {
                        ++i;
                        continue;
                }
-
-               /* adjust for track speed */
-
-               speed = 1.0;
-               
-               if (dest_rtv && dest_rtv->get_diskstream()) {
-                       speed = dest_rtv->get_diskstream()->speed();
-               }
                
-               changed_position = (drag_info.last_frame_position != (nframes_t) (rv->region()->position()/speed));
-               changed_tracks = (dest_tv != &rv->get_time_axis_view());
-
                if (changed_position && !drag_info.x_constrained) {
-                       where = (nframes_t) (unit_to_frame (ix1) * speed);
+                       where = rv->region()->position() - drag_delta;
                } else {
                        where = rv->region()->position();
                }
                        
-               /* undo the previous hide_dependent_views so that xfades don't
-                  disappear on copying regions 
-               */
-               
-               rv->get_time_axis_view().reveal_dependent_views (*rv);
-               
                boost::shared_ptr<Region> new_region;
 
                if (drag_info.copy) {
                        /* we already made a copy */
                        new_region = rv->region();
-               } else {
+
+                       /* undo the previous hide_dependent_views so that xfades don't
+                          disappear on copying regions 
+                       */
+               
+                       //rv->get_time_axis_view().reveal_dependent_views (*rv);
+               
+               } else if (changed_tracks && dest_rtv->playlist()) {
                        new_region = RegionFactory::create (rv->region());
                }
 
                if (changed_tracks || drag_info.copy) {
 
                        boost::shared_ptr<Playlist> to_playlist = dest_rtv->playlist();
+                       if (!to_playlist) {
+                               ++i;
+                               continue;
+                       }
 
                        latest_regionviews.clear ();
 
@@ -3886,7 +4037,7 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                        
                        insert_result = modified_playlists.insert (to_playlist);
                        if (insert_result.second) {
-                               session->add_command (new MementoCommand<Playlist>(*to_playlist, &to_playlist->get_state(), 0));        
+                               session->add_command (new MementoCommand<Playlist>(*to_playlist, &to_playlist->get_state(), 0));
                        }
 
                        to_playlist->add_region (new_region, where);
@@ -3895,19 +4046,33 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                                                              
                        if (!latest_regionviews.empty()) {
                                // XXX why just the first one ? we only expect one
-                               dest_rtv->reveal_dependent_views (*latest_regionviews.front());
+                               // commented out in nick_m's canvas reworking. is that intended?
+                               //dest_atv->reveal_dependent_views (*latest_regionviews.front());
                                new_selection.push_back (latest_regionviews.front());
                        }
 
                } else {
-                       
+                       /* 
+                          motion on the same track. plonk the previously reparented region 
+                          back to its original canvas group (its streamview).
+                          No need to do anything for copies as they are fake regions which will be deleted.
+                       */
+
+                       rv->get_canvas_group()->reparent (*dest_rtv->view()->canvas_item());
+                       rv->get_canvas_group()->property_y() = 0;
+                 
                        /* just change the model */
                        
                        boost::shared_ptr<Playlist> playlist = dest_rtv->playlist();
 
                        insert_result = modified_playlists.insert (playlist);
                        if (insert_result.second) {
-                               session->add_command (new MementoCommand<Playlist>(*playlist, &playlist->get_state(), 0));      
+                               session->add_command (new MementoCommand<Playlist>(*playlist, &playlist->get_state(), 0));
+                       }
+                       /* freeze to avoid lots of relayering in the case of a multi-region drag */
+                       frozen_insert_result = frozen_playlists.insert(playlist);
+                       if (frozen_insert_result.second) {
+                               playlist->freeze();
                        }
 
                        rv->region()->set_position (where, (void*) this);
@@ -3918,7 +4083,7 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                        /* get the playlist where this drag started. we can't use rv->region()->playlist()
                           because we may have copied the region and it has not been attached to a playlist.
                        */
-                       
+
                        assert ((source_tv = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view())));
                        assert ((ds = source_tv->get_diskstream()));
                        assert ((from_playlist = ds->playlist()));
@@ -3938,7 +4103,7 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
 
                        insert_result = modified_playlists.insert (from_playlist);
                        if (insert_result.second) {
-                               session->add_command (new MementoCommand<Playlist>(*from_playlist, &from_playlist->get_state(), 0));    
+                               session->add_command (new MementoCommand<Playlist>(*from_playlist, &from_playlist->get_state(), 0));
                        }
 
                        from_playlist->remove_region ((rv->region()));
@@ -3974,7 +4139,6 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                        copies.push_back (rv);
                }
        }
-
        
        if (new_selection.empty()) {
                if (drag_info.copy) {
@@ -3990,6 +4154,10 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                */
                selection->set (new_selection);
        }
+
+       for (set<boost::shared_ptr<Playlist> >::iterator p = frozen_playlists.begin(); p != frozen_playlists.end(); ++p) {
+               (*p)->thaw();
+       }
                        
   out:
        if (!nocommit) {
@@ -4002,6 +4170,7 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
        for (vector<RegionView*>::iterator x = copies.begin(); x != copies.end(); ++x) {
                delete *x;
        }
+
 }
        
 void
@@ -4036,7 +4205,7 @@ Editor::create_region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent
                begin_reversible_command (_("create region"));
                XMLNode &before = mtv->playlist()->get_state();
 
-               nframes_t start = drag_info.grab_frame;
+               nframes64_t start = drag_info.grab_frame;
                snap_to (start, -1);
                const Meter& m = session->tempo_map().meter_at(start);
                const Tempo& t = session->tempo_map().tempo_at(start);
@@ -4078,28 +4247,28 @@ Editor::region_view_item_click (AudioRegionView& rv, GdkEventButton* event)
 
                        if (Keyboard::modifier_state_equals (event->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
                                
-                               align_region (rv.region(), SyncPoint, (nframes_t) (where * speed));
+                               align_region (rv.region(), SyncPoint, (nframes64_t) (where * speed));
                                
                        } else if (Keyboard::modifier_state_equals (event->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
                                
-                               align_region (rv.region(), End, (nframes_t) (where * speed));
+                               align_region (rv.region(), End, (nframes64_t) (where * speed));
                                
                        } else {
                                
-                               align_region (rv.region(), Start, (nframes_t) (where * speed));
+                               align_region (rv.region(), Start, (nframes64_t) (where * speed));
                        }
                }
        }
 }
 
 void
-Editor::show_verbose_time_cursor (nframes_t frame, double offset, double xpos, double ypos) 
+Editor::show_verbose_time_cursor (nframes64_t frame, double offset, double xpos, double ypos) 
 {
        char buf[128];
        SMPTE::Time smpte;
        BBT_Time bbt;
        int hours, mins;
-       nframes_t frame_rate;
+       nframes64_t frame_rate;
        float secs;
 
        if (session == 0) {
@@ -4137,7 +4306,7 @@ Editor::show_verbose_time_cursor (nframes_t frame, double offset, double xpos, d
                break;
 
        default:
-               snprintf (buf, sizeof(buf), "%u", frame);
+               snprintf (buf, sizeof(buf), "%" PRIi64, frame);
                break;
        }
 
@@ -4145,20 +4314,20 @@ Editor::show_verbose_time_cursor (nframes_t frame, double offset, double xpos, d
                set_verbose_canvas_cursor (buf, xpos + offset, ypos + offset);
        }
        else {
-               set_verbose_canvas_cursor (buf, drag_info.current_pointer_x + offset, drag_info.current_pointer_y + offset);
+               set_verbose_canvas_cursor (buf, drag_info.current_pointer_x + offset - horizontal_adjustment.get_value(), drag_info.current_pointer_y + offset - vertical_adjustment.get_value() + canvas_timebars_vsize);
        }
        show_verbose_canvas_cursor ();
 }
 
 void
-Editor::show_verbose_duration_cursor (nframes_t start, nframes_t end, double offset, double xpos, double ypos) 
+Editor::show_verbose_duration_cursor (nframes64_t start, nframes64_t end, double offset, double xpos, double ypos) 
 {
        char buf[128];
        SMPTE::Time smpte;
        BBT_Time sbbt;
        BBT_Time ebbt;
        int hours, mins;
-       nframes_t distance, frame_rate;
+       nframes64_t distance, frame_rate;
        float secs;
        Meter meter_at_start(session->tempo_map().meter_at(start));
 
@@ -4219,7 +4388,7 @@ Editor::show_verbose_duration_cursor (nframes_t start, nframes_t end, double off
                break;
 
        default:
-               snprintf (buf, sizeof(buf), "%u", end - start);
+               snprintf (buf, sizeof(buf), "%" PRIi64, end - start);
                break;
        }
 
@@ -4331,8 +4500,8 @@ Editor::cancel_selection ()
 void
 Editor::start_selection_op (ArdourCanvas::Item* item, GdkEvent* event, SelectionOp op)
 {
-       nframes_t start = 0;
-       nframes_t end = 0;
+       nframes64_t start = 0;
+       nframes64_t end = 0;
 
        if (session == 0) {
                return;
@@ -4389,10 +4558,10 @@ Editor::start_selection_op (ArdourCanvas::Item* item, GdkEvent* event, Selection
 void
 Editor::drag_selection (ArdourCanvas::Item* item, GdkEvent* event)
 {
-       nframes_t start = 0;
-       nframes_t end = 0;
-       nframes_t length;
-       nframes_t pending_position;
+       nframes64_t start = 0;
+       nframes64_t end = 0;
+       nframes64_t length;
+       nframes64_t pending_position;
 
        if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pending_position = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
@@ -4550,9 +4719,9 @@ Editor::start_trim (ArdourCanvas::Item* item, GdkEvent* event)
                speed = tv->get_diskstream()->speed();
        }
        
-       nframes_t region_start = (nframes_t) (clicked_regionview->region()->position() / speed);
-       nframes_t region_end = (nframes_t) (clicked_regionview->region()->last_frame() / speed);
-       nframes_t region_length = (nframes_t) (clicked_regionview->region()->length() / speed);
+       nframes64_t region_start = (nframes64_t) (clicked_regionview->region()->position() / speed);
+       nframes64_t region_end = (nframes64_t) (clicked_regionview->region()->last_frame() / speed);
+       nframes64_t region_length = (nframes64_t) (clicked_regionview->region()->length() / speed);
 
        //drag_info.item = clicked_regionview->get_name_highlight();
        drag_info.item = item;
@@ -4591,7 +4760,7 @@ void
 Editor::trim_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        RegionView* rv = clicked_regionview;
-       nframes_t frame_delta = 0;
+       nframes64_t frame_delta = 0;
        bool left_direction;
        bool obey_snap = !Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier());
 
@@ -4676,7 +4845,7 @@ Editor::trim_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                }
                
        case EndTrim:
-               if ((left_direction == true) && (drag_info.current_pointer_frame > (nframes_t) (rv->region()->last_frame()/speed))) {
+               if ((left_direction == true) && (drag_info.current_pointer_frame > (nframes64_t) (rv->region()->last_frame()/speed))) {
                        break;
                } else {
                        for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ++i) {
@@ -4704,10 +4873,10 @@ Editor::trim_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
        switch (trim_op) {
        case StartTrim:
-               show_verbose_time_cursor((nframes_t) (rv->region()->position()/speed), 10);     
+               show_verbose_time_cursor((nframes64_t) (rv->region()->position()/speed), 10);   
                break;
        case EndTrim:
-               show_verbose_time_cursor((nframes_t) (rv->region()->last_frame()/speed), 10);   
+               show_verbose_time_cursor((nframes64_t) (rv->region()->last_frame()/speed), 10); 
                break;
        case ContentsTrim:
                show_verbose_time_cursor(drag_info.current_pointer_frame, 10);  
@@ -4719,7 +4888,7 @@ Editor::trim_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 }
 
 void
-Editor::single_contents_trim (RegionView& rv, nframes_t frame_delta, bool left_direction, bool swap_direction, bool obey_snap)
+Editor::single_contents_trim (RegionView& rv, nframes64_t frame_delta, bool left_direction, bool swap_direction, bool obey_snap)
 {
        boost::shared_ptr<Region> region (rv.region());
 
@@ -4727,7 +4896,7 @@ Editor::single_contents_trim (RegionView& rv, nframes_t frame_delta, bool left_d
                return;
        }
 
-       nframes_t new_bound;
+       nframes64_t new_bound;
 
        double speed = 1.0;
        TimeAxisView* tvp = clicked_axisview;
@@ -4739,27 +4908,27 @@ Editor::single_contents_trim (RegionView& rv, nframes_t frame_delta, bool left_d
        
        if (left_direction) {
                if (swap_direction) {
-                       new_bound = (nframes_t) (region->position()/speed) + frame_delta;
+                       new_bound = (nframes64_t) (region->position()/speed) + frame_delta;
                } else {
-                       new_bound = (nframes_t) (region->position()/speed) - frame_delta;
+                       new_bound = (nframes64_t) (region->position()/speed) - frame_delta;
                }
        } else {
                if (swap_direction) {
-                       new_bound = (nframes_t) (region->position()/speed) - frame_delta;
+                       new_bound = (nframes64_t) (region->position()/speed) - frame_delta;
                } else {
-                       new_bound = (nframes_t) (region->position()/speed) + frame_delta;
+                       new_bound = (nframes64_t) (region->position()/speed) + frame_delta;
                }
        }
 
        if (obey_snap) {
                snap_to (new_bound);
        }
-       region->trim_start ((nframes_t) (new_bound * speed), this);     
+       region->trim_start ((nframes64_t) (new_bound * speed), this);   
        rv.region_changed (StartChanged);
 }
 
 void
-Editor::single_start_trim (RegionView& rv, nframes_t frame_delta, bool left_direction, bool obey_snap)
+Editor::single_start_trim (RegionView& rv, nframes64_t frame_delta, bool left_direction, bool obey_snap)
 {
        boost::shared_ptr<Region> region (rv.region()); 
 
@@ -4767,7 +4936,7 @@ Editor::single_start_trim (RegionView& rv, nframes_t frame_delta, bool left_dire
                return;
        }
 
-       nframes_t new_bound;
+       nframes64_t new_bound;
 
        double speed = 1.0;
        TimeAxisView* tvp = clicked_axisview;
@@ -4778,22 +4947,22 @@ Editor::single_start_trim (RegionView& rv, nframes_t frame_delta, bool left_dire
        }
        
        if (left_direction) {
-               new_bound = (nframes_t) (region->position()/speed) - frame_delta;
+               new_bound = (nframes64_t) (region->position()/speed) - frame_delta;
        } else {
-               new_bound = (nframes_t) (region->position()/speed) + frame_delta;
+               new_bound = (nframes64_t) (region->position()/speed) + frame_delta;
        }
 
        if (obey_snap) {
                snap_to (new_bound, (left_direction ? 0 : 1));  
        }
 
-       region->trim_front ((nframes_t) (new_bound * speed), this);
+       region->trim_front ((nframes64_t) (new_bound * speed), this);
 
        rv.region_changed (Change (LengthChanged|PositionChanged|StartChanged));
 }
 
 void
-Editor::single_end_trim (RegionView& rv, nframes_t frame_delta, bool left_direction, bool obey_snap)
+Editor::single_end_trim (RegionView& rv, nframes64_t frame_delta, bool left_direction, bool obey_snap)
 {
        boost::shared_ptr<Region> region (rv.region());
 
@@ -4801,7 +4970,7 @@ Editor::single_end_trim (RegionView& rv, nframes_t frame_delta, bool left_direct
                return;
        }
 
-       nframes_t new_bound;
+       nframes64_t new_bound;
 
        double speed = 1.0;
        TimeAxisView* tvp = clicked_axisview;
@@ -4812,15 +4981,15 @@ Editor::single_end_trim (RegionView& rv, nframes_t frame_delta, bool left_direct
        }
        
        if (left_direction) {
-               new_bound = (nframes_t) ((region->last_frame() + 1)/speed) - frame_delta;
+               new_bound = (nframes64_t) ((region->last_frame() + 1)/speed) - frame_delta;
        } else {
-               new_bound = (nframes_t) ((region->last_frame() + 1)/speed) + frame_delta;
+               new_bound = (nframes64_t) ((region->last_frame() + 1)/speed) + frame_delta;
        }
 
        if (obey_snap) {
                snap_to (new_bound);
        }
-       region->trim_end ((nframes_t) (new_bound * speed), this);
+       region->trim_end ((nframes64_t) (new_bound * speed), this);
        rv.region_changed (LengthChanged);
 }
        
@@ -4860,7 +5029,7 @@ void
 Editor::point_trim (GdkEvent* event)
 {
        RegionView* rv = clicked_regionview;
-       nframes_t new_bound = drag_info.current_pointer_frame;
+       nframes64_t new_bound = drag_info.current_pointer_frame;
 
        if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier())) {
                snap_to (new_bound);
@@ -5008,8 +5177,8 @@ Editor::start_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event, Ran
 void
 Editor::drag_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event)
 {
-       nframes_t start = 0;
-       nframes_t end = 0;
+       nframes64_t start = 0;
+       nframes64_t end = 0;
        ArdourCanvas::SimpleRect *crect;
 
        switch (range_marker_op) {
@@ -5066,7 +5235,7 @@ Editor::drag_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event)
 
                        update_marker_drag_item (temp_location);
                        range_marker_drag_rect->show();
-                       range_marker_drag_rect->raise_to_top();
+                       //range_marker_drag_rect->raise_to_top();
                        
                } 
                break;          
@@ -5140,8 +5309,8 @@ Editor::end_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event)
 
                if (Keyboard::no_modifier_keys_pressed (&event->button) && range_marker_op != CreateCDMarker) {
 
-                       nframes_t start;
-                       nframes_t end;
+                       nframes64_t start;
+                       nframes64_t end;
 
                        start = session->locations()->first_mark_before (drag_info.grab_frame);
                        end = session->locations()->first_mark_after (drag_info.grab_frame);
@@ -5191,8 +5360,8 @@ Editor::start_mouse_zoom (ArdourCanvas::Item* item, GdkEvent* event)
 void
 Editor::drag_mouse_zoom (ArdourCanvas::Item* item, GdkEvent* event)
 {
-       nframes_t start;
-       nframes_t end;
+       nframes64_t start;
+       nframes64_t end;
 
        if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier())) {
                snap_to (drag_info.current_pointer_frame);
@@ -5252,7 +5421,7 @@ Editor::end_mouse_zoom (ArdourCanvas::Item* item, GdkEvent* event)
 }
 
 void
-Editor::reposition_zoom_rect (nframes_t start, nframes_t end)
+Editor::reposition_zoom_rect (nframes64_t start, nframes64_t end)
 {
        double x1 = frame_to_pixel (start);
        double x2 = frame_to_pixel (end);
@@ -5279,8 +5448,8 @@ Editor::start_rubberband_select (ArdourCanvas::Item* item, GdkEvent* event)
 void
 Editor::drag_rubberband_select (ArdourCanvas::Item* item, GdkEvent* event)
 {
-       nframes_t start;
-       nframes_t end;
+       nframes64_t start;
+       nframes64_t end;
        double y1;
        double y2;
 
@@ -5347,8 +5516,7 @@ Editor::end_rubberband_select (ArdourCanvas::Item* item, GdkEvent* event)
                if (drag_info.current_pointer_y < drag_info.grab_y) {
                        y1 = drag_info.current_pointer_y;
                        y2 = drag_info.grab_y;
-               }
-               else {
+               } else {
                        y2 = drag_info.current_pointer_y;
                        y1 = drag_info.grab_y;
                }
@@ -5453,21 +5621,21 @@ Editor::end_time_fx (ArdourCanvas::Item* item, GdkEvent* event)
                return;
        }
        
-       nframes_t newlen = drag_info.last_pointer_frame - clicked_regionview->region()->position();
+       nframes64_t newlen = drag_info.last_pointer_frame - clicked_regionview->region()->position();
 
        float percentage = (double) newlen / (double) clicked_regionview->region()->length();
-
+       
 #ifndef USE_RUBBERBAND
        // Soundtouch uses percentage / 100 instead of normal (/ 1) 
        if (clicked_regionview->region()->data_type() == DataType::AUDIO) {
                percentage = (float) ((double) newlen - (double) clicked_regionview->region()->length()) / ((double) newlen) * 100.0f;
-#endif 
        }
-
+#endif 
+       
        begin_reversible_command (_("timestretch"));
-
+       
        // XXX how do timeFX on multiple regions ?
-
+       
        RegionSelection rs;
        rs.add (clicked_regionview);
 
@@ -5477,14 +5645,10 @@ Editor::end_time_fx (ArdourCanvas::Item* item, GdkEvent* event)
 }
 
 void
-Editor::mouse_brush_insert_region (RegionView* rv, nframes_t pos)
+Editor::mouse_brush_insert_region (RegionView* rv, nframes64_t pos)
 {
        /* no brushing without a useful snap setting */
 
-       // FIXME
-       AudioRegionView* arv = dynamic_cast<AudioRegionView*>(rv);
-       assert(arv);
-
        switch (snap_mode) {
        case SnapMagnetic:
                return; /* can't work because it allows region to be placed anywhere */
@@ -5506,7 +5670,7 @@ Editor::mouse_brush_insert_region (RegionView* rv, nframes_t pos)
                return;
        }
 
-       RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&arv->get_time_axis_view());
+       RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&rv->get_time_axis_view());
 
        if (rtv == 0 || !rtv->is_track()) {
                return;
@@ -5516,7 +5680,7 @@ Editor::mouse_brush_insert_region (RegionView* rv, nframes_t pos)
        double speed = rtv->get_diskstream()->speed();
        
        XMLNode &before = playlist->get_state();
-       playlist->add_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (arv->audio_region())), (nframes_t) (pos * speed));
+       playlist->add_region (RegionFactory::create (rv->region()), (nframes64_t) (pos * speed));
        XMLNode &after = playlist->get_state();
        session->add_command(new MementoCommand<Playlist>(*playlist.get(), &before, &after));
        
@@ -5528,13 +5692,7 @@ Editor::mouse_brush_insert_region (RegionView* rv, nframes_t pos)
 gint
 Editor::track_height_step_timeout ()
 {
-       struct timeval now;
-       struct timeval delta;
-       
-       gettimeofday (&now, 0);
-       timersub (&now, &last_track_height_step_timestamp, &delta);
-       
-       if (delta.tv_sec * 1000000 + delta.tv_usec > 250000) { /* milliseconds */
+       if (get_microseconds() - last_track_height_step_timestamp < 250000) {
                current_stepping_trackview = 0;
                return false;
        }