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