Tempo ramps - allow live updating of tempo markers.
[ardour.git] / gtk2_ardour / editor_drag.cc
index d47109303bbfb4154b90309c257fce32d577af2d..dccb6cac51c94ea9c499d817d7b01be4556c62c0 100644 (file)
@@ -64,6 +64,7 @@
 #include "mouse_cursors.h"
 #include "note_base.h"
 #include "patch_change.h"
+#include "ui_config.h"
 #include "verbose_cursor.h"
 
 using namespace std;
@@ -81,7 +82,10 @@ double ControlPointDrag::_zero_gain_fraction = -1.0;
 DragManager::DragManager (Editor* e)
        : _editor (e)
        , _ending (false)
+       , _current_pointer_x (0.0)
+       , _current_pointer_y (0.0)
        , _current_pointer_frame (0)
+       , _old_follow_playhead (false)
 {
 }
 
@@ -96,8 +100,6 @@ DragManager::abort ()
 {
        _ending = true;
 
-       cerr << "Aborting drag\n";
-
        for (list<Drag*>::const_iterator i = _drags.begin(); i != _drags.end(); ++i) {
                (*i)->abort ();
                delete *i;
@@ -216,16 +218,25 @@ DragManager::have_item (ArdourCanvas::Item* i) const
 
 Drag::Drag (Editor* e, ArdourCanvas::Item* i, bool trackview_only)
        : _editor (e)
+       , _drags (0)
        , _item (i)
        , _pointer_frame_offset (0)
+       , _x_constrained (false)
+       , _y_constrained (false)
+       , _was_rolling (false)
        , _trackview_only (trackview_only)
        , _move_threshold_passed (false)
        , _starting_point_passed (false)
        , _initially_vertical (false)
        , _was_double_click (false)
+       , _grab_x (0.0)
+       , _grab_y (0.0)
+       , _last_pointer_x (0.0)
+       , _last_pointer_y (0.0)
        , _raw_grab_frame (0)
        , _grab_frame (0)
        , _last_pointer_frame (0)
+       , _snap_delta (0)
 {
 
 }
@@ -248,22 +259,11 @@ Drag::swap_grab (ArdourCanvas::Item* new_item, Gdk::Cursor* cursor, uint32_t /*t
 void
 Drag::start_grab (GdkEvent* event, Gdk::Cursor *cursor)
 {
-       // if dragging with button2, the motion is x constrained, with Alt-button2 it is y constrained
 
-       if (Keyboard::is_button2_event (&event->button)) {
-               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::SecondaryModifier)) {
-                       _y_constrained = true;
-                       _x_constrained = false;
-               } else {
-                       _y_constrained = false;
-                       _x_constrained = true;
-               }
-       } else {
-               _x_constrained = false;
-               _y_constrained = false;
-       }
+       /* we set up x/y dragging constraints on first move */
 
        _raw_grab_frame = _editor->canvas_event_sample (event, &_grab_x, &_grab_y);
+
        setup_pointer_frame_offset ();
        _grab_frame = adjusted_frame (_raw_grab_frame, event);
        _last_pointer_frame = _grab_frame;
@@ -343,6 +343,16 @@ Drag::adjusted_current_frame (GdkEvent const * event, bool snap) const
        return adjusted_frame (_drags->current_pointer_frame (), event, snap);
 }
 
+frameoffset_t
+Drag::snap_delta (guint state) const
+{
+       if (ArdourKeyboard::indicates_snap_delta (state)) {
+               return _snap_delta;
+       }
+
+       return 0;
+}
+
 double
 Drag::current_pointer_x() const
 {
@@ -359,6 +369,14 @@ Drag::current_pointer_y () const
        return _drags->current_pointer_y () - _editor->get_trackview_group()->canvas_origin().y;
 }
 
+void
+Drag::setup_snap_delta (framepos_t pos)
+{
+       framepos_t temp = pos;
+       _editor->snap_to (temp, ARDOUR::RoundNearest, false, true);
+       _snap_delta = temp - pos;
+}
+
 bool
 Drag::motion_handler (GdkEvent* event, bool from_autoscroll)
 {
@@ -394,6 +412,39 @@ Drag::motion_handler (GdkEvent* event, bool from_autoscroll)
                                } else {
                                        _initially_vertical = false;
                                }
+                               /** check constraints for this drag.
+                                *  Note that the current convention is to use "contains" for
+                                *  key modifiers during motion and "equals" when initiating a drag.
+                                *  In this case we haven't moved yet, so "equals" applies here.
+                                */
+                               if (Config->get_edit_mode() != Lock) {
+                                       if (event->motion.state & Gdk::BUTTON2_MASK) {
+                                               // if dragging with button2, the motion is x constrained, with constraint modifier it is y constrained
+                                               if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+                                                       _x_constrained = false;
+                                                       _y_constrained = true;
+                                               } else {
+                                                       _x_constrained = true;
+                                                       _y_constrained = false;
+                                               }
+                                       } else if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+                                               // if dragging normally, the motion is constrained to the first direction of movement.
+                                               if (_initially_vertical) {
+                                                       _x_constrained = true;
+                                                       _y_constrained = false;
+                                               } else {
+                                                       _x_constrained = false;
+                                                       _y_constrained = true;
+                                               }
+                                       }
+                               } else {
+                                       if (event->button.state & Gdk::BUTTON2_MASK) {
+                                               _x_constrained = false;
+                                       } else {
+                                               _x_constrained = true;
+                                       }
+                                       _y_constrained = false;
+                               }
                        }
 
                        if (!from_autoscroll) {
@@ -413,7 +464,7 @@ Drag::motion_handler (GdkEvent* event, bool from_autoscroll)
 
                                _last_pointer_x = _drags->current_pointer_x ();
                                _last_pointer_y = current_pointer_y ();
-                               _last_pointer_frame = adjusted_current_frame (event);
+                               _last_pointer_frame = adjusted_current_frame (event, false);
                        }
 
                        return true;
@@ -459,17 +510,16 @@ Drag::show_verbose_cursor_text (string const & text)
 }
 
 boost::shared_ptr<Region>
-Drag::add_midi_region (MidiTimeAxisView* view)
+Drag::add_midi_region (MidiTimeAxisView* view, bool commit)
 {
        if (_editor->session()) {
                const TempoMap& map (_editor->session()->tempo_map());
                framecnt_t pos = grab_frame();
-               const Meter& m = map.meter_at (pos);
                /* not that the frame rate used here can be affected by pull up/down which
                   might be wrong.
                */
-               framecnt_t len = m.frames_per_bar (map.tempo_at (pos), _editor->session()->frame_rate());
-               return view->add_region (grab_frame(), len, true);
+               framecnt_t len = map.frame_at_beat (map.beat_at_frame (pos) + 1.0) - pos;
+               return view->add_region (grab_frame(), len, commit);
        }
 
        return boost::shared_ptr<Region>();
@@ -485,8 +535,9 @@ struct EditorOrderTimeAxisViewSorter {
 };
 
 RegionDrag::RegionDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<RegionView*> const & v)
-       : Drag (e, i),
-         _primary (p)
+       : Drag (e, i)
+       , _primary (p)
+       , _ntracks (0)
 {
        _editor->visible_order_range (&_visible_y_low, &_visible_y_high);
 
@@ -551,10 +602,13 @@ RegionDrag::find_time_axis_view (TimeAxisView* t) const
 RegionMotionDrag::RegionMotionDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<RegionView*> const & v, bool b)
        : RegionDrag (e, i, p, v)
        , _brushing (b)
+       , _ignore_video_lock (false)
        , _total_x_delta (0)
        , _last_pointer_time_axis_view (0)
        , _last_pointer_layer (0)
-       , _single_axis (false)
+       , _ndropzone (0)
+       , _pdropzone (0)
+       , _ddropzone (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New RegionMotionDrag\n");
 }
@@ -563,10 +617,7 @@ void
 RegionMotionDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
 {
        Drag::start_grab (event, cursor);
-
-       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::TertiaryModifier)) {
-               _single_axis = true;
-       }
+       setup_snap_delta (_last_frame_position);
 
        show_verbose_cursor_time (_last_frame_position);
 
@@ -576,6 +627,15 @@ RegionMotionDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
                assert(_last_pointer_time_axis_view >= 0);
                _last_pointer_layer = tv.first->layer_display() == Overlaid ? 0 : tv.second;
        }
+
+       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::ModifierMask (Keyboard::TertiaryModifier))) {
+               _ignore_video_lock = true;
+       }
+
+       if (_brushing) {
+               /* cross track dragging seems broken here. disabled for now. */
+               _y_constrained = true;
+       }
 }
 
 double
