Tempo ramps - reinstate cross-dragging of music-locked meters, various bug fixes.
[ardour.git] / gtk2_ardour / editor_drag.cc
index 4be15613b03a6a42ebb113bf108ef32a58333fe0..a6867892537ac7ebc43bcac86a5045a12dd8323a 100644 (file)
@@ -82,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)
 {
 }
 
@@ -215,15 +218,21 @@ 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)
@@ -455,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;
@@ -501,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>();
@@ -636,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_frame (_drags->current_pointer_frame () + snap_delta (event->button.state), event, true);
+       *pending_region_position = adjusted_frame (_drags->current_pointer_frame (), event, false);
 
        framepos_t sync_frame;
        framecnt_t sync_offset;
@@ -648,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) - snap_delta (event->button.state);
+               *pending_region_position = _primary->region()->adjust_to_sync (sync_frame) - sd;
 
        } else {
                *pending_region_position = _last_frame_position;
@@ -1829,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");
 
@@ -2293,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
@@ -2319,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();
        }
 }
 
@@ -2340,6 +2352,7 @@ NoteResizeDrag::NoteResizeDrag (Editor* e, ArdourCanvas::Item* i)
        , region (0)
        , relative (false)
        , at_front (true)
+       , _was_selected (false)
        , _snap_delta (0)
 {
        DEBUG_TRACE (DEBUG::Drags, "New NoteResizeDrag\n");
@@ -2382,11 +2395,26 @@ NoteResizeDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*ignored*/)
                relative = true;
        }
 
-       /* select this note; if it is already selected, preserve the existing selection,
-          otherwise make this note the only one selected.
-       */
-       _editor->get_selection().clear_points();
-       region->note_selected (cnote, cnote->selected ());
+       if (!(_was_selected = cnote->selected())) {
+
+               /* 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.
+               */
+
+               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);
+                       }
+               }
+       }
 }
 
 void
@@ -2444,6 +2472,46 @@ void
 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;
        }
 
@@ -2671,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)
 {
@@ -2728,9 +2797,6 @@ TrimDrag::start_grab (GdkEvent* event, Gdk::Cursor*)
        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_duration (region_start, region_end);
@@ -2788,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) {
@@ -2991,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 ();
        }
 }
@@ -3049,10 +3117,13 @@ 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"));
        assert (_marker);
+       _real_section = &_marker->meter();
+
 }
 
 void
@@ -3072,21 +3143,12 @@ void
 MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
 {
        if (!_marker->meter().movable()) {
-               return;
+               //return;
        }
 
        if (first_move) {
 
-               // create a dummy marker for visual representation of moving the
-               // section, because whether its a copy or not, we're going to
-               // leave or lose the original marker (leave if its a copy; lose if its
-               // not, because we'll remove it from the map).
-
-               MeterSection section (_marker->meter());
-
-               if (!section.movable()) {
-                       return;
-               }
+               // create a dummy marker to catch events, then hide it.
 
                char name[64];
                snprintf (name, sizeof(name), "%g/%g", _marker->meter().divisions_per_bar(), _marker->meter().note_divisor ());
@@ -3101,20 +3163,65 @@ MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
 
                /* use the new marker for the grab */
                swap_grab (&_marker->the_item(), 0, GDK_CURRENT_TIME);
+               _marker->hide();
+
+               TempoMap& map (_editor->session()->tempo_map());
+               /* get current state */
+               before_state = &map.get_state();
 
                if (!_copy) {
-                       TempoMap& map (_editor->session()->tempo_map());
-                       /* get current state */
-                       before_state = &map.get_state();
-                       /* remove the section while we drag it */
-                       map.remove_meter (section, true);
+                       _editor->begin_reversible_command (_("move meter mark"));
+               } else {
+                       _editor->begin_reversible_command (_("copy meter mark"));
+
+                       Timecode::BBT_Time bbt = _real_section->bbt();
+
+                       /* we can't add a meter where one currently exists */
+                       if (_real_section->frame() < adjusted_current_frame (event, false)) {
+                               ++bbt.bars;
+                       } else {
+                               --bbt.bars;
+                       }
+                       const double beat = map.bbt_to_beats (bbt);
+
+                       if (_real_section->position_lock_style() == AudioTime) {
+                               _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor())
+                                                              , map.frame_time (bbt), beat, bbt);
+                       } else {
+                               _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor()), beat, bbt);
+                       }
                }
        }
 
