Merge master.
[dcpomatic.git] / src / wx / timecode.cc
1 /*
2     Copyright (C) 2013 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 #include <boost/lexical_cast.hpp>
21 #include "timecode.h"
22 #include "wx_util.h"
23
24 using std::string;
25 using std::cout;
26 using boost::lexical_cast;
27
28 Timecode::Timecode (wxWindow* parent)
29         : wxPanel (parent)
30         , _in_set (false)
31 {
32         wxClientDC dc (parent);
33         wxSize size = dc.GetTextExtent (wxT ("9999"));
34         size.SetHeight (-1);
35
36         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
37         wxArrayString list;
38
39         wxString n (wxT ("0123456789"));
40         for (size_t i = 0; i < n.Length(); ++i) {
41                 list.Add (n[i]);
42         }
43
44         validator.SetIncludes (list);
45         
46         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
47         _hours = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size, 0, validator);
48         _hours->SetMaxLength (2);
49         sizer->Add (_hours);
50         add_label_to_sizer (sizer, this, wxT (":"), false);
51         _minutes = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
52         _minutes->SetMaxLength (2);
53         sizer->Add (_minutes);
54         add_label_to_sizer (sizer, this, wxT (":"), false);
55         _seconds = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
56         _seconds->SetMaxLength (2);
57         sizer->Add (_seconds);
58         add_label_to_sizer (sizer, this, wxT ("."), false);
59         _frames = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
60         _frames->SetMaxLength (2);
61         sizer->Add (_frames);
62
63         _hours->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (Timecode::changed), 0, this);
64         _minutes->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (Timecode::changed), 0, this);
65         _seconds->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (Timecode::changed), 0, this);
66         _frames->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (Timecode::changed), 0, this);
67
68         SetSizerAndFit (sizer);
69 }
70
71 void
72 Timecode::set (Time t, int fps)
73 {
74         _in_set = true;
75         
76         int const h = t / (3600 * TIME_HZ);
77         t -= h * 3600 * TIME_HZ;
78         int const m = t / (60 * TIME_HZ);
79         t -= m * 60 * TIME_HZ;
80         int const s = t / TIME_HZ;
81         t -= s * TIME_HZ;
82         int const f = t * fps / TIME_HZ;
83
84         _hours->SetValue (wxString::Format (wxT ("%d"), h));
85         _minutes->SetValue (wxString::Format (wxT ("%d"), m));
86         _seconds->SetValue (wxString::Format (wxT ("%d"), s));
87         _frames->SetValue (wxString::Format (wxT ("%d"), f));
88
89         _in_set = false;
90 }
91
92 Time
93 Timecode::get (int fps) const
94 {
95         Time t = 0;
96         string const h = wx_to_std (_hours->GetValue ());
97         t += lexical_cast<int> (h.empty() ? "0" : h) * 3600 * TIME_HZ;
98         string const m = wx_to_std (_minutes->GetValue());
99         t += lexical_cast<int> (m.empty() ? "0" : m) * 60 * TIME_HZ;
100         string const s = wx_to_std (_seconds->GetValue());
101         t += lexical_cast<int> (s.empty() ? "0" : s) * TIME_HZ;
102         string const f = wx_to_std (_frames->GetValue());
103         t += lexical_cast<int> (f.empty() ? "0" : f) * TIME_HZ / fps;
104         return t;
105 }
106
107 void
108 Timecode::changed (wxCommandEvent &)
109 {
110         if (_in_set) {
111                 return;
112         }
113         
114         Changed ();
115 }