@@ -584,7 +644,7 @@ RegionMotionDrag::compute_x_delta (GdkEvent const * event, framepos_t* pending_r
        /* compute the amount of pointer motion in frames, and where
           the region would be if we moved it by that much.
        */
-       *pending_region_position = adjusted_current_frame (event);
+       *pending_region_position = adjusted_frame (_drags->current_pointer_frame (), event, false);
 
        framepos_t sync_frame;
        framecnt_t sync_offset;
@@ -596,11 +656,12 @@ RegionMotionDrag::compute_x_delta (GdkEvent const * event, framepos_t* pending_r
         */
        if (sync_dir >= 0 || (sync_dir < 0 && *pending_region_position >= sync_offset)) {
 
-               sync_frame = *pending_region_position + (sync_dir*sync_offset);
+               framecnt_t const sd = snap_delta (event->button.state);
+               sync_frame = *pending_region_position + (sync_dir * sync_offset) + sd;
 
                _editor->snap_to_with_modifier (sync_frame, event);
 
-               *pending_region_position = _primary->region()->adjust_to_sync (sync_frame);
+               *pending_region_position = _primary->region()->adjust_to_sync (sync_frame) - sd;
 
        } else {
                *pending_region_position = _last_frame_position;
@@ -612,8 +673,7 @@ RegionMotionDrag::compute_x_delta (GdkEvent const * event, framepos_t* pending_r
 
        double dx = 0;
 
-       /* in locked edit mode, reverse the usual meaning of _x_constrained */
-       bool const x_move_allowed = Config->get_edit_mode() == Lock ? _x_constrained : !_x_constrained;
+       bool const x_move_allowed = !_x_constrained;
 
        if ((*pending_region_position != _last_frame_position) && x_move_allowed) {
 
@@ -647,36 +707,23 @@ RegionDrag::apply_track_delta (const int start, const int delta, const int skip,
                return start;
        }
 
+       const int tavsize  = _time_axis_views.size();
        const int dt = delta > 0 ? +1 : -1;
-       int current = start;
-       int target  = start + delta - skip;
+       int current  = start;
+       int target   = start + delta - skip;
 
-       assert (current < 0 || current >= _time_axis_views.size() || !_time_axis_views[current]->hidden());
+       assert (current < 0 || current >= tavsize || !_time_axis_views[current]->hidden());
        assert (skip == 0 || (skip < 0 && delta < 0) || (skip > 0 && delta > 0));
 
-#ifdef DEBUG_DROPZONEDRAG
-       if (current >= _time_axis_views.size() && target >= 0 && target < _time_axis_views.size()) {
-               printf("MOVE OUT OF THE ZONE cur: %d  d: %d s: %d\n", start, delta, skip);
-       } else {
-               printf("CALC DISTANCE cur: %d  d: %d s: %d\n", start, delta, skip);
-       }
-#endif
-
        while (current >= 0 && current != target) {
                current += dt;
                if (current < 0 && dt < 0) {
-#ifdef DEBUG_DROPZONEDRAG
-                       printf("BREAK AT BOTTOM\n");
-#endif
                        break;
                }
-               if (current >= _time_axis_views.size() && dt > 0) {
-#ifdef DEBUG_DROPZONEDRAG
-                       printf("BREAK AT TOP\n");
-#endif
+               if (current >= tavsize && dt > 0) {
                        break;
                }
-               if (current < 0 || current >= _time_axis_views.size()) {
+               if (current < 0 || current >= tavsize) {
                        continue;
                }
 
@@ -686,9 +733,6 @@ RegionDrag::apply_track_delta (const int start, const int delta, const int skip,
                }
 
                if (distance_only && current == start + delta) {
-#ifdef DEBUG_DROPZONEDRAG
-                       printf("BREAK AFTER DISTANCE\n");
-#endif
                        break;
                }
        }
@@ -702,33 +746,24 @@ RegionMotionDrag::y_movement_allowed (int delta_track, double delta_layer, int s
                return false;
        }
 
+       const int tavsize  = _time_axis_views.size();
        for (list<DraggingView>::const_iterator i = _views.begin(); i != _views.end(); ++i) {
                int n = apply_track_delta (i->time_axis_view, delta_track, skip_invisible);
-#ifdef DEBUG_DROPZONEDRAG
-               printf("Y MOVEMENT CHECK: from %d to %d skip: %d\n",
-                               i->time_axis_view, i->time_axis_view + delta_track,
-                               skip_invisible);
-#endif
-               assert (n < 0 || n >= _time_axis_views.size() || !_time_axis_views[n]->hidden());
+               assert (n < 0 || n >= tavsize || !_time_axis_views[n]->hidden());
 
-               if (i->time_axis_view < 0 || i->time_axis_view >= _time_axis_views.size()) {
+               if (i->time_axis_view < 0 || i->time_axis_view >= tavsize) {
                        /* already in the drop zone */
                        if (delta_track >= 0) {
-                               /* downward motion - might be OK if others are still not in the dropzone,
-                                  so check at the end of the loop if that is the case.
-                                */
+                               /* downward motion - OK if others are still not in the dropzone */
                                continue;
                        }
 
-                       /* upward motion - set n to the track we would end up in if motion
-                          is successful, and check validity below. */
-                       n = _time_axis_views.size() + delta_track;
                }
 
                if (n < 0) {
                        /* off the top */
                        return false;
-               } else if (n >= int (_time_axis_views.size())) {
+               } else if (n >= tavsize) {
                        /* downward motion into drop zone. That's fine. */
                        continue;
                }
@@ -771,34 +806,30 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
 
        assert (!_views.empty ());
 
-       if (first_move) {
-               if (_single_axis) {
-                       if (initially_vertical()) {
-                               _y_constrained = false;
-                               _x_constrained = true;
-                       } else {
-                               _y_constrained = true;
-                               _x_constrained = false;
-                       }
-               }
-       }
-
-#ifdef DEBUG_DROPZONEDRAG
-       printf("--------- LAST AXIS: %d\n", _last_pointer_time_axis_view);
-#endif
        /* Note: time axis views in this method are often expressed as an index into the _time_axis_views vector */
 
        /* Find the TimeAxisView that the pointer is now over */
-
-       pair<TimeAxisView*, double> const r = _editor->trackview_by_y_position (current_pointer_y ());
+       const double cur_y = current_pointer_y ();
+       pair<TimeAxisView*, double> const r = _editor->trackview_by_y_position (cur_y);
        TimeAxisView* tv = r.first;
 
-       if (!tv && current_pointer_y() < 0) {
+       if (!tv && cur_y < 0) {
                /* above trackview area, autoscroll hasn't moved us since last time, nothing to do */
                return;
        }
 
+       /* find drop-zone y-position */
+       Coord last_track_bottom_edge;
+       last_track_bottom_edge = 0;
+       for (std::vector<TimeAxisView*>::reverse_iterator t = _time_axis_views.rbegin(); t != _time_axis_views.rend(); ++t) {
+               if (!(*t)->hidden()) {
+                       last_track_bottom_edge = (*t)->canvas_display()->canvas_origin ().y + (*t)->effective_height();
+                       break;
+               }
+       }
+
        if (tv && tv->view()) {
+               /* the mouse is over a track */
                double layer = r.second;
 
                if (first_move && tv->view()->layer_display() == Stacked) {
@@ -808,15 +839,15 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
                /* Here's the current pointer position in terms of time axis view and layer */
                current_pointer_time_axis_view = find_time_axis_view (tv);
                assert(current_pointer_time_axis_view >= 0);
-#ifdef DEBUG_DROPZONEDRAG
-               printf("            On AXIS: %d\n", current_pointer_time_axis_view);
-#endif
 
                double const current_pointer_layer = tv->layer_display() == Overlaid ? 0 : layer;
 
                /* Work out the change in y */
 
-               if (_last_pointer_time_axis_view < 0) {
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (tv);
+               if (!rtv || !rtv->is_track()) {
+                       /* ignore busses early on. we can't move any regions on them */
+               } else if (_last_pointer_time_axis_view < 0) {
                        /* Was in the drop-zone, now over a track.
                         * Hence it must be an upward move (from the bottom)
                         *
@@ -824,12 +855,13 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
                         * move up the correct number of tracks from the bottom.
                         *
                         * This is necessary because steps may be skipped if
-                        * the bottom-most track is not a valid target,
+                        * the bottom-most track is not a valid target and/or
+                        * if there are hidden tracks at the bottom.
+                        * Hence the initial offset (_ddropzone) as well as the
+                        * last valid pointer position (_pdropzone) need to be
+                        * taken into account.
                         */
-#ifdef DEBUG_DROPZONEDRAG
-                       printf("MOVE OUT OF THE ZONE...\n");
-#endif
-                       delta_time_axis_view = current_pointer_time_axis_view - _time_axis_views.size ();
+                       delta_time_axis_view = current_pointer_time_axis_view - _time_axis_views.size () + _ddropzone - _pdropzone;
                } else {
                        delta_time_axis_view = current_pointer_time_axis_view - _last_pointer_time_axis_view;
                }
@@ -858,32 +890,44 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
                delta_layer = current_pointer_layer - _last_pointer_layer;
 
        }
-       /* for automation lanes, there is a TimeAxisView but no ->view() */
-       else if (!tv && current_pointer_y() >= 0 && _last_pointer_time_axis_view >= 0) {
-               /* Moving into the drop-zone..
-                *
-                * TODO allow moving further down in drop-zone:
-                * e.g. 2 Tracks, select a region on both of them.
-                *
-                * A) grab the upper, drag 2 down, both regions are in  the dropzone: all fine (works)
-                *
-                * B) grab the lower, drag 1 down, region (and mouse) are in dropzone, The End.
-                *    upper region is only down one track and cannot be moved into the zone.
-                *
-                * Proposed solution:
-                *  keep track of how many regions are in the DZ (private var),
-                *  also count from how many tracks the dragged-regions come from (first move)
-                *
-                *  if not all regions are in the DZ, keep going.
+       /* for automation lanes, there is a TimeAxisView but no ->view()
+        * if (!tv) -> dropzone
+        */
+       else if (!tv && cur_y >= 0 && _last_pointer_time_axis_view >= 0) {
+               /* Moving into the drop-zone.. */
+               delta_time_axis_view = _time_axis_views.size () - _last_pointer_time_axis_view;
+               /* delta_time_axis_view may not be sufficient to move into the DZ
+                * the mouse may enter it, but it may not be a valid move due to
+                * constraints.
                 *
-                *  Using 'default height' H for all dropzone regions will make things
-                *  a lot simpler: (number_of_DZ_entries * H + Pointer_YPOS - DZ_YPOS) / H.
-                *  (because at this point in time PlaylistDropzoneMap is not yet populated)
+                * -> remember the delta needed to move into the dropzone
                 */
-               delta_time_axis_view = _time_axis_views.size () - _last_pointer_time_axis_view;
-#ifdef DEBUG_DROPZONEDRAG
-               printf("INTO THE ZONE DELTA: %d\n", delta_time_axis_view);
-#endif
+               _ddropzone = delta_time_axis_view;
+               /* ..but subtract hidden tracks (or routes) at the bottom.
+                * we silently move mover them
+                */
+               _ddropzone -= apply_track_delta(_last_pointer_time_axis_view, delta_time_axis_view, 0, true)
+                             - _time_axis_views.size();
+       }
+       else if (!tv && cur_y >= 0 && _last_pointer_time_axis_view < 0) {
+               /* move around inside the zone.
+                * This allows to move further down until all regions are in the zone.
+                */
+               const double ptr_y = cur_y + _editor->get_trackview_group()->canvas_origin().y;
+               assert(ptr_y >= last_track_bottom_edge);
+               assert(_ddropzone > 0);
+
+               /* calculate mouse position in 'tracks' below last track. */
+               const double dzi_h = TimeAxisView::preset_height (HeightNormal);
+               uint32_t dzpos = _ddropzone + floor((1 + ptr_y - last_track_bottom_edge) / dzi_h);
+
+               if (dzpos > _pdropzone && _ndropzone < _ntracks) {
+                       // move further down
+                       delta_time_axis_view =  dzpos - _pdropzone;
+               } else if (dzpos < _pdropzone && _ndropzone > 0) {
+                       // move up inside the DZ
+                       delta_time_axis_view =  dzpos - _pdropzone;
+               }
        }
 
        /* Work out the change in x */
@@ -891,26 +935,37 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
        double const x_delta = compute_x_delta (event, &pending_region_position);
        _last_frame_position = pending_region_position;
 
-       /* calculate hidden tracks in current delta */
+       /* calculate hidden tracks in current y-axis delta */
        int delta_skip = 0;
-       if (_last_pointer_time_axis_view < 0) {
-               // Moving out of the zone, check for hidden tracks at the bottom.
+       if (_last_pointer_time_axis_view < 0 && _pdropzone > 0) {
+               /* The mouse is more than one track below the dropzone.
+                * distance calculation is not needed (and would not work, either
+                * because the dropzone is "packed").
+                *
+                * Except when [partially] moving regions out of dropzone in a large step.
+                * (the mouse may or may not remain in the DZ)
+                * Hidden tracks at the bottom of the TAV need to be skipped.
+                *
+                * This also handles the case if the mouse entered the DZ
+                * in a large step (exessive delta), either due to fast-movement,
+                * autoscroll, laggy UI. _ddropzone copensates for that (see "move into dz" above)
+                */
+               if (delta_time_axis_view < 0 && (int)_ddropzone - delta_time_axis_view >= (int)_pdropzone) {
+                       const int dt = delta_time_axis_view + (int)_pdropzone - (int)_ddropzone;
+                       assert(dt <= 0);
+                       delta_skip = apply_track_delta(_time_axis_views.size(), dt, 0, true)
+                               -_time_axis_views.size() - dt;
+               }
+       }
+       else if (_last_pointer_time_axis_view < 0) {
+               /* Moving out of the zone. Check for hidden tracks at the bottom. */
                delta_skip = apply_track_delta(_time_axis_views.size(), delta_time_axis_view, 0, true)
                             -_time_axis_views.size() - delta_time_axis_view;
-#ifdef DEBUG_DROPZONEDRAG
-               printf("NOW WHAT?? last: %d  delta %d || skip %d\n", _last_pointer_time_axis_view, delta_time_axis_view, delta_skip);
-#endif
        } else {
-               // calculate hidden tracks that are skipped by the pointer movement
+               /* calculate hidden tracks that are skipped by the pointer movement */
                delta_skip = apply_track_delta(_last_pointer_time_axis_view, delta_time_axis_view, 0, true)
                             - _last_pointer_time_axis_view
                             - delta_time_axis_view;
-#ifdef DEBUG_DROPZONEDRAG
-               printf("Drag from %d to %d || skip %d\n",
-                               _last_pointer_time_axis_view,
-                               _last_pointer_time_axis_view + delta_time_axis_view,
-                               delta_skip);
-#endif
        }
 
        /* Verify change in y */
@@ -919,9 +974,6 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
                delta_time_axis_view = 0;
                delta_layer = 0;
                delta_skip = 0;
-#ifdef DEBUG_DROPZONEDRAG
-               printf(" ** NOT ALLOWED\n");
-#endif
        }
 
        if (x_delta == 0 && (tv && tv->view() && delta_time_axis_view == 0) && delta_layer == 0 && !first_move) {
@@ -931,20 +983,9 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
                return;
        }
 
-       typedef pair<int,double> NewTrackIndexAndPosition;
-       typedef map<boost::shared_ptr<Playlist>,NewTrackIndexAndPosition> PlaylistDropzoneMap;
+       typedef map<boost::shared_ptr<Playlist>, double> PlaylistDropzoneMap;
        PlaylistDropzoneMap playlist_dropzone_map;
-       int biggest_drop_zone_offset = 0;
-
-       /* find drop-zone y-position */
-       Coord last_track_bottom_edge;
-       last_track_bottom_edge = 0;
-       for (std::vector<TimeAxisView*>::reverse_iterator t = _time_axis_views.rbegin(); t != _time_axis_views.rend(); ++t) {
-               if (!(*t)->hidden()) {
-                       last_track_bottom_edge = (*t)->canvas_display()->canvas_origin ().y + (*t)->effective_height();
-                       break;
-               }
-       }
+       _ndropzone = 0; // number of elements currently in the dropzone
 
        if (first_move) {
                /* sort views by time_axis.
@@ -952,6 +993,27 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
                 * of actual selection order
                 */
                _views.sort (DraggingViewSorter());
+
+               /* count number of distinct tracks of all regions
+                * being dragged, used for dropzone.
+                */
+               int prev_track = -1;
+               for (list<DraggingView>::const_iterator i = _views.begin(); i != _views.end(); ++i) {
+                       if (i->time_axis_view != prev_track) {
+                               prev_track = i->time_axis_view;
+                               ++_ntracks;
+                       }
+               }
+#ifndef NDEBUG
+               int spread =
+                       _views.back().time_axis_view -
+                       _views.front().time_axis_view;
+
+               spread -= apply_track_delta (_views.front().time_axis_view, spread, 0, true)
+                         -  _views.back().time_axis_view;
+
+               printf("Dragging region(s) from %d different track(s), max dist: %d\n", _ntracks, spread);
+#endif
        }
 
        for (list<DraggingView>::iterator i = _views.begin(); i != _views.end(); ++i) {
@@ -961,7 +1023,7 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
 
                y_delta = 0;
 
-               if (rv->region()->locked() || rv->region()->video_locked()) {
+               if (rv->region()->locked() || (rv->region()->video_locked() && !_ignore_video_lock)) {
                        continue;
                }
 
@@ -992,42 +1054,30 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
                        this_delta_layer = - i->layer;
                }
 
-               int this_delta_time_axis_view = delta_time_axis_view;
-               this_delta_time_axis_view = apply_track_delta(i->time_axis_view, delta_time_axis_view, delta_skip) - i->time_axis_view;
+               int this_delta_time_axis_view = apply_track_delta(i->time_axis_view, delta_time_axis_view, delta_skip) - i->time_axis_view;
 
                int track_index = i->time_axis_view + this_delta_time_axis_view;
                assert(track_index >= 0);
 
                if (track_index < 0 || track_index >= (int) _time_axis_views.size()) {
+                       /* Track is in the Dropzone */
+
                        i->time_axis_view = track_index;
-#ifdef DEBUG_DROPZONEDRAG
-                       printf("IN THE ZONE\n");
-#endif
-                       assert(i->time_axis_view >= _time_axis_views.size());
-                       if (current_pointer_y() >= 0) {
+                       assert(i->time_axis_view >= (int) _time_axis_views.size());
+                       if (cur_y >= 0) {
 
-                               int dzoffset;
-                               NewTrackIndexAndPosition ip;
+                               double yposition = 0;
                                PlaylistDropzoneMap::iterator pdz = playlist_dropzone_map.find (i->view->region()->playlist());
+                               rv->set_height (TimeAxisView::preset_height (HeightNormal));
+                               ++_ndropzone;
 
                                /* store index of each new playlist as a negative count, starting at -1 */
 
                                if (pdz == playlist_dropzone_map.end()) {
-
-                                       /* TODO
-                                        * retain the ordering top -> bottom in the drop-zone
-                                        * this can be done by sorting the regions according to
-                                        * i->time_axis_view Y, prior to iterating over DraggingView
-                                        */
-
-                                       int n = playlist_dropzone_map.size() + 1;
-
                                        /* compute where this new track (which doesn't exist yet) will live
                                           on the y-axis.
                                        */
-
-                                       ip.first = -n;  /* in time axis units, negative to signify "in drop zone " */
-                                       ip.second = last_track_bottom_edge; /* where to place the top edge of the regionview */
+                                       yposition = last_track_bottom_edge; /* where to place the top edge of the regionview */
 
                                        /* How high is this region view ? */
 
@@ -1040,17 +1090,14 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
 
                                        last_track_bottom_edge += bbox.height();
 
-                                       playlist_dropzone_map.insert (make_pair (i->view->region()->playlist(), ip));
-                                       dzoffset = -n;
+                                       playlist_dropzone_map.insert (make_pair (i->view->region()->playlist(), yposition));
 
                                } else {
-                                       ip = pdz->second;
-                                       dzoffset = ip.first;
+                                       yposition = pdz->second;
                                }
 
                                /* values are zero or negative, hence the use of min() */
-                               biggest_drop_zone_offset = min (biggest_drop_zone_offset, dzoffset);
-                               y_delta = ip.second - rv->get_canvas_group()->canvas_origin().y;
+                               y_delta = yposition - rv->get_canvas_group()->canvas_origin().y;
                        }
 
                } else {
@@ -1127,26 +1174,54 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
                show_verbose_cursor_time (_last_frame_position);
        }
 
+       /* keep track of pointer movement */
        if (tv) {
-
                /* the pointer is currently over a time axis view */
 
                if (_last_pointer_time_axis_view < 0) {
-
-                       /* last motion event was not over a time axis view */
-
+                       /* last motion event was not over a time axis view
+                        * or last y-movement out of the dropzone was not valid
+                        */
+                       int dtz = 0;
                        if (delta_time_axis_view < 0) {
-                               /* was in the drop zone, moving up */
+                               /* in the drop zone, moving up */
+
+                               /* _pdropzone is the last known pointer y-axis position inside the DZ.
+                                * We do not use negative _last_pointer_time_axis_view because
+                                * the dropzone is "packed" (the actual track offset is ignored)
+                                *
+                                * As opposed to the actual number
+                                * of elements in the dropzone (_ndropzone)
+                                * _pdropzone is not constrained. This is necessary
+                                * to allow moving multiple regions with y-distance
+                                * into the DZ.
+                                *
+                                * There can be 0 elements in the dropzone,
+                                * even though the drag-pointer is inside the DZ.
+                                *
+                                * example:
+                                * [ Audio-track, Midi-track, Audio-track, DZ ]
+                                * move regions from both audio tracks at the same time into the
+                                * DZ by grabbing the region in the bottom track.
+                                */
                                assert(current_pointer_time_axis_view >= 0);
-                               _last_pointer_time_axis_view = current_pointer_time_axis_view;
-                       } else {
-                               /* was in the drop zone, moving down ... not possible */
+                               dtz = std::min((int)_pdropzone, (int)_ddropzone - delta_time_axis_view);
+                               _pdropzone -= dtz;
                        }
 
+                       /* only move out of the zone if the movement is OK */
+                       if (_pdropzone == 0 && delta_time_axis_view != 0) {
+                               assert(delta_time_axis_view < 0);
+                               _last_pointer_time_axis_view = current_pointer_time_axis_view;
+                               /* if all logic and maths are correct, there is no need to assign the 'current' pointer.
+                                * the current position can be calculated as follows:
+                                */
+                               // a well placed oofus attack can still throw this off.
+                               // likley auto-scroll related, printf() debugging may tell, commented out for now.
+                               //assert (current_pointer_time_axis_view == _time_axis_views.size() - dtz + _ddropzone + delta_time_axis_view);
+                       }
                } else {
-
                        /* last motion event was also over a time axis view */
-
                        _last_pointer_time_axis_view += delta_time_axis_view;
                        assert(_last_pointer_time_axis_view >= 0);
                }
@@ -1154,8 +1229,9 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
        } else {
 
                /* the pointer is not over a time axis view */
-
-               _last_pointer_time_axis_view = biggest_drop_zone_offset;
+               assert ((delta_time_axis_view > 0) || (((int)_pdropzone) >= (delta_skip - delta_time_axis_view)));
+               _pdropzone += delta_time_axis_view - delta_skip;
+               _last_pointer_time_axis_view = -1; // <0 : we're in the zone, value does not matter.
        }
 
        _last_pointer_layer += delta_layer;
@@ -1165,13 +1241,13 @@ void
 RegionMoveDrag::motion (GdkEvent* event, bool first_move)
 {
        if (_copy && first_move) {
-
-               if (_x_constrained) {
+               if (_x_constrained && !_brushing) {
                        _editor->begin_reversible_command (Operations::fixed_time_region_copy);
-               } else {
+               } else if (!_brushing) {
                        _editor->begin_reversible_command (Operations::region_copy);
+               } else if (_brushing) {
+                       _editor->begin_reversible_command (Operations::drag_region_brush);
                }
-
                /* duplicate the regionview(s) and region(s) */
 
                list<DraggingView> new_regionviews;
@@ -1229,14 +1305,14 @@ RegionMoveDrag::motion (GdkEvent* event, bool first_move)
                }
 
        } else if (!_copy && first_move) {
-
-               if (_x_constrained) {
+               if (_x_constrained && !_brushing) {
                        _editor->begin_reversible_command (_("fixed time region drag"));
-               } else {
+               } else if (!_brushing) {
                        _editor->begin_reversible_command (Operations::region_drag);
+               } else if (_brushing) {
+                       _editor->begin_reversible_command (Operations::drag_region_brush);
                }
        }
-
        RegionMotionDrag::motion (event, first_move);
 }
 
@@ -1272,14 +1348,6 @@ RegionMoveDrag::finished (GdkEvent* ev, bool movement_occurred)
                return;
        }
 
-       /* reverse this here so that we have the correct logic to finalize
-          the drag.
-       */
-
-       if (Config->get_edit_mode() == Lock) {
-               _x_constrained = !_x_constrained;
-       }
-
        assert (!_views.empty ());
 
        /* We might have hidden region views so that they weren't visible during the drag
@@ -1325,7 +1393,11 @@ RegionMoveDrag::create_destination_time_axis (boost::shared_ptr<Region> region,
        try {
                if (boost::dynamic_pointer_cast<AudioRegion> (region)) {
                        list<boost::shared_ptr<AudioTrack> > audio_tracks;
-                       audio_tracks = _editor->session()->new_audio_track (region->n_channels(), region->n_channels(), ARDOUR::Normal, 0, 1, region->name());
+                       uint32_t output_chan = region->n_channels();
+                       if ((Config->get_output_auto_connect() & AutoConnectMaster) && _editor->session()->master_out()) {
+                               output_chan =  _editor->session()->master_out()->n_inputs().n_audio();
+                       }
+                       audio_tracks = _editor->session()->new_audio_track (region->n_channels(), output_chan, ARDOUR::Normal, 0, 1, region->name());
                        RouteTimeAxisView* rtav = _editor->axis_view_from_route (audio_tracks.front());
                        if (rtav) {
                                rtav->set_height (original->current_height());
@@ -1373,7 +1445,7 @@ RegionMoveDrag::finished_copy (bool const changed_position, bool const /*changed
 
                RouteTimeAxisView* dest_rtv = 0;
 
-               if (i->view->region()->locked() || i->view->region()->video_locked()) {
+               if (i->view->region()->locked() || (i->view->region()->video_locked() && !_ignore_video_lock)) {
                        continue;
                }
 
@@ -1385,7 +1457,7 @@ RegionMoveDrag::finished_copy (bool const changed_position, bool const /*changed
                        where = i->view->region()->position();
                }
 
-               if (i->time_axis_view < 0 || i->time_axis_view >= _time_axis_views.size()) {
+               if (i->time_axis_view < 0 || i->time_axis_view >= (int)_time_axis_views.size()) {
                        /* dragged to drop zone */
 
                        PlaylistMapping::iterator pm;
@@ -1448,12 +1520,6 @@ RegionMoveDrag::finished_no_copy (
        set<RouteTimeAxisView*> views_to_update;
        RouteTimeAxisView* new_time_axis_view = 0;
 
-       if (_brushing) {
-               /* all changes were made during motion event handlers */
-               _editor->commit_reversible_command ();
-               return;
-       }
-
        typedef map<boost::shared_ptr<Playlist>, RouteTimeAxisView*> PlaylistMapping;
        PlaylistMapping playlist_mapping;
 
@@ -1462,12 +1528,12 @@ RegionMoveDrag::finished_no_copy (
                RegionView* rv = i->view;
                RouteTimeAxisView* dest_rtv = 0;
 
-               if (rv->region()->locked() || rv->region()->video_locked()) {
+               if (rv->region()->locked() || (rv->region()->video_locked() && !_ignore_video_lock)) {
                        ++i;
                        continue;
                }
 
-               if (i->time_axis_view < 0 || i->time_axis_view >= _time_axis_views.size()) {
+               if (i->time_axis_view < 0 || i->time_axis_view >= (int)_time_axis_views.size()) {
                        /* dragged to drop zone */
 
                        PlaylistMapping::iterator pm;
@@ -1567,7 +1633,6 @@ RegionMoveDrag::finished_no_copy (
                        }
 
                        rv->region()->set_position (where);
-
                        _editor->session()->add_command (new StatefulDiffCommand (rv->region()));
                }
 
@@ -1616,7 +1681,7 @@ RegionMoveDrag::finished_no_copy (
 
        /* write commands for the accumulated diffs for all our modified playlists */
        add_stateful_diff_commands_for_playlists (modified_playlists);
-
+       /* applies to _brushing */
        _editor->commit_reversible_command ();
 
        /* We have futzed with the layering of canvas items on our streamviews.
@@ -1773,6 +1838,7 @@ RegionMotionDrag::aborted (bool)
 RegionMoveDrag::RegionMoveDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<RegionView*> const & v, bool b, bool c)
        : RegionMotionDrag (e, i, p, v, b)
        , _copy (c)
+       , _new_region_view (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New RegionMoveDrag\n");
 
@@ -1814,7 +1880,7 @@ void
 RegionInsertDrag::finished (GdkEvent *, bool)
 {
        int pos = _views.front().time_axis_view;
-       assert(pos >= 0 && pos < _time_axis_views.size());
+       assert(pos >= 0 && pos < (int)_time_axis_views.size());
 
        RouteTimeAxisView* dest_rtv = dynamic_cast<RouteTimeAxisView*> (_time_axis_views[pos]);
 
@@ -2237,13 +2303,14 @@ void
 RegionCreateDrag::motion (GdkEvent* event, bool first_move)
 {
        if (first_move) {
-               _region = add_midi_region (_view);
+               _editor->begin_reversible_command (_("create region"));
+               _region = add_midi_region (_view, false);
                _view->playlist()->freeze ();
        } else {
                if (_region) {
                        framepos_t const f = adjusted_current_frame (event);
                        if (f < grab_frame()) {
-                               _region->set_position (f);
+                               _region->set_initial_position (f);
                        }
 
                        /* Don't use a zero-length region, and subtract 1 frame from the snapped length
@@ -2263,9 +2330,10 @@ void
 RegionCreateDrag::finished (GdkEvent*, bool movement_occurred)
 {
        if (!movement_occurred) {
-               add_midi_region (_view);
+               add_midi_region (_view, true);
        } else {
                _view->playlist()->thaw ();
+               _editor->commit_reversible_command();
        }
 }
 
@@ -2282,6 +2350,10 @@ RegionCreateDrag::aborted (bool)
 NoteResizeDrag::NoteResizeDrag (Editor* e, ArdourCanvas::Item* i)
        : Drag (e, i)
        , region (0)
+       , relative (false)
+       , at_front (true)
+       , _was_selected (false)
+       , _snap_delta (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New NoteResizeDrag\n");
 }
@@ -2306,64 +2378,171 @@ NoteResizeDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*ignored*/)
 
        region = &cnote->region_view();
 
+       double temp;
+       temp = region->snap_to_pixel (cnote->x0 (), true);
+       _snap_delta = temp - cnote->x0 ();
+
        _item->grab ();
 
-       if (event->motion.state & Keyboard::PrimaryModifier) {
+       if (event->motion.state & ArdourKeyboard::note_size_relative_modifier ()) {
                relative = false;
        } else {
                relative = true;
        }
-
        MidiRegionSelection& ms (_editor->get_selection().midi_regions);
-
        if (ms.size() > 1) {
                /* has to be relative, may make no sense otherwise */
                relative = true;
        }
 
-       /* select this note; if it is already selected, preserve the existing selection,
-          otherwise make this note the only one selected.
-       */
-       region->note_selected (cnote, cnote->selected ());
+       if (!(_was_selected = cnote->selected())) {
 
-       _editor->begin_reversible_command (_("resize notes"));
+               /* tertiary-click means extend selection - we'll do that on button release,
+                  so don't add it here, because otherwise we make it hard to figure
+                  out the "extend-to" range.
+               */
 
-       for (MidiRegionSelection::iterator r = ms.begin(); r != ms.end(); ) {
-               MidiRegionSelection::iterator next;
-               next = r;
-               ++next;
-               MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*r);
-               if (mrv) {
-                       mrv->begin_resizing (at_front);
+               bool extend = Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier);
+
+               if (!extend) {
+                       bool add = Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier);
+
+                       if (add) {
+                               region->note_selected (cnote, true);
+                       } else {
+                               _editor->get_selection().clear_points();
+                               region->unique_select (cnote);
+                       }
                }
-               r = next;
        }
 }
 
 void
-NoteResizeDrag::motion (GdkEvent* /*event*/, bool /*first_move*/)
+NoteResizeDrag::motion (GdkEvent* event, bool first_move)
 {
        MidiRegionSelection& ms (_editor->get_selection().midi_regions);
+       if (first_move) {
+               _editor->begin_reversible_command (_("resize notes"));
+
+               for (MidiRegionSelection::iterator r = ms.begin(); r != ms.end(); ) {
+                       MidiRegionSelection::iterator next;
+                       next = r;
+                       ++next;
+                       MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*r);
+                       if (mrv) {
+                               mrv->begin_resizing (at_front);
+                       }
+                       r = next;
+               }
+       }
+
        for (MidiRegionSelection::iterator r = ms.begin(); r != ms.end(); ++r) {
                NoteBase* nb = reinterpret_cast<NoteBase*> (_item->get_data ("notebase"));
                assert (nb);
                MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*r);
                if (mrv) {
-                       mrv->update_resizing (nb, at_front, _drags->current_pointer_x() - grab_x(), relative);
+                       double sd = 0.0;
+                       bool snap = true;
+                       bool apply_snap_delta = ArdourKeyboard::indicates_snap_delta (event->button.state);
+
+                       if (ArdourKeyboard::indicates_snap (event->button.state)) {
+                               if (_editor->snap_mode () != SnapOff) {
+                                       snap = false;
+                               }
+                       } else {
+                               if (_editor->snap_mode () == SnapOff) {
+                                       snap = false;
+                                       /* inverted logic here - we;re in snapoff but we've pressed the snap delta modifier */
+                                       if (apply_snap_delta) {
+                                               snap = true;
+                                       }
+                               }
+                       }
+
+                       if (apply_snap_delta) {
+                               sd = _snap_delta;
+                       }
+
+                       mrv->update_resizing (nb, at_front, _drags->current_pointer_x() - grab_x(), relative, sd, snap);
                }
        }
 }
 
 void
-NoteResizeDrag::finished (GdkEvent*, bool /*movement_occurred*/)
+NoteResizeDrag::finished (GdkEvent* event, bool movement_occurred)
 {
+       if (!movement_occurred) {
+               /* no motion - select note */
+               NoteBase* cnote = reinterpret_cast<NoteBase*> (_item->get_data ("notebase"));
+               if (_editor->current_mouse_mode() == Editing::MouseContent ||
+                   _editor->current_mouse_mode() == Editing::MouseDraw) {
+
+                       bool changed = false;
+
+                       if (_was_selected) {
+                               bool add = Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier);
+                               if (add) {
+                                       region->note_deselected (cnote);
+                                       changed = true;
+                               } else {
+                                       _editor->get_selection().clear_points();
+                                       region->unique_select (cnote);
+                                       changed = true;
+                               }
+                       } else {
+                               bool extend = Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier);
+                               bool add = Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier);
+
+                               if (!extend && !add && region->selection_size() > 1) {
+                                       _editor->get_selection().clear_points();
+                                       region->unique_select (cnote);
+                                       changed = true;
+                               } else if (extend) {
+                                       region->note_selected (cnote, true, true);
+                                       changed = true;
+                               } else {
+                                       /* it was added during button press */
+                                       changed = true;
+                               }
+                       }
+
+                       if (changed) {
+                               _editor->begin_reversible_selection_op(X_("Resize Select Note Release"));
+                               _editor->commit_reversible_selection_op();
+                       }
+               }
+
+               return;
+       }
+
        MidiRegionSelection& ms (_editor->get_selection().midi_regions);
        for (MidiRegionSelection::iterator r = ms.begin(); r != ms.end(); ++r) {
                NoteBase* nb = reinterpret_cast<NoteBase*> (_item->get_data ("notebase"));
                assert (nb);
                MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(*r);
+               double sd = 0.0;
+               bool snap = true;
+               bool apply_snap_delta = ArdourKeyboard::indicates_snap_delta (event->button.state);
                if (mrv) {
-                       mrv->commit_resizing (nb, at_front, _drags->current_pointer_x() - grab_x(), relative);
+                       if (ArdourKeyboard::indicates_snap (event->button.state)) {
+                               if (_editor->snap_mode () != SnapOff) {
+                                       snap = false;
+                               }
+                       } else {
+                               if (_editor->snap_mode () == SnapOff) {
+                                       snap = false;
+                                       /* inverted logic here - we;re in snapoff but we've pressed the snap delta modifier */
+                                       if (apply_snap_delta) {
+                                               snap = true;
+                                       }
+                               }
+                       }
+
+                       if (apply_snap_delta) {
+                               sd = _snap_delta;
+                       }
+
+                       mrv->commit_resizing (nb, at_front, _drags->current_pointer_x() - grab_x(), relative, sd, snap);
                }
        }
 
@@ -2399,11 +2578,15 @@ VideoTimeLineDrag::VideoTimeLineDrag (Editor* e, ArdourCanvas::Item* i)
        _editor->get_regions_after(rs, (framepos_t) 0, empty);
        std::list<RegionView*> views = rs.by_layer();
 
+       _stuck = false;
        for (list<RegionView*>::iterator i = views.begin(); i != views.end(); ++i) {
                RegionView* rv = (*i);
                if (!rv->region()->video_locked()) {
                        continue;
                }
+               if (rv->region()->locked()) {
+                       _stuck = true;
+               }
                _views.push_back (AVDraggingView (rv));
        }
 }
@@ -2416,6 +2599,16 @@ VideoTimeLineDrag::start_grab (GdkEvent* event, Gdk::Cursor*)
                return;
        }
 
+       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::ModifierMask (Keyboard::TertiaryModifier))) {
+               _stuck = false;
+               _views.clear();
+       }
+
+       if (_stuck) {
+               show_verbose_cursor_text (_("One or more Audio Regions\nare both Locked and\nLocked to Video.\nThe video cannot me moved."));
+               return;
+       }
+
        _startdrag_video_offset=ARDOUR_UI::instance()->video_timeline->get_offset();
        _max_backwards_drag = (
                          ARDOUR_UI::instance()->video_timeline->get_duration()
@@ -2446,6 +2639,10 @@ VideoTimeLineDrag::motion (GdkEvent* event, bool first_move)
        if (ARDOUR_UI::instance()->video_timeline->is_offset_locked()) {
                return;
        }
+       if (_stuck) {
+               show_verbose_cursor_text (_("One or more Audio Regions\nare both Locked and\nLocked to Video.\nThe video cannot me moved."));
+               return;
+       }
 
        framecnt_t dt = adjusted_current_frame (event) - raw_grab_frame() + _pointer_frame_offset;
        dt = ARDOUR_UI::instance()->video_timeline->quantify_frames_to_apv(_startdrag_video_offset+dt) - _startdrag_video_offset;
@@ -2492,6 +2689,9 @@ VideoTimeLineDrag::finished (GdkEvent * /*event*/, bool movement_occurred)
        if (ARDOUR_UI::instance()->video_timeline->is_offset_locked()) {
                return;
        }
+       if (_stuck) {
+               return;
+       }
 
        if (!movement_occurred || ! _editor->session()) {
                return;
@@ -2539,6 +2739,7 @@ VideoTimeLineDrag::aborted (bool)
 
 TrimDrag::TrimDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<RegionView*> const & v, bool preserve_fade_anchor)
        : RegionDrag (e, i, p, v)
+       , _operation (StartTrim)
        , _preserve_fade_anchor (preserve_fade_anchor)
        , _jump_position_when_done (false)
 {
@@ -2561,8 +2762,9 @@ TrimDrag::start_grab (GdkEvent* event, Gdk::Cursor*)
        framecnt_t const region_length = (framecnt_t) (_primary->region()->length() / speed);
 
        framepos_t const pf = adjusted_current_frame (event);
+       setup_snap_delta (region_start);
 
-       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
+       if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::trim_contents_modifier ())) {
                /* Move the contents of the region around without changing the region bounds */
                _operation = ContentsTrim;
                Drag::start_grab (event, _editor->cursors()->trimmer);
@@ -2571,8 +2773,7 @@ TrimDrag::start_grab (GdkEvent* event, Gdk::Cursor*)
                if (pf < (region_start + region_length/2)) {
                        /* closer to front */
                        _operation = StartTrim;
-
-                       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
+                       if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::trim_anchored_modifier ())) {
                                Drag::start_grab (event, _editor->cursors()->anchored_left_side_trim);
                        } else {
                                Drag::start_grab (event, _editor->cursors()->left_side_trim);
@@ -2580,27 +2781,25 @@ TrimDrag::start_grab (GdkEvent* event, Gdk::Cursor*)
                } else {
                        /* closer to end */
                        _operation = EndTrim;
-                       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
+                       if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::trim_anchored_modifier ())) {
                                Drag::start_grab (event, _editor->cursors()->anchored_right_side_trim);
                        } else {
                                Drag::start_grab (event, _editor->cursors()->right_side_trim);
                        }
                }
        }
-
-       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
+       /* jump trim disabled for now
+       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::trim_jump_modifier ())) {
                _jump_position_when_done = true;
        }
+       */
 
        switch (_operation) {
        case StartTrim:
                show_verbose_cursor_time (region_start);
-               for (list<DraggingView>::iterator i = _views.begin(); i != _views.end(); ++i) {
-                       i->view->trim_front_starting ();
-               }
                break;
        case EndTrim:
-               show_verbose_cursor_time (region_end);
+               show_verbose_cursor_duration (region_start, region_end);
                break;
        case ContentsTrim:
                show_verbose_cursor_time (pf);
@@ -2626,8 +2825,8 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
        if (tv && tv->is_track()) {
                speed = tv->track()->speed();
        }
-
-       framecnt_t dt = adjusted_current_frame (event) - raw_grab_frame () + _pointer_frame_offset;
+       framecnt_t adj_frame = adjusted_frame (_drags->current_pointer_frame () + snap_delta (event->button.state), event, true);
+       framecnt_t dt = adj_frame - raw_grab_frame () + _pointer_frame_offset - snap_delta (event->button.state);
 
        if (first_move) {
 
@@ -2655,6 +2854,10 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
                        rv->enable_display (false);
                        rv->region()->playlist()->clear_owned_changes ();
 
+                       if (_operation == StartTrim) {
+                               rv->trim_front_starting ();
+                       }
+
                        AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (rv);
 
                        if (arv) {
@@ -2673,7 +2876,7 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
 
        bool non_overlap_trim = false;
 
-       if (event && Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
+       if (event && Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::trim_overlap_modifier ())) {
                non_overlap_trim = true;
        }
 
@@ -2759,7 +2962,7 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
                show_verbose_cursor_time ((framepos_t) (rv->region()->position() / speed));
                break;
        case EndTrim:
-               show_verbose_cursor_time ((framepos_t) (rv->region()->last_frame() / speed));
+               show_verbose_cursor_duration ((framepos_t)  rv->region()->position() / speed, (framepos_t) rv->region()->last_frame() / speed);
                break;
        case ContentsTrim:
                // show_verbose_cursor_time (frame_delta);
@@ -2767,7 +2970,6 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
        }
 }
 
-
 void
 TrimDrag::finished (GdkEvent* event, bool movement_occurred)
 {
@@ -2859,14 +3061,12 @@ TrimDrag::finished (GdkEvent* event, bool movement_occurred)
 
        } else {
                /* no mouse movement */
-               _editor->point_trim (event, adjusted_current_frame (event));
+               if (adjusted_current_frame (event) != adjusted_frame (_drags->current_pointer_frame(), event, false)) {
+                       _editor->point_trim (event, adjusted_current_frame (event));
+               }
        }
 
        for (list<DraggingView>::const_iterator i = _views.begin(); i != _views.end(); ++i) {
-               if (_operation == StartTrim) {
-                       i->view->trim_front_ending ();
-               }
-
                i->view->region()->resume_property_changes ();
        }
 }
@@ -2917,6 +3117,7 @@ TrimDrag::setup_pointer_frame_offset ()
 MeterMarkerDrag::MeterMarkerDrag (Editor* e, ArdourCanvas::Item* i, bool c)
        : Drag (e, i),
          _copy (c)
+       , before_state (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New MeterMarkerDrag\n");
        _marker = reinterpret_cast<MeterMarker*> (_item->get_data ("marker"));
@@ -2962,7 +3163,7 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
                _marker = new MeterMarker (
                        *_editor,
                        *_editor->meter_group,
-                       ARDOUR_UI::config()->color ("meter marker"),
+                       UIConfiguration::instance().color ("meter marker"),
                        name,
                        *new MeterSection (_marker->meter())
                );
@@ -3004,7 +3205,7 @@ MeterMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
        Timecode::BBT_Time when;
 
        TempoMap& map (_editor->session()->tempo_map());
-       map.bbt_time (last_pointer_frame(), when);
+       map.bbt_time (_marker->position(), when);
 
        if (_copy == true) {
                _editor->begin_reversible_command (_("copy meter mark"));
@@ -3046,12 +3247,14 @@ MeterMarkerDrag::aborted (bool moved)
 }
 
 TempoMarkerDrag::TempoMarkerDrag (Editor* e, ArdourCanvas::Item* i, bool c)
-       : Drag (e, i),
-         _copy (c)
+       : Drag (e, i)
+       , _copy (c)
+       , before_state (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New TempoMarkerDrag\n");
 
        _marker = reinterpret_cast<TempoMarker*> (_item->get_data ("marker"));
+       _real_section = &_marker->tempo();
        assert (_marker);
 }
 
@@ -3093,7 +3296,7 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
                _marker = new TempoMarker (
                        *_editor,
                        *_editor->tempo_group,
-                       ARDOUR_UI::config()->color ("tempo marker"),
+                       UIConfiguration::instance().color ("tempo marker"),
                        name,
                        *new TempoSection (_marker->tempo())
                        );
@@ -3102,16 +3305,20 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
                swap_grab (&_marker->the_item(), 0, GDK_CURRENT_TIME);
 
                if (!_copy) {
+                       _editor->begin_reversible_command (_("move tempo mark"));
                        TempoMap& map (_editor->session()->tempo_map());
                        /* get current state */
                        before_state = &map.get_state();
                        /* remove the section while we drag it */
-                       map.remove_tempo (section, true);
+                       //map.remove_tempo (section, true);
                }
        }
 
-       framepos_t const pf = adjusted_current_frame (event);
+       framepos_t const pf = adjusted_current_frame (event, false);
+       TempoMap& map (_editor->session()->tempo_map());
        _marker->set_position (pf);
+       map.gui_set_tempo_frame (*_real_section, pf);
+
        show_verbose_cursor_time (pf);
 }
 
@@ -3132,7 +3339,7 @@ TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
        motion (event, false);
 
        TempoMap& map (_editor->session()->tempo_map());
-       framepos_t beat_time = map.round_to_beat (last_pointer_frame(), RoundNearest);
+       framepos_t beat_time = map.round_to_beat (_marker->position(), RoundNearest);
        Timecode::BBT_Time when;
 
        map.bbt_time (beat_time, when);
@@ -3140,15 +3347,14 @@ TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
        if (_copy == true) {
                _editor->begin_reversible_command (_("copy tempo mark"));
                XMLNode &before = map.get_state();
-               map.add_tempo (_marker->tempo(), when);
+               map.add_tempo (_marker->tempo(), when, _marker->tempo().type());
                XMLNode &after = map.get_state();
                _editor->session()->add_command (new MementoCommand<TempoMap>(map, &before, &after));
                _editor->commit_reversible_command ();
 
        } else {
-               _editor->begin_reversible_command (_("move tempo mark"));
                /* we removed it before, so add it back now */
-               map.add_tempo (_marker->tempo(), when);
+               map.replace_tempo (*_real_section, _marker->tempo().beats_per_minute() , when, _marker->tempo().type());
                XMLNode &after = map.get_state();
                _editor->session()->add_command (new MementoCommand<TempoMap>(map, before_state, &after));
                _editor->commit_reversible_command ();
@@ -3166,7 +3372,7 @@ TempoMarkerDrag::aborted (bool moved)
        if (moved) {
                TempoMap& map (_editor->session()->tempo_map());
                /* we removed it before, so add it back now */
-               map.add_tempo (_marker->tempo(), _marker->tempo().start());
+               map.add_tempo (_marker->tempo(), _marker->tempo().start(), _marker->tempo().type());
                // delete the dummy marker we used for visual representation while moving.
                // a new visual marker will show up automatically.
                delete _marker;
@@ -3177,6 +3383,7 @@ CursorDrag::CursorDrag (Editor* e, EditorCursor& c, bool s)
        : Drag (e, &c.track_canvas_item(), false)
        , _cursor (c)
        , _stop (s)
+       , _grab_zoom (0.0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New CursorDrag\n");
 }
@@ -3188,6 +3395,10 @@ CursorDrag::CursorDrag (Editor* e, EditorCursor& c, bool s)
 void
 CursorDrag::fake_locate (framepos_t t)
 {
+       if (_editor->session () == 0) {
+               return;
+       }
+
        _editor->playhead_cursor->set_position (t);
 
        Session* s = _editor->session ();
@@ -3211,10 +3422,11 @@ void
 CursorDrag::start_grab (GdkEvent* event, Gdk::Cursor* c)
 {
        Drag::start_grab (event, c);
+       setup_snap_delta (_editor->playhead_cursor->current_frame ());
 
        _grab_zoom = _editor->samples_per_pixel;
 
-       framepos_t where = _editor->canvas_event_sample (event);
+       framepos_t where = _editor->canvas_event_sample (event) + snap_delta (event->button.state);
 
        _editor->snap_to_with_modifier (where, event);
 
@@ -3252,15 +3464,16 @@ CursorDrag::start_grab (GdkEvent* event, Gdk::Cursor* c)
                }
        }
 
-       fake_locate (where);
+       fake_locate (where - snap_delta (event->button.state));
 }
 
 void
 CursorDrag::motion (GdkEvent* event, bool)
 {
-       framepos_t const adjusted_frame = adjusted_current_frame (event);
-       if (adjusted_frame != last_pointer_frame()) {
-               fake_locate (adjusted_frame);
+       framepos_t where = _editor->canvas_event_sample (event) + snap_delta (event->button.state);
+       _editor->snap_to_with_modifier (where, event);
+       if (where != last_pointer_frame()) {
+               fake_locate (where - snap_delta (event->button.state));
        }
 }
 
@@ -3311,6 +3524,7 @@ FadeInDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
 
        AudioRegionView* arv = dynamic_cast<AudioRegionView*> (_primary);
        boost::shared_ptr<AudioRegion> const r = arv->audio_region ();
+       setup_snap_delta (r->position ());
 
        show_verbose_cursor_duration (r->position(), r->position() + r->fade_in()->back()->when, 32);
 }
@@ -3327,7 +3541,11 @@ void
 FadeInDrag::motion (GdkEvent* event, bool)
 {
        framecnt_t fade_length;
-       framepos_t const pos = adjusted_current_frame (event);
+
+       framepos_t pos = _editor->canvas_event_sample (event) + snap_delta (event->button.state);
+       _editor->snap_to_with_modifier (pos, event);
+       pos -= snap_delta (event->button.state);
+
        boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (_primary->region ());
 
        if (pos < (region->position() + 64)) {
@@ -3360,8 +3578,9 @@ FadeInDrag::finished (GdkEvent* event, bool movement_occurred)
        }
 
        framecnt_t fade_length;
-
-       framepos_t const pos = adjusted_current_frame (event);
+       framepos_t pos = _editor->canvas_event_sample (event) + snap_delta (event->button.state);
+       _editor->snap_to_with_modifier (pos, event);
+       pos -= snap_delta (event->button.state);
 
        boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (_primary->region ());
 
@@ -3373,7 +3592,7 @@ FadeInDrag::finished (GdkEvent* event, bool movement_occurred)
                fade_length = pos - region->position();
        }
 
-       _editor->begin_reversible_command (_("change fade in length"));
+       bool in_command = false;
 
        for (list<DraggingView>::iterator i = _views.begin(); i != _views.end(); ++i) {
 
@@ -3389,11 +3608,17 @@ FadeInDrag::finished (GdkEvent* event, bool movement_occurred)
                tmp->audio_region()->set_fade_in_length (fade_length);
                tmp->audio_region()->set_fade_in_active (true);
 
+               if (!in_command) {
+                       _editor->begin_reversible_command (_("change fade in length"));
+                       in_command = true;
+               }
                XMLNode &after = alist->get_state();
                _editor->session()->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
        }
 
-       _editor->commit_reversible_command ();
+       if (in_command) {
+               _editor->commit_reversible_command ();
+       }
 }
 
 void
@@ -3423,6 +3648,7 @@ FadeOutDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
 
        AudioRegionView* arv = dynamic_cast<AudioRegionView*> (_primary);
        boost::shared_ptr<AudioRegion> r = arv->audio_region ();
+       setup_snap_delta (r->last_frame ());
 
        show_verbose_cursor_duration (r->last_frame() - r->fade_out()->back()->when, r->last_frame());
 }
@@ -3440,7 +3666,9 @@ FadeOutDrag::motion (GdkEvent* event, bool)
 {
        framecnt_t fade_length;
 
-       framepos_t const pos = adjusted_current_frame (event);
+       framepos_t pos = _editor->canvas_event_sample (event) + snap_delta (event->button.state);
+       _editor->snap_to_with_modifier (pos, event);
+       pos -= snap_delta (event->button.state);
 
        boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (_primary->region ());
 
@@ -3475,7 +3703,9 @@ FadeOutDrag::finished (GdkEvent* event, bool movement_occurred)
 
        framecnt_t fade_length;
 
-       framepos_t const pos = adjusted_current_frame (event);
+       framepos_t pos = _editor->canvas_event_sample (event) + snap_delta (event->button.state);
+       _editor->snap_to_with_modifier (pos, event);
+       pos -= snap_delta (event->button.state);
 
        boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (_primary->region ());
 
@@ -3487,7 +3717,7 @@ FadeOutDrag::finished (GdkEvent* event, bool movement_occurred)
                fade_length = region->last_frame() - pos;
        }
 
-       _editor->begin_reversible_command (_("change fade out length"));
+       bool in_command = false;
 
        for (list<DraggingView>::iterator i = _views.begin(); i != _views.end(); ++i) {
 
@@ -3503,11 +3733,17 @@ FadeOutDrag::finished (GdkEvent* event, bool movement_occurred)
                tmp->audio_region()->set_fade_out_length (fade_length);
                tmp->audio_region()->set_fade_out_active (true);
 
+               if (!in_command) {
+                       _editor->begin_reversible_command (_("change fade out length"));
+                       in_command = true;
+               }
                XMLNode &after = alist->get_state();
                _editor->session()->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
        }
 
-       _editor->commit_reversible_command ();
+       if (in_command) {
+               _editor->commit_reversible_command ();
+       }
 }
 
 void
@@ -3526,14 +3762,17 @@ FadeOutDrag::aborted (bool)
 
 MarkerDrag::MarkerDrag (Editor* e, ArdourCanvas::Item* i)
        : Drag (e, i)
+       , _selection_changed (false)
 {
        DEBUG_TRACE (DEBUG::Drags, "New MarkerDrag\n");
+       Gtk::Window* toplevel = _editor->current_toplevel();
+       _marker = reinterpret_cast<ArdourMarker*> (_item->get_data ("marker"));
 
-       _marker = reinterpret_cast<Marker*> (_item->get_data ("marker"));
        assert (_marker);
 
        _points.push_back (ArdourCanvas::Duple (0, 0));
-       _points.push_back (ArdourCanvas::Duple (0, physical_screen_height (_editor->get_window())));
+
+       _points.push_back (ArdourCanvas::Duple (0, toplevel ? physical_screen_height (toplevel->get_window()) : 900));
 }
 
 MarkerDrag::~MarkerDrag ()
@@ -3543,7 +3782,7 @@ MarkerDrag::~MarkerDrag ()
        }
 }
 
-MarkerDrag::CopiedLocationMarkerInfo::CopiedLocationMarkerInfo (Location* l, Marker* m)
+MarkerDrag::CopiedLocationMarkerInfo::CopiedLocationMarkerInfo (Location* l, ArdourMarker* m)
 {
        location = new Location (*l);
        markers.push_back (m);
@@ -3570,6 +3809,7 @@ MarkerDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
        } else {
                show_verbose_cursor_time (location->end());
        }
+       setup_snap_delta (is_start ? location->start() : location->end());
 
        Selection::Operation op = ArdourKeyboard::selection_type (event->button.state);
 
@@ -3580,12 +3820,13 @@ MarkerDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
        case Selection::Set:
                if (!_editor->selection->selected (_marker)) {
                        _editor->selection->set (_marker);
+                       _selection_changed = true;
                }
                break;
        case Selection::Extend:
        {
                Locations::LocationList ll;
-               list<Marker*> to_add;
+               list<ArdourMarker*> to_add;
                framepos_t s, e;
                _editor->selection->markers.range (s, e);
                s = min (_marker->position(), s);
@@ -3609,11 +3850,14 @@ MarkerDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
                }
                if (!to_add.empty()) {
                        _editor->selection->add (to_add);
+                       _selection_changed = true;
                }
                break;
        }
        case Selection::Add:
                _editor->selection->add (_marker);
+               _selection_changed = true;
+
                break;
        }
 