-       framepos_t const pf = adjusted_current_frame (event);
-
+       framepos_t const pf = adjusted_current_frame (event, false);
+       if (_marker->meter().position_lock_style() == MusicTime) {
+               TempoMap& map (_editor->session()->tempo_map());
+               Timecode::BBT_Time bbt;
+               map.bbt_time (pf, bbt);
+               /* round bbt to bars */
+               map.round_bbt (bbt, -1, RoundNearest);
+
+               if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+                       /* adjust previous tempo to match meter frame */
+                       _editor->session()->tempo_map().gui_dilate_tempo (_real_section, pf);
+               } else if ((bbt.bars != _real_section->bbt().bars && pf > last_pointer_frame())
+                          || (bbt.bars < _real_section->bbt().bars && pf < last_pointer_frame())) {
+                       /* move meter beat-based */
+                       _editor->session()->tempo_map().gui_move_meter (_real_section, bbt);
+               }
+       } else {
+               /* AudioTime */
+               if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+                       /* currently disabled in the lib for AudioTime */
+                       if (_real_section->movable()) {
+                               _editor->session()->tempo_map().gui_dilate_tempo (_real_section, pf);
+                       }
+               } else {
+                       _editor->session()->tempo_map().gui_move_meter (_real_section, pf);
+               }
+       }
        _marker->set_position (pf);
-       show_verbose_cursor_time (pf);
+       show_verbose_cursor_time (_real_section->frame());
 }
 
 void
@@ -3127,35 +3234,11 @@ MeterMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                return;
        }
 
-       if (!_marker->meter().movable()) {
-               return;
-       }
-
-       motion (event, false);
-
-       Timecode::BBT_Time when;
-
        TempoMap& map (_editor->session()->tempo_map());
-       map.bbt_time (last_pointer_frame(), when);
-
-       if (_copy == true) {
-               _editor->begin_reversible_command (_("copy meter mark"));
-               XMLNode &before = map.get_state();
-               map.add_meter (_marker->meter(), when);
-               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 meter mark"));
 
-               /* we removed it before, so add it back now */
-
-               map.add_meter (_marker->meter(), when);
-               XMLNode &after = map.get_state();
-               _editor->session()->add_command(new MementoCommand<TempoMap>(map, before_state, &after));
-               _editor->commit_reversible_command ();
-       }
+       XMLNode &after = map.get_state();
+       _editor->session()->add_command(new MementoCommand<TempoMap>(map, before_state, &after));
+       _editor->commit_reversible_command ();
 
        // delete the dummy marker we used for visual representation while moving.
        // a new visual marker will show up automatically.
