Fixed overflow issue. Code originally meant to truncate the 64 bit integer did not...
[ardour.git] / gtk2_ardour / editor_mouse.cc
index 36029a421358515dafca75c996e9445f63b6bcd2..1a440a825a04484ac0c3dde01ca7fd19205a3ab1 100644 (file)
@@ -1,3 +1,4 @@
+
 /*
     Copyright (C) 2000-2001 Paul Davis 
 
@@ -85,23 +86,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;
@@ -131,10 +124,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:
@@ -214,9 +213,9 @@ Editor::which_grabber_cursor ()
                return grabber_edit_point_cursor;
                break;
        default:
-               return grabber_cursor;
                break;
        }
+       return grabber_cursor;
 }
 
 void
@@ -478,7 +477,6 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                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 {
@@ -639,8 +637,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 GainAutomationControlPointItem:
@@ -1215,8 +1213,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());
 
@@ -1243,8 +1241,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());
 
@@ -1360,7 +1358,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;
 
@@ -1374,7 +1372,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:
@@ -1495,7 +1493,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;
                
@@ -1512,7 +1510,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;
@@ -1647,7 +1645,7 @@ 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);
        } 
 
@@ -1705,6 +1703,9 @@ Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item
        switch (item_type) {
        case PlayheadCursorItem:
        case MarkerItem:
+       case RangeMarkerBarItem:
+       case TransportMarkerBarItem:
+       case CdMarkerBarItem:
        case GainControlPointItem:
        case RedirectAutomationControlPointItem:
        case GainAutomationControlPointItem:
@@ -1730,13 +1731,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;
        }
@@ -1805,11 +1806,7 @@ 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;
-       }
+       drag_info.clear_copied_locations ();
 }
 
 void
@@ -1854,7 +1851,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;
@@ -2203,11 +2200,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;
 }
@@ -2238,8 +2238,7 @@ 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;
        }
@@ -2268,7 +2267,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);
@@ -2278,7 +2276,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) {
@@ -2294,28 +2292,67 @@ 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)
 {
-       nframes64_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;
 
-       nframes64_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 {
@@ -2332,102 +2369,196 @@ 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;
        }
 
-       if (real_location->locked()) {
+       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 (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
+                               
+                               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
                                
-                       } 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_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
 }
 
 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());
+                       }
                }
        }
 
@@ -2762,7 +2893,7 @@ Editor::start_control_point_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        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 ();
 }
@@ -2914,7 +3045,7 @@ Editor::start_line_grab (AutomationLine* line, GdkEvent* event)
        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 ();
 }
 
@@ -2951,8 +3082,7 @@ Editor::line_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        cy = max (0.0, cy);
        cy = min ((double) line->height(), cy);
 
-       double fraction;
-       fraction = 1.0 - (cy / line->height());
+       double fraction = 1.0 - (cy / line->height());
 
        bool push;
 
@@ -3014,6 +3144,18 @@ Editor::start_region_grab (ArdourCanvas::Item* item, GdkEvent* event)
        show_verbose_time_cursor (drag_info.last_frame_position, 10);
 
        begin_reversible_command (_("move region(s)"));
+       /* 
+          the group containing moved regions may have been 
+          offset during autoscroll. reset its y offset
+          (we should really handle this in the same way 
+          we do with the x axis, but a simple way of achieving that 
+          eludes me right now). 
+       */
+
+       _region_motion_group->property_y() = 0;
+
+       /* sync the canvas to what we think is its current state */
+       track_canvas->update_now();
 }
 
 void
@@ -3046,6 +3188,8 @@ Editor::start_region_copy_grab (ArdourCanvas::Item* item, GdkEvent* event)
        drag_info.motion_callback = &Editor::region_drag_motion_callback;
        drag_info.finished_callback = &Editor::region_drag_finished_callback;
        show_verbose_time_cursor (drag_info.last_frame_position, 10);
+
+       _region_motion_group->property_y() = 0;
 }
 
 void
@@ -3094,18 +3238,14 @@ 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;
                        AudioRegionView* arv;
                        
-                       rv = (*i);
-
-                       
-                       if ((arv = dynamic_cast<AudioRegionView*>(rv)) == 0) {
+                       if ((arv = dynamic_cast<AudioRegionView*>(*i)) == 0) {
                                /* XXX handle MIDI here */
                                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);
@@ -3135,6 +3275,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();
        }
 }
 