@@ -3667,11 +3911,12 @@ MarkerDrag::motion (GdkEvent* event, bool)
        bool move_both = false;
        Location *real_location;
        Location *copy_location = 0;
+       framecnt_t const sd = snap_delta (event->button.state);
 
-       framepos_t const newframe = adjusted_current_frame (event);
+       framecnt_t const newframe = adjusted_frame (_drags->current_pointer_frame () + sd, event, true) - sd;
        framepos_t next = newframe;
 
-       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
+       if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::push_points_modifier ())) {
                move_both = true;
        }
 
@@ -3700,17 +3945,17 @@ MarkerDrag::motion (GdkEvent* event, bool)
 
 
                                switch (_marker->type()) {
-                               case Marker::SessionStart:
-                               case Marker::RangeStart:
-                               case Marker::LoopStart:
-                               case Marker::PunchIn:
+                               case ArdourMarker::SessionStart:
+                               case ArdourMarker::RangeStart:
+                               case ArdourMarker::LoopStart:
+                               case ArdourMarker::PunchIn:
                                        f_delta = newframe - copy_location->start();
                                        break;
 
-                               case Marker::SessionEnd:
-                               case Marker::RangeEnd:
-                               case Marker::LoopEnd:
-                               case Marker::PunchOut:
+                               case ArdourMarker::SessionEnd:
+                               case ArdourMarker::RangeEnd:
+                               case ArdourMarker::LoopEnd:
+                               case ArdourMarker::PunchOut:
                                        f_delta = newframe - copy_location->end();
                                        break;
                                default:
@@ -3758,10 +4003,10 @@ MarkerDrag::motion (GdkEvent* event, bool)
                                if (move_both || (*x).move_both) {
                                        copy_location->set_start (new_start);
                                        copy_location->set_end (new_end);
-                               } else  if (new_start < copy_location->end()) {
+                               } else  if (new_start < copy_location->end()) {
                                        copy_location->set_start (new_start);
                                } else if (newframe > 0) {
-                                       _editor->snap_to (next, RoundUpAlways, true);
+                                       //_editor->snap_to (next, RoundUpAlways, true);
                                        copy_location->set_end (next);
                                        copy_location->set_start (newframe);
                                }
@@ -3774,7 +4019,7 @@ MarkerDrag::motion (GdkEvent* event, bool)
                                } else if (new_end > copy_location->start()) {
                                        copy_location->set_end (new_end);
                                } else if (newframe > 0) {
-                                       _editor->snap_to (next, RoundDownAlways, true);
+                                       //_editor->snap_to (next, RoundDownAlways, true);
                                        copy_location->set_start (next);
                                        copy_location->set_end (newframe);
                                }