@@ -3166,11 +3249,8 @@ void
 MeterMarkerDrag::aborted (bool moved)
 {
        _marker->set_position (_marker->meter().frame ());
-
        if (moved) {
-               TempoMap& map (_editor->session()->tempo_map());
-               /* we removed it before, so add it back now */
-               map.add_meter (_marker->meter(), _marker->meter().frame());
+               _editor->session()->tempo_map().set_state (*before_state, Stateful::current_state_version);
                // delete the dummy marker we used for visual representation while moving.
                // a new visual marker will show up automatically.
                delete _marker;
@@ -3178,12 +3258,15 @@ 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();
+       _movable = _real_section->movable();
        assert (_marker);
 }
 
@@ -3191,7 +3274,11 @@ void
 TempoMarkerDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
 {
        Drag::start_grab (event, cursor);
-       show_verbose_cursor_time (adjusted_current_frame (event));
+       if (!_real_section->active()) {
+               show_verbose_cursor_text (_("inactive"));
+       } else {
+               show_verbose_cursor_time (adjusted_current_frame (event));
+       }
 }
 
 void
@@ -3203,19 +3290,12 @@ TempoMarkerDrag::setup_pointer_frame_offset ()
 void
 TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
 {
-       if (!_marker->tempo().movable()) {
+       if (!_real_section->active()) {
                return;
        }
-
        if (first_move) {
 
-               // create a dummy marker for visual representation of moving the
-               // section, because whether its a copy or not, we're going to
-               // leave or lose the original marker (leave if its a copy; lose if its
-               // not, because we'll remove it from the map).
-
-               // create a dummy marker for visual representation of moving the copy.
-               // The actual copying is not done before we reach the finish callback.
+               // mvc drag - create a dummy marker to catch events, hide it.
 
                char name[64];
                snprintf (name, sizeof (name), "%.2f", _marker->tempo().beats_per_minute());
@@ -3232,24 +3312,139 @@ TempoMarkerDrag::motion (GdkEvent* event, bool first_move)
 
                /* use the new marker for the grab */
                swap_grab (&_marker->the_item(), 0, GDK_CURRENT_TIME);
+               _marker->hide();
 
+               TempoMap& map (_editor->session()->tempo_map());
+               /* get current state */
+               before_state = &map.get_state();
                if (!_copy) {
-                       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);
+                       _editor->begin_reversible_command (_("move tempo mark"));
+               } else {
+                       _editor->begin_reversible_command (_("copy tempo mark"));
+                       framepos_t frame;
+                       bool use_snap = false;
+
+                       if (!_editor->snap_musical()) {
+                               frame = adjusted_current_frame (event);
+                       } else {
+                               frame = adjusted_current_frame (event, false);
+                               if (ArdourKeyboard::indicates_snap (event->button.state)) {
+                                       if (_editor->snap_mode() == Editing::SnapOff) {
+                                               use_snap = true;
+                                       } else {
+                                               use_snap = false;
+                                       }
+                               } else {
+                                       if (_editor->snap_mode() == Editing::SnapOff) {
+                                               use_snap = false;
+                                       } else {
+                                               use_snap = true;
+                                       }
+                               }
+                       }
+
+                       Timecode::BBT_Time bbt;
+                       map.bbt_time (frame, bbt);
+
+                       /* add new tempo section to map, ensuring we don't refer to existing tempos for snap */
+
+                       if (_real_section->position_lock_style() == MusicTime) {
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (bbt, -1, (frame > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (bbt, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               double const pulse = map.predict_tempo_pulse (_real_section, map.frame_time (bbt));
+                               _real_section = map.add_tempo (_marker->tempo(), pulse, _real_section->type());
+                       } else {
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (bbt, -1, (frame > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (bbt, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               if (use_snap) {
+                                       frame = map.predict_tempo_frame (_real_section, bbt);
+                               }
+                               _real_section = map.add_tempo (_marker->tempo(), frame, _real_section->type());
+                       }
                }
+
        }
 
-       framepos_t const pf = adjusted_current_frame (event);
+       framepos_t pf;
+
+       if (Keyboard::modifier_state_contains (event->button.state, ArdourKeyboard::constraint_modifier ())) {
+               double new_bpm = _real_section->beats_per_minute() + ((last_pointer_y() - current_pointer_y()) / 5.0);
+               _editor->session()->tempo_map().gui_change_tempo (_real_section, Tempo (new_bpm, _real_section->note_type()));
+               stringstream strs;
+               strs << new_bpm;
+               show_verbose_cursor_text (strs.str());
+       } else if (_movable && !_real_section->locked_to_meter()) {
+               if (!_editor->snap_musical()) {
+                       /* snap normally (this is not self-referential).*/
+                       pf = adjusted_current_frame (event);
+               } else {
+                       /* but this is.
+                          we can't use the map for anything related to tempo,
+                          so we round bbt using meters, which have no dependency
+                          on pulse for this kind of thing.
+                       */
+                       bool use_snap;
+                       TempoMap& map (_editor->session()->tempo_map());
+
+                       pf = adjusted_current_frame (event, false);
+                       if (ArdourKeyboard::indicates_snap (event->button.state)) {
+                               if (_editor->snap_mode() == Editing::SnapOff) {
+                                       use_snap = true;
+                               } else {
+                                       use_snap = false;
+                               }
+                       } else {
+                               if (_editor->snap_mode() == Editing::SnapOff) {
+                                       use_snap = false;
+                               } else {
+                                       use_snap = true;
+                               }
+                       }
+
+                       Timecode::BBT_Time when;
+                       map.bbt_time (pf, when);
+
+                       if (_real_section->position_lock_style() == MusicTime) {
+
+                               const double pulse = map.predict_tempo_pulse (_real_section, pf);
+                               when = map.pulse_to_bbt (pulse);
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (when, -1, (pf > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (when, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               const double beat = map.bbt_to_beats (when);
+                               map.gui_move_tempo_beat (_real_section, beat);
+                       } else {
+                               if (use_snap && _editor->snap_type() == SnapToBar) {
+                                       map.round_bbt (when, -1, (pf > _real_section->frame()) ? RoundUpMaybe : RoundDownMaybe);
+                               } else if (use_snap) {
+                                       map.round_bbt (when, _editor->get_grid_beat_divisions (0), RoundNearest);
+                               }
+                               if (use_snap) {
+                                       pf = map.predict_tempo_frame (_real_section, when);
+                               }
+                               map.gui_move_tempo_frame (_real_section, pf);
+                       }
+               }
+
+               show_verbose_cursor_time (_real_section->frame());
+       }
        _marker->set_position (pf);
-       show_verbose_cursor_time (pf);
 }
 
 void
 TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
 {
+       if (!_real_section->active()) {
+               return;
+       }
        if (!movement_occurred) {
                if (was_double_click()) {
                        _editor->edit_tempo_marker (*_marker);
@@ -3257,34 +3452,11 @@ TempoMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                return;
        }
 
-       if (!_marker->tempo().movable()) {
-               return;
-       }
-
-       motion (event, false);
-
        TempoMap& map (_editor->session()->tempo_map());
-       framepos_t beat_time = map.round_to_beat (last_pointer_frame(), RoundNearest);
-       Timecode::BBT_Time when;
-
-       map.bbt_time (beat_time, when);
 
-       if (_copy == true) {
-               _editor->begin_reversible_command (_("copy tempo mark"));
-               XMLNode &before = map.get_state();
-               map.add_tempo (_marker->tempo(), when);
-               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);
-               XMLNode &after = map.get_state();
-               _editor->session()->add_command (new MementoCommand<TempoMap>(map, before_state, &after));
-               _editor->commit_reversible_command ();
-       }
+       XMLNode &after = map.get_state();
+       _editor->session()->add_command (new MementoCommand<TempoMap>(map, before_state, &after));
+       _editor->commit_reversible_command ();
 
        // delete the dummy marker we used for visual representation while moving.
        // a new visual marker will show up automatically.
@@ -3297,8 +3469,7 @@ TempoMarkerDrag::aborted (bool moved)
        _marker->set_position (_marker->tempo().frame());
        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.set_state (*before_state, Stateful::current_state_version);
                // delete the dummy marker we used for visual representation while moving.
                // a new visual marker will show up automatically.
                delete _marker;
@@ -3309,6 +3480,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");
 }
@@ -3687,14 +3859,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"));
+
        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 ()
@@ -3731,6 +3906,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);
 
@@ -3741,6 +3917,7 @@ 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:
@@ -3770,11 +3947,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;
        }
 
@@ -3828,8 +4008,9 @@ 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_contains (event->button.state, ArdourKeyboard::push_points_modifier ())) {
@@ -3919,10 +4100,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);
                                }
@@ -3935,7 +4116,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);
                                }
@@ -3979,17 +4160,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:
@@ -3997,6 +4180,11 @@ 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;
        }
 
@@ -4040,9 +4228,9 @@ MarkerDrag::finished (GdkEvent* event, bool movement_occurred)
 }
 
 void
-MarkerDrag::aborted (bool movement_occured)
+MarkerDrag::aborted (bool movement_occurred)
 {
-       if (!movement_occured) {
+       if (!movement_occurred) {
                return;
        }
 
@@ -4070,10 +4258,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());
@@ -4141,15 +4332,15 @@ ControlPointDrag::motion (GdkEvent* event, bool first_motion)
        _cumulative_x_drag = cx - _fixed_grab_x;
        _cumulative_y_drag = cy - _fixed_grab_y;
 
+       cx = max (0.0, cx);
+       cy = max (0.0, cy);
+       cy = min ((double) _point->line().height(), cy);
+
        // 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;
        }
 
-       cx = max (0.0, cx);
-       cy = max (0.0, cy);
-       cy = min ((double) _point->line().height(), cy);
-
        framepos_t cx_frames = _editor->pixel_to_sample (cx) + snap_delta (event->button.state);
 
        if (!_x_constrained && need_snap) {
@@ -4162,13 +4353,14 @@ ControlPointDrag::motion (GdkEvent* event, bool first_motion)
        float const fraction = 1.0 - (cy / _point->line().height());
 
        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, fraction);
+               _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);
 
-       _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
@@ -4182,7 +4374,6 @@ ControlPointDrag::finished (GdkEvent* event, bool movement_occurred)
                }
 
        } else {
-               motion (event, false);
                _point->line().end_drag (_pushing, _final_index);
                _editor->commit_reversible_command ();
        }
