51a99627c902d0260a5eb4e73f4e61866424bd6a
[dcpomatic.git] / src / wx / kdm_timing_panel.cc
1 /*
2     Copyright (C) 2015-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 "kdm_timing_panel.h"
22 #include "wx_util.h"
23 #include "time_picker.h"
24 #include "static_text.h"
25 #include <wx/datectrl.h>
26 #include <wx/dateevt.h>
27
28 using std::cout;
29 using boost::bind;
30
31 KDMTimingPanel::KDMTimingPanel (wxWindow* parent)
32         : wxPanel (parent, wxID_ANY)
33 {
34         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
35
36 #ifdef __WXGTK3__
37         /* wxDatePickerCtrl is too small with the GTK3 backend so we need to make it bigger with some fudge factors */
38         wxClientDC dc (parent);
39         wxSize size = dc.GetTextExtent (wxT("99/99/9999"));
40         size.SetWidth (size.GetWidth() * 1.75);
41         size.SetHeight (-1);
42 #else
43         wxSize size = wxDefaultSize;
44 #endif
45
46         wxSizer* table = new wxBoxSizer (wxHORIZONTAL);
47         add_label_to_sizer (table, this, _("From"), true);
48         wxDateTime from;
49         from.SetToCurrent ();
50         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from, wxDefaultPosition, size);
51         table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL);
52
53 #ifdef __WXGTK3__
54         _from_time = new TimePickerText (this, from);
55 #else
56         _from_time = new TimePickerSpin (this, from);
57 #endif
58
59 #ifdef DCPOMATIC_OSX
60         /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
61         table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL | wxTOP, 4);
62 #else
63         table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL);
64 #endif
65
66         add_label_to_sizer (table, this, _("until"), true);
67         wxDateTime to = from;
68         /* 1 week from now */
69         to.Add (wxDateSpan (0, 0, 1, 0));
70         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to, wxDefaultPosition, size);
71         table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL);
72
73 #ifdef __WXGTK3__
74         _until_time = new TimePickerText (this, to);
75 #else
76         _until_time = new TimePickerSpin (this, to);
77 #endif
78
79 #ifdef DCPOMATIC_OSX
80         table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL | wxTOP, 4);
81 #else
82         table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL);
83 #endif
84
85         overall_sizer->Add (table);
86
87         _warning = new StaticText (this, wxT(""));
88         overall_sizer->Add (_warning, 0, wxTOP, DCPOMATIC_SIZER_GAP);
89         wxFont font = _warning->GetFont();
90         font.SetStyle(wxFONTSTYLE_ITALIC);
91         font.SetPointSize(font.GetPointSize() - 1);
92         _warning->SetForegroundColour (wxColour (255, 0, 0));
93         _warning->SetFont(font);
94
95         /* I said I've been to the year 3000.  Not much has changed but they live underwater.  And your In-in-in-interop DCP
96            is pretty fine.
97          */
98         _from_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
99         _until_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
100
101         _from_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
102         _until_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
103         _from_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
104         _until_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
105
106         SetSizer (overall_sizer);
107 }
108
109 boost::posix_time::ptime
110 KDMTimingPanel::from () const
111 {
112         return posix_time (_from_date, _from_time);
113 }
114
115 boost::posix_time::ptime
116 KDMTimingPanel::posix_time (wxDatePickerCtrl* date_picker, TimePicker* time_picker)
117 {
118         wxDateTime const date = date_picker->GetValue ();
119         return boost::posix_time::ptime (
120                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
121                 boost::posix_time::time_duration (time_picker->hours(), time_picker->minutes(), 0)
122                 );
123 }
124
125 boost::posix_time::ptime
126 KDMTimingPanel::until () const
127 {
128         return posix_time (_until_date, _until_time);
129 }
130
131 bool
132 KDMTimingPanel::valid () const
133 {
134         return until() > from();
135 }
136
137 void
138 KDMTimingPanel::changed () const
139 {
140         if (valid ()) {
141                 _warning->SetLabel (wxT (""));
142         } else {
143                 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));
144         }
145
146         TimingChanged ();
147 }