Vkeybd: use ArdourWidgets for all GUI elements
[ardour.git] / gtk2_ardour / editor_canvas_events.cc
index be2b5c3de7431802ea0a13006a896111b77f9e56..acf00fdfdbdd5d82d7d6fdf767ed6b14887df4ca 100644 (file)
@@ -1,21 +1,28 @@
 /*
-    Copyright (C) 2000 Paul Davis
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
+ * Copyright (C) 2005-2018 Paul Davis <paul@linuxaudiosystems.com>
+ * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
+ * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
+ * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
+ * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
+ * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
+ * Copyright (C) 2015-2016 Tim Mayberry <mojofunk@gmail.com>
+ * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
+ * Copyright (C) 2015-2019 Ben Loftis <ben@harrisonconsoles.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
 
 #include <cstdlib>
 #include <cmath>
 
 #include "pbd/stacktrace.h"
 
+#include "ardour/audio_track.h"
+#include "ardour/midi_track.h"
 #include "ardour/midi_region.h"
-#include "ardour/region_factory.h"
 #include "ardour/profile.h"
+#include "ardour/region_factory.h"
 
 #include "canvas/canvas.h"
 #include "canvas/text.h"
+#include "canvas/scroll_group.h"
 
 #include "editor.h"
 #include "keyboard.h"
 #include "editor_drag.h"
 #include "midi_time_axis.h"
 #include "editor_regions.h"
+#include "editor_sources.h"
+#include "ui_config.h"
 #include "verbose_cursor.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
@@ -60,11 +72,6 @@ using Gtkmm2ext::Keyboard;
 bool
 Editor::track_canvas_scroll (GdkEventScroll* ev)
 {
-       if (Keyboard::some_magic_widget_has_focus()) {
-               return false;
-       }
-       
-       framepos_t xdelta;
        int direction = ev->direction;
 
        /* this event arrives without transformation by the canvas, so we have
@@ -72,20 +79,19 @@ Editor::track_canvas_scroll (GdkEventScroll* ev)
         */
 
        Duple event_coords = _track_canvas->window_to_canvas (Duple (ev->x, ev->y));