@@ -3818,17 +4063,19 @@ MarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                */
 
                Selection::Operation op = ArdourKeyboard::selection_type (event->button.state);
-
                switch (op) {
                case Selection::Set:
                        if (_editor->selection->selected (_marker) && _editor->selection->markers.size() > 1) {
                                _editor->selection->set (_marker);
+                               _selection_changed = true;
                        }
                        break;
 
                case Selection::Toggle:
                        /* we toggle on the button release, click only */
                        _editor->selection->toggle (_marker);
+                       _selection_changed = true;
+
                        break;
 
                case Selection::Extend:
@@ -3836,13 +4083,18 @@ MarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                        break;
                }
 
+               if (_selection_changed) {
+                       _editor->begin_reversible_selection_op(X_("Select Marker Release"));
+                       _editor->commit_reversible_selection_op();
+               }
+
                return;
        }
 
        _editor->_dragging_edit_point = false;
 
-       _editor->begin_reversible_command ( _("move marker") );
        XMLNode &before = _editor->session()->locations()->get_state();
+       bool in_command = false;
 
        MarkerSelection::iterator i;
        CopiedLocationInfo::iterator x;
@@ -3857,9 +4109,12 @@ MarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                if (location) {
 
                        if (location->locked()) {
-                               return;
+                               continue;
+                       }
+                       if (!in_command) {
+                               _editor->begin_reversible_command ( _("move marker") );
+                               in_command = true;
                        }
-
                        if (location->is_mark()) {
                                location->set_start (((*x).location)->start());
                        } else {
@@ -3868,15 +4123,17 @@ MarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                }
        }
 
