grid/snap minimum selection length
[ardour.git] / gtk2_ardour / editor_canvas.cc
index 9cd23e0112244108e5799ff414f7457fda34b135..3e7aad2a1bbffd9db281398e2f38268f8086103b 100644 (file)
@@ -24,7 +24,8 @@
 #include <libgnomecanvasmm/init.h>
 #include <libgnomecanvasmm/pixbuf.h>
 #include <jack/types.h>
-#include <gtkmm2ext/utils.h>
+
+#include "gtkmm2ext/utils.h"
 
 #include "ardour/profile.h"
 #include "ardour/rc_configuration.h"
@@ -49,7 +50,6 @@
 #include "editor_drag.h"
 #include "region_view.h"
 #include "editor_group_tabs.h"
-#include "editor_routes.h"
 #include "editor_summary.h"
 #include "keyboard.h"
 #include "editor_cursors.h"
@@ -256,8 +256,8 @@ Editor::initialize_canvas ()
 
        track_canvas->set_name ("EditorMainCanvas");
        track_canvas->add_events (Gdk::POINTER_MOTION_HINT_MASK | Gdk::SCROLL_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
-       track_canvas->signal_leave_notify_event().connect (sigc::mem_fun(*this, &Editor::left_track_canvas));
-       track_canvas->signal_enter_notify_event().connect (sigc::mem_fun(*this, &Editor::entered_track_canvas));
+       track_canvas->signal_leave_notify_event().connect (sigc::mem_fun(*this, &Editor::left_track_canvas), false);
+       track_canvas->signal_enter_notify_event().connect (sigc::mem_fun(*this, &Editor::entered_track_canvas), false);
        track_canvas->set_flags (CAN_FOCUS);
 
        /* set up drag-n-drop */
@@ -480,31 +480,79 @@ Editor::drop_regions (const RefPtr<Gdk::DragContext>& /*context*/,
        _drags->end_grab (0);
 }
 
+/** If the editor window is arranged such that the edge of the trackview is right up
+ *  against the edge of the screen, autoscroll will not work very well.  In this situation,
+ *  we start autoscrolling some distance in from the right-hand-side of the screen edge;
+ *  this is the distance at which that happens.
+ */
+int
+Editor::autoscroll_fudge_threshold () const
+{
+       return current_page_frames() / 6;
+}
+
+/** @param allow_horiz true to allow horizontal autoscroll, otherwise false.
+ *  @param allow_vert true to allow vertical autoscroll, otherwise false.
+ *  @param moving_left true if we are moving left, so we only want to autoscroll on the left of the canvas,
+ *  otherwise false, so we only want to autoscroll on the right of the canvas.
+ *  @param moving_up true if we are moving up, so we only want to autoscroll at the top of the canvas,
+ *  otherwise false, so we only want to autoscroll at the bottom of the canvas.
+ */
 void
-Editor::maybe_autoscroll (bool allow_horiz, bool allow_vert)
+Editor::maybe_autoscroll (bool allow_horiz, bool allow_vert, bool moving_left, bool moving_up)
 {
-       framepos_t rightmost_frame = leftmost_frame + current_page_frames();
+       if (!Config->get_autoscroll_editor ()) {
+               return;
+       }
+       
        bool startit = false;
 
+       /* Work out the distance between the right hand edge of the trackview and the edge of
+          the monitor that it is on.
+       */
+
+       Glib::RefPtr<Gdk::Window> gdk_window = get_window ();
+       Gdk::Rectangle window_rect;
+       gdk_window->get_frame_extents (window_rect);
+       
+       Glib::RefPtr<Gdk::Screen> screen = get_screen ();
+       Gdk::Rectangle root_rect;
+       screen->get_root_window()->get_frame_extents (root_rect);
+
+       Gtk::Allocation editor_list = _the_notebook.get_allocation ();
+
+       framecnt_t distance = pixel_to_frame (root_rect.get_x() + root_rect.get_width() - window_rect.get_x() - window_rect.get_width());
+       if (_the_notebook.is_visible ()) {
+               distance += pixel_to_frame (editor_list.get_width());
+       }
+
+       /* Note whether we're fudging the autoscroll (see autoscroll_fudge_threshold) */
+       _autoscroll_fudging = (distance < autoscroll_fudge_threshold ());
+
        double const ty = _drags->current_pointer_y() - get_trackview_group_vertical_offset ();
 
        autoscroll_y = 0;
        autoscroll_x = 0;
-       if (ty < canvas_timebars_vsize && allow_vert) {
+       if (ty < canvas_timebars_vsize && moving_up && allow_vert) {
                autoscroll_y = -1;
                startit = true;
-       } else if (ty > _canvas_height && allow_vert) {
+       } else if (ty > _canvas_height && !moving_up && allow_vert) {
                autoscroll_y = 1;
                startit = true;
        }
 
+       framepos_t rightmost_frame = leftmost_frame + current_page_frames();
+       if (_autoscroll_fudging) {
+               rightmost_frame -= autoscroll_fudge_threshold ();
+       }
+
        if (_drags->current_pointer_frame() > rightmost_frame && allow_horiz) {
-               if (rightmost_frame < max_framepos) {
+               if (rightmost_frame < max_framepos && !moving_left) {
                        autoscroll_x = 1;
                        startit = true;
                }
        } else if (_drags->current_pointer_frame() < leftmost_frame && allow_horiz) {
-               if (leftmost_frame > 0) {
+               if (leftmost_frame > 0 && moving_left) {
                        autoscroll_x = -1;
                        startit = true;
                }
@@ -536,11 +584,14 @@ Editor::autoscroll_canvas ()
        GdkEventMotion ev;
        double new_pixel;
        double target_pixel;
-
+       
        if (autoscroll_x_distance != 0) {
 
                if (autoscroll_x > 0) {
                        autoscroll_x_distance = (_drags->current_pointer_frame() - (leftmost_frame + current_page_frames())) / 3;
+                       if (_autoscroll_fudging) {
+                               autoscroll_x_distance += autoscroll_fudge_threshold () / 3;
+                       }
                } else if (autoscroll_x < 0) {
                        autoscroll_x_distance = (leftmost_frame - _drags->current_pointer_frame()) / 3;
 
@@ -684,6 +735,8 @@ bool
 Editor::left_track_canvas (GdkEventCrossing */*ev*/)
 {
        DropDownKeys ();
+       within_track_canvas = false;
+       //cerr << "left track canvas\n";
        set_entered_track (0);
        set_entered_regionview (0);
        reset_canvas_action_sensitivity (false);
@@ -693,6 +746,8 @@ Editor::left_track_canvas (GdkEventCrossing */*ev*/)
 bool
 Editor::entered_track_canvas (GdkEventCrossing */*ev*/)
 {
+       //cerr << "entered track canvas\n";
+       within_track_canvas = true;
        reset_canvas_action_sensitivity (true);
        return FALSE;
 }
@@ -704,7 +759,7 @@ Editor::ensure_time_axis_view_is_visible (const TimeAxisView& tav)
 
        double v = vertical_adjustment.get_value ();
 
-       if (begin < v || begin > v + _canvas_height) {
+       if (begin < v || begin + tav.current_height() > v + _canvas_height - canvas_timebars_vsize) {
                /* try to put the TimeAxisView roughly central */
                if (begin >= _canvas_height/2.0) {
                        begin -= _canvas_height/2.0;
@@ -877,6 +932,7 @@ Editor::horizontal_position () const
 {
        return frame_to_unit (leftmost_frame);
 }
+
 void
 Editor::set_canvas_cursor (Gdk::Cursor* cursor, bool save)
 {
@@ -884,13 +940,15 @@ Editor::set_canvas_cursor (Gdk::Cursor* cursor, bool save)
                current_canvas_cursor = cursor;
        }
 
-       if (is_drawable()) {
+       Glib::RefPtr<Gdk::Window> win = track_canvas->get_window();
+
+       if (win) {
                track_canvas->get_window()->set_cursor (*cursor);
        }
 }
 
 bool
-Editor::track_canvas_key_press (GdkEventKey* event)
+Editor::track_canvas_key_press (GdkEventKey*)
 {
        /* XXX: event does not report the modifier key pressed down, AFAICS, so use the Keyboard object instead */
        if (mouse_mode == Editing::MouseZoom && Keyboard::the_keyboard().key_is_down (GDK_Control_L)) {
@@ -901,7 +959,7 @@ Editor::track_canvas_key_press (GdkEventKey* event)
 }
 
 bool
-Editor::track_canvas_key_release (GdkEventKey* event)
+Editor::track_canvas_key_release (GdkEventKey*)
 {
        if (mouse_mode == Editing::MouseZoom && !Keyboard::the_keyboard().key_is_down (GDK_Control_L)) {
                set_canvas_cursor (_cursors->zoom_in, true);