Scroll labels view with zoom.
[dcpomatic.git] / src / wx / timeline.cc
index 2e4de2274ab2e2658012b4d9dde45c0708e90d4d..f68c98bda3241122da0522a1c1b5cec7b0d9f742 100644 (file)
@@ -55,7 +55,7 @@ using boost::optional;
 
 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
        : wxPanel (parent, wxID_ANY)
-       , _labels_panel (new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
+       , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
        , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
        , _content_panel (cp)
        , _film (film)
@@ -71,24 +71,26 @@ Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
        , _tool (SELECT)
        , _x_scroll_rate (16)
        , _y_scroll_rate (16)
+       , _track_height (48)
 {
 #ifndef __WXOSX__
-       _labels_panel->SetDoubleBuffered (true);
+       _labels_canvas->SetDoubleBuffered (true);
        _main_canvas->SetDoubleBuffered (true);
 #endif
 
        wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
-       sizer->Add (_labels_panel, 0, wxEXPAND);
+       sizer->Add (_labels_canvas, 0, wxEXPAND);
+       _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
        sizer->Add (_main_canvas, 1, wxEXPAND);
        SetSizer (sizer);
 
-       _labels_panel->Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint_labels, this));
-       _main_canvas->Bind  (wxEVT_PAINT,      boost::bind (&Timeline::paint_main,   this));
-       _main_canvas->Bind  (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,    this, _1));
-       _main_canvas->Bind  (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,      this, _1));
-       _main_canvas->Bind  (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,   this, _1));
-       _main_canvas->Bind  (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved,  this, _1));
-       _main_canvas->Bind  (wxEVT_SIZE,       boost::bind (&Timeline::resized,      this));
+       _labels_canvas->Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint_labels, this));
+       _main_canvas->Bind   (wxEVT_PAINT,      boost::bind (&Timeline::paint_main,   this));
+       _main_canvas->Bind   (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,    this, _1));
+       _main_canvas->Bind   (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,      this, _1));
+       _main_canvas->Bind   (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,   this, _1));
+       _main_canvas->Bind   (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved,  this, _1));
+       _main_canvas->Bind   (wxEVT_SIZE,       boost::bind (&Timeline::resized,      this));
 
        film_changed (Film::CONTENT);
 
@@ -100,19 +102,23 @@ Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
        _pixels_per_second = max (0.01, static_cast<double>(640) / film->length().seconds ());
 
        setup_scrollbars ();
-       _main_canvas->EnableScrolling (true, true);
+       _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
 }
 
 void
 Timeline::paint_labels ()
 {
-       wxPaintDC dc (this);
+       wxPaintDC dc (_labels_canvas);
 
        wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
        if (!gc) {
                return;
        }
 
+       int vsx, vsy;
+       _labels_canvas->GetViewStart (&vsx, &vsy);
+       gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
+
        _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
 
        delete gc;
@@ -121,7 +127,7 @@ Timeline::paint_labels ()
 void
 Timeline::paint_main ()
 {
-       wxPaintDC dc (this);
+       wxPaintDC dc (_main_canvas);
        _main_canvas->DoPrepareDC (dc);
 
        wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
@@ -376,6 +382,8 @@ Timeline::setup_scrollbars ()
        if (!film || !_pixels_per_second) {
                return;
        }
+       _labels_canvas->SetVirtualSize (_labels_view->bbox().width, tracks() * track_height() + 96);
+       _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
        _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * track_height() + 96);
        _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
 }
@@ -512,11 +520,17 @@ Timeline::left_up_zoom (wxMouseEvent& ev)
        wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
        wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
 
-       DCPTime time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
-       DCPTime time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
+       DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
+       DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
        _pixels_per_second = GetSize().GetWidth() / (time_right.seconds() - time_left.seconds());
+
+       double const tracks_top = double(top_left.y) / _track_height;
+       double const tracks_bottom = double(bottom_right.y) / _track_height;
+       _track_height = GetSize().GetHeight() / (tracks_bottom - tracks_top);
+
        setup_scrollbars ();
-       _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, wxDefaultCoord);
+       _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, tracks_top * _track_height / _y_scroll_rate);
+       _labels_canvas->Scroll (0, tracks_top * _track_height / _y_scroll_rate);
 
        _zoom_point = optional<wxPoint> ();
        Refresh ();
@@ -566,6 +580,7 @@ Timeline::right_down (wxMouseEvent& ev)
        case ZOOM:
                /* Zoom out */
                _pixels_per_second = *_pixels_per_second / 2;
+               _track_height = max (8, _track_height / 2);
                setup_scrollbars ();
                break;
        }