-       XMLNode &after = _editor->session()->locations()->get_state();
-       _editor->session()->add_command(new MementoCommand<Locations>(*(_editor->session()->locations()), &before, &after));
-       _editor->commit_reversible_command ();
+       if (in_command) {
+               XMLNode &after = _editor->session()->locations()->get_state();
+               _editor->session()->add_command(new MementoCommand<Locations>(*(_editor->session()->locations()), &before, &after));
+               _editor->commit_reversible_command ();
+       }
 }
 
 void
-MarkerDrag::aborted (bool movement_occured)
+MarkerDrag::aborted (bool movement_occurred)
 {
-       if (!movement_occured) {
+       if (!movement_occurred) {
                return;
        }
 
@@ -3885,7 +4142,7 @@ MarkerDrag::aborted (bool movement_occured)
                /* move all markers to their original location */
 
 
-               for (vector<Marker*>::iterator m = x->markers.begin(); m != x->markers.end(); ++m) {
+               for (vector<ArdourMarker*>::iterator m = x->markers.begin(); m != x->markers.end(); ++m) {
 
                        bool is_start;
                        Location * location = _editor->find_location_from_marker (*m, is_start);
@@ -3904,9 +4161,13 @@ MarkerDrag::update_item (Location*)
 }
 
 ControlPointDrag::ControlPointDrag (Editor* e, ArdourCanvas::Item* i)
-       : Drag (e, i),
-         _cumulative_x_drag (0),
-         _cumulative_y_drag (0)
+       : Drag (e, i)
+       , _fixed_grab_x (0.0)
+       , _fixed_grab_y (0.0)
+       , _cumulative_x_drag (0.0)
+       , _cumulative_y_drag (0.0)
+       , _pushing (false)
+       , _final_index (0)
 {
        if (_zero_gain_fraction < 0.0) {
                _zero_gain_fraction = gain_to_slider_position_with_max (dB_to_coefficient (0.0), Config->get_max_gain());
@@ -3929,13 +4190,13 @@ ControlPointDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*cursor*/)
        _fixed_grab_x = _point->get_x();
        _fixed_grab_y = _point->get_y();
 
-       float const fraction = 1 - (_point->get_y() / _point->line().height());
-
-       _point->line().start_drag_single (_point, _fixed_grab_x, fraction);
+       framepos_t pos = _editor->pixel_to_sample (_fixed_grab_x);
+       setup_snap_delta (pos);
 
+       float const fraction = 1 - (_point->get_y() / _point->line().height());
        show_verbose_cursor_text (_point->line().get_verbose_cursor_string (fraction));
 
-       _pushing = Keyboard::modifier_state_contains (event->button.state, Keyboard::PrimaryModifier);
+       _pushing = Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::push_points_modifier ());
 
        if (!_point->can_slide ()) {
                _x_constrained = true;
@@ -3943,14 +4204,16 @@ ControlPointDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*cursor*/)
 }
 
 void
-ControlPointDrag::motion (GdkEvent* event, bool)
+ControlPointDrag::motion (GdkEvent* event, bool first_motion)
 {
        double dx = _drags->current_pointer_x() - last_pointer_x();
        double dy = current_pointer_y() - last_pointer_y();
+       bool need_snap = true;
 
-       if (event->button.state & Keyboard::SecondaryModifier) {
+       if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::fine_adjust_modifier ())) {
                dx *= 0.1;
                dy *= 0.1;
+               need_snap = false;
        }
 
        /* coordinate in pixels relative to the start of the region (for region-based automation)
@@ -3962,11 +4225,6 @@ ControlPointDrag::motion (GdkEvent* event, bool)
        // positive side of zero
        double const zero_gain_y = (1.0 - _zero_gain_fraction) * _point->line().height() - .01;
 
-       // make sure we hit zero when passing through
-       if ((cy < zero_gain_y && (cy - dy) > zero_gain_y) || (cy > zero_gain_y && (cy - dy) < zero_gain_y)) {
-               cy = zero_gain_y;
-       }
-
        if (_x_constrained) {
                cx = _fixed_grab_x;
        }
@@ -3981,19 +4239,31 @@ ControlPointDrag::motion (GdkEvent* event, bool)
        cy = max (0.0, cy);
        cy = min ((double) _point->line().height(), cy);
 
-       framepos_t cx_frames = _editor->pixel_to_sample (cx);
+       // make sure we hit zero when passing through
+       if ((cy < zero_gain_y && (cy - dy) > zero_gain_y) || (cy > zero_gain_y && (cy - dy) < zero_gain_y)) {
+               cy = zero_gain_y;
+       }
 
-       if (!_x_constrained) {
+       framepos_t cx_frames = _editor->pixel_to_sample (cx) + snap_delta (event->button.state);
+
+       if (!_x_constrained && need_snap) {
                _editor->snap_to_with_modifier (cx_frames, event);
        }
 
+       cx_frames -= snap_delta (event->button.state);
        cx_frames = min (cx_frames, _point->line().maximum_time());
 
        float const fraction = 1.0 - (cy / _point->line().height());
 
-       _point->line().drag_motion (_editor->sample_to_pixel_unrounded (cx_frames), fraction, false, _pushing, _final_index);
+       if (first_motion) {
+               float const initial_fraction = 1.0 - (_fixed_grab_y / _point->line().height());
+               _editor->begin_reversible_command (_("automation event move"));
+               _point->line().start_drag_single (_point, _fixed_grab_x, initial_fraction);
+       }
+       pair<double, float> result;
+       result = _point->line().drag_motion (_editor->sample_to_pixel_unrounded (cx_frames), fraction, false, _pushing, _final_index);
 
-       show_verbose_cursor_text (_point->line().get_verbose_cursor_string (fraction));
+       show_verbose_cursor_text (_point->line().get_verbose_cursor_string (result.second));
 }
 
 void
@@ -4002,17 +4272,14 @@ ControlPointDrag::finished (GdkEvent* event, bool movement_occurred)
        if (!movement_occurred) {
 
                /* just a click */
-
-               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
+               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::ModifierMask (Keyboard::TertiaryModifier))) {
                        _editor->reset_point_selection ();
                }
 
        } else {
-               motion (event, false);
+               _point->line().end_drag (_pushing, _final_index);
+               _editor->commit_reversible_command ();
        }
