Editor does not need to reset track/stripable selection, since Selection already...
[ardour.git] / gtk2_ardour / editor_drag.cc
index aaa306f70f1d10bb4a7725ced1ccb2376c9914b2..f7b75a5855dee56e32f4991e194bba91b26ccebb 100644 (file)
@@ -44,7 +44,7 @@
 #include "canvas/scroll_group.h"
 
 #include "editor.h"
-#include "i18n.h"
+#include "pbd/i18n.h"
 #include "keyboard.h"
 #include "audio_region_view.h"
 #include "automation_region_view.h"
@@ -237,6 +237,7 @@ Drag::Drag (Editor* e, ArdourCanvas::Item* i, bool trackview_only)
        , _grab_frame (0)
        , _last_pointer_frame (0)
        , _snap_delta (0)
+       , _constraint_pressed (false)
 {
 
 }
@@ -261,6 +262,7 @@ Drag::start_grab (GdkEvent* event, Gdk::Cursor *cursor)
 {
 
        /* we set up x/y dragging constraints on first move */
+       _constraint_pressed = ArdourKeyboard::indicates_constraint (event->button.state);
 
        _raw_grab_frame = _editor->canvas_event_sample (event, &_grab_x, &_grab_y);
 
@@ -420,14 +422,14 @@ Drag::motion_handler (GdkEvent* event, bool from_autoscroll)
                                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 ())) {
+                                               if (_constraint_pressed) {
                                                        _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 ())) {
+                                       } else if (_constraint_pressed) {
                                                // if dragging normally, the motion is constrained to the first direction of movement.
                                                if (_initially_vertical) {
                                                        _x_constrained = true;
@@ -464,7 +466,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;
@@ -510,28 +512,24 @@ 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, const int32_t sub_num)
 {
        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 (max (0.0, map.beat_at_frame (pos)) + 1.0) - pos;
+               return view->add_region (grab_frame(), len, commit, sub_num);
        }
 
        return boost::shared_ptr<Region>();
 }
 