@@ -4209,6 +4400,8 @@ ControlPointDrag::active (Editing::MouseMode m)
 LineDrag::LineDrag (Editor* e, ArdourCanvas::Item* i)
        : Drag (e, i)
        , _line (0)
+       , _fixed_grab_x (0.0)
+       , _fixed_grab_y (0.0)
        , _cumulative_y_drag (0)
        , _before (0)
        , _after (0)
@@ -4228,12 +4421,12 @@ LineDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*cursor*/)
           origin, and ditto for y.
        */
 
-       double cx = event->button.x;
-       double cy = event->button.y;
+       double mx = event->button.x;
+       double my = event->button.y;
 
-       _line->parent_group().canvas_to_item (cx, cy);
+       _line->grab_item().canvas_to_item (mx, my);
 
-       framecnt_t const frame_within_region = (framecnt_t) floor (cx * _editor->samples_per_pixel);
+       framecnt_t const frame_within_region = (framecnt_t) floor (mx * _editor->samples_per_pixel);
 
        if (!_line->control_points_adjacent (frame_within_region, _before, _after)) {
                /* no adjacent points */
@@ -4242,9 +4435,14 @@ LineDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*cursor*/)
 
        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);
+
+       double const cy = ((_line->nth (_before)->get_y() * click_ratio) + (_line->nth (_after)->get_y() * (1 - click_ratio)));
 
