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