Include trimming.
[dcpomatic.git] / src / wx / timecode.cc
1 /*
2     Copyright (C) 2013-2014 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 "lib/util.h"
21 #include "timecode.h"
22 #include "wx_util.h"
23 #include <iostream>
24
25 using std::string;
26 using std::cout;
27
28 TimecodeBase::TimecodeBase (wxWindow* parent)
29         : wxPanel (parent)
30 {
31         wxSize const s = TimecodeBase::size (parent);
32
33         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
34         wxArrayString list;
35
36         wxString n (wxT ("0123456789"));
37         for (size_t i = 0; i < n.Length(); ++i) {
38                 list.Add (n[i]);
39         }
40
41         validator.SetIncludes (list);
42
43         _sizer = new wxBoxSizer (wxHORIZONTAL);
44
45         _editable = new wxPanel (this);
46         wxSizer* editable_sizer = new wxBoxSizer (wxHORIZONTAL);
47         _hours = new wxTextCtrl (_editable, wxID_ANY, wxT(""), wxDefaultPosition, s, 0, validator);
48         _hours->SetMaxLength (2);
49         editable_sizer->Add (_hours);
50         add_label_to_sizer (editable_sizer, _editable, wxT (":"), false);
51         _minutes = new wxTextCtrl (_editable, wxID_ANY, wxT(""), wxDefaultPosition, s, 0, validator);
52         _minutes->SetMaxLength (2);
53         editable_sizer->Add (_minutes);
54         add_label_to_sizer (editable_sizer, _editable, wxT (":"), false);
55         _seconds = new wxTextCtrl (_editable, wxID_ANY, wxT(""), wxDefaultPosition, s, 0, validator);
56         _seconds->SetMaxLength (2);
57         editable_sizer->Add (_seconds);
58         add_label_to_sizer (editable_sizer, _editable, wxT (":"), false);
59         _frames = new wxTextCtrl (_editable, wxID_ANY, wxT(""), wxDefaultPosition, s, 0, validator);
60         _frames->SetMaxLength (2);
61         editable_sizer->Add (_frames);
62         _set_button = new wxButton (_editable, wxID_ANY, _("Set"));
63         editable_sizer->Add (_set_button, 0, wxLEFT | wxRIGHT, 8);
64         _editable->SetSizerAndFit (editable_sizer);
65         _sizer->Add (_editable);
66
67         _fixed = add_label_to_sizer (_sizer, this, wxT ("42"), false);
68
69         _hours->Bind      (wxEVT_COMMAND_TEXT_UPDATED,   boost::bind (&TimecodeBase::changed, this));
70         _minutes->Bind    (wxEVT_COMMAND_TEXT_UPDATED,   boost::bind (&TimecodeBase::changed, this));
71         _seconds->Bind    (wxEVT_COMMAND_TEXT_UPDATED,   boost::bind (&TimecodeBase::changed, this));
72         _frames->Bind     (wxEVT_COMMAND_TEXT_UPDATED,   boost::bind (&TimecodeBase::changed, this));
73         _set_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimecodeBase::set_clicked, this));
74
75         _set_button->Enable (false);
76
77         set_editable (true);
78
79         SetSizerAndFit (_sizer);
80 }
81
82 void
83 TimecodeBase::clear ()
84 {
85         checked_set (_hours, wxT (""));
86         checked_set (_minutes, wxT (""));
87         checked_set (_seconds, wxT (""));
88         checked_set (_frames, wxT (""));
89         checked_set (_fixed, wxT (""));
90 }
91
92 void
93 TimecodeBase::changed ()
94 {
95         _set_button->Enable (true);
96 }
97
98 void
99 TimecodeBase::set_clicked ()
100 {
101         Changed ();
102         _set_button->Enable (false);
103 }
104
105 void
106 TimecodeBase::set_editable (bool e)
107 {
108         _editable->Show (e);
109         _fixed->Show (!e);
110         _sizer->Layout ();
111 }
112
113 wxSize
114 TimecodeBase::size (wxWindow* parent)
115 {
116         wxClientDC dc (parent);
117         wxSize size = dc.GetTextExtent (wxT ("9999"));
118         size.SetHeight (-1);
119         return size;
120 }