Move raw_convert into libdcp.
[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 <dcp/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 using dcp::raw_convert;
36
37 TimePicker::TimePicker (wxWindow* parent, wxDateTime time)
38         : wxPanel (parent)
39         , _block_update (false)
40 {
41         wxClientDC dc (parent);
42         wxSize size = dc.GetTextExtent (wxT ("9999"));
43         size.SetHeight (-1);
44
45         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
46         _hours = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
47         _hours->SetMaxLength (2);
48         sizer->Add (_hours, 0);
49         _hours_spin = new wxSpinButton (this, wxID_ANY);
50         sizer->Add (_hours_spin, 0, wxLEFT | wxRIGHT, 2);
51         sizer->Add (new wxStaticText (this, wxID_ANY, wxT (":")), 0, wxALIGN_CENTER_VERTICAL);
52         _minutes = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, size);
53         _minutes->SetMaxLength (2);
54         sizer->Add (_minutes, 0);
55         _minutes_spin = new wxSpinButton (this, wxID_ANY);
56         sizer->Add (_minutes_spin, 0, wxLEFT | wxRIGHT, 2);
57
58         SetSizerAndFit (sizer);
59
60         _minutes->MoveAfterInTabOrder (_hours);
61
62         _hours_spin->SetValue (time.GetHour ());
63         _hours_spin->SetRange (0, 23);
64         _minutes_spin->SetValue (time.GetMinute ());
65         _minutes_spin->SetRange (0, 59);
66
67         update_text ();
68
69         _hours->Bind (wxEVT_COMMAND_TEXT_UPDATED, (bind (&TimePicker::update_spin, this)));
70         _minutes->Bind (wxEVT_COMMAND_TEXT_UPDATED, (bind (&TimePicker::update_spin, this)));
71         _hours_spin->Bind (wxEVT_SPIN, bind (&TimePicker::update_text, this));
72         _minutes_spin->Bind (wxEVT_SPIN, bind (&TimePicker::update_text, this));
73 }
74
75 void
76 TimePicker::update_text ()
77 {
78         if (_block_update) {
79                 return;
80         }
81
82         _block_update = true;
83
84         _hours->SetValue (wxString::Format ("%d", _hours_spin->GetValue ()));
85         _minutes->SetValue (wxString::Format ("%02d", _minutes_spin->GetValue ()));
86
87         _block_update = false;
88
89         Changed ();
90 }
91
92 void
93 TimePicker::update_spin ()
94 {
95         if (_block_update) {
96                 return;
97         }
98
99         _block_update = true;
100         _hours_spin->SetValue (raw_convert<int> (wx_to_std (_hours->GetValue())));
101         _minutes_spin->SetValue (raw_convert<int> (wx_to_std (_minutes->GetValue())));
102         _block_update = false;
103
104         Changed ();
105 }
106
107 int
108 TimePicker::hours () const
109 {
110         return _hours_spin->GetValue();
111 }
112
113 int
114 TimePicker::minutes () const
115 {
116         return _minutes_spin->GetValue();
117 }