Merge with 2.0-ongoing R2988
[ardour.git] / gtk2_ardour / editor_mouse.cc
index 1a9e1161c283d3c16367349f5a2c4e4e06ef0644..4234368acdc6c67a992097039fd2152f8d3cd379 100644 (file)
 #include <pbd/error.h>
 #include <gtkmm2ext/utils.h>
 #include <pbd/memento_command.h>
+#include <pbd/basename.h>
 
 #include "ardour_ui.h"
 #include "editor.h"
 #include "time_axis_view.h"
 #include "audio_time_axis.h"
 #include "audio_region_view.h"
+#include "midi_region_view.h"
 #include "marker.h"
 #include "streamview.h"
 #include "region_gain_line.h"
 #include "automation_time_axis.h"
+#include "control_point.h"
 #include "prompter.h"
 #include "utils.h"
 #include "selection.h"
 #include <ardour/route.h>
 #include <ardour/audio_track.h>
 #include <ardour/audio_diskstream.h>
+#include <ardour/midi_diskstream.h>
 #include <ardour/playlist.h>
 #include <ardour/audioplaylist.h>
 #include <ardour/audioregion.h>
+#include <ardour/midi_region.h>
 #include <ardour/dB.h>
 #include <ardour/utils.h>
 #include <ardour/region_factory.h>
+#include <ardour/source_factory.h>
 
 #include <bitset>
 
@@ -68,8 +74,48 @@ using namespace sigc;
 using namespace Gtk;
 using namespace Editing;
 
-nframes_t
-Editor::event_frame (GdkEvent* event, double* pcx, double* pcy)
+const static double ZERO_GAIN_FRACTION = gain_to_slider_position(dB_to_coefficient(0.0));
+
+bool
+Editor::mouse_frame (nframes64_t& where, bool& in_track_canvas) const
+{
+       int x, y;
+       double wx, wy;
+       Gdk::ModifierType mask;
+       Glib::RefPtr<Gdk::Window> canvas_window = const_cast<Editor*>(this)->track_canvas.get_window();
+       Glib::RefPtr<const Gdk::Window> pointer_window;
+       
+       pointer_window = canvas_window->get_pointer (x, y, mask);
+
+       if (pointer_window == track_canvas.get_bin_window()) {
+
+               track_canvas.window_to_world (x, y, wx, wy);
+               in_track_canvas = true;
+
+       } else {
+               in_track_canvas = false;
+
+               if (pointer_window == time_canvas.get_bin_window()) {
+                       time_canvas.window_to_world (x, y, wx, wy);
+               } else {
+                       return false;
+               }
+       }
+
+       wx += horizontal_adjustment.get_value();
+       wy += vertical_adjustment.get_value();
+
+       GdkEvent event;
+       event.type = GDK_BUTTON_RELEASE;
+       event.button.x = wx;
+       event.button.y = wy;
+       
+       where = event_frame (&event, 0, 0);
+       return true;
+}
+
+nframes64_t
+Editor::event_frame (GdkEvent* event, double* pcx, double* pcy) const
 {
        double cx, cy;
 
@@ -157,6 +203,12 @@ Editor::mouse_mode_toggled (MouseMode m)
                        set_mouse_mode (m);
                }
                break;
+       
+       case MouseNote:
+               if (mouse_note_button.get_active()) {
+                       set_mouse_mode (m);
+               }
+               break;
 
        default:
                break;
@@ -204,7 +256,7 @@ Editor::set_mouse_mode (MouseMode m, bool force)
                }
        }
 
-       /* XXX the hack of unsetting all other buttongs should go 
+       /* XXX the hack of unsetting all other buttons should go 
           away once GTK2 allows us to use regular radio buttons drawn like
           normal buttons, rather than my silly GroupedButton hack.
        */
@@ -219,7 +271,11 @@ Editor::set_mouse_mode (MouseMode m, bool force)
 
        case MouseObject:
                mouse_move_button.set_active (true);
-               current_canvas_cursor = grabber_cursor;
+               if (Profile->get_sae()) {
+                       current_canvas_cursor = timebar_cursor;
+               } else {
+                       current_canvas_cursor = grabber_cursor;
+               }
                break;
 
        case MouseGain:
@@ -241,12 +297,22 @@ Editor::set_mouse_mode (MouseMode m, bool force)
                mouse_audition_button.set_active (true);
                current_canvas_cursor = speaker_cursor;
                break;
+       
+       case MouseNote:
+               mouse_note_button.set_active (true);
+               set_midi_edit_cursor (current_midi_edit_mode());
+               break;
        }
 
+       if (mouse_mode == MouseNote)
+               midi_toolbar_frame.show();
+       else
+               midi_toolbar_frame.hide();
+
        ignore_mouse_mode_toggle = false;
 
        if (is_drawable()) {
-               track_canvas.get_window()->set_cursor(*current_canvas_cursor);
+               track_canvas.get_window()->set_cursor(*current_canvas_cursor);
        }
 }
 
@@ -283,14 +349,106 @@ Editor::step_mouse_mode (bool next)
                if (next) set_mouse_mode (MouseObject);
                else set_mouse_mode (MouseTimeFX);
                break;
+       
+       case MouseNote:
+               if (next) set_mouse_mode (MouseObject);
+               else set_mouse_mode (MouseAudition);
+               break;
        }
 }
 
 void
-Editor::button_selection (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_type)
+Editor::midi_edit_mode_toggled (MidiEditMode m)
 {
-       bool commit = false;
+       if (ignore_midi_edit_mode_toggle) {
+               return;
+       }
+
+       switch (m) {
+       case MidiEditPencil:
+               if (midi_tool_pencil_button.get_active())
+                       set_midi_edit_mode (m);
+               break;
+
+       case MidiEditSelect:
+               if (midi_tool_select_button.get_active())
+                       set_midi_edit_mode (m);
+               break;
+
+       case MidiEditErase:
+               if (midi_tool_erase_button.get_active())
+                       set_midi_edit_mode (m);
+               break;
+
+       default:
+               break;
+       }
+
+       set_midi_edit_cursor(m);
+}      
+
+
+void
+Editor::set_midi_edit_mode (MidiEditMode m, bool force)
+{
+       if (drag_info.item) {
+               return;
+       }
+
+       if (!force && m == midi_edit_mode) {
+               return;
+       }
+       
+       midi_edit_mode = m;
+
+       instant_save ();
+       
+       ignore_midi_edit_mode_toggle = true;
+
+       switch (midi_edit_mode) {
+       case MidiEditPencil:
+               midi_tool_pencil_button.set_active (true);
+               break;
+
+       case MidiEditSelect:
+               midi_tool_select_button.set_active (true);
+               break;
+
+       case MidiEditErase:
+               midi_tool_erase_button.set_active (true);
+               break;
+       }
+
+       ignore_midi_edit_mode_toggle = false;
+
+       set_midi_edit_cursor (current_midi_edit_mode());
+
+       if (is_drawable()) {
+               track_canvas.get_window()->set_cursor(*current_canvas_cursor);
+       }
+}
+
+void
+Editor::set_midi_edit_cursor (MidiEditMode m)
+{
+       switch (midi_edit_mode) {
+       case MidiEditPencil:
+               current_canvas_cursor = midi_pencil_cursor;
+               break;
+
+       case MidiEditSelect:
+               current_canvas_cursor = midi_select_cursor;
+               break;
 
+       case MidiEditErase:
+               current_canvas_cursor = midi_erase_cursor;
+               break;
+       }
+}
+
+void
+Editor::button_selection (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_type)
+{
        /* in object/audition/timefx mode, any button press sets
           the selection if the object can be selected. this is a
           bit of hack, because we want to avoid this if the
@@ -329,61 +487,56 @@ Editor::button_selection (ArdourCanvas::Item* item, GdkEvent* event, ItemType it
        switch (item_type) {
        case RegionItem:
                if (mouse_mode != MouseRange) {
-                       commit = set_selected_regionview_from_click (press, op, true);
+                       set_selected_regionview_from_click (press, op, true);
                } else if (event->type == GDK_BUTTON_PRESS) {
-                       commit = set_selected_track_from_click (press, op, false);
+                       set_selected_track_as_side_effect ();
                }
                break;
                
-       case RegionViewNameHighlight:
-       case RegionViewName:
+       case RegionViewNameHighlight:
+       case RegionViewName:
                if (mouse_mode != MouseRange) {
-                       commit = set_selected_regionview_from_click (press, op, true);
+                       set_selected_regionview_from_click (press, op, true);
                } else if (event->type == GDK_BUTTON_PRESS) {
-                       commit = set_selected_track_from_click (press, op, false);
+                       set_selected_track_as_side_effect ();
                }
                break;
 
+
        case FadeInHandleItem:
        case FadeInItem:
        case FadeOutHandleItem:
        case FadeOutItem:
                if (mouse_mode != MouseRange) {
-                       commit = set_selected_regionview_from_click (press, op, true);
+                       set_selected_regionview_from_click (press, op, true);
                } else if (event->type == GDK_BUTTON_PRESS) {
-                       commit = set_selected_track_from_click (press, op, false);
+                       set_selected_track_as_side_effect ();
                }
                break;
-               
-       case GainAutomationControlPointItem:
-       case PanAutomationControlPointItem:
-       case RedirectAutomationControlPointItem:
-               commit = set_selected_track_from_click (press, op, true);
+
+       case ControlPointItem:
+               set_selected_track_as_side_effect ();
                if (mouse_mode != MouseRange) {
-                       commit |= set_selected_control_point_from_click (op, false);
+                       set_selected_control_point_from_click (op, false);
                }
                break;
                
        case StreamItem:
                /* for context click or range selection, select track */
                if (event->button.button == 3) {
-                       commit = set_selected_track_from_click (press, op, true);
+                       set_selected_track_as_side_effect ();
                } else if (event->type == GDK_BUTTON_PRESS && mouse_mode == MouseRange) {
-                       commit = set_selected_track_from_click (press, op, false);
+                       set_selected_track_as_side_effect ();
                }
                break;
                    
        case AutomationTrackItem:
-               commit = set_selected_track_from_click (press, op, true);
+               set_selected_track_as_side_effect (true);
                break;
                
        default:
                break;
        }
-       
-//     if (commit) {
-//             commit_reversible_command ();
-//     }
 }
 
 bool
@@ -422,14 +575,12 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                        */
                        
                        switch (item_type) {
-                       case EditCursorItem:
                        case PlayheadCursorItem:
                                start_cursor_grab (item, event);
                                return true;
 
                        case MarkerItem:
-                               if (Keyboard::modifier_state_equals (event->button.state, 
-                                                                    Keyboard::ModifierMask(Keyboard::Control|Keyboard::Shift))) {
+                               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::ModifierMask(Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
                                        hide_marker (item, event);
                                } else {
                                        start_marker_grab (item, event);
@@ -437,7 +588,7 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                return true;
 
                        case TempoMarkerItem:
-                               if (Keyboard::modifier_state_contains (event->button.state, Keyboard::Control)) {
+                               if (Keyboard::modifier_state_contains (event->button.state, Keyboard::CopyModifier)) {
                                        start_tempo_marker_copy_grab (item, event);
                                } else {
                                        start_tempo_marker_grab (item, event);
@@ -445,7 +596,7 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                return true;
 
                        case MeterMarkerItem:
-                               if (Keyboard::modifier_state_contains (event->button.state, Keyboard::Control)) {
+                               if (Keyboard::modifier_state_contains (event->button.state, Keyboard::CopyModifier)) {
                                        start_meter_marker_copy_grab (item, event);
                                } else {
                                        start_meter_marker_grab (item, event);
@@ -463,6 +614,11 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                return true;
                                break;
 
+                       case CdMarkerBarItem:
+                               start_range_markerbar_op (item, event, CreateCDMarker); 
+                               return true;
+                               break;
+
                        case TransportMarkerBarItem:
                                start_range_markerbar_op (item, event, CreateTransportMarker); 
                                return true;
@@ -486,14 +642,13 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
 
                        case SelectionItem:
                                if (Keyboard::modifier_state_contains 
-                                   (event->button.state, Keyboard::ModifierMask(Keyboard::Alt))) {
+                                   (event->button.state, Keyboard::ModifierMask(Keyboard::SecondaryModifier))) {
                                        // contains and not equals because I can't use alt as a modifier alone.
                                        start_selection_grab (item, event);
-                               } else if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Control)) {
+                               } else if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
                                        /* grab selection for moving */
                                        start_selection_op (item, event, SelectionMove);
-                               }
-                               else {
+                               } else {
                                        /* this was debated, but decided the more common action was to
                                           make a new selection */
                                        start_selection_op (item, event, CreateSelection);
@@ -507,7 +662,7 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                        break;
                        
                case MouseObject:
-                       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::ModifierMask(Keyboard::Control|Keyboard::Alt)) &&
+                       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::ModifierMask(Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) &&
                            event->type == GDK_BUTTON_PRESS) {
                                
                                start_rubberband_select (item, event);
@@ -524,7 +679,7 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                        return true;
 
                                case RegionItem:
-                                       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::Control)) {
+                                       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::CopyModifier)) {
                                                start_region_copy_grab (item, event);
                                        } else if (Keyboard::the_keyboard().key_is_down (GDK_b)) {
                                                start_region_brush_grab (item, event);
@@ -544,16 +699,12 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                                return true;
                                        break;
 
-                               case GainAutomationControlPointItem:
-                               case PanAutomationControlPointItem:
-                               case RedirectAutomationControlPointItem:
+                               case ControlPointItem:
                                        start_control_point_grab (item, event);
                                        return true;
                                        break;
                                        
-                               case GainAutomationLineItem:
-                               case PanAutomationLineItem:
-                               case RedirectAutomationLineItem:
+                               case AutomationLineItem:
                                        start_line_grab_from_line (item, event);
                                        return true;
                                        break;
@@ -563,7 +714,7 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                        start_rubberband_select (item, event);
                                        break;
                                        
-                               /* <CMT Additions> */
+#ifdef WITH_CMT
                                case ImageFrameHandleStartItem:
                                        imageframe_start_handle_op(item, event) ;
                                        return(true) ;
@@ -580,16 +731,13 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                        markerview_item_end_handle_op(item, event) ;
                                        return(true) ;
                                        break ;
-                               /* </CMT Additions> */
-                               
-                               /* <CMT Additions> */
                                case MarkerViewItem:
                                        start_markerview_grab(item, event) ;
                                        break ;
                                case ImageFrameItem:
                                        start_imageframe_grab(item, event) ;
                                        break ;
-                               /* </CMT Additions> */
+#endif
 
                                case MarkerBarItem:
                                        
@@ -608,17 +756,11 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                // start_line_grab_from_regionview (item, event);
                                break;
 
-                       case GainControlPointItem:
-                               start_control_point_grab (item, event);
-                               return true;
-                               
                        case GainLineItem:
                                start_line_grab_from_line (item, event);
                                return true;
 
-                       case GainAutomationControlPointItem:
-                       case PanAutomationControlPointItem:
-                       case RedirectAutomationControlPointItem:
+                       case ControlPointItem:
                                start_control_point_grab (item, event);
                                return true;
                                break;
@@ -630,15 +772,11 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                        break;
 
                        switch (item_type) {
-                       case GainAutomationControlPointItem:
-                       case PanAutomationControlPointItem:
-                       case RedirectAutomationControlPointItem:
+                       case ControlPointItem:
                                start_control_point_grab (item, event);
                                break;
 
-                       case GainAutomationLineItem:
-                       case PanAutomationLineItem:
-                       case RedirectAutomationLineItem:
+                       case AutomationLineItem:
                                start_line_grab_from_line (item, event);
                                break;
 
@@ -669,9 +807,19 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                        break;
 
                case MouseAudition:
-                       /* handled in release */
+                       _scrubbing = true;
+                       scrub_reversals = 0;
+                       scrub_reverse_distance = 0;
+                       last_scrub_x = event->button.x;
+                       scrubbing_direction = 0;
+                       track_canvas.get_window()->set_cursor (*transparent_cursor);
+                       /* rest handled in motion & release */
                        break;
 
+               case MouseNote:
+                       start_create_region_grab (item, event);
+                       break;
+               
                default:
                        break;
                }
@@ -683,16 +831,14 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                        if (event->type == GDK_BUTTON_PRESS) {
                                switch (item_type) {
                                case RegionItem:
-                                       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::Control)) {
+                                       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::CopyModifier)) {
                                                start_region_copy_grab (item, event);
                                        } else {
                                                start_region_grab (item, event);
                                        }
-                                       
+                                       return true;
                                        break;
-                               case GainAutomationControlPointItem:
-                               case PanAutomationControlPointItem:
-                               case RedirectAutomationControlPointItem:
+                               case ControlPointItem:
                                        start_control_point_grab (item, event);
                                        return true;
                                        break;
@@ -729,7 +875,7 @@ Editor::button_press_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemTyp
                                        
                                
                case MouseZoom:
-                       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Control)) {
+                       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
                                temporal_zoom_session();
                        } else {
                                temporal_zoom_to_frame (true, event_frame(event));
@@ -821,7 +967,7 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                        case FadeOutHandleItem:
                                popup_fade_context_menu (1, event->button.time, item, item_type);
                                break;
-
+                       
                        case StreamItem:
                                popup_track_context_menu (1, event->button.time, item_type, false, where);
                                break;
@@ -842,7 +988,8 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
 
                        case MarkerBarItem: 
                        case RangeMarkerBarItem: 
-                       case TransportMarkerBarItem: 
+                       case TransportMarkerBarItem:
+                       case CdMarkerBarItem:
                        case TempoBarItem:
                        case MeterBarItem:
                                popup_ruler_menu (pixel_to_frame(event->button.x), item_type);
@@ -859,12 +1006,12 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                        case MeterMarkerItem:
                                tm_marker_context_menu (&event->button, item);
                                break;
-
+                       
                        case CrossfadeViewItem:
                                popup_track_context_menu (1, event->button.time, item_type, false, where);
                                break;
 
-                       /* <CMT Additions> */
+#ifdef WITH_CMT
                        case ImageFrameItem:
                                popup_imageframe_edit_menu(1, event->button.time, item, true) ;
                                break ;
@@ -877,8 +1024,7 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                        case MarkerTimeAxisItem:
                                popup_marker_time_axis_edit_menu(1, event->button.time, item, false) ;
                                break ;
-                       /* <CMT Additions> */
-
+#endif
                                
                        default:
                                break;
@@ -911,18 +1057,14 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                        }
                        break;
                        
