Remove seconds from KDM time period specification (#819).
[dcpomatic.git] / src / wx / time_picker.cc
1 /*
2     Copyright (C) 2016 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 "lib/raw_convert.h"
23 #include <wx/spinctrl.h>
24 #include <boost/bind.hpp>
25 #include <iomanip>
26
27 using std::setfill;
28 using std::setw;
29 using std::min;
30 using std::max;
31 using std::string;
32 using std::cout;
33 using boost::bind;
34
35 TimePicker::TimePicker (wxWindow* parent, wxDateTime time)
36         : wxPanel (parent)
37         , _block_update (false)
38 {
39         wxClientDC dc (parent);
40         wxSize size = dc.GetTextExtent (wxT ("9999"));
41         size.SetHeight (-1);
42
43         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
44         _hours = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
45         _hours->SetMaxLength (2);
46         sizer->Add (_hours, 0);
47         _hours_spin = new wxSpinButton (this, wxID_ANY);
48         sizer->Add (_hours_spin, 0, wxLEFT | wxRIGHT, 2);
49         sizer->Add (new wxStaticText (this, wxID_ANY, wxT (":")), 0, wxALIGN_CENTER_VERTICAL);
50         _minutes = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
51         _minutes->SetMaxLength (2);
52         sizer->Add (_minutes, 0);
53         _minutes_spin = new wxSpinButton (this, wxID_ANY);
54         sizer->Add (_minutes_spin, 0, wxLEFT | wxRIGHT, 2);
55
56         SetSizerAndFit (sizer);
57
58         _minutes->MoveAfterInTabOrder (_hours);
59
60         _hours_spin->SetValue (time.GetHour ());
61         _hours_spin->SetRange (0, 23);
62         _minutes_spin->SetValue (time.GetMinute ());
63         _minutes_spin->SetRange (0, 59);
64
65         update_text ();
66
67         _hours->Bind (wxEVT_COMMAND_TEXT_UPDATED, (bind (&TimePicker::update_spin, this)));
68         _minutes->Bind (wxEVT_COMMAND_TEXT_UPDATED, (bind (&TimePicker::update_spin, this)));
69         _hours_spin->Bind (wxEVT_SPIN, bind (&TimePicker::update_text, this));
70         _minutes_spin->Bind (wxEVT_SPIN, bind (&TimePicker::update_text, this));
71 }
72
73 void
74 TimePicker::update_text ()
75 {
76         if (_block_update) {
77                 return;
78         }
79
80         _block_update = true;
81
82         _hours->SetValue (wxString (raw_convert<string> (_hours_spin->GetValue ())));
83
84         SafeStringStream m;
85         m << setfill('0') << setw(2) << _minutes_spin->GetValue();
86         _minutes->SetValue (wxString (m.str()));
87
88         _block_update = false;
89
90         Changed ();
91 }
92
93 void
94 TimePicker::update_spin ()
95 {
96         if (_block_update) {
97                 return;
98         }
99
100         _block_update = true;
101         _hours_spin->SetValue (raw_convert<int> (_hours->GetValue().ToStdString()));
102         _minutes_spin->SetValue (raw_convert<int> (_minutes->GetValue().ToStdString()));
103         _block_update = false;
104
105         Changed ();
106 }
107
108 int
109 TimePicker::hours () const
110 {
111         return _hours_spin->GetValue();
112 }
113
114 int
115 TimePicker::minutes () const
116 {
117         return _minutes_spin->GetValue();
118 }