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