-
-       _point->line().end_drag (_pushing, _final_index);
-       _editor->commit_reversible_command ();
 }
 
 void
@@ -4034,9 +4301,13 @@ ControlPointDrag::active (Editing::MouseMode m)
 }
 
 LineDrag::LineDrag (Editor* e, ArdourCanvas::Item* i)
-       : Drag (e, i),
-         _line (0),
-         _cumulative_y_drag (0)
+       : Drag (e, i)
+       , _line (0)
+       , _fixed_grab_x (0.0)
+       , _fixed_grab_y (0.0)
+       , _cumulative_y_drag (0)
+       , _before (0)
+       , _after (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New LineDrag\n");
 }
@@ -4053,41 +4324,41 @@ LineDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*cursor*/)
           origin, and ditto for y.
        */
 
-       double cx = event->button.x;
-       double cy = event->button.y;
-
-       _line->parent_group().canvas_to_item (cx, cy);
+       double mx = event->button.x;
+       double my = event->button.y;
 
-       framecnt_t const frame_within_region = (framecnt_t) floor (cx * _editor->samples_per_pixel);
+       _line->grab_item().canvas_to_item (mx, my);
 
-       uint32_t before;
-       uint32_t after;
+       framecnt_t const frame_within_region = (framecnt_t) floor (mx * _editor->samples_per_pixel);
 
-       if (!_line->control_points_adjacent (frame_within_region, before, after)) {
+       if (!_line->control_points_adjacent (frame_within_region, _before, _after)) {
                /* no adjacent points */
                return;
        }
 
        Drag::start_grab (event, _editor->cursors()->fader);
 
-       /* store grab start in parent frame */
+       /* store grab start in item frame */
+       double const bx = _line->nth (_before)->get_x();
+       double const ax = _line->nth (_after)->get_x();
+       double const click_ratio = (ax - mx) / (ax - bx);
 
-       _fixed_grab_x = cx;
+       double const cy = ((_line->nth (_before)->get_y() * click_ratio) + (_line->nth (_after)->get_y() * (1 - click_ratio)));
+
+       _fixed_grab_x = mx;
        _fixed_grab_y = cy;
 
        double fraction = 1.0 - (cy / _line->height());
 
-       _line->start_drag_line (before, after, fraction);
-
        show_verbose_cursor_text (_line->get_verbose_cursor_string (fraction));
 }
 
 void
