Don't overlap simultaneous video content in the timeline. Fix keep-aligned for separ...
[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 "lib/util.h"
22 #include "timecode.h"
23 #include "wx_util.h"
24
25 using std::string;
26 using std::cout;
27 using boost::lexical_cast;
28
29 Timecode::Timecode (wxWindow* parent)
30         : wxPanel (parent)
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         _sizer = new wxBoxSizer (wxHORIZONTAL);
47         
48         _editable = new wxPanel (this);
49         wxSizer* editable_sizer = new wxBoxSizer (wxHORIZONTAL);
50         _hours = new wxTextCtrl (_editable, wxID_ANY, wxT(""), wxDefaultPosition, size, 0, validator);
51         _hours->SetMaxLength (2);
52         editable_sizer->Add (_hours);
53         add_label_to_sizer (editable_sizer, _editable, wxT (":"), false);
54         _minutes = new wxTextCtrl (_editable, wxID_ANY, wxT(""), wxDefaultPosition, size, 0, validator);
55         _minutes->SetMaxLength (2);
56         editable_sizer->Add (_minutes);
57         add_label_to_sizer (editable_sizer, _editable, wxT (":"), false);
58         _seconds = new wxTextCtrl (_editable, wxID_ANY, wxT(""), wxDefaultPosition, size, 0, validator);
59         _seconds->SetMaxLength (2);
60         editable_sizer->Add (_seconds);
61         add_label_to_sizer (editable_sizer, _editable, wxT (":"), false);
62         _frames = new wxTextCtrl (_editable, wxID_ANY, wxT(""), wxDefaultPosition, size, 0, validator);
63         _frames->SetMaxLength (2);
64         editable_sizer->Add (_frames);
65         _set_button = new wxButton (_editable, wxID_ANY, _("Set"));
66         editable_sizer->Add (_set_button, 0, wxLEFT | wxRIGHT, 8);
67         _editable->SetSizerAndFit (editable_sizer);
68         _sizer->Add (_editable);
69
70         _fixed = add_label_to_sizer (_sizer, this, wxT ("42"), false);
71         
72         _hours->Bind      (wxEVT_COMMAND_TEXT_UPDATED,   boost::bind (&Timecode::changed, this));
73         _minutes->Bind    (wxEVT_COMMAND_TEXT_UPDATED,   boost::bind (&Timecode::changed, this));
74         _seconds->Bind    (wxEVT_COMMAND_TEXT_UPDATED,   boost::bind (&Timecode::changed, this));
75         _frames->Bind     (wxEVT_COMMAND_TEXT_UPDATED,   boost::bind (&Timecode::changed, this));
76         _set_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&Timecode::set_clicked, this));
77
78         _set_button->Enable (false);
79
80         set_editable (true);
81
82         SetSizerAndFit (_sizer);
83 }
84
85 void
86 Timecode::set (Time t, int fps)
87 {
88         /* Do this calculation with frames so that we can round
89            to a frame boundary at the start rather than the end.
90         */
91         int64_t f = divide_with_round (t * fps, TIME_HZ);
92         
93         int const h = f / (3600 * fps);
94         f -= h * 3600 * fps;
95         int const m = f / (60 * fps);
96         f -= m * 60 * fps;
97         int const s = f / fps;
98         f -= s * fps;
99
100         checked_set (_hours, lexical_cast<string> (h));
101         checked_set (_minutes, lexical_cast<string> (m));
102         checked_set (_seconds, lexical_cast<string> (s));
103         checked_set (_frames, lexical_cast<string> (f));
104
105         _fixed->SetLabel (wxString::Format ("%02d:%02d:%02d.%02" wxLongLongFmtSpec "d", h, m, s, f));
106 }
107
108 Time
109 Timecode::get (int fps) const
110 {
111         Time t = 0;
112         string const h = wx_to_std (_hours->GetValue ());
113         t += lexical_cast<int> (h.empty() ? "0" : h) * 3600 * TIME_HZ;
114         string const m = wx_to_std (_minutes->GetValue());
115         t += lexical_cast<int> (m.empty() ? "0" : m) * 60 * TIME_HZ;
116         string const s = wx_to_std (_seconds->GetValue());
117         t += lexical_cast<int> (s.empty() ? "0" : s) * TIME_HZ;
118         string const f = wx_to_std (_frames->GetValue());
119         t += lexical_cast<int> (f.empty() ? "0" : f) * TIME_HZ / fps;
120
121         return t;
122 }
123
124 void
125 Timecode::changed ()
126 {
127         _set_button->Enable (true);
128 }
129
130 void
131 Timecode::set_clicked ()
132 {
133         Changed ();
134         _set_button->Enable (false);
135 }
136
137 void
138 Timecode::set_editable (bool e)
139 {
140         _editable->Show (e);
141         _fixed->Show (!e);
142         _sizer->Layout ();
143 }