Fix scrollbars when using zoom in the timeline.
[dcpomatic.git] / src / wx / timeline.cc
index 80f4133c03fdc9df6f567dc02c4eb1b73b29c1df..5cf84dfd51196b9435f6c98711c8631d5fea3c3e 100644 (file)
@@ -35,6 +35,7 @@
 #include "lib/film.h"
 #include "lib/image_content.h"
 #include "lib/playlist.h"
+#include "lib/scope_guard.h"
 #include "lib/text_content.h"
 #include "lib/timer.h"
 #include "lib/video_content.h"
@@ -150,18 +151,23 @@ Timeline::paint_labels ()
 {
        wxPaintDC dc (_labels_canvas);
 
+       auto film = _film.lock();
+       if (film->content().empty()) {
+               return;
+       }
+
        auto gc = wxGraphicsContext::Create (dc);
        if (!gc) {
                return;
        }
 
+       ScopeGuard sg = [gc]() { delete gc; };
+
        int vsx, vsy;
        _labels_canvas->GetViewStart (&vsx, &vsy);
        gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
 
        _labels_view->paint (gc, {});
-
-       delete gc;
 }
 
 
@@ -169,6 +175,12 @@ void
 Timeline::paint_main ()
 {
        wxPaintDC dc (_main_canvas);
+
+       auto film = _film.lock();
+       if (film->content().empty()) {
+               return;
+       }
+
        _main_canvas->DoPrepareDC (dc);
 
        auto gc = wxGraphicsContext::Create (dc);
@@ -176,9 +188,7 @@ Timeline::paint_main ()
                return;
        }
 
-       int vsx, vsy;
-       _main_canvas->GetViewStart (&vsx, &vsy);
-       gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
+       ScopeGuard sg = [gc]() { delete gc; };
 
        gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
 
@@ -205,8 +215,6 @@ Timeline::paint_main ()
        }
 
        if (_zoom_point) {
-               /* Translate back as _down_point and _zoom_point do not take scroll into account */
-               gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
                gc->SetPen(gui_is_dark() ? *wxWHITE_PEN : *wxBLACK_PEN);
                gc->SetBrush (*wxTRANSPARENT_BRUSH);
                gc->DrawRectangle (
@@ -225,8 +233,6 @@ Timeline::paint_main ()
        path.MoveToPoint (ph, 0);
        path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
        gc->StrokePath (path);
-
-       delete gc;
 }
 
 
@@ -318,7 +324,7 @@ place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
                int t = base;
 
                auto content = cv->content();
-               DCPTimePeriod const content_period (content->position(), content->end(film));
+               DCPTimePeriod const content_period = content->period(film);
 
                while (true) {
                        auto j = views.begin();
@@ -332,7 +338,8 @@ place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
                                auto test_content = test->content();
                                if (
                                        test->track() && test->track().get() == t &&
-                                       content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end(film)))) {
+                                       content_period.overlap(test_content->period(film))
+                                  ) {
                                        /* we have an overlap on track `t' */
                                        ++t;
                                        break;
@@ -520,6 +527,12 @@ Timeline::left_down_select (wxMouseEvent& ev)
                _down_view_position = content_view->content()->position ();
        }
 
+       if (dynamic_pointer_cast<TimelineTimeAxisView>(view)) {
+               int vsx, vsy;
+               _main_canvas->GetViewStart(&vsx, &vsy);
+               _viewer.seek(DCPTime::from_seconds((ev.GetPosition().x + vsx * _x_scroll_rate) / _pixels_per_second.get_value_or(1)), true);
+       }
+
        for (auto i: _views) {
                auto cv = dynamic_pointer_cast<TimelineContentView>(i);
                if (!cv) {
@@ -689,6 +702,7 @@ Timeline::mouse_moved_zoom (wxMouseEvent& ev)
        }
 
        _zoom_point = ev.GetPosition ();
+       setup_scrollbars();
        Refresh ();
 }
 
@@ -943,3 +957,26 @@ Timeline::zoom_all ()
        _labels_canvas->Scroll (0, 0);
        Refresh ();
 }
+
+
+void
+Timeline::keypress(wxKeyEvent const& event)
+{
+       if (event.GetKeyCode() == WXK_DELETE) {
+               auto film = _film.lock();
+               DCPOMATIC_ASSERT(film);
+               film->remove_content(selected_content());
+       } else {
+               switch (event.GetRawKeyCode()) {
+               case '+':
+                       set_pixels_per_second(_pixels_per_second.get_value_or(1) * 2);
+                       setup_scrollbars();
+                       break;
+               case '-':
+                       set_pixels_per_second(_pixels_per_second.get_value_or(1) / 2);
+                       setup_scrollbars();
+                       break;
+               }
+       }
+}
+