-LineDrag::motion (GdkEvent* event, bool)
+LineDrag::motion (GdkEvent* event, bool first_move)
 {
        double dy = current_pointer_y() - last_pointer_y();
 
-       if (event->button.state & Keyboard::SecondaryModifier) {
+       if (Keyboard::modifier_state_equals (event->button.state, ArdourKeyboard::fine_adjust_modifier ())) {
                dy *= 0.1;
        }
 
@@ -4101,32 +4372,49 @@ LineDrag::motion (GdkEvent* event, bool)
        double const fraction = 1.0 - (cy / _line->height());
        uint32_t ignored;
 
+       if (first_move) {
+               float const initial_fraction = 1.0 - (_fixed_grab_y / _line->height());
+
+               _editor->begin_reversible_command (_("automation range move"));
+               _line->start_drag_line (_before, _after, initial_fraction);
+       }
+
        /* we are ignoring x position for this drag, so we can just pass in anything */
-       _line->drag_motion (0, fraction, true, false, ignored);
+       pair<double, float> result;
 
-       show_verbose_cursor_text (_line->get_verbose_cursor_string (fraction));
+       result = _line->drag_motion (0, fraction, true, false, ignored);
+       show_verbose_cursor_text (_line->get_verbose_cursor_string (result.second));
 }
 
 void
-LineDrag::finished (GdkEvent* event, bool movement_occured)
+LineDrag::finished (GdkEvent* event, bool movement_occurred)
 {
-       if (movement_occured) {
+       if (movement_occurred) {
                motion (event, false);
                _line->end_drag (false, 0);
+               _editor->commit_reversible_command ();
        } else {
                /* add a new control point on the line */
 
                AutomationTimeAxisView* atv;
 
-               _line->end_drag (false, 0);
-
                if ((atv = dynamic_cast<AutomationTimeAxisView*>(_editor->clicked_axisview)) != 0) {
-                       framepos_t where = _editor->window_event_sample (event, 0, 0);
-                       atv->add_automation_event (event, where, event->button.y, false);
+                       framepos_t where = grab_frame ();
+
+                       double cx = 0;
+                       double cy = _fixed_grab_y;
+
+                       _line->grab_item().item_to_canvas (cx, cy);
+
+                       atv->add_automation_event (event, where, cy, false);
+               } else if (dynamic_cast<AudioTimeAxisView*>(_editor->clicked_axisview) != 0) {
+                       AudioRegionView* arv;
+
+                       if ((arv = dynamic_cast<AudioRegionView*>(_editor->clicked_regionview)) != 0) {
+                               arv->add_gain_point_event (&arv->get_gain_line()->grab_item(), event, false);
+                       }
                }
        }
-
-       _editor->commit_reversible_command ();
 }
 
 void
@@ -4138,7 +4426,11 @@ LineDrag::aborted (bool)
 FeatureLineDrag::FeatureLineDrag (Editor* e, ArdourCanvas::Item* i)
        : Drag (e, i),
          _line (0),
-         _cumulative_x_drag (0)
+         _arv (0),
+         _region_view_grab_x (0.0),
+         _cumulative_x_drag (0),
+         _before (0.0),
+         _max_x (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New FeatureLineDrag\n");
 }
@@ -4222,7 +4514,7 @@ void
 RubberbandSelectDrag::start_grab (GdkEvent* event, Gdk::Cursor *)
 {
        Drag::start_grab (event);
-       show_verbose_cursor_time (adjusted_current_frame (event, ARDOUR_UI::config()->get_rubberbanding_snaps_to_grid()));
+       show_verbose_cursor_time (adjusted_current_frame (event, UIConfiguration::instance().get_rubberbanding_snaps_to_grid()));
 }
 
 void
@@ -4233,10 +4525,10 @@ RubberbandSelectDrag::motion (GdkEvent* event, bool)
        double y1;
        double y2;
 
-       framepos_t const pf = adjusted_current_frame (event, ARDOUR_UI::config()->get_rubberbanding_snaps_to_grid());
+       framepos_t const pf = adjusted_current_frame (event, UIConfiguration::instance().get_rubberbanding_snaps_to_grid());
 
        framepos_t grab = grab_frame ();
-       if (ARDOUR_UI::config()->get_rubberbanding_snaps_to_grid ()) {
+       if (UIConfiguration::instance().get_rubberbanding_snaps_to_grid ()) {
                _editor->snap_to_with_modifier (grab, event);
        } else {
                grab = raw_grab_frame ();
@@ -4313,7 +4605,7 @@ RubberbandSelectDrag::do_select_things (GdkEvent* event, bool drag_in_progress)
        framepos_t grab = grab_frame ();
        framepos_t lpf = last_pointer_frame ();
 
-       if (!ARDOUR_UI::config()->get_rubberbanding_snaps_to_grid ()) {
+       if (!UIConfiguration::instance().get_rubberbanding_snaps_to_grid ()) {
                grab = raw_grab_frame ();
                lpf = _editor->pixel_to_sample_from_event (last_pointer_x());
        }
@@ -4359,7 +4651,7 @@ RubberbandSelectDrag::finished (GdkEvent* event, bool movement_occurred)
                        /* MIDI track */
                        if (_editor->selection->empty() && _editor->mouse_mode == MouseDraw) {
                                /* nothing selected */
-                               add_midi_region (mtv);
+                               add_midi_region (mtv, true);
                                do_deselect = false;
                        }
                }
@@ -4396,7 +4688,12 @@ TimeFXDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
 {
        Drag::start_grab (event, cursor);
 
-       show_verbose_cursor_time (adjusted_current_frame (event));
+       _editor->get_selection().add (_primary);
+
+       framepos_t where = _primary->region()->position();
+       setup_snap_delta (where);
+
+       show_verbose_cursor_duration (where, adjusted_current_frame (event), 0);
 }
 
 void
@@ -4408,40 +4705,51 @@ TimeFXDrag::motion (GdkEvent* event, bool)
        pair<TimeAxisView*, double> const tv = _editor->trackview_by_y_position (grab_y());
        int layer = tv.first->layer_display() == Overlaid ? 0 : tv.second;
        int layers = tv.first->layer_display() == Overlaid ? 1 : cv->layers();
-
-       framepos_t const pf = adjusted_current_frame (event);
+       framepos_t pf = _editor->canvas_event_sample (event) + snap_delta (event->button.state);
+       _editor->snap_to_with_modifier (pf, event);
+       pf -= snap_delta (event->button.state);
 
        if (pf > rv->region()->position()) {
                rv->get_time_axis_view().show_timestretch (rv->region()->position(), pf, layers, layer);
        }
 
-       show_verbose_cursor_time (pf);
+       show_verbose_cursor_duration (_primary->region()->position(), pf, 0);
 }
 
 void
-TimeFXDrag::finished (GdkEvent* /*event*/, bool movement_occurred)
+TimeFXDrag::finished (GdkEvent* event, bool movement_occurred)
 {
-       _primary->get_time_axis_view().hide_timestretch ();
+       /* this may have been a single click, no drag. We still want the dialog
+          to show up in that case, so that the user can manually edit the
+          parameters for the timestretch.
+       */
 
-       if (!movement_occurred) {
-               return;
-       }
+       float fraction = 1.0;
 
-       if (last_pointer_frame() < _primary->region()->position()) {
-               /* backwards drag of the left edge - not usable */
-               return;
-       }
+       if (movement_occurred) {
 
-       framecnt_t newlen = last_pointer_frame() - _primary->region()->position();
+               motion (event, false);
 
-       float percentage = (double) newlen / (double) _primary->region()->length();
+               _primary->get_time_axis_view().hide_timestretch ();
+
+               framepos_t adjusted_frame_pos = adjusted_current_frame (event);
+
+               if (adjusted_frame_pos < _primary->region()->position()) {
+                       /* backwards drag of the left edge - not usable */
+                       return;
+               }
+
+               framecnt_t newlen = adjusted_frame_pos - _primary->region()->position();
+
+               fraction = (double) newlen / (double) _primary->region()->length();
 
 #ifndef USE_RUBBERBAND
-       // Soundtouch uses percentage / 100 instead of normal (/ 1)
-       if (_primary->region()->data_type() == DataType::AUDIO) {
-               percentage = (float) ((double) newlen - (double) _primary->region()->length()) / ((double) newlen) * 100.0f;
-       }
+               // Soundtouch uses fraction / 100 instead of normal (/ 1)
+               if (_primary->region()->data_type() == DataType::AUDIO) {
+                       fraction = (float) ((double) newlen - (double) _primary->region()->length()) / ((double) newlen) * 100.0f;
+               }
 #endif
+       }
 
        if (!_editor->get_selection().regions.empty()) {
                /* primary will already be included in the selection, and edit
@@ -4450,7 +4758,7 @@ TimeFXDrag::finished (GdkEvent* /*event*/, bool movement_occurred)
                   selection.
                */
 
-               if (_editor->time_stretch (_editor->get_selection().regions, percentage) == -1) {
+               if (_editor->time_stretch (_editor->get_selection().regions, fraction) == -1) {
                        error << _("An error occurred while executing time stretch operation") << endmsg;
                }
        }
@@ -4698,7 +5006,6 @@ SelectionDrag::motion (GdkEvent* event, bool first_move)
 
        case SelectionStartTrim:
 
-               start = _editor->selection->time[_editor->clicked_selection].start;
                end = _editor->selection->time[_editor->clicked_selection].end;
 
                if (pending_position > end) {
@@ -4711,7 +5018,6 @@ SelectionDrag::motion (GdkEvent* event, bool first_move)
        case SelectionEndTrim:
 
                start = _editor->selection->time[_editor->clicked_selection].start;
-               end = _editor->selection->time[_editor->clicked_selection].end;
 
                if (pending_position < start) {
                        end = start;
@@ -4773,16 +5079,22 @@ SelectionDrag::finished (GdkEvent* event, bool movement_occurred)
 
                /* XXX what if its a music time selection? */
                if (s) {
-                       if ( s->get_play_range() && s->transport_rolling() ) {
+                       if (s->get_play_range() && s->transport_rolling()) {
                                s->request_play_range (&_editor->selection->time, true);
-                       } else {
-                               if (ARDOUR_UI::config()->get_follow_edits() && !s->transport_rolling()) {
+                       } else if (!s->config.get_external_sync()) {
+                               if (UIConfiguration::instance().get_follow_edits() && !s->transport_rolling()) {
                                        if (_operation == SelectionEndTrim)
                                                _editor->maybe_locate_with_edit_preroll( _editor->get_selection().time.end_frame());
                                        else
                                                s->request_locate (_editor->get_selection().time.start());
                                }
                        }
+
+                       if (_editor->get_selection().time.length() != 0) {
+                               s->set_range_selection (_editor->get_selection().time.start(), _editor->get_selection().time.end_frame());
+                       } else {
+                               s->clear_range_selection ();
+                       }
                }
 
        } else {
@@ -4838,11 +5150,11 @@ RangeMarkerBarDrag::RangeMarkerBarDrag (Editor* e, ArdourCanvas::Item* i, Operat
 
        _drag_rect = new ArdourCanvas::Rectangle (_editor->time_line_group,
                                                  ArdourCanvas::Rect (0.0, 0.0, 0.0,
-                                                                     physical_screen_height (_editor->get_window())));
+                                                                     physical_screen_height (_editor->current_toplevel()->get_window())));
        _drag_rect->hide ();
 
-       _drag_rect->set_fill_color (ARDOUR_UI::config()->color ("range drag rect"));
-       _drag_rect->set_outline_color (ARDOUR_UI::config()->color ("range drag rect"));
+       _drag_rect->set_fill_color (UIConfiguration::instance().color ("range drag rect"));
+       _drag_rect->set_outline_color (UIConfiguration::instance().color ("range drag rect"));
 }
 
 RangeMarkerBarDrag::~RangeMarkerBarDrag()
@@ -4873,7 +5185,7 @@ RangeMarkerBarDrag::start_grab (GdkEvent* event, Gdk::Cursor *)
        case CreateTransportMarker:
        case CreateCDMarker:
 
-               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
+               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::CopyModifier)) {
                        _copy = true;
                } else {
                        _copy = false;
@@ -5060,9 +5372,9 @@ RangeMarkerBarDrag::finished (GdkEvent* event, bool movement_occurred)
 }
 
 void
-RangeMarkerBarDrag::aborted (bool movement_occured)
+RangeMarkerBarDrag::aborted (bool movement_occurred)
 {
-       if (movement_occured) {
+       if (movement_occurred) {
                _drag_rect->hide ();
        }
 }
@@ -5081,6 +5393,7 @@ NoteDrag::NoteDrag (Editor* e, ArdourCanvas::Item* i)
        : Drag (e, i)
        , _cumulative_dx (0)
        , _cumulative_dy (0)
+       , _was_selected (false)
 {
        DEBUG_TRACE (DEBUG::Drags, "New NoteDrag\n");
 
@@ -5094,6 +5407,7 @@ void
 NoteDrag::start_grab (GdkEvent* event, Gdk::Cursor *)
 {
        Drag::start_grab (event);
+       setup_snap_delta (_region->source_beats_to_absolute_frames (_primary->note()->time ()));
 
        if (!(_was_selected = _primary->selected())) {
 
@@ -5110,18 +5424,16 @@ NoteDrag::start_grab (GdkEvent* event, Gdk::Cursor *)
                        if (add) {
                                _region->note_selected (_primary, true);
                        } else {
+                               _editor->get_selection().clear_points();
                                _region->unique_select (_primary);
                        }
-
-                       _editor->begin_reversible_selection_op(X_("Select Note Press"));
-                       _editor->commit_reversible_selection_op();
                }
        }
 }
 
 /** @return Current total drag x change in frames */
 frameoffset_t
-NoteDrag::total_dx () const
+NoteDrag::total_dx (const guint state) const
 {
        /* dx in frames */
        frameoffset_t const dx = _editor->pixel_to_sample (_drags->current_pointer_x() - grab_x());
@@ -5130,15 +5442,39 @@ NoteDrag::total_dx () const
        frameoffset_t const n = _region->source_beats_to_absolute_frames (_primary->note()->time ());
 
        /* new time of the primary note in session frames */
-       frameoffset_t st = n + dx;
+       frameoffset_t st = n + dx + snap_delta (state);
 
        framepos_t const rp = _region->region()->position ();
 
        /* prevent the note being dragged earlier than the region's position */
        st = max (st, rp);
 
-       /* snap and return corresponding delta */
-       return _region->snap_frame_to_frame (st - rp) + rp - n;
+       /* possibly snap and return corresponding delta */
+
+       bool snap = true;
+
+       if (ArdourKeyboard::indicates_snap (state)) {
+               if (_editor->snap_mode () != SnapOff) {
+                       snap = false;
+               }
+       } else {
+               if (_editor->snap_mode () == SnapOff) {
+                       snap = false;
+                       /* inverted logic here - we;re in snapoff but we've pressed the snap delta modifier */
+                       if (ArdourKeyboard::indicates_snap_delta (state)) {
+                               snap = true;
+                       }
+               }
+       }
+
+       frameoffset_t ret;
+       if (snap) {
+               bool const ensure_snap = _editor->snap_mode () != SnapMagnetic;
+               ret =  _region->snap_frame_to_frame (st - rp, ensure_snap) + rp - n - snap_delta (state);
+       } else {
+               ret = st - n - snap_delta (state);
+       }
+       return ret;
 }
 
 /** @return Current total drag y change in note number */
@@ -5157,10 +5493,10 @@ NoteDrag::total_dy () const
 }
 
 void
-NoteDrag::motion (GdkEvent *, bool)
+NoteDrag::motion (GdkEvent * event, bool)
 {
        /* Total change in x and y since the start of the drag */
-       frameoffset_t const dx = total_dx ();
+       frameoffset_t const dx = total_dx (event->button.state);
        int8_t const dy = total_dy ();
 
        /* Now work out what we have to do to the note canvas items to set this new drag delta */
@@ -5181,13 +5517,9 @@ NoteDrag::motion (GdkEvent *, bool)
                 * odd with them. so show the note value anyway, always.
                 */
 
-               char buf[12];
                uint8_t new_note = min (max (_primary->note()->note() + note_delta, 0), 127);
 
-               snprintf (buf, sizeof (buf), "%s (%d)", Evoral::midi_note_name (new_note).c_str(),
-                         (int) floor ((double)new_note));
-
-               show_verbose_cursor_text (buf);
+               _region->show_verbose_cursor_for_new_note_value (_primary->note(), new_note);
        }
 }
 
@@ -5197,7 +5529,7 @@ NoteDrag::finished (GdkEvent* ev, bool moved)
        if (!moved) {
                /* no motion - select note */
 
-               if (_editor->current_mouse_mode() == Editing::MouseObject ||
+               if (_editor->current_mouse_mode() == Editing::MouseContent ||
                    _editor->current_mouse_mode() == Editing::MouseDraw) {
 
                        bool changed = false;
@@ -5207,12 +5539,17 @@ NoteDrag::finished (GdkEvent* ev, bool moved)
                                if (add) {
                                        _region->note_deselected (_primary);
                                        changed = true;
+                               } else {
+                                       _editor->get_selection().clear_points();
+                                       _region->unique_select (_primary);
+                                       changed = true;
                                }
                        } else {
                                bool extend = Keyboard::modifier_state_equals (ev->button.state, Keyboard::TertiaryModifier);
                                bool add = Keyboard::modifier_state_equals (ev->button.state, Keyboard::PrimaryModifier);
 
                                if (!extend && !add && _region->selection_size() > 1) {
+                                       _editor->get_selection().clear_points();
                                        _region->unique_select (_primary);
                                        changed = true;
                                } else if (extend) {
@@ -5220,6 +5557,8 @@ NoteDrag::finished (GdkEvent* ev, bool moved)
                                        changed = true;
                                } else {
                                        /* it was added during button press */
+                                       changed = true;
+
                                }
                        }
 
@@ -5229,7 +5568,7 @@ NoteDrag::finished (GdkEvent* ev, bool moved)
                        }
                }
        } else {
-               _region->note_dropped (_primary, total_dx(), total_dy());
+               _region->note_dropped (_primary, total_dx (ev->button.state), total_dy());
        }
 }
 
@@ -5348,123 +5687,142 @@ AutomationRangeDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
                        }
                }
 
