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