-       _fixed_grab_x = cx;
+       _fixed_grab_x = mx;
        _fixed_grab_y = cy;
 
        double fraction = 1.0 - (cy / _line->height());
@@ -4272,20 +4470,23 @@ LineDrag::motion (GdkEvent* event, bool first_move)
        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, fraction);
+               _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 ();
@@ -4295,14 +4496,19 @@ LineDrag::finished (GdkEvent* event, bool movement_occured)
                AutomationTimeAxisView* atv;
 
                if ((atv = dynamic_cast<AutomationTimeAxisView*>(_editor->clicked_axisview)) != 0) {
-                       framepos_t where = _editor->canvas_event_sample (event, 0, 0);
+                       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, event->button.y, false);
+                       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_canvas_group (), event, false);
+                               arv->add_gain_point_event (&arv->get_gain_line()->grab_item(), event, false);
                        }
                }
        }
@@ -4317,7 +4523,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");
 }
@@ -4538,7 +4748,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;
                        }
                }
@@ -4604,29 +4814,39 @@ TimeFXDrag::motion (GdkEvent* event, bool)
 }
 
 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) {
+
+               motion (event, false);
+
+               _primary->get_time_axis_view().hide_timestretch ();
 
-       framecnt_t newlen = last_pointer_frame() - _primary->region()->position();
+               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;
+               }
 
-       float percentage = (double) newlen / (double) _primary->region()->length();
+               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
@@ -4635,7 +4855,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;
                }
        }
@@ -5027,7 +5247,7 @@ 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 (UIConfiguration::instance().color ("range drag rect"));
@@ -5249,9 +5469,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 ();
        }
 }
@@ -5270,6 +5490,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");
 
@@ -5345,7 +5566,8 @@ NoteDrag::total_dx (const guint state) const
 
        frameoffset_t ret;
        if (snap) {
-               ret =  _region->snap_frame_to_frame (st - rp) + rp - n - snap_delta (state);
+               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);
        }
@@ -5392,13 +5614,9 @@ NoteDrag::motion (GdkEvent * event, 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);
        }
 }
 
@@ -5691,9 +5909,10 @@ AutomationRangeDrag::motion (GdkEvent*, bool first_move)
        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));
        }
 }