Hand-apply d7329603f8698f5302d11acfc233ca67695e654d from master; timecode labelling.
[dcpomatic.git] / src / wx / timecode.h
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef DCPOMATIC_WX_TIMECODE_H
21 #define DCPOMATIC_WX_TIMECODE_H
22
23 #include "wx_util.h"
24 #include "lib/types.h"
25 #include <wx/wx.h>
26 #include <boost/signals2.hpp>
27 #include <boost/lexical_cast.hpp>
28
29 class TimecodeBase : public wxPanel
30 {
31 public:
32         TimecodeBase (wxWindow *);
33
34         void clear ();
35
36         void set_editable (bool);
37
38         boost::signals2::signal<void ()> Changed;
39
40         static wxSize size (wxWindow* parent);
41
42 protected:
43         void changed ();
44         void set_clicked ();
45         
46         wxSizer* _sizer;
47         wxPanel* _editable;
48         wxTextCtrl* _hours;
49         wxTextCtrl* _minutes;
50         wxTextCtrl* _seconds;
51         wxTextCtrl* _frames;
52         wxButton* _set_button;
53         wxStaticText* _fixed;
54 };
55
56 template <class T>
57 class Timecode : public TimecodeBase
58 {
59 public:
60         Timecode (wxWindow* parent)
61                 : TimecodeBase (parent)
62         {
63
64         }
65
66         void set (T t, float fps)
67         {
68                 int h;
69                 int m;
70                 int s;
71                 int f;
72                 t.split (fps, h, m, s, f);
73                 
74                 checked_set (_hours, boost::lexical_cast<std::string> (h));
75                 checked_set (_minutes, boost::lexical_cast<std::string> (m));
76                 checked_set (_seconds, boost::lexical_cast<std::string> (s));
77                 checked_set (_frames, boost::lexical_cast<std::string> (f));
78                 
79                 _fixed->SetLabel (std_to_wx (t.timecode (fps)));
80         }
81
82         T get (int fps) const
83         {
84                 T t;
85                 std::string const h = wx_to_std (_hours->GetValue ());
86                 t += T::from_seconds (boost::lexical_cast<int> (h.empty() ? "0" : h) * 3600);
87                 std::string const m = wx_to_std (_minutes->GetValue());
88                 t += T::from_seconds (boost::lexical_cast<int> (m.empty() ? "0" : m) * 60);
89                 std::string const s = wx_to_std (_seconds->GetValue());
90                 t += T::from_seconds (boost::lexical_cast<int> (s.empty() ? "0" : s));
91                 std::string const f = wx_to_std (_frames->GetValue());
92                 t += T::from_seconds (boost::lexical_cast<double> (f.empty() ? "0" : f) / fps);
93                 
94                 return t;
95         }
96 };
97
98 #endif