Limit markers to the film length on entry (#1920).
[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 #include "markers_dialog.h"
22 #include "wx_util.h"
23 #include "timecode.h"
24 #include "static_text.h"
25 #include "dcpomatic_button.h"
26 #include "check_box.h"
27 #include "film_viewer.h"
28 #include "lib/film.h"
29 #include <dcp/types.h>
30 #include <wx/gbsizer.h>
31 #include <boost/bind/bind.hpp>
32 #include <iostream>
33
34 using std::cout;
35 using std::shared_ptr;
36 using std::weak_ptr;
37 using std::make_shared;
38 using boost::optional;
39 using boost::bind;
40 using dcpomatic::DCPTime;
41
42 class Marker
43 {
44 public:
45         Marker (wxWindow* parent, wxGridBagSizer* grid, int row, weak_ptr<Film> film_, weak_ptr<FilmViewer> viewer_, wxString name, dcp::Marker type_)
46                 : film (film_)
47                 , viewer (viewer_)
48                 , type (type_)
49         {
50                 checkbox = new CheckBox(parent, name);
51                 grid->Add (checkbox, wxGBPosition(row, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
52                 timecode = new Timecode<DCPTime> (parent);
53                 grid->Add (timecode, wxGBPosition(row, 1));
54                 set_button = new Button (parent, _("Set from current position"));
55                 grid->Add (set_button, wxGBPosition(row, 2));
56
57                 auto f = film.lock ();
58                 DCPOMATIC_ASSERT (f);
59
60                 auto t = f->marker (type);
61                 checkbox->SetValue (static_cast<bool>(t));
62                 if (t) {
63                         timecode->set (*t, f->video_frame_rate());
64                 }
65
66                 set_sensitivity ();
67
68                 set_button->Bind (wxEVT_BUTTON, bind(&Marker::set, this));
69                 checkbox->Bind (wxEVT_CHECKBOX, bind(&Marker::set_sensitivity, this));
70                 timecode->Changed.connect (bind(&Marker::changed, this));
71         }
72
73 private:
74         void set_sensitivity ()
75         {
76                 timecode->Enable (checkbox->GetValue());
77                 set_button->Enable (checkbox->GetValue());
78         }
79
80         void set ()
81         {
82                 auto f = film.lock ();
83                 DCPOMATIC_ASSERT (f);
84                 auto v = viewer.lock ();
85                 DCPOMATIC_ASSERT (v);
86                 timecode->set (v->position(), f->video_frame_rate());
87                 changed ();
88         }
89
90         void changed ()
91         {
92                 auto f = film.lock ();
93                 DCPOMATIC_ASSERT (f);
94                 auto tc = timecode->get(f->video_frame_rate());
95                 if (tc >= f->length()) {
96                         tc = f->length();
97                         timecode->set (tc, f->video_frame_rate());
98                 }
99                 if (checkbox->GetValue()) {
100                         f->set_marker (type, tc);
101                 } else {
102                         f->unset_marker (type);
103                 }
104         }
105
106         weak_ptr<Film> film;
107         weak_ptr<FilmViewer> viewer;
108         dcp::Marker type;
109         CheckBox* checkbox;
110         Timecode<dcpomatic::DCPTime>* timecode;
111         Button* set_button;
112 };
113
114 MarkersDialog::MarkersDialog (wxWindow* parent, weak_ptr<Film> film, weak_ptr<FilmViewer> viewer)
115         : wxDialog (parent, wxID_ANY, _("Markers"))
116         , _film (film)
117 {
118         auto sizer = new wxBoxSizer (wxVERTICAL);
119         auto grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
120
121         int r = 0;
122         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of composition"), dcp::Marker::FFOC));
123         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of composition"), dcp::Marker::LFOC));
124         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of title credits"), dcp::Marker::FFTC));
125         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of title credits"), dcp::Marker::LFTC));
126         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of intermission"), dcp::Marker::FFOI));
127         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of intermission"), dcp::Marker::LFOI));
128         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of end credits"), dcp::Marker::FFEC));
129         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of end credits"), dcp::Marker::LFEC));
130         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("First frame of moving credits"), dcp::Marker::FFMC));
131         _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, _("Last frame of moving credits"), dcp::Marker::LFMC));
132
133         sizer->Add (grid, 0, wxALL, 8);
134         SetSizerAndFit (sizer);
135 }