-       } else {
+       }
+
+       if (_nothing_to_drag) {
+               return;
+       }
+}
+
+void
+AutomationRangeDrag::motion (GdkEvent*, bool first_move)
+{
+       if (_nothing_to_drag && !first_move) {
+               return;
+       }
 
-               for (list<AudioRange>::const_iterator i = _ranges.begin(); i != _ranges.end(); ++i) {
+       if (first_move) {
+               _editor->begin_reversible_command (_("automation range move"));
 
-                       framecnt_t const half = (i->start + i->end) / 2;
+               if (!_ranges.empty()) {
 
-                       /* find the line that this audio range starts in */
-                       list<Line>::iterator j = _lines.begin();
-                       while (j != _lines.end() && (j->range.first > i->start || j->range.second < i->start)) {
-                               ++j;
-                       }
+                       for (list<AudioRange>::const_iterator i = _ranges.begin(); i != _ranges.end(); ++i) {
 
-                       if (j != _lines.end()) {
-                               boost::shared_ptr<AutomationList> the_list = j->line->the_list ();
+                               framecnt_t const half = (i->start + i->end) / 2;
+
+                               /* find the line that this audio range starts in */
+                               list<Line>::iterator j = _lines.begin();
+                               while (j != _lines.end() && (j->range.first > i->start || j->range.second < i->start)) {
+                                       ++j;
+                               }
+
+                               if (j != _lines.end()) {
+                                       boost::shared_ptr<AutomationList> the_list = j->line->the_list ();
 
                                /* j is the line that this audio range starts in; fade into it;
                                   64 samples length plucked out of thin air.
                                */
 
-                               framepos_t a = i->start + 64;
-                               if (a > half) {
-                                       a = half;
-                               }
+                                       framepos_t a = i->start + 64;
+                                       if (a > half) {
+                                               a = half;
+                                       }
 
-                               double const p = j->line->time_converter().from (i->start - j->line->time_converter().origin_b ());
-                               double const q = j->line->time_converter().from (a - j->line->time_converter().origin_b ());
+                                       double const p = j->line->time_converter().from (i->start - j->line->time_converter().origin_b ());
+                                       double const q = j->line->time_converter().from (a - j->line->time_converter().origin_b ());
 
-                               the_list->editor_add (p, value (the_list, p));
-                               the_list->editor_add (q, value (the_list, q));
-                       }
+                                       XMLNode &before = the_list->get_state();
+                                       bool const add_p = the_list->editor_add (p, value (the_list, p), false);
+                                       bool const add_q = the_list->editor_add (q, value (the_list, q), false);
 
-                       /* same thing for the end */
+                                       if (add_p || add_q) {
+                                               _editor->session()->add_command (
+                                                       new MementoCommand<AutomationList>(*the_list.get (), &before, &the_list->get_state()));
+                                       }
+                               }
 
-                       j = _lines.begin();
-                       while (j != _lines.end() && (j->range.first > i->end || j->range.second < i->end)) {
-                               ++j;
-                       }
+                               /* same thing for the end */
 
-                       if (j != _lines.end()) {
-                               boost::shared_ptr<AutomationList> the_list = j->line->the_list ();
+                               j = _lines.begin();
+                               while (j != _lines.end() && (j->range.first > i->end || j->range.second < i->end)) {
+                                       ++j;
+                               }
 
-                               /* j is the line that this audio range starts in; fade out of it;
-                                  64 samples length plucked out of thin air.
-                               */
+                               if (j != _lines.end()) {
+                                       boost::shared_ptr<AutomationList> the_list = j->line->the_list ();
 
-                               framepos_t b = i->end - 64;
-                               if (b < half) {
-                                       b = half;
-                               }
+                                       /* j is the line that this audio range starts in; fade out of it;
+                                          64 samples length plucked out of thin air.
+                                       */
+
+                                       framepos_t b = i->end - 64;
+                                       if (b < half) {
+                                               b = half;
+                                       }
+
+                                       double const p = j->line->time_converter().from (b - j->line->time_converter().origin_b ());
+                                       double const q = j->line->time_converter().from (i->end - j->line->time_converter().origin_b ());
 
-                               double const p = j->line->time_converter().from (b - j->line->time_converter().origin_b ());
-                               double const q = j->line->time_converter().from (i->end - j->line->time_converter().origin_b ());
+                                       XMLNode &before = the_list->get_state();
+                                       bool const add_p = the_list->editor_add (p, value (the_list, p), false);
+                                       bool const add_q = the_list->editor_add (q, value (the_list, q), false);
 
-                               the_list->editor_add (p, value (the_list, p));
-                               the_list->editor_add (q, value (the_list, q));
+                                       if (add_p || add_q) {
+                                               _editor->session()->add_command (
+                                                       new MementoCommand<AutomationList>(*the_list.get (), &before, &the_list->get_state()));
+                                       }
+                               }
                        }
-               }
 
-               _nothing_to_drag = true;
+                       _nothing_to_drag = true;
 
-               /* Find all the points that should be dragged and put them in the relevant
-                  points lists in the Line structs.
-               */
+                       /* Find all the points that should be dragged and put them in the relevant
+                          points lists in the Line structs.
+                       */
 
-               for (list<Line>::iterator i = _lines.begin(); i != _lines.end(); ++i) {
+                       for (list<Line>::iterator i = _lines.begin(); i != _lines.end(); ++i) {
 
-                       uint32_t const N = i->line->npoints ();
-                       for (uint32_t j = 0; j < N; ++j) {
+                               uint32_t const N = i->line->npoints ();
+                               for (uint32_t j = 0; j < N; ++j) {
 
-                               /* here's a control point on this line */
-                               ControlPoint* p = i->line->nth (j);
-                               double const w = i->line->time_converter().to ((*p->model())->when) + i->line->time_converter().origin_b ();
+                                       /* here's a control point on this line */
+                                       ControlPoint* p = i->line->nth (j);
+                                       double const w = i->line->time_converter().to ((*p->model())->when) + i->line->time_converter().origin_b ();
 
-                               /* see if it's inside a range */
-                               list<AudioRange>::const_iterator k = _ranges.begin ();
-                               while (k != _ranges.end() && (k->start >= w || k->end <= w)) {
-                                       ++k;
-                               }
+                                       /* see if it's inside a range */
+                                       list<AudioRange>::const_iterator k = _ranges.begin ();
+                                       while (k != _ranges.end() && (k->start >= w || k->end <= w)) {
+                                               ++k;
+                                       }
 
-                               if (k != _ranges.end()) {
-                                       /* dragging this point */
-                                       _nothing_to_drag = false;
-                                       i->points.push_back (p);
+                                       if (k != _ranges.end()) {
+                                               /* dragging this point */
+                                               _nothing_to_drag = false;
+                                               i->points.push_back (p);
+                                       }
                                }
                        }
                }
-       }
 
-       if (_nothing_to_drag) {
-               return;
-       }
-
-       for (list<Line>::iterator i = _lines.begin(); i != _lines.end(); ++i) {
-               i->line->start_drag_multiple (i->points, y_fraction (i->line, current_pointer_y()), i->state);
-       }
-}
-
-void
-AutomationRangeDrag::motion (GdkEvent*, bool /*first_move*/)
-{
-       if (_nothing_to_drag) {
-               return;
+               for (list<Line>::iterator i = _lines.begin(); i != _lines.end(); ++i) {
+                       i->line->start_drag_multiple (i->points, y_fraction (i->line, current_pointer_y()), i->state);
+               }
        }
 
        for (list<Line>::iterator l = _lines.begin(); l != _lines.end(); ++l) {
                float const f = y_fraction (l->line, current_pointer_y());
                /* we are ignoring x position for this drag, so we can just pass in anything */
+               pair<double, float> result;
                uint32_t ignored;
-               l->line->drag_motion (0, f, true, false, ignored);
-               show_verbose_cursor_text (l->line->get_verbose_cursor_relative_string (l->original_fraction, f));
+               result = l->line->drag_motion (0, f, true, false, ignored);
+               show_verbose_cursor_text (l->line->get_verbose_cursor_relative_string (l->original_fraction, result.second));
        }
 }
 
 void
-AutomationRangeDrag::finished (GdkEvent* event, bool)
+AutomationRangeDrag::finished (GdkEvent* event, bool motion_occurred)
 {
-       if (_nothing_to_drag) {
+       if (_nothing_to_drag || !motion_occurred) {
                return;
        }