-struct EditorOrderTimeAxisViewSorter {
+struct PresentationInfoTimeAxisViewSorter {
        bool operator() (TimeAxisView* a, TimeAxisView* b) {
-               RouteTimeAxisView* ra = dynamic_cast<RouteTimeAxisView*> (a);
-               RouteTimeAxisView* rb = dynamic_cast<RouteTimeAxisView*> (b);
-               assert (ra && rb);
-               return ra->route()->order_key () < rb->route()->order_key ();
+               return a->stripable()->presentation_info().order() < b->stripable()->presentation_info().order();
        }
 };
 
@@ -547,7 +545,7 @@ RegionDrag::RegionDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list<Re
        */
 
        TrackViewList track_views = _editor->track_views;
-       track_views.sort (EditorOrderTimeAxisViewSorter ());
+       track_views.sort (PresentationInfoTimeAxisViewSorter ());
 
        for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                _time_axis_views.push_back (*i);
@@ -645,7 +643,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;
@@ -657,11 +655,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;
@@ -846,7 +845,7 @@ RegionMotionDrag::motion (GdkEvent* event, bool first_move)
 
                RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (tv);
                if (!rtv || !rtv->is_track()) {
-                       /* ignore busses early on. we can't move any regions on them */
+                       /* ignore non-tracks 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)
@@ -1259,8 +1258,8 @@ RegionMoveDrag::motion (GdkEvent* event, bool first_move)
                        MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(rv);
 
                        const boost::shared_ptr<const Region> original = rv->region();
-                       boost::shared_ptr<Region> region_copy = RegionFactory::create (original, true);
-                       region_copy->set_position (original->position());
+                       boost::shared_ptr<Region> region_copy = RegionFactory::create (original, true
+                                                                                      , _editor->get_grid_music_divisions (event->button.state));
                        /* need to set this so that the drop zone code can work. This doesn't
                           actually put the region into the playlist, but just sets a weak pointer
                           to it.
@@ -1367,7 +1366,8 @@ RegionMoveDrag::finished (GdkEvent* ev, bool movement_occurred)
                finished_copy (
                        changed_position,
                        changed_tracks,
-                       drag_delta
+                       drag_delta,
+                       ev->button.state
                        );
 
        } else {
@@ -1375,7 +1375,8 @@ RegionMoveDrag::finished (GdkEvent* ev, bool movement_occurred)
                finished_no_copy (
                        changed_position,
                        changed_tracks,
-                       drag_delta
+                       drag_delta,
+                       ev->button.state
                        );
 
        }
@@ -1397,21 +1398,23 @@ RegionMoveDrag::create_destination_time_axis (boost::shared_ptr<Region> region,
                        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());
+                       audio_tracks = _editor->session()->new_audio_track (region->n_channels(), output_chan, 0, 1, region->name(), PresentationInfo::max_order);
+                       TimeAxisView* tav =_editor->axis_view_from_stripable (audio_tracks.front());
+                       if (tav) {
+                               tav->set_height (original->current_height());
                        }
-                       return rtav;
+                       return dynamic_cast<RouteTimeAxisView*>(tav);
                } else {
                        ChanCount one_midi_port (DataType::MIDI, 1);
                        list<boost::shared_ptr<MidiTrack> > midi_tracks;
-                       midi_tracks = _editor->session()->new_midi_track (one_midi_port, one_midi_port, boost::shared_ptr<ARDOUR::PluginInfo>(), ARDOUR::Normal, 0, 1, region->name());
-                       RouteTimeAxisView* rtav = _editor->axis_view_from_route (midi_tracks.front());
-                       if (rtav) {
-                               rtav->set_height (original->current_height());
+                       midi_tracks = _editor->session()->new_midi_track (one_midi_port, one_midi_port, boost::shared_ptr<ARDOUR::PluginInfo>(),
+                                                                         (ARDOUR::Plugin::PresetRecord*) 0,
+                                                                         (ARDOUR::RouteGroup*) 0, 1, region->name(), PresentationInfo::max_order);
+                       TimeAxisView* tav = _editor->axis_view_from_stripable (midi_tracks.front());
+                       if (tav) {
+                               tav->set_height (original->current_height());
                        }
-                       return rtav;
+                       return dynamic_cast<RouteTimeAxisView*> (tav);
                }
        } catch (...) {
                error << _("Could not create new track after region placed in the drop zone") << endmsg;
@@ -1420,7 +1423,7 @@ RegionMoveDrag::create_destination_time_axis (boost::shared_ptr<Region> region,
 }
 
 void
-RegionMoveDrag::finished_copy (bool const changed_position, bool const /*changed_tracks*/, framecnt_t const drag_delta)
+RegionMoveDrag::finished_copy (bool const changed_position, bool const /*changed_tracks*/, framecnt_t const drag_delta, int32_t const ev_state)
 {
        RegionSelection new_views;
        PlaylistSet modified_playlists;
@@ -1477,7 +1480,9 @@ RegionMoveDrag::finished_copy (bool const changed_position, bool const /*changed
                }
 
                if (dest_rtv != 0) {
-                       RegionView* new_view = insert_region_into_playlist (i->view->region(), dest_rtv, i->layer, where, modified_playlists);
+                       RegionView* new_view = insert_region_into_playlist (i->view->region(), dest_rtv, i->layer, where,
+                                                                           modified_playlists, _editor->get_grid_music_divisions (ev_state));
+
                        if (new_view != 0) {
                                new_views.push_back (new_view);
                        }
@@ -1511,7 +1516,8 @@ void
 RegionMoveDrag::finished_no_copy (
        bool const changed_position,
        bool const changed_tracks,
-       framecnt_t const drag_delta
+       framecnt_t const drag_delta,
+       int32_t const ev_state
        )
 {
        RegionSelection new_views;
@@ -1572,7 +1578,8 @@ RegionMoveDrag::finished_no_copy (
                        /* insert into new playlist */
 
                        RegionView* new_view = insert_region_into_playlist (
-                               RegionFactory::create (rv->region (), true), dest_rtv, dest_layer, where, modified_playlists
+                               RegionFactory::create (rv->region (), true), dest_rtv, dest_layer, where,
+                               modified_playlists, _editor->get_grid_music_divisions (ev_state)
                                );
 
                        if (new_view == 0) {
@@ -1632,7 +1639,7 @@ RegionMoveDrag::finished_no_copy (
                                playlist->freeze ();
                        }
 
-                       rv->region()->set_position (where);
+                       rv->region()->set_position (where, _editor->get_grid_music_divisions (ev_state));
                        _editor->session()->add_command (new StatefulDiffCommand (rv->region()));
                }
 
@@ -1735,7 +1742,8 @@ RegionMoveDrag::insert_region_into_playlist (
        RouteTimeAxisView* dest_rtv,
        layer_t dest_layer,
        framecnt_t where,
-       PlaylistSet& modified_playlists
+       PlaylistSet& modified_playlists,
+       const int32_t sub_num
        )
 {
        boost::shared_ptr<Playlist> dest_playlist = dest_rtv->playlist ();
@@ -1752,8 +1760,7 @@ RegionMoveDrag::insert_region_into_playlist (
        if (r.second) {
                dest_playlist->clear_changes ();
        }
-
-       dest_playlist->add_region (region, where);
+       dest_playlist->add_region (region, where, 1.0, false, sub_num);
 
        if (dest_rtv->view()->layer_display() == Stacked || dest_rtv->view()->layer_display() == Expanded) {
                dest_playlist->set_layer (region, dest_layer);
@@ -1877,7 +1884,7 @@ RegionInsertDrag::RegionInsertDrag (Editor* e, boost::shared_ptr<Region> r, Rout
 }
 
 void
-RegionInsertDrag::finished (GdkEvent *, bool)
+RegionInsertDrag::finished (GdkEvent * event, bool)
 {
        int pos = _views.front().time_axis_view;
        assert(pos >= 0 && pos < (int)_time_axis_views.size());
@@ -2303,13 +2310,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, _editor->get_grid_music_divisions (event->button.state));
                _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
@@ -2320,18 +2328,19 @@ RegionCreateDrag::motion (GdkEvent* event, bool first_move)
                        */
 
                        framecnt_t const len = (framecnt_t) fabs ((double)(f - grab_frame () - 1));
-                       _region->set_length (len < 1 ? 1 : len);
+                       _region->set_length (len < 1 ? 1 : len, _editor->get_grid_music_divisions (event->button.state));
                }
        }
 }
 
 void
-RegionCreateDrag::finished (GdkEvent*, bool movement_occurred)
+RegionCreateDrag::finished (GdkEvent* event, bool movement_occurred)
 {
        if (!movement_occurred) {
-               add_midi_region (_view);
+               add_midi_region (_view, true, _editor->get_grid_music_divisions (event->button.state));
        } else {
                _view->playlist()->thaw ();
+               _editor->commit_reversible_command();
        }
 }
 
@@ -2795,9 +2804,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);
@@ -2852,9 +2858,12 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
 
                for (list<DraggingView>::const_iterator i = _views.begin(); i != _views.end(); ++i) {
                        RegionView* rv = i->view;
-                       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) {
@@ -2909,7 +2918,9 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
        switch (_operation) {
        case StartTrim:
                for (list<DraggingView>::iterator i = _views.begin(); i != _views.end(); ++i) {
-                       bool changed = i->view->trim_front (i->initial_position + dt, non_overlap_trim);
+                       bool changed = i->view->trim_front (i->initial_position + dt, non_overlap_trim
+                                                           , _editor->get_grid_music_divisions (event->button.state));
+
                        if (changed && _preserve_fade_anchor) {
                                AudioRegionView* arv = dynamic_cast<AudioRegionView*> (i->view);
                                if (arv) {
@@ -2927,7 +2938,7 @@ TrimDrag::motion (GdkEvent* event, bool first_move)
 
        case EndTrim:
                for (list<DraggingView>::iterator i = _views.begin(); i != _views.end(); ++i) {
-                       bool changed = i->view->trim_end (i->initial_end + dt, non_overlap_trim);
+                       bool changed = i->view->trim_end (i->initial_end + dt, non_overlap_trim, _editor->get_grid_music_divisions (event->button.state));
                        if (changed && _preserve_fade_anchor) {
                                AudioRegionView* arv = dynamic_cast<AudioRegionView*> (i->view);
                                if (arv) {
@@ -3058,14 +3069,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 ();
        }
 }
@@ -3078,10 +3087,11 @@ TrimDrag::aborted (bool movement_occurred)
           behind which may be slightly odd from the user's point of view.
        */
 
-       finished (0, true);
+       GdkEvent ev;
+       finished (&ev, true);
 
        if (movement_occurred) {
-               _editor->undo ();
+               _editor->session()->undo (1);
        }
 
        for (list<DraggingView>::const_iterator i = _views.begin(); i != _views.end(); ++i) {
@@ -3114,13 +3124,17 @@ TrimDrag::setup_pointer_frame_offset ()
 }
 
 MeterMarkerDrag::MeterMarkerDrag (Editor* e, ArdourCanvas::Item* i, bool c)
-       : Drag (e, i),
-         _copy (c)
+       : Drag (e, i)
+       , _copy (c)
+       , _old_snap_type (e->snap_type())
+       , _old_snap_mode (e->snap_mode())
        , 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
@@ -3139,22 +3153,8 @@ MeterMarkerDrag::setup_pointer_frame_offset ()
 void
 MeterMarkerDrag::motion (GdkEvent* event, bool first_move)
 {
-       if (!_marker->meter().movable()) {
-               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 ());
@@ -3169,20 +3169,54 @@ 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.beat_at_bbt (bbt);
+                       _real_section = map.add_meter (Meter (_marker->meter().divisions_per_bar(), _marker->meter().note_divisor())
+                                                      , beat, bbt, map.frame_at_bbt (bbt), _real_section->position_lock_style());
+                       if (!_real_section) {
+                               aborted (true);
+                               return;
+                       }
+
+               }
+               /* only snap to bars. leave snap mode alone for audio locked meters.*/
+               if (_real_section->position_lock_style() != AudioTime) {
+                       _editor->set_snap_to (SnapToBar);
+                       _editor->set_snap_mode (SnapNormal);
                }
        }
 
-       framepos_t const pf = adjusted_current_frame (event);
+       framepos_t pf = adjusted_current_frame (event);
 
-       _marker->set_position (pf);
-       show_verbose_cursor_time (pf);
+       if (_real_section->position_lock_style() == AudioTime && _editor->snap_musical()) {
+               /* never snap to music for audio locked */
+               pf = adjusted_current_frame (event, false);
+       }
+
+       _editor->session()->tempo_map().gui_move_meter (_real_section, pf);
+
+       /* fake marker meeds to stay under the mouse, unlike the real one. */
+       _marker->set_position (adjusted_current_frame (event, false));
+
+       show_verbose_cursor_time (_real_section->frame());
 }
 
 void
@@ -3195,35 +3229,15 @@ MeterMarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                return;
        }
 
-       if (!_marker->meter().movable()) {
-               return;
-       }
-
-       motion (event, false);
-
-       Timecode::BBT_Time when;
+       /* reinstate old snap setting */
+       _editor->set_snap_to (_old_snap_type);
+       _editor->set_snap_mode (_old_snap_mode);
 
        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.
@@ -3234,11 +3248,12 @@ 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());
+               /* reinstate old snap setting */
+               _editor->set_snap_to (_old_snap_type);
+               _editor->set_snap_mode (_old_snap_mode);
+
+               _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;
@@ -3253,6 +3268,8 @@ TempoMarkerDrag::TempoMarkerDrag (Editor* e, ArdourCanvas::Item* i, bool c)
        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);
 }
 
@@ -3260,31 +3277,29 @@ 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
 TempoMarkerDrag::setup_pointer_frame_offset ()
 {
-       _pointer_frame_offset = raw_grab_frame() - _marker->tempo().frame();
+       _pointer_frame_offset = raw_grab_frame() - _real_section->frame();
 }
 
 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());
@@ -3301,24 +3316,75 @@ 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 {
+                       const Tempo tempo (_marker->tempo());
+                       const framepos_t frame = adjusted_current_frame (event) + 1;
+                       const TempoSection::Type type = _real_section->type();
+
+                       _editor->begin_reversible_command (_("copy tempo mark"));
+
+                       if (_real_section->position_lock_style() == MusicTime) {
+                               _real_section = map.add_tempo (tempo, map.pulse_at_frame (frame), 0, type, MusicTime);
+                       } else {
+                               _real_section = map.add_tempo (tempo, 0.0, frame, type, AudioTime);
+                       }
+
+                       if (!_real_section) {
+                               aborted (true);
+                               return;
+                       }
                }
+
        }
 
