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