Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / wx / timecode.cc
1 /*
2     Copyright (C) 2013-2018 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 #include "timecode.h"
22 #include "wx_util.h"
23 #include "dcpomatic_button.h"
24 #include "lib/util.h"
25 #include <iostream>
26
27 using std::string;
28 using std::cout;
29
30 TimecodeBase::TimecodeBase (wxWindow* parent, bool set_button)
31         : wxPanel (parent)
32         , _set_button (0)
33 {
34         wxSize const s = TimecodeBase::size (parent);
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, s, 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, s, 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, s, 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, s, 0, validator);
63         _frames->SetMaxLength (2);
64         editable_sizer->Add (_frames);
65         if (set_button) {
66                 _set_button = new Button (_editable, _("Set"));
67                 editable_sizer->Add (_set_button, 0, wxLEFT | wxRIGHT, 8);
68         }
69         _editable->SetSizerAndFit (editable_sizer);
70         _sizer->Add (_editable);
71
72         _fixed = add_label_to_sizer (_sizer, this, wxT ("42"), false);
73
74         _hours->Bind      (wxEVT_TEXT,   boost::bind (&TimecodeBase::changed, this));
75         _minutes->Bind    (wxEVT_TEXT,   boost::bind (&TimecodeBase::changed, this));
76         _seconds->Bind    (wxEVT_TEXT,   boost::bind (&TimecodeBase::changed, this));
77         _frames->Bind     (wxEVT_TEXT,   boost::bind (&TimecodeBase::changed, this));
78         if (_set_button) {
79                 _set_button->Bind (wxEVT_BUTTON, boost::bind (&TimecodeBase::set_clicked, this));
80                 _set_button->Enable (false);
81         }
82
83         set_editable (true);
84
85         SetSizerAndFit (_sizer);
86 }
87
88 void
89 TimecodeBase::set_focus ()
90 {
91         _hours->SetFocus ();
92 }
93
94 void
95 TimecodeBase::clear ()
96 {
97         checked_set (_hours, wxT (""));
98         checked_set (_minutes, wxT (""));
99         checked_set (_seconds, wxT (""));
100         checked_set (_frames, wxT (""));
101         checked_set (_fixed, wxT (""));
102 }
103
104 void
105 TimecodeBase::changed ()
106 {
107         if (_set_button) {
108                 _set_button->Enable (true);
109         }
110 }
111
112 void
113 TimecodeBase::set_clicked ()
114 {
115         Changed ();
116         if (_set_button) {
117                 _set_button->Enable (false);
118         }
119 }
120
121 void
122 TimecodeBase::set_editable (bool e)
123 {
124         _editable->Show (e);
125         _fixed->Show (!e);
126         _sizer->Layout ();
127 }
128
129 wxSize
130 TimecodeBase::size (wxWindow* parent)
131 {
132         wxClientDC dc (parent);
133         wxSize size = dc.GetTextExtent (wxT ("9999"));
134         size.SetHeight (-1);
135         return size;
136 }