-       framepos_t const pf = adjusted_current_frame (event);
-       _marker->set_position (pf);
-       show_verbose_cursor_time (pf);
+       if (ArdourKeyboard::indicates_constraint (event->button.state)) {
+               /* use vertical movement to alter tempo .. should be log */
+               double new_bpm = _real_section->beats_per_minute() + ((last_pointer_y() - current_pointer_y()) / 5.0);
+               stringstream strs;
+
+               _editor->session()->tempo_map().gui_change_tempo (_real_section, Tempo (new_bpm, _real_section->note_type()));
+               strs << new_bpm;
+               show_verbose_cursor_text (strs.str());
+
+       } else if (_movable && !_real_section->locked_to_meter()) {
+               framepos_t pf;
+
+               if (_editor->snap_musical()) {
+                       /* we can't snap to a grid that we are about to move.
+                        * gui_move_tempo() will sort out snap using the supplied beat divisions.
+                       */
+                       pf = adjusted_current_frame (event, false);
+               } else {
+                       pf = adjusted_current_frame (event);
+               }
+
+               TempoMap& map (_editor->session()->tempo_map());
+
+               /* snap to beat is 1, snap to bar is -1 (sorry) */
+               const int sub_num = _editor->get_grid_music_divisions (event->button.state);
+
+               map.gui_move_tempo (_real_section, pf, sub_num);
+
+               show_verbose_cursor_time (_real_section->frame());
+       }
+       _marker->set_position (adjusted_current_frame (event, false));
 }
 
 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);
