Tidying.
[dcpomatic.git] / src / wx / time_picker.cc
1 /*
2     Copyright (C) 2016-2020 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
22 #include "static_text.h"
23 #include "time_picker.h"
24 #include "wx_util.h"
25 #include <dcp/locale_convert.h>
26 #include <wx/spinctrl.h>
27 #include <boost/bind/bind.hpp>
28 #include <iomanip>
29
30
31 using std::cout;
32 using std::max;
33 using std::min;
34 using std::setfill;
35 using std::setw;
36 using std::string;
37 using boost::bind;
38 using dcp::locale_convert;
39
40
41 TimePicker::TimePicker (wxWindow* parent)
42         : wxPanel (parent)
43 {
44
45 }
46
47
48 TimePickerSpin::TimePickerSpin (wxWindow* parent, wxDateTime time)
49         : TimePicker (parent)
50 {
51         wxClientDC dc (parent);
52         wxSize size = dc.GetTextExtent (wxT ("9999999"));
53         size.SetHeight (-1);
54
55         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
56         _hours = new wxSpinCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
57         sizer->Add (_hours, 1, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
58         sizer->Add (new StaticText(this, wxT(":")), 0, wxALIGN_CENTER_VERTICAL);
59         _minutes = new wxSpinCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
60         sizer->Add (_minutes, 1, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
61
62         SetSizerAndFit (sizer);
63
64         _minutes->MoveAfterInTabOrder (_hours);
65
66         _hours->SetValue (time.GetHour ());
67         _hours->SetRange (0, 23);
68         _minutes->SetValue (time.GetMinute ());
69         _minutes->SetRange (0, 59);
70
71         _hours->Bind (wxEVT_SPINCTRL, (bind (&TimePickerSpin::changed, this)));
72         _minutes->Bind (wxEVT_SPINCTRL, (bind (&TimePickerSpin::changed, this)));
73 }
74
75
76 void
77 TimePickerSpin::changed ()
78 {
79         Changed ();
80 }
81
82
83 int
84 TimePickerSpin::hours () const
85 {
86         return _hours->GetValue();
87 }
88
89
90 int
91 TimePickerSpin::minutes () const
92 {
93         return _minutes->GetValue();
94 }
95
96
97 TimePickerText::TimePickerText (wxWindow* parent, wxDateTime time)
98         : TimePicker (parent)
99 {
100         wxClientDC dc (parent);
101         wxSize size = dc.GetTextExtent (wxT("99999"));
102         size.SetHeight (-1);
103
104         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
105         _hours = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
106         sizer->Add (_hours, 1, wxEXPAND | wxLEFT, DCPOMATIC_SIZER_GAP);
107         sizer->Add (new StaticText (this, wxT (":")), 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, 4);
108         _minutes = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
109         sizer->Add (_minutes, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
110
111         SetSizerAndFit (sizer);
112
113         _minutes->MoveAfterInTabOrder (_hours);
114
115         _hours->SetValue (wxString::Format("%d", time.GetHour()));
116         _minutes->SetValue (wxString::Format("%d", time.GetMinute()));
117
118         _hours->Bind (wxEVT_TEXT, (bind(&TimePickerText::changed, this)));
119         _minutes->Bind (wxEVT_TEXT, (bind(&TimePickerText::changed, this)));
120 }
121
122
123 void
124 TimePickerText::changed ()
125 {
126         Changed ();
127 }
128
129
130 int
131 TimePickerText::hours () const
132 {
133         return max(0, min(23, wxAtoi(_hours->GetValue())));
134 }
135
136
137 int
138 TimePickerText::minutes () const
139 {
140         return max(0, min(59, wxAtoi(_minutes->GetValue())));
141 }
142
143
144