@@ -3254,7 +3402,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        }
 
        original_pointer_order = drag_info.dest_trackview->order;
-               
+       
        /************************************************************
                  Y-Delta Computation
        ************************************************************/   
@@ -3336,6 +3484,8 @@ 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);
+                       iy1 += vertical_adjustment.get_value() - canvas_timebars_vsize;
+
                        TimeAxisView* tvp2 = trackview_by_y_position (iy1);
                        RouteTimeAxisView* atv2 = dynamic_cast<RouteTimeAxisView*>(tvp2);
 
@@ -3421,10 +3571,9 @@ 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) {
 
                        nframes64_t sync_frame;
                        nframes64_t sync_offset;
@@ -3459,7 +3608,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                if (pending_region_position > max_frames - rv->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;
@@ -3475,7 +3624,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.
@@ -3485,10 +3634,29 @@ 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_group()->i2w (ix1, iy1);
+                       
+                                       if (-x_delta > ix1 + horizontal_adjustment.get_value()) {
+                                               //      do_move = false;
+                                               cerr << "illegal move" << endl;
+                                               x_delta = 0;
+                                               pending_region_position = drag_info.last_frame_position;
+                                               break;
+                                       }
+                               }
 
+                       }
+               
                        drag_info.last_frame_position = pending_region_position;
-           
+
                } else {
                        x_delta = 0;
                }
@@ -3508,41 +3676,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) {
@@ -3567,6 +3710,31 @@ 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);
+
+                       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);
+                       }
+                       /* for evaluation of the track position of iy1, we have to adjust 
+                          to allow for the vertical scrolling adjustment and the height of the timebars.
+                       */
+
+                       iy1 += vertical_adjustment.get_value() - canvas_timebars_vsize;
+
                        TimeAxisView* tvp2 = trackview_by_y_position (iy1);
                        AudioTimeAxisView* canvas_atv = dynamic_cast<AudioTimeAxisView*>(tvp2);
                        AudioTimeAxisView* temp_atv;
@@ -3629,43 +3797,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 */
@@ -3693,7 +3828,9 @@ 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;
 
        /* first_move is set to false if the regionview has been moved in the 
           motion handler. 
@@ -3706,11 +3843,12 @@ 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
+       /* XXX is this true??? i can''t tell the difference.
+          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);
+       //region_drag_motion_callback (item, event);
 
        if (Config->get_edit_mode() == Splice && !pre_drag_region_selection.empty()) {
                selection->set (pre_drag_region_selection);
@@ -3761,11 +3899,12 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                double ix1, ix2, iy1, iy2;
                rv->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
                rv->get_canvas_group()->i2w (ix1, iy1);
+               iy1 += vertical_adjustment.get_value() - canvas_timebars_vsize;
+
                TimeAxisView* dest_tv = trackview_by_y_position (iy1);
                AudioTimeAxisView* dest_atv = dynamic_cast<AudioTimeAxisView*>(dest_tv);
                double speed;
-               bool changed_tracks;
-               bool changed_position;
+               bool changed_tracks, changed_position;
                nframes64_t where;
 
                if (rv->region()->locked()) {
@@ -3785,23 +3924,25 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                changed_tracks = (dest_tv != &rv->get_time_axis_view());
 
                if (changed_position && !drag_info.x_constrained) {
+                       _master_group->w2i(ix1, iy1);
                        where = (nframes64_t) (unit_to_frame (ix1) * speed);
                } 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) {
                        new_region = RegionFactory::create (rv->region());
                }
 
@@ -3815,7 +3956,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);
@@ -3824,19 +3965,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_atv->reveal_dependent_views (*latest_regionviews.front());
+                               //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.
+                       */
+
+                       RouteTimeAxisView* dest_rtv = dynamic_cast<RouteTimeAxisView*> (dest_atv);
+                       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_atv->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);
@@ -3867,7 +4022,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()));
@@ -3903,7 +4058,6 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                        copies.push_back (rv);
                }
        }
-
        
        if (new_selection.empty()) {
                if (drag_info.copy) {
@@ -3919,6 +4073,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) {
@@ -3931,6 +4089,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
@@ -4021,7 +4180,7 @@ Editor::show_verbose_time_cursor (nframes64_t frame, double offset, double xpos,
                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 ();
 }
@@ -4934,7 +5093,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;          
@@ -5215,8 +5374,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;
                }