Use more ScopeGuards.
[dcpomatic.git] / src / wx / controls.cc
index 29f4aeaa364e2416de48dda239f25e2ebbd8ee27..804b59daec390173e28931f9e65fb6c2f8397f8e 100644 (file)
 */
 
 
+#include "check_box.h"
+#include "content_view.h"
 #include "controls.h"
+#include "dcpomatic_button.h"
 #include "film_viewer.h"
-#include "wx_util.h"
-#include "playhead_to_timecode_dialog.h"
+#include "markers_panel.h"
 #include "playhead_to_frame_dialog.h"
-#include "content_view.h"
+#include "playhead_to_timecode_dialog.h"
 #include "static_text.h"
-#include "check_box.h"
-#include "dcpomatic_button.h"
-#include "lib/job_manager.h"
-#include "lib/player_video.h"
-#include "lib/dcp_content.h"
-#include "lib/job.h"
-#include "lib/examine_content_job.h"
+#include "wx_util.h"
 #include "lib/content_factory.h"
 #include "lib/cross.h"
-#include <dcp/dcp.h>
+#include "lib/dcp_content.h"
+#include "lib/examine_content_job.h"
+#include "lib/job.h"
+#include "lib/job_manager.h"
+#include "lib/player_video.h"
+#include "lib/scope_guard.h"
 #include <dcp/cpl.h>
+#include <dcp/dcp.h>
 #include <dcp/reel.h>
 #include <dcp/reel_picture_asset.h>
-#include <wx/wx.h>
-#include <wx/tglbtn.h>
+#include <dcp/warnings.h>
+LIBDCP_DISABLE_WARNINGS
 #include <wx/listctrl.h>
 #include <wx/progdlg.h>
+#include <wx/tglbtn.h>
+#include <wx/wx.h>
+LIBDCP_ENABLE_WARNINGS
 
 
-using std::string;
-using std::list;
 using std::cout;
-using std::make_pair;
+using std::dynamic_pointer_cast;
 using std::exception;
-using boost::optional;
+using std::list;
+using std::make_pair;
 using std::shared_ptr;
+using std::string;
 using std::weak_ptr;
-using std::dynamic_pointer_cast;
+using boost::optional;
 #if BOOST_VERSION >= 106100
 using namespace boost::placeholders;
 #endif
 using namespace dcpomatic;
 
 
-Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
+Controls::Controls(wxWindow* parent, FilmViewer& viewer, bool editor_controls)
        : wxPanel (parent)
+       , _markers (new MarkersPanel(this, viewer))
        , _slider (new wxSlider(this, wxID_ANY, 0, 0, 4096))
        , _viewer (viewer)
        , _slider_being_moved (false)
