Add unfinished timing tab; fix crash on reconstruction of timeline; fix lack of black...
[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 boost::lexical_cast;
26
27 Timecode::Timecode (wxWindow* parent)
28         : wxPanel (parent)
29 {
30         wxClientDC dc (parent);
31         wxSize size = dc.GetTextExtent (wxT ("9999"));
32         size.SetHeight (-1);
33
34         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
35         wxArrayString list;
36
37         string n = "0123456789";
38         for (size_t i = 0; i < n.length(); ++i) {
39                 list.Add (n[i]);
40         }
41
42         validator.SetIncludes (list);
43         
44         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
45         _hours = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size, 0, validator);
46         _hours->SetMaxLength (2);
47         sizer->Add (_hours);
48         add_label_to_sizer (sizer, this, ":");
49         _minutes = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
50         _minutes->SetMaxLength (2);
51         sizer->Add (_minutes);
52         add_label_to_sizer (sizer, this, ":");
53         _seconds = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
54         _seconds->SetMaxLength (2);
55         sizer->Add (_seconds);
56         add_label_to_sizer (sizer, this, ".");
57         _frames = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
58         _frames->SetMaxLength (2);
59         sizer->Add (_frames);
60
61         SetSizerAndFit (sizer);
62 }
63
64 void
65 Timecode::set (Time t, int fps)
66 {
67         int const h = t / (3600 * TIME_HZ);
68         t -= h * 3600 * TIME_HZ;
69         int const m = t / (60 * TIME_HZ);
70         t -= m * 60 * TIME_HZ;
71         int const s = t / TIME_HZ;
72         t -= s * TIME_HZ;
73         int const f = t * fps / TIME_HZ;
74
75         _hours->SetValue (wxString::Format ("%02d", h));
76         _minutes->SetValue (wxString::Format ("%02d", m));
77         _seconds->SetValue (wxString::Format ("%02d", s));
78         _frames->SetValue (wxString::Format ("%02d", f));
79 }
80
81 Time
82 Timecode::get (int fps) const
83 {
84         Time t = 0;
85         t += lexical_cast<int> (wx_to_std (_hours->GetValue())) * 3600 * TIME_HZ;
86         t += lexical_cast<int> (wx_to_std (_minutes->GetValue())) * 60 * TIME_HZ;
87         t += lexical_cast<int> (wx_to_std (_seconds->GetValue())) * TIME_HZ;
88         t += lexical_cast<int> (wx_to_std (_frames->GetValue())) * TIME_HZ / fps;
89         return t;
90 }