6c5bd7e3b56a7fbf2bbfcf6d168ff5849b43b09e
[dcpomatic.git] / src / wx / kdm_timing_panel.cc
1 /*
2     Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "kdm_timing_panel.h"
21 #include "wx_util.h"
22 #include <wx/datectrl.h>
23 #include <wx/timectrl.h>
24 #include <wx/dateevt.h>
25
26 using boost::bind;
27
28 KDMTimingPanel::KDMTimingPanel (wxWindow* parent)
29         : wxPanel (parent, wxID_ANY)
30 {
31         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
32
33         wxFlexGridSizer* table = new wxFlexGridSizer (6, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
34         add_label_to_sizer (table, this, _("From"), true);
35         wxDateTime from;
36         from.SetToCurrent ();
37         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from);
38         table->Add (_from_date, 1, wxEXPAND);
39         _from_time = new wxTimePickerCtrl (this, wxID_ANY, from);
40         table->Add (_from_time, 1, wxEXPAND);
41
42         add_label_to_sizer (table, this, _("until"), true);
43         wxDateTime to = from;
44         /* 1 week from now */
45         to.Add (wxDateSpan (0, 0, 1, 0));
46         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to);
47         table->Add (_until_date, 1, wxEXPAND);
48         _until_time = new wxTimePickerCtrl (this, wxID_ANY, to);
49         table->Add (_until_time, 1, wxEXPAND);
50
51         overall_sizer->Add (table);
52
53         _warning = new wxStaticText (this, wxID_ANY, wxT (""));
54         overall_sizer->Add (_warning, 0, wxTOP, DCPOMATIC_SIZER_GAP);
55         wxFont font = _warning->GetFont();
56         font.SetStyle(wxFONTSTYLE_ITALIC);
57         font.SetPointSize(font.GetPointSize() - 1);
58         _warning->SetForegroundColour (wxColour (255, 0, 0));
59         _warning->SetFont(font);
60
61         _from_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
62         _until_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
63         _from_time->Bind (wxEVT_TIME_CHANGED, bind (&KDMTimingPanel::changed, this));
64         _until_time->Bind (wxEVT_TIME_CHANGED, bind (&KDMTimingPanel::changed, this));
65
66         SetSizer (overall_sizer);
67 }
68
69 boost::posix_time::ptime
70 KDMTimingPanel::from () const
71 {
72         return posix_time (_from_date, _from_time);
73 }
74
75 boost::posix_time::ptime
76 KDMTimingPanel::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
77 {
78         wxDateTime const date = date_picker->GetValue ();
79         wxDateTime const time = time_picker->GetValue ();
80         return boost::posix_time::ptime (
81                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
82                 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
83                 );
84 }
85
86 boost::posix_time::ptime
87 KDMTimingPanel::until () const
88 {
89         return posix_time (_until_date, _until_time);
90 }
91
92 bool
93 KDMTimingPanel::valid () const
94 {
95         return until() > from();
96 }
97
98 void
99 KDMTimingPanel::changed () const
100 {
101         if (valid ()) {
102                 _warning->SetLabel (wxT (""));
103         } else {
104                 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));
105         }
106
107         TimingChanged ();
108 }