@@ -3326,34 +3392,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.
@@ -3366,14 +3409,112 @@ 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());
-               // delete the dummy marker we used for visual representation while moving.
-               // a new visual marker will show up automatically.
+               map.set_state (*before_state, Stateful::current_state_version);
+               // delete the dummy (hidden) marker we used for events while moving.
                delete _marker;
        }
 }
 
+BBTRulerDrag::BBTRulerDrag (Editor* e, ArdourCanvas::Item* i)
+       : Drag (e, i)
+       , _pulse (0.0)
+       , _tempo (0)
+       , before_state (0)
+{
+       DEBUG_TRACE (DEBUG::Drags, "New BBTRulerDrag\n");
+
+}
+
+void
+BBTRulerDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
+{
+       Drag::start_grab (event, cursor);
+       TempoMap& map (_editor->session()->tempo_map());
+       _tempo = const_cast<TempoSection*> (&map.tempo_section_at_frame (raw_grab_frame()));
+       ostringstream sstr;
+
+       sstr << "^" << fixed << setprecision(3) << map.tempo_at_frame (adjusted_current_frame (event)).beats_per_minute() << "\n";
+       sstr << "<" << fixed << setprecision(3) << _tempo->beats_per_minute();
+       show_verbose_cursor_text (sstr.str());
+       finished (event, false);
+}
+
+void
+BBTRulerDrag::setup_pointer_frame_offset ()
+{
+       TempoMap& map (_editor->session()->tempo_map());
+       const double beat_at_frame = max (0.0, map.beat_at_frame (raw_grab_frame()));
+       const uint32_t divisions = _editor->get_grid_beat_divisions (0);
+       double beat = 0.0;
+
+       if (divisions > 0) {
+               beat = floor (beat_at_frame) + (floor (((beat_at_frame - floor (beat_at_frame)) * divisions)) / divisions);
+       } else {
+               /* while it makes some sense for the user to determine the division to 'grab',
+                  grabbing a bar often leads to confusing results wrt the actual tempo section being altered
+                  and the result over steep tempo curves. Use sixteenths.
+               */
+               beat = floor (beat_at_frame) + (floor (((beat_at_frame - floor (beat_at_frame)) * 4)) / 4);
+       }
+
+       _pulse = map.pulse_at_beat (beat);
+
+       _pointer_frame_offset = raw_grab_frame() - map.frame_at_pulse (_pulse);
+
+}
+
+void
+BBTRulerDrag::motion (GdkEvent* event, bool first_move)
+{
+       TempoMap& map (_editor->session()->tempo_map());
+
+       if (first_move) {
+               /* get current state */
+               before_state = &map.get_state();
+               _editor->begin_reversible_command (_("dilate tempo"));
+       }
+
+       framepos_t pf;
+
+       if (_editor->snap_musical()) {
+               pf = adjusted_current_frame (event, false);
+       } else {
+               pf = adjusted_current_frame (event);
+       }
+
+       if (ArdourKeyboard::indicates_constraint (event->button.state)) {
+               /* adjust previous tempo to match pointer frame */
+               _editor->session()->tempo_map().gui_dilate_tempo (_tempo, map.frame_at_pulse (_pulse), pf, _pulse);
+       }
+       ostringstream sstr;
+       sstr << "^" << fixed << setprecision(3) << map.tempo_at_frame (pf).beats_per_minute() << "\n";
+       sstr << "<" << fixed << setprecision(3) << _tempo->beats_per_minute();
+       show_verbose_cursor_text (sstr.str());
+}
+
+void
+BBTRulerDrag::finished (GdkEvent* event, bool movement_occurred)
+{
+       if (!movement_occurred) {
+               return;
+       }
+
+       TempoMap& map (_editor->session()->tempo_map());
+
+       XMLNode &after = map.get_state();
+       _editor->session()->add_command(new MementoCommand<TempoMap>(map, before_state, &after));
+       _editor->commit_reversible_command ();
+}
+
+void
+BBTRulerDrag::aborted (bool moved)
+{
+       if (moved) {
+               _editor->session()->tempo_map().set_state (*before_state, Stateful::current_state_version);
+       }
+}
+
+
 CursorDrag::CursorDrag (Editor* e, EditorCursor& c, bool s)
        : Drag (e, &c.track_canvas_item(), false)
        , _cursor (c)
