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