a-fluidsynth: implement LV2_BANKPATCH__notify
[ardour.git] / gtk2_ardour / editor_summary.cc
index 47f856cbbd0a0f07c65c5332f3a2cf7e2885cd74..95b54cdf02a3b0981176143dfafb9e8e57335d77 100644 (file)
@@ -21,6 +21,9 @@
 
 #include "canvas/debug.h"
 
+#include <gtkmm/menu.h>
+#include <gtkmm/menuitem.h>
+
 #include "time_axis_view.h"
 #include "streamview.h"
 #include "editor_summary.h"
@@ -35,6 +38,8 @@
 #include "route_time_axis.h"
 #include "ui_config.h"
 
+#include "pbd/i18n.h"
+
 using namespace std;
 using namespace ARDOUR;
 using Gtkmm2ext::Keyboard;
@@ -46,17 +51,13 @@ EditorSummary::EditorSummary (Editor* e)
        : EditorComponent (e),
          _start (0),
          _end (1),
-         _overhang_fraction (0.02),
          _x_scale (1),
          _track_height (16),
          _last_playhead (-1),
-         _begin_dragging (false),
          _move_dragging (false),
-         _moved (false),
          _view_rectangle_x (0, 0),
          _view_rectangle_y (0, 0),
          _zoom_trim_dragging (false),
-         _zoom_dragging (false),
          _old_follow_playhead (false),
          _image (0),
          _background_dirty (true)
@@ -116,9 +117,9 @@ EditorSummary::set_session (Session* s)
                _session->EndTimeChanged.connect (_session_connections, invalidator (*this), boost::bind (&EditorSummary::set_background_dirty, this), gui_context());
                _editor->selection->RegionsChanged.connect (sigc::mem_fun(*this, &EditorSummary::set_background_dirty));
        }
-       
-       _leftmost = _session->current_start_frame();
-       _rightmost = min (_session->nominal_frame_rate()*60*2, _session->current_start_frame() );  //always show at least 2 minutes
+
+       _leftmost = max_framepos;
+       _rightmost = 0;
 }
 
 void
@@ -137,9 +138,9 @@ EditorSummary::render_background_image ()
 
        /* compute start and end points for the summary */
 
-       framecnt_t const session_length = _session->current_end_frame() - _session->current_start_frame ();
-       double theoretical_start = _session->current_start_frame() - session_length * _overhang_fraction;
-       double theoretical_end = _session->current_end_frame();
+       std::pair<framepos_t, framepos_t> ext = _editor->session_gui_extents();
+       double theoretical_start = ext.first;
+       double theoretical_end = ext.second;
 
        /* the summary should encompass the full extent of everywhere we've visited since the session was opened */
        if ( _leftmost < theoretical_start)
@@ -149,7 +150,7 @@ EditorSummary::render_background_image ()
 
        /* range-check */
        _start = theoretical_start > 0 ? theoretical_start : 0;