@@ -3760,12 +3901,14 @@ MarkerDrag::MarkerDrag (Editor* e, ArdourCanvas::Item* 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 ()
@@ -3996,7 +4139,7 @@ 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);
@@ -4113,6 +4256,10 @@ MarkerDrag::finished (GdkEvent* event, bool movement_occurred)
                        } else {
                                location->set (((*x).location)->start(), ((*x).location)->end());
                        }
+
+                       if (location->is_session_range()) {
+                               _editor->session()->set_end_is_free (false);
+                       }
                }
        }
 
@@ -4124,9 +4271,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;
        }
 
@@ -4320,7 +4467,7 @@ LineDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*cursor*/)
        double mx = event->button.x;
        double my = event->button.y;
 
-       _line->parent_group().canvas_to_item (mx, my);
+       _line->grab_item().canvas_to_item (mx, my);
 
        framecnt_t const frame_within_region = (framecnt_t) floor (mx * _editor->samples_per_pixel);
 
@@ -4331,7 +4478,7 @@ 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);
@@ -4380,9 +4527,9 @@ LineDrag::motion (GdkEvent* event, bool first_move)
 }
 
 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 ();
@@ -4392,14 +4539,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;
 
-                       atv->add_automation_event (event, where, event->button.y, false);
+                       _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_canvas_group (), event, false);
+                               arv->add_gain_point_event (&arv->get_gain_line()->grab_item(), event, false);
                        }
                }
        }