-       
-  retry:
+
        switch (direction) {
        case GDK_SCROLL_UP:
                if (Keyboard::modifier_state_equals (ev->state, Keyboard::ScrollZoomHorizontalModifier)) {
-                       //for mouse-wheel zoom, force zoom-focus to mouse
-                       Editing::ZoomFocus temp_focus = zoom_focus;
-                       zoom_focus = Editing::ZoomFocusMouse;
-                       temporal_zoom_step (false);
-                       zoom_focus = temp_focus;
+                       if (UIConfiguration::instance().get_use_mouse_position_as_zoom_focus_on_scroll()) {
+                               temporal_zoom_step_mouse_focus (false);
+                       } else {
+                               temporal_zoom_step (false);
+                       }
                        return true;
                } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::ScrollHorizontalModifier)) {
-                       direction = GDK_SCROLL_LEFT;
-                       goto retry;
+                       scroll_left_step ();
+                       return true;
                } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::ScrollZoomVerticalModifier)) {
                        if (!current_stepping_trackview) {
                                step_timeout = Glib::signal_timeout().connect (sigc::mem_fun(*this, &Editor::track_height_step_timeout), 500);
@@ -99,22 +105,22 @@ Editor::track_canvas_scroll (GdkEventScroll* ev)
                        current_stepping_trackview->step_height (false);
                        return true;
                } else {
-                       scroll_tracks_up_line ();
+                       scroll_up_one_track ();
                        return true;
                }
                break;
 
        case GDK_SCROLL_DOWN:
                if (Keyboard::modifier_state_equals (ev->state, Keyboard::ScrollZoomHorizontalModifier)) {
-                       //for mouse-wheel zoom, force zoom-focus to mouse
-                       Editing::ZoomFocus temp_focus = zoom_focus;
-                       zoom_focus = Editing::ZoomFocusMouse;
-                       temporal_zoom_step (true);
-                       zoom_focus = temp_focus;
+                       if (UIConfiguration::instance().get_use_mouse_position_as_zoom_focus_on_scroll()) {
+                               temporal_zoom_step_mouse_focus (true);
+                       } else {
+                               temporal_zoom_step (true);
+                       }
                        return true;
                } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::ScrollHorizontalModifier)) {
-                       direction = GDK_SCROLL_RIGHT;
-                       goto retry;
+                       scroll_right_step ();
+                       return true;
                } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::ScrollZoomVerticalModifier)) {
                        if (!current_stepping_trackview) {
                                step_timeout = Glib::signal_timeout().connect (sigc::mem_fun(*this, &Editor::track_height_step_timeout), 500);
@@ -128,27 +134,19 @@ Editor::track_canvas_scroll (GdkEventScroll* ev)
                        current_stepping_trackview->step_height (true);
                        return true;
                } else {
-                       scroll_tracks_down_line ();
+                       scroll_down_one_track ();
                        return true;
                }
                break;
 
        case GDK_SCROLL_LEFT:
-               xdelta = (current_page_samples() / 8);
-               if (leftmost_frame > xdelta) {
-                       reset_x_origin (leftmost_frame - xdelta);
-               } else {
-                       reset_x_origin (0);
-               }
+               scroll_left_step ();
+               return true;
                break;
 
        case GDK_SCROLL_RIGHT:
-               xdelta = (current_page_samples() / 8);
-               if (max_framepos - xdelta > leftmost_frame) {
-                       reset_x_origin (leftmost_frame + xdelta);
-               } else {
-                       reset_x_origin (max_framepos - current_page_samples());
-               }
+               scroll_right_step ();
+               return true;
                break;
 
        default:
@@ -163,7 +161,7 @@ bool
 Editor::canvas_scroll_event (GdkEventScroll *event, bool from_canvas)
 {
        if (from_canvas) {
-               boost::optional<Rect> rulers = _time_markers_group->bounding_box();
+               boost::optional<ArdourCanvas::Rect> rulers = _time_markers_group->bounding_box();
                if (rulers && rulers->contains (Duple (event->x, event->y))) {
                        return canvas_ruler_event ((GdkEvent*) event, timecode_ruler, TimecodeRulerItem);
                }
@@ -174,18 +172,24 @@ Editor::canvas_scroll_event (GdkEventScroll *event, bool from_canvas)
 }
 
 bool
-Editor::track_canvas_button_press_event (GdkEventButton */*event*/)
+Editor::track_canvas_button_press_event (GdkEventButton *event)
 {
-       selection->clear ();
        _track_canvas->grab_focus();
+       if (!Keyboard::is_context_menu_event (event)) {
+               begin_reversible_selection_op (X_("Clear Selection Click (track canvas)"));
+               selection->clear ();
+               commit_reversible_selection_op();
+       }
        return false;
 }
 
 bool
 Editor::track_canvas_button_release_event (GdkEventButton *event)
 {
-       if (_drags->active ()) {
-               _drags->end_grab ((GdkEvent*) event);
+       if (!Keyboard::is_context_menu_event (event)) {
+               if (_drags->active ()) {
+                       _drags->end_grab ((GdkEvent*) event);
+               }
        }
        return false;
 }
@@ -200,18 +204,12 @@ Editor::track_canvas_motion_notify_event (GdkEventMotion */*event*/)
 }
 
 bool
-Editor::track_canvas_motion (GdkEvent *ev)
+Editor::typed_event (ArdourCanvas::Item* item, GdkEvent *event, ItemType type)
 {
-       if (_verbose_cursor->visible ()) {
-               _verbose_cursor->set_position (ev->motion.x + 10, ev->motion.y + 10);
+       if (!session () || session()->loading () || session()->deletion_in_progress ()) {
+               return false;
        }
 
-       return false;
-}
-
-bool
-Editor::typed_event (ArdourCanvas::Item* item, GdkEvent *event, ItemType type)
-{
        gint ret = FALSE;
 
        switch (event->type) {
@@ -278,16 +276,14 @@ Editor::canvas_region_view_event (GdkEvent *event, ArdourCanvas::Item* item, Reg
                break;
 
        case GDK_ENTER_NOTIFY:
-               if (event->crossing.detail != GDK_NOTIFY_INFERIOR) {
-                       set_entered_regionview (rv);
-                       ret = true;
-               }
+               set_entered_regionview (rv);
+               ret = enter_handler (item, event, RegionItem);
                break;
 
        case GDK_LEAVE_NOTIFY:
                if (event->crossing.detail != GDK_NOTIFY_INFERIOR) {
                        set_entered_regionview (0);
-                       ret = true;
+                       ret = leave_handler (item, event, RegionItem);
                }
                break;
 
@@ -298,6 +294,42 @@ Editor::canvas_region_view_event (GdkEvent *event, ArdourCanvas::Item* item, Reg
        return ret;
 }
 
+bool
+Editor::canvas_wave_view_event (GdkEvent *event, ArdourCanvas::Item* item, RegionView* rv)
+{
+       /* we only care about enter events here, required for mouse/cursor
+        * tracking. there is a non-linear (non-child/non-parent) relationship
+        * between various components of a regionview and so when we leave one
+        * of them (e.g. a trim handle) and enter another (e.g. the waveview)
+        * no other items get notified. enter/leave handling does not propagate
+        * in the same way as other events, so we need to catch this because
+        * entering (and leaving) the waveview is equivalent to
+        * entering/leaving the regionview (which is why it is passed in as a
+        * third argument).
+        *
+        * And in fact, we really only care about enter events.
+        */
+
+       bool ret = false;
+
+       if (!rv->sensitive ()) {
+               return false;
+       }
+
+       switch (event->type) {
+       case GDK_ENTER_NOTIFY:
+               set_entered_regionview (rv);
+               ret = enter_handler (item, event, WaveItem);
+               break;
+
+       default:
+               break;
+       }
+
+       return ret;
+}
+
+
 bool
 Editor::canvas_stream_view_event (GdkEvent *event, ArdourCanvas::Item* item, RouteTimeAxisView *tv)
 {
@@ -324,11 +356,14 @@ Editor::canvas_stream_view_event (GdkEvent *event, ArdourCanvas::Item* item, Rou
 
        case GDK_ENTER_NOTIFY:
                set_entered_track (tv);
-               ret = true;
+               ret = enter_handler (item, event, StreamItem);
                break;
 
        case GDK_LEAVE_NOTIFY:
-               set_entered_track (0);
+               if (event->crossing.detail != GDK_NOTIFY_INFERIOR) {
+                       set_entered_track (0);
+               }
+               ret = leave_handler (item, event, StreamItem);
                break;
 
        default:
@@ -509,7 +544,6 @@ Editor::canvas_fade_in_handle_event (GdkEvent *event, ArdourCanvas::Item* item,
 
        case GDK_BUTTON_RELEASE:
                ret = button_release_handler (item, event, trim ? FadeInTrimHandleItem : FadeInHandleItem);
-               maybe_locate_with_edit_preroll ( rv->region()->position() );
                break;
 
        case GDK_MOTION_NOTIFY:
@@ -593,7 +627,6 @@ Editor::canvas_fade_out_handle_event (GdkEvent *event, ArdourCanvas::Item* item,
 
        case GDK_BUTTON_RELEASE:
                ret = button_release_handler (item, event, trim ? FadeOutTrimHandleItem : FadeOutHandleItem);
-               maybe_locate_with_edit_preroll ( rv->region()->last_frame() - rv->get_fade_out_shape_width() );
                break;
 
        case GDK_MOTION_NOTIFY:
@@ -616,9 +649,9 @@ Editor::canvas_fade_out_handle_event (GdkEvent *event, ArdourCanvas::Item* item,
 }
 
 struct DescendingRegionLayerSorter {
-    bool operator()(boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
-           return a->layer() > b->layer();
-    }
+       bool operator()(boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
+               return a->layer() > b->layer();
+       }
 };
 
 bool
@@ -651,13 +684,23 @@ bool
 Editor::canvas_line_event (GdkEvent *event, ArdourCanvas::Item* item, AutomationLine* al)
 {
        ItemType type;
-
-       if (dynamic_cast<AudioRegionGainLine*> (al) != 0) {
+       AudioRegionGainLine* gl;
+       if ((gl = dynamic_cast<AudioRegionGainLine*> (al)) != 0) {
                type = GainLineItem;
+               if (event->type == GDK_BUTTON_PRESS) {
+                       clicked_regionview = &gl->region_view ();
+               }
        } else {
                type = AutomationLineItem;
+               if (event->type == GDK_BUTTON_PRESS) {
+                       clicked_regionview = 0;
+               }
        }
 
+       clicked_control_point = 0;
+       clicked_axisview = &al->trackview;
+       clicked_routeview = dynamic_cast<RouteTimeAxisView*>(clicked_axisview);
+
        return typed_event (item, event, type);
 }
 
@@ -939,7 +982,7 @@ Editor::canvas_feature_line_event (GdkEvent *event, ArdourCanvas::Item* item, Re
 }
 
 bool
-Editor::canvas_marker_event (GdkEvent *event, ArdourCanvas::Item* item, Marker* /*marker*/)
+Editor::canvas_marker_event (GdkEvent *event, ArdourCanvas::Item* item, ArdourMarker* /*marker*/)
 {
        return typed_event (item, event, MarkerItem);
 }
@@ -975,11 +1018,17 @@ Editor::canvas_videotl_bar_event (GdkEvent *event, ArdourCanvas::Item* item)
 }
 
 bool
-Editor::canvas_tempo_marker_event (GdkEvent *event, ArdourCanvas::Item* item, TempoMarker* /*marker*/)
+Editor::canvas_tempo_marker_event (GdkEvent *event, ArdourCanvas::Item* item, TempoMarker* marker)
 {
        return typed_event (item, event, TempoMarkerItem);
 }
 
+bool
+Editor::canvas_tempo_curve_event (GdkEvent *event, ArdourCanvas::Item* item, TempoCurve* /*marker*/)
+{
+       return typed_event (item, event, TempoCurveItem);
+}
+
 bool
 Editor::canvas_meter_marker_event (GdkEvent *event, ArdourCanvas::Item* item, MeterMarker* /*marker*/)
 {
@@ -989,46 +1038,49 @@ Editor::canvas_meter_marker_event (GdkEvent *event, ArdourCanvas::Item* item, Me
 bool
 Editor::canvas_ruler_event (GdkEvent *event, ArdourCanvas::Item* item, ItemType type)
 {
-       framepos_t xdelta;
        bool handled = false;
 
        if (event->type == GDK_SCROLL) {
-               
+
                /* scroll events in the rulers are handled a little differently from
                   scrolling elsewhere in the canvas.
                */
 
                switch (event->scroll.direction) {
                case GDK_SCROLL_UP:
-                       temporal_zoom_step (false);
+                       if (Keyboard::modifier_state_equals(event->scroll.state,
+                                                           Keyboard::ScrollHorizontalModifier)) {
+                               scroll_left_step ();
+                       } else if (UIConfiguration::instance().get_use_mouse_position_as_zoom_focus_on_scroll()) {
+                               temporal_zoom_step_mouse_focus (false);
+                       } else {
+                               temporal_zoom_step (false);
+                       }
                        handled = true;
                        break;
-                       
+
                case GDK_SCROLL_DOWN:
-                       temporal_zoom_step (true);
+                       if (Keyboard::modifier_state_equals(event->scroll.state,
+                                                           Keyboard::ScrollHorizontalModifier)) {
+                               scroll_right_step ();
+                       } else if (UIConfiguration::instance().get_use_mouse_position_as_zoom_focus_on_scroll()) {
+                               temporal_zoom_step_mouse_focus (true);
+                       } else {
+                               temporal_zoom_step (true);
+                       }
                        handled = true;
                        break;
-                       
+
                case GDK_SCROLL_LEFT:
-                       xdelta = (current_page_samples() / 2);
-                       if (leftmost_frame > xdelta) {
-                               reset_x_origin (leftmost_frame - xdelta);
-                       } else {
-                               reset_x_origin (0);
-                       }
+                       scroll_left_half_page ();
                        handled = true;
                        break;
-                       
+
                case GDK_SCROLL_RIGHT:
-                       xdelta = (current_page_samples() / 2);
-                       if (max_framepos - xdelta > leftmost_frame) {
-                               reset_x_origin (leftmost_frame + xdelta);
-                       } else {
-                               reset_x_origin (max_framepos - current_page_samples());
-                       }
+                       scroll_right_half_page ();
                        handled = true;
                        break;
-                       
+
                default:
                        /* what? */
                        break;
@@ -1057,12 +1109,6 @@ Editor::canvas_playhead_cursor_event (GdkEvent *event, ArdourCanvas::Item* item)
        return typed_event (item, event, PlayheadCursorItem);
 }
 
-bool
-Editor::canvas_zoom_rect_event (GdkEvent *event, ArdourCanvas::Item* item)
-{
-       return typed_event (item, event, NoItem);
-}
-
 bool
 Editor::canvas_note_event (GdkEvent *event, ArdourCanvas::Item* item)
 {
@@ -1074,8 +1120,42 @@ Editor::canvas_note_event (GdkEvent *event, ArdourCanvas::Item* item)
 }
 
 bool
-Editor::canvas_drop_zone_event (GdkEvent* /*event*/)
+Editor::canvas_drop_zone_event (GdkEvent* event)
 {
+       GdkEventScroll scroll;
+       ArdourCanvas::Duple winpos;
+
+       switch (event->type) {
+       case GDK_BUTTON_RELEASE:
+               if (event->button.button == 1) {
+                       begin_reversible_selection_op (X_("Nowhere Click"));
+                       selection->clear_objects ();
+                       selection->clear_tracks ();
+                       commit_reversible_selection_op ();
+               }
+               break;
+
+       case GDK_SCROLL:
+               /* convert coordinates back into window space so that
+                  we can just call canvas_scroll_event().
+               */
+               winpos = _track_canvas->canvas_to_window (Duple (event->scroll.x, event->scroll.y));
+               scroll = event->scroll;
+               scroll.x = winpos.x;
+               scroll.y = winpos.y;
+               return canvas_scroll_event (&scroll, true);
+               break;
+
+       case GDK_ENTER_NOTIFY:
+               return typed_event (_canvas_drop_zone, event, DropZoneItem);
+
+       case GDK_LEAVE_NOTIFY:
+               return typed_event (_canvas_drop_zone, event, DropZoneItem);
+
+       default:
+               break;
+       }
+
        return true;
 }
 
@@ -1103,20 +1183,20 @@ Editor::track_canvas_drag_motion (Glib::RefPtr<Gdk::DragContext> const& context,
 
        (void) window_event_sample (&event, &px, &py);
 
-       std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (py);
+       std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (py, false);
        bool can_drop = false;
-       
+
        if (tv.first != 0) {
 
                /* over a time axis view of some kind */
 
                rtav = dynamic_cast<RouteTimeAxisView*> (tv.first);
-               
+
                if (rtav != 0 && rtav->is_track ()) {
                        /* over a track, not a bus */
                        can_drop = true;
                }
-                       
+
 
        } else {
                /* not over a time axis view, so drop is possible */
@@ -1124,20 +1204,35 @@ Editor::track_canvas_drag_motion (Glib::RefPtr<Gdk::DragContext> const& context,
        }
 
        if (can_drop) {
-               region = _regions->get_dragged_region ();
-               
+
+               if (target == X_("regions")) {
+                       region = _regions->get_dragged_region ();
+               }
+
                if (region) {
-                       
+
+                       if (tv.first == 0
+                           && (
+                               boost::dynamic_pointer_cast<AudioRegion> (region) != 0 ||
+                               boost::dynamic_pointer_cast<MidiRegion> (region) != 0
+                              )
+                          )
+                       {
+                               /* drop to drop-zone */
+                               context->drag_status (context->get_suggested_action(), time);
+                               return true;
+                       }
+
                        if ((boost::dynamic_pointer_cast<AudioRegion> (region) != 0 &&
                             dynamic_cast<AudioTimeAxisView*> (tv.first) != 0) ||
                            (boost::dynamic_pointer_cast<MidiRegion> (region) != 0 &&
                             dynamic_cast<MidiTimeAxisView*> (tv.first) != 0)) {
-                               
-                               /* audio to audio 
-                                  OR 
+
+                               /* audio to audio
+                                  OR
                                   midi to midi
                                */
-                               
+
                                context->drag_status (context->get_suggested_action(), time);
                                return true;
                        }
@@ -1147,7 +1242,7 @@ Editor::track_canvas_drag_motion (Glib::RefPtr<Gdk::DragContext> const& context,
                         * TODO: check if file is audio/midi, allow drops on same track-type only,
                         * currently: if audio is dropped on a midi-track, it is only added to the region-list
                         */
-                       if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
+                       if (UIConfiguration::instance().get_only_copy_imported_files()) {
                                context->drag_status(Gdk::ACTION_COPY, time);
                        } else {
                                if ((context->get_actions() & (Gdk::ACTION_COPY | Gdk::ACTION_LINK | Gdk::ACTION_MOVE)) == Gdk::ACTION_COPY) {
@@ -1167,13 +1262,11 @@ Editor::track_canvas_drag_motion (Glib::RefPtr<Gdk::DragContext> const& context,
 
 void
 Editor::drop_regions (const Glib::RefPtr<Gdk::DragContext>& /*context*/,
-                     int x, int y,
-                     const SelectionData& /*data*/,
-                     guint /*info*/, guint /*time*/)
+                      int x, int y,
+                      const SelectionData& /*data*/,
+                      guint /*info*/, guint /*time*/,
+                      bool from_region_list)
 {
-       boost::shared_ptr<Region> region;
-       boost::shared_ptr<Region> region_copy;
-       RouteTimeAxisView* rtav;
        GdkEvent event;
        double px;
        double py;
@@ -1183,39 +1276,58 @@ Editor::drop_regions (const Glib::RefPtr<Gdk::DragContext>& /*context*/,
        event.button.y = y;
        /* assume we're dragging with button 1 */
        event.motion.state = Gdk::BUTTON1_MASK;
+       samplepos_t const pos = window_event_sample (&event, &px, &py);
 
-       framepos_t const pos = window_event_sample (&event, &px, &py);
-
-       std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (py);
-
-       if (tv.first != 0) {
-
-               rtav = dynamic_cast<RouteTimeAxisView*> (tv.first);
-               
-               if (rtav != 0 && rtav->is_track ()) {
-
-                       boost::shared_ptr<Region> region = _regions->get_dragged_region ();
-                       
-                       if (region) {
-
-                               region_copy = RegionFactory::create (region, true);
-       
+       boost::shared_ptr<Region> region;
 
-                               if ((boost::dynamic_pointer_cast<AudioRegion> (region_copy) != 0 &&
-                                   dynamic_cast<AudioTimeAxisView*> (tv.first) != 0) ||
-                                   (boost::dynamic_pointer_cast<MidiRegion> (region_copy) != 0 &&
-                                    dynamic_cast<MidiTimeAxisView*> (tv.first) != 0)) {
+       if (from_region_list) {
+               region = _regions->get_dragged_region ();
+       } else {
+               region = _sources->get_dragged_region ();
+       }
 
-                                       /* audio to audio 
-                                          OR 
-                                          midi to midi
-                                       */
+       if (!region) { return; }
 
+       RouteTimeAxisView* rtav = 0;
+       std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (py, false);
 
-                                       _drags->set (new RegionInsertDrag (this, region_copy, rtav, pos), &event);
-                                       _drags->end_grab (0);
+       if (tv.first != 0) {
+               rtav = dynamic_cast<RouteTimeAxisView*> (tv.first);
+       } else {
+               try {
+                       if (boost::dynamic_pointer_cast<AudioRegion> (region)) {
+                               uint32_t output_chan = region->n_channels();
+                               if ((Config->get_output_auto_connect() & AutoConnectMaster) && session()->master_out()) {
+                                       output_chan =  session()->master_out()->n_inputs().n_audio();
                                }
+                               list<boost::shared_ptr<AudioTrack> > audio_tracks;
+                               audio_tracks = session()->new_audio_track (region->n_channels(), output_chan, 0, 1, region->name(), PresentationInfo::max_order);
+                               rtav = dynamic_cast<RouteTimeAxisView*> (time_axis_view_from_stripable (audio_tracks.front()));
+                       } else if (boost::dynamic_pointer_cast<MidiRegion> (region)) {
+                               ChanCount one_midi_port (DataType::MIDI, 1);
+                               list<boost::shared_ptr<MidiTrack> > midi_tracks;
+                               midi_tracks = session()->new_midi_track (one_midi_port, one_midi_port,
+                                                                        Config->get_strict_io () || Profile->get_mixbus (),
+                                                                        boost::shared_ptr<ARDOUR::PluginInfo>(),
+                                                                        (ARDOUR::Plugin::PresetRecord*) 0,
+                                                                        (ARDOUR::RouteGroup*) 0, 1, region->name(), PresentationInfo::max_order);
+                               rtav = dynamic_cast<RouteTimeAxisView*> (time_axis_view_from_stripable (midi_tracks.front()));
+                       } else {
+                               return;
                        }
+               } catch (...) {
+                       error << _("Could not create new track after region placed in the drop zone") << endmsg;
+                       return;
+               }
+       }
+
+       if (rtav != 0 && rtav->is_track ()) {
+               boost::shared_ptr<Region> region_copy = RegionFactory::create (region, true);
+
+               if ((boost::dynamic_pointer_cast<AudioRegion> (region_copy) != 0 && dynamic_cast<AudioTimeAxisView*> (rtav) != 0) ||
+                   (boost::dynamic_pointer_cast<MidiRegion> (region_copy) != 0 && dynamic_cast<MidiTimeAxisView*> (rtav) != 0)) {
+                       _drags->set (new RegionInsertDrag (this, region_copy, rtav, pos), &event);
+                       _drags->end_grab (&event);
                }
        }
 }
@@ -1261,4 +1373,3 @@ Editor::key_release_handler (ArdourCanvas::Item* item, GdkEvent* event, ItemType
 
        return handled;
 }
-