-               case GainControlPointItem:
+               case ControlPointItem:
                        if (mouse_mode == MouseGain) {
                                remove_gain_control_point (item, event);
+                       } else {
+                               remove_control_point (item, event);
                        }
                        break;
 
-               case GainAutomationControlPointItem:
-               case PanAutomationControlPointItem:
-               case RedirectAutomationControlPointItem:
-                       remove_control_point (item, event);
-                       break;
-
                default:
                        break;
                }
@@ -934,13 +1076,10 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
 
                switch (item_type) {
                /* see comments in button_press_handler */
-               case EditCursorItem:
                case PlayheadCursorItem:
                case MarkerItem:
                case GainLineItem:
-               case GainAutomationLineItem:
-               case PanAutomationLineItem:
-               case RedirectAutomationLineItem:
+               case AutomationLineItem:
                case StartSelectionTrimItem:
                case EndSelectionTrimItem:
                        return true;
@@ -952,6 +1091,14 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                        mouse_add_new_marker (where);
                        return true;
 
+               case CdMarkerBarItem:
+                       // if we get here then a dragged range wasn't done
+                       if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier())) {
+                               snap_to (where, 0, true);
+                       }
+                       mouse_add_new_marker (where, true);
+                       return true;
+
                case TempoBarItem:
                        if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier())) {
                                snap_to (where);
@@ -972,7 +1119,7 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                case MouseObject:
                        switch (item_type) {
                        case AutomationTrackItem:
-                               dynamic_cast<AutomationTimeAxisView*>(clicked_trackview)->add_automation_event 
+                               dynamic_cast<AutomationTimeAxisView*>(clicked_axisview)->add_automation_event 
                                        (item,
                                         event,
                                         where,
@@ -999,7 +1146,7 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                                break;
                                
                        case AutomationTrackItem:
-                               dynamic_cast<AutomationTimeAxisView*>(clicked_trackview)->
+                               dynamic_cast<AutomationTimeAxisView*>(clicked_axisview)->
                                        add_automation_event (item, event, where, event->button.y);
                                return true;
                                break;
@@ -1009,15 +1156,23 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                        break;
                        
                case MouseAudition:
-                       switch (item_type) {
-                       case RegionItem:
-                               audition_selected_region ();
-                               break;
-                       default:
-                               break;
-                       }
+                       _scrubbing = false;
+                       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
+                       if (scrubbing_direction == 0) {
+                               /* no drag, just a click */
+                               switch (item_type) {
+                               case RegionItem:
+                                       play_selected_region ();
+                                       break;
+                               default:
+                                       break;
+                               }
+                       } else {
+                               /* make sure we stop */
+                               session->request_transport_speed (0.0);
+                       }
                        break;
-
+                       
                default:
                        break;
 
@@ -1033,9 +1188,9 @@ Editor::button_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemT
                case MouseObject:
                        switch (item_type) {
                        case RegionItem:
-                               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Shift)) {
+                               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
                                        raise_region ();
-                               } else if (Keyboard::modifier_state_equals (event->button.state, Keyboard::ModifierMask (Keyboard::Shift|Keyboard::Alt))) {
+                               } else if (Keyboard::modifier_state_equals (event->button.state, Keyboard::ModifierMask (Keyboard::TertiaryModifier|Keyboard::SecondaryModifier))) {
                                        lower_region ();
                                } else {
                                        // Button2 click is unused
@@ -1078,73 +1233,46 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
        double fraction;
        
        switch (item_type) {
-       case GainControlPointItem:
-               if (mouse_mode == MouseGain) {
+       case ControlPointItem:
+               if (mouse_mode == MouseGain || mouse_mode == MouseObject) {
                        cp = static_cast<ControlPoint*>(item->get_data ("control_point"));
                        cp->set_visible (true);
 
                        double at_x, at_y;
                        at_x = cp->get_x();
                        at_y = cp->get_y ();
-                       cp->item->i2w (at_x, at_y);
+                       cp->item()->i2w (at_x, at_y);
                        at_x += 20.0;
                        at_y += 20.0;
 
-                       fraction = 1.0 - (cp->get_y() / cp->line.height());
+                       fraction = 1.0 - (cp->get_y() / cp->line().height());
 
-                       set_verbose_canvas_cursor (cp->line.get_verbose_cursor_string (fraction), at_x, at_y);
+                       set_verbose_canvas_cursor (cp->line().get_verbose_cursor_string (fraction), at_x, at_y);
                        show_verbose_canvas_cursor ();
 
-                       if (is_drawable()) {
+                       if (is_drawable() && !_scrubbing) {
                                track_canvas.get_window()->set_cursor (*fader_cursor);
                        }
                }
                break;
 
-       case GainAutomationControlPointItem:
-       case PanAutomationControlPointItem:
-       case RedirectAutomationControlPointItem:
-               if (mouse_mode == MouseGain || mouse_mode == MouseObject) {
-                       cp = static_cast<ControlPoint*>(item->get_data ("control_point"));
-                       cp->set_visible (true);
-
-                       double at_x, at_y;
-                       at_x = cp->get_x();
-                       at_y = cp->get_y ();
-                       cp->item->i2w (at_x, at_y);
-                       at_x += 20.0;
-                       at_y += 20.0;
-
-                       fraction = 1.0 - (cp->get_y() / cp->line.height());
-
-                       set_verbose_canvas_cursor (cp->line.get_verbose_cursor_string (fraction), at_x, at_y);
-                       show_verbose_canvas_cursor ();
-
-                       if (is_drawable()) {
-                               track_canvas.get_window()->set_cursor (*fader_cursor);
-                       }
-               }
-               break;
-               
        case GainLineItem:
                if (mouse_mode == MouseGain) {
                        ArdourCanvas::Line *line = dynamic_cast<ArdourCanvas::Line *> (item);
                        if (line)
-                               line->property_fill_color_rgba() = color_map[cEnteredGainLine];
+                               line->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_EnteredGainLine.get();
                        if (is_drawable()) {
                                track_canvas.get_window()->set_cursor (*fader_cursor);
                        }
                }
                break;
                        
-       case GainAutomationLineItem:
-       case RedirectAutomationLineItem:
-       case PanAutomationLineItem:
+       case AutomationLineItem:
                if (mouse_mode == MouseGain || mouse_mode == MouseObject) {
                        {
                                ArdourCanvas::Line *line = dynamic_cast<ArdourCanvas::Line *> (item);
                                if (line)
-                                       line->property_fill_color_rgba() = color_map[cEnteredAutomationLine];
+                                       line->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_EnteredAutomationLine.get();
                        }
                        if (is_drawable()) {
                                track_canvas.get_window()->set_cursor (*fader_cursor);
@@ -1160,19 +1288,19 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
 
        case StartSelectionTrimItem:
        case EndSelectionTrimItem:
-       /* <CMT Additions> */
+
+#ifdef WITH_CMT
        case ImageFrameHandleStartItem:
        case ImageFrameHandleEndItem:
        case MarkerViewHandleStartItem:
        case MarkerViewHandleEndItem:
-       /* </CMT Additions> */
+#endif
 
                if (is_drawable()) {
                        track_canvas.get_window()->set_cursor (*trimmer_cursor);
                }
                break;
 
-       case EditCursorItem:
        case PlayheadCursorItem:
                if (is_drawable()) {
                        track_canvas.get_window()->set_cursor (*grabber_cursor);
@@ -1193,7 +1321,7 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
 
        case AutomationTrackItem:
                if (is_drawable()) {
-                       Gdk::Cursor *cursor;
+                       Gdk::Cursor *cursor;
                        switch (mouse_mode) {
                        case MouseRange:
                                cursor = selector_cursor;
@@ -1219,6 +1347,7 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
        case MarkerBarItem:
        case RangeMarkerBarItem:
        case TransportMarkerBarItem:
+       case CdMarkerBarItem:
        case MeterBarItem:
        case TempoBarItem:
                if (is_drawable()) {
@@ -1230,7 +1359,8 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
                if ((marker = static_cast<Marker *> (item->get_data ("marker"))) == 0) {
                        break;
                }
-               marker->set_color_rgba (color_map[cEnteredMarker]);
+               entered_marker = marker;
+               marker->set_color_rgba (ARDOUR_UI::config()->canvasvar_EnteredMarker.get());
                // fall through
        case MeterMarkerItem:
        case TempoMarkerItem:
@@ -1258,13 +1388,8 @@ Editor::enter_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
 
        switch (item_type) {
        case GainLineItem:
-       case GainAutomationLineItem:
-       case RedirectAutomationLineItem:
-       case PanAutomationLineItem:
-       case GainControlPointItem:
-       case GainAutomationControlPointItem:
-       case PanAutomationControlPointItem:
-       case RedirectAutomationControlPointItem:
+       case AutomationLineItem:
+       case ControlPointItem:
                /* these do not affect the current entered track state */
                clear_entered_track = false;
                break;
@@ -1292,13 +1417,10 @@ Editor::leave_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
        bool is_start;
 
        switch (item_type) {
-       case GainControlPointItem:
-       case GainAutomationControlPointItem:
-       case PanAutomationControlPointItem:
-       case RedirectAutomationControlPointItem:
+       case ControlPointItem:
                cp = reinterpret_cast<ControlPoint*>(item->get_data ("control_point"));
-               if (cp->line.npoints() > 1) {
-                       if (!cp->selected) {
+               if (cp->line().the_list()->interpolation() != AutomationList::Discrete) {
+                       if (cp->line().npoints() > 1 && !cp->selected()) {
                                cp->set_visible (false);
                        }
                }
@@ -1313,23 +1435,22 @@ Editor::leave_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
        case RegionViewNameHighlight:
        case StartSelectionTrimItem:
        case EndSelectionTrimItem:
-       case EditCursorItem:
        case PlayheadCursorItem:
-       /* <CMT Additions> */
+
+#ifdef WITH_CMT
        case ImageFrameHandleStartItem:
        case ImageFrameHandleEndItem:
        case MarkerViewHandleStartItem:
        case MarkerViewHandleEndItem:
-       /* </CMT Additions> */
+#endif
+
                if (is_drawable()) {
                        track_canvas.get_window()->set_cursor (*current_canvas_cursor);
                }
                break;
 
        case GainLineItem:
-       case GainAutomationLineItem:
-       case RedirectAutomationLineItem:
-       case PanAutomationLineItem:
+       case AutomationLineItem:
                al = reinterpret_cast<AutomationLine*> (item->get_data ("line"));
                {
                        ArdourCanvas::Line *line = dynamic_cast<ArdourCanvas::Line *> (item);
@@ -1352,6 +1473,7 @@ Editor::leave_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
 
        case RangeMarkerBarItem:
        case TransportMarkerBarItem:
+       case CdMarkerBarItem:
        case MeterBarItem:
        case TempoBarItem:
        case MarkerBarItem:
@@ -1364,8 +1486,10 @@ Editor::leave_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_
                if ((marker = static_cast<Marker *> (item->get_data ("marker"))) == 0) {
                        break;
                }
-               loc = find_location_from_marker (marker, is_start);
-               if (loc) location_flags_changed (loc, this);
+               entered_marker = 0;
+               if ((loc = find_location_from_marker (marker, is_start)) != 0) {
+                       location_flags_changed (loc, this);
+               }
                // fall through
        case MeterMarkerItem:
        case TempoMarkerItem:
@@ -1416,19 +1540,21 @@ Editor::left_automation_track ()
 bool
 Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item_type, bool from_autoscroll)
 {
-       gint x, y;
+       if (event->motion.is_hint) {
+               gint x, y;
+               
+               /* We call this so that MOTION_NOTIFY events continue to be
+                  delivered to the canvas. We need to do this because we set
+                  Gdk::POINTER_MOTION_HINT_MASK on the canvas. This reduces
+                  the density of the events, at the expense of a round-trip
+                  to the server. Given that this will mostly occur on cases
+                  where DISPLAY = :0.0, and given the cost of what the motion
+                  event might do, its a good tradeoff.  
+               */
+               
+               track_canvas.get_pointer (x, y);
+       }
        
-       /* We call this so that MOTION_NOTIFY events continue to be
-          delivered to the canvas. We need to do this because we set
-          Gdk::POINTER_MOTION_HINT_MASK on the canvas. This reduces
-          the density of the events, at the expense of a round-trip
-          to the server. Given that this will mostly occur on cases
-          where DISPLAY = :0.0, and given the cost of what the motion
-          event might do, its a good tradeoff.  
-       */
-
-       track_canvas.get_pointer (x, y);
-
        if (current_stepping_trackview) {
                /* don't keep the persistent stepped trackview if the mouse moves */
                current_stepping_trackview = 0;
@@ -1441,17 +1567,104 @@ Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item
        }
 
        drag_info.item_type = item_type;
+       drag_info.last_pointer_x = drag_info.current_pointer_x;
+       drag_info.last_pointer_y = drag_info.current_pointer_y;
        drag_info.current_pointer_frame = event_frame (event, &drag_info.current_pointer_x,
                                                       &drag_info.current_pointer_y);
 
+       switch (mouse_mode) {
+       case MouseAudition:
+               if (_scrubbing) {
+
+                       double delta;
+
+                       if (scrubbing_direction == 0) {
+                               /* first move */
+                               session->request_locate (drag_info.current_pointer_frame, false);
+                               session->request_transport_speed (0.1);
+                               scrubbing_direction = 1;
+
+                       } else {
+                               
+                               if (last_scrub_x > drag_info.current_pointer_x) {
+
+                                       /* pointer moved to the left */
+                                       
+                                       if (scrubbing_direction > 0) {
+
+                                               /* we reversed direction to go backwards */
+
+                                               scrub_reversals++;
+                                               scrub_reverse_distance += (int) (last_scrub_x - drag_info.current_pointer_x);
+
+                                       } else {
+
+                                               /* still moving to the left (backwards) */
+                                               
+                                               scrub_reversals = 0;
+                                               scrub_reverse_distance = 0;
+
+                                               delta = 0.01 * (last_scrub_x - drag_info.current_pointer_x);
+                                               session->request_transport_speed (session->transport_speed() - delta);
+                                       }
+                                       
+                               } else {
+                                       /* pointer moved to the right */
+
+                                       if (scrubbing_direction < 0) {
+                                               /* we reversed direction to go forward */
+
+                                               scrub_reversals++;
+                                               scrub_reverse_distance += (int) (drag_info.current_pointer_x - last_scrub_x);
+
+                                       } else {
+                                               /* still moving to the right */
+
+                                               scrub_reversals = 0;
+                                               scrub_reverse_distance = 0;
+                                               
+                                               delta = 0.01 * (drag_info.current_pointer_x - last_scrub_x);
+                                               session->request_transport_speed (session->transport_speed() + delta);
+                                       }
+                               }
+
+                               /* if there have been more than 2 opposite motion moves detected, or one that moves
+                                  back more than 10 pixels, reverse direction
+                               */
+
+                               if (scrub_reversals >= 2 || scrub_reverse_distance > 10) {
+
+                                       if (scrubbing_direction > 0) {
+                                               /* was forwards, go backwards */
+                                               session->request_transport_speed (-0.1);
+                                               scrubbing_direction = -1;
+                                       } else {
+                                               /* was backwards, go forwards */
+                                               session->request_transport_speed (0.1);
+                                               scrubbing_direction = 1;
+                                       }
+                                       
+                                       scrub_reverse_distance = 0;
+                                       scrub_reversals = 0;
+                               }
+                       }
+
+                       last_scrub_x = drag_info.current_pointer_x;
+               }
+
+       default:
+               break;
+       }
+
+
        if (!from_autoscroll && drag_info.item) {
                /* item != 0 is the best test i can think of for dragging.
                */
                if (!drag_info.move_threshold_passed) {
 
-                       bool x_threshold_passed =  (abs ((int) (drag_info.current_pointer_x - drag_info.grab_x)) > 4);
-                       bool y_threshold_passed =  (abs ((int) (drag_info.current_pointer_y - drag_info.grab_y)) > 4);
-
+                       bool x_threshold_passed =  (::llabs ((nframes64_t) (drag_info.current_pointer_x - drag_info.grab_x)) > 4LL);
+                       bool y_threshold_passed =  (::llabs ((nframes64_t) (drag_info.current_pointer_y - drag_info.grab_y)) > 4LL);
+                       
                        drag_info.move_threshold_passed = (x_threshold_passed || y_threshold_passed);
                        
                        // and change the initial grab loc/frame if this drag info wants us to
@@ -1468,12 +1681,8 @@ Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item
 
        switch (item_type) {
        case PlayheadCursorItem:
-       case EditCursorItem:
        case MarkerItem:
-       case GainControlPointItem:
-       case RedirectAutomationControlPointItem:
-       case GainAutomationControlPointItem:
-       case PanAutomationControlPointItem:
+       case ControlPointItem:
        case TempoMarkerItem:
        case MeterMarkerItem:
        case RegionViewNameHighlight:
@@ -1481,17 +1690,17 @@ Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item
        case EndSelectionTrimItem:
        case SelectionItem:
        case GainLineItem:
-       case RedirectAutomationLineItem:
-       case GainAutomationLineItem:
-       case PanAutomationLineItem:
+       case AutomationLineItem:
        case FadeInHandleItem:
        case FadeOutHandleItem:
-       /* <CMT Additions> */
+
+#ifdef WITH_CMT
        case ImageFrameHandleStartItem:
        case ImageFrameHandleEndItem:
        case MarkerViewHandleStartItem:
        case MarkerViewHandleEndItem:
-       /* </CMT Additions> */
+#endif
+
          if (drag_info.item && (event->motion.state & Gdk::BUTTON1_MASK ||
                                 (event->motion.state & Gdk::BUTTON2_MASK))) {
                  if (!from_autoscroll) {
@@ -1511,6 +1720,7 @@ Editor::motion_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType item
        case MouseRange:
        case MouseZoom:
        case MouseTimeFX:
+       case MouseNote:
                if (drag_info.item && (event->motion.state & GDK_BUTTON1_MASK ||
                                       (event->motion.state & GDK_BUTTON2_MASK))) {
                        if (!from_autoscroll) {
@@ -1548,10 +1758,10 @@ Editor::start_grab (GdkEvent* event, Gdk::Cursor *cursor)
                cursor = grabber_cursor;
        }
 
-        // if dragging with button2, the motion is x constrained, with Alt-button2 it is y constrained
+       // if dragging with button2, the motion is x constrained, with Alt-button2 it is y constrained
 
        if (event->button.button == 2) {
-               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Alt)) {
+               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::SecondaryModifier)) {
                        drag_info.y_constrained = true;
                        drag_info.x_constrained = false;
                } else {
@@ -1563,11 +1773,13 @@ Editor::start_grab (GdkEvent* event, Gdk::Cursor *cursor)
                drag_info.y_constrained = false;
        }
 
-       drag_info.grab_frame = event_frame(event, &drag_info.grab_x, &drag_info.grab_y);
+       drag_info.grab_frame = event_frame (event, &drag_info.grab_x, &drag_info.grab_y);
        drag_info.last_pointer_frame = drag_info.grab_frame;
        drag_info.current_pointer_frame = drag_info.grab_frame;
        drag_info.current_pointer_x = drag_info.grab_x;
        drag_info.current_pointer_y = drag_info.grab_y;
+       drag_info.last_pointer_x = drag_info.current_pointer_x;
+       drag_info.last_pointer_y = drag_info.current_pointer_y;
        drag_info.cumulative_x_drag = 0;
        drag_info.cumulative_y_drag = 0;
        drag_info.first_move = true;
@@ -1620,13 +1832,14 @@ Editor::end_grab (ArdourCanvas::Item* item, GdkEvent* event)
        stop_canvas_autoscroll ();
 
        if (drag_info.item == 0) {
-               cerr << "end grab with no item\n";
                return false;
        }
        
        drag_info.item->ungrab (event->button.time);
 
        if (drag_info.finished_callback) {
+               drag_info.last_pointer_x = drag_info.current_pointer_x;
+               drag_info.last_pointer_y = drag_info.current_pointer_y;
                (this->*(drag_info.finished_callback)) (item, event);
        }
 
@@ -1653,35 +1866,6 @@ Editor::end_grab (ArdourCanvas::Item* item, GdkEvent* event)
        return did_drag;
 }
 
-void
-Editor::set_edit_cursor (GdkEvent* event)
-{
-       nframes_t pointer_frame = event_frame (event);
-
-       if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier())) {
-               if (snap_type != SnapToEditCursor) {
-                       snap_to (pointer_frame);
-               }
-       }
-
-       edit_cursor->set_position (pointer_frame);
-       edit_cursor_clock.set (pointer_frame);
-}
-
-void
-Editor::set_playhead_cursor (GdkEvent* event)
-{
-       nframes_t pointer_frame = event_frame (event);
-
-       if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier())) {
-               snap_to (pointer_frame);
-       }
-
-       if (session) {
-               session->request_locate (pointer_frame, session->transport_rolling());
-       }
-}
-
 void
 Editor::start_fade_in_grab (ArdourCanvas::Item* item, GdkEvent* event)
 {
@@ -1698,7 +1882,7 @@ Editor::start_fade_in_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        AudioRegionView* arv = static_cast<AudioRegionView*>(drag_info.data);
 
-       drag_info.pointer_frame_offset = drag_info.grab_frame - ((nframes_t) arv->audio_region()->fade_in().back()->when + arv->region()->position());  
+       drag_info.pointer_frame_offset = drag_info.grab_frame - ((nframes_t) arv->audio_region()->fade_in()->back()->when + arv->region()->position()); 
 }
 
 void
@@ -1708,7 +1892,7 @@ Editor::fade_in_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        nframes_t pos;
        nframes_t fade_length;
 
-       if ((int32_t)drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+       if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pos = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
        }
        else {
@@ -1753,7 +1937,7 @@ Editor::fade_in_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* even
 
        if (drag_info.first_move) return;
 
-       if ((int32_t)drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+       if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pos = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
        } else {
                pos = 0;
@@ -1777,13 +1961,13 @@ Editor::fade_in_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* even
                        continue;
                }
        
-               AutomationList& alist = tmp->audio_region()->fade_in();
-               XMLNode &before = alist.get_state();
+               boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_in();
+               XMLNode &before = alist->get_state();
 
                tmp->audio_region()->set_fade_in_length (fade_length);
                
-               XMLNode &after = alist.get_state();
-               session->add_command(new MementoCommand<AutomationList>(alist, &before, &after));
+               XMLNode &after = alist->get_state();
+               session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
        }
 
        commit_reversible_command ();
@@ -1805,7 +1989,7 @@ Editor::start_fade_out_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        AudioRegionView* arv = static_cast<AudioRegionView*>(drag_info.data);
 
-       drag_info.pointer_frame_offset = drag_info.grab_frame - (arv->region()->length() - (nframes_t) arv->audio_region()->fade_out().back()->when + arv->region()->position());       
+       drag_info.pointer_frame_offset = drag_info.grab_frame - (arv->region()->length() - (nframes_t) arv->audio_region()->fade_out()->back()->when + arv->region()->position());      
 }
 
 void
@@ -1815,10 +1999,9 @@ Editor::fade_out_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event
        nframes_t pos;
        nframes_t fade_length;
 
-       if ((long)drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+       if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pos = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
-       }
-       else {
+       } else {
                pos = 0;
        }
 
@@ -1863,7 +2046,7 @@ Editor::fade_out_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* eve
        nframes_t pos;
        nframes_t fade_length;
 
-       if ((long)drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+       if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pos = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
        }
        else {
@@ -1894,13 +2077,13 @@ Editor::fade_out_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* eve
                        continue;
                }
        
-               AutomationList& alist = tmp->audio_region()->fade_out();
-               XMLNode &before = alist.get_state();
+               boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_out();
+               XMLNode &before = alist->get_state();
                
                tmp->audio_region()->set_fade_out_length (fade_length);
 
-               XMLNode &after = alist.get_state();
-               session->add_command(new MementoCommand<AutomationList>(alist, &before, &after));
+               XMLNode &after = alist->get_state();
+               session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
        }
 
        commit_reversible_command ();
@@ -1945,7 +2128,7 @@ Editor::cursor_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        Cursor* cursor = (Cursor *) drag_info.data;
        nframes_t adjusted_frame;
        
-       if ((long)drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+       if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                adjusted_frame = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
        }
        else {
@@ -1953,7 +2136,7 @@ Editor::cursor_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        }
        
        if (!Keyboard::modifier_state_contains (event->button.state, Keyboard::snap_modifier())) {
-               if (cursor != edit_cursor || snap_type != SnapToEditCursor) {
+               if (cursor == playhead_cursor) {
                        snap_to (adjusted_frame);
                }
        }
@@ -1962,11 +2145,7 @@ Editor::cursor_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
        cursor->set_position (adjusted_frame);
        
-       if (cursor == edit_cursor) {
-               edit_cursor_clock.set (cursor->current_frame);
-       } else {
-               UpdateAllTransportClocks (cursor->current_frame);
-       }
+       UpdateAllTransportClocks (cursor->current_frame);
 
        show_verbose_time_cursor (cursor->current_frame, 10);
 
@@ -1987,9 +2166,6 @@ Editor::cursor_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                if (session) {
                        session->request_locate (playhead_cursor->current_frame, drag_info.was_rolling);
                }
-       } else if (item == &edit_cursor->canvas_item) {
-               edit_cursor->set_position (edit_cursor->current_frame);
-               edit_cursor_clock.set (edit_cursor->current_frame);
        } 
 }
 
@@ -2000,7 +2176,7 @@ Editor::update_marker_drag_item (Location *location)
        double x2 = frame_to_pixel (location->end());
 
        if (location->is_mark()) {
-               marker_drag_line_points.front().set_x(x1);
+               marker_drag_line_points.front().set_x(x1);
                marker_drag_line_points.back().set_x(x1);
                marker_drag_line->property_points() = marker_drag_line_points;
        }
@@ -2031,22 +2207,43 @@ Editor::start_marker_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        start_grab (event);
 
+       _dragging_edit_point = true;
+
        drag_info.copied_location = new Location (*location);
        drag_info.pointer_frame_offset = drag_info.grab_frame - (is_start ? location->start() : location->end());       
 
        update_marker_drag_item (location);
 
        if (location->is_mark()) {
-               marker_drag_line->show();
-               marker_drag_line->raise_to_top();
-       }
-       else {
+               // marker_drag_line->show();
+               // marker_drag_line->raise_to_top();
+       } else {
                range_marker_drag_rect->show();
                range_marker_drag_rect->raise_to_top();
        }
-       
-       if (is_start) show_verbose_time_cursor (location->start(), 10);
-       else show_verbose_time_cursor (location->end(), 10);
+
+       if (is_start) {
+               show_verbose_time_cursor (location->start(), 10);
+       } else {
+               show_verbose_time_cursor (location->end(), 10);
+       }
+
+       Selection::Operation op = Keyboard::selection_type (event->button.state);
+
+       switch (op) {
+       case Selection::Toggle:
+               selection->toggle (marker);
+               break;
+       case Selection::Set:
+               selection->set (marker);
+               break;
+       case Selection::Extend:
+               selection->add (marker);
+               break;
+       case Selection::Add:
+               selection->add (marker);
+               break;
+       }
 }
 
 void
@@ -2059,12 +2256,10 @@ Editor::marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        bool is_start;
        bool move_both = false;
 
-
        nframes_t newframe;
-       if (drag_info.pointer_frame_offset <= (long) drag_info.current_pointer_frame) {
+       if (drag_info.pointer_frame_offset <= drag_info.current_pointer_frame) {
                newframe = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
-       }
-       else {
+       } else {
                newframe = 0;
        }
 
@@ -2080,7 +2275,13 @@ Editor::marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
        /* call this to find out if its the start or end */
        
-       real_location = find_location_from_marker (marker, is_start);
+       if ((real_location = find_location_from_marker (marker, is_start)) == 0) {
+               return;
+       }
+
+       if (real_location->locked()) {
+               return;
+       }
 
        /* use the copy that we're "dragging" around */
        
@@ -2088,7 +2289,7 @@ Editor::marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
        f_delta = copy_location->end() - copy_location->start();
        
-       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Control)) {
+       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
                move_both = true;
        }
 
@@ -2135,7 +2336,8 @@ Editor::marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
        LocationMarkers* lm = find_location_markers (real_location);
        lm->set_position (copy_location->start(), copy_location->end());
-       
+       edit_point_clock.set (copy_location->start());
+
        show_verbose_time_cursor (newframe, 10);
 }
 
@@ -2146,17 +2348,23 @@ Editor::marker_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                marker_drag_motion_callback (item, event);
 
        }
+
+       _dragging_edit_point = false;
        
        Marker* marker = (Marker *) drag_info.data;
        bool is_start;
 
-
        begin_reversible_command ( _("move marker") );
        XMLNode &before = session->locations()->get_state();
        
        Location * location = find_location_from_marker (marker, is_start);
-       
+
        if (location) {
+
+               if (location->locked()) {
+                       return;
+               }
+
                if (location->is_mark()) {
                        location->set_start (drag_info.copied_location->start());
                } else {
@@ -2192,6 +2400,7 @@ Editor::start_meter_marker_grab (ArdourCanvas::Item* item, GdkEvent* event)
        }
 
        drag_info.item = item;
+       drag_info.copy = false;
        drag_info.data = marker;
        drag_info.motion_callback = &Editor::meter_marker_drag_motion_callback;
        drag_info.finished_callback = &Editor::meter_marker_drag_finished_callback;
@@ -2220,7 +2429,7 @@ Editor::start_meter_marker_copy_grab (ArdourCanvas::Item* item, GdkEvent* event)
        // The actual copying is not done before we reach the finish callback.
        char name[64];
        snprintf (name, sizeof(name), "%g/%g", meter_marker->meter().beats_per_bar(), meter_marker->meter().note_divisor ());
-       MeterMarker* new_marker = new MeterMarker(*this, *meter_group, color_map[cMeterMarker], name, 
+       MeterMarker* new_marker = new MeterMarker(*this, *meter_group, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), name, 
                                                  *new MeterSection(meter_marker->meter()));
 
        drag_info.item = &new_marker->the_item();
@@ -2242,7 +2451,7 @@ Editor::meter_marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* e
        MeterMarker* marker = (MeterMarker *) drag_info.data;
        nframes_t adjusted_frame;
 
-       if ((long)drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+       if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                adjusted_frame = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
        }
        else {
@@ -2279,12 +2488,12 @@ Editor::meter_marker_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent*
        
        if (drag_info.copy == true) {
                begin_reversible_command (_("copy meter mark"));
-                XMLNode &before = map.get_state();
+               XMLNode &before = map.get_state();
                map.add_meter (marker->meter(), when);
                XMLNode &after = map.get_state();
-                session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
+               session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
                commit_reversible_command ();
-               
+
                // delete the dummy marker we used for visual representation of copying.
                // a new visual marker will show up automatically.
                delete marker;
@@ -2293,7 +2502,7 @@ Editor::meter_marker_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent*
                XMLNode &before = map.get_state();
                map.move_meter (marker->meter(), when);
                XMLNode &after = map.get_state();
-                session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
+               session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
                commit_reversible_command ();
        }
 }
@@ -2321,6 +2530,7 @@ Editor::start_tempo_marker_grab (ArdourCanvas::Item* item, GdkEvent* event)
        }
 
        drag_info.item = item;
+       drag_info.copy = false;
        drag_info.data = marker;
        drag_info.motion_callback = &Editor::tempo_marker_drag_motion_callback;
        drag_info.finished_callback = &Editor::tempo_marker_drag_finished_callback;
@@ -2351,7 +2561,7 @@ Editor::start_tempo_marker_copy_grab (ArdourCanvas::Item* item, GdkEvent* event)
        // The actual copying is not done before we reach the finish callback.
        char name[64];
        snprintf (name, sizeof (name), "%.2f", tempo_marker->tempo().beats_per_minute());
-       TempoMarker* new_marker = new TempoMarker(*this, *tempo_group, color_map[cTempoMarker], name, 
+       TempoMarker* new_marker = new TempoMarker(*this, *tempo_group, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), name, 
                                                  *new TempoSection(tempo_marker->tempo()));
 
        drag_info.item = &new_marker->the_item();
@@ -2373,7 +2583,7 @@ Editor::tempo_marker_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* e
        TempoMarker* marker = (TempoMarker *) drag_info.data;
        nframes_t adjusted_frame;
        
-       if ((long)drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+       if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                adjusted_frame = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
        }
        else {
@@ -2422,10 +2632,10 @@ Editor::tempo_marker_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent*
                delete marker;
        } else {
                begin_reversible_command (_("move tempo mark"));
-                XMLNode &before = map.get_state();
+               XMLNode &before = map.get_state();
                map.move_tempo (marker->tempo(), when);
-                XMLNode &after = map.get_state();
-                session->add_command (new MementoCommand<TempoMap>(map, &before, &after));
+               XMLNode &after = map.get_state();
+               session->add_command (new MementoCommand<TempoMap>(map, &before, &after));
                commit_reversible_command ();
        }
 }
@@ -2441,16 +2651,16 @@ Editor::remove_gain_control_point (ArdourCanvas::Item*item, GdkEvent* event)
        }
 
        // We shouldn't remove the first or last gain point
-       if (control_point->line.is_last_point(*control_point) ||
-               control_point->line.is_first_point(*control_point)) {   
+       if (control_point->line().is_last_point(*control_point) ||
+               control_point->line().is_first_point(*control_point)) { 
                return;
        }
 
-       control_point->line.remove_point (*control_point);
+       control_point->line().remove_point (*control_point);
 }
 
 void
-Editor::remove_control_point (ArdourCanvas::Item*item, GdkEvent* event)
+Editor::remove_control_point (ArdourCanvas::Item* item, GdkEvent* event)
 {
        ControlPoint* control_point;
 
@@ -2459,14 +2669,14 @@ Editor::remove_control_point (ArdourCanvas::Item*item, GdkEvent* event)
                /*NOTREACHED*/
        }
 
-       control_point->line.remove_point (*control_point);
+       control_point->line().remove_point (*control_point);
 }
 
 void
 Editor::start_control_point_grab (ArdourCanvas::Item* item, GdkEvent* event)
 {
        ControlPoint* control_point;
-       
+
        if ((control_point = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"))) == 0) {
                fatal << _("programming error: control point canvas item has no control point object pointer!") << endmsg;
                /*NOTREACHED*/
@@ -2479,10 +2689,19 @@ Editor::start_control_point_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        start_grab (event, fader_cursor);
 
-       control_point->line.start_drag (control_point, drag_info.grab_frame, 0);
+       // start the grab at the center of the control point so
+       // the point doesn't 'jump' to the mouse after the first drag
+       drag_info.grab_x = control_point->get_x();
+       drag_info.grab_y = control_point->get_y();
+       control_point->line().parent_group().i2w(drag_info.grab_x, drag_info.grab_y);
+       track_canvas.w2c(drag_info.grab_x, drag_info.grab_y, drag_info.grab_x, drag_info.grab_y);
 
-       float fraction = 1.0 - (control_point->get_y() / control_point->line.height());
-       set_verbose_canvas_cursor (control_point->line.get_verbose_cursor_string (fraction), 
+       drag_info.grab_frame = pixel_to_frame(drag_info.grab_x);
+
+       control_point->line().start_drag (control_point, drag_info.grab_frame, 0);
+
+       double fraction = 1.0 - ((control_point->get_y() - control_point->line().y_position()) / (double)control_point->line().height());
+       set_verbose_canvas_cursor (control_point->line().get_verbose_cursor_string (fraction), 
                                   drag_info.current_pointer_x + 20, drag_info.current_pointer_y + 20);
 
        show_verbose_canvas_cursor ();
@@ -2493,11 +2712,28 @@ Editor::control_point_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent*
 {
        ControlPoint* cp = reinterpret_cast<ControlPoint *> (drag_info.data);
 
-       double cx = drag_info.current_pointer_x;
-       double cy = drag_info.current_pointer_y;
+       double dx = drag_info.current_pointer_x - drag_info.last_pointer_x;
+       double dy = drag_info.current_pointer_y - drag_info.last_pointer_y;
+
+       if (event->button.state & Keyboard::SecondaryModifier) {
+               dx *= 0.1;
+               dy *= 0.1;
+       }
 
-       drag_info.cumulative_x_drag = cx - drag_info.grab_x ;
-       drag_info.cumulative_y_drag = cy - drag_info.grab_y ;
+       double cx = drag_info.grab_x + drag_info.cumulative_x_drag + dx;
+       double cy = drag_info.grab_y + drag_info.cumulative_y_drag + dy;
+
+       // calculate zero crossing point. back off by .01 to stay on the
+       // positive side of zero
+       double _unused = 0;
+       double zero_gain_y = (1.0 - ZERO_GAIN_FRACTION) * cp->line().height() - .01;
+       cp->line().parent_group().i2w(_unused, zero_gain_y);
+
+       // make sure we hit zero when passing through
+       if ((cy < zero_gain_y and (cy - dy) > zero_gain_y)
+                       or (cy > zero_gain_y and (cy - dy) < zero_gain_y)) {
+               cy = zero_gain_y;
+       }
 
        if (drag_info.x_constrained) {
                cx = drag_info.grab_x;
@@ -2506,11 +2742,14 @@ Editor::control_point_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent*
                cy = drag_info.grab_y;
        }
 
-       cp->line.parent_group().w2i (cx, cy);
+       drag_info.cumulative_x_drag = cx - drag_info.grab_x;
+       drag_info.cumulative_y_drag = cy - drag_info.grab_y;
+
+       cp->line().parent_group().w2i (cx, cy);
 
        cx = max (0.0, cx);
        cy = max (0.0, cy);
-       cy = min ((double) cp->line.height(), cy);
+       cy = min ((double) (cp->line().y_position() + cp->line().height()), cy);
 
        //translate cx to frames
        nframes_t cx_frames = unit_to_frame (cx);
@@ -2519,19 +2758,19 @@ Editor::control_point_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent*
                snap_to (cx_frames);
        }
 
-       float fraction = 1.0 - (cy / cp->line.height());
-       
+       const double fraction = 1.0 - ((cy - cp->line().y_position()) / (double)cp->line().height());
+
        bool push;
 
-       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::Control)) {
+       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::PrimaryModifier)) {
                push = true;
        } else {
                push = false;
        }
 
-       cp->line.point_drag (*cp, cx_frames , fraction, push);
+       cp->line().point_drag (*cp, cx_frames , fraction, push);
        
-       set_verbose_canvas_cursor_text (cp->line.get_verbose_cursor_string (fraction));
+       set_verbose_canvas_cursor_text (cp->line().get_verbose_cursor_string (fraction));
 
        drag_info.first_move = false;
 }
@@ -2545,14 +2784,14 @@ Editor::control_point_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent
 
                /* just a click */
                
-               if ((event->type == GDK_BUTTON_RELEASE) && (event->button.button == 1) && Keyboard::modifier_state_equals (event->button.state, Keyboard::Shift)) {
+               if ((event->type == GDK_BUTTON_RELEASE) && (event->button.button == 1) && Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
                        reset_point_selection ();
                }
 
        } else {
                control_point_drag_motion_callback (item, event);
        }
-       cp->line.end_drag (cp);
+       cp->line().end_drag (cp);
 }
 
 void
@@ -2610,7 +2849,7 @@ Editor::start_line_grab (AutomationLine* line, GdkEvent* event)
 
        start_grab (event, fader_cursor);
 
-       double fraction = 1.0 - (cy / line->height());
+       const double fraction = 1.0 - ((cy - line->y_position()) / (double)line->height());
 
        line->start_drag (0, drag_info.grab_frame, fraction);
        
@@ -2623,17 +2862,40 @@ void
 Editor::line_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        AutomationLine* line = reinterpret_cast<AutomationLine *> (drag_info.data);
+
+       double dy = drag_info.current_pointer_y - drag_info.last_pointer_y;
+
+       if (event->button.state & Keyboard::SecondaryModifier) {
+               dy *= 0.1;
+       }
+
        double cx = drag_info.current_pointer_x;
-       double cy = drag_info.current_pointer_y;
+       double cy = drag_info.grab_y + drag_info.cumulative_y_drag + dy;
+
+       // calculate zero crossing point. back off by .01 to stay on the
+       // positive side of zero
+       double _unused = 0;
+       double zero_gain_y = (1.0 - ZERO_GAIN_FRACTION) * line->height() - .01;
+       line->parent_group().i2w(_unused, zero_gain_y);
+
+       // make sure we hit zero when passing through
+       if ((cy < zero_gain_y and (cy - dy) > zero_gain_y)
+                       or (cy > zero_gain_y and (cy - dy) < zero_gain_y)) {
+               cy = zero_gain_y;
+       }
+
+       drag_info.cumulative_y_drag = cy - drag_info.grab_y;
 
        line->parent_group().w2i (cx, cy);
-       
-       double fraction;
-       fraction = 1.0 - (cy / line->height());
+
+       cy = max (0.0, cy);
+       cy = min ((double) line->height(), cy);
+
+       const double fraction = 1.0 - ((cy - line->y_position()) / (double)line->height());
 
        bool push;
 
-       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::Control)) {
+       if (Keyboard::modifier_state_contains (event->button.state, Keyboard::PrimaryModifier)) {
                push = false;
        } else {
                push = true;
@@ -2662,16 +2924,22 @@ Editor::start_region_grab (ArdourCanvas::Item* item, GdkEvent* event)
        drag_info.copy = false;
        drag_info.item = item;
        drag_info.data = clicked_regionview;
-       drag_info.motion_callback = &Editor::region_drag_motion_callback;
-       drag_info.finished_callback = &Editor::region_drag_finished_callback;
+
+       if (Config->get_edit_mode() == Splice) {
+               drag_info.motion_callback = &Editor::region_drag_splice_motion_callback;
+               drag_info.finished_callback = &Editor::region_drag_splice_finished_callback;
+       } else {
+               drag_info.motion_callback = &Editor::region_drag_motion_callback;
+               drag_info.finished_callback = &Editor::region_drag_finished_callback;
+       }
 
        start_grab (event);
 
        double speed = 1.0;
-       TimeAxisView* tvp = clicked_trackview;
+       TimeAxisView* tvp = clicked_axisview;
        RouteTimeAxisView* tv = dynamic_cast<RouteTimeAxisView*>(tvp);
 
-       if (tv && tv->is_audio_track()) {
+       if (tv && tv->is_track()) {
                speed = tv->get_diskstream()->speed();
        }
        
@@ -2686,6 +2954,19 @@ Editor::start_region_grab (ArdourCanvas::Item* item, GdkEvent* event)
        begin_reversible_command (_("move region(s)"));
 }
 
+void
+Editor::start_create_region_grab (ArdourCanvas::Item* item, GdkEvent* event)
+{
+       drag_info.copy = false;
+       drag_info.item = item;
+       drag_info.data = clicked_axisview;
+       drag_info.last_trackview = clicked_axisview;
+       drag_info.motion_callback = &Editor::create_region_drag_motion_callback;
+       drag_info.finished_callback = &Editor::create_region_drag_finished_callback;
+
+       start_grab (event);
+}
+
 void
 Editor::start_region_copy_grab (ArdourCanvas::Item* item, GdkEvent* event)
 {
@@ -2700,11 +2981,11 @@ Editor::start_region_copy_grab (ArdourCanvas::Item* item, GdkEvent* event)
        start_grab(event);
 
        TimeAxisView* tv = &clicked_regionview->get_time_axis_view();
-       RouteTimeAxisView* atv = dynamic_cast<RouteTimeAxisView*>(tv);
+       RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(tv);
        double speed = 1.0;
 
-       if (atv && atv->is_audio_track()) {
-               speed = atv->get_diskstream()->speed();
+       if (rtv && rtv->is_track()) {
+               speed = rtv->get_diskstream()->speed();
        }
        
        drag_info.last_trackview = &clicked_regionview->get_time_axis_view();
@@ -2720,7 +3001,7 @@ Editor::start_region_copy_grab (ArdourCanvas::Item* item, GdkEvent* event)
 void
 Editor::start_region_brush_grab (ArdourCanvas::Item* item, GdkEvent* event)
 {
-       if (selection->regions.empty() || clicked_regionview == 0) {
+       if (selection->regions.empty() || clicked_regionview == 0 || Config->get_edit_mode() == Splice) {
                return;
        }
 
@@ -2733,10 +3014,10 @@ Editor::start_region_brush_grab (ArdourCanvas::Item* item, GdkEvent* event)
        start_grab (event);
 
        double speed = 1.0;
-       TimeAxisView* tvp = clicked_trackview;
+       TimeAxisView* tvp = clicked_axisview;
        RouteTimeAxisView* tv = dynamic_cast<RouteTimeAxisView*>(tvp);
 
-       if (tv && tv->is_audio_track()) {
+       if (tv && tv->is_track()) {
                speed = tv->get_diskstream()->speed();
        }
        
@@ -2751,40 +3032,34 @@ Editor::start_region_brush_grab (ArdourCanvas::Item* item, GdkEvent* event)
 }
 
 void
-Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
+Editor::possibly_copy_regions_during_grab (GdkEvent* event)
 {
-       double x_delta;
-       double y_delta = 0;
-       RegionView* rv = reinterpret_cast<RegionView*> (drag_info.data); 
-       nframes_t pending_region_position = 0;
-       int32_t pointer_y_span = 0, canvas_pointer_y_span = 0, original_pointer_order;
-       int32_t visible_y_high = 0, visible_y_low = 512;  //high meaning higher numbered.. not the height on the screen
-       bool clamp_y_axis = false;
-       vector<int32_t>  height_list(512) ;
-       vector<int32_t>::iterator j;
-
        if (drag_info.copy && drag_info.move_threshold_passed && drag_info.want_move_threshold) {
 
                drag_info.want_move_threshold = false; // don't copy again
 
                /* duplicate the region(s) */
-               
+
                vector<RegionView*> new_regionviews;
                
                for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ++i) {
+
                        RegionView* rv;
                        RegionView* nrv;
-                       AudioRegionView* arv;
 
                        rv = (*i);
 
-                       
-                       if ((arv = dynamic_cast<AudioRegionView*>(rv)) == 0) {
-                               /* XXX handle MIDI here */
+                       AudioRegionView* arv = dynamic_cast<AudioRegionView*>(rv);
+                       MidiRegionView* mrv = dynamic_cast<MidiRegionView*>(rv);
+
+                       if (arv) {
+                               nrv = new AudioRegionView (*arv);
+                       } else if (mrv) {
+                               nrv = new MidiRegionView (*mrv);
+                       } else {
                                continue;
                        }
 
-                       nrv = new AudioRegionView (*arv);
                        nrv->get_canvas_group()->show ();
 
                        new_regionviews.push_back (nrv);
@@ -2795,37 +3070,136 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                }
 
                /* reset selection to new regionviews */
-               
+
                selection->set (new_regionviews);
-               
+
                /* reset drag_info data to reflect the fact that we are dragging the copies */
                
                drag_info.data = new_regionviews.front();
-               
+
                swap_grab (new_regionviews.front()->get_canvas_group (), 0, event->motion.time);
        }
+}
 
+bool
+Editor::check_region_drag_possible (RouteTimeAxisView** tv)
+{
        /* Which trackview is this ? */
 
        TimeAxisView* tvp = trackview_by_y_position (drag_info.current_pointer_y);
-       AudioTimeAxisView* tv = dynamic_cast<AudioTimeAxisView*>(tvp);
+       (*tv) = dynamic_cast<RouteTimeAxisView*>(tvp);
 
        /* The region motion is only processed if the pointer is over
           an audio track.
        */
        
-       if (!tv || !tv->is_audio_track()) {
+       if (!(*tv) || !(*tv)->is_track()) {
                /* To make sure we hide the verbose canvas cursor when the mouse is 
                   not held over and audiotrack. 
                */
                hide_verbose_canvas_cursor ();
-               return;
+               return false;
        }
        
+       return true;
+}
+
+struct RegionSelectionByPosition {
+    bool operator() (RegionView*a, RegionView* b) {
+           return a->region()->position () < b->region()->position();
+    }
+};
+
+void
+Editor::region_drag_splice_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
+{
+       RouteTimeAxisView* tv;
+       
+       if (!check_region_drag_possible (&tv)) {
+               return;
+       }
+
+       if (!drag_info.move_threshold_passed) {
+               return;
+       }
+
+       int dir;
+
+       if (drag_info.current_pointer_x - drag_info.grab_x > 0) {
+               dir = 1;
+       } else {
+               dir = -1;
+       }
+
+       RegionSelection copy (selection->regions);
+
+       RegionSelectionByPosition cmp;
+       copy.sort (cmp);
+
+       for (RegionSelection::iterator i = copy.begin(); i != copy.end(); ++i) {
+
+               RouteTimeAxisView* atv = dynamic_cast<RouteTimeAxisView*> (&(*i)->get_time_axis_view());
+
+               if (!atv) {
+                       continue;
+               }
+
+               boost::shared_ptr<Playlist> playlist;
+
+               if ((playlist = atv->playlist()) == 0) {
+                       continue;
+               }
+
+               if (!playlist->region_is_shuffle_constrained ((*i)->region())) {
+                       continue;
+               } 
+
+               if (dir > 0) {
+                       if (drag_info.current_pointer_frame < (*i)->region()->last_frame() + 1) {
+                               continue;
+                       }
+               } else {
+                       if (drag_info.current_pointer_frame > (*i)->region()->first_frame()) {
+                               continue;
+                       }
+               }
+
+               
+               playlist->shuffle ((*i)->region(), dir);
+
+               drag_info.grab_x = drag_info.current_pointer_x;
+       }
+}
+
+void
+Editor::region_drag_splice_finished_callback (ArdourCanvas::Item* item, GdkEvent* event)
+{
+}
+
+void
+Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
+{
+       double x_delta;
+       double y_delta = 0;
+       RegionView* rv = reinterpret_cast<RegionView*> (drag_info.data); 
+       nframes_t pending_region_position = 0;
+       int32_t pointer_y_span = 0, canvas_pointer_y_span = 0, original_pointer_order;
+       int32_t visible_y_high = 0, visible_y_low = 512;  //high meaning higher numbered.. not the height on the screen
+       bool clamp_y_axis = false;
+       vector<int32_t>  height_list(512) ;
+       vector<int32_t>::iterator j;
+       RouteTimeAxisView* tv;
+
+       possibly_copy_regions_during_grab (event);
+
+       if (!check_region_drag_possible (&tv)) {
+               return;
+       }
+
        original_pointer_order = drag_info.last_trackview->order;
                
        /************************************************************
-                 Y-Delta Computation
+            Y-Delta Computation
        ************************************************************/   
 
        if (drag_info.brushing) {
@@ -2833,7 +3207,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                pointer_y_span = 0;
                goto y_axis_done;
        }
-       
+
        if ((pointer_y_span = (drag_info.last_trackview->order - tv->order)) != 0) {
 
                int32_t children = 0, numtracks = 0;
@@ -2844,30 +3218,30 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                        TimeAxisView *tracklist_timeview;
                        tracklist_timeview = (*i);
-                       AudioTimeAxisView* atv2 = dynamic_cast<AudioTimeAxisView*>(tracklist_timeview);
-                       list<TimeAxisView*> children_list;
+                       RouteTimeAxisView* rtv2 = dynamic_cast<RouteTimeAxisView*>(tracklist_timeview);
+                       TimeAxisView::Children children_list;
              
                        /* zeroes are audio tracks. ones are other types. */
              
-                       if (!atv2->hidden()) {
+                       if (!rtv2->hidden()) {
                                
-                               if (visible_y_high < atv2->order) {
-                                       visible_y_high = atv2->order;
+                               if (visible_y_high < rtv2->order) {
+                                       visible_y_high = rtv2->order;
                                }
-                               if (visible_y_low > atv2->order) {
-                                       visible_y_low = atv2->order;
+                               if (visible_y_low > rtv2->order) {
+                                       visible_y_low = rtv2->order;
                                }
                
-                               if (!atv2->is_audio_track()) {                            
-                                       tracks = tracks |= (0x01 << atv2->order);
+                               if (!rtv2->is_track()) {                                  
+                                       tracks = tracks |= (0x01 << rtv2->order);
                                }
        
-                               height_list[atv2->order] = (*i)->height;
+                               height_list[rtv2->order] = (*i)->height;
                                children = 1;
-                               if ((children_list = atv2->get_child_list()).size() > 0) {
-                                       for (list<TimeAxisView*>::iterator j = children_list.begin(); j != children_list.end(); ++j) { 
-                                               tracks = tracks |= (0x01 << (atv2->order + children));
-                                               height_list[atv2->order + children] =  (*j)->height;                
+                               if ((children_list = rtv2->get_child_list()).size() > 0) {
+                                       for (TimeAxisView::Children::iterator j = children_list.begin(); j != children_list.end(); ++j) { 
+                                               tracks = tracks |= (0x01 << (rtv2->order + children));
+                                               height_list[rtv2->order + children] =  (*j)->height;                
                                                numtracks++;
                                                children++;     
                                        }
@@ -2902,27 +3276,27 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                        rv2->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
                        rv2->get_canvas_group()->i2w (ix1, iy1);
                        TimeAxisView* tvp2 = trackview_by_y_position (iy1);
-                       RouteTimeAxisView* atv2 = dynamic_cast<RouteTimeAxisView*>(tvp2);
+                       RouteTimeAxisView* rtv2 = dynamic_cast<RouteTimeAxisView*>(tvp2);
 
-                       if (atv2->order != original_pointer_order) {    
+                       if (rtv2->order != original_pointer_order) {    
                                /* this isn't the pointer track */      
 
                                if (canvas_pointer_y_span > 0) {
 
                                        /* moving up the canvas */
-                                       if ((atv2->order - canvas_pointer_y_span) >= visible_y_low) {
+                                       if ((rtv2->order - canvas_pointer_y_span) >= visible_y_low) {
        
                                                int32_t visible_tracks = 0;
                                                while (visible_tracks < canvas_pointer_y_span ) {
                                                        visible_tracks++;
                  
-                                                       while (height_list[atv2->order - (visible_tracks - n)] == 0) {
+                                                       while (height_list[rtv2->order - (visible_tracks - n)] == 0) {
                                                                /* we're passing through a hidden track */
                                                                n--;
                                                        }                 
                                                }
                 
-                                               if (tracks[atv2->order - (canvas_pointer_y_span - n)] != 0x00) {                  
+                                               if (tracks[rtv2->order - (canvas_pointer_y_span - n)] != 0x00) {                  
                                                        clamp_y_axis = true;
                                                }
                    
@@ -2934,7 +3308,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
                                        /*moving down the canvas*/
 
-                                       if ((atv2->order - (canvas_pointer_y_span - n)) <= visible_y_high) { // we will overflow
+                                       if ((rtv2->order - (canvas_pointer_y_span - n)) <= visible_y_high) { // we will overflow
                    
                    
                                                int32_t visible_tracks = 0;
@@ -2942,11 +3316,11 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                                                while (visible_tracks > canvas_pointer_y_span ) {
                                                        visible_tracks--;
                      
-                                                       while (height_list[atv2->order - (visible_tracks - n)] == 0) {             
+                                                       while (height_list[rtv2->order - (visible_tracks - n)] == 0) {             
                                                                n++;
                                                        }                
                                                }
-                                               if (  tracks[atv2->order - ( canvas_pointer_y_span - n)] != 0x00) {
+                                               if (  tracks[rtv2->order - ( canvas_pointer_y_span - n)] != 0x00) {
                                                        clamp_y_axis = true;
                            
                                                }
@@ -2959,9 +3333,9 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                        } else {
                      
                                /* this is the pointer's track */
-                               if ((atv2->order - pointer_y_span) > visible_y_high) { // we will overflow 
+                               if ((rtv2->order - pointer_y_span) > visible_y_high) { // we will overflow 
                                        clamp_y_axis = true;
-                               } else if ((atv2->order - pointer_y_span) < visible_y_low) { // we will underflow
+                               } else if ((rtv2->order - pointer_y_span) < visible_y_low) { // we will underflow
                                        clamp_y_axis = true;
                                }
                        }             
@@ -2980,7 +3354,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        }
          
        /************************************************************
-                       X DELTA COMPUTATION
+           X DELTA COMPUTATION
        ************************************************************/
 
        /* compute the amount of pointer motion in frames, and where
@@ -2989,14 +3363,14 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
        if (drag_info.move_threshold_passed) {
 
-               if ((int32_t)drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+               if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
 
                        nframes_t sync_frame;
                        nframes_t sync_offset;
                        int32_t sync_dir;
            
                        pending_region_position = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
-           
+
                        sync_offset = rv->region()->sync_offset (sync_dir);
                        sync_frame = rv->region()->adjust_to_sync (pending_region_position);
 
@@ -3025,16 +3399,16 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
          
                if (pending_region_position != drag_info.last_frame_position && !drag_info.x_constrained) {
 
-                       /* now compute the canvas unit distance we need to move the regiondrag_info.last_trackview->order
+                       /* now compute the canvas unit distance we need to move the regionview
                           to make it appear at the new location.
                        */
-           
+
                        if (pending_region_position > drag_info.last_frame_position) {
                                x_delta = ((double) (pending_region_position - drag_info.last_frame_position) / frames_per_unit);
                        } else {
                                x_delta = -((double) (drag_info.last_frame_position - pending_region_position) / frames_per_unit);
                        }
-           
+
                        drag_info.last_frame_position = pending_region_position;
            
                } else {
@@ -3048,7 +3422,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        }
 
        /*************************************************************
-                        PREPARE TO MOVE
+           PREPARE TO MOVE
        ************************************************************/
 
        if (x_delta == 0 && (pointer_y_span == 0)) {
@@ -3058,6 +3432,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                return;
        } 
 
+
        if (x_delta < 0) {
                for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ++i) {
 
@@ -3077,7 +3452,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        }
 
        /*************************************************************
-                        MOTION                                                               
+           MOTION                                                                    
        ************************************************************/
 
        bool do_move;
@@ -3096,7 +3471,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
                pair<set<boost::shared_ptr<Playlist> >::iterator,bool> insert_result;
                const list<RegionView*>& layered_regions = selection->regions.by_layer();
-
+               
                for (list<RegionView*>::const_iterator i = layered_regions.begin(); i != layered_regions.end(); ++i) {
            
                        RegionView* rv = (*i);
@@ -3111,14 +3486,14 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                        rv->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
                        rv->get_canvas_group()->i2w (ix1, iy1);
                        TimeAxisView* tvp2 = trackview_by_y_position (iy1);
-                       AudioTimeAxisView* canvas_atv = dynamic_cast<AudioTimeAxisView*>(tvp2);
-                       AudioTimeAxisView* temp_atv;
+                       RouteTimeAxisView* canvas_rtv = dynamic_cast<RouteTimeAxisView*>(tvp2);
+                       RouteTimeAxisView* temp_rtv;
 
                        if ((pointer_y_span != 0) && !clamp_y_axis) {
                                y_delta = 0;
                                int32_t x = 0;
                                for (j = height_list.begin(); j!= height_list.end(); j++) {     
-                                       if (x == canvas_atv->order) {
+                                       if (x == canvas_rtv->order) {
                                                /* we found the track the region is on */
                                                if (x != original_pointer_order) {
                                                        /*this isn't from the same track we're dragging from */
@@ -3156,14 +3531,14 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                                                /* find out where we'll be when we move and set height accordingly */
                  
                                                tvp2 = trackview_by_y_position (iy1 + y_delta);
-                                               temp_atv = dynamic_cast<AudioTimeAxisView*>(tvp2);
-                                               rv->set_height (temp_atv->height);
-       
+                                               temp_rtv = dynamic_cast<RouteTimeAxisView*>(tvp2);
+                                               rv->set_y_position_and_height (0, temp_rtv->height);
+
                                                /*   if you un-comment the following, the region colours will follow the track colours whilst dragging,
                                                     personally, i think this can confuse things, but never mind.
                                                */
                                  
-                                               //const GdkColor& col (temp_atv->view->get_region_color());
+                                               //const GdkColor& col (temp_rtv->view->get_region_color());
                                                //rv->set_color (const_cast<GdkColor&>(col));
                                                break;          
                                        }
@@ -3180,10 +3555,11 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                                if (-x_delta > ix1) {
                                        x_delta = -ix1;
                                }
-                       } else if ((x_delta > 0) &&(rv->region()->last_frame() > max_frames - x_delta)) {
+                       } else if ((x_delta > 0) && (rv->region()->last_frame() > max_frames - x_delta)) {
                                x_delta = max_frames - rv->region()->last_frame();
                        }
 
+
                        if (drag_info.first_move) {
 
                                /* hide any dependent views */
@@ -3202,7 +3578,7 @@ Editor::region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
 
                                rv->fake_set_opaque (true);
                        }
-                       
+
                        if (drag_info.brushing) {
                                mouse_brush_insert_region (rv, pending_region_position);
                        } else {
@@ -3227,13 +3603,17 @@ void
 Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event)
 {
        nframes_t where;
-       RegionView* rv = reinterpret_cast<RegionView *> (drag_info.data);
+       RegionView* rvdi = reinterpret_cast<RegionView *> (drag_info.data);
        pair<set<boost::shared_ptr<Playlist> >::iterator,bool> insert_result;
        bool nocommit = true;
        double speed;
-       RouteTimeAxisView* atv;
+       RouteTimeAxisView* rtv;
        bool regionview_y_movement;
        bool regionview_x_movement;
+       vector<RegionView*> copies;
+       list <boost::shared_ptr<Playlist > > used_playlists;
+       list <sigc::connection > used_connections;
+       bool preserve_selection = false;
 
        /* first_move is set to false if the regionview has been moved in the 
           motion handler. 
@@ -3252,21 +3632,33 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
 
        region_drag_motion_callback (item, event);
 
+       if (Config->get_edit_mode() == Splice && !pre_drag_region_selection.empty()) {
+               selection->set (pre_drag_region_selection);
+               pre_drag_region_selection.clear ();
+       }
+
        if (drag_info.brushing) {
                /* all changes were made during motion event handlers */
+               
+               if (drag_info.copy) {
+                       for (list<RegionView*>::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+                               copies.push_back (*i);
+                       }
+               }
+
                goto out;
        }
 
        /* adjust for track speed */
        speed = 1.0;
 
-       atv = dynamic_cast<AudioTimeAxisView*> (drag_info.last_trackview);
-       if (atv && atv->get_diskstream()) {
-               speed = atv->get_diskstream()->speed();
+       rtv = dynamic_cast<RouteTimeAxisView*> (drag_info.last_trackview);
+       if (rtv && rtv->get_diskstream()) {
+               speed = rtv->get_diskstream()->speed();
        }
        
-       regionview_x_movement = (drag_info.last_frame_position != (nframes_t) (rv->region()->position()/speed));
-       regionview_y_movement = (drag_info.last_trackview != &rv->get_time_axis_view());
+       regionview_x_movement = (drag_info.last_frame_position != (nframes_t) (rvdi->region()->position()/speed));
+       regionview_y_movement = (drag_info.last_trackview != &rvdi->get_time_axis_view());
 
        //printf ("last_frame: %s position is %lu  %g\n", rv->get_time_axis_view().name().c_str(), drag_info.last_frame_position, speed); 
        //printf ("last_rackview: %s \n", drag_info.last_trackview->name().c_str()); 
@@ -3291,27 +3683,50 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
 
        if (regionview_y_movement) {
 
-               /* moved to a different audio track. */
+               /* moved to a different track. */
                
                vector<RegionView*> new_selection;
-
+               
                for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ) {
-           
-                       RegionView* rv = (*i);              
+                       
+                       RegionView* rv = (*i);      
 
                        double ix1, ix2, iy1, iy2;
                        
                        rv->get_canvas_frame()->get_bounds (ix1, iy1, ix2, iy2);
                        rv->get_canvas_group()->i2w (ix1, iy1);
-                       TimeAxisView* tvp2 = trackview_by_y_position (iy1);
-                       AudioTimeAxisView* atv2 = dynamic_cast<AudioTimeAxisView*>(tvp2);
+
+                       RouteTimeAxisView* rtv2 = dynamic_cast<RouteTimeAxisView*>(trackview_by_y_position (iy1));
 
                        boost::shared_ptr<Playlist> from_playlist = rv->region()->playlist();
-                       boost::shared_ptr<Playlist> to_playlist = atv2->playlist();
-           
+                       boost::shared_ptr<Playlist> to_playlist = rtv2->playlist();
+                       
                        where = (nframes_t) (unit_to_frame (ix1) * speed);
                        boost::shared_ptr<Region> new_region (RegionFactory::create (rv->region()));
 
+                       if (! to_playlist->frozen()) {
+                               /* 
+                                  we haven't seen this playlist before. 
+                                  we want to freeze it because we don't want to relayer per-region. 
+                                  its much better to do that just once if the playlist is large. 
+                               */
+
+                               /*
+                                  connect so the selection is changed when the new regionview finally appears (after thaw). 
+                                  keep track of it so we can disconnect later. 
+                               */
+
+                               sigc::connection c = rtv2->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_and_select_new_region_view));
+                               used_connections.push_back (c);
+
+                               /* undo */
+                               session->add_command (new MementoCommand<Playlist>(*to_playlist, &to_playlist->get_state(), 0));
+
+                               /* remember used playlists so we can thaw them later */ 
+                               used_playlists.push_back(to_playlist);
+                               to_playlist->freeze();
+                       }
+                       
                        /* undo the previous hide_dependent_views so that xfades don't
                           disappear on copying regions 
                        */
@@ -3332,25 +3747,26 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                                session->add_command (new MementoCommand<Playlist>(*from_playlist, &from_playlist->get_state(), 0));    
                                from_playlist->remove_region ((rv->region()));
                                session->add_command (new MementoCommand<Playlist>(*from_playlist, 0, &from_playlist->get_state()));    
+
+                       } else {
+
+                               /* the regionview we dragged around is a temporary copy, queue it for deletion */
+
+                               copies.push_back (rv);
                        }
 
-                       latest_regionview = 0;
-                       
-                       sigc::connection c = atv2->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
+                       latest_regionviews.clear ();
+                       sigc::connection c = rtv2->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
                        session->add_command (new MementoCommand<Playlist>(*to_playlist, &to_playlist->get_state(), 0));        
+
                        to_playlist->add_region (new_region, where);
                        session->add_command (new MementoCommand<Playlist>(*to_playlist, 0, &to_playlist->get_state()));        
                        c.disconnect ();
                                                              
-                       if (latest_regionview) {
-                               new_selection.push_back (latest_regionview);
+                       if (!latest_regionviews.empty()) {
+                               new_selection.insert (new_selection.end(), latest_regionviews.begin(), latest_regionviews.end());
                        }
 
-                       if (drag_info.copy) {
-                               // get rid of the copy
-                               delete rv;
-                       } 
-
                        /* OK, this is where it gets tricky. If the playlist was being used by >1 tracks, and the region
                           was selected in all of them, then removing it from the playlist will have removed all
                           trace of it from the selection (i.e. there were N regions selected, we removed 1,
@@ -3363,16 +3779,27 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                           here. if the region selection is not empty, then restart the loop because we know that
                           we must have removed at least the region(view) we've just been working on as well as any
                           that we processed on previous iterations.
+
+                          EXCEPT .... if we are doing a copy drag, then the selection hasn't been modified and
+                          we can just iterate.
+
                        */
 
-                       if (selection->regions.empty()) {
-                               break;
-                       } else { 
-                               i = selection->regions.by_layer().begin();
-                       }
-               } 
+                       if (drag_info.copy) {
+                               ++i;
+                       } else {
+                               if (selection->regions.empty()) {
+                                       break;
+                               } else { 
+                                 /*
+                                   XXX see above .. but we just froze the playlists.. we have to keep iterating, right? 
+                                 */
 
-               selection->set (new_selection);
+                                 //i = selection->regions.by_layer().begin();
+                                 ++i;
+                               }
+                       }
+               }
 
        } else {
 
@@ -3380,25 +3807,21 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
 
                list<RegionView*> regions = selection->regions.by_layer();
 
-               if (drag_info.copy) {
-                       selection->clear_regions();
-               }
-               
                for (list<RegionView*>::iterator i = regions.begin(); i != regions.end(); ++i) {
 
-                       rv = (*i);
+                       RegionView* rv = (*i);
+                       boost::shared_ptr<Playlist> to_playlist = (*i)->region()->playlist();
+                       RouteTimeAxisView* from_rtv = dynamic_cast<RouteTimeAxisView*> (&(rv->get_time_axis_view()));
 
-                       if (rv->region()->locked()) {
+                       if (!rv->region()->can_move()) {
                                continue;
                        }
-                       
 
                        if (regionview_x_movement) {
                                double ownspeed = 1.0;
-                               atv = dynamic_cast<AudioTimeAxisView*> (&(rv->get_time_axis_view()));
 
-                               if (atv && atv->get_diskstream()) {
-                                       ownspeed = atv->get_diskstream()->speed();
+                               if (from_rtv && from_rtv->get_diskstream()) {
+                                       ownspeed = from_rtv->get_diskstream()->speed();
                                }
                                
                                /* base the new region position on the current position of the regionview.*/
@@ -3414,60 +3837,71 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
                                where = rv->region()->position();
                        }
 
-                       boost::shared_ptr<Playlist> to_playlist = rv->region()->playlist();
+                       if (! to_playlist->frozen()) {
+                               sigc::connection c = from_rtv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_and_select_new_region_view));
+                               used_connections.push_back (c);
 
-                       assert (to_playlist);
+                               /* add the undo */      
+                               session->add_command (new MementoCommand<Playlist>(*to_playlist, &to_playlist->get_state(), 0));
 
-                       /* add the undo */
-
-                       session->add_command (new MementoCommand<Playlist>(*to_playlist, &to_playlist->get_state(), 0));        
+                               used_playlists.push_back(to_playlist);
+                               to_playlist->freeze();
+                       }
 
                        if (drag_info.copy) {
 
                                boost::shared_ptr<Region> newregion;
                                boost::shared_ptr<Region> ar;
+                               boost::shared_ptr<Region> mr;
 
                                if ((ar = boost::dynamic_pointer_cast<AudioRegion>(rv->region())) != 0) {
                                        newregion = RegionFactory::create (ar);
-                               } else {
-                                       /* XXX MIDI HERE drobilla */
-                                       continue;
+                               } else if ((mr = boost::dynamic_pointer_cast<MidiRegion>(rv->region())) != 0) {
+                                       newregion = RegionFactory::create (mr);
                                }
 
                                /* add it */
-                               
-                               latest_regionview = 0;
-                               sigc::connection c = atv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
-                               to_playlist->add_region (newregion, (nframes_t) (where * atv->get_diskstream()->speed()));
+
+                               latest_regionviews.clear ();
+                               sigc::connection c = rtv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
+                               to_playlist->add_region (newregion, (nframes_t) (where * rtv->get_diskstream()->speed()));
                                c.disconnect ();
-                               
-                               if (latest_regionview) {
-                                       atv->reveal_dependent_views (*latest_regionview);
-                                       selection->add (latest_regionview);
+
+                               if (!latest_regionviews.empty()) {
+                                       // XXX why just the first one ? we only expect one
+                                       rtv->reveal_dependent_views (*latest_regionviews.front());
+                                       selection->add (latest_regionviews);
                                }
                                
-                               /* if the original region was locked, we don't care for the new one */
-                               
-                               newregion->set_locked (false);                  
-
                        } else {
 
                                /* just change the model */
 
                                rv->region()->set_position (where, (void*) this);
+                               preserve_selection = true;
 
                        }
 
-                       /* add the redo */
+               }
 
-                       session->add_command (new MementoCommand<Playlist>(*to_playlist, 0, &to_playlist->get_state()));
+       }
+       if (! preserve_selection) {
+         //selection->clear_regions();
+       }
+       while (used_playlists.size() > 0) {
 
-                       /* get rid of the copy */
+               list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
+               (*i)->thaw();
 
-                       if (drag_info.copy) {
-                               delete rv;
-                       }
+               if (used_connections.size()) {
+                       sigc::connection c = used_connections.front();
+                       c.disconnect();
+                       used_connections.pop_front();
                }
+               /* add the redo */
+
+               session->add_command (new MementoCommand<Playlist>(*(*i), 0, &(*i)->get_state()));
+               used_playlists.pop_front();
        }
 
   out:
@@ -3475,6 +3909,64 @@ Editor::region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event
        if (!nocommit) {
                commit_reversible_command ();
        }
+
+       for (vector<RegionView*>::iterator x = copies.begin(); x != copies.end(); ++x) {
+               delete *x;
+       }
+}
+
+       
+void
+Editor::create_region_drag_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
+{
+       if (drag_info.move_threshold_passed) {
+               if (drag_info.first_move) {
+                       // TODO: create region-create-drag region view here
+                       drag_info.first_move = false;
+               }
+
+               // TODO: resize region-create-drag region view here
+       }
+} 
+
+void
+Editor::create_region_drag_finished_callback (ArdourCanvas::Item* item, GdkEvent* event)
+{
+       MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (drag_info.last_trackview);
+       if (!mtv)
+               return;
+
+       const boost::shared_ptr<MidiDiskstream> diskstream =
+               boost::dynamic_pointer_cast<MidiDiskstream>(mtv->view()->trackview().track()->diskstream());
+       
+       if (!diskstream) {
+               warning << "Cannot create non-MIDI region" << endl;
+               return;
+       }
+
+       if (drag_info.first_move) {
+               begin_reversible_command (_("create region"));
+               XMLNode &before = mtv->playlist()->get_state();
+
+               nframes_t start = drag_info.grab_frame;
+               snap_to (start, -1);
+               const Meter& m = session->tempo_map().meter_at(start);
+               const Tempo& t = session->tempo_map().tempo_at(start);
+               double length = floor (m.frames_per_bar(t, session->frame_rate()));
+
+               boost::shared_ptr<Source> src = session->create_midi_source_for_session(*diskstream.get());
+                               
+               mtv->playlist()->add_region (boost::dynamic_pointer_cast<MidiRegion>
+                                            (RegionFactory::create(src, 0, (nframes_t) length, 
+                                                                   PBD::basename_nosuffix(src->name()))), start);
+               XMLNode &after = mtv->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*mtv->playlist().get(), &before, &after));
+               commit_reversible_command();
+
+       } else {
+               create_region_drag_motion_callback (item, event);
+               // TODO: create region-create-drag region here
+       }
 }
 
 void
@@ -3484,25 +3976,30 @@ Editor::region_view_item_click (AudioRegionView& rv, GdkEventButton* event)
           this is an alignment click (control used)
        */
        
-       if (Keyboard::modifier_state_contains (event->state, Keyboard::Control)) {
+       if (Keyboard::modifier_state_contains (event->state, Keyboard::PrimaryModifier)) {
                TimeAxisView* tv = &rv.get_time_axis_view();
-               AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(tv);
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(tv);
                double speed = 1.0;
-               if (atv && atv->is_audio_track()) {
-                       speed = atv->get_diskstream()->speed();
+               if (rtv && rtv->is_track()) {
+                       speed = rtv->get_diskstream()->speed();
                }
 
-               if (Keyboard::modifier_state_equals (event->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Alt))) {
+               nframes64_t where = get_preferred_edit_position();
 
-                       align_region (rv.region(), SyncPoint, (nframes_t) (edit_cursor->current_frame * speed));
-
-               } else if (Keyboard::modifier_state_equals (event->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
-
-                       align_region (rv.region(), End, (nframes_t) (edit_cursor->current_frame * speed));
-
-               } else {
+               if (where >= 0) {
 
-                       align_region (rv.region(), Start, (nframes_t) (edit_cursor->current_frame * speed));
+                       if (Keyboard::modifier_state_equals (event->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::SecondaryModifier))) {
+                               
+                               align_region (rv.region(), SyncPoint, (nframes_t) (where * speed));
+                               
+                       } else if (Keyboard::modifier_state_equals (event->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
+                               
+                               align_region (rv.region(), End, (nframes_t) (where * speed));
+                               
+                       } else {
+                               
+                               align_region (rv.region(), Start, (nframes_t) (where * speed));
+                       }
                }
        }
 }
@@ -3634,7 +4131,14 @@ Editor::show_verbose_duration_cursor (nframes_t start, nframes_t end, double off
 void
 Editor::collect_new_region_view (RegionView* rv)
 {
-       latest_regionview = rv;
+       latest_regionviews.push_back (rv);
+}
+
+void
+Editor::collect_and_select_new_region_view (RegionView* rv)
+{
+       selection->add(rv);
+       latest_regionviews.push_back (rv);
 }
 
 void
@@ -3664,8 +4168,8 @@ Editor::start_selection_grab (ArdourCanvas::Item* item, GdkEvent* event)
           set the regionview we want to then drag.
        */
        
-       latest_regionview = 0;
-       sigc::connection c = clicked_audio_trackview->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
+       latest_regionviews.clear();
+       sigc::connection c = clicked_routeview->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
        
        /* A selection grab currently creates two undo/redo operations, one for 
           creating the new region and another for moving it.
@@ -3673,35 +4177,36 @@ Editor::start_selection_grab (ArdourCanvas::Item* item, GdkEvent* event)
 
        begin_reversible_command (_("selection grab"));
 
-       boost::shared_ptr<Playlist> playlist = clicked_trackview->playlist();
+       boost::shared_ptr<Playlist> playlist = clicked_axisview->playlist();
 
-        XMLNode *before = &(playlist->get_state());
-       clicked_trackview->playlist()->add_region (region, selection->time[clicked_selection].start);
-        XMLNode *after = &(playlist->get_state());
+       XMLNode *before = &(playlist->get_state());
+       clicked_routeview->playlist()->add_region (region, selection->time[clicked_selection].start);
+       XMLNode *after = &(playlist->get_state());
        session->add_command(new MementoCommand<Playlist>(*playlist, before, after));
 
        commit_reversible_command ();
        
        c.disconnect ();
        
-       if (latest_regionview == 0) {
+       if (latest_regionviews.empty()) {
                /* something went wrong */
                return;
        }
 
        /* we need to deselect all other regionviews, and select this one
-          i'm ignoring undo stuff, because the region creation will take care of it */
-       selection->set (latest_regionview);
+          i'm ignoring undo stuff, because the region creation will take care of it 
+       */
+       selection->set (latest_regionviews);
        
-       drag_info.item = latest_regionview->get_canvas_group();
-       drag_info.data = latest_regionview;
+       drag_info.item = latest_regionviews.front()->get_canvas_group();
+       drag_info.data = latest_regionviews.front();
        drag_info.motion_callback = &Editor::region_drag_motion_callback;
        drag_info.finished_callback = &Editor::region_drag_finished_callback;
 
        start_grab (event);
        
-       drag_info.last_trackview = clicked_trackview;
-       drag_info.last_frame_position = latest_regionview->region()->position();
+       drag_info.last_trackview = clicked_axisview;
+       drag_info.last_frame_position = latest_regionviews.front()->region()->position();
        drag_info.pointer_frame_offset = drag_info.grab_frame - drag_info.last_frame_position;
        
        show_verbose_time_cursor (drag_info.last_frame_position, 10);
@@ -3710,13 +4215,11 @@ Editor::start_selection_grab (ArdourCanvas::Item* item, GdkEvent* event)
 void
 Editor::cancel_selection ()
 {
-        for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                (*i)->hide_selection ();
        }
-       begin_reversible_command (_("cancel selection"));
        selection->clear ();
        clicked_selection = 0;
-       commit_reversible_command ();
 }      
 
 void
@@ -3737,7 +4240,7 @@ Editor::start_selection_op (ArdourCanvas::Item* item, GdkEvent* event, Selection
 
        switch (op) {
        case CreateSelection:
-               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Shift)) {
+               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
                        drag_info.copy = true;
                } else {
                        drag_info.copy = false;
@@ -3746,8 +4249,8 @@ Editor::start_selection_op (ArdourCanvas::Item* item, GdkEvent* event, Selection
                break;
 
        case SelectionStartTrim:
-               if (clicked_trackview) {
-                       clicked_trackview->order_selection_trims (item, true);
+               if (clicked_axisview) {
+                       clicked_axisview->order_selection_trims (item, true);
                } 
                start_grab (event, trimmer_cursor);
                start = selection->time[clicked_selection].start;
@@ -3755,8 +4258,8 @@ Editor::start_selection_op (ArdourCanvas::Item* item, GdkEvent* event, Selection
                break;
                
        case SelectionEndTrim:
-               if (clicked_trackview) {
-                       clicked_trackview->order_selection_trims (item, false);
+               if (clicked_axisview) {
+                       clicked_axisview->order_selection_trims (item, false);
                }
                start_grab (event, trimmer_cursor);
                end = selection->time[clicked_selection].end;
@@ -3785,10 +4288,9 @@ Editor::drag_selection (ArdourCanvas::Item* item, GdkEvent* event)
        nframes_t length;
        nframes_t pending_position;
 
-       if ((int32_t) drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
+       if (drag_info.current_pointer_frame > drag_info.pointer_frame_offset) {
                pending_position = drag_info.current_pointer_frame - drag_info.pointer_frame_offset;
-       }
-       else {
+       } else {
                pending_position = 0;
        }
        
@@ -3831,7 +4333,7 @@ Editor::drag_selection (ArdourCanvas::Item* item, GdkEvent* event)
                                drag_info.copy = false;
                        } else {
                                /* new selection-> */
-                               clicked_selection = selection->set (clicked_trackview, start, end);
+                               clicked_selection = selection->set (clicked_axisview, start, end);
                        }
                } 
                break;
@@ -3935,10 +4437,10 @@ void
 Editor::start_trim (ArdourCanvas::Item* item, GdkEvent* event)
 {
        double speed = 1.0;
-       TimeAxisView* tvp = clicked_trackview;
-       AudioTimeAxisView* tv = dynamic_cast<AudioTimeAxisView*>(tvp);
+       TimeAxisView* tvp = clicked_axisview;
+       RouteTimeAxisView* tv = dynamic_cast<RouteTimeAxisView*>(tvp);
 
-       if (tv && tv->is_audio_track()) {
+       if (tv && tv->is_track()) {
                speed = tv->get_diskstream()->speed();
        }
        
@@ -3953,7 +4455,7 @@ Editor::start_trim (ArdourCanvas::Item* item, GdkEvent* event)
 
        start_grab (event, trimmer_cursor);
        
-       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Control)) {
+       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
                trim_op = ContentsTrim;
        } else {
                /* These will get overridden for a point trim.*/
@@ -3993,11 +4495,11 @@ Editor::trim_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        */ 
 
        double speed = 1.0;
-       TimeAxisView* tvp = clicked_trackview;
+       TimeAxisView* tvp = clicked_axisview;
        RouteTimeAxisView* tv = dynamic_cast<RouteTimeAxisView*>(tvp);
        pair<set<boost::shared_ptr<Playlist> >::iterator,bool> insert_result;
 
-       if (tv && tv->is_audio_track()) {
+       if (tv && tv->is_track()) {
                speed = tv->get_diskstream()->speed();
        }
        
@@ -4044,7 +4546,8 @@ Editor::trim_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                        boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
                        insert_result = motion_frozen_playlists.insert (pl);
                        if (insert_result.second) {
-                                session->add_command(new MementoCommand<Playlist>(*pl, &pl->get_state(), 0));
+                               session->add_command(new MementoCommand<Playlist>(*pl, &pl->get_state(), 0));
+                               pl->freeze();
                        }
                }
        }
@@ -4059,7 +4562,7 @@ Editor::trim_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
        case StartTrim:
                if ((left_direction == false) && (drag_info.current_pointer_frame <= rv->region()->first_frame()/speed)) {
                        break;
-                } else {
+               } else {
                        for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ++i) {
                                single_start_trim (**i, frame_delta, left_direction, obey_snap);
                        }
@@ -4080,7 +4583,7 @@ Editor::trim_motion_callback (ArdourCanvas::Item* item, GdkEvent* event)
                {
                        bool swap_direction = false;
 
-                       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Control)) {
+                       if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) {
                                swap_direction = true;
                        }
                        
@@ -4121,10 +4624,10 @@ Editor::single_contents_trim (RegionView& rv, nframes_t frame_delta, bool left_d
        nframes_t new_bound;
 
        double speed = 1.0;
-       TimeAxisView* tvp = clicked_trackview;
+       TimeAxisView* tvp = clicked_axisview;
        RouteTimeAxisView* tv = dynamic_cast<RouteTimeAxisView*>(tvp);
 
-       if (tv && tv->is_audio_track()) {
+       if (tv && tv->is_track()) {
                speed = tv->get_diskstream()->speed();
        }
        
@@ -4161,10 +4664,10 @@ Editor::single_start_trim (RegionView& rv, nframes_t frame_delta, bool left_dire
        nframes_t new_bound;
 
        double speed = 1.0;
-       TimeAxisView* tvp = clicked_trackview;
-       AudioTimeAxisView* tv = dynamic_cast<AudioTimeAxisView*>(tvp);
+       TimeAxisView* tvp = clicked_axisview;
+       RouteTimeAxisView* tv = dynamic_cast<RouteTimeAxisView*>(tvp);
 
-       if (tv && tv->is_audio_track()) {
+       if (tv && tv->is_track()) {
                speed = tv->get_diskstream()->speed();
        }
        
@@ -4195,10 +4698,10 @@ Editor::single_end_trim (RegionView& rv, nframes_t frame_delta, bool left_direct
        nframes_t new_bound;
 
        double speed = 1.0;
-       TimeAxisView* tvp = clicked_trackview;
-       AudioTimeAxisView* tv = dynamic_cast<AudioTimeAxisView*>(tvp);
+       TimeAxisView* tvp = clicked_axisview;
+       RouteTimeAxisView* tv = dynamic_cast<RouteTimeAxisView*>(tvp);
 
-       if (tv && tv->is_audio_track()) {
+       if (tv && tv->is_track()) {
                speed = tv->get_diskstream()->speed();
        }
        
@@ -4221,7 +4724,7 @@ Editor::trim_finished_callback (ArdourCanvas::Item* item, GdkEvent* event)
        if (!drag_info.first_move) {
                trim_motion_callback (item, event);
                
-               if (!clicked_regionview->get_selected()) {
+               if (!selection->selected (clicked_regionview)) {
                        thaw_region_after_trim (*clicked_regionview);           
                } else {
                        
@@ -4234,9 +4737,9 @@ Editor::trim_finished_callback (ArdourCanvas::Item* item, GdkEvent* event)
                }
                
                for (set<boost::shared_ptr<Playlist> >::iterator p = motion_frozen_playlists.begin(); p != motion_frozen_playlists.end(); ++p) {
-                       //(*p)->thaw ();
-                        session->add_command (new MementoCommand<Playlist>(*(*p).get(), 0, &(*p)->get_state()));
-                }
+                       (*p)->thaw ();
+                       session->add_command (new MementoCommand<Playlist>(*(*p).get(), 0, &(*p)->get_state()));
+               }
                
                motion_frozen_playlists.clear ();
 
@@ -4263,17 +4766,17 @@ Editor::point_trim (GdkEvent* event)
                trim_op = StartTrim;
                begin_reversible_command (_("Start point trim"));
 
-               if (rv->get_selected()) {
+               if (selection->selected (rv)) {
 
                        for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin();
                             i != selection->regions.by_layer().end(); ++i)
                        {
                                if (!(*i)->region()->locked()) {
                                        boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
-                                        XMLNode &before = pl->get_state();
+                                       XMLNode &before = pl->get_state();
                                        (*i)->region()->trim_front (new_bound, this);   
-                                        XMLNode &after = pl->get_state();
-                                        session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
+                                       XMLNode &after = pl->get_state();
+                                       session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
                                }
                        }
 
@@ -4283,7 +4786,7 @@ Editor::point_trim (GdkEvent* event)
                                boost::shared_ptr<Playlist> pl = rv->region()->playlist();
                                XMLNode &before = pl->get_state();
                                rv->region()->trim_front (new_bound, this);     
-                                XMLNode &after = pl->get_state();
+                               XMLNode &after = pl->get_state();
                                session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
                        }
                }
@@ -4295,7 +4798,7 @@ Editor::point_trim (GdkEvent* event)
                trim_op = EndTrim;
                begin_reversible_command (_("End point trim"));
 
-               if (rv->get_selected()) {
+               if (selection->selected (rv)) {
                        
                        for (list<RegionView*>::const_iterator i = selection->regions.by_layer().begin(); i != selection->regions.by_layer().end(); ++i)
                        {
@@ -4314,7 +4817,7 @@ Editor::point_trim (GdkEvent* event)
                                boost::shared_ptr<Playlist> pl = rv->region()->playlist();
                                XMLNode &before = pl->get_state();
                                rv->region()->trim_end (new_bound, this);
-                                XMLNode &after = pl->get_state();
+                               XMLNode &after = pl->get_state();
                                session->add_command (new MementoCommand<Playlist>(*pl.get(), &before, &after));
                        }
                }
@@ -4337,7 +4840,7 @@ Editor::thaw_region_after_trim (RegionView& rv)
        }
 
        region->thaw (_("trimmed region"));
-        XMLNode &after = region->playlist()->get_state();
+       XMLNode &after = region->playlist()->get_state();
        session->add_command (new MementoCommand<Playlist>(*(region->playlist()), 0, &after));
 
        AudioRegionView* arv = dynamic_cast<AudioRegionView*>(&rv);
@@ -4381,8 +4884,9 @@ Editor::start_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event, Ran
        switch (op) {
        case CreateRangeMarker:
        case CreateTransportMarker:
-               
-               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::Shift)) {
+       case CreateCDMarker:
+       
+               if (Keyboard::modifier_state_equals (event->button.state, Keyboard::TertiaryModifier)) {
                        drag_info.copy = true;
                } else {
                        drag_info.copy = false;
@@ -4415,6 +4919,7 @@ Editor::drag_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event)
        switch (range_marker_op) {
        case CreateRangeMarker:
        case CreateTransportMarker:
+       case CreateCDMarker:
                if (drag_info.first_move) {
                        snap_to (drag_info.grab_frame);
                }
@@ -4472,19 +4977,27 @@ Editor::end_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event)
 {
        Location * newloc = 0;
        string rangename;
+       int flags;
        
        if (!drag_info.first_move) {
                drag_range_markerbar_op (item, event);
 
                switch (range_marker_op) {
                case CreateRangeMarker:
+               case CreateCDMarker:
                    {
                        begin_reversible_command (_("new range marker"));
-                        XMLNode &before = session->locations()->get_state();
+                       XMLNode &before = session->locations()->get_state();
                        session->locations()->next_available_name(rangename,"unnamed");
-                       newloc = new Location(temp_location->start(), temp_location->end(), rangename, Location::IsRangeMarker);
+                       if (range_marker_op == CreateCDMarker) {
+                               flags =  Location::IsRangeMarker|Location::IsCDMarker;
+                       }
+                       else {
+                               flags =  Location::IsRangeMarker;
+                       }
+                       newloc = new Location(temp_location->start(), temp_location->end(), rangename, (Location::Flags) flags);
                        session->locations()->add (newloc, true);
-                        XMLNode &after = session->locations()->get_state();
+                       XMLNode &after = session->locations()->get_state();
                        session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
                        commit_reversible_command ();
                        
@@ -4502,7 +5015,7 @@ Editor::end_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event)
        } else {
                /* just a click, no pointer movement. remember that context menu stuff was handled elsewhere */
 
-               if (Keyboard::no_modifier_keys_pressed (&event->button)) {
+               if (Keyboard::no_modifier_keys_pressed (&event->button) && range_marker_op != CreateCDMarker) {
 
                        nframes_t start;
                        nframes_t end;
@@ -4521,7 +5034,7 @@ Editor::end_range_markerbar_op (ArdourCanvas::Item* item, GdkEvent* event)
                        switch (mouse_mode) {
                        case MouseObject:
                                /* find the two markers on either side and then make the selection from it */
-                               select_all_within (start, end, 0.0f, FLT_MAX, Selection::Set);
+                               select_all_within (start, end, 0.0f, FLT_MAX, track_views, Selection::Set);
                                break;
 
                        case MouseRange:
@@ -4724,9 +5237,9 @@ Editor::end_rubberband_select (ArdourCanvas::Item* item, GdkEvent* event)
                begin_reversible_command (_("rubberband selection"));
 
                if (drag_info.grab_frame < drag_info.last_pointer_frame) {
-                       commit = select_all_within (drag_info.grab_frame, drag_info.last_pointer_frame, y1, y2, op);
+                       commit = select_all_within (drag_info.grab_frame, drag_info.last_pointer_frame, y1, y2, track_views, op);
                } else {
-                       commit = select_all_within (drag_info.last_pointer_frame, drag_info.grab_frame, y1, y2, op);
+                       commit = select_all_within (drag_info.last_pointer_frame, drag_info.grab_frame, y1, y2, track_views, op);
                }               
 
                if (commit) {
@@ -4758,10 +5271,10 @@ Editor::mouse_rename_region (ArdourCanvas::Item* item, GdkEvent* event)
        prompter.show_all ();
        switch (prompter.run ()) {
        case Gtk::RESPONSE_ACCEPT:
-        string str;
+               string str;
                prompter.get_result(str);
                if (str.length()) {
-               clicked_regionview->region()->set_name (str);
+                       clicked_regionview->region()->set_name (str);
                }
                break;
        }
@@ -4811,13 +5324,31 @@ Editor::end_time_fx (ArdourCanvas::Item* item, GdkEvent* event)
        if (drag_info.first_move) {
                return;
        }
+
+       if (drag_info.last_pointer_frame < clicked_regionview->region()->position()) {
+               /* backwards drag of the left edge - not usable */
+               return;
+       }
        
        nframes_t newlen = drag_info.last_pointer_frame - clicked_regionview->region()->position();
-       float percentage = (float) ((double) newlen - (double) clicked_regionview->region()->length()) / ((double) newlen) * 100.0f;
-       
+
+       float percentage = (double) newlen / (double) clicked_regionview->region()->length();
+
+#ifndef USE_RUBBERBAND
+       // Soundtouch uses percentage / 100 instead of normal (/ 1) 
+       if (clicked_regionview->region()->data_type() == DataType::AUDIO) {
+               percentage = (float) ((double) newlen - (double) clicked_regionview->region()->length()) / ((double) newlen) * 100.0f;
+#endif 
+       }
+
        begin_reversible_command (_("timestretch"));
 
-       if (run_timestretch (selection->regions, percentage) == 0) {
+       // XXX how do timeFX on multiple regions ?
+
+       RegionSelection rs;
+       rs.add (clicked_regionview);
+
+       if (time_stretch (rs, percentage) == 0) {
                session->commit_reversible_command ();
        }
 }
@@ -4839,9 +5370,7 @@ Editor::mouse_brush_insert_region (RegionView* rv, nframes_t pos)
        }
 
        switch (snap_type) {
-       case SnapToFrame:
        case SnapToMark:
-       case SnapToEditCursor:
                return;
 
        default:
@@ -4854,18 +5383,18 @@ Editor::mouse_brush_insert_region (RegionView* rv, nframes_t pos)
                return;
        }
 
-       RouteTimeAxisView* atv = dynamic_cast<RouteTimeAxisView*>(&arv->get_time_axis_view());
+       RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&arv->get_time_axis_view());
 
-       if (atv == 0 || !atv->is_audio_track()) {
+       if (rtv == 0 || !rtv->is_track()) {
                return;
        }
 
-       boost::shared_ptr<Playlist> playlist = atv->playlist();
-       double speed = atv->get_diskstream()->speed();
+       boost::shared_ptr<Playlist> playlist = rtv->playlist();
+       double speed = rtv->get_diskstream()->speed();
        
-        XMLNode &before = playlist->get_state();
+       XMLNode &before = playlist->get_state();
        playlist->add_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (arv->audio_region())), (nframes_t) (pos * speed));
-        XMLNode &after = playlist->get_state();
+       XMLNode &after = playlist->get_state();
        session->add_command(new MementoCommand<Playlist>(*playlist.get(), &before, &after));
        
        // playlist is frozen, so we have to update manually
@@ -4888,3 +5417,4 @@ Editor::track_height_step_timeout ()
        }
        return true;
 }
+