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