Supporters update.
[dcpomatic.git] / src / wx / timecode.h
1 /*
2     Copyright (C) 2013-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 #ifndef DCPOMATIC_WX_TIMECODE_H
23 #define DCPOMATIC_WX_TIMECODE_H
24
25
26 #include "wx_util.h"
27 #include "lib/dcpomatic_time.h"
28 #include <dcp/raw_convert.h>
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <wx/wx.h>
32 LIBDCP_ENABLE_WARNINGS
33 #include <boost/signals2.hpp>
34
35
36 class TimecodeBase : public wxPanel
37 {
38 public:
39         TimecodeBase (wxWindow *, bool set_button);
40
41         void clear ();
42
43         void set_editable (bool);
44         void set_focus ();
45
46         boost::signals2::signal<void ()> Changed;
47
48         static wxSize size (wxWindow* parent);
49
50 protected:
51         void changed ();
52         void set_clicked ();
53
54         wxSizer* _sizer;
55         wxPanel* _editable;
56         wxTextCtrl* _hours;
57         wxTextCtrl* _minutes;
58         wxTextCtrl* _seconds;
59         wxTextCtrl* _frames;
60         wxButton* _set_button;
61         wxStaticText* _fixed;
62
63         bool _ignore_changed = false;
64 };
65
66
67 template <class T>
68 class Timecode : public TimecodeBase
69 {
70 public:
71         Timecode (wxWindow* parent, bool set_button = true)
72                 : TimecodeBase (parent, set_button)
73         {
74
75         }
76
77         void set (T t, float fps)
78         {
79                 auto const hmsf = t.split (fps);
80
81                 checked_set (_hours, dcp::raw_convert<std::string>(hmsf.h));
82                 checked_set (_minutes, dcp::raw_convert<std::string>(hmsf.m));
83                 checked_set (_seconds, dcp::raw_convert<std::string>(hmsf.s));
84                 checked_set (_frames, dcp::raw_convert<std::string>(hmsf.f));
85
86                 checked_set (_fixed, t.timecode (fps));
87         }
88
89         void set_hint (T t, float fps)
90         {
91                 auto hmsf = t.split (fps);
92
93                 _hours->SetHint (std_to_wx(dcp::raw_convert<std::string>(hmsf.h)));
94                 _minutes->SetHint (std_to_wx(dcp::raw_convert<std::string>(hmsf.m)));
95                 _seconds->SetHint (std_to_wx(dcp::raw_convert<std::string>(hmsf.s)));
96                 _frames->SetHint (std_to_wx(dcp::raw_convert<std::string>(hmsf.f)));
97         }
98
99         dcpomatic::HMSF get () const
100         {
101                 auto value_or_hint = [](wxTextCtrl const * t) {
102                         auto s = wx_to_std (t->GetValue().IsEmpty() ? t->GetHint() : t->GetValue());
103                         if (s.empty()) {
104                                 return 0;
105                         }
106                         return dcp::raw_convert<int>(s);
107                 };
108
109                 return { value_or_hint(_hours),
110                         value_or_hint(_minutes),
111                         value_or_hint(_seconds),
112                         value_or_hint(_frames) };
113         }
114
115         T get (float fps) const
116         {
117                 return T(get(), fps);
118         }
119 };
120
121 #endif