C++11 tidying.
[dcpomatic.git] / src / wx / markers_dialog.cc
1 /*
2     Copyright (C) 2019-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "markers_dialog.h"
23 #include "wx_util.h"
24 #include "timecode.h"
25 #include "static_text.h"
26 #include "dcpomatic_button.h"
27 #include "check_box.h"
28 #include "film_viewer.h"
29 #include "lib/film.h"
30 #include <dcp/types.h>
31 #include <wx/gbsizer.h>
32 #include <boost/bind/bind.hpp>
33 #include <iostream>
34
35
36 using std::cout;
37 using std::shared_ptr;
38 using std::weak_ptr;
39 using std::make_shared;
40 using boost::optional;
41 using boost::bind;
42 using dcpomatic::DCPTime;
43
44
45 class Marker
46 {
47 public:
48         Marker (wxWindow* parent, wxGridBagSizer* grid, int row, weak_ptr<Film> film_, weak_ptr<FilmViewer> viewer_, wxString name, dcp::Marker type_)
49                 : film (film_)
50                 , viewer (viewer_)
51                 , type (type_)
52         {
53                 checkbox = new CheckBox(parent, name);
54                 grid->Add (checkbox, wxGBPosition(row, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
55                 timecode = new Timecode<DCPTime> (parent);
56                 grid->Add (timecode, wxGBPosition(row, 1));
57                 set_button = new Button (parent, _("Set from current position"));
58                 grid->Add (set_button, wxGBPosition(row, 2));
59
60                 auto f = film.lock ();
61                 DCPOMATIC_ASSERT (f);
62
63                 auto t = f->marker (type);
64                 checkbox->SetValue (static_cast<bool>(t));
65                 if (t) {
66                         timecode->set (*t, f->video_frame_rate());
67                 }
68
69                 set_sensitivity ();
70
71                 set_button->Bind (wxEVT_BUTTON, bind(&Marker::set, this));
72                 checkbox->Bind (wxEVT_CHECKBOX, bind(&Marker::set_sensitivity, this));
73                 timecode->Changed.connect (bind(&Marker::changed, this));
74         }
75
76 private:
77         void set_sensitivity ()
78         {
79                 timecode->Enable (checkbox->GetValue());
80                 set_button->Enable (checkbox->GetValue());
81         }
82
83         void set ()
84         {
85                 auto f = film.lock ();
86                 DCPOMATIC_ASSERT (f);
87                 auto v = viewer.lock ();
88                 DCPOMATIC_ASSERT (v);
89                 timecode->set (v->position(), f->video_frame_rate());
90                 changed ();
91         }
92
93         void changed ()
94         {
95                 auto f = film.lock ();
96                 DCPOMATIC_ASSERT (f);
97                 auto vfr = f->video_frame_rate();
98                 auto tc = timecode->get(vfr);
99                 if (tc >= f->length()) {
100                         tc = f->length() - DCPTime::from_frames(1, vfr);
101                         timecode->set (tc, vfr);
102                 }
103                 if (checkbox->GetValue()) {
104                         f->set_marker (type, tc);
105                 } else {
106                         f->unset_marker (type);
107                 }
108         }
109
110         weak_ptr<Film> film;
111         weak_ptr<FilmViewer> viewer;
112         dcp::Marker type;
113         CheckBox* checkbox;
114         Timecode<dcpomatic::DCPTime>* timecode;
115         Button* set_button;
116 };
117
118
119 MarkersDialog::MarkersDialog (wxWindow* parent, weak_ptr<Film> film, weak_ptr<FilmViewer> viewer)
120         : wxDialog (parent, wxID_ANY, _("Markers"))
121         , _film (film)
122 {
123         auto sizer = new wxBoxSizer (wxVERTICAL);
124         auto grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
125
126         int r = 0;
127         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of composition"), dcp::Marker::FFOC));
128         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of composition"), dcp::Marker::LFOC));
129         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of title credits"), dcp::Marker::FFTC));
130         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of title credits"), dcp::Marker::LFTC));
131         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of intermission"), dcp::Marker::FFOI));
132         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of intermission"), dcp::Marker::LFOI));
133         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of end credits"), dcp::Marker::FFEC));
134         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of end credits"), dcp::Marker::LFEC));
135         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of moving credits"), dcp::Marker::FFMC));
136         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of moving credits"), dcp::Marker::LFMC));
137
138         sizer->Add (grid, 0, wxALL, 8);
139         SetSizerAndFit (sizer);
140 }