-       _end = theoretical_end + session_length * _overhang_fraction;
+       _end = theoretical_end < max_framepos ? theoretical_end : max_framepos;
 
        /* calculate x scale */
        if (_end != _start) {
@@ -183,12 +184,14 @@ EditorSummary::render_background_image ()
 
                /* paint a non-bg colored strip to represent the track itself */
 
-               cairo_set_source_rgb (cr, 0.2, 0.2, 0.2);
-               cairo_set_line_width (cr, _track_height - 1);
-               cairo_move_to (cr, 0, y + _track_height / 2);
-               cairo_line_to (cr, get_width(), y + _track_height / 2);
-               cairo_stroke (cr);
-
+               if ( _track_height > 4 ) {
+                       cairo_set_source_rgb (cr, 0.2, 0.2, 0.2);
+                       cairo_set_line_width (cr, _track_height - 1);
+                       cairo_move_to (cr, 0, y + _track_height / 2);
+                       cairo_line_to (cr, get_width(), y + _track_height / 2);
+                       cairo_stroke (cr);
+               }
+               
                StreamView* s = (*i)->view ();
 
                if (s) {
@@ -270,7 +273,6 @@ EditorSummary::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle
 
        int32_t width = _view_rectangle_x.second - _view_rectangle_x.first;
        std::min(8, width);
-       int32_t height = _view_rectangle_y.second - _view_rectangle_y.first;
        cairo_rectangle (cr, _view_rectangle_x.first, 0, width, get_height ());
        cairo_set_source_rgba (cr, 1, 1, 1, 0.15);
        cairo_fill (cr);
@@ -342,9 +344,9 @@ EditorSummary::set_overlays_dirty ()
 
 /** Set the summary so that just the overlays (viewbox, playhead etc.) in a given area will be re-rendered */
 void
-EditorSummary::set_overlays_dirty (int x, int y, int w, int h)
+EditorSummary::set_overlays_dirty_rect (int x, int y, int w, int h)
 {
-       ENSURE_GUI_THREAD (*this, &EditorSummary::set_overlays_dirty);
+       ENSURE_GUI_THREAD (*this, &EditorSummary::set_overlays_dirty_rect);
        queue_draw_area (x, y, w, h);
 }
 
@@ -427,6 +429,8 @@ EditorSummary::on_key_release_event (GdkEventKey* key)
        return false;
 }
 
+#include "gtkmm2ext/utils.h"
+
 /** Handle a button press.
  *  @param ev GTK event.
  */
@@ -435,6 +439,16 @@ EditorSummary::on_button_press_event (GdkEventButton* ev)
 {
        _old_follow_playhead = _editor->follow_playhead ();
 
+       if (ev->button == 3) {  //right-click:  show the reset menu action
+               using namespace Gtk::Menu_Helpers;
+               Gtk::Menu* m = manage (new Gtk::Menu);
+               MenuList& items = m->items ();
+               items.push_back(MenuElem(_("Reset Summary to Extents"),
+                       sigc::mem_fun(*this, &EditorSummary::reset_to_extents)));
+               m->popup (ev->button, ev->time);
+               return true;
+       }
+
        if (ev->button != 1) {
                return true;
        }
@@ -474,9 +488,22 @@ EditorSummary::on_button_press_event (GdkEventButton* ev)
 
        } else {
 
-               /* start a move or zoom drag */
-               /* won't know which one until the mouse moves */
-               _begin_dragging = true;
+               /* start a move+zoom drag */
+               get_editor (&_pending_editor_x, &_pending_editor_y);
+               _pending_editor_changed = false;
+               _editor->_dragging_playhead = true;
+               _editor->set_follow_playhead (false);
+
+               _move_dragging = true;
+               
+               _last_mx = ev->x;
+               _last_my = ev->y;
+               _last_dx = 0;
+               _last_dy = 0;
+               _last_y_delta = 0;
+
+               get_window()->set_cursor (*_editor->_cursors->expand_left_right);
+       
        }
 
        return true;
@@ -488,7 +515,7 @@ EditorSummary::on_button_press_event (GdkEventButton* ev)
 bool
 EditorSummary::suspending_editor_updates () const
 {
-       return (!UIConfiguration::instance().get_update_editor_during_summary_drag () && (_zoom_dragging || _zoom_trim_dragging || _move_dragging));
+       return (!UIConfiguration::instance().get_update_editor_during_summary_drag () && (_zoom_trim_dragging || _move_dragging));
 }
 
 /** Fill in x and y with the editor's current viewable area in summary coordinates */
@@ -546,6 +573,18 @@ EditorSummary::get_position (double x, double y) const
        }
 }
 
