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