@@ -4639,7 +4791,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, _editor->get_grid_music_divisions(event->button.state));
                                do_deselect = false;
                        }
                }
@@ -4705,29 +4857,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) {
 
-       framecnt_t newlen = last_pointer_frame() - _primary->region()->position();
+               motion (event, false);
+
+               _primary->get_time_axis_view().hide_timestretch ();
 
-       float percentage = (double) newlen / (double) _primary->region()->length();
+               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
@@ -4736,7 +4898,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;
                }
        }
@@ -4956,7 +5118,7 @@ SelectionDrag::motion (GdkEvent* event, bool first_move)
                        TrackViewList grouped_add = new_selection;
                        for (TrackViewList::const_iterator i = new_selection.begin(); i != new_selection.end(); ++i) {
                                RouteTimeAxisView *n = dynamic_cast<RouteTimeAxisView *>(*i);
-                               if ( n && n->route()->route_group() && n->route()->route_group()->is_active() && n->route()->route_group()->enabled_property (ARDOUR::Properties::select.property_id) ) {
+                               if ( n && n->route()->route_group() && n->route()->route_group()->is_active() && n->route()->route_group()->enabled_property (ARDOUR::Properties::group_select.property_id) ) {
                                        for (TrackViewList::const_iterator j = all_tracks.begin(); j != all_tracks.end(); ++j) {
                                                RouteTimeAxisView *check = dynamic_cast<RouteTimeAxisView *>(*j);
                                                if ( check && (n != check) && (check->route()->route_group() == n->route()->route_group()) )
@@ -5128,7 +5290,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"));
@@ -5350,9 +5512,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 ();
        }
 }
@@ -5413,11 +5575,17 @@ NoteDrag::start_grab (GdkEvent* event, Gdk::Cursor *)
 frameoffset_t
 NoteDrag::total_dx (const guint state) const
 {
+       if (_x_constrained) {
+               return 0;
+       }
+       TempoMap& map (_editor->session()->tempo_map());
+
        /* dx in frames */
        frameoffset_t const dx = _editor->pixel_to_sample (_drags->current_pointer_x() - grab_x());
 
        /* primary note time */
-       frameoffset_t const n = _region->source_beats_to_absolute_frames (_primary->note()->time ());
+       double const quarter_note_start = map.quarter_note_at_beat (_region->region()->beat() - _region->midi_region()->start_beats().to_double());
+       frameoffset_t const n = map.frame_at_quarter_note (quarter_note_start + _primary->note()->time().to_double());
 
        /* new time of the primary note in session frames */
        frameoffset_t st = n + dx + snap_delta (state);
@@ -5447,7 +5615,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);
        }
@@ -5458,6 +5627,10 @@ NoteDrag::total_dx (const guint state) const
 int8_t
 NoteDrag::total_dy () const
 {
+       if (_y_constrained) {
+               return 0;
+       }
+
        MidiStreamView* msv = _region->midi_stream_view ();
        double const y = _region->midi_view()->y_position ();
        /* new current note */
@@ -5477,8 +5650,8 @@ NoteDrag::motion (GdkEvent * event, bool)
        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 */
-       double const tdx = _editor->sample_to_pixel (dx) - _cumulative_dx;
-       double const tdy = -dy * _note_height - _cumulative_dy;
+       double const tdx = _x_constrained ? 0 : _editor->sample_to_pixel (dx) - _cumulative_dx;
+       double const tdy = _y_constrained ? 0 : -dy * _note_height - _cumulative_dy;
 
        if (tdx || tdy) {
                _cumulative_dx += tdx;
@@ -5486,21 +5659,19 @@ NoteDrag::motion (GdkEvent * event, bool)
 
                int8_t note_delta = total_dy();
 
-               _region->move_selection (tdx, tdy, note_delta);
+               if (tdx || tdy) {
+                       _region->move_selection (tdx, tdy, note_delta);
 
-               /* the new note value may be the same as the old one, but we
-                * don't know what that means because the selection may have
-                * involved more than one note and we might be doing something
-                * 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);
+                       /* the new note value may be the same as the old one, but we
+                        * don't know what that means because the selection may have
+                        * involved more than one note and we might be doing something
+                        * odd with them. so show the note value anyway, always.
+                        */
 
-               snprintf (buf, sizeof (buf), "%s (%d)", Evoral::midi_note_name (new_note).c_str(),
-                         (int) floor ((double)new_note));
+                       uint8_t new_note = min (max (_primary->note()->note() + note_delta, 0), 127);
 
-               show_verbose_cursor_text (buf);
+                       _region->show_verbose_cursor_for_new_note_value (_primary->note(), new_note);
+               }
        }
 }
 
@@ -5867,6 +6038,9 @@ void
 PatchChangeDrag::finished (GdkEvent* ev, bool movement_occurred)
 {
        if (!movement_occurred) {
+               if (was_double_click()) {
+                       _region_view->edit_patch_change (_patch_change);
+               }
                return;
        }
 
@@ -6011,18 +6185,33 @@ NoteCreateDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor)
        Drag::start_grab (event, cursor);
 
        _drag_rect = new ArdourCanvas::Rectangle (_region_view->get_canvas_group ());
+       TempoMap& map (_editor->session()->tempo_map());
 
-       framepos_t pf = _drags->current_pointer_frame ();
-       framecnt_t const g = grid_frames (pf);
+       const framepos_t pf = _drags->current_pointer_frame ();
+       const int32_t divisions = _editor->get_grid_music_divisions (event->button.state);
 
-       /* Hack so that we always snap to the note that we are over, instead of snapping
-          to the next one if we're more than halfway through the one we're over.
-       */
-       if (_editor->snap_mode() == SnapNormal && pf > g / 2) {
-               pf -= g / 2;
+       double eqaf = map.exact_qn_at_frame (pf, divisions);
+
+       if (divisions != 0) {
+               bool success = false;
+               Evoral::Beats grid_beats = _editor->get_grid_type_as_beats (success, pf);
+               if (!success) {
+                       grid_beats = Evoral::Beats(1);
+               }
+
+               const double qaf = map.quarter_note_at_frame (pf);
+
+               /* Hack so that we always snap to the note that we are over, instead of snapping
+                  to the next one if we're more than halfway through the one we're over.
+               */
+
+               const double rem = eqaf - qaf;
+               if (rem >= 0.0 && eqaf - grid_beats.to_double() > _region_view->region()->pulse() * 4.0) {
+                       eqaf -= grid_beats.to_double();
+               }
        }
 
-       _note[0] = adjusted_frame (pf, event) - _region_view->region()->position ();
+       _note[0] = map.frame_at_quarter_note (eqaf) - _region_view->region()->position();
        _note[1] = _note[0];
 
        MidiStreamView* sv = _region_view->midi_stream_view ();
@@ -6046,26 +6235,28 @@ NoteCreateDrag::motion (GdkEvent* event, bool)
 }
 
 void
-NoteCreateDrag::finished (GdkEvent*, bool had_movement)
+NoteCreateDrag::finished (GdkEvent* ev, bool had_movement)
 {
        if (!had_movement) {
                return;
        }
 
        framepos_t const start = min (_note[0], _note[1]);
+       framepos_t const start_sess_rel = start + _region_view->region()->position();
        framecnt_t length = (framecnt_t) fabs ((double)(_note[0] - _note[1]));
 
        framecnt_t const g = grid_frames (start);
        Evoral::Beats const one_tick = Evoral::Beats::ticks(1);
 
-       if (_editor->snap_mode() == SnapNormal && length < g) {
+       if (_editor->get_grid_music_divisions (ev->button.state) != 0 && length < g) {
                length = g;
        }
 
-       Evoral::Beats length_beats = max (
-               one_tick, _region_view->region_frames_to_region_beats (length) - one_tick);
+       TempoMap& map (_editor->session()->tempo_map());
+       const double qn_length = map.quarter_note_at_frame (start_sess_rel + length) - map.quarter_note_at_frame (start_sess_rel);
 
-       _region_view->create_note_at (start, _drag_rect->y0(), length_beats, false);
+       Evoral::Beats qn_length_beats = max (one_tick, Evoral::Beats (qn_length));
+       _region_view->create_note_at (start, _drag_rect->y0(), qn_length_beats, ev->button.state, false);
 }
 
 double
@@ -6201,7 +6392,7 @@ RegionCutDrag::motion (GdkEvent*, bool)
 }
 
 void
-RegionCutDrag::finished (GdkEvent*, bool)
+RegionCutDrag::finished (GdkEvent* event, bool)
 {
        _editor->get_track_canvas()->canvas()->re_enter();
 
@@ -6215,7 +6406,7 @@ RegionCutDrag::finished (GdkEvent*, bool)
                return;
        }
 
-       _editor->split_regions_at (pos, rs);
+       _editor->split_regions_at (pos, rs, _editor->get_grid_music_divisions (event->button.state));
 }
 
 void