+void
+EditorSummary::reset_to_extents()
+{
+       //reset as if the user never went anywhere outside the extents
+       _leftmost = max_framepos;
+       _rightmost = 0;
+
+       _editor->temporal_zoom_extents ();
+       set_background_dirty ();
+}
+
+
 void
 EditorSummary::set_cursor (Position p)
 {
@@ -575,14 +614,16 @@ EditorSummary::summary_zoom_step ( int steps /* positive steps to zoom "out" , n
        pair<double, double> xn;
 
        get_editor (&xn);
-//     {
-//             xn.first = (_editor->leftmost_sample () - _start) * _x_scale;
-//             xn.second = xn.first + _editor->current_page_samples() * _x_scale;
-//     }
 
        xn.first -= steps;
        xn.second += steps;
 
+       //for now, disallow really close zooming-in from the scroomer. ( currently it causes the start-offset to 'walk' because of integer limitations.  to fix this, probably need to maintain float throught the get/set_editor() path )
+       if (steps<0) {
+      if ( (xn.second-xn.first) < 2)
+               return;
+       }
+
        set_overlays_dirty ();
        set_editor_x (xn);
 }
@@ -591,41 +632,76 @@ EditorSummary::summary_zoom_step ( int steps /* positive steps to zoom "out" , n
 bool
 EditorSummary::on_motion_notify_event (GdkEventMotion* ev)
 {
-       pair<double, double> xr = _start_editor_x;
-       double x = _start_editor_x.first;
-
        if (_move_dragging) {
 
-               _moved = true;
-
-               assert (_start_position == INSIDE || _start_position == TO_LEFT_OR_RIGHT);
-               x += ev->x - _start_mouse_x;
-
-               if (x < 0) {
-                       x = 0;
+               //To avoid accidental zooming, the mouse must move exactly vertical, not diagonal, to trigger a zoom step
+               //we use screen coordinates for this, not canvas-based grab_x
+               double mx = ev->x;
+               double dx = mx - _last_mx;
+               double my = ev->y;
+               double dy = my - _last_my;
+
+               //do zooming in windowed "steps" so it feels more reversible ?
+               const int stepsize = 2;
+               int y_delta = _start_mouse_y - my;
+               y_delta = y_delta / stepsize;
+
+               //do the zoom?
+               const float zscale = 3;
+               if ( (dx==0) && (_last_dx ==0) && (y_delta != _last_y_delta) ) {
+
+                       summary_zoom_step( dy * zscale );
+
+                       //after the zoom we must re-calculate x-pos grabs
+                       pair<double, double> xr;
+                       get_editor (&xr);
+                       _start_editor_x = xr;
+                       _start_mouse_x = ev->x;
+                       
+                       _last_y_delta = y_delta;
                }
+               
+               //always track horizontal movement, if any
+               if ( dx != 0 ) {
 
-               set_editor (x);
-
-       } else if (_zoom_dragging) {
+                       double x = _start_editor_x.first;
+                       x += ev->x - _start_mouse_x;
+                       
+                       if (x < 0) {
+                               x = 0;
+                       }
 
-               //ToDo: refactor into summary_zoom_in/out(
-               //ToDo:  protect the case where the editor position is small, and results in offsetting the position
+                       //zoom-behavior-tweaks
+                       //protect the right edge from expanding beyond the end
+                       pair<double, double> xr;
+                       get_editor (&xr);
+                       double w = xr.second - xr.first;
+                       if ( x + w < get_width() ) {
+                               set_editor (x);
+                       }
+               }
 
-               double const dy = ev->y - _zoom_last_y;
-               
-               summary_zoom_step( dy );
+               _last_my = my;
+               _last_mx = mx;
+               _last_dx = dx;
+               _last_dy = dy;
 
-               _zoom_last_y = ev->y;
-                       
        } else if (_zoom_trim_dragging) {
 
+               pair<double, double> xr = _start_editor_x;
+
                double const dx = ev->x - _start_mouse_x;
 
                if (_zoom_trim_position == LEFT) {
                        xr.first += dx;
                } else if (_zoom_trim_position == RIGHT) {
-                       xr.second += dx;
+
+                       //zoom-behavior-tweaks
+                       //protect the right edge from expanding beyond the edge
+                       if ( (xr.second + dx) < get_width() ) {
+                               xr.second += dx;
+                       }
+
                } else {
                        assert (0);
                        xr.first = -1; /* do not change */
@@ -635,47 +711,6 @@ EditorSummary::on_motion_notify_event (GdkEventMotion* ev)
                set_cursor (_zoom_trim_position);
                set_editor (xr);
 
-       } else if (_begin_dragging) {
-
-               double const dx = ev->x - _start_mouse_x;
-               double const dy = ev->y - _start_mouse_y;
-
-               if ( fabs(dx) > fabs(dy) ) {
-                       
-                       /* initiate a move drag */
-
-                       /* get the editor's state in case we are suspending updates */
-                       get_editor (&_pending_editor_x, &_pending_editor_y);
-                       _pending_editor_changed = false;
-
-                       _move_dragging = true;
-                       _moved = false;
-                       _editor->_dragging_playhead = true;
-                       _editor->set_follow_playhead (false);
-
-                       get_window()->set_cursor (*_editor->_cursors->expand_left_right);
-
-                       _begin_dragging = false;
-               
-               } else if ( fabs(dy) > fabs(dx) ) {
-               
-                       /* initiate a zoom drag */
-
-                       /* get the editor's state in case we are suspending updates */
-                       get_editor (&_pending_editor_x, &_pending_editor_y);
-                       _pending_editor_changed = false;
-
-                       //_zoom_position = get_position (ev->x, ev->y);
-                       _zoom_dragging = true;
-                       _zoom_last_y = ev->y;
-                       _editor->_dragging_playhead = true;
-                       _editor->set_follow_playhead (false);
-
-                       get_window()->set_cursor (*_editor->_cursors->expand_up_down);
-
-                       _begin_dragging = false;
-               }
-               
        } else {
                set_cursor ( get_position(ev->x, ev->y) );
        }
@@ -688,10 +723,8 @@ EditorSummary::on_button_release_event (GdkEventButton*)
 {
        bool const was_suspended = suspending_editor_updates ();
 
-       _begin_dragging = false;
        _move_dragging = false;
        _zoom_trim_dragging = false;
-       _zoom_dragging = false;
        _editor->_dragging_playhead = false;
        _editor->set_follow_playhead (_old_follow_playhead, false);
 
@@ -864,7 +897,7 @@ EditorSummary::playhead_position_changed (framepos_t p)
        if (_session && o != n) {
                int a = max(2, min (o, n));
                int b = max (o, n);
-               set_overlays_dirty (a - 2, 0, b + 2, get_height ());
+               set_overlays_dirty_rect (a - 2, 0, b + 2, get_height ());
        }
 }