Supporters update.
[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 "check_box.h"
23 #include "dcpomatic_button.h"
24 #include "film_viewer.h"
25 #include "markers.h"
26 #include "markers_dialog.h"
27 #include "static_text.h"
28 #include "timecode.h"
29 #include "wx_util.h"
30 #include "lib/film.h"
31 #include <dcp/types.h>
32 #include <dcp/warnings.h>
33 LIBDCP_DISABLE_WARNINGS
34 #include <wx/gbsizer.h>
35 LIBDCP_ENABLE_WARNINGS
36 #include <boost/bind/bind.hpp>
37
38
39 using std::make_shared;
40 using std::shared_ptr;
41 using std::weak_ptr;
42 using boost::bind;
43 using boost::optional;
44 using dcpomatic::DCPTime;
45
46
47 class Marker
48 {
49 public:
50         Marker(wxWindow* parent, wxGridBagSizer* grid, int row, weak_ptr<Film> film, FilmViewer const& viewer, wxString name, dcp::Marker type)
51                 : _film(film)
52                 , _viewer(viewer)
53                 , _type(type)
54         {
55                 _checkbox = new CheckBox(parent, name);
56                 grid->Add(_checkbox, wxGBPosition(row, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
57                 _timecode = new Timecode<DCPTime>(parent);
58                 grid->Add(_timecode, wxGBPosition(row, 1));
59                 _set_button = new Button(parent, _("Set from current position"));
60                 grid->Add(_set_button, wxGBPosition(row, 2));
61
62                 auto f = _film.lock();
63                 DCPOMATIC_ASSERT (f);
64
65                 auto t = f->marker(_type);
66                 _checkbox->SetValue(static_cast<bool>(t));
67                 if (t) {
68                         _timecode->set(*t, f->video_frame_rate());
69                 }
70
71                 set_sensitivity ();
72
73                 _set_button->Bind(wxEVT_BUTTON, bind(&Marker::set, this));
74                 _checkbox->bind(&Marker::checkbox_clicked, this);
75                 _timecode->Changed.connect(bind(&Marker::changed, this));
76         }
77
78 private:
79         void checkbox_clicked ()
80         {
81                 set_sensitivity ();
82                 changed ();
83         }
84
85         void set_sensitivity ()
86         {
87                 _timecode->Enable(_checkbox->GetValue());
88                 _set_button->Enable(_checkbox->GetValue());
89         }
90
91         void set ()
92         {
93                 auto f = _film.lock();
94                 DCPOMATIC_ASSERT (f);
95                 _timecode->set(_viewer.position(), f->video_frame_rate());
96                 changed ();
97         }
98
99         void changed ()
100         {
101                 auto f = _film.lock();
102                 DCPOMATIC_ASSERT (f);
103                 auto vfr = f->video_frame_rate();
104                 auto tc = _timecode->get(vfr);
105                 if (tc >= f->length()) {
106                         tc = f->length() - DCPTime::from_frames(1, vfr);
107                         _timecode->set(tc, vfr);
108                 }
109                 if (_checkbox->GetValue()) {
110                         f->set_marker(_type, tc);
111                 } else {
112                         f->unset_marker(_type);
113                 }
114         }
115
116         weak_ptr<Film> _film;
117         FilmViewer const& _viewer;
118         dcp::Marker _type;
119         CheckBox* _checkbox;
120         Timecode<dcpomatic::DCPTime>* _timecode;
121         Button* _set_button;
122 };
123
124
125 MarkersDialog::MarkersDialog(wxWindow* parent, weak_ptr<Film> film, FilmViewer const& viewer)
126         : wxDialog (parent, wxID_ANY, _("Markers"))
127         , _film (film)
128 {
129         auto sizer = new wxBoxSizer (wxVERTICAL);
130         auto grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
131
132         int r = 0;
133         for (auto const& marker: all_editable_markers()) {
134                 _markers.push_back (make_shared<Marker>(this, grid, r++, film, viewer, marker.first, marker.second));
135         }
136
137         sizer->Add (grid, 0, wxALL, 8);
138
139         auto buttons = CreateSeparatedButtonSizer (wxCLOSE);
140         if (buttons) {
141                 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
142         }
143
144         SetSizerAndFit (sizer);
145 }