@@ -100,14 +106,19 @@ Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor
        time_sizer->Add (_timecode, 0, wxEXPAND);
 
        h_sizer->Add (_rewind_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
-       h_sizer->Add (time_sizer, 0, wxEXPAND);
+       h_sizer->Add (time_sizer, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
        h_sizer->Add (_back_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
        h_sizer->Add (_forward_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2);
 
        _button_sizer = new wxBoxSizer (wxHORIZONTAL);
        h_sizer->Add (_button_sizer, 0, wxEXPAND);
 
-       h_sizer->Add (_slider, 1, wxEXPAND);
+       {
+               auto box = new wxBoxSizer (wxVERTICAL);
+               box->Add (_markers, 0, wxEXPAND);
+               box->Add (_slider, 0, wxEXPAND);
+               h_sizer->Add (box, 1, wxEXPAND);
+       }
 
        _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
 
@@ -127,7 +138,7 @@ Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor
                _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
        }
        if (_outline_content) {
-               _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
+               _outline_content->bind(&Controls::outline_content_changed, this);
        }
 
        _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,    boost::bind(&Controls::slider_moved,    this, false));
@@ -140,17 +151,17 @@ Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor
        _frame_number->Bind     (wxEVT_LEFT_DOWN,            boost::bind(&Controls::frame_number_clicked, this));
        _timecode->Bind         (wxEVT_LEFT_DOWN,            boost::bind(&Controls::timecode_clicked, this));
        if (_jump_to_selected) {
-               _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
+               _jump_to_selected->bind(&Controls::jump_to_selected_clicked, this);
                _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
        }
 
-       viewer->Started.connect (boost::bind(&Controls::started, this));
-       viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
+       viewer.Started.connect (boost::bind(&Controls::started, this));
+       viewer.Stopped.connect (boost::bind(&Controls::stopped, this));
 
        Bind (wxEVT_TIMER, boost::bind(&Controls::update_position, this));
        _timer.Start (80, wxTIMER_CONTINUOUS);
 
-       set_film (viewer->film());
+       set_film(viewer.film());
 
        setup_sensitivity ();
 
@@ -186,12 +197,7 @@ Controls::stopped ()
 void
 Controls::update_position ()
 {
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return;
-       }
-
-       if (!_slider_being_moved && !viewer->pending_idle_get()) {
+       if (!_slider_being_moved && !_viewer.pending_idle_get()) {
                update_position_label ();
                update_position_slider ();
        }
@@ -201,24 +207,14 @@ Controls::update_position ()
 void
 Controls::eye_changed ()
 {
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return;
-       }
-
-       viewer->set_eyes (_eye->GetSelection() == 0 ? Eyes::LEFT : Eyes::RIGHT);
+       _viewer.set_eyes(_eye->GetSelection() == 0 ? Eyes::LEFT : Eyes::RIGHT);
 }
 
 
 void
 Controls::outline_content_changed ()
 {
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return;
-       }
-
-       viewer->set_outline_content (_outline_content->GetValue());
+       _viewer.set_outline_content(_outline_content->GetValue());
 }
 
 
@@ -226,14 +222,13 @@ Controls::outline_content_changed ()
 void
 Controls::slider_moved (bool page)
 {
-       auto viewer = _viewer.lock ();
-       if (!_film || !viewer) {
+       if (!_film) {
                return;
        }
 
        if (!page && !_slider_being_moved) {
                /* This is the first event of a drag; stop playback for the duration of the drag */
-               viewer->suspend ();
+               _viewer.suspend();
                _slider_being_moved = true;
        }
 
@@ -244,30 +239,19 @@ Controls::slider_moved (bool page)
        */
        bool accurate = false;
        if (t >= _film->length ()) {
-               t = _film->length() - viewer->one_video_frame();
+               t = _film->length() - _viewer.one_video_frame();
                accurate = true;
        }
-       viewer->seek (t, accurate);
+       _viewer.seek(t, accurate);
        update_position_label ();
-
-       log (
-               wxString::Format(
-                       "playback-seeked %s", t.timecode(_film->video_frame_rate()).c_str()
-                       )
-               );
 }
 
 
 void
 Controls::slider_released ()
 {
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return;
-       }
-
        /* Restart after a drag */
-       viewer->resume ();
+       _viewer.resume();
        _slider_being_moved = false;
 }
 
@@ -280,15 +264,10 @@ Controls::update_position_slider ()
                return;
        }
 
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return;
-       }
-
        auto const len = _film->length ();
 
        if (len.get ()) {
-               int const new_slider_position = 4096 * viewer->position().get() / len.get();
+               int const new_slider_position = 4096 * _viewer.position().get() / len.get();
                if (new_slider_position != _slider->GetValue()) {
                        _slider->SetValue (new_slider_position);
                }
@@ -305,15 +284,10 @@ Controls::update_position_label ()
                return;
        }
 
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return;
-       }
-
        double const fps = _film->video_frame_rate ();
        /* Count frame number from 1 ... not sure if this is the best idea */
