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