-       checked_set (_frame_number, wxString::Format (wxT("%ld"), lrint (viewer->position().seconds() * fps) + 1));
-       checked_set (_timecode, time_to_timecode (viewer->position(), fps));
+       checked_set(_frame_number, wxString::Format (wxT("%ld"), lrint(_viewer.position().seconds() * fps) + 1));
+       checked_set(_timecode, time_to_timecode(_viewer.position(), fps));
 }
 
 
@@ -328,12 +302,7 @@ Controls::active_jobs_changed (optional<string> j)
 DCPTime
 Controls::nudge_amount (wxKeyboardState& ev)
 {
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return {};
-       }
-
-       auto amount = viewer->one_video_frame ();
+       auto amount = _viewer.one_video_frame();
 
        if (ev.ShiftDown() && !ev.ControlDown()) {
                amount = DCPTime::from_seconds (1);
@@ -350,10 +319,7 @@ Controls::nudge_amount (wxKeyboardState& ev)
 void
 Controls::rewind_clicked (wxMouseEvent& ev)
 {
-       auto viewer = _viewer.lock ();
-       if (viewer) {
-               viewer->seek (DCPTime(), true);
-       }
+       _viewer.seek(DCPTime(), true);
        ev.Skip();
 }
 
@@ -361,40 +327,28 @@ Controls::rewind_clicked (wxMouseEvent& ev)
 void
 Controls::back_frame ()
 {
-       auto viewer = _viewer.lock ();
-       if (viewer) {
-               viewer->seek_by (-viewer->one_video_frame(), true);
-       }
+       _viewer.seek_by(-_viewer.one_video_frame(), true);
 }
 
 
 void
 Controls::forward_frame ()
 {
-       auto viewer = _viewer.lock ();
-       if (viewer) {
-               viewer->seek_by (viewer->one_video_frame(), true);
-       }
+       _viewer.seek_by(_viewer.one_video_frame(), true);
 }
 
 
 void
 Controls::back_clicked (wxKeyboardState& ev)
 {
-       auto viewer = _viewer.lock ();
-       if (viewer) {
-               viewer->seek_by (-nudge_amount(ev), true);
-       }
+       _viewer.seek_by(-nudge_amount(ev), true);
 }
 
 
 void
 Controls::forward_clicked (wxKeyboardState& ev)
 {
-       auto viewer = _viewer.lock ();
-       if (viewer) {
-               viewer->seek_by (nudge_amount(ev), true);
-       }
+       _viewer.seek_by(nudge_amount(ev), true);
 }
 
 
@@ -427,32 +381,24 @@ Controls::setup_sensitivity ()
 void
 Controls::timecode_clicked ()
 {
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return;
-       }
+       auto dialog = new PlayheadToTimecodeDialog(this, _viewer.position(), _film->video_frame_rate());
+       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
 
-       auto dialog = new PlayheadToTimecodeDialog (this, viewer->position(), _film->video_frame_rate());
        if (dialog->ShowModal() == wxID_OK) {
-               viewer->seek (dialog->get(), true);
+               _viewer.seek(dialog->get(), true);
        }
-       dialog->Destroy ();
 }
 
 
 void
 Controls::frame_number_clicked ()
 {
-       auto viewer = _viewer.lock ();
-       if (!viewer) {
-               return;
-       }
+       auto dialog = new PlayheadToFrameDialog(this, _viewer.position(), _film->video_frame_rate());
+       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
 
-       auto dialog = new PlayheadToFrameDialog (this, viewer->position(), _film->video_frame_rate());
        if (dialog->ShowModal() == wxID_OK) {
-               viewer->seek (dialog->get(), true);
+               _viewer.seek(dialog->get(), true);
        }
-       dialog->Destroy ();
 }
 
 
@@ -472,6 +418,8 @@ Controls::set_film (shared_ptr<Film> film)
 
        _film = film;
 
+       _markers->set_film (_film);
+
        if (_film) {
                _film_change_connection = _film->Change.connect (boost::bind(&Controls::film_change, this, _1, _2));
        }